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.

194 lines
6.8 KiB

  1. local Abbreviation = {}
  2. function Abbreviation.addBackLayer(__node, __num)
  3. local num = __num or 200
  4. local bBack = cc.LayerColor:create(cc.c4b(0, 0, 0, num))
  5. __node:addChild(bBack, -1)
  6. bBack:setPosition(cc.p(0, 0))
  7. return bBack
  8. end
  9. --创建按钮
  10. function Abbreviation.createMenu(__node, __z_order, __tag, __pos, __anchorpoint)
  11. local menu = cc.Menu:create()
  12. __node:addChild(menu)
  13. menu:setLocalZOrder(__z_order or 0)
  14. menu:setTag(__tag or 0)
  15. menu:setPosition(__pos or cc.p(0,0))
  16. menu:setAnchorPoint(__anchorpoint or cc.p(0.5,0.5))
  17. return menu
  18. end
  19. --菜单栏
  20. function Abbreviation.createItemWithFile(__file, __node, __pos, __callBack, __change)
  21. assert(type(__callBack)=="function", "Abbreviation.createItemWithFile error, must have a callback")
  22. local sp = display.newSprite(__file)
  23. local sp_hover = display.newSprite(__file)
  24. sp_hover:setColor(cc.c3b(100, 100, 100))
  25. local sp_dis = display.newSprite(__file)
  26. sp_dis:setColor(cc.c3b(100, 100, 100))
  27. local item = cc.MenuItemSprite:create(sp, sp_hover, sp_dis)
  28. :onClicked(__callBack)
  29. __node:addChild(item)
  30. item:setPosition(__pos)
  31. return item
  32. end
  33. --菜单栏
  34. function Abbreviation.createItemAll(__files, __node, __pos, __callBack, __change)
  35. assert(type(__callBack)=="function", "Abbreviation.createItemAll error, must have a callback")
  36. local sp = display.newSprite(__files[1])
  37. local sp_hover = display.newSprite(__files[1])
  38. sp_hover:setColor(cc.c3b(100, 100, 100))
  39. local sp_dis = display.newSprite(__files[2])
  40. local item = cc.MenuItemSprite:create(sp, sp_hover, sp_dis)
  41. :onClicked(__callBack)
  42. __node:addChild(item)
  43. item:setPosition(__pos)
  44. return item
  45. end
  46. --菜单栏
  47. function Abbreviation.createItemFont(__node, __pos ,__str, __fontsize, __color, __disCol, __bold, __callBack)
  48. assert(type(__callBack)=="function", "Abbreviation.createItemLabel error, must have a callback")
  49. local item = cc.MenuItemFont:create(__str)
  50. :onClicked(__callBack)
  51. __node:addChild(item)
  52. item:setPosition(__pos)
  53. item:setFontSizeObj(__fontsize)
  54. item:setColor(__color)
  55. item:setDisabledColor(__disCol)
  56. local sysFont = Abbreviation.getSystemFont()
  57. if __bold then
  58. sysFont = Abbreviation.getBoldFont()
  59. end
  60. item:setFontNameObj(sysFont)
  61. return item
  62. end
  63. --
  64. function Abbreviation.createLabel(__node, __pos, __str, __fontsize, __color,__shadow)
  65. local label = cc.Label:createWithTTF(__str, "fonts/huakangziti.TTF", __fontsize)
  66. :align(display.CENTER, __pos)
  67. :addTo(__node)
  68. label:setColor(__color or display.COLOR_WHITE)
  69. if (__shadow) then
  70. label:enableShadow()
  71. end
  72. return label
  73. end
  74. --获取平台字体
  75. function Abbreviation.getSystemFont()
  76. return "res/default/msyh.ttc"
  77. end
  78. --获取平台加粗字体
  79. function Abbreviation.getBoldFont()
  80. return "res/default/msyh.ttc"
  81. end
  82. --更改显示系统字体的lable(创建新lable,并隐藏旧lable)
  83. function Abbreviation.changeSystemLabel(__oldLabel, __outLineColor)
  84. local label = cc.Label:createWithSystemFont(__oldLabel:getString(), Abbreviation.getSystemFont(), __oldLabel:getFontSize())
  85. __oldLabel:getParent():addChild(label, __oldLabel:getLocalZOrder())
  86. label:setAnchorPoint(__oldLabel:getAnchorPoint())
  87. label:setPosition(__oldLabel:getPositionX(), __oldLabel:getPositionY())
  88. label:setTextColor(__oldLabel:getTextColor())
  89. __oldLabel:setVisible(false)
  90. if (__outLineColor) then
  91. label:enableOutline(__outLineColor, 1)
  92. end
  93. return label
  94. end
  95. --改变lable大小,返回新lable,隐藏旧lable
  96. function Abbreviation.changeSystemLabelRect(__oldLabel, __size, __hAlignment, __outLineColor, __vAlignment)
  97. local hAlignment = __hAlignment or cc.TEXT_ALIGNMENT_CENTER --cc.TEXT_ALIGNMENT_LEFT==0 cc.TEXT_ALIGNMENT_CENTER==1
  98. local vAlignment = __vAlignment or cc.VERTICAL_TEXT_ALIGNMENT_CENTER --居中对齐
  99. local size = __size
  100. local label = cc.Label:createWithSystemFont(__oldLabel:getString(), Abbreviation.getSystemFont(), __oldLabel:getFontSize(), size, hAlignment, vAlignment)
  101. label:setLineBreakWithoutSpace(true)
  102. __oldLabel:getParent():addChild(label, __oldLabel:getLocalZOrder())
  103. label:setAnchorPoint(__oldLabel:getAnchorPoint())
  104. label:setPosition(__oldLabel:getPositionX(), __oldLabel:getPositionY())
  105. label:setTextColor(__oldLabel:getTextColor())
  106. __oldLabel:setVisible(false)
  107. return label
  108. end
  109. --以系统字体创建lable,并开启阴影
  110. function Abbreviation.createSystemLabel(__node, __pos, __str, __fontsize, __color, __bold, __hAlignment, __shadow)
  111. local sysFont = Abbreviation.getSystemFont()
  112. if __bold then
  113. sysFont = Abbreviation.getBoldFont()
  114. end
  115. __hAlignment = __hAlignment or display.CENTER
  116. -- local label = cc.Label:createWithSystemFont(__str, sysFont, __fontsize)
  117. local label = cc.Label:create(__str, "res/default/msyh.ttc", __fontsize)
  118. :align(__hAlignment, __pos)
  119. :addTo(__node)
  120. label:setColor(__color or display.COLOR_WHITE)
  121. if (__shadow) then
  122. label:enableShadow()
  123. end
  124. return label
  125. end
  126. --创建精灵
  127. function Abbreviation.createSprite(__file, __node, __pos, __scale9, __z_order, __tag, __anchorpoint)
  128. local x = __pos and __pos.x or 0
  129. local y = __pos and __pos.y or 0
  130. local params = {
  131. scale9 = __scale9,
  132. capInsets = cc.rect(24, 24, 3, 3)
  133. }
  134. if not __scale9 then
  135. params = nil
  136. end
  137. local sp = display.newSprite(__file, x, y, params)
  138. dump(__node, "Abbreviation.createSprite = __node")
  139. print("Abbreviation.createSprite = sp = ", sp)
  140. __node:addChild(sp)
  141. sp:setLocalZOrder(__z_order or 0)
  142. sp:setTag(__tag or 0)
  143. sp:setAnchorPoint(__anchorpoint or cc.p(0.5,0.5))
  144. return sp
  145. end
  146. --创建精灵
  147. function Abbreviation.createImageView(__file, __node, __pos, __scale9, __z_order, __tag, __anchorpoint)
  148. local x = __pos and __pos.x or 0
  149. local y = __pos and __pos.y or 0
  150. local sp = ccui.ImageView:create(__file)
  151. if __scale9 then
  152. sp:setScale9Enabled(true)
  153. sp:setCapInsets(cc.rect(23, 23, 4, 4))
  154. end
  155. sp:setPosition(cc.p(x, y))
  156. __node:addChild(sp)
  157. sp:setLocalZOrder(__z_order or 0)
  158. sp:setTag(__tag or 0)
  159. sp:setAnchorPoint(__anchorpoint or cc.p(0.5,0.5))
  160. return sp
  161. end
  162. --创建定宽label
  163. function Abbreviation.createFixWLabel(__node, __pos, __str, __fontsize, __color, __width)
  164. local label = cc.Text:createNode();
  165. label:setDefaults();
  166. local config = label:getFontConfig();
  167. config.fontSize = __fontsize or 20;
  168. label:setFontConfig(config);
  169. label:setColor(__color)
  170. label:setTextAreaSize(cc.size(__width, 0))
  171. label:setLinePadding(2)
  172. label:setAnchorPoint(cc.p(0.5, 0.5))
  173. label:setPosition(__pos)
  174. label:setText(__str or "")
  175. __node:addChild(label)
  176. return label
  177. end
  178. return Abbreviation--Abbreviation_lua--Abbreviation.lua