1 /** 2 * 3 * Copyright (c) 2010-2012 cocos2d-x.org 4 * 5 * Copyright 2012 Stewart Hamilton-Arrandale. 6 * http://creativewax.co.uk 7 * 8 * Modified by Yannick Loriot. 9 * http://yannickloriot.com 10 * 11 * Permission is hereby granted, free of charge, to any person obtaining a copy 12 * of this software and associated documentation files (the "Software"), to deal 13 * in the Software without restriction, including without limitation the rights 14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 15 * copies of the Software, and to permit persons to whom the Software is 16 * furnished to do so, subject to the following conditions: 17 * 18 * The above copyright notice and this permission notice shall be included in 19 * all copies or substantial portions of the Software. 20 * 21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 27 * THE SOFTWARE. 28 * 29 * 30 * converted to Javascript / cocos2d-x by Angus C 31 */ 32 33 /** 34 * ControlColourPicker: color picker ui component. 35 * @class 36 * @extends cc.Control 37 * 38 * @property {cc.Sprite} background - <@readonly> The background sprite 39 */ 40 cc.ControlColourPicker = cc.Control.extend(/** @lends cc.ControlColourPicker# */{ 41 _hsv:null, 42 _colourPicker:null, 43 _huePicker:null, 44 45 _background:null, 46 _className:"ControlColourPicker", 47 hueSliderValueChanged:function (sender, controlEvent) { 48 this._hsv.h = sender.getHue(); 49 50 // Update the value 51 var rgb = cc.ControlUtils.RGBfromHSV(this._hsv); 52 cc.Control.prototype.setColor.call(this,cc.color(0 | (rgb.r * 255), 0 | (rgb.g * 255), 0 | (rgb.b * 255))); 53 54 // Send CCControl callback 55 this.sendActionsForControlEvents(cc.CONTROL_EVENT_VALUECHANGED); 56 this._updateControlPicker(); 57 }, 58 59 colourSliderValueChanged:function (sender, controlEvent) { 60 this._hsv.s = sender.getSaturation(); 61 this._hsv.v = sender.getBrightness(); 62 63 64 // Update the value 65 var rgb = cc.ControlUtils.RGBfromHSV(this._hsv); 66 cc.Control.prototype.setColor.call(this,cc.color(0 | (rgb.r * 255), 0 | (rgb.g * 255), 0 | (rgb.b * 255))); 67 68 // Send CCControl callback 69 this.sendActionsForControlEvents(cc.CONTROL_EVENT_VALUECHANGED); 70 }, 71 72 setColor:function (color) { 73 cc.Control.prototype.setColor.call(this,color); 74 //this._colorValue = color; 75 var rgba = new cc.RGBA(); 76 rgba.r = color.r / 255.0; 77 rgba.g = color.g / 255.0; 78 rgba.b = color.b / 255.0; 79 rgba.a = 1.0; 80 81 this._hsv = cc.ControlUtils.HSVfromRGB(rgba); 82 this._updateHueAndControlPicker(); 83 }, 84 85 getBackground:function () { 86 return this._background; 87 }, 88 89 init:function () { 90 if (cc.Control.prototype.init.call(this)) { 91 // Cache the sprites 92 cc.spriteFrameCache.addSpriteFrames(res.CCControlColourPickerSpriteSheet_plist); 93 94 // Create the sprite batch node 95 var spriteSheet = cc.SpriteBatchNode.create(res.CCControlColourPickerSpriteSheet_png); 96 this.addChild(spriteSheet); 97 98 /*// MIPMAP 99 //TODO WebGL code 100 var params = [gl.LINEAR_MIPMAP_NEAREST, gl.LINEAR, gl.REPEAT, gl.CLAMP_TO_EDGE]; 101 spriteSheet.getTexture().setAliasTexParameters(); 102 spriteSheet.getTexture().setTexParameters(params); 103 spriteSheet.getTexture().generateMipmap();*/ 104 105 // Init default color 106 this._hsv = new cc.HSV(0, 0, 0); 107 108 // Add image 109 this._background = cc.ControlUtils.addSpriteToTargetWithPosAndAnchor("menuColourPanelBackground.png", spriteSheet, cc.p(0,0), cc.p(0.5, 0.5)); 110 111 var backgroundPointZero = cc.pSub(this._background.getPosition(), 112 cc.p(this._background.getContentSize().width / 2, this._background.getContentSize().height / 2)); 113 114 // Setup panels . currently hard-coded... 115 var hueShift = 8; 116 var colourShift = 28; 117 118 this._huePicker = cc.ControlHuePicker.create(spriteSheet, cc.p(backgroundPointZero.x + hueShift, backgroundPointZero.y + hueShift)); 119 this._colourPicker = cc.ControlSaturationBrightnessPicker.create(spriteSheet, cc.p(backgroundPointZero.x + colourShift, backgroundPointZero.y + colourShift)); 120 121 // Setup events 122 this._huePicker.addTargetWithActionForControlEvents(this, this.hueSliderValueChanged, cc.CONTROL_EVENT_VALUECHANGED); 123 this._colourPicker.addTargetWithActionForControlEvents(this, this.colourSliderValueChanged, cc.CONTROL_EVENT_VALUECHANGED); 124 125 // Set defaults 126 this._updateHueAndControlPicker(); 127 this.addChild(this._huePicker); 128 this.addChild(this._colourPicker); 129 130 // Set content size 131 this.setContentSize(this._background.getContentSize()); 132 return true; 133 } 134 else 135 return false; 136 }, 137 138 _updateControlPicker:function () { 139 this._huePicker.setHue(this._hsv.h); 140 this._colourPicker.updateWithHSV(this._hsv); 141 }, 142 143 _updateHueAndControlPicker:function () { 144 this._huePicker.setHue(this._hsv.h); 145 this._colourPicker.updateWithHSV(this._hsv); 146 this._colourPicker.updateDraggerWithHSV(this._hsv); 147 }, 148 setEnabled:function (enabled) { 149 cc.Control.prototype.setEnabled.call(this, enabled); 150 if (this._huePicker != null) { 151 this._huePicker.setEnabled(enabled); 152 } 153 if (this._colourPicker) { 154 this._colourPicker.setEnabled(enabled); 155 } 156 }, 157 onTouchBegan:function () { 158 //ignore all touches, handled by children 159 return false; 160 } 161 }); 162 163 window._p = cc.ControlColourPicker.prototype; 164 165 // Extended properties 166 /** @expose */ 167 _p.background; 168 cc.defineGetterSetter(_p, "background", _p.getBackground); 169 170 delete window._p; 171 172 cc.ControlColourPicker.create = function () { 173 var pRet = new cc.ControlColourPicker(); 174 pRet.init(); 175 return pRet; 176 }; 177 178 // compatible with NPM 179 var res = res || {}; 180 res.CCControlColourPickerSpriteSheet_plist = res.CCControlColourPickerSpriteSheet_plist || "res/extensions/CCControlColourPickerSpriteSheet.plist"; 181 res.CCControlColourPickerSpriteSheet_png = res.CCControlColourPickerSpriteSheet_png || "res/extensions/CCControlColourPickerSpriteSheet.png";