PageRenderTime 22ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/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
  1. package sparrowGui.components
  2. {
  3. import flash.display.DisplayObject;
  4. import flash.display.DisplayObjectContainer;
  5. import flash.display.Sprite;
  6. import flash.events.Event;
  7. import flash.events.MouseEvent;
  8. import sparrowGui.utils.SparrowUtil;
  9. /**
  10. * ???????
  11. *
  12. * ????
  13. *
  14. var sp:ScrollPanel = new ScrollPanel();
  15. sp.x = 300;
  16. sp.source = cb;
  17. addChild(sp);
  18. *
  19. * @author Pelephone
  20. * @website http://cnblogs.com/pelephone
  21. */
  22. public class ScrollPanel extends BaseUIComponent
  23. {
  24. /**
  25. * ????????
  26. */
  27. public static const VER_SCROLL_NAME:String = "vScroll";
  28. /**
  29. * ????????
  30. */
  31. public static const HOR_SCROLL_NAME:String = "hScroll";
  32. private var contDP:Sprite;
  33. /**
  34. * ??????
  35. */
  36. private var _source:DisplayObject;
  37. /**
  38. * ?????????
  39. */
  40. private var maskDP:Sprite;
  41. private var _vScroll:VScrollBar;
  42. private var _hScroll:HScrollBar;
  43. private var _autoVhidden:Boolean = true;
  44. private var _autoHhidden:Boolean = true;
  45. private var _autoScroll:Boolean = true;
  46. private var _width:Number = 80;
  47. private var _height:Number = 80;
  48. /**
  49. * ??????
  50. * @param uiVars ????
  51. */
  52. public function ScrollPanel(uiVars:Object=null)
  53. {
  54. super(uiVars);
  55. contDP = new Sprite();
  56. maskDP = new Sprite();
  57. contDP.mask = maskDP;
  58. var skinDC:DisplayObjectContainer = skin as DisplayObjectContainer;
  59. if(skinDC)
  60. {
  61. skinDC.addChild(contDP);
  62. skinDC.addChild(maskDP);
  63. }
  64. addSkinListen();
  65. }
  66. override protected function setUI(uiVars:Object=null):void
  67. {
  68. super.setUI(uiVars);
  69. var skinDC:DisplayObjectContainer = skin as DisplayObjectContainer;
  70. if(skinDC)
  71. {
  72. var vScrollSkin:DisplayObject = skinDC.getChildByName(VER_SCROLL_NAME);
  73. _vScroll = new VScrollBar(vScrollSkin);
  74. var hScrollSkin:DisplayObject = skinDC.getChildByName(HOR_SCROLL_NAME);
  75. _hScroll = new HScrollBar(hScrollSkin);
  76. }
  77. }
  78. /**
  79. * ???????
  80. */
  81. override protected function addSkinListen():void
  82. {
  83. vScroll.addEventListener(Event.CHANGE,onScrollVEvent);
  84. skin.addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
  85. if(hScroll) hScroll.addEventListener(Event.CHANGE,onscrollHEvent);
  86. }
  87. /**
  88. * ???????
  89. */
  90. override protected function removeSkinListen():void
  91. {
  92. vScroll.removeEventListener(Event.CHANGE,onScrollVEvent);
  93. skin.removeEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
  94. if(hScroll) hScroll.removeEventListener(Event.CHANGE,onscrollHEvent);
  95. }
  96. /**
  97. * ?????????
  98. * @param e
  99. */
  100. private function onScrollVEvent(e:Event):void
  101. {
  102. var scrollNum:Number = contDP.height - maskDP.height;
  103. contDP.y = -1 * vScroll.scrollPercent * scrollNum;
  104. }
  105. /**
  106. * ?????????
  107. * @param e
  108. */
  109. private function onscrollHEvent(e:Event):void
  110. {
  111. var scrollNum:Number = contDP.width - maskDP.width;
  112. contDP.x = -1 * hScroll.scrollPercent * scrollNum;
  113. }
  114. /**
  115. * ????
  116. * @param event
  117. */
  118. private function onMouseWheel(event:MouseEvent):void
  119. {
  120. var scrollDist:Number = sourceHeight - maskDP.height;
  121. vScroll.scrollPercent -= event.delta/scrollDist*2;
  122. }
  123. /**
  124. * ?????????
  125. */
  126. public function activeScroll():void
  127. {
  128. var scrollWidth:Number = this.width;
  129. var scrollHeight:Number = this.height;
  130. // ????????????
  131. if(contDP.height<=this.height){
  132. if(autoVhidden)
  133. vScroll.visible = false;
  134. else
  135. {
  136. vScroll.visible = true;
  137. vScroll.enabled = false;
  138. scrollWidth = this.width - (autoScroll?vScroll.width:0);
  139. }
  140. }
  141. else
  142. {
  143. vScroll.visible = true;
  144. vScroll.setSliderParams(this.height,contDP.height);
  145. scrollWidth = this.width - (autoScroll?vScroll.width:0);
  146. }
  147. if(autoScroll)
  148. vScroll.x = this.width - vScroll.width;
  149. if(!hScroll){
  150. if(autoScroll) vScroll.height = height;
  151. drawMask(scrollWidth,scrollHeight);
  152. return;
  153. }
  154. // ????????????
  155. if(contDP.width<=scrollWidth){
  156. if(autoHhidden)
  157. hScroll.visible = false;
  158. else
  159. {
  160. hScroll.visible = true;
  161. hScroll.enabled = false;
  162. scrollHeight = this.height - (autoScroll?hScroll.height:0);
  163. }
  164. }
  165. else
  166. {
  167. hScroll.visible = true;
  168. hScroll.setSliderParams(scrollWidth,contDP.width);
  169. scrollHeight = this.height - (autoScroll?hScroll.height:0);
  170. }
  171. // ?????????????
  172. if(autoScroll)
  173. {
  174. hScroll.y = this.height - hScroll.height;
  175. vScroll.height = this.height - (hScroll.visible?hScroll.height:0);
  176. hScroll.width = this.width - (vScroll.visible?vScroll.width:0);
  177. }
  178. drawMask(scrollWidth,scrollHeight);
  179. }
  180. /**
  181. * ????
  182. * @param w
  183. * @param h
  184. */
  185. private function drawMask(w:Number,h:Number):void
  186. {
  187. maskDP.visible = true;
  188. maskDP.graphics.clear();
  189. maskDP.graphics.beginFill(0xFFFF00);
  190. maskDP.graphics.drawRect(0,0,w,h);
  191. maskDP.graphics.endFill();
  192. }
  193. ///////////////////////////////////////
  194. // get/set
  195. ///////////////////////////////////////
  196. /**
  197. * ????????
  198. * @param picDP
  199. */
  200. public function set source(picDP:DisplayObject):void
  201. {
  202. SparrowUtil.clearDisp(contDP);
  203. contDP.addChild(picDP);
  204. activeScroll();
  205. _source = picDP;
  206. // skin.addChildAt(picDP,skin.getChildIndex(contDP));
  207. // contDP.parent.removeChild(contDP);
  208. // _contDP = picDP;
  209. }
  210. public function get source():DisplayObject
  211. {
  212. return _source;
  213. }
  214. /**
  215. * ???????????
  216. */
  217. public function get autoVhidden():Boolean
  218. {
  219. return _autoVhidden;
  220. }
  221. /**
  222. * @private
  223. */
  224. public function set autoVhidden(value:Boolean):void
  225. {
  226. _autoVhidden = value;
  227. }
  228. /**
  229. * ???????????
  230. */
  231. public function get autoHhidden():Boolean
  232. {
  233. return _autoHhidden;
  234. }
  235. /**
  236. * @private
  237. */
  238. public function set autoHhidden(value:Boolean):void
  239. {
  240. _autoHhidden = value;
  241. }
  242. /**
  243. * ?????
  244. */
  245. public function get vScroll():VScrollBar
  246. {
  247. return _vScroll;
  248. }
  249. /**
  250. * ??????????????
  251. */
  252. public function setVScroll(scrollSkin:Sprite):void
  253. {
  254. _vScroll = new VScrollBar(scrollSkin);
  255. }
  256. /**
  257. * ?????
  258. */
  259. public function get hScroll():HScrollBar
  260. {
  261. return _hScroll;
  262. }
  263. /**
  264. * ??????????????
  265. */
  266. public function setHScroll(scrollSkin:Sprite):void
  267. {
  268. _hScroll = new HScrollBar(scrollSkin);
  269. }
  270. override public function get width():Number
  271. {
  272. return _width;
  273. }
  274. override public function set width(value:Number):void
  275. {
  276. _width = value;
  277. activeScroll();
  278. }
  279. override public function get height():Number
  280. {
  281. return _height;
  282. }
  283. override public function set height(value:Number):void
  284. {
  285. _height = value;
  286. activeScroll();
  287. }
  288. /**
  289. * ?????????
  290. * @return
  291. */
  292. public function get sourceWidth():Number
  293. {
  294. return contDP.width;
  295. }
  296. /**
  297. * ?????????
  298. * @return
  299. */
  300. public function get sourceHeight():Number
  301. {
  302. return contDP.height;
  303. }
  304. /**
  305. * ???????????
  306. */
  307. public function get autoScroll():Boolean
  308. {
  309. return _autoScroll;
  310. }
  311. /**
  312. * @private
  313. */
  314. public function set autoScroll(value:Boolean):void
  315. {
  316. _autoScroll = value;
  317. }
  318. /**
  319. * ???????????????
  320. public function get contDP():Sprite
  321. {
  322. return _contDP;
  323. }*/
  324. override public function getDefaultUIName():String
  325. {
  326. return "scrollPanel"
  327. }
  328. }
  329. }