Skip to content

Commit 4f084fa

Browse files
committed
- move samples repo to new Scale9Sprite
- fixed Scale9Sprite to work with all tests
1 parent 3622929 commit 4f084fa

File tree

3 files changed

+78
-35
lines changed

3 files changed

+78
-35
lines changed

cocos2d/sprite_nodes/CCSprite.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,7 @@ cc.Sprite = cc.Node.extend(/** @lends cc.Sprite# */{
914914
context.drawImage(this._texture, posX, -(posY + this._texture.height));
915915
} else {
916916
context.drawImage(this._texture,
917-
0, 0,
917+
this._rect.origin.x, this._rect.origin.y,
918918
this._rect.size.width, this._rect.size.height,
919919
posX, -(posY + this._rect.size.height),
920920
this._rect.size.width, this._rect.size.height);

extensions/GUI/CCControlExtension/CCScale9Sprite.js

Lines changed: 76 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,8 @@ cc.Scale9Sprite = cc.Node.extend({
8080
},
8181

8282
_updatePositions:function () {
83-
var size = this._contentSize;
84-
85-
var sizableWidth = size.width - this._topLeft.getContentSize().width - this._topRight.getContentSize().width;
86-
var sizableHeight = size.height - this._topLeft.getContentSize().height - this._bottomRight.getContentSize().height;
87-
var horizontalScale = sizableWidth / this._centre.getContentSize().width;
88-
var verticalScale = sizableHeight / this._centre.getContentSize().height;
89-
this._centre.setScaleX(horizontalScale);
90-
this._centre.setScaleY(verticalScale);
91-
var rescaledWidth = this._centre.getContentSize().width * horizontalScale;
92-
var rescaledHeight = this._centre.getContentSize().height * verticalScale;
9383

94-
var leftWidth = this._bottomLeft.getContentSize().width;
95-
var bottomHeight = this._bottomLeft.getContentSize().height;
84+
var size = this._contentSize;
9685

9786
this._bottomLeft.setAnchorPoint(cc.p(0, 0));
9887
this._bottomRight.setAnchorPoint(cc.p(0, 0));
@@ -104,24 +93,67 @@ cc.Scale9Sprite = cc.Node.extend({
10493
this._bottom.setAnchorPoint(cc.p(0, 0));
10594
this._centre.setAnchorPoint(cc.p(0, 0));
10695

107-
// Position corners
108-
this._bottomLeft.setPosition(cc.p(0, 0));
109-
this._bottomRight.setPosition(cc.p(leftWidth + rescaledWidth, 0));
110-
this._topLeft.setPosition(cc.p(0, bottomHeight + rescaledHeight));
111-
this._topRight.setPosition(cc.p(leftWidth + rescaledWidth, bottomHeight + rescaledHeight));
112-
113-
// Scale and position borders
114-
this._left.setPosition(cc.p(0, bottomHeight));
115-
this._left.setScaleY(verticalScale);
116-
this._right.setPosition(cc.p(leftWidth + rescaledWidth, bottomHeight));
117-
this._right.setScaleY(verticalScale);
118-
this._bottom.setPosition(cc.p(leftWidth, 0));
119-
this._bottom.setScaleX(horizontalScale);
120-
this._top.setPosition(cc.p(leftWidth, bottomHeight + rescaledHeight));
121-
this._top.setScaleX(horizontalScale);
122-
123-
// Position centre
124-
this._centre.setPosition(cc.p(leftWidth, bottomHeight));
96+
if(!this._capInsets)
97+
{
98+
var equalWidth = size.width / 3;
99+
var equalHeight = size.height / 3;
100+
101+
var equalWidthScale = equalWidth / this._centre.getContentSize().width;
102+
var equalHeightScale = equalHeight / this._centre.getContentSize().height;
103+
104+
this._topLeft.setScale(equalWidthScale, equalHeightScale);
105+
this._top.setScale(equalWidthScale, equalHeightScale);
106+
this._topRight.setScale(equalWidthScale, equalHeightScale);
107+
this._left.setScale(equalWidthScale, equalHeightScale);
108+
this._centre.setScale(equalWidthScale, equalHeightScale);
109+
this._right.setScale(equalWidthScale, equalHeightScale);
110+
this._bottomLeft.setScale(equalWidthScale, equalHeightScale);
111+
this._bottom.setScale(equalWidthScale, equalHeightScale);
112+
this._bottomRight.setScale(equalWidthScale, equalHeightScale);
113+
114+
this._topLeft.setPosition(cc.p(0, 2 * equalHeight));
115+
this._top.setPosition(cc.p(equalWidth, 2 * equalHeight));
116+
this._topRight.setPosition(cc.p(2 * equalWidth, 2 * equalHeight));
117+
this._left.setPosition(cc.p(0, equalHeight));
118+
this._centre.setPosition(cc.p(equalWidth, equalHeight));
119+
this._right.setPosition(cc.p(2 * equalWidth, equalHeight));
120+
this._bottomLeft.setPosition(cc.p(0, 0));
121+
this._bottom.setPosition(cc.p(equalWidth, 0));
122+
this._bottomRight.setPosition(cc.p(2 * equalWidth, 0));
123+
}
124+
else
125+
{
126+
var sizableWidth = size.width - this._topLeft.getContentSize().width - this._topRight.getContentSize().width;
127+
var sizableHeight = size.height - this._topLeft.getContentSize().height - this._bottomRight.getContentSize().height;
128+
var horizontalScale = sizableWidth / this._centre.getContentSize().width;
129+
var verticalScale = sizableHeight / this._centre.getContentSize().height;
130+
this._centre.setScaleX(horizontalScale);
131+
this._centre.setScaleY(verticalScale);
132+
var rescaledWidth = this._centre.getContentSize().width * horizontalScale;
133+
var rescaledHeight = this._centre.getContentSize().height * verticalScale;
134+
135+
var leftWidth = this._bottomLeft.getContentSize().width;
136+
var bottomHeight = this._bottomLeft.getContentSize().height;
137+
138+
// Position corners
139+
this._bottomLeft.setPosition(cc.p(0, 0));
140+
this._bottomRight.setPosition(cc.p(leftWidth + rescaledWidth, 0));
141+
this._topLeft.setPosition(cc.p(0, bottomHeight + rescaledHeight));
142+
this._topRight.setPosition(cc.p(leftWidth + rescaledWidth, bottomHeight + rescaledHeight));
143+
144+
// Scale and position borders
145+
this._left.setPosition(cc.p(0, bottomHeight));
146+
this._left.setScaleY(verticalScale);
147+
this._right.setPosition(cc.p(leftWidth + rescaledWidth, bottomHeight));
148+
this._right.setScaleY(verticalScale);
149+
this._bottom.setPosition(cc.p(leftWidth, 0));
150+
this._bottom.setScaleX(horizontalScale);
151+
this._top.setPosition(cc.p(leftWidth, bottomHeight + rescaledHeight));
152+
this._top.setScaleX(horizontalScale);
153+
154+
// Position centre
155+
this._centre.setPosition(cc.p(leftWidth, bottomHeight));
156+
}
125157
},
126158

127159
ctor:function () {
@@ -237,7 +269,6 @@ cc.Scale9Sprite = cc.Node.extend({
237269
initWithBatchNode:function (batchNode, rect, unused, capInsets) {
238270
if (batchNode) {
239271
this.updateWithBatchNode(batchNode, rect, unused, capInsets);
240-
this.setAnchorPoint(cc.p(0.5, 0.5));
241272
}
242273
this.m_positionsAreDirty = true;
243274
return true;
@@ -377,16 +408,27 @@ cc.Scale9Sprite = cc.Node.extend({
377408
this._spriteRect = rect;
378409
this._originalSize = new cc.Size(rect.size.width, rect.size.height);
379410
this._preferredSize = this._originalSize;
380-
this._capInsetsInternal = capInsets;
411+
this._capInsetsInternal = capInsets || cc.RectZero();
381412

382413
// If there is no specified center region
383-
if (cc.Rect.CCRectEqualToRect(this._capInsetsInternal, cc.RectZero())) {
414+
if (cc.Rect.CCRectEqualToRect(this._capInsetsInternal, cc.RectZero()) ||
415+
cc.Rect.CCRectEqualToRect(this._capInsetsInternal, this._spriteRect)) {
384416
// Apply the 3x3 grid format
385417
this._capInsetsInternal = cc.RectMake(
386418
rect.origin.x + this._originalSize.width / 3,
387419
rect.origin.y + this._originalSize.height / 3,
388420
this._originalSize.width / 3,
389421
this._originalSize.height / 3);
422+
this._capInsets = null;
423+
}
424+
else
425+
{
426+
this._capInsetsInternal = cc.RectMake(
427+
rect.origin.x + this._capInsetsInternal.origin.x,
428+
rect.origin.y + this._capInsetsInternal.origin.y,
429+
this._capInsetsInternal.size.width,
430+
this._capInsetsInternal.size.height
431+
);
390432
}
391433

392434
// Get the image edges
@@ -456,6 +498,7 @@ cc.Scale9Sprite = cc.Node.extend({
456498

457499
this.setContentSize(rect.size);
458500
this.addChild(this._scale9Image);
501+
this.setAnchorPoint(cc.p(0.5, 0.5));
459502
return true;
460503
},
461504

0 commit comments

Comments
 (0)