/SparrowGUIv1/src/sparrowGui/components/ScrollPanel.as
http://sparrowgui.googlecode.com/ · ActionScript · 380 lines · 217 code · 49 blank · 114 comment · 12 complexity · 0d6a7d2c8912fce3eb9ad01fd9c227dc MD5 · raw file
- package sparrowGui.components
- {
- import flash.display.DisplayObject;
- import flash.display.DisplayObjectContainer;
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.events.MouseEvent;
-
- import sparrowGui.utils.SparrowUtil;
-
- /**
- * ???????
- *
- * ????
- *
-
- var sp:ScrollPanel = new ScrollPanel();
- sp.x = 300;
- sp.source = cb;
- addChild(sp);
- *
- * @author Pelephone
- * @website http://cnblogs.com/pelephone
- */
- public class ScrollPanel extends BaseUIComponent
- {
- /**
- * ????????
- */
- public static const VER_SCROLL_NAME:String = "vScroll";
- /**
- * ????????
- */
- public static const HOR_SCROLL_NAME:String = "hScroll";
-
- private var contDP:Sprite;
- /**
- * ??????
- */
- private var _source:DisplayObject;
-
- /**
- * ?????????
- */
- private var maskDP:Sprite;
-
- private var _vScroll:VScrollBar;
- private var _hScroll:HScrollBar;
-
- private var _autoVhidden:Boolean = true;
- private var _autoHhidden:Boolean = true;
-
- private var _autoScroll:Boolean = true;
-
- private var _width:Number = 80;
- private var _height:Number = 80;
-
- /**
- * ??????
- * @param uiVars ????
- */
- public function ScrollPanel(uiVars:Object=null)
- {
- super(uiVars);
-
- contDP = new Sprite();
- maskDP = new Sprite();
- contDP.mask = maskDP;
-
- var skinDC:DisplayObjectContainer = skin as DisplayObjectContainer;
- if(skinDC)
- {
- skinDC.addChild(contDP);
- skinDC.addChild(maskDP);
- }
- addSkinListen();
- }
-
- override protected function setUI(uiVars:Object=null):void
- {
- super.setUI(uiVars);
-
- var skinDC:DisplayObjectContainer = skin as DisplayObjectContainer;
- if(skinDC)
- {
- var vScrollSkin:DisplayObject = skinDC.getChildByName(VER_SCROLL_NAME);
- _vScroll = new VScrollBar(vScrollSkin);
-
- var hScrollSkin:DisplayObject = skinDC.getChildByName(HOR_SCROLL_NAME);
- _hScroll = new HScrollBar(hScrollSkin);
- }
- }
-
- /**
- * ???????
- */
- override protected function addSkinListen():void
- {
- vScroll.addEventListener(Event.CHANGE,onScrollVEvent);
- skin.addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
- if(hScroll) hScroll.addEventListener(Event.CHANGE,onscrollHEvent);
- }
-
- /**
- * ???????
- */
- override protected function removeSkinListen():void
- {
- vScroll.removeEventListener(Event.CHANGE,onScrollVEvent);
- skin.removeEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
- if(hScroll) hScroll.removeEventListener(Event.CHANGE,onscrollHEvent);
- }
-
- /**
- * ?????????
- * @param e
- */
- private function onScrollVEvent(e:Event):void
- {
- var scrollNum:Number = contDP.height - maskDP.height;
- contDP.y = -1 * vScroll.scrollPercent * scrollNum;
- }
-
- /**
- * ?????????
- * @param e
- */
- private function onscrollHEvent(e:Event):void
- {
- var scrollNum:Number = contDP.width - maskDP.width;
- contDP.x = -1 * hScroll.scrollPercent * scrollNum;
- }
-
- /**
- * ????
- * @param event
- */
- private function onMouseWheel(event:MouseEvent):void
- {
- var scrollDist:Number = sourceHeight - maskDP.height;
- vScroll.scrollPercent -= event.delta/scrollDist*2;
- }
-
- /**
- * ?????????
- */
- public function activeScroll():void
- {
- var scrollWidth:Number = this.width;
- var scrollHeight:Number = this.height;
- // ????????????
- if(contDP.height<=this.height){
- if(autoVhidden)
- vScroll.visible = false;
- else
- {
- vScroll.visible = true;
- vScroll.enabled = false;
-
- scrollWidth = this.width - (autoScroll?vScroll.width:0);
- }
- }
- else
- {
- vScroll.visible = true;
- vScroll.setSliderParams(this.height,contDP.height);
-
- scrollWidth = this.width - (autoScroll?vScroll.width:0);
- }
- if(autoScroll)
- vScroll.x = this.width - vScroll.width;
-
- if(!hScroll){
- if(autoScroll) vScroll.height = height;
- drawMask(scrollWidth,scrollHeight);
- return;
- }
- // ????????????
- if(contDP.width<=scrollWidth){
- if(autoHhidden)
- hScroll.visible = false;
- else
- {
- hScroll.visible = true;
- hScroll.enabled = false;
- scrollHeight = this.height - (autoScroll?hScroll.height:0);
- }
- }
- else
- {
- hScroll.visible = true;
- hScroll.setSliderParams(scrollWidth,contDP.width);
- scrollHeight = this.height - (autoScroll?hScroll.height:0);
- }
- // ?????????????
- if(autoScroll)
- {
- hScroll.y = this.height - hScroll.height;
-
- vScroll.height = this.height - (hScroll.visible?hScroll.height:0);
- hScroll.width = this.width - (vScroll.visible?vScroll.width:0);
- }
-
-
- drawMask(scrollWidth,scrollHeight);
- }
-
- /**
- * ????
- * @param w
- * @param h
- */
- private function drawMask(w:Number,h:Number):void
- {
- maskDP.visible = true;
- maskDP.graphics.clear();
- maskDP.graphics.beginFill(0xFFFF00);
- maskDP.graphics.drawRect(0,0,w,h);
- maskDP.graphics.endFill();
- }
-
- ///////////////////////////////////////
- // get/set
- ///////////////////////////////////////
-
-
- /**
- * ????????
- * @param picDP
- */
- public function set source(picDP:DisplayObject):void
- {
- SparrowUtil.clearDisp(contDP);
- contDP.addChild(picDP);
- activeScroll();
- _source = picDP;
- // skin.addChildAt(picDP,skin.getChildIndex(contDP));
- // contDP.parent.removeChild(contDP);
- // _contDP = picDP;
- }
-
- public function get source():DisplayObject
- {
- return _source;
- }
-
- /**
- * ???????????
- */
- public function get autoVhidden():Boolean
- {
- return _autoVhidden;
- }
-
- /**
- * @private
- */
- public function set autoVhidden(value:Boolean):void
- {
- _autoVhidden = value;
- }
-
- /**
- * ???????????
- */
- public function get autoHhidden():Boolean
- {
- return _autoHhidden;
- }
-
- /**
- * @private
- */
- public function set autoHhidden(value:Boolean):void
- {
- _autoHhidden = value;
- }
-
- /**
- * ?????
- */
- public function get vScroll():VScrollBar
- {
- return _vScroll;
- }
-
- /**
- * ??????????????
- */
- public function setVScroll(scrollSkin:Sprite):void
- {
- _vScroll = new VScrollBar(scrollSkin);
- }
-
- /**
- * ?????
- */
- public function get hScroll():HScrollBar
- {
- return _hScroll;
- }
-
- /**
- * ??????????????
- */
- public function setHScroll(scrollSkin:Sprite):void
- {
- _hScroll = new HScrollBar(scrollSkin);
- }
-
- override public function get width():Number
- {
- return _width;
- }
-
- override public function set width(value:Number):void
- {
- _width = value;
- activeScroll();
- }
-
- override public function get height():Number
- {
- return _height;
- }
-
- override public function set height(value:Number):void
- {
- _height = value;
- activeScroll();
- }
-
- /**
- * ?????????
- * @return
- */
- public function get sourceWidth():Number
- {
- return contDP.width;
- }
-
- /**
- * ?????????
- * @return
- */
- public function get sourceHeight():Number
- {
- return contDP.height;
- }
-
- /**
- * ???????????
- */
- public function get autoScroll():Boolean
- {
- return _autoScroll;
- }
-
- /**
- * @private
- */
- public function set autoScroll(value:Boolean):void
- {
- _autoScroll = value;
- }
-
- /**
- * ???????????????
-
- public function get contDP():Sprite
- {
- return _contDP;
- }*/
-
- override public function getDefaultUIName():String
- {
- return "scrollPanel"
- }
- }
- }