|
- function cc.TextField:extend(node)
- cc.Widget:extend(node);
- node:setName("TextField")
- end
- cc.TextField.ClassName = "TextField"
- cc.TextField.getText = cc.TextField.getString
-
- -- android和ios使用弹出式输入框
- if cc.Application:getInstance():getTargetPlatform() ~= cc.PLATFORM_OS_WINDOWS
- and cc.Application:getInstance():getTargetPlatform() ~= cc.PLATFORM_OS_WINRT
- and cc.Application:getInstance():getTargetPlatform() ~= cc.PLATFORM_OS_WP8
- then
- --require("luaScript.Tools.Widgets.UITextFieldDevice")
-
- function cc.TextField:createIME()
- --电脑不用发送事件,手机需要
- if cc.Application:getInstance():getTargetPlatform() ~= 0 then
- self:dispatchEvent(cc.TextFiledEventType.attach_with_ime);
- end
- local function onInputEnd(text)
- if not tolua.isnull(self) then
- self:setText(text);
- self:dispatchEvent(cc.TextFiledEventType.enter);
- end
- end
- if self:isMaxLengthEnabled() then
- deviceInputBox("" , self:getString() , 999999 , onInputEnd);
- else
- deviceInputBox("" , self:getString() , 999999 , onInputEnd);
- end
- end
-
-
- function cc.TextField:destroyIME()
-
- end
-
- cc.TextField:setFunc(cc.TextField.createIME , cc.TextField.destroyIME);
- end
-
-
- function cc.TextField:saveToXmlNode(xmlNode)
- cc.Widget.saveToXmlNode(self , xmlNode);
-
-
- xmlNode.PlaceHolder = self:getPlaceHolder();
- xmlNode.Text = self:getString();
- xmlNode.FontConfig = self:getFontConfig();
- xmlNode.UseTouchArea = self:isTouchAreaEnabled();
- if xmlNode.UseTouchArea then
- xmlNode.TouchArea = self:getTouchArea();
- end
- xmlNode.MaxLengthEnabled = self:isMaxLengthEnabled();
- xmlNode.MaxLength = self:getMaxLength();
- xmlNode.PasswordEnabled = self:isPasswordEnabled();
- xmlNode.PasswordStyleText = self:getPasswordStyleText();
-
- xmlNode.CharPadding = self:getCharPadding();
- xmlNode.LinePadding = self:getLinePadding();
- end
-
- function cc.TextField:loadFromXmlNode(xmlNode)
- cc.Widget.loadFromXmlNode(self , xmlNode);
-
- self:setPlaceHolder(xmlNode.PlaceHolder);
-
- local fontConfig = xmlNode.FontConfig
- if not fontConfig then
- fontConfig = {}
- fontConfig.fontFilePath = "res/default/msyh.ttc"
- fontConfig.fontSize = xmlNode.FontSize
- fontConfig.glyphs = cc.GLYPHCOLLECTION_DYNAMIC
- fontConfig.customGlyphs = nil
- fontConfig.distanceFieldEnabled = false
- fontConfig.outlineSize = 0
- fontConfig.texColor = cc.c4b(255, 255, 255, 255);
- end
- self:setFontConfig(fontConfig);
-
- local useTouchArea = xmlNode.UseTouchArea;
- self:setTouchAreaEnabled(useTouchArea);
- if useTouchArea then
- if xmlNode.TouchSize then
- self:setTouchArea(cc.rect(0,0,xmlNode.TouchSize.width , xmlNode.TouchSize.height));
- else
- self:setTouchArea(xmlNode.TouchArea);
- end
- end
-
- self:setMaxLengthEnabled(xmlNode.MaxLengthEnabled);
- self:setMaxLength(xmlNode.MaxLength);
- self:setPasswordEnabled(xmlNode.PasswordEnabled);
- self:setPasswordStyleText(xmlNode.PasswordStyleText);
-
- if xmlNode.CharPadding then
- self:setCharPadding(xmlNode.CharPadding);
- self:setLinePadding(xmlNode.LinePadding);
- end
- self:setText(xmlNode.Text);
- end
-
- -- 收集ui的文本,返回文本table
- function cc.TextField:collectText(xmlNode)
- return {TextTranslator:collect(self:getString());TextTranslator:collect(self:getPlaceHolder());};
- end
-
- -- 翻译xmlNode里的文本,dict是字典表
- function cc.TextField:applyDict(dict)
-
- local translated = dict[TextTranslator:collect(self:getString())];
- if translated then
- self:setText(TextTranslator:translate(self:getString() , translated));
- end
-
-
- translated = dict[TextTranslator:collect(self:getPlaceHolder())];
- if translated then
- self:setPlaceHolder(TextTranslator:translate(self:getPlaceHolder() , translated));
- end
-
- end
-
- function cc.TextField:updateTextLabel()
- if self.Label == nil then
- self.Label = tolua.cast(self:getVirtualRenderer() , "cc.Label");
- end
- end
-
- function cc.TextField:setFontConfig(ttfConfig)
- self:updateTextLabel()
- local label = self.Label;
- self.FontConfig = ttfConfig;
- label:setFontConfig(ttfConfig)
- end
-
- function cc.TextField:getFontConfig()
- self:updateTextLabel()
- local label = self.Label;
- local config = label:getFontConfig()
- if self.FontConfig then
- config.texColor = self.FontConfig.texColor;
- end
- return config;
- end
-
-
- function cc.TextField:setCharPadding(padding)
- self:updateTextLabel()
- local label = self.Label;
- label:setCharPadding(padding)
- end
-
- function cc.TextField:getCharPadding()
- self:updateTextLabel()
- local label = self.Label;
- return label:getCharPadding()
- end
-
- function cc.TextField:setLinePadding(padding)
- self:updateTextLabel()
- local label = self.Label;
- label:setLinePadding(padding)
- end
-
- function cc.TextField:getLinePadding()
- self:updateTextLabel()
- local label = self.Label;
- return label:getLinePadding()
- end
-
- function cc.TextField:setPadding(x , y)
- self:updateTextLabel()
- local label = self.Label;
- label:setPadding(x , y)
- end
-
-
- function cc.TextField:copyProperties(source)
- cc.Widget.copyProperties(self , source);
- self:setFontConfig(source:getFontConfig());
- end
-
-
- function cc.TextField:createNode()
- local layer = cc.TextField:create();
- layer.Label = tolua.cast(layer:getVirtualRenderer() , "cc.Label");
- cc.TextField:extend(layer);
- return layer;
- end
-
- -- 设置默认值
- function cc.TextField:setDefaults()
- local ttfConfig = {}
- ttfConfig.fontFilePath = "res/default/msyh.ttc"
- ttfConfig.fontSize = 30
- ttfConfig.glyphs = cc.GLYPHCOLLECTION_DYNAMIC
- ttfConfig.customGlyphs = nil
- ttfConfig.distanceFieldEnabled = false
- ttfConfig.outlineSize = 0
- ttfConfig.texColor = cc.c4b(255, 255, 255, 255);
-
- self:setFontConfig(ttfConfig);
-
- self:setText("Text Field");
- self:setMaxLength(10);
- self:setPasswordStyleText("*");
- self:setTouchArea(cc.rect(0,0,200,50));
- self:loadDefaults();
- -- 默认需要响应touch事件
- self:setTouchEnabled(true)
- end
-
- -- 载入时默认设置
- function cc.TextField:loadDefaults()
- self:addProtectedChild(self:createTouchSizeDrawer());
- end
-
- -- 创建触摸区域
- function cc.TextField:createTouchSizeDrawer()
- local glNode = gl.glNodeCreate()
- glNode:setAnchorPoint(cc.p(0, 0))
-
- local function primitivesDraw(transform, transformUpdated)
- if self:isTouchAreaEnabled() then
- -- 绑定相机、世界矩阵
- cc.DrawPrimitives.setCamera(self:getActiveCamera());
- cc.DrawPrimitives.setWorldTransform(transform);
-
- -- 画矩形
- gl.lineWidth(1)
- cc.DrawPrimitives.drawColor4B(255,255,255,255)
- local rect = self:getTouchArea();
- cc.DrawPrimitives.drawRect(cc.p(rect.x , rect.y), cc.p(rect.x + rect.width , rect.y + rect.height))
-
- -- 状态恢复
- gl.lineWidth(1)
- cc.DrawPrimitives.drawColor4B(255,255,255,255)
- cc.DrawPrimitives.setPointSize(1)
- end
- end
-
- glNode:registerScriptDrawHandler(primitivesDraw)
- return glNode;
- end
|