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.

158 lines
3.5 KiB

  1. -- 重新实现TextField
  2. cc.TextField = class("TextField")
  3. cc.TextField.ClassName = "TextField"
  4. function cc.TextField:extend(node)
  5. cc.Text:extend(node);
  6. -- 把lua的类函数扩展给node(C++对象)
  7. node:extendClass(cc.TextField);
  8. node:setName("TextField")
  9. node.MaxLengthEnabled = false;
  10. node.MaxLength = 10;
  11. node.PasswordEnabled = false;
  12. node.PasswordStyleText = "*";
  13. node.TouchAreaEnabled = false;
  14. node.TouchArea = cc.rect(0,0,200,50);
  15. node.PlaceHolder = "";
  16. node.Text = "";
  17. node.Listener = nil;
  18. local function onTouchBegan(touch , event)
  19. if node.TouchAreaEnabled then
  20. local nsp = node:convertToNodeSpace2D(touch:getLocation());
  21. return cc.rectContainsPoint(node.TouchArea , nsp);
  22. else
  23. return node:hitTest(touch:getLocation());
  24. end
  25. end
  26. local function onTouchMoved(touch , event)
  27. end
  28. local function onTouchEnded(touch , event)
  29. node:onInput();
  30. end
  31. local function onTouchCancelled(touch , event)
  32. end
  33. -- 侦听点击通知
  34. node:registerTouch(onTouchBegan , onTouchMoved , onTouchEnded , onTouchCancelled);
  35. end
  36. function cc.TextField:addEventListener(listener)
  37. self.Listener = listener;
  38. end
  39. -- 弹出输入框
  40. function cc.TextField:onInput()
  41. local function onInputEnd(text)
  42. if not tolua.isnull(self) then
  43. self:setText(text);
  44. if self.Listener then
  45. self.Listener(self , cc.TextFiledEventType.enter);
  46. end
  47. end
  48. end
  49. if self.MaxLengthEnabled then
  50. --deviceInputBox("" , self.Text , self.MaxLength , onInputEnd);
  51. deviceInputBox("" , self.Text , 999999 , onInputEnd);
  52. else
  53. deviceInputBox("" , self.Text , 999999 , onInputEnd);
  54. end
  55. end
  56. function cc.TextField:create()
  57. local layer = cc.Text:create();
  58. self:extend(layer);
  59. return layer;
  60. end
  61. function cc.TextField:setFontConfig(ttfConfig)
  62. self.OriginTexColor = ttfConfig.texColor;
  63. cc.Text.setFontConfig(self , ttfConfig);
  64. end
  65. function cc.TextField:setText(text)
  66. self.Text = text;
  67. local fontConfig = self:getFontConfig();
  68. if text == "" then
  69. self.OriginTexColor = fontConfig.texColor;
  70. fontConfig.texColor = cc.c4b(149, 149, 149, 255);
  71. self:setString(self.PlaceHolder);
  72. else
  73. if self.OriginTexColor then
  74. fontConfig.texColor = self.OriginTexColor;
  75. end
  76. if self.PasswordEnabled then
  77. local text16 = Utf16:create(text);
  78. local size = text16:len();
  79. text = string.rep(self.PasswordStyleText , size);
  80. end
  81. self:setString(text);
  82. end
  83. self:setFontConfig(fontConfig);
  84. end
  85. function cc.TextField:getText()
  86. return self.Text;
  87. end
  88. function cc.TextField:getString()
  89. return self.Text;
  90. end
  91. function cc.TextField:setTouchArea(size)
  92. self.TouchArea = size;
  93. end
  94. function cc.TextField:getTouchArea()
  95. return self.TouchArea
  96. end
  97. function cc.TextField:setTouchAreaEnabled(enable)
  98. self.TouchAreaEnabled = enable;
  99. end
  100. function cc.TextField:isTouchAreaEnabled()
  101. return self.TouchAreaEnabled
  102. end
  103. function cc.TextField:setPlaceHolder(value)
  104. self.PlaceHolder = value;
  105. end
  106. function cc.TextField:getPlaceHolder()
  107. return self.PlaceHolder
  108. end
  109. function cc.TextField:setMaxLengthEnabled(enable)
  110. self.MaxLengthEnabled = enable;
  111. end
  112. function cc.TextField:isMaxLengthEnabled()
  113. return self.MaxLengthEnabled
  114. end
  115. function cc.TextField:setMaxLength(length)
  116. self.MaxLength = length;
  117. end
  118. function cc.TextField:getMaxLength()
  119. return self.MaxLength
  120. end
  121. function cc.TextField:setPasswordEnabled(enable)
  122. self.PasswordEnabled = enable;
  123. end
  124. function cc.TextField:isPasswordEnabled()
  125. return self.PasswordEnabled
  126. end
  127. function cc.TextField:setPasswordStyleText(styleText)
  128. self.PasswordStyleText = styleText;
  129. end
  130. function cc.TextField:getPasswordStyleText()
  131. return self.PasswordStyleText
  132. end