-
Notifications
You must be signed in to change notification settings - Fork 903
/
Copy pathUIWidget.js
2087 lines (1877 loc) · 67.4 KB
/
UIWidget.js
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
/****************************************************************************
Copyright (c) 2011-2012 cocos2d-x.org
Copyright (c) 2013-2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
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.
****************************************************************************/
ccui._FocusNavigationController = cc.Class.extend({
_keyboardListener: null,
_firstFocusedWidget: null,
_enableFocusNavigation: false,
_keyboardEventPriority: 1,
enableFocusNavigation: function (flag) {
if (this._enableFocusNavigation === flag)
return;
this._enableFocusNavigation = flag;
if (flag)
this._addKeyboardEventListener();
else
this._removeKeyboardEventListener();
},
_setFirstFocsuedWidget: function (widget) {
this._firstFocusedWidget = widget;
},
_onKeyPressed: function (keyCode, event) {
if (this._enableFocusNavigation && this._firstFocusedWidget) {
if (keyCode === cc.KEY.dpadDown) {
this._firstFocusedWidget = this._firstFocusedWidget.findNextFocusedWidget(ccui.Widget.DOWN, this._firstFocusedWidget);
}
if (keyCode === cc.KEY.dpadUp) {
this._firstFocusedWidget = this._firstFocusedWidget.findNextFocusedWidget(ccui.Widget.UP, this._firstFocusedWidget);
}
if (keyCode === cc.KEY.dpadLeft) {
this._firstFocusedWidget = this._firstFocusedWidget.findNextFocusedWidget(ccui.Widget.LEFT, this._firstFocusedWidget);
}
if (keyCode === cc.KEY.dpadRight) {
this._firstFocusedWidget = this._firstFocusedWidget.findNextFocusedWidget(ccui.Widget.RIGHT, this._firstFocusedWidget);
}
}
},
_addKeyboardEventListener: function () {
if (!this._keyboardListener) {
this._keyboardListener = cc.EventListener.create({
event: cc.EventListener.KEYBOARD,
onKeyReleased: this._onKeyPressed.bind(this)
});
cc.eventManager.addListener(this._keyboardListener, this._keyboardEventPriority);
}
},
_removeKeyboardEventListener: function () {
if (this._keyboardListener) {
cc.eventManager.removeEventListener(this._keyboardListener);
this._keyboardListener = null;
}
}
});
ccui.__LAYOUT_COMPONENT_NAME = "__ui_layout";
/**
* The base class for ccui controls and layout
* @sample
* var uiWidget = new ccui.Widget();
* this.addChild(uiWidget);
* @class
* @extends ccui.ProtectedNode
*
* @property {Number} xPercent - Position x in percentage of width
* @property {Number} yPercent - Position y in percentage of height
* @property {Number} widthPercent - Width in percentage of parent width
* @property {Number} heightPercent - Height in percentage of parent height
* @property {ccui.Widget} widgetParent - <@readonly> The direct parent when it's a widget also, otherwise equals null
* @property {Boolean} enabled - Indicate whether the widget is enabled
* @property {Boolean} focused - Indicate whether the widget is focused
* @property {ccui.Widget.SIZE_ABSOLUTE|ccui.Widget.SIZE_PERCENT} sizeType - The size type of the widget
* @property {ccui.Widget.TYPE_WIDGET|ccui.Widget.TYPE_CONTAINER} widgetType - <@readonly> The type of the widget
* @property {Boolean} touchEnabled - Indicate whether touch events are enabled
* @property {Boolean} updateEnabled - Indicate whether the update function is scheduled
* @property {Boolean} bright - Indicate whether the widget is bright
* @property {String} name - The name of the widget
* @property {Number} actionTag - The action tag of the widget
*/
ccui.Widget = ccui.ProtectedNode.extend(/** @lends ccui.Widget# */{
_enabled: true, ///< Highest control of widget
_bright: true, ///< is this widget bright
_touchEnabled: false, ///< is this widget touch endabled
_brightStyle: null, ///< bright style
_touchBeganPosition: null, ///< touch began point
_touchMovePosition: null, ///< touch moved point
_touchEndPosition: null, ///< touch ended point
_touchEventListener: null,
_touchEventSelector: null,
_name: "default",
_widgetType: null,
_actionTag: 0,
_customSize: null,
_layoutParameterDictionary: null,
_layoutParameterType: 0,
_focused: false,
_focusEnabled: true,
_ignoreSize: false,
_affectByClipping: false,
_sizeType: null,
_sizePercent: null,
_positionType: null,
_positionPercent: null,
_hit: false,
_nodes: null,
_touchListener: null,
_className: "Widget",
_flippedX: false,
_flippedY: false,
_opacity: 255,
_highlight: false,
_touchEventCallback: null,
_clickEventListener: null,
_propagateTouchEvents: true,
_unifySize: false,
_callbackName: null,
_callbackType: null,
_usingLayoutComponent: false,
_inViewRect: true,
/**
* Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
* @function
*/
ctor: function () {
cc.ProtectedNode.prototype.ctor.call(this);
this._brightStyle = ccui.Widget.BRIGHT_STYLE_NONE;
this._touchBeganPosition = cc.p(0, 0);
this._touchMovePosition = cc.p(0, 0);
this._touchEndPosition = cc.p(0, 0);
this._widgetType = ccui.Widget.TYPE_WIDGET;
this._customSize = cc.size(0, 0);
this._layoutParameterDictionary = {};
this._sizeType = ccui.Widget.SIZE_ABSOLUTE;
this._sizePercent = cc.p(0, 0);
this._positionType = ccui.Widget.POSITION_ABSOLUTE;
this._positionPercent = cc.p(0, 0);
this._nodes = [];
this._layoutParameterType = ccui.LayoutParameter.NONE;
ccui.Widget.prototype.init.call(this);
},
/**
* initializes state of widget. please do not call this function by yourself, you should pass the parameters to constructor to initialize it
.
* @returns {boolean}
*/
init: function () {
this._layoutParameterDictionary = {};
this._initRenderer();
this.setBright(true);
this.onFocusChanged = this.onFocusChange;
this.onNextFocusedWidget = null;
this.setAnchorPoint(cc.p(0.5, 0.5));
this.ignoreContentAdaptWithSize(true);
return true;
},
/**
* Calls updateSizeAndPosition and its parent's onEnter
* @override
*/
onEnter: function () {
var locListener = this._touchListener;
if (locListener && !locListener._isRegistered() && this._touchEnabled)
cc.eventManager.addListener(locListener, this);
if(!this._usingLayoutComponent)
this.updateSizeAndPosition();
if (this._sizeDirty)
this._onSizeChanged();
cc.ProtectedNode.prototype.onEnter.call(this);
},
/**
* Calls unscheduleUpdate and its parent's onExit
* @override
*/
onExit: function () {
this.unscheduleUpdate();
cc.ProtectedNode.prototype.onExit.call(this);
},
_getOrCreateLayoutComponent: function(){
var layoutComponent = this.getComponent(ccui.__LAYOUT_COMPONENT_NAME);
if (null == layoutComponent){
layoutComponent = new ccui.LayoutComponent();
this.addComponent(layoutComponent);
}
return layoutComponent;
},
/**
* The direct parent when it's a widget also, otherwise equals null
* @returns {ccui.Widget|null}
*/
getWidgetParent: function () {
var widget = this.getParent();
if (widget instanceof ccui.Widget)
return widget;
return null;
},
_updateContentSizeWithTextureSize: function (size) {
if(this._unifySize){
this.setContentSize(size);
return;
}
this.setContentSize(this._ignoreSize ? size : this._customSize);
},
_isAncestorsEnabled: function () {
var parentWidget = this._getAncensterWidget(this);
if (parentWidget == null)
return true;
if (parentWidget && !parentWidget.isEnabled())
return false;
return parentWidget._isAncestorsEnabled();
},
/**
* Allow widget touch events to propagate to its parents. Set false will disable propagation
* @since v3.2
* @param {Boolean} isPropagate
*/
setPropagateTouchEvents: function (isPropagate) {
this._propagateTouchEvents = isPropagate;
},
/**
* Return whether the widget is propagate touch events to its parents or not
* @since v3.2
* @returns {boolean}
*/
isPropagateTouchEvents: function () {
return this._propagateTouchEvents;
},
/**
* Specify widget to swallow touches or not
* @since v3.2
* @param {Boolean} swallow
*/
setSwallowTouches: function (swallow) {
if (this._touchListener)
this._touchListener.setSwallowTouches(swallow);
},
/**
* Return whether the widget is swallowing touch or not
* @since v3.2
* @returns {boolean}
*/
isSwallowTouches: function () {
if (this._touchListener) {
//return true; //todo need test
return this._touchListener.isSwallowTouches();
}
return false;
},
_getAncensterWidget: function (node) {
if (null == node)
return null;
var parent = node.getParent();
if (null == parent)
return null;
if (parent instanceof ccui.Widget)
return parent;
else
return this._getAncensterWidget(parent.getParent());
},
_isAncestorsVisible: function (node) {
if (null == node)
return true;
var parent = node.getParent();
if (parent && !parent.isVisible())
return false;
return this._isAncestorsVisible(parent);
},
/**
* <p>
* Sets whether the widget is enabled <br/>
* true if the widget is enabled, widget may be touched , false if the widget is disabled, widget cannot be touched. <br/>
* The default value is true, a widget is default to enabled
* </p>
* @param {Boolean} enabled
*/
setEnabled: function (enabled) {
this._enabled = enabled;
this.setBright(enabled);
},
/**
* initializes renderer of widget.
*/
_initRenderer: function () {
},
/**
* Sets _customSize of ccui.Widget, if ignoreSize is true, the content size is its renderer's contentSize, otherwise the content size is parameter.
* and updates size percent by parent content size. At last, updates its children's size and position.
* @param {cc.Size|Number} contentSize content size or width of content size
* @param {Number} [height]
* @override
*/
setContentSize: function(contentSize, height){
cc.Node.prototype.setContentSize.call(this, contentSize, height);
var locWidth = this._contentSize.width;
var locHeight = this._contentSize.height;
this._customSize.width = locWidth;
this._customSize.height = locHeight;
if(this._unifySize){
//unify size logic
} else if (this._ignoreSize) {
this._contentSize = this.getVirtualRendererSize();
}
if (!this._usingLayoutComponent && this._running) {
var widgetParent = this.getWidgetParent();
var pSize = widgetParent ? widgetParent.getContentSize() : this._parent.getContentSize();
this._sizePercent.x = (pSize.width > 0.0) ? locWidth / pSize.width : 0.0;
this._sizePercent.y = (pSize.height > 0.0) ? locHeight / pSize.height : 0.0;
}
if (this._running) {
this._onSizeChanged();
} else {
this._sizeDirty = true;
}
},
_setWidth: function (w) {
if (w === this._contentSize.width) {
return;
}
cc.Node.prototype._setWidth.call(this, w);
this._customSize.width = w;
if(this._unifySize){
//unify size logic
} else if (this._ignoreSize) {
this._contentSize = this.getVirtualRendererSize();
}
if (!this._usingLayoutComponent && this._running) {
var widgetParent = this.getWidgetParent();
var locWidth = widgetParent ? widgetParent.width : this._parent.width;
this._sizePercent.x = locWidth > 0 ? this._customSize.width / locWidth : 0;
}
if (this._running) {
this._onSizeChanged();
} else {
this._sizeDirty = true;
}
},
_setHeight: function (h) {
if (h === this._contentSize.height) {
return;
}
cc.Node.prototype._setHeight.call(this, h);
this._customSize.height = h;
if(this._unifySize){
//unify size logic
} else if (this._ignoreSize) {
this._contentSize = this.getVirtualRendererSize();
}
if (!this._usingLayoutComponent && this._running) {
var widgetParent = this.getWidgetParent();
var locH = widgetParent ? widgetParent.height : this._parent.height;
this._sizePercent.y = locH > 0 ? this._customSize.height / locH : 0;
}
if (this._running) {
this._onSizeChanged();
} else {
this._sizeDirty = true;
}
},
/**
* Changes the percent that is widget's percent size
* @param {cc.Point} percent that is widget's percent size, width and height value from 0 to 1.
*/
setSizePercent: function (percent) {
if(this._usingLayoutComponent){
var component = this._getOrCreateLayoutComponent();
component.setUsingPercentContentSize(true);
component.setPercentContentSize(percent);
component.refreshLayout();
return;
}
this._sizePercent.x = percent.x;
this._sizePercent.y = percent.y;
var width = this._customSize.width, height = this._customSize.height;
if (this._running) {
var widgetParent = this.getWidgetParent();
if (widgetParent) {
width = widgetParent.width * percent.x;
height = widgetParent.height * percent.y;
} else {
width = this._parent.width * percent.x;
height = this._parent.height * percent.y;
}
}
if (this._ignoreSize)
this.setContentSize(this.getVirtualRendererSize());
else
this.setContentSize(width, height);
this._customSize.width = width;
this._customSize.height = height;
},
_setWidthPercent: function (percent) {
this._sizePercent.x = percent;
var width = this._customSize.width;
if (this._running) {
var widgetParent = this.getWidgetParent();
width = (widgetParent ? widgetParent.width : this._parent.width) * percent;
}
if (this._ignoreSize)
this._setWidth(this.getVirtualRendererSize().width);
else
this._setWidth(width);
this._customSize.width = width;
},
_setHeightPercent: function (percent) {
this._sizePercent.y = percent;
var height = this._customSize.height;
if (this._running) {
var widgetParent = this.getWidgetParent();
height = (widgetParent ? widgetParent.height : this._parent.height) * percent;
}
if (this._ignoreSize)
this._setHeight(this.getVirtualRendererSize().height);
else
this._setHeight(height);
this._customSize.height = height;
},
/**
* updates its size by size type and its position by position type.
* @param {cc.Size} [parentSize] parent size
*/
updateSizeAndPosition: function (parentSize) {
if (!parentSize) {
var widgetParent = this.getWidgetParent();
if (widgetParent)
parentSize = widgetParent.getLayoutSize();
else
parentSize = this._parent.getContentSize();
}
switch (this._sizeType) {
case ccui.Widget.SIZE_ABSOLUTE:
if (this._ignoreSize)
this.setContentSize(this.getVirtualRendererSize());
else
this.setContentSize(this._customSize);
this._sizePercent.x = (parentSize.width > 0) ? this._customSize.width / parentSize.width : 0;
this._sizePercent.y = (parentSize.height > 0) ? this._customSize.height / parentSize.height : 0;
break;
case ccui.Widget.SIZE_PERCENT:
var cSize = cc.size(parentSize.width * this._sizePercent.x, parentSize.height * this._sizePercent.y);
if (this._ignoreSize)
this.setContentSize(this.getVirtualRendererSize());
else
this.setContentSize(cSize);
this._customSize.width = cSize.width;
this._customSize.height = cSize.height;
break;
default:
break;
}
this._onSizeChanged();
var absPos = this.getPosition();
switch (this._positionType) {
case ccui.Widget.POSITION_ABSOLUTE:
if (parentSize.width <= 0 || parentSize.height <= 0) {
this._positionPercent.x = this._positionPercent.y = 0;
} else {
this._positionPercent.x = absPos.x / parentSize.width;
this._positionPercent.y = absPos.y / parentSize.height;
}
break;
case ccui.Widget.POSITION_PERCENT:
absPos = cc.p(parentSize.width * this._positionPercent.x, parentSize.height * this._positionPercent.y);
break;
default:
break;
}
if (this._parent instanceof ccui.ImageView) {
var renderer = this._parent._imageRenderer;
if (renderer && !renderer._textureLoaded)
return;
}
this.setPosition(absPos);
},
/**TEXTURE_RES_TYPE
* Changes the size type of widget.
* @param {ccui.Widget.SIZE_ABSOLUTE|ccui.Widget.SIZE_PERCENT} type that is widget's size type
*/
setSizeType: function (type) {
this._sizeType = type;
if (this._usingLayoutComponent) {
var component = this._getOrCreateLayoutComponent();
component.setUsingPercentContentSize(this._sizeType === ccui.SIZE_PERCENT);
}
},
/**
* Gets the size type of widget.
* @returns {ccui.Widget.SIZE_ABSOLUTE|ccui.Widget.SIZE_PERCENT} that is widget's size type
*/
getSizeType: function () {
return this._sizeType;
},
/**
* Ignore the widget size
* @param {Boolean} ignore true that widget will ignore it's size, use texture size, false otherwise. Default value is true.
*/
ignoreContentAdaptWithSize: function (ignore) {
if(this._unifySize){
this.setContentSize(this._customSize);
return;
}
if (this._ignoreSize === ignore)
return;
this._ignoreSize = ignore;
this.setContentSize(ignore ? this.getVirtualRendererSize() : this._customSize);
//this._onSizeChanged();
},
/**
* Gets whether ignore the content size (custom size)
* @returns {boolean} true that widget will ignore it's size, use texture size, false otherwise.
*/
isIgnoreContentAdaptWithSize: function () {
return this._ignoreSize;
},
/**
* Get custom size of ccui.Widget
* @returns {cc.Size}
*/
getCustomSize: function () {
return cc.size(this._customSize);
},
/**
* Gets layout size of ccui.Widget.
* @returns {cc.Size}
*/
getLayoutSize: function () {
return cc.size(this._contentSize);
},
/**
* Returns size percent of ccui.Widget
* @returns {cc.Point}
*/
getSizePercent: function () {
if(this._usingLayoutComponent){
var component = this._getOrCreateLayoutComponent();
this._sizePercent = component.getPercentContentSize();
}
return this._sizePercent;
},
_getWidthPercent: function () {
return this._sizePercent.x;
},
_getHeightPercent: function () {
return this._sizePercent.y;
},
/**
* Gets world position of ccui.Widget.
* @returns {cc.Point} world position of ccui.Widget.
*/
getWorldPosition: function () {
return this.convertToWorldSpace(cc.p(this._anchorPoint.x * this._contentSize.width, this._anchorPoint.y * this._contentSize.height));
},
/**
* Gets the Virtual Renderer of widget.
* @returns {ccui.Widget}
*/
getVirtualRenderer: function () {
return this;
},
/**
* Gets the content size of widget. Content size is widget's texture size.
*/
getVirtualRendererSize: function () {
return cc.size(this._contentSize);
},
/**
* call back function called when size changed.
*/
_onSizeChanged: function () {
if(!this._usingLayoutComponent){
var locChildren = this.getChildren();
for (var i = 0, len = locChildren.length; i < len; i++) {
var child = locChildren[i];
if (child instanceof ccui.Widget)
child.updateSizeAndPosition();
}
this._sizeDirty = false;
}
},
/**
* Sets whether the widget is touch enabled. The default value is false, a widget is default to touch disabled
* @param {Boolean} enable true if the widget is touch enabled, false if the widget is touch disabled.
*/
setTouchEnabled: function (enable) {
if (this._touchEnabled === enable)
return;
this._touchEnabled = enable; //TODO need consider remove and re-add.
if (this._touchEnabled) {
if (!this._touchListener)
this._touchListener = cc.EventListener.create({
event: cc.EventListener.TOUCH_ONE_BY_ONE,
swallowTouches: true,
onTouchBegan: this.onTouchBegan.bind(this),
onTouchMoved: this.onTouchMoved.bind(this),
onTouchEnded: this.onTouchEnded.bind(this)
});
cc.eventManager.addListener(this._touchListener, this);
} else {
cc.eventManager.removeListener(this._touchListener);
}
},
/**
* Returns whether or not touch is enabled.
* @returns {boolean} true if the widget is touch enabled, false if the widget is touch disabled.
*/
isTouchEnabled: function () {
return this._touchEnabled;
},
/**
* Determines if the widget is highlighted
* @returns {boolean} true if the widget is highlighted, false if the widget is not highlighted .
*/
isHighlighted: function () {
return this._highlight;
},
/**
* Sets whether the widget is highlighted. The default value is false, a widget is default to not highlighted
* @param highlight true if the widget is highlighted, false if the widget is not highlighted.
*/
setHighlighted: function (highlight) {
if (highlight === this._highlight)
return;
this._highlight = highlight;
if (this._bright) {
if (this._highlight)
this.setBrightStyle(ccui.Widget.BRIGHT_STYLE_HIGH_LIGHT);
else
this.setBrightStyle(ccui.Widget.BRIGHT_STYLE_NORMAL);
} else
this._onPressStateChangedToDisabled();
},
/**
* Determines if the widget is on focused
* @returns {boolean} whether the widget is focused or not
*/
isFocused: function () {
return this._focused;
},
/**
* Sets whether the widget is on focused
* The default value is false, a widget is default to not on focused
* @param {boolean} focus pass true to let the widget get focus or pass false to let the widget lose focus
*/
setFocused: function (focus) {
this._focused = focus;
//make sure there is only one focusedWidget
if (focus) {
ccui.Widget._focusedWidget = this;
if (ccui.Widget._focusNavigationController)
ccui.Widget._focusNavigationController._setFirstFocsuedWidget(this);
}
},
/**
* returns whether the widget could accept focus.
* @returns {boolean} true represent the widget could accept focus, false represent the widget couldn't accept focus
*/
isFocusEnabled: function () {
return this._focusEnabled;
},
/**
* sets whether the widget could accept focus.
* @param {Boolean} enable true represent the widget could accept focus, false represent the widget couldn't accept focus
*/
setFocusEnabled: function (enable) {
this._focusEnabled = enable;
},
/**
* <p>
* When a widget is in a layout, you could call this method to get the next focused widget within a specified direction. <br/>
* If the widget is not in a layout, it will return itself
* </p>
* @param direction the direction to look for the next focused widget in a layout
* @param current the current focused widget
* @return the next focused widget in a layout
*/
findNextFocusedWidget: function (direction, current) {
if (null === this.onNextFocusedWidget || null == this.onNextFocusedWidget(direction)) {
var isLayout = current instanceof ccui.Layout;
if (this.isFocused() || isLayout) {
var layout = this.getParent();
if (null === layout || !(layout instanceof ccui.Layout)) {
//the outer layout's default behaviour is : loop focus
if (isLayout)
return current.findNextFocusedWidget(direction, current);
return current;
} else
return layout.findNextFocusedWidget(direction, current);
} else
return current;
} else {
var getFocusWidget = this.onNextFocusedWidget(direction);
this.dispatchFocusEvent(this, getFocusWidget);
return getFocusWidget;
}
},
/**
* when a widget calls this method, it will get focus immediately.
*/
requestFocus: function () {
if (this === ccui.Widget._focusedWidget)
return;
this.dispatchFocusEvent(ccui.Widget._focusedWidget, this);
},
/**
* no matter what widget object you call this method on , it will return you the exact one focused widget
*/
getCurrentFocusedWidget: function () {
return ccui.Widget._focusedWidget;
},
/**
* <p>
* When a widget lose/get focus, this method will be called. Be Caution when you provide your own version, <br/>
* you must call widget.setFocused(true/false) to change the focus state of the current focused widget;
* </p>
*/
onFocusChanged: null,
/**
* use this function to manually specify the next focused widget regards to each direction
*/
onNextFocusedWidget: null,
/**
* Sends the touch event to widget's parent, its subclass will override it, e.g. ccui.ScrollView, ccui.PageView
* @param {Number} eventType
* @param {ccui.Widget} sender
* @param {cc.Touch} touch
*/
interceptTouchEvent: function (eventType, sender, touch) {
var widgetParent = this.getWidgetParent();
if (widgetParent)
widgetParent.interceptTouchEvent(eventType, sender, touch);
},
/**
* This method is called when a focus change event happens
* @param {ccui.Widget} widgetLostFocus
* @param {ccui.Widget} widgetGetFocus
*/
onFocusChange: function (widgetLostFocus, widgetGetFocus) {
//only change focus when there is indeed a get&lose happens
if (widgetLostFocus)
widgetLostFocus.setFocused(false);
if (widgetGetFocus)
widgetGetFocus.setFocused(true);
},
/**
* Dispatch a EventFocus through a EventDispatcher
* @param {ccui.Widget} widgetLostFocus
* @param {ccui.Widget} widgetGetFocus
*/
dispatchFocusEvent: function (widgetLostFocus, widgetGetFocus) {
//if the widgetLoseFocus doesn't get focus, it will use the previous focused widget instead
if (widgetLostFocus && !widgetLostFocus.isFocused())
widgetLostFocus = ccui.Widget._focusedWidget;
if (widgetGetFocus !== widgetLostFocus) {
if (widgetGetFocus && widgetGetFocus.onFocusChanged)
widgetGetFocus.onFocusChanged(widgetLostFocus, widgetGetFocus);
if (widgetLostFocus && widgetGetFocus.onFocusChanged)
widgetLostFocus.onFocusChanged(widgetLostFocus, widgetGetFocus);
cc.eventManager.dispatchEvent(new cc.EventFocus(widgetLostFocus, widgetGetFocus));
}
},
/**
* Sets whether the widget is bright. The default value is true, a widget is default to bright
* @param {Boolean} bright true if the widget is bright, false if the widget is dark.
*/
setBright: function (bright) {
this._bright = bright;
if (this._bright) {
this._brightStyle = ccui.Widget.BRIGHT_STYLE_NONE;
this.setBrightStyle(ccui.Widget.BRIGHT_STYLE_NORMAL);
} else
this._onPressStateChangedToDisabled();
},
/**
* To set the bright style of ccui.Widget.
* @param {Number} style BRIGHT_NORMAL the widget is normal state, BRIGHT_HIGHLIGHT the widget is height light state.
*/
setBrightStyle: function (style) {
if (this._brightStyle === style)
return;
style = style || ccui.Widget.BRIGHT_STYLE_NORMAL;
this._brightStyle = style;
switch (this._brightStyle) {
case ccui.Widget.BRIGHT_STYLE_NORMAL:
this._onPressStateChangedToNormal();
break;
case ccui.Widget.BRIGHT_STYLE_HIGH_LIGHT:
this._onPressStateChangedToPressed();
break;
default:
break;
}
},
_onPressStateChangedToNormal: function () {
},
_onPressStateChangedToPressed: function () {
},
_onPressStateChangedToDisabled: function () {
},
_updateChildrenDisplayedRGBA: function () {
this.setColor(this.getColor());
this.setOpacity(this.getOpacity());
},
/**
* A call back function when widget lost of focus.
*/
didNotSelectSelf: function () {
},
/**
* <p>
* The callback of touch began event. <br/>
* If the bounding box of ccui.Widget contains the touch point, it will do the following things: <br/>
* 1. sets highlight state, <br/>
* 2. sends event to parent widget by interceptTouchEvent <br/>
* 3. calls the callback of touch began event. <br/>
* 4. returns true, <br/>
* otherwise returns false directly. <br/>
* </p>
* @override
* @param {cc.Touch} touch
* @param {cc.Event} event
* @returns {boolean}
*/
onTouchBegan: function (touch, event) {
this._hit = false;
if (this.isVisible() && this.isEnabled() && this._isAncestorsEnabled() && this._isAncestorsVisible(this)) {
var touchPoint = touch.getLocation();
this._touchBeganPosition.x = touchPoint.x;
this._touchBeganPosition.y = touchPoint.y;
if (this.hitTest(this._touchBeganPosition) && this.isClippingParentContainsPoint(this._touchBeganPosition))
this._hit = true;
}
if (!this._hit) {
return false;
}
this.setHighlighted(true);
/*
* Propagate touch events to its parents
*/
if (this._propagateTouchEvents) {
this.propagateTouchEvent(ccui.Widget.TOUCH_BEGAN, this, touch);
}
this._pushDownEvent();
return true;
},
propagateTouchEvent: function (event, sender, touch) {
var widgetParent = this.getWidgetParent();
if (widgetParent) {
widgetParent.interceptTouchEvent(event, sender, touch);
}
},
/**
* <p>
* The callback of touch moved event. <br/>
* It sets the highlight state by touch, sends event to parent widget by interceptTouchEvent and calls the callback of touch moved event.
* </p>
* @param {cc.Touch} touch
* @param {cc.Event} event
*/
onTouchMoved: function (touch, event) {
var touchPoint = touch.getLocation();
this._touchMovePosition.x = touchPoint.x;
this._touchMovePosition.y = touchPoint.y;
this.setHighlighted(this.hitTest(touchPoint));
/*
* Propagate touch events to its parents
*/
if (this._propagateTouchEvents)
this.propagateTouchEvent(ccui.Widget.TOUCH_MOVED, this, touch);
this._moveEvent();
},
/**
* <p>
* The callback of touch end event
* It sends event to parent widget by interceptTouchEvent,
* calls the callback of touch end event (highlight= true) or touch canceled event (highlight= false).
* sets the highlight state to false ,
* </p>
* @param touch
* @param event
*/
onTouchEnded: function (touch, event) {
var touchPoint = touch.getLocation();