PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/v01/src/com/CGFinal/Controls/ScrollPanel.as

http://my-as3-lib.googlecode.com/
ActionScript | 306 lines | 234 code | 13 blank | 59 comment | 30 complexity | 2ab12817d87b840fb4f075158a8bb9f0 MD5 | raw file
  1. package com.CGFinal.Controls {
  2. import com.CGFinal.LiteComponent;
  3. import flash.display.DisplayObject;
  4. import flash.display.Graphics;
  5. import flash.display.Shape;
  6. import flash.display.Sprite;
  7. import flash.events.Event;
  8. import flash.events.TimerEvent;
  9. import flash.geom.Rectangle;
  10. import flash.utils.Timer;
  11. /**
  12. * ...
  13. * @author KoaQiu
  14. */
  15. public class ScrollPanel extends LiteComponent {
  16. public static const AutoShowScrollBar:String = "auto";
  17. public static const DisableScrollBar:String = "none";
  18. /**
  19. * ??
  20. */
  21. public static const VerticalScrollBar:String = "vertical";
  22. /**
  23. * ??
  24. */
  25. public static const HorizontalScrollBar:String = "horizontal";
  26. //??
  27. private var _vScrollBar:ScrollBar;
  28. //??
  29. private var _hScrollBar:ScrollBar;
  30. private var _frame:Sprite;
  31. private var _bg:Sprite;
  32. protected var _con:Sprite = new Sprite();
  33. private var _conRc:Sprite;
  34. private var timer:Timer;
  35. private var _created:Boolean = false;
  36. private var _BackgroundColor:uint = 0xffffff;
  37. /**
  38. * ????
  39. * @author KoaQiu
  40. */
  41. public function get BackgroundColor():uint{
  42. return _BackgroundColor;
  43. }
  44. public function set BackgroundColor(v:uint):void {
  45. _BackgroundColor = v;
  46. this.draw();
  47. }
  48. private var _BackgroundAlpha:uint=60;
  49. /**
  50. * ?????
  51. * @author KoaQiu
  52. */
  53. public function get BackgroundAlpha():uint{
  54. return _BackgroundAlpha;
  55. }
  56. public function set BackgroundAlpha(v:uint):void {
  57. _BackgroundAlpha = v > 100?100:v;
  58. this.draw();
  59. }
  60. private var _FrameColor:uint = 0x999999;
  61. /**
  62. * ????
  63. * @author KoaQiu
  64. */
  65. public function get FrameColor():uint{
  66. return _FrameColor;
  67. }
  68. public function set FrameColor(v:uint):void {
  69. _FrameColor = v;
  70. }
  71. private var _ScrollBars:String = "auto";
  72. /**
  73. * ???????
  74. * @author KoaQiu
  75. */
  76. public function get ScrollBars():String{
  77. return _ScrollBars;
  78. }
  79. public function set ScrollBars(v:String):void {
  80. _ScrollBars = v;
  81. if (_created) {
  82. //this._showScrollBars(v);
  83. this.reLayout();
  84. }
  85. }
  86. private function _showScrollBars(v:String):void {
  87. switch(v) {
  88. case ScrollPanel.AutoShowScrollBar:
  89. this._vScrollBar.visible =
  90. this._hScrollBar.visible = true;
  91. break;
  92. case ScrollPanel.DisableScrollBar:
  93. this._vScrollBar.visible =
  94. this._hScrollBar.visible = false;
  95. break;
  96. case ScrollPanel.HorizontalScrollBar:
  97. this._vScrollBar.visible = false;
  98. this._hScrollBar.visible = true;
  99. break;
  100. case ScrollPanel.VerticalScrollBar:
  101. this._vScrollBar.visible = true;
  102. this._hScrollBar.visible = false;
  103. break;
  104. }
  105. }
  106. private var _FrameVisible:Boolean=true;
  107. /**
  108. * ??????
  109. * @author KoaQiu
  110. */
  111. public function get FrameVisible():Boolean{
  112. return _FrameVisible;
  113. }
  114. public function set FrameVisible(v:Boolean):void {
  115. _FrameVisible = v;
  116. if (_frame != null) {
  117. _frame.visible = v;
  118. }
  119. }
  120. override protected function createChildren():void {
  121. this._bg = new Sprite();
  122. this._frame = new Sprite();
  123. this._conRc = new Sprite();
  124. this._vScrollBar = new ScrollBar();
  125. this._vScrollBar.visible = false;
  126. this._vScrollBar.addEventListener(Event.CHANGE, DPE_VScrollBarChanged);
  127. this._hScrollBar = new ScrollBar();
  128. this._hScrollBar.rotation = 270;
  129. this._hScrollBar.visible = false;
  130. this._hScrollBar.addEventListener(Event.CHANGE, DPE_HScrollBarChanged);
  131. //this._hScrollBar.direction = "horizontal";
  132. this.timer = new Timer(200);
  133. this.timer.addEventListener(TimerEvent.TIMER, DPE_Timer);
  134. //this.timer.start();
  135. super.addChild(_bg);
  136. super.addChild(_frame);
  137. super.addChild(_conRc);
  138. _conRc.addChild(_con);
  139. super.addChild(_vScrollBar);
  140. super.addChild(_hScrollBar);
  141. _created = true;
  142. super.createChildren();
  143. }
  144. override public function SetSize(pWidth:Number, pHeight:Number):void {
  145. super.SetSize(pWidth, pHeight);
  146. if (_created) {
  147. var rc:Rectangle = this._conRc.scrollRect;
  148. if (rc == null) {
  149. rc = new Rectangle(0, 0, pWidth, pHeight);
  150. }else {
  151. rc.width = pWidth;
  152. rc.height = pHeight;
  153. }
  154. this._conRc.scrollRect = rc;
  155. }
  156. }
  157. public function Clear():void {
  158. while (this._con.numChildren > 0) {
  159. this._con.removeChildAt(0);
  160. }
  161. }
  162. /**
  163. * ?child?????
  164. * @param child
  165. */
  166. public function BringToFornt(child:DisplayObject):void {
  167. this._con.swapChildrenAt(this._con.getChildIndex(child), this._con.numChildren - 1);
  168. }
  169. /**
  170. * ?????
  171. */
  172. public function get ChildCount():int {
  173. return _con.numChildren;
  174. }
  175. /**
  176. * ????
  177. * @param item
  178. */
  179. public function RemoveItem(item:DisplayObject):DisplayObject {
  180. return this._con.removeChild(item);
  181. }
  182. /**
  183. * ????
  184. * @param index
  185. * @return
  186. */
  187. public function RemoveItemAt(index:int):DisplayObject {
  188. var tmp:DisplayObject = this._con.removeChildAt(index);
  189. this.reLayout();
  190. return tmp;
  191. }
  192. /**
  193. * ????
  194. * @param child
  195. * @return
  196. */
  197. public function AddItem(child:DisplayObject):DisplayObject {
  198. var tmp:DisplayObject = _con.addChild(child);
  199. this.reLayout();
  200. return tmp;
  201. }
  202. public function AddItemAt(child:DisplayObject, index:int):DisplayObject {
  203. var tmp:DisplayObject = _con.addChildAt(child, index);
  204. this.reLayout();
  205. return tmp;
  206. }
  207. public function GetItemAt(index:int):DisplayObject {
  208. if (index >= 0 && index < _con.numChildren) {
  209. return _con.getChildAt(index) as DisplayObject;
  210. }
  211. return null;
  212. }
  213. override protected function draw():void {
  214. super.draw();
  215. if (this._created == false) {
  216. return;
  217. }
  218. var g:Graphics;
  219. g = this._bg.graphics;
  220. g.clear();
  221. g.beginFill(this.BackgroundColor, this.BackgroundAlpha / 100);
  222. g.drawRect(0, 0, width, height);
  223. g.endFill();
  224. if (this.FrameVisible) {
  225. g = this._frame.graphics;
  226. g.lineStyle(1, this.FrameColor);
  227. g.moveTo(0, height);
  228. g.lineTo(0, 0);
  229. g.lineTo(width, 0);
  230. //g.lineStyle(1, 0xdddddd);
  231. g.lineTo(width, height);
  232. g.lineTo(0, height);
  233. }
  234. this.OnConRender();
  235. }
  236. protected function reLayout():void {
  237. this.OnConRender();
  238. }
  239. //Do Event
  240. private function OnConRender():void {
  241. _showScrollBars(this.ScrollBars);
  242. if (this.ScrollBars == ScrollPanel.DisableScrollBar) {
  243. return;
  244. }
  245. var rc:Rectangle = this._conRc.scrollRect;
  246. if (rc == null) {
  247. rc = new Rectangle(0, 0, this.width, this.height);
  248. this._conRc.scrollRect = rc;
  249. }
  250. trace(rc)
  251. this._vScrollBar.x = width - this._vScrollBar.width;
  252. this._hScrollBar.y = height;// - this._hScrollBar.height;
  253. if (this._con.width > rc.width &&
  254. (this.ScrollBars == ScrollPanel.AutoShowScrollBar || this.ScrollBars == ScrollPanel.HorizontalScrollBar)
  255. ) {
  256. this._hScrollBar.visible = true;
  257. this._vScrollBar.height = height - this._hScrollBar.width;
  258. }else {
  259. this._hScrollBar.visible = false;
  260. this._vScrollBar.height = height;
  261. }
  262. if (this._con.height > rc.height &&
  263. (this.ScrollBars == ScrollPanel.AutoShowScrollBar || this.ScrollBars == ScrollPanel.VerticalScrollBar)
  264. ){
  265. this._vScrollBar.visible = true;
  266. this._hScrollBar.height = width - this._vScrollBar.width;
  267. }else {
  268. this._vScrollBar.visible = false;
  269. this._hScrollBar.height = width;
  270. }
  271. }
  272. //Events
  273. private function DPE_Timer(e:TimerEvent):void {
  274. this.OnConRender();
  275. }
  276. private function DPE_VScrollBarChanged(e:Event):void {
  277. var maxH:Number = this._con.height - this._conRc.scrollRect.height;
  278. if (this.ScrollBars == ScrollPanel.AutoShowScrollBar) {
  279. maxH += this._vScrollBar.width;
  280. }
  281. var h:Number = maxH * this._vScrollBar.Value / 100;
  282. this._con.y = -h;
  283. }
  284. private function DPE_HScrollBarChanged(e:Event):void {
  285. var maxW:Number = this._con.width - this._conRc.scrollRect.width;
  286. if (this.ScrollBars == ScrollPanel.AutoShowScrollBar) {
  287. maxW += this._vScrollBar.width;
  288. }
  289. var w:Number = maxW * this._hScrollBar.Value / 100;
  290. this._con.x = -w;
  291. }
  292. }
  293. }