/* * TextScrollBar class by Wade Walker. * wadedwalker.com * wadedwalker@gmail.com * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, * copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following * conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. */ package com.wadedwalker.utilities { //---------------------------------------------------------------------------------------------------------\\ import flash.display.Sprite; import flash.geom.Rectangle; import flash.text.TextField; import flash.events.Event; import flash.events.MouseEvent; final public class TextScrollBar { //---------------------------------------------------------------------------------------------------------\\ private var thumb:Sprite; private var track:Sprite; private var textfield:TextField; //---------------------------------------------------------------------------------------------------------\\ final public function TextScrollBar() { // } //------------------------------------------------------------------------------------------------------------------\\ //------------------------------------------------ PUBLIC FUNCTIONS ------------------------------------------------\\ //------------------------------------------------------------------------------------------------------------------\\ //---------------------------------------------------------------------------------------------------------\\ final public function initialize(tf:TextField, thm:Sprite, trk:Sprite):void { thumb = thm; track = trk; textfield = tf; thumb.buttonMode = track.buttonMode = true; thumb.addEventListener(MouseEvent.MOUSE_DOWN, _startDragging); track.addEventListener(MouseEvent.MOUSE_DOWN, _trackClicked); } //------------------------------------------------------------------------------------------------------------------\\ //----------------------------------------------- PRIVATE FUNCTIONS ------------------------------------------------\\ //------------------------------------------------------------------------------------------------------------------\\ //---------------------------------------------------------------------------------------------------------\\ final private function _startDragging(e:MouseEvent):void { thumb.startDrag(false, new Rectangle(thumb.x,track.y,0,int(track.height - thumb.height))); track.stage.addEventListener(MouseEvent.MOUSE_UP, _stopDragging); track.stage.addEventListener(Event.ENTER_FRAME, _onEnterFrameUpdate); } //---------------------------------------------------------------------------------------------------------\\ final 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 (int(track.height - e.localY) > track.height - (thumb.height >> 1)) { thumb.y = int(track.y + e.localY - thumb.height); } //If the mouse clicked on the bottom most area of the track else { thumb.y = int(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)); } //---------------------------------------------------------------------------------------------------------\\ final private function _stopDragging(e:Event):void { thumb.stopDrag(); track.stage.removeEventListener(MouseEvent.MOUSE_UP, _stopDragging); track.stage.removeEventListener(Event.ENTER_FRAME, _onEnterFrameUpdate); } //---------------------------------------------------------------------------------------------------------\\ final private function _onEnterFrameUpdate(e:Event):void { textfield.scrollV = int(((thumb.y - track.y) / (track.height - thumb.height)) * (textfield.maxScrollV)); } } }