Java Reference
In-Depth Information
var
controlX
: Number =
bind (slider.value-slider.minimum)/
range * slider.width;
...
...
onMousePressed
: function(e: MouseEvent) : Void {
saveX = controlX;
}
As the mouse is dragged and finally released, we calculate the percentage of the
mouse horizontal position to the total width of the
Slider
and use that percent-
age to adjust the value. The mouse event
dragX
variable actually represents a
delta since the drag started, so we must add this to the saved location for the
knob to get a new location for the knob. We do not change the knob location
directly because the knob's location
,
held in the
controlX
variable, is bound to
the slider's
value
. Instead, we merely set the
value
based on the percentage of
the total slider width. This, in turn, results in the knob moving to the new loca-
tion. Notice, we have to limit the percentage derived from the mouse position to
between 0.0 and 1.0, inclusive. This is because the mouse may be dragged
beyond the boundaries of the
Slider
.
onMouseReleased
: function(e: MouseEvent) : Void {
if(inDrag) {
var newX = saveX + e.dragX;
var per = newX / slider.width;
per = if(per < 0) 0.0 else
if (per > 1.0) 1.0 else per;
slider.value = calcValue(per);
inDrag = false;
}
}
onMouseDragged
: function(e: MouseEvent) : Void {
inDrag = true;
var newX = saveX + e.dragX;
var per = newX / slider.width;
per = if(per < 0) 0.0 else
if (per > 1.0) 1.0 else per;
slider.value = calcValue(per);
}
The private function,
calcValue()
, takes the width percentage and applies it to
the value range to determine the new slider
value
. This function also rounds the
actual value based on the
Slider
's
snapTo
variable. For example, if
snapTo
is
1.0, the value will be rounded to the nearest whole number. If
snapTo
is 0.5, the
resulting value is rounded to the nearest half. For instance, a value of 50.37
becomes 50.5, whereas 50.21 becomes 50.0. Listing 12.23 shows how this is done.
Search WWH ::
Custom Search