Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.hit.display.ui
- {
- import flash.display.BitmapData;
- import flash.display.DisplayObject;
- import flash.display.DisplayObjectContainer;
- import flash.display.Loader;
- import flash.display.LoaderInfo;
- import flash.display.MovieClip;
- import flash.display.SimpleButton;
- import flash.display.SpreadMethod;
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.events.IOErrorEvent;
- import flash.events.MouseEvent;
- import flash.net.URLRequest;
- import flash.system.ApplicationDomain;
- import flash.text.Font;
- import flash.text.TextField;
- import flash.text.TextFormat;
- import flash.utils.getQualifiedClassName;
- import flash.utils.Dictionary;
- import com.greensock.TweenNano;
- import com.greensock.easing.Back;
- import com.greensock.easing.Bounce;
- import com.greensock.easing.Linear;
- import com.hit.events.DialogEvent;
- /**
- * Dialog
- *
- * TODO: Change _defaultTween to be a function instead. Have the class use Linear.easeIn as a
- * default. The user already needs greensock's library anyways, might as well
- * streamline the handling of the tweens.
- * TODO: Allow the user to access and modify the TextFields (HeaderText, WindowText and Button)
- * TODO?: Allow the user to manually position the window
- *
- * @author Guyllaume Cardinal
- */
- public class Dialog extends Sprite
- {
- // Name constant, used so no constant shares the same name as an other class' constants
- private static const NAME:String = "Dialog";
- // Components URL. Dictates to the class where to fetch the SWF used to create the window
- // Will be used by default.
- private static const ASSETS_URL:String = "components/DialogAssets/DialogAssets.swf";
- // Position constants
- public static const TOP:String = NAME + "Top";
- public static const BOTTOM:String = NAME + "Bottom";
- public static const LEFT:String = NAME + "Left";
- public static const RIGHT:String = NAME + "Right";
- public static const CENTER:String = NAME + "Center";
- // Function Strings
- public static const SHOW_FUNCTION:String = "show";
- public static const UPDATE_FUNCTION:String = "update";
- public static const HIDE_FUNCTION:String = "hide";
- private var _dialog:Dialog;
- // Window graphics
- private var _headerGraphic:MovieClip;
- private var _windowGraphic:MovieClip;
- private var _closeButton:SimpleButton;
- private var _overlayGraphic:Sprite;
- private var _buttonContainer:Sprite;
- // Button classes
- private var ButtonUpGraphic:Class;
- private var ButtonOverGraphic:Class;
- private var ButtonDownGraphic:Class;
- // Component parameters
- private var _parentContainer:DisplayObjectContainer;
- private var _window:Sprite;
- private var _text:String;
- private var _title:String;
- // Component texts
- private var _headerFormat:TextFormat;
- private var _windowFormat:TextFormat;
- private var _headerText:TextField;
- private var _windowText:TextField;
- private var _textFont:Font;
- private var _selectable:Boolean;
- // Optional parameters
- private var _draggable:Boolean;
- private var _autoShow:Boolean;
- private var _position:String;
- private var _titleIcon:DisplayObject;
- private var _textIcon:DisplayObject;
- private var _defaultTween:Function;
- private var _buttons:Dictionary;
- private var _padding:int;
- // Control variables
- private var _ready:Boolean;
- private var _loading:Boolean;
- private var _shown:Boolean;
- /**
- *
- * @param parent DisplayObjectContainer The DisplayObjectContainer in which to add
- * or remove the Dialog
- * @param title String The title of the Dialog
- * @param text String
- * @param options Object
- */
- public function Dialog(parent:DisplayObjectContainer, title:String, text:String,
- options:Object = null ):void
- {
- _dialog = this as Dialog;
- // Store the required parameters
- _parentContainer = parent;
- _text = text;
- _title = title;
- // Register the optional parameters, setting up the default values if none was passed
- registerOptionalParameters(options);
- // Load the swf with the components to draw the actual window
- var assetsLoader:Loader = new Loader();
- assetsLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
- assetsLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, assetsLoadedhandler);
- assetsLoader.load(new URLRequest(ASSETS_URL));
- _loading = true;
- }
- /**
- *
- * @param tweenType
- */
- public function show():void
- {
- _parentContainer.addChild(_dialog);
- // Configue and tweeen the overlay and window
- TweenNano.from(_overlayGraphic, 0.5, { alpha:0, ease:_defaultTween } );
- TweenNano.from(_window, 0.6, { width:0, height:0, ease:_defaultTween } );
- // Allow the window to be dragged if the option is selected
- if (_draggable)
- {
- _headerGraphic.buttonMode = true;
- _headerGraphic.addEventListener(MouseEvent.MOUSE_DOWN, headerMouseDownHandler, false, 0, true);
- this.stage.addEventListener(MouseEvent.MOUSE_UP, headerMouseUpHandler, false, 0, true);
- }
- this.dispatchEvent(new DialogEvent(DialogEvent.SHOWN));
- _shown = true;
- }
- /**
- *
- * @param event
- */
- public function hide(event:MouseEvent = null):void
- {
- if (_draggable)
- {
- _headerGraphic.removeEventListener(MouseEvent.MOUSE_DOWN, headerMouseDownHandler);
- this.stage.removeEventListener(MouseEvent.MOUSE_UP, headerMouseUpHandler);
- }
- TweenNano.to(_overlayGraphic, 0.3, { alpha:0, ease:_defaultTween } );
- TweenNano.to(_window, 0.6, { width:0, height:0, alpha: 0, ease:_defaultTween,
- onComplete: function() {
- _parentContainer.removeChild(_dialog);
- } } );
- this.dispatchEvent(new DialogEvent(DialogEvent.HIDDEN));
- _shown = false;
- }
- /**
- *
- */
- public function update():void
- {
- draw();
- this.dispatchEvent(new DialogEvent(DialogEvent.UPDATED));
- }
- /**
- *
- * @param event
- */
- private function assetsLoadedhandler(event:Event):void
- {
- dispatchEvent(new DialogEvent(DialogEvent.LOADED));
- _loading = false;
- configUI(event.target as LoaderInfo);
- draw();
- dispatchEvent(new DialogEvent(DialogEvent.READY));
- _ready = true;
- if (_autoShow) show();
- }
- /**
- *
- * @param loaderContent
- */
- private function configUI(loaderContent:LoaderInfo = null):void
- {
- if (loaderContent)
- registerComponents(loaderContent);
- _window = new Sprite();
- // Setup the header
- _window.addChild(_headerGraphic);
- // Setup the main window
- _window.addChild(_windowGraphic);
- _window.addChild(_closeButton);
- // Adds the icon in the header if one is supplied
- if (_titleIcon)
- _headerGraphic.addChild(_titleIcon);
- if (_textIcon)
- _windowGraphic.addChild(_textIcon);
- if (_buttons)
- {
- _buttonContainer = new Sprite();
- _windowGraphic.addChild(_buttonContainer);
- }
- // Textfields
- _headerText = new TextField();
- _windowText = new TextField();
- _headerGraphic.addChild(_headerText);
- _windowGraphic.addChild(_windowText);
- // background overlay
- _overlayGraphic = new Sprite();
- _overlayGraphic.graphics.beginBitmapFill(new BitmapData(_parentContainer.width,
- _parentContainer.height, false,
- 0x000000), null, false);
- _overlayGraphic.graphics.drawRect(0, 0, _parentContainer.width, _parentContainer.height);
- _overlayGraphic.graphics.endFill();
- _overlayGraphic.alpha = 0.5;
- this.addChildAt(_overlayGraphic, 0);
- this.addChild(_window);
- }
- /**
- *
- */
- private function draw():void
- {
- // Print the texts
- drawTexts();
- // Header
- _headerText.y = _headerGraphic.height / 2 - _headerText.height / 2;
- if (_titleIcon)
- {
- _titleIcon.x = _padding;
- _titleIcon.y = _headerText.y + _headerText.height / 2 - _titleIcon.height / 2;
- _headerText.x = _titleIcon.x + _titleIcon.width + _padding;
- }
- else
- {
- _headerText.x = _padding;
- }
- // Main window
- _windowText.y = _padding;
- if (_textIcon)
- {
- _textIcon.x = _padding;
- _textIcon.y = _padding;
- _windowText.x = _textIcon.x + _textIcon.width + _padding;
- // _padding * 3 because we have padding on the left, right and between the text and icon
- _windowText.width = _window.width - _textIcon.width - _padding * 3;
- }
- else
- {
- _windowText.x = _padding;
- _windowText.width = _windowGraphic.width - _padding * 2;
- }
- // Position the elements
- _windowGraphic.y = _headerGraphic.y + _headerGraphic.height;
- _closeButton.y = _headerGraphic.height / 2 - _closeButton.height / 2;
- _closeButton.x = _headerGraphic.width - _closeButton.width - _padding;
- // Buttons
- if (_buttons)
- {
- var buttonIterator:int = 0;
- for (var key:Object in _buttons)
- {
- var button:Sprite = createButton(_buttons[key]);
- button.mouseChildren = false;
- var buttonFunction:Function;
- if (_buttons[key].callback is Function)
- buttonFunction = _buttons[key].callback as Function;
- else
- buttonFunction = this[_buttons[key].callback] as Function;
- button.addEventListener(MouseEvent.CLICK, buttonFunction);
- button.x = (button.width + _padding) * buttonIterator;
- _buttonContainer.addChild(button)
- buttonIterator++;
- }
- _buttonContainer.x = _windowGraphic.width - _buttonContainer.width - _padding;
- _buttonContainer.y = _windowText.y + _windowText.height + _padding;
- }
- _windowGraphic.getChildAt(0).height = _windowGraphic.height + _padding * 2;
- // Center the registration point
- var halfHeight:int = _window.height / 2;
- var halfWidth:int = _window.width / 2;
- var i:int = 0;
- var n:int = _window.numChildren;
- for ( ; i < n; i++ )
- {
- var obj:DisplayObject = _window.getChildAt(i);
- obj.x -= halfWidth;
- obj.y -= halfHeight;
- }
- // Set the initial position of the Dialog window
- switch (_position)
- {
- case Dialog.CENTER:
- _window.x = _parentContainer.width / 2;
- _window.y = _parentContainer.height / 2;
- break;
- case Dialog.LEFT:
- _window.x = _window.width / 2;
- _window.y = _parentContainer.height / 2;
- break;
- case Dialog.RIGHT:
- _window.x = _parentContainer.width - _window.width / 2;
- _window.y = _parentContainer.height / 2;
- break;
- case Dialog.TOP:
- _window.x = _parentContainer.width / 2;
- _window.y = _window.height / 2;
- break;
- case Dialog.BOTTOM:
- _window.x = _parentContainer.width / 2;
- _window.y = _parentContainer.height - _window.height / 2;
- break;
- default:
- _window.x = _parentContainer.width / 2;
- _window.y = _parentContainer.height / 2;
- }
- // Add the eventListeners
- _closeButton.addEventListener(MouseEvent.CLICK, hide);
- // Launch an event saying the component is drawn
- this.dispatchEvent(new DialogEvent(DialogEvent.DRAWN));
- }
- /**
- *
- */
- private function drawTexts():void
- {
- if (!_windowFormat)
- _windowFormat = new TextFormat(_textFont ? _textFont.fontName : "_sans", 14, 0x333333);
- if (!_headerFormat)
- _headerFormat = new TextFormat(_textFont ? _textFont.fontName : "_sans", 16, 0x333333);
- // Header Text
- if (_textFont) _headerText.embedFonts = true;
- _headerText.defaultTextFormat = _headerFormat;
- _headerText.autoSize = "left";
- _headerText.wordWrap = true;
- _headerText.htmlText = _title;
- _headerText.selectable = false;
- // Window Text
- if (_textFont) _windowText.embedFonts = true;
- _windowText.defaultTextFormat = _windowFormat;
- _windowText.autoSize = "left";
- _windowText.wordWrap = true;
- _windowText.htmlText = _text;
- _windowText.selectable = _selectable;
- }
- /**
- *
- * @param params
- * @return
- */
- private function createButton(params:Object):Sprite
- {
- var button:Sprite = new Sprite;
- button.buttonMode = true;
- var up:Sprite = new ButtonUpGraphic();
- var over:Sprite = new ButtonOverGraphic();
- var down:Sprite = new ButtonDownGraphic();
- var fmt:TextFormat = new TextFormat(_textFont ? _textFont.fontName : "_sans", 16, 0x333333);
- fmt.align = "center";
- var buttonLabel:TextField = new TextField();
- if (_textFont) buttonLabel.embedFonts = true;
- buttonLabel.text = params.label
- buttonLabel.autoSize = "center";
- buttonLabel.mouseEnabled = false;
- buttonLabel.x = buttonLabel.y = 0;
- buttonLabel.setTextFormat(fmt);
- buttonLabel.x = up.width / 2 - buttonLabel.width / 2;
- buttonLabel.y = up.height / 2 - buttonLabel.height / 2;
- over.visible = down.visible = false;
- button.addChild(up);
- button.addChild(over);
- button.addChild(down);
- button.addChild(buttonLabel);
- button.addEventListener(MouseEvent.MOUSE_DOWN, buttonDownHandler);
- button.addEventListener(MouseEvent.MOUSE_OVER, buttonOverHandler);
- button.addEventListener(MouseEvent.MOUSE_UP, buttonUpHandler);
- button.addEventListener(MouseEvent.MOUSE_OUT, buttonUpHandler);
- up.name = "up";
- over.name = "over";
- down.name = "down";
- buttonLabel.name = "buttonLabel";
- return button;
- }
- /**
- *
- * @param loaderContent
- */
- private function registerComponents(loaderContent:LoaderInfo):void
- {
- var swfLibrary:ApplicationDomain = loaderContent.applicationDomain;
- // Window Header
- var HeaderGraphic:Class = swfLibrary.getDefinition("HeaderGraphic") as Class;
- _headerGraphic = new HeaderGraphic() as MovieClip;
- // Main Window Background
- var WindowGraphic:Class = swfLibrary.getDefinition("WindowGraphic") as Class;
- _windowGraphic = new WindowGraphic() as MovieClip;
- // Close Button
- var CloseButton:Class = swfLibrary.getDefinition("CloseButton") as Class;
- _closeButton = new CloseButton() as SimpleButton;
- // Button graphics
- ButtonUpGraphic = swfLibrary.getDefinition("ButtonUpGraphic") as Class;
- ButtonOverGraphic = swfLibrary.getDefinition("ButtonOverGraphic") as Class;
- ButtonDownGraphic = swfLibrary.getDefinition("ButtonDownGraphic") as Class;
- }
- /**
- *
- * @param parameters
- */
- private function registerOptionalParameters(parameters:Object):void
- {
- if (parameters && parameters.autoShow)
- {
- if (parameters.autoShow is Boolean)
- {
- _autoShow = parameters.autoShow;
- }
- else
- {
- trace("WARNING: Invalid value (non-Boolean) used in [autoShow], defaulting to false");
- _autoShow = false;
- }
- }
- else _autoShow = false;
- if (parameters && parameters.draggable)
- {
- if (parameters.draggable is Boolean)
- {
- _draggable = parameters.draggable;
- }
- else
- {
- trace("WARNING: Invalid value (non-Boolean) used in [draggable], defaulting to false");
- _draggable = false;
- }
- }
- else _draggable = false;
- if (parameters && parameters.position)
- {
- if (parameters.position == Dialog.CENTER || parameters.position == Dialog.TOP ||
- parameters.position == Dialog.BOTTOM || parameters.position == Dialog.LEFT ||
- parameters.position == Dialog.RIGHT)
- {
- _position = parameters.position;
- }
- else
- {
- trace("WARNING: Invalid value used in [position], defaulting to Dialog.CENTER");
- _position = Dialog.CENTER;
- }
- }
- else _position = Dialog.CENTER;
- if (parameters && parameters.defaultTween)
- {
- if (parameters.defaultTween is Function)
- {
- _defaultTween = parameters.defaultTween;
- }
- else
- {
- trace("WARNING: Invalid value (not a greensock easing function) used in [defaultTween], defaulting to Linear.easeIn");
- _defaultTween = Linear.easeIn;
- }
- }
- else _defaultTween = Linear.easeIn;
- if (parameters && parameters.titleIcon)
- {
- if (parameters.titleIcon is DisplayObject)
- {
- _titleIcon = parameters.titleIcon;
- }
- else
- {
- trace("WARNING: The [titleIcon] parameter can only accept a DisplayObject. No icon will be used");
- _titleIcon = null;
- }
- }
- else _titleIcon = null;
- if (parameters && parameters.textIcon)
- {
- if (parameters.textIcon is DisplayObject)
- {
- _textIcon = parameters.textIcon;
- }
- else
- {
- trace("WARNING: The [textIcon] parameter can only accept a DisplayObject. No icon will be used");
- _textIcon = null;
- }
- }
- else _textIcon = null;
- if (parameters && parameters.padding)
- {
- if (parameters.padding is int)
- {
- _padding = parameters.padding;
- }
- else
- {
- trace("WARNING: Invalid value (not an int) used in [padding], defaulting to 10");
- _padding = 10;
- }
- }
- else _padding = 10;
- if (parameters && parameters.buttons)
- {
- if (parameters.buttons is Dictionary)
- {
- _buttons = parameters.buttons;
- }
- else
- {
- trace("WARNING: Invalid value (not a Dictionary) used in [buttons], no button will be created");
- }
- }
- if (parameters && parameters.windowFormat)
- {
- if (parameters.windowFormat is TextFormat)
- {
- _windowFormat = parameters.windowFormat;
- }
- else
- {
- trace("WARNING: Invalid value (not a TextFormat) used in [windowFormat], reverting to default TextFormat");
- }
- }
- if (parameters && parameters.headerFormat)
- {
- if (parameters.headerFormat is TextFormat)
- {
- _headerFormat = parameters.headerFormat;
- }
- else
- {
- trace("WARNING: Invalid value (not a TextFormat) used in [headerFormat], reverting to default TextFormat");
- }
- }
- if (parameters && parameters.selectable)
- {
- if (parameters.selectable is Boolean)
- {
- _selectable = parameters.selectable;
- }
- else
- {
- trace("WARNING: Invalid value (non-Boolean) used in [selectable], text will be selectable");
- }
- }
- if (parameters && parameters.textFont)
- {
- if (parameters.textFont is Font)
- {
- _textFont = parameters.textFont;
- }
- else
- {
- trace("WARNING: Invalid value (not a Font) used in [textFont], '_sans' will be used");
- }
- }
- }
- /*****************************************
- * INTERMEDIATE FUNCTIONS
- */
- /**
- *
- * @param event
- */
- private function buttonOverHandler(event:MouseEvent = null):void
- {
- event.target.getChildByName("up").visible = event.target.getChildByName("down").visible = false;
- event.target.getChildByName("over").visible = true;
- }
- /**
- *
- * @param event
- */
- private function buttonUpHandler(event:MouseEvent = null):void
- {
- event.target.getChildByName("over").visible = event.target.getChildByName("down").visible = false;
- event.target.getChildByName("up").visible = true;
- }
- /**
- *
- * @param event
- */
- private function buttonDownHandler(event:MouseEvent = null):void
- {
- event.target.getChildByName("up").visible = event.target.getChildByName("over").visible = false;
- event.target.getChildByName("down").visible = true;
- }
- /**
- *
- * @param event
- */
- private function headerMouseDownHandler(event:MouseEvent):void
- {
- _window.startDrag();
- }
- /**
- *
- * @param event
- */
- private function headerMouseUpHandler(event:MouseEvent):void
- {
- _window.stopDrag();
- }
- /**
- *
- * @param event
- */
- private function ioErrorHandler(event:IOErrorEvent):void
- {
- trace("ERROR: Could not load the required swf.");
- this.dispatchEvent(new DialogEvent(DialogEvent.IO_ERROR));
- }
- /*****************************************************************
- * GETTERS / SETTERS
- ****************************************************************/
- /**
- *
- */
- public function get ready():Boolean
- {
- return _ready;
- }
- public function get shown():Boolean
- {
- return _shown;
- }
- public function get loading():Boolean
- {
- return _loading;
- }
- }
- }
Add Comment
Please, Sign In to add comment