Skip to content

Commit 63eb018

Browse files
committed
Merge pull request #1428 from xingsenma/develop_gui_button
issue #3419 add a default display when pressed texture is not loaded
2 parents 9d3084d + 89a9953 commit 63eb018

File tree

1 file changed

+61
-19
lines changed

1 file changed

+61
-19
lines changed

extensions/CocoStudio/GUI/UIWidgets/UIButton.js

Lines changed: 61 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ ccs.Button = ccs.Widget.extend(/** @lends ccs.Button# */{
5454
_disabledTextureSize: null,
5555
_pressedActionEnabled: false,
5656
_titleColor: null,
57+
_normalTextureScaleXInSize: 1,
58+
_normalTextureScaleYInSize: 1,
59+
_pressedTextureScaleXInSize: 1,
60+
_pressedTextureScaleYInSize: 1,
61+
_normalTextureLoaded: false,
62+
_pressedTextureLoaded: false,
63+
_disabledTextureLoaded: false,
5764
ctor: function () {
5865
ccs.Widget.prototype.ctor.call(this);
5966
this._buttonNormalRenderer = null;
@@ -77,6 +84,13 @@ ccs.Button = ccs.Widget.extend(/** @lends ccs.Button# */{
7784
this._disabledTextureSize = cc.size(locSize.width, locSize.height);
7885
this._pressedActionEnabled = false;
7986
this._titleColor = cc.white();
87+
this._normalTextureScaleXInSize = 1;
88+
this._normalTextureScaleYInSize = 1;
89+
this._pressedTextureScaleXInSize = 1;
90+
this._pressedTextureScaleYInSize = 1;
91+
this._normalTextureLoaded = false;
92+
this._pressedTextureLoaded = false;
93+
this._disabledTextureLoaded = false;
8094
},
8195

8296
init: function () {
@@ -211,6 +225,7 @@ ccs.Button = ccs.Widget.extend(/** @lends ccs.Button# */{
211225

212226
this._updateDisplay();
213227
this.normalTextureScaleChangedWithSize();
228+
this._normalTextureLoaded = true;
214229
},
215230

216231
/**
@@ -256,6 +271,7 @@ ccs.Button = ccs.Widget.extend(/** @lends ccs.Button# */{
256271
}
257272
this._updateDisplay();
258273
this.pressedTextureScaleChangedWithSize();
274+
this._pressedTextureLoaded = true;
259275
},
260276

261277
/**
@@ -301,6 +317,7 @@ ccs.Button = ccs.Widget.extend(/** @lends ccs.Button# */{
301317
}
302318
this._updateDisplay();
303319
this.disabledTextureScaleChangedWithSize();
320+
this._disabledTextureLoaded = true;
304321
},
305322

306323
_updateDisplay:function(){
@@ -359,40 +376,57 @@ ccs.Button = ccs.Widget.extend(/** @lends ccs.Button# */{
359376
this._buttonNormalRenderer.setVisible(true);
360377
this._buttonClickedRenderer.setVisible(false);
361378
this._buttonDisableRenderer.setVisible(false);
362-
if (this._pressedActionEnabled) {
379+
if (this._pressedTextureLoaded) {
380+
if (this._pressedActionEnabled) {
381+
this._buttonNormalRenderer.stopAllActions();
382+
this._buttonClickedRenderer.stopAllActions();
383+
this._buttonDisableRenderer.stopAllActions();
384+
var zoomAction = cc.ScaleTo.create(0.05, 1.0);
385+
var zoomAction1 = cc.ScaleTo.create(0.05, 1.0);
386+
var zoomAction2 = cc.ScaleTo.create(0.05, 1.0);
387+
this._buttonNormalRenderer.runAction(zoomAction);
388+
this._buttonClickedRenderer.runAction(zoomAction1);
389+
this._buttonDisableRenderer.runAction(zoomAction2);
390+
}
391+
} else {
363392
this._buttonNormalRenderer.stopAllActions();
364-
this._buttonClickedRenderer.stopAllActions();
365-
this._buttonDisableRenderer.stopAllActions();
366-
var zoomAction = cc.ScaleTo.create(0.05, 1.0);
367-
var zoomAction1 = cc.ScaleTo.create(0.05, 1.0);
368-
var zoomAction2 = cc.ScaleTo.create(0.05, 1.0);
393+
var zoomAction = cc.ScaleTo.create(0.05, this._normalTextureScaleXInSize, this._normalTextureScaleYInSize);
369394
this._buttonNormalRenderer.runAction(zoomAction);
370-
this._buttonClickedRenderer.runAction(zoomAction1);
371-
this._buttonDisableRenderer.runAction(zoomAction2);
372395
}
373396
},
374397

375398
onPressStateChangedToPressed: function () {
376-
this._buttonNormalRenderer.setVisible(false);
377-
this._buttonClickedRenderer.setVisible(true);
378-
this._buttonDisableRenderer.setVisible(false);
379-
if (this._pressedActionEnabled) {
399+
if (this._pressedTextureLoaded) {
400+
this._buttonNormalRenderer.setVisible(false);
401+
this._buttonClickedRenderer.setVisible(true);
402+
this._buttonDisableRenderer.setVisible(false);
403+
if (this._pressedActionEnabled) {
404+
this._buttonNormalRenderer.stopAllActions();
405+
this._buttonClickedRenderer.stopAllActions();
406+
this._buttonDisableRenderer.stopAllActions();
407+
var zoomAction = cc.ScaleTo.create(0.05, 1.1);
408+
var zoomAction1 = cc.ScaleTo.create(0.05, 1.1);
409+
var zoomAction2 = cc.ScaleTo.create(0.05, 1.1);
410+
this._buttonNormalRenderer.runAction(zoomAction);
411+
this._buttonClickedRenderer.runAction(zoomAction1);
412+
this._buttonDisableRenderer.runAction(zoomAction2);
413+
}
414+
} else {
415+
this._buttonNormalRenderer.setVisible(true);
416+
this._buttonClickedRenderer.setVisible(true);
417+
this._buttonDisableRenderer.setVisible(false);
380418
this._buttonNormalRenderer.stopAllActions();
381-
this._buttonClickedRenderer.stopAllActions();
382-
this._buttonDisableRenderer.stopAllActions();
383-
var zoomAction = cc.ScaleTo.create(0.05, 1.1);
384-
var zoomAction1 = cc.ScaleTo.create(0.05, 1.1);
385-
var zoomAction2 = cc.ScaleTo.create(0.05, 1.1);
419+
var zoomAction = cc.ScaleTo.create(0.05, this._pressedTextureScaleXInSize + 0.1, this._pressedTextureScaleYInSize + 0.1);
386420
this._buttonNormalRenderer.runAction(zoomAction);
387-
this._buttonClickedRenderer.runAction(zoomAction1);
388-
this._buttonDisableRenderer.runAction(zoomAction2);
389421
}
390422
},
391423

392424
onPressStateChangedToDisabled: function () {
393425
this._buttonNormalRenderer.setVisible(false);
394426
this._buttonClickedRenderer.setVisible(false);
395427
this._buttonDisableRenderer.setVisible(true);
428+
this._buttonNormalRenderer.setScale(this._normalTextureScaleXInSize, this._normalTextureScaleYInSize);
429+
this._buttonClickedRenderer.setScale(this._pressedTextureScaleXInSize, this._pressedTextureScaleYInSize);
396430
},
397431

398432
/**
@@ -505,13 +539,15 @@ ccs.Button = ccs.Widget.extend(/** @lends ccs.Button# */{
505539
if (this._ignoreSize) {
506540
if (!this._scale9Enabled) {
507541
this._buttonNormalRenderer.setScale(1.0);
542+
this._normalTextureScaleXInSize = this._normalTextureScaleYInSize = 1;
508543
this._size.width = this._normalTextureSize.width;
509544
this._size.height = this._normalTextureSize.height;
510545
}
511546
}
512547
else {
513548
if (this._scale9Enabled) {
514549
this._buttonNormalRenderer.setPreferredSize(this._size);
550+
this._normalTextureScaleXInSize = this._normalTextureScaleYInSize = 1;
515551
}
516552
else {
517553
var textureSize = this._normalTextureSize;
@@ -523,6 +559,8 @@ ccs.Button = ccs.Widget.extend(/** @lends ccs.Button# */{
523559
var scaleY = this._size.height / textureSize.height;
524560
this._buttonNormalRenderer.setScaleX(scaleX);
525561
this._buttonNormalRenderer.setScaleY(scaleY);
562+
this._normalTextureScaleXInSize = scaleX;
563+
this._normalTextureScaleYInSize = scaleY;
526564
}
527565
}
528566
},
@@ -531,11 +569,13 @@ ccs.Button = ccs.Widget.extend(/** @lends ccs.Button# */{
531569
if (this._ignoreSize) {
532570
if (!this._scale9Enabled) {
533571
this._buttonClickedRenderer.setScale(1.0);
572+
this._pressedTextureScaleXInSize = this._pressedTextureScaleYInSize = 1;
534573
}
535574
}
536575
else {
537576
if (this._scale9Enabled) {
538577
this._buttonClickedRenderer.setPreferredSize(this._size);
578+
this._pressedTextureScaleXInSize = this._pressedTextureScaleYInSize = 1;
539579
}
540580
else {
541581
var textureSize = this._pressedTextureSize;
@@ -547,6 +587,8 @@ ccs.Button = ccs.Widget.extend(/** @lends ccs.Button# */{
547587
var scaleY = this._size.height / textureSize.height;
548588
this._buttonClickedRenderer.setScaleX(scaleX);
549589
this._buttonClickedRenderer.setScaleY(scaleY);
590+
this._pressedTextureScaleXInSize = scaleX;
591+
this._pressedTextureScaleYInSize = scaleY;
550592
}
551593
}
552594
},

0 commit comments

Comments
 (0)