forked from scratchfoundation/scratch-flash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlider.as
More file actions
154 lines (135 loc) · 4.81 KB
/
Slider.as
File metadata and controls
154 lines (135 loc) · 4.81 KB
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
/*
* Scratch Project Editor and Player
* Copyright (C) 2014 Massachusetts Institute of Technology
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
// Slider.as
// John Maloney, February 2013
//
// A simple slider for a fractional value from 0-1. Either vertical or horizontal, depending on its aspect ratio.
// The client can supply and optional function to be called when the value is changed.
package uiwidgets {
import flash.display.*;
import flash.events.*;
import flash.geom.*;
import util.DragClient;
public class Slider extends Sprite implements DragClient {
public var slotColor:int = 0xBBBDBF;
public var slotColor2:int = -1; // if >= 0, fill with linear gradient from slotColor to slotColor2
private var slot:Shape;
private var knob:Shape;
private var positionFraction:Number = 0; // range: 0-1
private var isVertical:Boolean;
private var dragOffset:int;
private var scrollFunction:Function;
private var minValue:Number;
private var maxValue:Number;
public function Slider(w:int, h:int, scrollFunction:Function = null) {
this.scrollFunction = scrollFunction;
minValue = 0;
maxValue = 1;
addChild(slot = new Shape());
addChild(knob = new Shape());
setWidthHeight(w, h);
moveKnob();
addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
}
public function get min():Number { return minValue; }
public function set min(n:Number):void { minValue = n; }
public function get max():Number { return maxValue; }
public function set max(n:Number):void { maxValue = n; }
public function get value():Number { return positionFraction * (maxValue - minValue) + minValue; }
public function set value(n:Number):void {
// Update the slider value (0-1).
var newFraction:Number = Math.max(0, Math.min((n - minValue) / (maxValue - minValue), 1));
if (newFraction != positionFraction) {
positionFraction = newFraction;
moveKnob();
}
}
public function setWidthHeight(w:int, h:int):void {
isVertical = h > w;
drawSlot(w, h);
drawKnob(w, h);
}
private function drawSlot(w:int, h:int):void {
const slotRadius:int = 9;
var g:Graphics = slot.graphics;
g.clear();
if (slotColor2 >= 0) {
var m:Matrix = new Matrix();
m.createGradientBox(w, h, (isVertical ? -Math.PI / 2 : Math.PI), 0, 0);
g.beginGradientFill(GradientType.LINEAR, [slotColor, slotColor2], [1, 1], [0, 255], m);
} else {
g.beginFill(slotColor);
}
g.drawRoundRect(0, 0, w, h, slotRadius, slotRadius);
g.endFill();
}
private function drawKnob(w:int, h:int):void {
const knobOutline:int = 0x707070;
const knobFill:int = 0xEBEBEB;
const knobRadius:int = 6; // 3;
var knobW:int = isVertical ? w + 7 : 7;
var knobH:int = isVertical ? 7 : h + 7;
var g:Graphics = knob.graphics;
g.clear();
g.lineStyle(1, knobOutline);
g.beginFill(knobFill);
g.drawRoundRect(0.5, 0.5, knobW, knobH, knobRadius, knobRadius);
g.endFill();
}
private function moveKnob():void {
if (isVertical) {
knob.x = -4;
knob.y = Math.round((1 - positionFraction) * (slot.height - knob.height));
} else {
knob.x = Math.round(positionFraction * (slot.width - knob.width));
knob.y = -4;
}
}
private function mouseDown(evt:MouseEvent):void {
Scratch.app.gh.setDragClient(this, evt);
}
public function dragBegin(evt:MouseEvent):void {
var sliderOrigin:Point = knob.localToGlobal(new Point(0, 0));
if (isVertical) {
dragOffset = evt.stageY - sliderOrigin.y;
dragOffset = Math.max(5, Math.min(dragOffset, knob.height - 5));
} else {
dragOffset = evt.stageX - sliderOrigin.x;
dragOffset = Math.max(5, Math.min(dragOffset, knob.width - 5));
}
dragMove(evt);
}
public function dragMove(evt:MouseEvent):void {
var range:int, frac:Number;
var localP:Point = globalToLocal(new Point(evt.stageX, evt.stageY));
if (isVertical) {
range = slot.height - knob.height;
positionFraction = 1 - (localP.y - dragOffset) / range;
} else {
range = slot.width - knob.width;
positionFraction = (localP.x - dragOffset) / range;
}
positionFraction = Math.max(0, Math.min(positionFraction, 1));
moveKnob();
if (scrollFunction != null) scrollFunction(this.value);
}
public function dragEnd(evt:MouseEvent):void {
dispatchEvent(new Event(Event.COMPLETE));
}
}}