1 /**
  2  *
  3  * Copyright (c) 2008-2010 Ricardo Quesada
  4  * Copyright (c) 2011-2012 cocos2d-x.org
  5  * Copyright (c) 2013-2014 Chukong Technologies Inc.
  6  *
  7  * Copyright 2012 Stewart Hamilton-Arrandale.
  8  * http://creativewax.co.uk
  9  *
 10  * Modified by Yannick Loriot.
 11  * http://yannickloriot.com
 12  *
 13  * Permission is hereby granted, free of charge, to any person obtaining a copy
 14  * of this software and associated documentation files (the "Software"), to deal
 15  * in the Software without restriction, including without limitation the rights
 16  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 17  * copies of the Software, and to permit persons to whom the Software is
 18  * furnished to do so, subject to the following conditions:
 19  *
 20  * The above copyright notice and this permission notice shall be included in
 21  * all copies or substantial portions of the Software.
 22  *
 23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 24  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 25  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 26  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 27  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 28  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 29  * THE SOFTWARE.
 30  *
 31  *
 32  * converted to Javascript / cocos2d-x by Angus C
 33  */
 34 
 35 /**
 36  * ControlSaturationBrightnessPicker: Saturation brightness picker ui component.
 37  * @class
 38  * @extends cc.Control
 39  *
 40  * @property {Number}       saturation  - <@readonly> Saturation value of the picker
 41  * @property {Number}       brightness  - <@readonly> Brightness value of the picker
 42  * @property {cc.Sprite}    background  - <@readonly> The background sprite
 43  * @property {cc.Sprite}    overlay     - <@readonly> The overlay sprite
 44  * @property {cc.Sprite}    shadow      - <@readonly> The shadow sprite
 45  * @property {cc.Sprite}    slider      - <@readonly> The slider sprite
 46  * @property {cc.Point}     startPos    - <@readonly> The start position of the picker
 47  */
 48 cc.ControlSaturationBrightnessPicker = cc.Control.extend(/** @lends cc.ControlSaturationBrightnessPicker# */{
 49     _saturation:0,
 50     _brightness:0,
 51 
 52     _background:null,
 53     _overlay:null,
 54     _shadow:null,
 55     _slider:null,
 56     _startPos:null,
 57 
 58     _boxPos:0,
 59     _boxSize:0,
 60     _className:"ControlSaturationBrightnessPicker",
 61 
 62     getSaturation:function () {
 63         return this._saturation;
 64     },
 65     getBrightness:function () {
 66         return this._brightness;
 67     },
 68 
 69     //not sure if these need to be there actually. I suppose someone might want to access the sprite?
 70     getBackground:function () {
 71         return this._background;
 72     },
 73     getOverlay:function () {
 74         return this._brightness;
 75     },
 76     getShadow:function () {
 77         return this._shadow;
 78     },
 79     getSlider:function () {
 80         return this._slider;
 81     },
 82     getStartPos:function () {
 83         return this._startPos;
 84     },
 85 
 86     initWithTargetAndPos:function (target, pos) {
 87         if (cc.Control.prototype.init.call(this)) {
 88             // Add background and slider sprites
 89             this._background = cc.ControlUtils.addSpriteToTargetWithPosAndAnchor("colourPickerBackground.png", target, pos, cc.p(0.0, 0.0));
 90             this._overlay = cc.ControlUtils.addSpriteToTargetWithPosAndAnchor("colourPickerOverlay.png", target, pos, cc.p(0.0, 0.0));
 91             this._shadow = cc.ControlUtils.addSpriteToTargetWithPosAndAnchor("colourPickerShadow.png", target, pos, cc.p(0.0, 0.0));
 92             this._slider = cc.ControlUtils.addSpriteToTargetWithPosAndAnchor("colourPicker.png", target, pos, cc.p(0.5, 0.5));
 93 
 94             this._startPos = pos; // starting position of the colour picker
 95             this._boxPos = 35;    // starting position of the virtual box area for picking a colour
 96             this._boxSize = this._background.getContentSize().width / 2;    // the size (width and height) of the virtual box for picking a colour from
 97             return true;
 98         } else
 99             return false;
100     },
101 
102     setEnabled:function (enabled) {
103         cc.Control.prototype.setEnabled.call(this, enabled);
104         if (this._slider) {
105             this._slider.setOpacity(enabled ? 255 : 128);
106         }
107     },
108 
109     updateWithHSV:function (hsv) {
110         var hsvTemp = new cc.HSV();
111         hsvTemp.s = 1;
112         hsvTemp.h = hsv.h;
113         hsvTemp.v = 1;
114 
115         var rgb = cc.ControlUtils.RGBfromHSV(hsvTemp);
116         this._background.setColor(cc.color(0 | (rgb.r * 255), 0 | (rgb.g * 255), 0 | (rgb.b * 255)));
117     },
118     updateDraggerWithHSV:function (hsv) {
119         // Set the position of the slider to the correct saturation and brightness
120         var pos = cc.p(this._startPos.x + this._boxPos + (this._boxSize * (1 - hsv.s)),
121             this._startPos.y + this._boxPos + (this._boxSize * hsv.v));
122 
123         // update
124         this._updateSliderPosition(pos);
125     },
126 
127     _updateSliderPosition:function (sliderPosition) {
128         // Clamp the position of the icon within the circle
129 
130         // Get the center point of the bkgd image
131         var centerX = this._startPos.x + this._background.getBoundingBox().width * 0.5;
132         var centerY = this._startPos.y + this._background.getBoundingBox().height * 0.5;
133 
134         // Work out the distance difference between the location and center
135         var dx = sliderPosition.x - centerX;
136         var dy = sliderPosition.y - centerY;
137         var dist = Math.sqrt(dx * dx + dy * dy);
138 
139         // Update angle by using the direction of the location
140         var angle = Math.atan2(dy, dx);
141 
142         // Set the limit to the slider movement within the colour picker
143         var limit = this._background.getBoundingBox().width * 0.5;
144 
145         // Check distance doesn't exceed the bounds of the circle
146         if (dist > limit) {
147             sliderPosition.x = centerX + limit * Math.cos(angle);
148             sliderPosition.y = centerY + limit * Math.sin(angle);
149         }
150 
151         // Set the position of the dragger
152         this._slider.setPosition(sliderPosition);
153 
154 
155         // Clamp the position within the virtual box for colour selection
156         if (sliderPosition.x < this._startPos.x + this._boxPos)
157             sliderPosition.x = this._startPos.x + this._boxPos;
158         else if (sliderPosition.x > this._startPos.x + this._boxPos + this._boxSize - 1)
159             sliderPosition.x = this._startPos.x + this._boxPos + this._boxSize - 1;
160         if (sliderPosition.y < this._startPos.y + this._boxPos)
161             sliderPosition.y = this._startPos.y + this._boxPos;
162         else if (sliderPosition.y > this._startPos.y + this._boxPos + this._boxSize)
163             sliderPosition.y = this._startPos.y + this._boxPos + this._boxSize;
164 
165         // Use the position / slider width to determin the percentage the dragger is at
166         this._saturation = 1.0 - Math.abs((this._startPos.x + this._boxPos - sliderPosition.x) / this._boxSize);
167         this._brightness = Math.abs((this._startPos.y + this._boxPos - sliderPosition.y) / this._boxSize);
168     },
169 
170     _checkSliderPosition:function (location) {
171         // Clamp the position of the icon within the circle
172         // get the center point of the bkgd image
173         var centerX = this._startPos.x + this._background.getBoundingBox().width * 0.5;
174         var centerY = this._startPos.y + this._background.getBoundingBox().height * 0.5;
175 
176         // work out the distance difference between the location and center
177         var dx = location.x - centerX;
178         var dy = location.y - centerY;
179         var dist = Math.sqrt(dx * dx + dy * dy);
180 
181         // check that the touch location is within the bounding rectangle before sending updates
182         if (dist <= this._background.getBoundingBox().width * 0.5) {
183             this._updateSliderPosition(location);
184             this.sendActionsForControlEvents(cc.CONTROL_EVENT_VALUECHANGED);
185             return true;
186         }
187         return false;
188     },
189 
190     onTouchBegan:function (touch, event) {
191         if (!this.isEnabled() || !this.isVisible())        {
192             return false;
193         }
194         // Get the touch location
195         var touchLocation = this.getTouchLocation(touch);
196 
197         // Check the touch position on the slider
198         return this._checkSliderPosition(touchLocation);
199     },
200 
201     onTouchMoved:function (touch, event) {
202         // Get the touch location
203         var touchLocation = this.getTouchLocation(touch);
204 
205         //small modification: this allows changing of the colour, even if the touch leaves the bounding area
206         //this._updateSliderPosition(touchLocation);
207         //this.sendActionsForControlEvents(cc.CONTROL_EVENT_VALUECHANGED);
208         // Check the touch position on the slider
209         this._checkSliderPosition(touchLocation);
210     }
211 });
212 
213 var _p = cc.ControlSaturationBrightnessPicker.prototype;
214 
215 // Extended properties
216 /** @expose */
217 _p.saturation;
218 cc.defineGetterSetter(_p, "saturation", _p.getSaturation);
219 /** @expose */
220 _p.brightness;
221 cc.defineGetterSetter(_p, "brightness", _p.getBrightness);
222 /** @expose */
223 _p.background;
224 cc.defineGetterSetter(_p, "background", _p.getBackground);
225 /** @expose */
226 _p.overlay;
227 cc.defineGetterSetter(_p, "overlay", _p.getOverlay);
228 /** @expose */
229 _p.shadow;
230 cc.defineGetterSetter(_p, "shadow", _p.getShadow);
231 /** @expose */
232 _p.slider;
233 cc.defineGetterSetter(_p, "slider", _p.getSlider);
234 /** @expose */
235 _p.startPos;
236 cc.defineGetterSetter(_p, "startPos", _p.getStartPos);
237 
238 _p = null;
239 
240 cc.ControlSaturationBrightnessPicker.create = function (target, pos) {
241     var pRet = new cc.ControlSaturationBrightnessPicker();
242     pRet.initWithTargetAndPos(target, pos);
243     return pRet;
244 };