Guest User

Untitled

a guest
Dec 10th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.60 KB | None | 0 0
  1. package com.hit.display.ui
  2. {
  3. import flash.display.BitmapData;
  4. import flash.display.DisplayObject;
  5. import flash.display.DisplayObjectContainer;
  6. import flash.display.Loader;
  7. import flash.display.LoaderInfo;
  8. import flash.display.MovieClip;
  9. import flash.display.SimpleButton;
  10. import flash.display.SpreadMethod;
  11. import flash.display.Sprite;
  12. import flash.events.Event;
  13. import flash.events.IOErrorEvent;
  14. import flash.events.MouseEvent;
  15. import flash.net.URLRequest;
  16. import flash.system.ApplicationDomain;
  17. import flash.text.Font;
  18. import flash.text.TextField;
  19. import flash.text.TextFormat;
  20. import flash.utils.getQualifiedClassName;
  21. import flash.utils.Dictionary;
  22.  
  23. import com.greensock.TweenNano;
  24. import com.greensock.easing.Back;
  25. import com.greensock.easing.Bounce;
  26. import com.greensock.easing.Linear;
  27.  
  28. import com.hit.events.DialogEvent;
  29.  
  30. /**
  31. * Dialog
  32. *
  33. * TODO?: Allow the user to manually position the window
  34. *
  35. * @author Guyllaume Cardinal
  36. */
  37. public class Dialog extends Sprite
  38. {
  39. // Name constant, used so no constant shares the same name as an other class' constants
  40. private static const NAME:String = "Dialog";
  41.  
  42. // Components URL. Dictates to the class where to fetch the SWF used to create the window
  43. // Will be used by default.
  44. private static const ASSETS_URL:String = "components/DialogAssets/DialogAssets.swf";
  45.  
  46. // Position constants
  47. public static const TOP:String = NAME + "Top";
  48. public static const BOTTOM:String = NAME + "Bottom";
  49. public static const LEFT:String = NAME + "Left";
  50. public static const RIGHT:String = NAME + "Right";
  51. public static const CENTER:String = NAME + "Center";
  52.  
  53. // Function Strings
  54. public static const SHOW_FUNCTION:String = "show";
  55. public static const UPDATE_FUNCTION:String = "update";
  56. public static const HIDE_FUNCTION:String = "hide";
  57.  
  58.  
  59. private var _dialog:Dialog;
  60. // Window graphics
  61. private var _headerGraphic:MovieClip;
  62. private var _windowGraphic:MovieClip;
  63. private var _closeButton:SimpleButton;
  64. private var _overlayGraphic:Sprite;
  65. private var _buttonContainer:Sprite;
  66. // Button classes
  67. private var ButtonUpGraphic:Class;
  68. private var ButtonOverGraphic:Class;
  69. private var ButtonDownGraphic:Class;
  70.  
  71. // Component parameters
  72. private var _parentContainer:DisplayObjectContainer;
  73. private var _window:Sprite;
  74. private var _text:String;
  75. private var _title:String;
  76. // Component texts
  77. private var _headerFormat:TextFormat;
  78. private var _windowFormat:TextFormat;
  79. private var _headerText:TextField;
  80. private var _windowText:TextField;
  81. private var _textFont:Font;
  82. private var _selectable:Boolean;
  83. // Optional parameters
  84. private var _draggable:Boolean;
  85. private var _autoShow:Boolean;
  86. private var _position:String;
  87. private var _titleIcon:DisplayObject;
  88. private var _textIcon:DisplayObject;
  89. private var _defaultTween:Function;
  90. private var _buttons:Dictionary;
  91. private var _padding:int;
  92.  
  93. // Control variables
  94. private var _ready:Boolean;
  95. private var _loading:Boolean;
  96. private var _shown:Boolean;
  97.  
  98.  
  99. /**
  100. *
  101. * @param parent DisplayObjectContainer The DisplayObjectContainer in which to add
  102. * or remove the Dialog
  103. * @param title String The title of the Dialog
  104. * @param text String
  105. * @param options Object
  106. */
  107. public function Dialog(parent:DisplayObjectContainer, title:String, text:String,
  108. options:Object = null ):void
  109. {
  110. _dialog = this as Dialog;
  111.  
  112. // Store the required parameters
  113. _parentContainer = parent;
  114. _text = text;
  115. _title = title;
  116. // Register the optional parameters, setting up the default values if none was passed
  117. registerOptionalParameters(options);
  118.  
  119. // Load the swf with the components to draw the actual window
  120. var assetsLoader:Loader = new Loader();
  121. assetsLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
  122. assetsLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, assetsLoadedhandler);
  123. assetsLoader.load(new URLRequest(ASSETS_URL));
  124. _loading = true;
  125. }
  126.  
  127.  
  128. /**
  129. *
  130. * @param tweenType
  131. */
  132. public function show():void
  133. {
  134. _parentContainer.addChild(_dialog);
  135.  
  136. // Configue and tweeen the overlay and window
  137. TweenNano.from(_overlayGraphic, 0.5, { alpha:0, ease:_defaultTween } );
  138. TweenNano.from(_window, 0.6, { width:0, height:0, ease:_defaultTween } );
  139.  
  140. // Allow the window to be dragged if the option is selected
  141. if (_draggable)
  142. {
  143. _headerGraphic.buttonMode = true;
  144. _headerGraphic.addEventListener(MouseEvent.MOUSE_DOWN, headerMouseDownHandler, false, 0, true);
  145. this.stage.addEventListener(MouseEvent.MOUSE_UP, headerMouseUpHandler, false, 0, true);
  146. }
  147.  
  148. this.dispatchEvent(new DialogEvent(DialogEvent.SHOWN));
  149. _shown = true;
  150. }
  151.  
  152.  
  153. /**
  154. *
  155. * @param event
  156. */
  157. public function hide(event:MouseEvent = null):void
  158. {
  159. if (_draggable)
  160. {
  161. _headerGraphic.removeEventListener(MouseEvent.MOUSE_DOWN, headerMouseDownHandler);
  162. this.stage.removeEventListener(MouseEvent.MOUSE_UP, headerMouseUpHandler);
  163. }
  164.  
  165. TweenNano.to(_overlayGraphic, 0.3, { alpha:0, ease:_defaultTween } );
  166. TweenNano.to(_window, 0.6, { width:0, height:0, alpha: 0, ease:_defaultTween,
  167. onComplete: function() {
  168. _parentContainer.removeChild(_dialog);
  169. } } );
  170.  
  171. this.dispatchEvent(new DialogEvent(DialogEvent.HIDDEN));
  172. _shown = false;
  173. }
  174.  
  175.  
  176. /**
  177. *
  178. */
  179. public function update():void
  180. {
  181. draw();
  182. this.dispatchEvent(new DialogEvent(DialogEvent.UPDATED));
  183. }
  184.  
  185.  
  186. /**
  187. *
  188. * @param event
  189. */
  190. private function assetsLoadedhandler(event:Event):void
  191. {
  192. dispatchEvent(new DialogEvent(DialogEvent.LOADED));
  193. _loading = false;
  194.  
  195. configUI(event.target as LoaderInfo);
  196. draw();
  197.  
  198. dispatchEvent(new DialogEvent(DialogEvent.READY));
  199. _ready = true;
  200.  
  201. if (_autoShow) show();
  202. }
  203.  
  204.  
  205. /**
  206. *
  207. * @param loaderContent
  208. */
  209. private function configUI(loaderContent:LoaderInfo = null):void
  210. {
  211. if (loaderContent)
  212. registerComponents(loaderContent);
  213.  
  214. _window = new Sprite();
  215.  
  216. // Setup the header
  217. _window.addChild(_headerGraphic);
  218.  
  219. // Setup the main window
  220. _window.addChild(_windowGraphic);
  221. _window.addChild(_closeButton);
  222.  
  223. // Adds the icon in the header if one is supplied
  224. if (_titleIcon)
  225. _headerGraphic.addChild(_titleIcon);
  226.  
  227. if (_textIcon)
  228. _windowGraphic.addChild(_textIcon);
  229.  
  230. if (_buttons)
  231. {
  232. _buttonContainer = new Sprite();
  233. _windowGraphic.addChild(_buttonContainer);
  234. }
  235.  
  236. // Textfields
  237. _headerText = new TextField();
  238. _windowText = new TextField();
  239. _headerGraphic.addChild(_headerText);
  240. _windowGraphic.addChild(_windowText);
  241.  
  242. // background overlay
  243. _overlayGraphic = new Sprite();
  244. _overlayGraphic.graphics.beginBitmapFill(new BitmapData(_parentContainer.width,
  245. _parentContainer.height, false,
  246. 0x000000), null, false);
  247. _overlayGraphic.graphics.drawRect(0, 0, _parentContainer.width, _parentContainer.height);
  248. _overlayGraphic.graphics.endFill();
  249. _overlayGraphic.alpha = 0.5;
  250. this.addChildAt(_overlayGraphic, 0);
  251.  
  252. this.addChild(_window);
  253. }
  254.  
  255.  
  256. /**
  257. *
  258. */
  259. private function draw():void
  260. {
  261. // Print the texts
  262. drawTexts();
  263.  
  264. // Header
  265. _headerText.y = _headerGraphic.height / 2 - _headerText.height / 2;
  266. if (_titleIcon)
  267. {
  268. _titleIcon.x = _padding;
  269. _titleIcon.y = _headerText.y + _headerText.height / 2 - _titleIcon.height / 2;
  270. _headerText.x = _titleIcon.x + _titleIcon.width + _padding;
  271. }
  272. else
  273. {
  274. _headerText.x = _padding;
  275. }
  276.  
  277. // Main window
  278. _windowText.y = _padding;
  279. if (_textIcon)
  280. {
  281. _textIcon.x = _padding;
  282. _textIcon.y = _padding;
  283. _windowText.x = _textIcon.x + _textIcon.width + _padding;
  284. // _padding * 3 because we have padding on the left, right and between the text and icon
  285. _windowText.width = _window.width - _textIcon.width - _padding * 3;
  286. }
  287. else
  288. {
  289. _windowText.x = _padding;
  290. _windowText.width = _windowGraphic.width - _padding * 2;
  291. }
  292.  
  293. // Position the elements
  294. _windowGraphic.y = _headerGraphic.y + _headerGraphic.height;
  295. _closeButton.y = _headerGraphic.height / 2 - _closeButton.height / 2;
  296. _closeButton.x = _headerGraphic.width - _closeButton.width - _padding;
  297.  
  298. // Buttons
  299. if (_buttons)
  300. {
  301. var buttonIterator:int = 0;
  302. for (var key:Object in _buttons)
  303. {
  304. var button:Sprite = createButton(_buttons[key]);
  305. button.mouseChildren = false;
  306.  
  307. var buttonFunction:Function;
  308. if (_buttons[key].callback is Function)
  309. buttonFunction = _buttons[key].callback as Function;
  310. else
  311. buttonFunction = this[_buttons[key].callback] as Function;
  312.  
  313. button.addEventListener(MouseEvent.CLICK, buttonFunction);
  314. button.x = (button.width + _padding) * buttonIterator;
  315. _buttonContainer.addChild(button)
  316. buttonIterator++;
  317. }
  318. _buttonContainer.x = _windowGraphic.width - _buttonContainer.width - _padding;
  319. _buttonContainer.y = _windowText.y + _windowText.height + _padding;
  320. }
  321.  
  322. _windowGraphic.getChildAt(0).height = _windowGraphic.height + _padding * 2;
  323.  
  324. // Center the registration point
  325. var halfHeight:int = _window.height / 2;
  326. var halfWidth:int = _window.width / 2;
  327. var i:int = 0;
  328. var n:int = _window.numChildren;
  329. for ( ; i < n; i++ )
  330. {
  331. var obj:DisplayObject = _window.getChildAt(i);
  332. obj.x -= halfWidth;
  333. obj.y -= halfHeight;
  334. }
  335.  
  336. // Set the initial position of the Dialog window
  337. switch (_position)
  338. {
  339. case Dialog.CENTER:
  340. _window.x = _parentContainer.width / 2;
  341. _window.y = _parentContainer.height / 2;
  342. break;
  343.  
  344. case Dialog.LEFT:
  345. _window.x = _window.width / 2;
  346. _window.y = _parentContainer.height / 2;
  347. break;
  348.  
  349. case Dialog.RIGHT:
  350. _window.x = _parentContainer.width - _window.width / 2;
  351. _window.y = _parentContainer.height / 2;
  352. break;
  353.  
  354. case Dialog.TOP:
  355. _window.x = _parentContainer.width / 2;
  356. _window.y = _window.height / 2;
  357. break;
  358.  
  359. case Dialog.BOTTOM:
  360. _window.x = _parentContainer.width / 2;
  361. _window.y = _parentContainer.height - _window.height / 2;
  362. break;
  363.  
  364. default:
  365. _window.x = _parentContainer.width / 2;
  366. _window.y = _parentContainer.height / 2;
  367. }
  368.  
  369. // Add the eventListeners
  370. _closeButton.addEventListener(MouseEvent.CLICK, hide);
  371.  
  372. // Launch an event saying the component is drawn
  373. this.dispatchEvent(new DialogEvent(DialogEvent.DRAWN));
  374. }
  375.  
  376.  
  377. /**
  378. *
  379. */
  380. private function drawTexts():void
  381. {
  382. if (!_windowFormat)
  383. _windowFormat = new TextFormat(_textFont ? _textFont.fontName : "_sans", 14, 0x333333);
  384. if (!_headerFormat)
  385. _headerFormat = new TextFormat(_textFont ? _textFont.fontName : "_sans", 16, 0x333333);
  386.  
  387. // Header Text
  388. if (_textFont) _headerText.embedFonts = true;
  389. _headerText.defaultTextFormat = _headerFormat;
  390. _headerText.autoSize = "left";
  391. _headerText.wordWrap = true;
  392. _headerText.htmlText = _title;
  393. _headerText.selectable = false;
  394.  
  395. // Window Text
  396. if (_textFont) _windowText.embedFonts = true;
  397. _windowText.defaultTextFormat = _windowFormat;
  398. _windowText.autoSize = "left";
  399. _windowText.wordWrap = true;
  400. _windowText.htmlText = _text;
  401. _windowText.selectable = _selectable;
  402. }
  403.  
  404.  
  405. /**
  406. *
  407. * @param params
  408. * @return
  409. */
  410. private function createButton(params:Object):Sprite
  411. {
  412. var button:Sprite = new Sprite;
  413. button.buttonMode = true;
  414. var up:Sprite = new ButtonUpGraphic();
  415. var over:Sprite = new ButtonOverGraphic();
  416. var down:Sprite = new ButtonDownGraphic();
  417.  
  418. var fmt:TextFormat = new TextFormat(_textFont ? _textFont.fontName : "_sans", 16, 0x333333);
  419. fmt.align = "center";
  420. var buttonLabel:TextField = new TextField();
  421. if (_textFont) buttonLabel.embedFonts = true;
  422. buttonLabel.text = params.label
  423. buttonLabel.autoSize = "center";
  424. buttonLabel.mouseEnabled = false;
  425. buttonLabel.x = buttonLabel.y = 0;
  426. buttonLabel.setTextFormat(fmt);
  427.  
  428. buttonLabel.x = up.width / 2 - buttonLabel.width / 2;
  429. buttonLabel.y = up.height / 2 - buttonLabel.height / 2;
  430.  
  431. over.visible = down.visible = false;
  432.  
  433. button.addChild(up);
  434. button.addChild(over);
  435. button.addChild(down);
  436. button.addChild(buttonLabel);
  437.  
  438. button.addEventListener(MouseEvent.MOUSE_DOWN, buttonDownHandler);
  439. button.addEventListener(MouseEvent.MOUSE_OVER, buttonOverHandler);
  440. button.addEventListener(MouseEvent.MOUSE_UP, buttonUpHandler);
  441. button.addEventListener(MouseEvent.MOUSE_OUT, buttonUpHandler);
  442.  
  443. up.name = "up";
  444. over.name = "over";
  445. down.name = "down";
  446. buttonLabel.name = "buttonLabel";
  447.  
  448. return button;
  449. }
  450.  
  451.  
  452. /**
  453. *
  454. * @param loaderContent
  455. */
  456. private function registerComponents(loaderContent:LoaderInfo):void
  457. {
  458. var swfLibrary:ApplicationDomain = loaderContent.applicationDomain;
  459.  
  460. // Window Header
  461. var HeaderGraphic:Class = swfLibrary.getDefinition("HeaderGraphic") as Class;
  462. _headerGraphic = new HeaderGraphic() as MovieClip;
  463.  
  464. // Main Window Background
  465. var WindowGraphic:Class = swfLibrary.getDefinition("WindowGraphic") as Class;
  466. _windowGraphic = new WindowGraphic() as MovieClip;
  467.  
  468. // Close Button
  469. var CloseButton:Class = swfLibrary.getDefinition("CloseButton") as Class;
  470. _closeButton = new CloseButton() as SimpleButton;
  471.  
  472. // Button graphics
  473. ButtonUpGraphic = swfLibrary.getDefinition("ButtonUpGraphic") as Class;
  474. ButtonOverGraphic = swfLibrary.getDefinition("ButtonOverGraphic") as Class;
  475. ButtonDownGraphic = swfLibrary.getDefinition("ButtonDownGraphic") as Class;
  476. }
  477.  
  478.  
  479. /**
  480. *
  481. * @param parameters
  482. */
  483. private function registerOptionalParameters(parameters:Object):void
  484. {
  485.  
  486. if (parameters && parameters.autoShow)
  487. {
  488. if (parameters.autoShow is Boolean)
  489. {
  490. _autoShow = parameters.autoShow;
  491. }
  492. else
  493. {
  494. trace("WARNING: Invalid value (non-Boolean) used in [autoShow], defaulting to false");
  495. _autoShow = false;
  496. }
  497. }
  498. else _autoShow = false;
  499.  
  500.  
  501. if (parameters && parameters.draggable)
  502. {
  503. if (parameters.draggable is Boolean)
  504. {
  505. _draggable = parameters.draggable;
  506. }
  507. else
  508. {
  509. trace("WARNING: Invalid value (non-Boolean) used in [draggable], defaulting to false");
  510. _draggable = false;
  511. }
  512. }
  513. else _draggable = false;
  514.  
  515.  
  516. if (parameters && parameters.position)
  517. {
  518. if (parameters.position == Dialog.CENTER || parameters.position == Dialog.TOP ||
  519. parameters.position == Dialog.BOTTOM || parameters.position == Dialog.LEFT ||
  520. parameters.position == Dialog.RIGHT)
  521. {
  522. _position = parameters.position;
  523. }
  524. else
  525. {
  526. trace("WARNING: Invalid value used in [position], defaulting to Dialog.CENTER");
  527. _position = Dialog.CENTER;
  528. }
  529. }
  530. else _position = Dialog.CENTER;
  531.  
  532.  
  533. if (parameters && parameters.defaultTween)
  534. {
  535. if (parameters.defaultTween is Function)
  536. {
  537. _defaultTween = parameters.defaultTween;
  538. }
  539. else
  540. {
  541. trace("WARNING: Invalid value (not a greensock easing function) used in [defaultTween], defaulting to Linear.easeIn");
  542. _defaultTween = Linear.easeIn;
  543. }
  544. }
  545. else _defaultTween = Linear.easeIn;
  546.  
  547.  
  548. if (parameters && parameters.titleIcon)
  549. {
  550. if (parameters.titleIcon is DisplayObject)
  551. {
  552. _titleIcon = parameters.titleIcon;
  553. }
  554. else
  555. {
  556. trace("WARNING: The [titleIcon] parameter can only accept a DisplayObject. No icon will be used");
  557. _titleIcon = null;
  558. }
  559. }
  560. else _titleIcon = null;
  561.  
  562.  
  563. if (parameters && parameters.textIcon)
  564. {
  565. if (parameters.textIcon is DisplayObject)
  566. {
  567. _textIcon = parameters.textIcon;
  568. }
  569. else
  570. {
  571. trace("WARNING: The [textIcon] parameter can only accept a DisplayObject. No icon will be used");
  572. _textIcon = null;
  573. }
  574. }
  575. else _textIcon = null;
  576.  
  577.  
  578. if (parameters && parameters.padding)
  579. {
  580. if (parameters.padding is int)
  581. {
  582. _padding = parameters.padding;
  583. }
  584. else
  585. {
  586. trace("WARNING: Invalid value (not an int) used in [padding], defaulting to 10");
  587. _padding = 10;
  588. }
  589. }
  590. else _padding = 10;
  591.  
  592.  
  593. if (parameters && parameters.buttons)
  594. {
  595. if (parameters.buttons is Dictionary)
  596. {
  597. _buttons = parameters.buttons;
  598. }
  599. else
  600. {
  601. trace("WARNING: Invalid value (not a Dictionary) used in [buttons], no button will be created");
  602. }
  603. }
  604.  
  605.  
  606. if (parameters && parameters.windowFormat)
  607. {
  608. if (parameters.windowFormat is TextFormat)
  609. {
  610. _windowFormat = parameters.windowFormat;
  611. }
  612. else
  613. {
  614. trace("WARNING: Invalid value (not a TextFormat) used in [windowFormat], reverting to default TextFormat");
  615. }
  616. }
  617.  
  618.  
  619. if (parameters && parameters.headerFormat)
  620. {
  621. if (parameters.headerFormat is TextFormat)
  622. {
  623. _headerFormat = parameters.headerFormat;
  624. }
  625. else
  626. {
  627. trace("WARNING: Invalid value (not a TextFormat) used in [headerFormat], reverting to default TextFormat");
  628. }
  629. }
  630.  
  631.  
  632. if (parameters && parameters.selectable)
  633. {
  634. if (parameters.selectable is Boolean)
  635. {
  636. _selectable = parameters.selectable;
  637. }
  638. else
  639. {
  640. trace("WARNING: Invalid value (non-Boolean) used in [selectable], text will be selectable");
  641. }
  642. }
  643.  
  644.  
  645. if (parameters && parameters.textFont)
  646. {
  647. if (parameters.textFont is Font)
  648. {
  649. _textFont = parameters.textFont;
  650. }
  651. else
  652. {
  653. trace("WARNING: Invalid value (not a Font) used in [textFont], '_sans' will be used");
  654. }
  655. }
  656. }
  657.  
  658.  
  659. /*****************************************
  660. * INTERMEDIATE FUNCTIONS
  661. */
  662.  
  663. /**
  664. *
  665. * @param event
  666. */
  667. private function buttonOverHandler(event:MouseEvent = null):void
  668. {
  669. event.target.getChildByName("up").visible = event.target.getChildByName("down").visible = false;
  670. event.target.getChildByName("over").visible = true;
  671. }
  672.  
  673. /**
  674. *
  675. * @param event
  676. */
  677. private function buttonUpHandler(event:MouseEvent = null):void
  678. {
  679. event.target.getChildByName("over").visible = event.target.getChildByName("down").visible = false;
  680. event.target.getChildByName("up").visible = true;
  681. }
  682.  
  683. /**
  684. *
  685. * @param event
  686. */
  687. private function buttonDownHandler(event:MouseEvent = null):void
  688. {
  689. event.target.getChildByName("up").visible = event.target.getChildByName("over").visible = false;
  690. event.target.getChildByName("down").visible = true;
  691. }
  692.  
  693. /**
  694. *
  695. * @param event
  696. */
  697. private function headerMouseDownHandler(event:MouseEvent):void
  698. {
  699. _window.startDrag();
  700. }
  701.  
  702. /**
  703. *
  704. * @param event
  705. */
  706. private function headerMouseUpHandler(event:MouseEvent):void
  707. {
  708. _window.stopDrag();
  709. }
  710.  
  711. /**
  712. *
  713. * @param event
  714. */
  715. private function ioErrorHandler(event:IOErrorEvent):void
  716. {
  717. trace("ERROR: Could not load the required swf.");
  718. this.dispatchEvent(new DialogEvent(DialogEvent.IO_ERROR));
  719. }
  720.  
  721.  
  722. /*****************************************************************
  723. * GETTERS / SETTERS
  724. ****************************************************************/
  725.  
  726. /**
  727. *
  728. */
  729. public function get ready():Boolean
  730. {
  731. return _ready;
  732. }
  733.  
  734. public function get shown():Boolean
  735. {
  736. return _shown;
  737. }
  738.  
  739. public function get loading():Boolean
  740. {
  741. return _loading;
  742. }
  743.  
  744. }
  745.  
  746. }
Add Comment
Please, Sign In to add comment