You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

244 lines
6.6 KiB

  1. function cc.TextField:extend(node)
  2. cc.Widget:extend(node);
  3. node:setName("TextField")
  4. end
  5. cc.TextField.ClassName = "TextField"
  6. cc.TextField.getText = cc.TextField.getString
  7. -- android和ios使用弹出式输入框
  8. if cc.Application:getInstance():getTargetPlatform() ~= cc.PLATFORM_OS_WINDOWS
  9. and cc.Application:getInstance():getTargetPlatform() ~= cc.PLATFORM_OS_WINRT
  10. and cc.Application:getInstance():getTargetPlatform() ~= cc.PLATFORM_OS_WP8
  11. then
  12. --require("luaScript.Tools.Widgets.UITextFieldDevice")
  13. function cc.TextField:createIME()
  14. --电脑不用发送事件,手机需要
  15. if cc.Application:getInstance():getTargetPlatform() ~= 0 then
  16. self:dispatchEvent(cc.TextFiledEventType.attach_with_ime);
  17. end
  18. local function onInputEnd(text)
  19. if not tolua.isnull(self) then
  20. self:setText(text);
  21. self:dispatchEvent(cc.TextFiledEventType.enter);
  22. end
  23. end
  24. if self:isMaxLengthEnabled() then
  25. deviceInputBox("" , self:getString() , 999999 , onInputEnd);
  26. else
  27. deviceInputBox("" , self:getString() , 999999 , onInputEnd);
  28. end
  29. end
  30. function cc.TextField:destroyIME()
  31. end
  32. cc.TextField:setFunc(cc.TextField.createIME , cc.TextField.destroyIME);
  33. end
  34. function cc.TextField:saveToXmlNode(xmlNode)
  35. cc.Widget.saveToXmlNode(self , xmlNode);
  36. xmlNode.PlaceHolder = self:getPlaceHolder();
  37. xmlNode.Text = self:getString();
  38. xmlNode.FontConfig = self:getFontConfig();
  39. xmlNode.UseTouchArea = self:isTouchAreaEnabled();
  40. if xmlNode.UseTouchArea then
  41. xmlNode.TouchArea = self:getTouchArea();
  42. end
  43. xmlNode.MaxLengthEnabled = self:isMaxLengthEnabled();
  44. xmlNode.MaxLength = self:getMaxLength();
  45. xmlNode.PasswordEnabled = self:isPasswordEnabled();
  46. xmlNode.PasswordStyleText = self:getPasswordStyleText();
  47. xmlNode.CharPadding = self:getCharPadding();
  48. xmlNode.LinePadding = self:getLinePadding();
  49. end
  50. function cc.TextField:loadFromXmlNode(xmlNode)
  51. cc.Widget.loadFromXmlNode(self , xmlNode);
  52. self:setPlaceHolder(xmlNode.PlaceHolder);
  53. local fontConfig = xmlNode.FontConfig
  54. if not fontConfig then
  55. fontConfig = {}
  56. fontConfig.fontFilePath = "res/default/msyh.ttc"
  57. fontConfig.fontSize = xmlNode.FontSize
  58. fontConfig.glyphs = cc.GLYPHCOLLECTION_DYNAMIC
  59. fontConfig.customGlyphs = nil
  60. fontConfig.distanceFieldEnabled = false
  61. fontConfig.outlineSize = 0
  62. fontConfig.texColor = cc.c4b(255, 255, 255, 255);
  63. end
  64. self:setFontConfig(fontConfig);
  65. local useTouchArea = xmlNode.UseTouchArea;
  66. self:setTouchAreaEnabled(useTouchArea);
  67. if useTouchArea then
  68. if xmlNode.TouchSize then
  69. self:setTouchArea(cc.rect(0,0,xmlNode.TouchSize.width , xmlNode.TouchSize.height));
  70. else
  71. self:setTouchArea(xmlNode.TouchArea);
  72. end
  73. end
  74. self:setMaxLengthEnabled(xmlNode.MaxLengthEnabled);
  75. self:setMaxLength(xmlNode.MaxLength);
  76. self:setPasswordEnabled(xmlNode.PasswordEnabled);
  77. self:setPasswordStyleText(xmlNode.PasswordStyleText);
  78. if xmlNode.CharPadding then
  79. self:setCharPadding(xmlNode.CharPadding);
  80. self:setLinePadding(xmlNode.LinePadding);
  81. end
  82. self:setText(xmlNode.Text);
  83. end
  84. -- 收集ui的文本,返回文本table
  85. function cc.TextField:collectText(xmlNode)
  86. return {TextTranslator:collect(self:getString());TextTranslator:collect(self:getPlaceHolder());};
  87. end
  88. -- 翻译xmlNode里的文本,dict是字典表
  89. function cc.TextField:applyDict(dict)
  90. local translated = dict[TextTranslator:collect(self:getString())];
  91. if translated then
  92. self:setText(TextTranslator:translate(self:getString() , translated));
  93. end
  94. translated = dict[TextTranslator:collect(self:getPlaceHolder())];
  95. if translated then
  96. self:setPlaceHolder(TextTranslator:translate(self:getPlaceHolder() , translated));
  97. end
  98. end
  99. function cc.TextField:updateTextLabel()
  100. if self.Label == nil then
  101. self.Label = tolua.cast(self:getVirtualRenderer() , "cc.Label");
  102. end
  103. end
  104. function cc.TextField:setFontConfig(ttfConfig)
  105. self:updateTextLabel()
  106. local label = self.Label;
  107. self.FontConfig = ttfConfig;
  108. label:setFontConfig(ttfConfig)
  109. end
  110. function cc.TextField:getFontConfig()
  111. self:updateTextLabel()
  112. local label = self.Label;
  113. local config = label:getFontConfig()
  114. if self.FontConfig then
  115. config.texColor = self.FontConfig.texColor;
  116. end
  117. return config;
  118. end
  119. function cc.TextField:setCharPadding(padding)
  120. self:updateTextLabel()
  121. local label = self.Label;
  122. label:setCharPadding(padding)
  123. end
  124. function cc.TextField:getCharPadding()
  125. self:updateTextLabel()
  126. local label = self.Label;
  127. return label:getCharPadding()
  128. end
  129. function cc.TextField:setLinePadding(padding)
  130. self:updateTextLabel()
  131. local label = self.Label;
  132. label:setLinePadding(padding)
  133. end
  134. function cc.TextField:getLinePadding()
  135. self:updateTextLabel()
  136. local label = self.Label;
  137. return label:getLinePadding()
  138. end
  139. function cc.TextField:setPadding(x , y)
  140. self:updateTextLabel()
  141. local label = self.Label;
  142. label:setPadding(x , y)
  143. end
  144. function cc.TextField:copyProperties(source)
  145. cc.Widget.copyProperties(self , source);
  146. self:setFontConfig(source:getFontConfig());
  147. end
  148. function cc.TextField:createNode()
  149. local layer = cc.TextField:create();
  150. layer.Label = tolua.cast(layer:getVirtualRenderer() , "cc.Label");
  151. cc.TextField:extend(layer);
  152. return layer;
  153. end
  154. -- 设置默认值
  155. function cc.TextField:setDefaults()
  156. local ttfConfig = {}
  157. ttfConfig.fontFilePath = "res/default/msyh.ttc"
  158. ttfConfig.fontSize = 30
  159. ttfConfig.glyphs = cc.GLYPHCOLLECTION_DYNAMIC
  160. ttfConfig.customGlyphs = nil
  161. ttfConfig.distanceFieldEnabled = false
  162. ttfConfig.outlineSize = 0
  163. ttfConfig.texColor = cc.c4b(255, 255, 255, 255);
  164. self:setFontConfig(ttfConfig);
  165. self:setText("Text Field");
  166. self:setMaxLength(10);
  167. self:setPasswordStyleText("*");
  168. self:setTouchArea(cc.rect(0,0,200,50));
  169. self:loadDefaults();
  170. -- 默认需要响应touch事件
  171. self:setTouchEnabled(true)
  172. end
  173. -- 载入时默认设置
  174. function cc.TextField:loadDefaults()
  175. self:addProtectedChild(self:createTouchSizeDrawer());
  176. end
  177. -- 创建触摸区域
  178. function cc.TextField:createTouchSizeDrawer()
  179. local glNode = gl.glNodeCreate()
  180. glNode:setAnchorPoint(cc.p(0, 0))
  181. local function primitivesDraw(transform, transformUpdated)
  182. if self:isTouchAreaEnabled() then
  183. -- 绑定相机、世界矩阵
  184. cc.DrawPrimitives.setCamera(self:getActiveCamera());
  185. cc.DrawPrimitives.setWorldTransform(transform);
  186. -- 画矩形
  187. gl.lineWidth(1)
  188. cc.DrawPrimitives.drawColor4B(255,255,255,255)
  189. local rect = self:getTouchArea();
  190. cc.DrawPrimitives.drawRect(cc.p(rect.x , rect.y), cc.p(rect.x + rect.width , rect.y + rect.height))
  191. -- 状态恢复
  192. gl.lineWidth(1)
  193. cc.DrawPrimitives.drawColor4B(255,255,255,255)
  194. cc.DrawPrimitives.setPointSize(1)
  195. end
  196. end
  197. glNode:registerScriptDrawHandler(primitivesDraw)
  198. return glNode;
  199. end