| 1 | /** |
|---|
| 2 | * This draws a simple scrollbar next to a textfield/mask combination. |
|---|
| 3 | **/ |
|---|
| 4 | package com.jeroenwijering.utils { |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | import com.jeroenwijering.utils.Draw; |
|---|
| 8 | import flash.display.Sprite; |
|---|
| 9 | import flash.events.MouseEvent; |
|---|
| 10 | import flash.text.TextField; |
|---|
| 11 | import flash.utils.clearInterval; |
|---|
| 12 | import flash.utils.setInterval; |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | public class Scrollbar { |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | /** Textfield that has to be scrolled. **/ |
|---|
| 19 | private var field:TextField; |
|---|
| 20 | /** Mask for the scrolleable field. **/ |
|---|
| 21 | private var mask:Sprite; |
|---|
| 22 | /** Clip in which the scrollbar is drawn. **/ |
|---|
| 23 | private var scrollbar:Sprite; |
|---|
| 24 | /** Icon of the scrollbar. **/ |
|---|
| 25 | private var icon:Sprite; |
|---|
| 26 | /** Color of the scrollbar. **/ |
|---|
| 27 | private var color:String; |
|---|
| 28 | /** Proportion between the field and mask. **/ |
|---|
| 29 | private var proportion:Number; |
|---|
| 30 | /** Interval ID for smooth scrolling. **/ |
|---|
| 31 | private var interval:Number; |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | /** |
|---|
| 35 | * Constructor; initializes the scrollbar parameters. |
|---|
| 36 | * |
|---|
| 37 | * @param fld The field that has to be scrolled. |
|---|
| 38 | * @param msk The field to use as mask for the scrolleable field. |
|---|
| 39 | * @param icl The color of the icon of the scrollbar (the part that moves). |
|---|
| 40 | * @param rcl The color of the rail of the scrollbar (the background). |
|---|
| 41 | **/ |
|---|
| 42 | public function Scrollbar(fld:TextField,msk:Sprite,clr:String="000000"):void { |
|---|
| 43 | field = fld; |
|---|
| 44 | mask = msk; |
|---|
| 45 | color = clr; |
|---|
| 46 | scrollbar = new Sprite(); |
|---|
| 47 | field.parent.addChild(scrollbar); |
|---|
| 48 | scrollbar.mouseChildren = false; |
|---|
| 49 | scrollbar.buttonMode = true; |
|---|
| 50 | }; |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | /** |
|---|
| 54 | * Invoke a (re)draw of the scrollbar. |
|---|
| 55 | **/ |
|---|
| 56 | public function draw():void { |
|---|
| 57 | Draw.clear(scrollbar); |
|---|
| 58 | field.y = mask.y; |
|---|
| 59 | scrollbar.x = mask.x+mask.width; |
|---|
| 60 | scrollbar.y = mask.y; |
|---|
| 61 | proportion = mask.height/field.height; |
|---|
| 62 | if(proportion < 1) { |
|---|
| 63 | scrollbar.visible = true; |
|---|
| 64 | scrollbar.addEventListener(MouseEvent.MOUSE_DOWN,downHandler); |
|---|
| 65 | Draw.rect(scrollbar,color,10,mask.height,0,0,0); |
|---|
| 66 | Draw.rect(scrollbar,color,1,mask.height,4,0,0.5); |
|---|
| 67 | icon = Draw.rect(scrollbar,color,3,mask.height*proportion,3,0,1); |
|---|
| 68 | } else { |
|---|
| 69 | scrollbar.visible = false; |
|---|
| 70 | scrollbar.removeEventListener(MouseEvent.MOUSE_DOWN,downHandler); |
|---|
| 71 | } |
|---|
| 72 | }; |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | /** The mouse is pressed over the scrollbar. **/ |
|---|
| 76 | private function downHandler(evt:MouseEvent):void { |
|---|
| 77 | interval = setInterval(scroll,25); |
|---|
| 78 | mask.stage.addEventListener(MouseEvent.MOUSE_UP,upHandler); |
|---|
| 79 | }; |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | /** The mouse has been released after a scrollbarpress. **/ |
|---|
| 83 | private function upHandler(evt:MouseEvent):void { |
|---|
| 84 | clearInterval(interval); |
|---|
| 85 | mask.stage.removeEventListener(MouseEvent.MOUSE_UP,upHandler); |
|---|
| 86 | }; |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | /** Calculate and scroll to the new y position. **/ |
|---|
| 90 | private function scroll():void { |
|---|
| 91 | var mps:Number = scrollbar.mouseY; |
|---|
| 92 | if(mps < icon.height/2) { |
|---|
| 93 | icon.y = 0; |
|---|
| 94 | field.y = mask.y; |
|---|
| 95 | } else if (mps > scrollbar.height - icon.height/2) { |
|---|
| 96 | icon.y = scrollbar.height-icon.height; |
|---|
| 97 | field.y = mask.y + mask.height - field.height; |
|---|
| 98 | } else { |
|---|
| 99 | icon.y = mps - icon.height/2; |
|---|
| 100 | field.y = mask.y + mask.height/2 - mps/proportion; |
|---|
| 101 | } |
|---|
| 102 | }; |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | }; |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | } |
|---|