Skip to content

Commit 64ff7a3

Browse files
committed
Merge pull request #3193 from 1scaR1/develop
Refactor ccui.ScrollView,ListView,PageView to correspond cocos2d-x v3.11 api.
2 parents 02c9cf9 + 6487236 commit 64ff7a3

File tree

9 files changed

+2461
-1354
lines changed

9 files changed

+2461
-1354
lines changed

Diff for: extensions/ccui/uiwidgets/scroll-widget/UIListView.js

+570-27
Large diffs are not rendered by default.

Diff for: extensions/ccui/uiwidgets/scroll-widget/UIPageView.js

+315-384
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
/****************************************************************************
2+
Copyright (c) 2015 Neo Kim ([email protected])
3+
Copyright (c) 2015 Nikita Besshaposhnikov ([email protected])
4+
5+
http://www.cocos2d-x.org
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
****************************************************************************/
25+
26+
/**
27+
* The PageViewIndicator control of Cocos UI <br/>
28+
* Indicator being attached to page view.
29+
* @class
30+
* @extends ccui.ProtectedNode
31+
* @property {Number} spaceBetweenIndexNodes - Space between index nodes in PageViewIndicator
32+
*/
33+
ccui.PageViewIndicator = ccui.ProtectedNode.extend(/** @lends ccui.PageViewIndicator# */{
34+
_direction: null,
35+
_indexNodes: null,
36+
_currentIndexNode: null,
37+
_spaceBetweenIndexNodes: 0,
38+
_className: "PageViewIndicator",
39+
40+
/**
41+
* Allocates and initializes a PageViewIndicator.
42+
* Constructor of ccui.PageViewIndicator. override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
43+
*/
44+
ctor: function () {
45+
cc.ProtectedNode.prototype.ctor.call(this);
46+
47+
this._direction = ccui.ScrollView.DIR_HORIZONTAL;
48+
this._indexNodes = [];
49+
this._spaceBetweenIndexNodes = ccui.PageViewIndicator.SPACE_BETWEEN_INDEX_NODES_DEFAULT;
50+
51+
this.init();
52+
53+
// this.setCascadeColorEnabled(true);
54+
// this.setCascadeOpacityEnabled(true);
55+
},
56+
57+
/**
58+
* Initializes a ccui.PageViewIndicator. Please do not call this function by yourself, you should pass the parameters to constructor to initialize it.
59+
* @returns {boolean}
60+
*/
61+
init: function () {
62+
if (cc.ProtectedNode.prototype.init.call(this)) {
63+
64+
var image = new Image();
65+
image.src = ccui.PageViewIndicator.CIRCLE_IMAGE;
66+
67+
this._currentIndexNode = new cc.Sprite(image);
68+
this._currentIndexNode.setVisible(false);
69+
this.addProtectedChild(this._currentIndexNode, 1);
70+
71+
return true;
72+
}
73+
return false;
74+
},
75+
76+
/**
77+
* Sets direction of indicator
78+
* @param {ccui.ScrollView.DIR_NONE | ccui.ScrollView.DIR_VERTICAL | ccui.ScrollView.DIR_HORIZONTAL | ccui.ScrollView.DIR_BOTH} direction
79+
*/
80+
setDirection: function(direction)
81+
{
82+
this._direction = direction;
83+
this._rearrange();
84+
},
85+
86+
/**
87+
* resets indicator with new page count.
88+
* @param {number} numberOfTotalPages
89+
*/
90+
reset: function(numberOfTotalPages)
91+
{
92+
while(this._indexNodes.length < numberOfTotalPages)
93+
{
94+
this._increaseNumberOfPages();
95+
}
96+
while(this._indexNodes.length > numberOfTotalPages)
97+
{
98+
this._decreaseNumberOfPages();
99+
}
100+
this._rearrange();
101+
this._currentIndexNode.setVisible(this._indexNodes.length > 0);
102+
},
103+
104+
/**
105+
* Indicates node by index
106+
* @param {number} index
107+
*/
108+
indicate: function(index)
109+
{
110+
if (index < 0 || index >= this._indexNodes.length)
111+
{
112+
return;
113+
}
114+
this._currentIndexNode.setPosition(this._indexNodes[index].getPosition());
115+
},
116+
117+
_rearrange: function()
118+
{
119+
if(this._indexNodes.length === 0)
120+
{
121+
return;
122+
}
123+
124+
var horizontal = (this._direction === ccui.ScrollView.DIR_HORIZONTAL);
125+
126+
// Calculate total size
127+
var indexNodeSize = this._indexNodes[0].getContentSize();
128+
var sizeValue = (horizontal ? indexNodeSize.width : indexNodeSize.height);
129+
130+
var numberOfItems = this._indexNodes.length;
131+
var totalSizeValue = sizeValue * numberOfItems + this._spaceBetweenIndexNodes * (numberOfItems - 1);
132+
133+
var posValue = -(totalSizeValue / 2) + (sizeValue / 2);
134+
for(var i = 0; i < this._indexNodes.length; ++i)
135+
{
136+
var position;
137+
if(horizontal)
138+
{
139+
position = cc.p(posValue, indexNodeSize.height / 2.0);
140+
}
141+
else
142+
{
143+
position = cc.p(indexNodeSize.width / 2.0, -posValue);
144+
}
145+
this._indexNodes[i].setPosition(position);
146+
posValue += sizeValue + this._spaceBetweenIndexNodes;
147+
}
148+
},
149+
150+
/**
151+
* Sets space between index nodes.
152+
* @param {number} spaceBetweenIndexNodes
153+
*/
154+
setSpaceBetweenIndexNodes: function(spaceBetweenIndexNodes)
155+
{
156+
if(this._spaceBetweenIndexNodes === spaceBetweenIndexNodes)
157+
{
158+
return;
159+
}
160+
this._spaceBetweenIndexNodes = spaceBetweenIndexNodes;
161+
this._rearrange();
162+
},
163+
164+
/**
165+
* Gets space between index nodes.
166+
* @returns {number}
167+
*/
168+
getSpaceBetweenIndexNodes: function()
169+
{
170+
return this._spaceBetweenIndexNodes;
171+
},
172+
173+
/**
174+
* Sets color of selected index node
175+
* @param {cc.Color} color
176+
*/
177+
setSelectedIndexColor: function(color)
178+
{
179+
this._currentIndexNode.setColor(color);
180+
},
181+
182+
/**
183+
* Gets color of selected index node
184+
* @returns {cc.Color}
185+
*/
186+
getSelectedIndexColor: function()
187+
{
188+
return this._currentIndexNode.getColor();
189+
},
190+
191+
_increaseNumberOfPages: function()
192+
{
193+
var image = new Image();
194+
image.src = ccui.PageViewIndicator.CIRCLE_IMAGE;
195+
196+
var indexNode = new cc.Sprite(image);
197+
this.addProtectedChild(indexNode);
198+
this._indexNodes.push(indexNode);
199+
},
200+
201+
_decreaseNumberOfPages: function()
202+
{
203+
if(this._indexNodes.length === 0)
204+
{
205+
return;
206+
}
207+
this.removeProtectedChild(this._indexNodes[0]);
208+
this._indexNodes.splice(0, 1);
209+
},
210+
211+
/**
212+
* Removes all index nodes.
213+
*/
214+
clear: function()
215+
{
216+
for(var i = 0; i < this._indexNodes.length; ++i)
217+
{
218+
this.removeProtectedChild(this._indexNodes[i]);
219+
}
220+
this._indexNodes.length = 0;
221+
this._currentIndexNode.setVisible(false);
222+
}
223+
224+
});
225+
226+
var _p = ccui.PageViewIndicator.prototype;
227+
228+
// Extended properties
229+
/** @expose */
230+
_p.spaceBetweenIndexNodes;
231+
cc.defineGetterSetter(_p, "spaceBetweenIndexNodes", _p.getSpaceBetweenIndexNodes, _p.setSpaceBetweenIndexNodes);
232+
/**
233+
* @ignore
234+
*/
235+
ccui.PageViewIndicator.SPACE_BETWEEN_INDEX_NODES_DEFAULT = 23;
236+
ccui.PageViewIndicator.CIRCLE_IMAGE = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAQAAADZc7J/AAAA8ElEQVRIx62VyRGCQBBF+6gWRCEmYDIQkhiBCgHhSclC8YqWzOV5oVzKAYZp3r1/9fpbxAIBMTsKrjx5cqVgR0wgLhCRUWOjJiPqD56xoaGPhpRZV/iSEy6crHmw5oIrF9b/lVeMofrJgjlnxlIy/wik+JB+mme8BExbBhm+5CJC2LE2LtSEQoyGWDioBA5CoRIohJtK4CYDxzNEM4GAugR1E9VjVC+SZpXvhCJCrjomESLvc17pDGX7bWmlh6UtpjPVCWy9zaJ0TD7qfm3pwERMz2trRVZk3K3BD/L34AY+dEDCniMVBkPFkT2J/b2/AIV+dRpFLOYoAAAAAElFTkSuQmCC";

0 commit comments

Comments
 (0)