-
Notifications
You must be signed in to change notification settings - Fork 867
/
ui.lua
596 lines (458 loc) · 17.2 KB
/
ui.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
--[[
Copyright (c) 2011-2014 chukong-inc.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
]]
--[[--
创建和管理用户界面
]]
local ui = {}
ui.DEFAULT_TTF_FONT = "Arial"
ui.DEFAULT_TTF_FONT_SIZE = 24
ui.TEXT_ALIGN_LEFT = kCCTextAlignmentLeft
ui.TEXT_ALIGN_CENTER = kCCTextAlignmentCenter
ui.TEXT_ALIGN_RIGHT = kCCTextAlignmentRight
ui.TEXT_VALIGN_TOP = kCCVerticalTextAlignmentTop
ui.TEXT_VALIGN_CENTER = kCCVerticalTextAlignmentCenter
ui.TEXT_VALIGN_BOTTOM = kCCVerticalTextAlignmentBottom
--[[--
创建一个文字输入框,并返回 CCEditBox 对象。
可用参数:
- image: 输入框的图像,可以是图像名或者是 CCSprite9Scale 显示对象。用 display.newScale9Sprite() 创建 CCSprite9Scale 显示对象。
- imagePressed: 输入状态时输入框显示的图像(可选)
- imageDisabled: 禁止状态时输入框显示的图像(可选)
- listener: 回调函数
- size: 输入框的尺寸,用 CCSize(宽度, 高度) 创建
- x, y: 坐标(可选)
~~~ lua
local function onEdit(event, editbox)
if event == "began" then
-- 开始输入
elseif event == "changed" then
-- 输入框内容发生变化
elseif event == "ended" then
-- 输入结束
elseif event == "return" then
-- 从输入框返回
end
end
local editbox = ui.newEditBox({
image = "EditBox.png",
listener = onEdit,
size = CCSize(200, 40)
})
~~~
注意: 使用setInputFlag(0) 可设为密码输入框。
注意:构造输入框时,请使用setPlaceHolder来设定初始文本显示。setText为出现输入法后的默认文本。
注意:事件触发机制,player模拟器上与真机不同,请使用真机实测(不同ios版本貌似也略有不同)。
注意:changed事件中,需要条件性使用setText(如trim或转化大小写等),否则在某些ios版本中会造成死循环。
~~~ lua
--错误,会造成死循环
editbox:setText(string.trim(editbox:getText()))
~~~
~~~ lua
--正确,不会造成死循环
local _text = editbox:getText()
local _trimed = string.trim(_text)
if _trimed ~= _text then
editbox:setText(_trimed)
end
~~~
@param table params 参数表格对象
@return CCEditBox 文字输入框
]]
function ui.newEditBox(params)
local imageNormal = params.image
local imagePressed = params.imagePressed
local imageDisabled = params.imageDisabled
if type(imageNormal) == "string" then
imageNormal = display.newScale9Sprite(imageNormal)
end
if type(imagePressed) == "string" then
imagePressed = display.newScale9Sprite(imagePressed)
end
if type(imageDisabled) == "string" then
imageDisabled = display.newScale9Sprite(imageDisabled)
end
local editbox = CCEditBox:create(params.size, imageNormal, imagePressed, imageDisabled)
if editbox then
if params.listener then
-- 仅当 监听 方法传入时,才绑定监听方法。以避免 cocos2d-x 层面抛出异常
editbox:addEditBoxEventListener(params.listener)
end
if params.x and params.y then
editbox:setPosition(params.x, params.y)
end
end
return editbox
end
--[[--
创建菜单,并返回 CCMenu 对象。
@param table items 菜单项的数组表
@return CCMenu CCMenu对象
]]
function ui.newMenu(items)
local menu
menu = CCMenu:create()
for k, item in pairs(items) do
if not tolua.isnull(item) then
menu:addChild(item, 0, item:getTag())
end
end
menu:setPosition(0, 0)
return menu
end
--[[--
创建一个图像菜单项,并返回 CCMenuItemSprite 对象。
可用参数:
- image: 正常状态的按钮图像
- imageSelected: 按钮按下时的图像(可选)
- imageDisabled: 按钮被禁用时的图像(可选)
- listener: 回调函数
- tag: 按钮的 Tag,会传入回调函数。多个按钮使用同一个回调函数时,可根据 Tag 区分哪一个按钮被按下(可选)
- x, y: 坐标(可选)
- sound: 按钮按下时播放什么音效(可选)
~~~ lua
local function onButtonClicked(tag)
-- 按钮被按下
end
local item = ui.newImageMenuItem({
image = "Button.png",
imageSelected = "ButtonSelected.png",
listener = onButtonClicked
})
-- 创建菜单并加入场景,否则菜单项不会工作
local menu = ui.newMenu({item})
scene:addChild(menu)
~~~
@param table params 参数表格对象
@return CCMenuItemSprite CCMenuItemSprite对象
]]
function ui.newImageMenuItem(params)
local imageNormal = params.image
local imageSelected = params.imageSelected
local imageDisabled = params.imageDisabled
local listener = params.listener
local tag = params.tag
local x = params.x
local y = params.y
local sound = params.sound
if type(imageNormal) == "string" then
imageNormal = display.newSprite(imageNormal)
end
if type(imageSelected) == "string" then
imageSelected = display.newSprite(imageSelected)
end
if type(imageDisabled) == "string" then
imageDisabled = display.newSprite(imageDisabled)
end
local item = CCMenuItemSprite:create(imageNormal, imageSelected, imageDisabled)
if item then
if type(listener) == "function" then
item:addNodeEventListener(cc.MENU_ITEM_CLICKED_EVENT, function(tag)
if sound then audio.playSound(sound) end
listener(tag)
end)
end
if x and y then item:setPosition(x, y) end
if tag then item:setTag(tag) end
end
return item
end
--[[--
创建一个文字标签菜单项,并返回 CCMenuItemLabel 对象。
可用参数:
- listener: 回调函数
- tag: 按钮的 Tag,会传入回调函数。多个按钮使用同一个回调函数时,可根据 Tag 区分哪一个按钮被按下(可选)
- x, y: 坐标(可选)
- sound: 按钮按下时播放什么音效(可选)
以及所有可以用于 ui.newTTFLabel() 的参数。
@param table params 参数表格对象
@return CCMenuItemLabel CCMenuItemLabel对象
]]
function ui.newTTFLabelMenuItem(params)
local p = clone(params)
p.x, p.y = nil, nil
local label = ui.newTTFLabel(p)
local listener = params.listener
local tag = params.tag
local x = params.x
local y = params.y
local sound = params.sound
local item = CCMenuItemLabel:create(label)
if item then
if type(listener) == "function" then
item:addNodeEventListener(cc.MENU_ITEM_CLICKED_EVENT, function(tag)
if sound then audio.playSound(sound) end
listener(tag)
end)
end
if x and y then item:setPosition(x, y) end
if tag then item:setTag(tag) end
end
return item
end
--[[--
用位图字体创建文本显示对象,并返回 CCLabelBMFont 对象。
BMFont 通常用于显示英文内容,因为英文字母加数字和常用符号也不多,生成的 BMFont 文件较小。如果是中文,应该用 TTFLabel。
可用参数:
- text: 要显示的文本
- font: 字体文件名
- align: 文字的水平对齐方式(可选)
- x, y: 坐标(可选)
~~~ lua
local label = ui.newBMFontLabel({
text = "Hello",
font = "UIFont.fnt",
})
~~~
@param table params 参数表格对象
@return CCLabelBMFont CCLabelBMFont对象
]]
function ui.newBMFontLabel(params)
assert(type(params) == "table",
"[framework.ui] newBMFontLabel() invalid params")
local text = tostring(params.text)
local font = params.font
local textAlign = params.align or ui.TEXT_ALIGN_CENTER
local x, y = params.x, params.y
assert(font ~= nil, "ui.newBMFontLabel() - not set font")
local label = CCLabelBMFont:create(text, font, kCCLabelAutomaticWidth, textAlign)
if not label then return end
if type(x) == "number" and type(y) == "number" then
label:setPosition(x, y)
end
if textAlign == ui.TEXT_ALIGN_LEFT then
label:align(display.LEFT_CENTER)
elseif textAlign == ui.TEXT_ALIGN_RIGHT then
label:align(display.RIGHT_CENTER)
else
label:align(display.CENTER)
end
return label
end
--[[--
使用 TTF 字体创建文字显示对象,并返回 CCLabelTTF 对象。
可用参数:
- text: 要显示的文本
- font: 字体名,如果是非系统自带的 TTF 字体,那么指定为字体文件名
- size: 文字尺寸,因为是 TTF 字体,所以可以任意指定尺寸
- color: 文字颜色(可选),用 ccc3() 指定,默认为白色
- align: 文字的水平对齐方式(可选)
- valign: 文字的垂直对齐方式(可选),仅在指定了 dimensions 参数时有效
- dimensions: 文字显示对象的尺寸(可选),使用 CCSize() 指定
- x, y: 坐标(可选)
align 和 valign 参数可用的值:
- ui.TEXT_ALIGN_LEFT 左对齐
- ui.TEXT_ALIGN_CENTER 水平居中对齐
- ui.TEXT_ALIGN_RIGHT 右对齐
- ui.TEXT_VALIGN_TOP 垂直顶部对齐
- ui.TEXT_VALIGN_CENTER 垂直居中对齐
- ui.TEXT_VALIGN_BOTTOM 垂直底部对齐
~~~ lua
-- 创建一个居中对齐的文字显示对象
local label = ui.newTTFLabel({
text = "Hello, World",
font = "Marker Felt",
size = 64,
align = ui.TEXT_ALIGN_CENTER -- 文字内部居中对齐
})
-- 左对齐,并且多行文字顶部对齐
local label = ui.newTTFLabel({
text = "Hello, World\n您好,世界",
font = "Arial",
size = 64,
color = ccc3(255, 0, 0), -- 使用纯红色
align = ui.TEXT_ALIGN_LEFT,
valign = ui.TEXT_VALIGN_TOP,
dimensions = CCSize(400, 200)
})
~~~
@param table params 参数表格对象
@return CCLabelTTF CCLabelTTF对象
]]
function ui.newTTFLabel(params)
assert(type(params) == "table",
"[framework.ui] newTTFLabel() invalid params")
local label
local text = tostring(params.text)
local font = params.font or ui.DEFAULT_TTF_FONT
local size = params.size or ui.DEFAULT_TTF_FONT_SIZE
local color = params.color
local textAlign = params.align or ui.TEXT_ALIGN_LEFT
local textValign = params.valign or ui.TEXT_VALIGN_CENTER
local x, y = params.x, params.y
local dimensions = params.dimensions or CCSizeMake(0, 0)
local outlineWidth = params.outlineWidth or 0
local outlineColor
if outlineWidth > 0 then
outlineColor = params.outlineColor or ccc3(46,28,18)
label = CCLabelTTF:create(text, font, size, dimensions, textAlign, textValign, outlineColor, outlineWidth)
else
label = CCLabelTTF:create(text, font, size, dimensions, textAlign, textValign)
end
assert(type(size) == "number",
"[framework.ui] newTTFLabel() invalid params.size")
if label then
if color then
label:setColor(color)
end
function label:realign(x, y)
if textAlign == ui.TEXT_ALIGN_LEFT then
label:setPosition(math.round(x + label:getContentSize().width / 2), y)
elseif textAlign == ui.TEXT_ALIGN_RIGHT then
label:setPosition(x - math.round(label:getContentSize().width / 2), y)
else
label:setPosition(x, y)
end
end
if x and y then label:realign(x, y) end
end
return label
end
--[[--
创建带阴影的 TTF 文字显示对象,并返回 CCLabelTTF 对象。
相比 ui.newTTFLabel() 增加一个参数:
- shadowColor: 阴影颜色(可选),用 ccc3() 指定,默认为黑色
@param table params 参数表格对象
@return CCLabelTTF CCLabelTTF对象
]]
function ui.newTTFLabelWithShadow(params)
assert(type(params) == "table",
"[framework.ui] newTTFLabelWithShadow() invalid params")
local color = params.color or display.COLOR_WHITE
local shadowColor = params.shadowColor or display.COLOR_BLACK
local x, y = params.x, params.y
local g = display.newNode()
params.size = params.size
params.color = shadowColor
params.x, params.y = 0, 0
g.shadow1 = ui.newTTFLabel(params)
local offset = 1 / (display.widthInPixels / display.width)
g.shadow1:realign(offset, -offset)
g:addChild(g.shadow1)
params.color = color
g.label = ui.newTTFLabel(params)
g.label:realign(0, 0)
g:addChild(g.label)
function g:setString(text)
g.shadow1:setString(text)
g.label:setString(text)
end
function g:realign(x, y)
g:setPosition(x, y)
end
function g:getContentSize()
return g.label:getContentSize()
end
function g:setColor(...)
g.label:setColor(...)
end
function g:setShadowColor(...)
g.shadow1:setColor(...)
end
function g:setOpacity(opacity)
g.label:setOpacity(opacity)
g.shadow1:setOpacity(opacity)
end
if x and y then
g:setPosition(x, y)
end
return g
end
--[[--
创建带描边效果的 TTF 文字显示对象,并返回 CCLabelTTF 对象。
相比 ui.newTTFLabel() 增加参数:
outlineColor: 描边颜色(可选),用 ccc3() 指定,默认为黑色
outlineWidth: 描边宽度(可选), 默认为1
opacity: 描边透明度 (可选),默认为255
@param table params 参数表格对象
@return CCLabelTTF CCLabelTTF对象
]]
function ui.newTTFLabelWithOutline(params)
assert(type(params) == "table",
"[framework.ui] newTTFLabelWithShadow() invalid params")
return ui.newTTFLabel(params)
end
--[[--
为一个label或者sprite绘制描边,并将绘制好的描边添加到传入node的后面
@param node 需要描边的对象
outlineWidth 描边宽度
color外边框颜色
opacity描边透明度
scr,dst混合模式
@return CCRenderTexture 描边对象
]]
function ui.createOutline(node,outlineWidth,color,opacity,src,dst)
local w = node:getTexture():getContentSize().width + outlineWidth * 2
local h = node:getTexture():getContentSize().height + outlineWidth * 2
local rt = CCRenderTexture:create(w, h)
-- 记录原始位置
local originX, originY = node:getPosition()
local originColorR = 255
local originColorG = 255
local originColorB = 255
if node.getColor then
-- 记录原始颜色RGB信息
originColorR = node:getColor().r
originColorG = node:getColor().g
originColorB = node:getColor().b
end
-- 记录原始透明度信息
local originOpacity = node:getOpacity()
-- 记录原始是否显示
local originVisibility = node:isVisible()
-- 记录原始混合模式
local originBlend = node:getBlendFunc()
-- 设置颜色、透明度、显示
node:setColor(color)
node:setOpacity(opacity)
node:setVisible(true)
-- 设置新的混合模式
local blendFuc = ccBlendFunc:new()
blendFuc.src = src or GL_SRC_ALPHA
blendFuc.dst = dst or GL_ONE
-- blendFuc.dst = GL_ONE_MINUS_SRC_COLOR
node:setBlendFunc(blendFuc)
-- 这里考虑到锚点的位置,如果锚点刚好在中心处,代码可能会更好理解点
local bottomLeftX = node:getTexture():getContentSize().width * node:getAnchorPoint().x + outlineWidth
local bottomLeftY = node:getTexture():getContentSize().height * node:getAnchorPoint().y + outlineWidth
local positionOffsetX = node:getTexture():getContentSize().width * node:getAnchorPoint().x - node:getTexture():getContentSize().width / 2
local positionOffsetY = node:getTexture():getContentSize().height * node:getAnchorPoint().y - node:getTexture():getContentSize().height / 2
local rtPosition = ccp(originX - positionOffsetX, originY - positionOffsetY)
local function degrees2radians(angle)
return angle * 0.01745329252
end
rt:begin()
for i = 0, 360, 5 do
node:setPosition(ccp(bottomLeftX + math.sin(degrees2radians(i)) * outlineWidth, bottomLeftY + math.cos(degrees2radians(i)) * outlineWidth))
node:visit()
end
rt:endToLua()
-- node恢复原状
node:setPosition(originX, originY)
node:setColor(ccc3(originColorR, originColorG, originColorB))
node:setBlendFunc(originBlend)
node:setVisible(originVisibility)
node:setOpacity(originOpacity)
rt:setPosition(rtPosition)
--防锯齿
rt:getSprite():getTexture():setAntiAliasTexParameters()
node:getParent():addChild(rt,node:getZOrder()-1)
node.stroke = rt
return rt
end
return ui