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 cc.ControlColourPicker = cc.Control.extend({
 34     _hsv:null,
 35     _colourPicker:null,
 36     _huePicker:null,
 37 
 38     _background:null,
 39 
 40     hueSliderValueChanged:function (sender, controlEvent) {
 41         this._hsv.h = sender.getHue();
 42 
 43         // Update the value
 44         var rgb = cc.ControlUtils.RGBfromHSV(this._hsv);
 45         cc.Control.prototype.setColor.call(this,cc.c3(0 | (rgb.r * 255), 0 | (rgb.g * 255), 0 | (rgb.b * 255)));
 46 
 47         // Send CCControl callback
 48         this.sendActionsForControlEvents(cc.CONTROL_EVENT_VALUECHANGED);
 49         this._updateControlPicker();
 50     },
 51 
 52     colourSliderValueChanged:function (sender, controlEvent) {
 53         this._hsv.s = sender.getSaturation();
 54         this._hsv.v = sender.getBrightness();
 55 
 56 
 57         // Update the value
 58         var rgb = cc.ControlUtils.RGBfromHSV(this._hsv);
 59         cc.Control.prototype.setColor.call(this,cc.c3(0 | (rgb.r * 255), 0 | (rgb.g * 255), 0 | (rgb.b * 255)));
 60 
 61         // Send CCControl callback
 62         this.sendActionsForControlEvents(cc.CONTROL_EVENT_VALUECHANGED);
 63     },
 64 
 65     setColor:function (color) {
 66         cc.Control.prototype.setColor.call(this,color);
 67         //this._colorValue = color;
 68         var rgba = new cc.RGBA();
 69         rgba.r = color.r / 255.0;
 70         rgba.g = color.g / 255.0;
 71         rgba.b = color.b / 255.0;
 72         rgba.a = 1.0;
 73 
 74         this._hsv = cc.ControlUtils.HSVfromRGB(rgba);
 75         this._updateHueAndControlPicker();
 76     },
 77 
 78     getBackground:function () {
 79         return this._background;
 80     },
 81 
 82     init:function () {
 83         if (cc.Control.prototype.init.call(this)) {
 84             this.setTouchEnabled(true);
 85             // Cache the sprites
 86             cc.SpriteFrameCache.getInstance().addSpriteFrames(res.CCControlColourPickerSpriteSheet_plist);
 87 
 88             // Create the sprite batch node
 89             var spriteSheet = cc.SpriteBatchNode.create(res.CCControlColourPickerSpriteSheet_png);
 90             this.addChild(spriteSheet);
 91 
 92           /*// MIPMAP
 93             //TODO WebGL code
 94             var params = [gl.LINEAR_MIPMAP_NEAREST, gl.LINEAR, gl.REPEAT, gl.CLAMP_TO_EDGE];
 95             spriteSheet.getTexture().setAliasTexParameters();
 96             spriteSheet.getTexture().setTexParameters(params);
 97             spriteSheet.getTexture().generateMipmap();*/
 98 
 99             // Init default color
100             this._hsv = new cc.HSV(0, 0, 0);
101 
102             // Add image
103             this._background = cc.ControlUtils.addSpriteToTargetWithPosAndAnchor("menuColourPanelBackground.png", spriteSheet, cc.PointZero(), cc.p(0.5, 0.5));
104 
105             var backgroundPointZero = cc.pSub(this._background.getPosition(),
106                 cc.p(this._background.getContentSize().width / 2, this._background.getContentSize().height / 2));
107 
108             // Setup panels . currently hard-coded...
109             var hueShift = 8;
110             var colourShift = 28;
111 
112             this._huePicker = cc.ControlHuePicker.create(spriteSheet, cc.p(backgroundPointZero.x + hueShift, backgroundPointZero.y + hueShift));
113             this._colourPicker = cc.ControlSaturationBrightnessPicker.create(spriteSheet, cc.p(backgroundPointZero.x + colourShift, backgroundPointZero.y + colourShift));
114 
115             // Setup events
116             this._huePicker.addTargetWithActionForControlEvents(this, this.hueSliderValueChanged, cc.CONTROL_EVENT_VALUECHANGED);
117             this._colourPicker.addTargetWithActionForControlEvents(this, this.colourSliderValueChanged, cc.CONTROL_EVENT_VALUECHANGED);
118 
119             // Set defaults
120             this._updateHueAndControlPicker();
121             this.addChild(this._huePicker);
122             this.addChild(this._colourPicker);
123 
124             // Set content size
125             this.setContentSize(this._background.getContentSize());
126             return true;
127         }
128         else
129             return false;
130     },
131 
132     _updateControlPicker:function () {
133         this._huePicker.setHue(this._hsv.h);
134         this._colourPicker.updateWithHSV(this._hsv);
135     },
136 
137     _updateHueAndControlPicker:function () {
138         this._huePicker.setHue(this._hsv.h);
139         this._colourPicker.updateWithHSV(this._hsv);
140         this._colourPicker.updateDraggerWithHSV(this._hsv);
141     },
142     setEnabled:function (enabled) {
143         cc.Control.prototype.setEnabled.call(this, enabled);
144         if (this._huePicker != null) {
145             this._huePicker.setEnabled(enabled);
146         }
147         if (this._colourPicker) {
148             this._colourPicker.setEnabled(enabled);
149         }
150     },
151     onTouchBegan:function () {
152         //ignore all touches, handled by children
153         return false;
154     }
155 });
156 
157 cc.ControlColourPicker.create = function () {
158     var pRet = new cc.ControlColourPicker();
159     pRet.init();
160     return pRet;
161 };
162 
163 // compatible with NPM
164 var res = res || {};
165 res.CCControlColourPickerSpriteSheet_plist = res.CCControlColourPickerSpriteSheet_plist || "res/extensions/CCControlColourPickerSpriteSheet.plist";
166 res.CCControlColourPickerSpriteSheet_png = res.CCControlColourPickerSpriteSheet_png || "res/extensions/CCControlColourPickerSpriteSheet.png";