/
RotarySelector.hx
292 lines (250 loc) · 7.29 KB
/
RotarySelector.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/**
* RotarySelector.as
* Keith Peters
* version 0.97
*
* A rotary selector component for choosing among different values.
*
* Copyright (c) 2009 Keith Peters
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package minimalcomps;
import flash.display.DisplayObjectContainer;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
class RotarySelector extends Component {
public var choice(getChoice, setChoice) : UInt;
public var labelMode(getLabelMode, setLabelMode) : String;
public var numChoices(getNumChoices, setNumChoices) : UInt;
public inline static var ALPHABETIC:String = "alphabetic";
public inline static var NUMERIC:String = "numeric";
public inline static var NONE:String = "none";
public inline static var ROMAN:String = "roman";
var _label:Label;
var _labelText:String ;
var _knob:Sprite;
var _numChoices:Int ;
var _choice:Int ;
var _labels:Sprite;
var _labelMode:String ;
/**
* Constructor
* @param parent The parent DisplayObjectContainer on which to add this CheckBox.
* @param xpos The x position to place this component.
* @param ypos The y position to place this component.
* @param label String containing the label for this component.
* @param defaultHandler The event handling function to handle the default event for this component (change in this case).
*/
public function new(?parent:DisplayObjectContainer = null, ?xpos:Float = 0, ?ypos:Float = 0, ?label:String = "", ?defaultHandler:Dynamic = null)
{
_labelText = "";
_numChoices = 2;
_choice = 0;
_labelMode = ALPHABETIC;
_labelText = label;
super(parent, xpos, ypos);
if(defaultHandler != null)
{
addEventListener(Event.CHANGE, defaultHandler);
}
}
/**
* Initializes the component.
*/
override function init() {
super.init();
setSize(60, 60);
}
/**
* Creates the children for this component
*/
override function addChildren() {
_knob = new Sprite();
_knob.buttonMode = true;
_knob.useHandCursor = true;
addChild(_knob);
_label = new Label();
_label.autoSize = true;
addChild(_label);
_labels = new Sprite();
addChild(_labels);
_knob.addEventListener(MouseEvent.CLICK, onClick);
}
/**
* Decrements the index of the current choice.
*/
function decrement() {
if(_choice > 0)
{
_choice--;
draw();
dispatchEvent(new Event(Event.CHANGE));
}
}
/**
* Increments the index of the current choice.
*/
function increment() {
if(_choice < _numChoices - 1)
{
_choice++;
draw();
dispatchEvent(new Event(Event.CHANGE));
}
}
/**
* Removes old labels.
*/
function resetLabels() {
while(_labels.numChildren > 0)
{
_labels.removeChildAt(0);
}
_labels.x = _width / 2 - 5;
_labels.y = _height / 2 - 10;
}
/**
* Draw the knob at the specified radius.
* @param radius The radius with which said knob will be drawn.
*/
function drawKnob(radius:Float) {
_knob.graphics.clear();
_knob.graphics.beginFill(Style.BACKGROUND);
_knob.graphics.drawCircle(0, 0, radius);
_knob.graphics.endFill();
_knob.graphics.beginFill(Style.BUTTON_FACE);
_knob.graphics.drawCircle(0, 0, radius - 2);
_knob.x = _width / 2;
_knob.y = _height / 2;
}
///////////////////////////////////
// public methods
///////////////////////////////////
/**
* Draws the visual ui of the component.
*/
public override function draw() {
super.draw();
var radius:Int = Math.min(_width, _height) / 2;
drawKnob(radius);
resetLabels();
var arc:Float = Math.PI * 1.5 / _numChoices; // the angle between each choice
var start:Int = - Math.PI / 2 - arc * (_numChoices - 1) / 2; // the starting angle for choice 0
graphics.clear();
graphics.lineStyle(4, Style.BACKGROUND, .5);
for(i in 0..._numChoices)
{
var angle:Float = start + arc * i;
var sin:Float = Math.sin(angle);
var cos:Float = Math.cos(angle);
graphics.moveTo(_knob.x, _knob.y);
graphics.lineTo(_knob.x + cos * (radius + 2), _knob.y + sin * (radius + 2));
var lab:Label = new Label(_labels, cos * (radius + 10), sin * (radius + 10));
lab.mouseEnabled = true;
lab.buttonMode = true;
lab.useHandCursor = true;
lab.addEventListener(MouseEvent.CLICK, onLabelClick);
if(_labelMode == ALPHABETIC)
{
lab.text = String.fromCharCode(65 + i);
}
else if(_labelMode == NUMERIC)
{
lab.text = (i + 1).toString();
}
else if(_labelMode == ROMAN)
{
var chars:Array<Dynamic> = ["I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X"];
lab.text = chars[i];
}
if(i != _choice)
{
lab.alpha = 0.5;
}
}
angle = start + arc * _choice;
graphics.lineStyle(4, Style.LABEL_TEXT);
graphics.moveTo(_knob.x, _knob.y);
graphics.lineTo(_knob.x + Math.cos(angle) * (radius + 2), _knob.y + Math.sin(angle) * (radius + 2));
_label.text = _labelText;
_label.draw();
_label.x = _width / 2 - _label.width / 2;
_label.y = _height + 2;
}
///////////////////////////////////
// event handler
///////////////////////////////////
/**
* Internal click handler.
* @param event The MouseEvent passed by the system.
*/
function onClick(event:MouseEvent) {
if(mouseX < _width / 2)
{
decrement();
}
else
{
increment();
}
}
function onLabelClick(event:Event) {
var lab:Label = cast( event.target, Label);
choice = _labels.getChildIndex(lab);
}
///////////////////////////////////
// getter/setters
///////////////////////////////////
/**
* Gets / sets the number of available choices (maximum of 10).
*/
public function setNumChoices(value:UInt):UInt {
_numChoices = Math.min(value, 10);
draw();
return value;
}
public function getNumChoices():UInt {
return _numChoices;
}
/**
* Gets / sets the current choice, keeping it in range of 0 to numChoices - 1.
*/
public function setChoice(value:UInt):UInt {
_choice = Math.max(0, Math.min(_numChoices - 1, value));
draw();
dispatchEvent(new Event(Event.CHANGE));
return value;
}
public function getChoice():UInt {
return _choice;
}
/**
* Specifies what will be used as labels for each choice. Valid values are "alphabetic", "numeric", and "none".
*/
public function setLabelMode(value:String):String {
_labelMode = value;
draw();
return value;
}
public function getLabelMode():String {
return _labelMode;
}
}