forked from Nlcke/layout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLayout.lua
1984 lines (1759 loc) · 57.5 KB
/
Layout.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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[[------------------------------------------------------------------------
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Layout API ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Layout.new{...} -- new layout with optional settings and/or children
Layout:update{...} -- update layout with settings and/or children
Layout:with(p) or Layout:with(old, new)
-- inheritance: extend Layout (sub)class with additional parameters
-- accepts one or two tables
-- if 'old' and 'new' tables used then keys will be checked
-- 'init' and 'upd' functions are inherited from all parents
-- resulting class can be instantiated (new) or extended (with)
Layout(id) -- access child layout by it's unique id or it's draw order
Layout(col, row) -- access cell by it's row and column numbers
Layout.select(sprite) -- set focus and selector to that sprite
Layout.newAnimation(frames, mark, strength, seed)
-- returns random animation, all parameters are optional
-- 'seed' is used for randomizer to get same animation each time
Layout:play(anim, [newstate], [callback])
-- plays animation if 'anim' is table, see Animation section below
-- or instantly finishes current animation if 'anim' is false
-- newstate is a table with numeric parameters of layout
-- callback is a function which will be called at the end of animation
Layout.newResources{...}
-- loads resources, see Resource Loader section below
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Resource Loader ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Layout has optional resource loader to automatically load various resources.
From the box it supports .jpg, .png, .wav, .mp3, .ttf, .otf, .lua, .json.
Resource Loader returs a table with resources, their names and indexes where
resources can be accessed with t[integer_number] or t[resource_name] and
resource name can be received with negative index i.e. t[-integer_number].
It has following interface:
Layout.newResources{...} where {...} is a table with following parameters:
path = string -- path to directory with resources
subdirs = boolean -- load resources from subdirectories
names = string -- table of names to filter directory files
from = number -- index of first resource file to load
to = number -- index of last resource file to load
output = boolean -- print list of resource files
namemod = function(name, path, base, ext, i) -- to filter file names
onlynames = boolean -- instead of resources return filenames
textureFiltering = boolean
textureOptions = table
fontSize = number
fontText = text
fontFiltering = boolean
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Animation ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
to describe animation a table with the following keys must be used:
-- 'frames' key --
defines animation length
if 'frames' is not exist or is 0 or less then you will get error message
-- 'mark' key --
defines the type of animation
it accepts following values: [-1|0|0..1|1..]
if 'mark' is -1 then it's ending animation (from initial state to new)
if 'mark' is 0 then it's opening animation (from new state to initial)
if 'mark' is greater than 0 then animation will be played as
initial -> new -> initial and 'mark' defines 'initial -> new' length
if 'mark' is in [0..1] range then it will relative to 'frames'
if 'mark' is in [1..frames] then it will be used as is
if 'mark' is in [frames..] then their values will be swapped
-- 'strength' key --
defines multiplier for t (time) parameter to change animation range
equal to 1 if not defined
-- other animation keys --
key names are same as Sprite.set accepts:
x, y, anchorX, anchorY,
rotationX, rotationY, scaleX, scaleY,
rotation, alpha,
redMultiplier, greenMultiplier, blueMultiplier, alphaMultiplier
key values can be [nil|number|function]
all keys are relative to initial state of layout i.e. 0 means no change
-- numeric keys --
each number will be multiplied by t (time) and added to origin,
where t is time (frame / frames) in range [0..1]
-- function-keys --
function must accept one parameter t (time), where t = [0..1]
function must return a number (delta) based on that parameter t
result of function will be multiplied by t and added to original value
-- x, y, anchorX, anchorY --
this parameters are relative to width and height of the layout
-- rotationX, rotationY, rotation --
each rotation will be multiplied by 360 (1.0 = 360, -0.5 = -180, etc)
-- skewX, skewY --
each skew will be multiplied by 90 (1.0 = 90, -0.5 = -45, etc)
--]]------------------------------------------------------------------------
Layout = Core.class(Mesh)
-- default parameters for each new layout (can be modified)
local default = {
-- scale modes for Layout.texM and Layout.sprM
NO_SCALE = 0,
LETTERBOX = 1,
STRETCH = 2,
FIT_WIDTH = 3,
FIT_HEIGHT = 4,
CROP = 5,
-- anchored (to width and height of parent) positioning
ancX = 0.5, -- anchored X, [number]
ancY = 0.5, -- anchored Y, [number]
-- relative (to width and height of parent) positioning
relX = false, -- relative X, [false|number]
relY = false, -- relative Y, [false|number]
-- absolute positioning (disables relative and anchored one)
absX = false, -- absolute X, [false|number]
absY = false, -- absolute X, [false|number]
-- relative (to width and height of parent) size
relW = 1.0,
relH = 1.0,
-- absolute size (disables relative and anchored one)
absW = false,
absH = false,
-- relative width/height restriction
limW = false, -- maximal width/height aspect ratio, [0..]
limH = false, -- maximal height/width aspect ratio, [0..]
-- relative content size
conRelW = 1, -- content width relative to parent, [number]
conRelH = 1, -- content height relative to parent, [number]
-- absolute content size (disables relative width and/or height)
conAbsW = false, -- content absolute width (in pixels), [false|number]
conAbsH = false, -- content absolute height (in pixels), [false|number]
-- template grid (template and database should be both enabled)
template = false, -- Layout or Layout-based class
database = false, -- list of cells' parameters
colsFill = false, -- columns will be filled first if true
-- manual and template grid
cols = 0, -- grid cols number, [0..]
rows = 0, -- grid rows number, [0..]
cellBrdW = 0.0, -- cell border width in pixels
cellBrdH = 0.0, -- cell border height in pixels
cellRelW = 1.0, -- cell relative width for children layouts
cellRelH = 1.0, -- cell relative width for children layouts
cellAbsW = false, -- cell absolute width for children layouts
cellAbsH = false, -- cell absolute height for children layouts
-- grid cell settings
col = false, -- cell column number, [number|false]
row = false, -- cell row number, [number|false]
cellW = 1.0, -- cell width relative modifier
cellH = 1.0, -- cell height relative modifier
-- selector
selector = Layout.new{bgrA = 0.25}, -- [Layout]
selectable = true, -- can be selected by keyboard/joystick, [true|false]
selectedCol = 0, -- col to select grid cell
selectedRow = 0, -- row to select grid cell
-- background
bgrC = 0x000000, -- background color, [0x000000..0xFFFFFF]
bgrA = 0.0, -- background alpha, [0..1]
-- texture
texture = false, -- texture object, [Texture|false]
texC = 0xFFFFFF, -- texture color, [0x000000..0xFFFFFF]
texA = 1.0, -- texture alpha, [0..1]
texM = 1, -- texture scale mode (default: LETTERBOX), [number]
texS = 1.0, -- texture scale, [number]
texAncX = 0.5, -- texture anchored X, [0..1]
texAncY = 0.5, -- texture anchored Y, [0..1]
texOffX = 0.0, -- texture X offset (in pixels)
texOffY = 0.0, -- texture Y offset (in pixels)
-- non-layout children sprites
sprM = 1, -- sprite scale mode (default: LETTERBOX), [number]
sprS = 1.0, -- sprite scale
sprAncX = 0.5, -- sprite anchored X, [0..1]
sprAncY = 0.5, -- sprite anchored Y, [0..1]
sprOffX = 0.0, -- sprite X offset (in pixels)
sprOffY = 0.0, -- sprite Y offset (in pixels)
-- relative center (affects rotation and scaling)
centerX = 0.5, -- [0..1]
centerY = 0.5, -- [0..1]
-- content clipping
clip = true,
-- identification
id = false, -- to get child by id with 'layout(id)' call
-- inheritance
init = false, -- callback at instantiation (useful for custom classes)
-- [false|function(self, parameters)]
ext = false, -- callback at children adding (to modify them)
-- [false|function(self, child)]
upd = false, -- callback at updating (useful for custom classes)
-- [false|function(self, parameters)]
-- keyboard control, can have multiple keys for the same action
keys = { -- [realCode] = action
[16777234] = "LEFT", -- jump to leftward cell
[16777235] = "UP", -- jump to upward cell
[16777236] = "RIGHT", -- jump to rightward cell
[16777237] = "DOWN", -- jump to downward cell
[16777220] = "SELECT", -- enter the layout or press it
[16777219] = "BACK", -- return to parent layout or stage
-- mouse wheel for "UP" and "DOWN" actions
mouseWheel = true,
},
-- gamepad control, can have multiple buttons for the same action
buttons = { -- [keyCode] = action
[19] = "UP",
[20] = "DOWN",
[22] = "RIGHT",
[21] = "LEFT",
[96] = "SELECT",
[97] = "BACK",
-- left stick for "UP", "DOWN", "RIGHT", "LEFT" actions
stickDeadzone = 0.5, -- [0..1], stick disabled at 1
},
-- moving/scrolling
moveReactionX = 1, -- reaction coefficient for X while layout dragged
moveReactionY = 1, -- reaction coefficient for Y while layout dragged
moveDeadzone = 5, -- moving/scrolling starts outside this zone, [0..]
moveFriction = 0.5, -- friction coefficient while layout dragged
moveDamping = 0.9, -- damping coefficient while layout released
moveDelta = 1, -- area side to detect moving/scrolling, [0..]
-- scrolling
scrollFrames = 20, -- animation frames for keyboard or joystick scroll
-- scaling
zoomMouseResp = 0.005, -- scale response for mouse
zoomTouchResp = 0.005, -- scale response for touch
zoomMin = 0.2, -- scale minimal value
zoomMax = 2.0, -- scale maximal value
-- tilting
tiltMouseResp = 1, -- tilt response for mouse
tiltTouchResp = 1, -- tilt response for touch
events = true, -- enable mouse/touch events, [false|true]
-- event callbacks
onAdd = false, -- callback at added to stage event, [false|function]
onRemove = false, -- callback at removed from stage event, [false|function]
onHover = false, -- callback at mouse hovering, [false|function]
onPress = false, -- callback at LMB or touch press, [false|function]
onHold = false, -- callback at LMB or touch while hold, [false|function]
onMove = false, -- callback at layout moving, [false|function]
onScroll = false, -- callback at layout scrolling, [false|function]
onBack = false, -- callback at "BACK" action, [false|function]
onResize = false, -- callback at changing size, [false|function]
onZoom = false, -- callback at zooming, [false|function]
onTilt = false, -- callback at tilt, [false|function]
-- built-in callbacks
scroll = false, -- move children with mouse or touch, [false|true]
move = false, -- move layout with mouse or touch, [false|true]
zoom = false, -- zoom layout with RMB or 2-point touch, [false|true]
tilt = false, -- tilt layout with RMB or 2-point touch, [false|true]
-- animation
anAdd = false, -- opening animation (mark=0)
anRemove = false, -- ending animation (mark=-1)
anPress = false, -- press animation (mark>0)
anHover = false, -- hover animation (mark>0)
}
for k,v in pairs(default) do Layout[k] = v end
local internal = {
-- identification
isLayout = true,
getClass = function() return "Layout" end,
-- inheritance
with = false,
-- to check if event is mouse move event
isMouseMove = false,
-- to check if parent changed
parent = false,
-- event types
IDLE = 0,
HOVER = 1,
PRESS_HOLD = 2,
MOVE_SCROLL = 3,
ZOOM_TILT = 4,
ADD = 5,
REMOVE = 6,
PLAY = 7,
event = 0,
onPlay = false,
-- position and size
x = 0,
y = 0,
w = 0,
h = 0,
-- parent size
parW = 0,
parH = 0,
-- parent cell size
parCellW = 0,
parCellH = 0,
-- parent cell border size
parCellBrdW = 0,
parCellBrdH = 0,
-- selected sprite/layout
selected = stage,
-- scroll offset
offX = 0,
offY = 0,
-- scrolling area size
scrW = 0,
scrH = 0,
-- content size
conW = 0,
conH = 0,
-- animation parameters
frame = 0,
mark = 0,
frames = 0,
newstate = false,
oldstate = false,
-- scroll and move parameters
pointerX0 = 0, -- initial X of pointer
pointerY0 = 0, -- initial Y of pointer
pointerX = 0, -- current X of pointer
pointerY = 0, -- current Y of pointer
pointerDX = 0, -- delta X value
pointerDY = 0, -- delta Y value
pointerAX = 0, -- accumulated X value
pointerAY = 0, -- accumulated Y value,
-- built-in Sprite parameters
super = Layout.super,
removeFromParent = Layout.removeFromParent,
hitTestPoint = Layout.hitTestPoint,
-- animation functions
play = false,
continueAnimation = false,
animate = false,
backupState = false,
restoreState = false,
getAnchorPoint = false,
getRelativePosition = false,
setAnchorPoint = false,
setRelativePosition = false,
-- events functions
disableEvents = false,
enableEvents = false,
-- order changing function
bringToFront = false,
-- grid helper function
getGridSize = false,
-- core funciton to check for updates and animation
enterFrame = false,
-- auxilary functions
newAnimation = false,
newResources = false,
-- event processing functions
atHover = false,
atPress = false,
atZoomOrTilt = false,
onKeyOrButton = false,
onMouseDown = false,
onMouseHover = false,
onMouseMove = false,
onRelease = false,
onTouchesBegin = false,
onTouchesMove = false,
-- select functions
select = false,
selectCell = false,
-- update functions
update = false,
updateColor = false,
updateMove = false,
updateScroll = false,
updateSprite = false,
updateTemplateGrid = false,
updateTexture = false,
updateZoom = false,
}
for k,v in pairs(internal) do Layout[k] = v end
function Layout:with(old, new)
local d = {}
local s = self.superclass
if new then
for k,v in pairs(old) do
if Layout[k] == nil then
if s and s[k] == nil then
error("Layout: new key `"..tostring(k).."`", 2)
end
end
end
for k,v in pairs(new) do
if Layout[k] ~= nil or (s and s[k] ~= nil) then
error("Layout: old key `"..tostring(k).."`", 2)
end
end
for k,v in pairs(new) do d[k] = v end
end
for k,v in pairs(old) do d[k] = v end
if s then
for k,v in pairs(s) do if d[k] == nil then d[k] = v end end
if s.init ~= d.init then
local initS, initD = s.init, d.init
d.init = function(self, p) initS(self, p); initD(self, p) end
end
if s.upd ~= d.upd then
local updS, updD = s.upd, d.upd
d.upd = function(self, p) updS(self, p); updD(self, p) end
end
if s.ext ~= d.ext then
local extS, extD = s.ext, d.ext
d.ext = function(self, p) extS(self, p); extD(self, p) end
end
end
return {
new = function(p)
for k,v in pairs(d) do if p[k] == nil then p[k] = v end end
local layout = Layout.new(p, d)
return layout
end,
superclass = d,
with = Layout.with,
}
end
function Layout.new(p, d)
local self = Mesh.new()
Mesh.setIndexArray(self, 1, 2, 3, 1, 3, 4)
Mesh.setVertices(self, 1,0,0, 2,0,0, 3,0,0, 4,0,0)
setmetatable(self, Layout)
self.backup = {}
for k,v in pairs(p) do
if tonumber(k) then
self:addChild(self.ext and self:ext(v) or v)
elseif Layout[k] ~= nil or (d and d[k] ~= nil) then
self[k] = v
else
error("Layout: unknown key `"..tostring(k).."`", 2)
end
end
if self.init then self:init(p) end
if self.texture then self:setTexture(self.texture) end
local c = self.texture and self.texC or self.bgrC
local a = self.texture and self.texA or self.bgrA
Mesh.setColorArray(self, c, a, c, a, c, a, c, a)
self:addEventListener(Event.ENTER_FRAME, self.enterFrame, self)
--self:addEventListener(Event.ADDED_TO_STAGE, self.enterFrame, self)
return self
end
function Layout:enterFrame(e)
if not self.__parent then return end
local parent = self.__parent
local parW, parH = self.parW, self.parH
if parent.isLayout then
if parent.w == 0 or parent.h == 0 then return end
self.parW, self.parH = parent.w, parent.h
self.parCellBrdW, self.parCellBrdH = parent.cellBrdW, parent.cellBrdH
self.parCellW = parent.cellAbsW or parent.cellRelW * parent.w
self.parCellH = parent.cellAbsH or parent.cellRelH * parent.h
elseif parent == stage then
if application:getScaleMode() == "noScale" then
self.parW = application:getDeviceWidth()
self.parH = application:getDeviceHeight()
else
self.parW = application:getContentWidth()
self.parH = application:getContentHeight()
end
else
Sprite.removeFromParent(self)
local _
_, _, self.parW, self.parH = parent:getBounds(parent)
parent:addChild(self)
end
local update = self.parW ~= parW or self.parH ~= parH
if self.onHold and self.event == Layout.PRESS_HOLD then self:onHold() end
if self.frame < self.frames then
self.frame = self.frame + 1
if self.mark == 0 then
self:animate(1 - self.frame / self.frames)
elseif self.mark == -1 then
self:animate(self.frame / self.frames)
elseif self.frame < self.mark then
self:animate(self.frame / self.mark)
elseif self.frame == self.mark+1 and self.event == Layout.PRESS_HOLD then
self.frame = self.frame - 1
return
elseif self.frame ~= self.frames then
self:animate(1 - (self.frame - self.mark) / (self.frames - self.mark))
end
if self.frame == self.frames then
update = true
self:restoreState()
if self.event == Layout.ADD then
if self.onAdd then self:onAdd() end
if self.events then self:enableEvents() end
elseif self.event == Layout.REMOVE then
if self.onRemove then self:onRemove() end
if self == Layout.selected then
Layout.select(self.__parent)
end
self.parent = nil
Sprite.removeFromParent(self)
return
elseif self.event == Layout.PLAY then
if self.onPlay then self:onPlay() end
if self.events then self:enableEvents() end
end
if self.event ~= Layout.HOVER and self.event ~= Layout.MOVE_SCROLL then
self.event = self.IDLE
end
end
end
if self.scroll or self.move or self.zoom or self.tilt then
if self.event == Layout.MOVE_SCROLL then
local f = self.moveFriction
self.pointerAX = f * (self.pointerAX + self.pointerDX)
self.pointerAY = f * (self.pointerAY + self.pointerDY)
if self.pointerDX ~= 0 or self.pointerDY ~= 0 then
local rx, ry = self.moveReactionX, self.moveReactionY
if self.scroll then
self:updateScroll(rx*self.pointerDX, ry*self.pointerDY)
end
if self.move then
self:updateMove(rx*self.pointerDX, ry*self.pointerDY)
if self.parent and self.parent.scroll then
self.parent:updateScroll(-2 * rx * self.pointerDX,
- 2 * ry * self.pointerDY)
self.parent.pointerAX, self.parent.pointerAY = 0, 0
end
end
self.pointerX = self.pointerX + self.pointerDX
self.pointerY = self.pointerY + self.pointerDY
self.pointerDX, self.pointerDY = 0, 0
end
elseif self.event == Layout.ZOOM_TILT then
if self.pointerDX ~= 0 or self.pointerDY ~= 0 then
if self.zoom then
if self.scroll then
local k = self.isMouseMove and -self.zoomMouseResp
or self.zoomTouchResp
self:updateScroll(0, 0, k * self.pointerDY)
else
local minS, maxS = self.zoomMin, self.zoomMax
local k = self.isMouseMove and -self.zoomMouseResp
or self.zoomTouchResp
local s = self:getScale() + k * self.pointerDY
if s < minS then s = minS
elseif s > maxS then s = maxS end
self:setScale(s)
self.backup.scaleX, self.backup.scaleY = s, s
end
end
if self.tilt then
local k = self.isMouseMove and self.tiltMouseResp
or -57.3 * self.tiltTouchResp
if self.scroll then
else
local r = self:getRotation() + k * self.pointerDX
self:setRotation(r)
self.backup.rotation = r
end
end
self.pointerX = self.pointerX + self.pointerDX
self.pointerY = self.pointerY + self.pointerDY
self.pointerDX, self.pointerDY = 0, 0
end
elseif self.scroll or self.move then
self.pointerAX = math.abs(self.pointerAX) > self.moveDelta and
self.moveDamping * self.pointerAX or 0
self.pointerAY = math.abs(self.pointerAY) > self.moveDelta and
self.moveDamping * self.pointerAY or 0
if self.pointerAX ~= 0 or self.pointerAY ~= 0 then
if self.scroll then
self:updateScroll(self.pointerAX, self.pointerAY)
end
if self.move then
self:updateMove(self.pointerAX, self.pointerAY)
end
end
end
end
if self.parent ~= parent then
self.parent = parent
self:update()
if self.anAdd then
self.event = Layout.ADD
if self.events then self:disableEvents() end
self:play(self.anAdd, nil, 0)
self:animate(1)
else
if self.onAdd then self:onAdd() end
if self.events then self:enableEvents() end
end
elseif update then
self:update()
end
end
function Layout:removeFromParent()
if self.anRemove then
self.event = Layout.REMOVE
self:disableEvents()
self:play(self.anRemove, nil, -1)
else
if self.onRemove then self:onRemove() end
if self == Layout.selected then
Layout.select(self.__parent)
end
self.parent = nil
Sprite.removeFromParent(self)
end
end
-- MOUSE AND TOUCH EVENTS --
function Layout:enableEvents()
local hoverEvents = self.anHover or self.onHover
local moveEvents = self.scroll or self.move or self.zoom or self.tilt
local pressEvents = self.onPress or self.onHold or self.anPress
if hoverEvents then
self:addEventListener(Event.MOUSE_HOVER, self.onMouseHover, self)
end
if pressEvents or moveEvents then
self:addEventListener(Event.MOUSE_DOWN, self.onMouseDown, self)
self:addEventListener(Event.MOUSE_UP, self.onRelease, self)
self:addEventListener(Event.TOUCHES_BEGIN, self.onTouchesBegin, self)
self:addEventListener(Event.TOUCHES_END, self.onRelease, self)
end
if moveEvents then
self:addEventListener(Event.MOUSE_MOVE, self.onMouseMove, self)
self:addEventListener(Event.TOUCHES_MOVE, self.onTouchesMove, self)
end
end
function Layout:disableEvents()
self:removeEventListener(Event.MOUSE_DOWN, self.onMouseDown, self)
self:removeEventListener(Event.MOUSE_UP, self.onRelease, self)
self:removeEventListener(Event.MOUSE_MOVE, self.onMouseMove, self)
self:removeEventListener(Event.MOUSE_HOVER, self.onMouseHover, self)
self:removeEventListener(Event.TOUCHES_BEGIN, self.onTouchesBegin, self)
self:removeEventListener(Event.TOUCHES_MOVE, self.onTouchesMove, self)
self:removeEventListener(Event.TOUCHES_END, self.onRelease, self)
end
function Layout:hitTestPoint(x, y)
local parent = self.__parent
if not parent then return false end
if parent.events then
if parent.frame < parent.frames then return false end
if not parent:hitTestPoint(x, y) then return false end
end
if (self.zoom or self.tilt) and not self.scroll then
local r = Sprite.getRotation(self)
local s = Sprite.getScale(self)
local lx, ly = Sprite.globalToLocal(self, x, y)
Sprite.setRotation(self, 0)
Sprite.setScale(self, 1)
x, y = Sprite.localToGlobal(self, lx, ly)
Sprite.setRotation(self, r)
Sprite.setScale(self, s)
end
local ltg = Sprite.localToGlobal
local x1, y1 = ltg(parent, self.x, self.y)
local x2, y2 = ltg(parent, self.x + self.w, self.y + self.h)
return x >= x1 and y >= y1 and x <= x2 and y <= y2
end
function Layout:bringToFront()
local parent = self.__parent
if parent.template then return end
if parent:getChildIndex(self) ~= parent:getNumChildren() then
Sprite.removeFromParent(self)
parent:addChild(self)
end
end
function Layout:onMouseHover(e)
if Layout.selected:hitTestPoint(e.x, e.y) then
return e:stopPropagation()
end
local focus = self:hitTestPoint(e.x, e.y)
if self.event == Layout.HOVER then
if not focus then self.event = Layout.IDLE end
elseif focus then
self:atHover()
e:stopPropagation()
end
end
function Layout:atHover()
self:bringToFront()
Layout.select(self)
if self.onHover then self.onHover(self) end
if self.anHover then
if self.frame < self.frames and self.event == Layout.HOVER then
self:continueAnimation()
else
self:play(self.anHover, nil, -2)
end
end
self.event = Layout.HOVER
end
function Layout:onMouseDown(e)
if e.button == 1 or e.button == 2 then
self.pointerX, self.pointerY = e.x, e.y
self.pointerX0, self.pointerY0 = self.pointerX, self.pointerY
self.pointerDX, self.pointerDY = 0, 0
if self:hitTestPoint(e.x, e.y) then
if e.button == 1 then
self:atPress()
else
self:atZoomOrTilt()
self.isMouseMove = true
end
e:stopPropagation()
end
end
end
function Layout:onTouchesBegin(e)
if #e.allTouches == 1 then
self.pointerX, self.pointerY = e.touch.x, e.touch.y
self.pointerX0, self.pointerY0 = self.pointerX, self.pointerY
self.pointerDX, self.pointerDY = 0, 0
if self:hitTestPoint(e.touch.x, e.touch.y) then
self:atPress()
e:stopPropagation()
end
elseif #e.allTouches == 2 then
local t1, t2 = e.allTouches[1], e.allTouches[2]
self.pointerX0, self.pointerY0 = (t1.x + t2.x) / 2, (t1.y + t2.y) / 2
if self:hitTestPoint(self.pointerX0, self.pointerY0) then
self.pointerX = math.atan2(t1.x - t2.x, t1.y - t2.y)
self.pointerY = math.sqrt((t1.x - t2.x)^2 + (t1.y - t2.y)^2)
self:atZoomOrTilt()
self.isMouseMove = false
e:stopPropagation()
end
end
end
function Layout:atPress()
local parent = self.__parent
if parent and parent.isLayout then
parent.pointerX, parent.pointerY = self.pointerX, self.pointerY
parent.pointerDX, parent.pointerDY = 0, 0
parent.event = Layout.PRESS_HOLD
end
self:bringToFront()
if self.anHover or self.onHover then Layout.select(self) end
local event = self.event
self.event = Layout.PRESS_HOLD
if self.anPress then
if self.frame < self.frames and event == Layout.PRESS_HOLD then
self:continueAnimation()
else
self:play(self.anPress, nil, -2)
end
end
end
function Layout:atZoomOrTilt(e)
self:bringToFront()
self.pointerAX, self.pointerAY = 0, 0
if self.frame < self.frames then
self.frame = self.frames
self:restoreState()
end
self.event = Layout.ZOOM_TILT
end
function Layout:onMouseMove(e)
self.pointerDX = e.x - self.pointerX
self.pointerDY = e.y - self.pointerY
if self.event == Layout.PRESS_HOLD and (
math.abs(self.pointerDX) > self.moveDeadzone or
math.abs(self.pointerDY) > self.moveDeadzone
) then
if self.frame < self.mark then self:continueAnimation() end
self.event = Layout.MOVE_SCROLL
end
end
function Layout:onTouchesMove(e)
if #e.allTouches == 1 then
self.pointerDX = e.touch.x - self.pointerX
self.pointerDY = e.touch.y - self.pointerY
if self.event == Layout.PRESS_HOLD and (
math.abs(self.pointerDX) > self.moveDeadzone or
math.abs(self.pointerDY) > self.moveDeadzone
) then
if self.frame < self.mark then self:continueAnimation() end
self.event = Layout.MOVE_SCROLL
end
elseif self.event == Layout.ZOOM_TILT then
local t1, t2 = e.allTouches[1], e.allTouches[2]
self.pointerDY = math.sqrt((t1.x - t2.x)^2 + (t1.y - t2.y)^2) - self.pointerY
self.pointerDX = math.atan2(t1.x - t2.x, t1.y - t2.y) - self.pointerX
e:stopPropagation()
end
end
local eventsToHover = {
[Layout.PRESS_HOLD] = true,
[Layout.MOVE_SCROLL] = true,
[Layout.ZOOM_TILT] = true,
}
function Layout:onRelease(e)
if eventsToHover[self.event] then
if self.event == Layout.PRESS_HOLD then
if self.onPress then self:onPress() end
if self.event == Layout.REMOVE then return end
if self.frame < self.frames then self:continueAnimation() end
end
self.event = Layout.HOVER
local parent = self.__parent
if parent and parent.isLayout then
if eventsToHover[parent.event] then
parent.event = Layout.HOVER
end
end
e:stopPropagation()
end
end
-- UPDATING --
function Layout:update(p)
if p then
if self.__parent and self.parent ~= self.__parent then
local parent = self.__parent
if parent.isLayout then
self.parW, self.parH = parent.w, parent.h
self.parCellBrdW = parent.cellBrdW
self.parCellBrdH = parent.cellBrdH
self.parCellW = parent.cellAbsW or parent.cellRelW * parent.w
self.parCellH = parent.cellAbsH or parent.cellRelH * parent.h
elseif parent == stage then
if application:getScaleMode() == "noScale" then
self.parW = application:getDeviceWidth()
self.parH = application:getDeviceHeight()
else
self.parW = application:getContentWidth()
self.parH = application:getContentHeight()
end
else
Sprite.removeFromParent(self)
local _
_, _, self.parW, self.parH = parent:getBounds(parent)
parent:addChild(self)
end
end
if self.upd then self:upd(p) end
if p.texture ~= nil then
local c, a
if p.texture then
self:setTexture(p.texture)
c, a = self.texC, self.texA
else
self:clearTexture()
c, a = self.bgrC, self.bgrA
end
Mesh.setColorArray(self, c, a, c, a, c, a, c, a)
end
if p.events ~= nil then
if p.events then
self:enableEvents()
else
self:disableEvents()
end
end
if p.database ~= nil then
self.offX, self.offY = 0, 0
self.w, self.h = 0, 0
self.selectedCol, self.selectedRow = 0, 0
if self.__children then
for i, child in pairs(self.__children) do
child:removeFromParent()
end
end
end
for k,v in pairs(p) do
if tonumber(k) then
self:addChild(self.ext and self:ext(v) or v)
else
self[k] = v
end
end
self:updateColor(p.texC, p.texA, p.bgrC, p.bgrA)
if self.onScroll and (p.offX or p.offY) then self:onScroll() end
end
local w = self.col and self.parCellW * self.cellW or self.absW or
self.relW * self.parW
local h = self.row and self.parCellH * self.cellH or self.absH or
self.relH * self.parH
if self.limW and w / h > self.limW then w = self.limW * h end
if self.limH and h / w > self.limH then h = self.limH * w end
if w ~= self.w or h ~= self.h then
Mesh.setVertices(self, 1,-1,-1, 2,w+1,-1, 3,w+1,h+1, 4,-1,h+1)
local min, max = math.min, math.max
if self.template then
local cols, rows = self:getGridSize()
local cw = (self.cellAbsW or self.cellRelW * w) + self.cellBrdW
local ch = (self.cellAbsH or self.cellRelH * h) + self.cellBrdH
self.conW = cw * cols - self.cellBrdW
self.conH = ch * rows - self.cellBrdH
if p and (p.selectedCol or p.selectedRow) then
self.scrW = max(0, self.conW - w)
self.scrH = max(0, self.conH - h)
if p.selectedCol then
self.offX = min(cw * self.selectedCol, self.scrW)
end
if p.selectedRow then
self.offY = min(ch * self.selectedRow, self.scrH)
end
if self.onScroll then self:onScroll() end
end
else
if self.cols > 0 then
self.conW = (self.cellAbsW or self.cellRelW * w) * self.cols
else
self.conW = self.conAbsW or self.conRelW * w
end
if self.rows > 0 then
self.conH = (self.cellAbsH or self.cellRelH * h) * self.rows
else
self.conH = self.conAbsH or self.conRelH * h
end
end
self.scrW = max(0, self.conW * self:getScaleX() - w)
self.scrH = max(0, self.conH * self:getScaleY() - h)
self.offX = min(self.offX, self.scrW)
self.offY = min(self.offY, self.scrH)
if self.onResize then self:onResize() end
end
local offX, offY = self.offX, self.offY
if self.clip then self:setClip(offX, offY, w, h) end