forked from scratchfoundation/scratch-flash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScrollbar.as
More file actions
162 lines (146 loc) · 5.13 KB
/
Scrollbar.as
File metadata and controls
162 lines (146 loc) · 5.13 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
155
156
157
158
159
160
161
162
/*
* 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.
*/
package uiwidgets {
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.filters.BevelFilter;
import flash.geom.Point;
import util.DragClient
public class Scrollbar extends Sprite implements DragClient {
public static var color:int = 0xCBCDCF;
public static var sliderColor:int = 0x424447;
public static var cornerRadius:int = 9;
public static var look3D:Boolean = false;
public var w:int, h:int;
private var base:Shape;
private var slider:Shape;
private var positionFraction:Number = 0; // scroll amount (range: 0-1)
private var sliderSizeFraction:Number = 0.1; // slider size, used to show fraction of docutment vislbe (range: 0-1)
private var isVertical:Boolean;
private var dragOffset:int;
private var scrollFunction:Function;
public function Scrollbar(w:int, h:int, scrollFunction:Function = null) {
this.scrollFunction = scrollFunction;
base = new Shape();
slider = new Shape();
addChild(base);
addChild(slider);
if (look3D) addFilters();
alpha = 0.7;
setWidthHeight(w, h);
allowDragging(true);
}
public function scrollValue():Number { return positionFraction }
public function sliderSize():Number { return sliderSizeFraction }
public function update(position:Number, sliderSize:Number = 0):Boolean {
// Update the scrollbar scroll position (0-1) and slider size (0-1)
var newPosition:Number = Math.max(0, Math.min(position, 1));
var newSliderSize:Number = Math.max(0, Math.min(sliderSize, 1));
if ((newPosition != positionFraction) || (newSliderSize != sliderSizeFraction)) {
positionFraction = newPosition;
sliderSizeFraction = newSliderSize;
drawSlider();
slider.visible = newSliderSize < 0.99;
}
return slider.visible;
}
public function setWidthHeight(w:int, h:int):void {
this.w = w;
this.h = h;
base.graphics.clear();
base.graphics.beginFill(color);
base.graphics.drawRoundRect(0, 0, w, h, cornerRadius, cornerRadius);
base.graphics.endFill();
drawSlider();
}
private function drawSlider():void {
var w:int, h:int, maxSize:int;
isVertical = base.height > base.width;
if (isVertical) {
maxSize = base.height;
w = base.width;
h = Math.max(10, Math.min(sliderSizeFraction * maxSize, maxSize));
slider.x = 0;
slider.y = positionFraction * (this.height - h);
} else {
maxSize = base.width;
w = Math.max(10, Math.min(sliderSizeFraction * maxSize, maxSize));
h = base.height;
slider.x = positionFraction * (this.width - w);
slider.y = 0;
}
slider.graphics.clear();
slider.graphics.beginFill(sliderColor);
slider.graphics.drawRoundRect(0, 0, w, h, cornerRadius, cornerRadius);
slider.graphics.endFill();
}
private function addFilters():void {
var f:BevelFilter = new BevelFilter();
f.distance = 1;
f.blurX = f.blurY = 2;
f.highlightAlpha = 0.5;
f.shadowAlpha = 0.5;
f.angle = 225;
base.filters = [f];
f = new BevelFilter();
f.distance = 2;
f.blurX = f.blurY = 4;
f.highlightAlpha = 1.0;
f.shadowAlpha = 0.5;
slider.filters = [f];
}
public function allowDragging(flag:Boolean):void {
if (flag) addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
else removeEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
}
private function mouseDown(evt:MouseEvent):void {
Scratch.app.gh.setDragClient(this, evt);
}
public function dragBegin(evt:MouseEvent):void {
var sliderOrigin:Point = slider.localToGlobal(new Point(0, 0));
if (isVertical) {
dragOffset = evt.stageY - sliderOrigin.y;
dragOffset = Math.max(5, Math.min(dragOffset, slider.height - 5));
} else {
dragOffset = evt.stageX - sliderOrigin.x;
dragOffset = Math.max(5, Math.min(dragOffset, slider.width - 5));
}
dispatchEvent(new Event(Event.SCROLL));
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 = base.height - slider.height;
positionFraction = (localP.y - dragOffset) / range;
} else {
range = base.width - slider.width;
positionFraction = (localP.x - dragOffset) / range;
}
positionFraction = Math.max(0, Math.min(positionFraction, 1));
drawSlider();
if (scrollFunction != null) scrollFunction(positionFraction);
}
public function dragEnd(evt:MouseEvent):void {
dispatchEvent(new Event(Event.COMPLETE));
}
}}