/** * Window.as * Keith Peters * version 0.97 * * A draggable window. Can be used as a container for other components. * * Copyright (c) 2009 Keith Peters * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package com.bit101.components { import flash.display.DisplayObjectContainer; import flash.events.MouseEvent; public class Window extends Component { private var _title:String; private var _titleBar:Panel; private var _titleLabel:Label; private var _panel:Panel; private var _color:int = -1; private var _shadow:Boolean = true; /** * Constructor * @param parent The parent DisplayObjectContainer on which to add this Panel. * @param xpos The x position to place this component. * @param ypos The y position to place this component. * @param title The string to display in the title bar. */ public function Window(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0, title:String="Window") { _title = title; super(parent, xpos, ypos); } /** * Initializes the component. */ override protected function init():void { super.init(); setSize(100, 100); } /** * Creates and adds the child display objects of this component. */ override protected function addChildren():void { _titleBar = new Panel(this); _titleBar.buttonMode = true; _titleBar.useHandCursor = true; _titleBar.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown); _titleLabel = new Label(_titleBar.content, 5, 1, _title); _panel = new Panel(this, 0, 20); filters = [getShadow(4, false)]; } /////////////////////////////////// // public methods /////////////////////////////////// /** * Draws the visual ui of the component. */ override public function draw():void { super.draw(); _titleBar.color = _color; _panel.color = _color; _titleBar.width = width; _panel.setSize(_width, _height - 20); } /////////////////////////////////// // event handlers /////////////////////////////////// /** * Internal mouseDown handler. Starts a drag. * @param event The MouseEvent passed by the system. */ protected function onMouseDown(event:MouseEvent):void { this.startDrag(); stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp); parent.addChild(this); } /** * Internal mouseUp handler. Stops the drag. * @param event The MouseEvent passed by the system. */ protected function onMouseUp(event:MouseEvent):void { this.stopDrag(); stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp); } /////////////////////////////////// // getter/setters /////////////////////////////////// /** * Gets / sets whether or not this Panel will have an inner shadow. */ public function set shadow(b:Boolean):void { _shadow = b; _titleBar.shadow = _shadow; _panel.shadow = _shadow; if(_shadow) { filters = [getShadow(4, false)]; } else { filters = []; } } public function get shadow():Boolean { return _shadow; } /** * Gets / sets the backgrond color of this panel. */ public function set color(c:int):void { _color = c; invalidate(); } public function get color():int { return _color; } /** * Gets / sets the title shown in the title bar. */ public function set title(t:String):void { _title = t; _titleLabel.text = _title; } public function get title():String { return _title; } /** * Container for content added to this panel. This is just a reference to the content of the internal Panel, which is masked, so best to add children to content, rather than directly to the window. */ public function get content():DisplayObjectContainer { return _panel.content; } } }