Tworzenie suwaka ( scroll bar ) przy użyciu wyłącznie kodu ActionScript 3.0
Skrypt jest stworzony z 3 plików AS: Scroll.as, ScrollApp.as, StageDetector.as oraz jednego pliku FLA, który używa klasy Scroll . Suwak działa na zasadzie "przyciągnij i upuść" oraz używa mechanizmu scrolowania na myszce.
Scroll to klasa posiadająca wszystkie funkcje suwaka (każdy suwak to jedna klasa Scroll z tekstem jako parametr w konstruktorze).
ScrollApp to aplikacja główna gdzie tworzymy obiekt tekstu oraz przypisujemy go do stworzenia nowego suwaka z klasy Scroll.as
StageDetector posiada funkcje przydatne dla suwaka podczas wychwytywania operacji.
Listing
//kod dla pliku Scroll.as
package {
import flash.display.*;
import flash.text.*;
import flash.events.*;
import flash.utils.*;
import flash.geom.*;
public class Scroll extends Sprite {
private var t:TextField;
private var tWysokosc:Number;
private var scrollTrack:Sprite;
private var scrollThumb:Sprite;
private var scrollbarSzerokosc:int = 15;
private var minimumThumbWysokosc:int = 10;
private var zmieniony:Boolean = false;
private var dragging:Boolean = false;
public function Scroll(pole:TextField) {
t = pole;
tWysokosc = t.height;
scrollTrack = new Sprite();
scrollTrack.graphics.lineStyle();
scrollTrack.graphics.beginFill(0x333333);
scrollTrack.graphics.drawRect(0, 0, 1, 1);
addChild(scrollTrack);
scrollThumb = new Sprite();
scrollThumb.graphics.lineStyle();
scrollThumb.graphics.beginFill(0xAAAAAA);
scrollThumb.graphics.drawRect(0, 0, 1, 1);
addChild(scrollThumb);
t.addEventListener(Event.SCROLL, skrolowanie);
scrollThumb.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
var stageDetector:StageDetector = new StageDetector(this);
stageDetector.addEventListener(StageDetector.ADDED_TO_STAGE, dodanoDoSceny);
stageDetector.addEventListener(StageDetector.REMOVED_FROM_STAGE, usunietoZeSceny);
addEventListener(Event.ENTER_FRAME, enterFrameListener);
zmieniony = true;
}
private function dodanoDoSceny(e:Event):void {
stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
}
private function usunietoZeSceny(e:Event):void {
stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUp);
stage.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
}
private function enterFrameListener(e:Event):void {
if (t.height != tWysokosc) {
zmieniony = true;
tWysokosc = t.height;
if (dragging) {
scrollThumb.stopDrag();
dragging = false;
}
}
if(zmieniony) {
aktualizacja();
zmieniony = false;
}
}
private function skrolowanie(e:Event):void {
if (t.scrollV > t.maxScrollV) {
return;
}
if(!dragging) {
zmieniony = true;
}
}
public function aktualizacja():void {
scrollTrack.x = t.x + t.width;
scrollTrack.y = t.y;
scrollTrack.height = t.height;
scrollTrack.width = scrollbarSzerokosc;
var widoczneLinie:int = t.bottomScrollV - (t.scrollV-1);
if (widoczneLinie < t.numLines) {
scrollThumb.visible = true;
var thumbWysokosc:int = Math.floor(t.height * (widoczneLinie/t.numLines));
scrollThumb.height = Math.max(minimumThumbWysokosc, thumbWysokosc);
scrollThumb.width = scrollbarSzerokosc;
scrollThumb.x = t.x + t.width;
scrollThumb.y = t.y + (scrollTrack.height - scrollThumb.height)*((t.scrollV-1)/(t.maxScrollV-1));
}else{
scrollThumb.visible = false;
}
}
public function mouseDown (e:MouseEvent):void {
var bounds:Rectangle = new Rectangle(t.x + t.width, t.y, 0, t.height-scrollThumb.height);
scrollThumb.startDrag(false, bounds);
dragging = true;
}
public function mouseUp (e:MouseEvent):void {
if (dragging) {
synchTextToScrollThumb();
scrollThumb.stopDrag();
dragging = false;
}
}
public function mouseMove (e:MouseEvent):void {
if(dragging) {
synchTextToScrollThumb();
}
}
public function synchTextToScrollThumb():void {
var scrollThumbMaxY:Number = t.height-scrollThumb.height;
var scrollThumbY:Number = scrollThumb.y - t.y;
t.scrollV = Math.round(t.maxScrollV*(scrollThumbY/scrollThumbMaxY));
}
}
}
//kod dla pliku ScrollApp.as
package {
import flash.display.*;
import flash.text.*;
import flash.events.*;
import flash.utils.*;
public class ScrollApp extends Sprite {
public function ScrollApp() {
var tekst:TextField = new TextField();
tekst.text = "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n12\n13";
tekst.height = 50;
tekst.width = 100;
tekst.border = true;
tekst.background = true;
tekst.type = TextFieldType.INPUT;
tekst.multiline = true;
addChild(tekst);
var mojScroll:Scroll = new Scroll(tekst);
addChild(mojScroll);
}
}
}
//kod dla pliku StageDetector.as
package {
import flash.display.*;
import flash.events.*;
// Monitors a specified display object to see when it is added to or
// removed from the Stage, and broadcasts the correspoding custom events
// StageDetector.ADDED_TO_STAGE and StageDetector.REMOVED_FROM_STAGE.
// USAGE:
// var stageDetector:StageDetector = new StageDetector(someDisplayObject);
// stageDetector.addEventListener(StageDetector.ADDED_TO_STAGE,
// addedToStageListenerFunction);
// stageDetector.addEventListener(StageDetector.REMOVED_FROM_STAGE,
// removedFromStageListenerFunction);
public class StageDetector extends EventDispatcher {
// Event constants
public static const ADDED_TO_STAGE:String = "ADDED_TO_STAGE";
public static const REMOVED_FROM_STAGE:String = "REMOVED_FROM_STAGE";
// The object for which ADDED_TO_STAGE and REMOVED_FROM_STAGE events
// will be generated
private var watchedObject:DisplayObject = null;
// The root of the display hierarchy that contains watchedObject
private var watchedRoot:DisplayObject = null;
// Flag indicating whether watchedObject is currently on the
// display list
private var onStage:Boolean = false;
// Constructor
public function StageDetector (objectToWatch:DisplayObject) {
// Begin monitoring the specified object
setWatchedObject(objectToWatch);
}
// Begins monitoring the specified object to see when it is added to or
// removed from the display list
public function setWatchedObject (objectToWatch:DisplayObject):void {
// Store the object being monitored
watchedObject = objectToWatch;
// Note whether watchedObject is currently on the display list
if (watchedObject.stage != null) {
onStage = true;
}
// Find the root of the display hierarchy containing the
// watchedObject, and register with it for ADDED/REMOVED events.
// By observing where watchedObject's root is added and removed,
// we'll determine whether watchedObject is on or off the
// display list.
setWatchedRoot(findWatchedObjectRoot());
}
// Returns a reference to the object being monitored
public function getWatchedObject ():DisplayObject {
return watchedObject;
}
// Frees this StageDetector object's resources. Call this method before
// discarding a StageDetector object.
public function dispose ():void {
clearWatchedRoot();
watchedObject = null;
}
// Handles Event.ADDED events targeted at the root of
// watchedObject's display hierarchy
private function addedListener (e:Event):void {
// If the current watchedRoot was added...
if (e.eventPhase == EventPhase.AT_TARGET) {
// ...check if watchedObject is now on the display list
if (watchedObject.stage != null) {
// Note that watchedObject is now on the display list
onStage = true;
// Notify listeners that watchedObject is now
// on the display list
dispatchEvent(new Event(StageDetector.ADDED_TO_STAGE));
}
// watchedRoot was added to another container, so there's
// now a new root of the display hierarchy containing
// watchedObject. Find that new root, and register with it
// for ADDED and REMOVED events.
setWatchedRoot(findWatchedObjectRoot());
}
}
// Handles Event.REMOVED events for the root of
// watchedObject's display hierarchy
private function removedListener (e:Event):void {
// If watchedObject is on the display list...
if (onStage) {
// ...check if watchedObject or one of its ancestors was removed
var wasRemoved:Boolean = false;
var ancestor:DisplayObject = watchedObject;
var target:DisplayObject = DisplayObject(e.target);
while (ancestor != null) {
if (target == ancestor) {
wasRemoved = true;
break;
}
ancestor = ancestor.parent;
}
// If watchedObject or one of its ancestors was removed...
if (wasRemoved) {
// ...register for ADDED and REMOVED events from the removed
// object (which is the new root of watchedObject's display
// hierarchy).
setWatchedRoot(target);
// Note that watchedObject is not on the display list anymore
onStage = false;
// Notify listeners that watchedObject was removed from the Stage
dispatchEvent(new Event(StageDetector.REMOVED_FROM_STAGE));
}
}
}
// Returns the root of the display hierarchy that currently contains
// watchedObject
private function findWatchedObjectRoot ():DisplayObject {
var watchedObjectRoot:DisplayObject = watchedObject;
while (watchedObjectRoot.parent != null) {
watchedObjectRoot = watchedObjectRoot.parent;
}
return watchedObjectRoot;
}
// Begins listening for ADDED and REMOVED events targeted at the root of
// watchedObject's display hierarchy
private function setWatchedRoot (newWatchedRoot:DisplayObject):void {
clearWatchedRoot();
watchedRoot = newWatchedRoot;
registerListeners(watchedRoot);
}
// Removes event listeners from watchedRoot, and removes
// this StageDetector object's reference to watchedRoot
private function clearWatchedRoot ():void {
if (watchedRoot != null) {
unregisterListeners(watchedRoot);
watchedRoot = null;
}
}
// Registers ADDED and REMOVED event listeners with watchedRoot
private function registerListeners (target:DisplayObject):void {
target.addEventListener(Event.ADDED, addedListener);
target.addEventListener(Event.REMOVED, removedListener);
}
// Unregisters ADDED and REMOVED event listeners from watchedRoot
private function unregisterListeners (target:DisplayObject):void {
target.removeEventListener(Event.ADDED, addedListener);
target.removeEventListener(Event.REMOVED, removedListener);
}
}
}
package {
import flash.display.*;
import flash.text.*;
import flash.events.*;
import flash.utils.*;
import flash.geom.*;
public class Scroll extends Sprite {
private var t:TextField;
private var tWysokosc:Number;
private var scrollTrack:Sprite;
private var scrollThumb:Sprite;
private var scrollbarSzerokosc:int = 15;
private var minimumThumbWysokosc:int = 10;
private var zmieniony:Boolean = false;
private var dragging:Boolean = false;
public function Scroll(pole:TextField) {
t = pole;
tWysokosc = t.height;
scrollTrack = new Sprite();
scrollTrack.graphics.lineStyle();
scrollTrack.graphics.beginFill(0x333333);
scrollTrack.graphics.drawRect(0, 0, 1, 1);
addChild(scrollTrack);
scrollThumb = new Sprite();
scrollThumb.graphics.lineStyle();
scrollThumb.graphics.beginFill(0xAAAAAA);
scrollThumb.graphics.drawRect(0, 0, 1, 1);
addChild(scrollThumb);
t.addEventListener(Event.SCROLL, skrolowanie);
scrollThumb.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
var stageDetector:StageDetector = new StageDetector(this);
stageDetector.addEventListener(StageDetector.ADDED_TO_STAGE, dodanoDoSceny);
stageDetector.addEventListener(StageDetector.REMOVED_FROM_STAGE, usunietoZeSceny);
addEventListener(Event.ENTER_FRAME, enterFrameListener);
zmieniony = true;
}
private function dodanoDoSceny(e:Event):void {
stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
}
private function usunietoZeSceny(e:Event):void {
stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUp);
stage.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
}
private function enterFrameListener(e:Event):void {
if (t.height != tWysokosc) {
zmieniony = true;
tWysokosc = t.height;
if (dragging) {
scrollThumb.stopDrag();
dragging = false;
}
}
if(zmieniony) {
aktualizacja();
zmieniony = false;
}
}
private function skrolowanie(e:Event):void {
if (t.scrollV > t.maxScrollV) {
return;
}
if(!dragging) {
zmieniony = true;
}
}
public function aktualizacja():void {
scrollTrack.x = t.x + t.width;
scrollTrack.y = t.y;
scrollTrack.height = t.height;
scrollTrack.width = scrollbarSzerokosc;
var widoczneLinie:int = t.bottomScrollV - (t.scrollV-1);
if (widoczneLinie < t.numLines) {
scrollThumb.visible = true;
var thumbWysokosc:int = Math.floor(t.height * (widoczneLinie/t.numLines));
scrollThumb.height = Math.max(minimumThumbWysokosc, thumbWysokosc);
scrollThumb.width = scrollbarSzerokosc;
scrollThumb.x = t.x + t.width;
scrollThumb.y = t.y + (scrollTrack.height - scrollThumb.height)*((t.scrollV-1)/(t.maxScrollV-1));
}else{
scrollThumb.visible = false;
}
}
public function mouseDown (e:MouseEvent):void {
var bounds:Rectangle = new Rectangle(t.x + t.width, t.y, 0, t.height-scrollThumb.height);
scrollThumb.startDrag(false, bounds);
dragging = true;
}
public function mouseUp (e:MouseEvent):void {
if (dragging) {
synchTextToScrollThumb();
scrollThumb.stopDrag();
dragging = false;
}
}
public function mouseMove (e:MouseEvent):void {
if(dragging) {
synchTextToScrollThumb();
}
}
public function synchTextToScrollThumb():void {
var scrollThumbMaxY:Number = t.height-scrollThumb.height;
var scrollThumbY:Number = scrollThumb.y - t.y;
t.scrollV = Math.round(t.maxScrollV*(scrollThumbY/scrollThumbMaxY));
}
}
}
//kod dla pliku ScrollApp.as
package {
import flash.display.*;
import flash.text.*;
import flash.events.*;
import flash.utils.*;
public class ScrollApp extends Sprite {
public function ScrollApp() {
var tekst:TextField = new TextField();
tekst.text = "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n12\n13";
tekst.height = 50;
tekst.width = 100;
tekst.border = true;
tekst.background = true;
tekst.type = TextFieldType.INPUT;
tekst.multiline = true;
addChild(tekst);
var mojScroll:Scroll = new Scroll(tekst);
addChild(mojScroll);
}
}
}
//kod dla pliku StageDetector.as
package {
import flash.display.*;
import flash.events.*;
// Monitors a specified display object to see when it is added to or
// removed from the Stage, and broadcasts the correspoding custom events
// StageDetector.ADDED_TO_STAGE and StageDetector.REMOVED_FROM_STAGE.
// USAGE:
// var stageDetector:StageDetector = new StageDetector(someDisplayObject);
// stageDetector.addEventListener(StageDetector.ADDED_TO_STAGE,
// addedToStageListenerFunction);
// stageDetector.addEventListener(StageDetector.REMOVED_FROM_STAGE,
// removedFromStageListenerFunction);
public class StageDetector extends EventDispatcher {
// Event constants
public static const ADDED_TO_STAGE:String = "ADDED_TO_STAGE";
public static const REMOVED_FROM_STAGE:String = "REMOVED_FROM_STAGE";
// The object for which ADDED_TO_STAGE and REMOVED_FROM_STAGE events
// will be generated
private var watchedObject:DisplayObject = null;
// The root of the display hierarchy that contains watchedObject
private var watchedRoot:DisplayObject = null;
// Flag indicating whether watchedObject is currently on the
// display list
private var onStage:Boolean = false;
// Constructor
public function StageDetector (objectToWatch:DisplayObject) {
// Begin monitoring the specified object
setWatchedObject(objectToWatch);
}
// Begins monitoring the specified object to see when it is added to or
// removed from the display list
public function setWatchedObject (objectToWatch:DisplayObject):void {
// Store the object being monitored
watchedObject = objectToWatch;
// Note whether watchedObject is currently on the display list
if (watchedObject.stage != null) {
onStage = true;
}
// Find the root of the display hierarchy containing the
// watchedObject, and register with it for ADDED/REMOVED events.
// By observing where watchedObject's root is added and removed,
// we'll determine whether watchedObject is on or off the
// display list.
setWatchedRoot(findWatchedObjectRoot());
}
// Returns a reference to the object being monitored
public function getWatchedObject ():DisplayObject {
return watchedObject;
}
// Frees this StageDetector object's resources. Call this method before
// discarding a StageDetector object.
public function dispose ():void {
clearWatchedRoot();
watchedObject = null;
}
// Handles Event.ADDED events targeted at the root of
// watchedObject's display hierarchy
private function addedListener (e:Event):void {
// If the current watchedRoot was added...
if (e.eventPhase == EventPhase.AT_TARGET) {
// ...check if watchedObject is now on the display list
if (watchedObject.stage != null) {
// Note that watchedObject is now on the display list
onStage = true;
// Notify listeners that watchedObject is now
// on the display list
dispatchEvent(new Event(StageDetector.ADDED_TO_STAGE));
}
// watchedRoot was added to another container, so there's
// now a new root of the display hierarchy containing
// watchedObject. Find that new root, and register with it
// for ADDED and REMOVED events.
setWatchedRoot(findWatchedObjectRoot());
}
}
// Handles Event.REMOVED events for the root of
// watchedObject's display hierarchy
private function removedListener (e:Event):void {
// If watchedObject is on the display list...
if (onStage) {
// ...check if watchedObject or one of its ancestors was removed
var wasRemoved:Boolean = false;
var ancestor:DisplayObject = watchedObject;
var target:DisplayObject = DisplayObject(e.target);
while (ancestor != null) {
if (target == ancestor) {
wasRemoved = true;
break;
}
ancestor = ancestor.parent;
}
// If watchedObject or one of its ancestors was removed...
if (wasRemoved) {
// ...register for ADDED and REMOVED events from the removed
// object (which is the new root of watchedObject's display
// hierarchy).
setWatchedRoot(target);
// Note that watchedObject is not on the display list anymore
onStage = false;
// Notify listeners that watchedObject was removed from the Stage
dispatchEvent(new Event(StageDetector.REMOVED_FROM_STAGE));
}
}
}
// Returns the root of the display hierarchy that currently contains
// watchedObject
private function findWatchedObjectRoot ():DisplayObject {
var watchedObjectRoot:DisplayObject = watchedObject;
while (watchedObjectRoot.parent != null) {
watchedObjectRoot = watchedObjectRoot.parent;
}
return watchedObjectRoot;
}
// Begins listening for ADDED and REMOVED events targeted at the root of
// watchedObject's display hierarchy
private function setWatchedRoot (newWatchedRoot:DisplayObject):void {
clearWatchedRoot();
watchedRoot = newWatchedRoot;
registerListeners(watchedRoot);
}
// Removes event listeners from watchedRoot, and removes
// this StageDetector object's reference to watchedRoot
private function clearWatchedRoot ():void {
if (watchedRoot != null) {
unregisterListeners(watchedRoot);
watchedRoot = null;
}
}
// Registers ADDED and REMOVED event listeners with watchedRoot
private function registerListeners (target:DisplayObject):void {
target.addEventListener(Event.ADDED, addedListener);
target.addEventListener(Event.REMOVED, removedListener);
}
// Unregisters ADDED and REMOVED event listeners from watchedRoot
private function unregisterListeners (target:DisplayObject):void {
target.removeEventListener(Event.ADDED, addedListener);
target.removeEventListener(Event.REMOVED, removedListener);
}
}
}
Dodano przez: divix
Ranga: Administrator serwisu Punktów: 0
Ranga: Administrator serwisu Punktów: 0
Komentarze użytkowników
:: Losowe artykuły
:: Wymiana linków
Modowe inspiracje |
Android Gry i Aplikacje |
ZaplanujTransport.pl: Przeprowadzki, transport, aukcje |
Logo dla firmy |
Change Tires - Car Weather Forecast Reminder |
Laminas: MVC Framework for PHP |
IT Books Reviews and Programming: JS, JAVA, PHP, ANDROID, CSS |
Katalog roślin |
Programming articles: JAVA, PHP, C++, Python, JavaScript |
Kancelaria Adwokacka Łukasz Huszno