package com.oxylusflash.book { import caurina.transitions.Tweener; import com.oxylusflash.events.ParamEvent; import flash.display.MovieClip; import flash.display.Sprite; import flash.display.StageDisplayState; import flash.events.Event; import flash.events.MouseEvent; import flash.geom.Rectangle; /** * Scroll pane (can be found on summarry list) */ public class ScrollPane extends Sprite { private var hitMc:Sprite; private var topLine:ScrollPaneLine; private var btmLine:ScrollPaneLine; private var scrollBar:ScrollBar = new ScrollBar; private var listing:ScrollPaneListing; private var listingY:Number = 0; private var lstHolder:Sprite; private static const LINE_OFFSET:Number = 0; public function ScrollPane() { hitMc = this.addChild(new Sprite) as Sprite; hitMc.graphics.beginFill(0, 0); hitMc.graphics.drawRect(0, 0, 10, 10); hitMc.graphics.endFill(); hitMc.cacheAsBitmap = true; this.hitArea = hitMc; hitMc.visible = false; topLine = this.addChild(new LibScrollPaneLineTop) as ScrollPaneLine; btmLine = this.addChild(new LibScrollPaneLineBtm) as ScrollPaneLine; btmLine.x = topLine.x = LINE_OFFSET; lstHolder = this.addChild(new Sprite) as Sprite; lstHolder.scrollRect = new Rectangle(0, 0, 100, 100); lstHolder.y = topLine.height; this.addChild(scrollBar); if (stage) addEventHandlers(); else this.addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler, false, 0, true); } private function addedToStageHandler(e:Event):void { removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler); addEventHandlers(); } private function addEventHandlers():void { this.addEventListener(MouseEvent.MOUSE_WHEEL, mouseWheelHandler, false, 0, true); scrollBar.addEventListener(ScrollBar.SCROLL, scrollBar_scrollHandler, false, 0, true); } private function mouseWheelHandler(e:MouseEvent):void { if (e.delta > 0) scrollBar.doScrollUp(); else scrollBar.doScrollDown(); } private function scrollBar_scrollHandler(e:ParamEvent):void { if (listing) { scrollListingTo((lstHolder.scrollRect.height - listing.height) * e.params.percentage); } } private function scrollListingTo(yPos:Number, instant:Boolean = false, forced:Boolean = false):void { if (listing) { yPos = Math.min(0, Math.max(lstHolder.scrollRect.height - listing.height, Math.round(yPos))); if (listingY != yPos || forced) { listingY = yPos; Tweener.addTween(listing, { y: listingY, base: Global.baseTween, time: instant ? 0 : 0.2 } ); } } } override public function get width():Number { return lstHolder.scrollRect.width; } override public function set width(value:Number):void { lstHolder.scrollRect = new Rectangle(0, 0, value, this.height); scrollBar.x = value + SummaryPanel.PADDING - 1; topLine.width = value - 2 * LINE_OFFSET; btmLine.width = topLine.width; hitMc.width = scrollBar.x + scrollBar.width; } override public function get height():Number { return lstHolder.scrollRect.height; } override public function set height(value:Number):void { lstHolder.scrollRect = new Rectangle(0, 0, this.width, value - topLine.height - btmLine.height); scrollListingTo(listingY, true, true); scrollBar.height = value; updateScroll(); btmLine.y = value; hitMc.height = value; } public function populate(xmlData:XML):void { var ani:Boolean = listing ? true : false; clearListing(); scrollBar.percentage = 0; listing = lstHolder.addChild(new ScrollPaneListing(xmlData, lstHolder.scrollRect.width, this, null, ani)) as ScrollPaneListing; updateScroll(); } public function clearListing():void { if (listing) { lstHolder.removeChild(listing); listing = null; updateScroll(); } } public function updateScroll():void { scrollBar.blockScrollEvent = true; if (listing && listing.height > lstHolder.scrollRect.height) { scrollBar.proportion = lstHolder.scrollRect.height / listing.height; scrollBar.percentage = listingY / (lstHolder.scrollRect.height - listing.height); } else { scrollBar.proportion = 1; scrollBar.percentage = 0; } scrollBar.blockScrollEvent = false; scrollListingTo(listingY, true, true); } override public function get mouseEnabled():Boolean { return super.mouseEnabled; } override public function set mouseEnabled(value:Boolean):void { if (super.mouseEnabled != value) { super.mouseEnabled = value; Tweener.addTween(lstHolder, { alpha: value ? 1 : 0.3, base: Global.baseTween } ); } } } }