package com.wadedwalker { //--------------------------------------------------------------------------------------------------------------------------------------\\ //--------------------------------------------------------------- Imports --------------------------------------------------------------\\ //--------------------------------------------------------------------------------------------------------------------------------------\\ import flash.display.Sprite; import flash.geom.Rectangle; import flash.text.TextField; import flash.events.Event; import flash.events.MouseEvent; public class TextScrollBar { //--------------------------------------------------------------------------------------------------------------------------------------\\ //----------------------------------------------------------- Variables ----------------------------------------------------------------\\ //--------------------------------------------------------------------------------------------------------------------------------------\\ private var thumb:Sprite; private var track:Sprite; private var textfield:TextField; private var dragging:Boolean; //--------------------------------------------------------------------------------------------------------------------------------------\\ public function TextScrollBar() { // } //--------------------------------------------------------------------------------------------------------------------------------------\\ public function initialize(tf:TextField, thm:Sprite, trk:Sprite):void { thumb = thm; track = trk; textfield = tf; dragging = false; thumb.buttonMode = track.buttonMode = true; thumb.addEventListener(MouseEvent.ROLL_OVER, onRollEvent); track.addEventListener(MouseEvent.ROLL_OVER, onRollEvent); thumb.addEventListener(MouseEvent.ROLL_OUT, onRollEvent); track.addEventListener(MouseEvent.ROLL_OUT, onRollEvent); thumb.addEventListener(MouseEvent.MOUSE_DOWN, startDragging); track.addEventListener(MouseEvent.MOUSE_DOWN, trackClicked); } //--------------------------------------------------------------------------------------------------------------------------------------\\ private function startDragging(e:MouseEvent):void { dragging = true; thumb.startDrag(false, new Rectangle(thumb.x,track.y,0,track.height - thumb.height)); track.stage.addEventListener(MouseEvent.MOUSE_UP, stopDragging); track.stage.addEventListener(Event.ENTER_FRAME, update); } //--------------------------------------------------------------------------------------------------------------------------------------\\ private function trackClicked(e:MouseEvent):void { //If the mouse was clicked in the top most area of the track if (e.localY < ((thumb.height >> 1))) { thumb.y = track.y; } //If the track was clicked anywhere in the main area of the track else if (track.height - e.localY > track.height - (thumb.height >> 1)) { thumb.y = track.y + e.localY - thumb.height; } //If the mouse clicked on the bottom most area of the track else { thumb.y = track.y + e.localY - (thumb.height >> 1); } //Repositioning the text within the textfield based on the current location of the thumb on the track textfield.scrollV = int(((thumb.y - track.y) / (track.height - thumb.height)) * (textfield.maxScrollV)); } //--------------------------------------------------------------------------------------------------------------------------------------\\ private function stopDragging(e:Event):void { dragging = false; thumb.stopDrag(); track.stage.removeEventListener(MouseEvent.MOUSE_UP, stopDragging); track.stage.removeEventListener(Event.ENTER_FRAME, update); } //--------------------------------------------------------------------------------------------------------------------------------------\\ private function onRollEvent(e:MouseEvent):void { // } //--------------------------------------------------------------------------------------------------------------------------------------\\ private function update(e:Event):void { textfield.scrollV = int(((thumb.y - track.y) / (track.height - thumb.height)) * (textfield.maxScrollV)); } } }