/src/com/addictedcompany/skin/Skin.as
ActionScript | 659 lines | 494 code | 107 blank | 58 comment | 38 complexity | 944150db50df7f904899e781f4815a4a MD5 | raw file
- package com.addictedcompany.skin
- {
- import adobe.utils.CustomActions;
- import flash.display.BitmapData;
- import flash.display.DisplayObject;
- import flash.display.Graphics;
- import flash.display.InteractiveObject;
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.geom.Point;
- import flash.geom.Rectangle;
- import flash.utils.Dictionary;
- import org.bytearray.display.ScaleBitmap;
- /**
- * ...
- * @author Jewelz
- */
- public class Skin extends Sprite
- {
- //////////////////////////////////////////////////////////////////////////////////////////////////
- // CONSTS //
- //////////////////////////////////////////////////////////////////////////////////////////////////
-
- public static const ALIGN_TOP:String = "alignTop";
- public static const ALIGN_BOTTOM:String = "alignBottom";
- public static const ALIGN_LEFT:String = "alignLeft";
- public static const ALIGN_RIGHT:String = "alignRight";
- public static const ALIGN_TOP_LEFT:String = "alignTopLeft";
- public static const ALIGN_TOP_RIGHT:String = "alignTopRight";
- public static const ALIGN_BOTTOM_LEFT:String = "alignBottomLeft";
- public static const ALIGN_BOTTOM_RIGHT:String = "alignBottomRight";
- public static const ALIGN_CENTER:String = "alignCenter";
- public static const NO_ALIGN:String = "noAlign";
-
- //////////////////////////////////////////////////////////////////////////////////////////////////
- // PRIVATE/PROTECTED VARS //
- //////////////////////////////////////////////////////////////////////////////////////////////////
-
- private var _align:String;
- private var _width:int;
- private var _height:int;
-
- private var actualWidth:int;
- private var actualHeight:int;
-
- private var states:/*SkinState*/Array;
-
- private var currentStateIndex:int;
- private var currentState:SkinState;
-
- //////////////////////////////////////////////////////////////////////////////////////////////////
-
- /**
- * Контейнер для фона
- */
- protected var cont:Sprite;
-
-
- public function Skin(width:int, height:int, align:String = "noAlign")
- {
- _width = width;
- _height = height;
- _align = align;
-
- init();
- }
-
- //////////////////////////////////////////////////////////////////////////////////////////////////
- // PUBLIC METHODS //
- //////////////////////////////////////////////////////////////////////////////////////////////////
-
- /**
- * Устанавливает размеры скина
- * @param width
- * @param height
- */
- public function setSize(width:int, height:int):void
- {
- _width = width;
- _height = height;
-
- resize();
- }
-
- /**
- * Устанавливает состояние скина
- * @param index номер состояния
- */
- public function setState(index:int):void
- {
- if (states[index])
- {
- currentState = states[index];
- currentStateIndex = index;
- removeOldState();
- addNewState();
- resize(false);
- }
- }
-
- private function addNewState():void
- {
- var i:int;
- var length:int = currentState.childs.length;
- for (i = 0; i < length; i++)
- {
- cont.addChild(currentState.childs[i] as DisplayObject);
- }
- }
-
- private function removeOldState():void
- {
- while (cont.numChildren) cont.removeChildAt(0);
- }
-
- /**
- * Добавляет растягиваемый растр со смещениями
- * @param bitmapData
- * @param scale9grid
- * @param offset x - смещение слева, y - сверху, width - справа, height - снизу
- */
- public function addBitmap(bitmapData:BitmapData, scale9grid:Rectangle, offset:Rectangle, state:int = 0):void
- {
- var sb:ScaleBitmap = new ScaleBitmap(bitmapData);
- sb.scale9Grid = scale9grid;
- if (state == 0) cont.addChild(sb);
-
- var s:SkinState = states[state];
- if (!s)
- {
- s = new SkinState();
- states[state] = s;
- if (state == 0) currentState = s;
- }
-
- s.childs.push(sb);
- s.bitmaps.push(sb);
- s.offsets[sb] = offset;
- }
-
- /**
- * Добавляет tile для отрисовки методом bitmapDraw
- * @param bitmapData
- * @param offset
- */
- public function addDrawBitmap(bitmapData:BitmapData, offset:Rectangle, state:int = 0):void
- {
- var c:Sprite = new Sprite();
- if (state == 0) cont.addChild(c);
-
- var s:SkinState = states[state];
- if (!s)
- {
- s = new SkinState();
- states[state] = s;
- if (state == 0) currentState = s;
- }
-
- s.childs.push(c);
- s.bitmapDraws.push(c);
- s.offsets[c] = offset;
- s.bitmapForDraw[c] = bitmapData;
- }
-
- //////////////////////////////////////////////////////////////////////////////////////////////////
- // PRIVATE/PROTECTED METHODS //
- //////////////////////////////////////////////////////////////////////////////////////////////////
-
- private function init():void
- {
- if (stage) onAdded(null);
- else addEventListener(Event.ADDED_TO_STAGE, onAdded);
-
- cont = new Sprite();
- addChild(cont);
-
- states = [];
-
- currentStateIndex = 0;
- }
-
- protected function resize(dispatch:Boolean = true):void
- {
- if (stage)
- {
- switch (_align)
- {
- case NO_ALIGN:
- alignNo();
- break;
-
- case ALIGN_CENTER:
- alignCenter()
- break;
-
- case ALIGN_LEFT:
- alignLeft();
- break;
-
- case ALIGN_RIGHT:
- alignRight();
- break;
-
- case ALIGN_BOTTOM:
- alignBottom();
- break;
-
- case ALIGN_TOP:
- alignTop();
- break;
-
- case ALIGN_TOP_LEFT:
- alignTopLeft();
- break;
-
- case ALIGN_TOP_RIGHT:
- alignTopRight();
- break;
-
- case ALIGN_BOTTOM_LEFT:
- alignBottomLeft();
- break;
-
- case ALIGN_BOTTOM_RIGHT:
- alignBottomRight();
- break;
- }
-
- if (dispatch)
- {
- dispatchEvent(new Event(Event.RESIZE));
- }
- }
- }
-
- private function alignNo():void
- {
- var offset:Rectangle;
-
- actualWidth = _width;
- actualHeight = _height;
-
- var i:int;
- var length:int = bitmaps.length;
- for (i = 0; i < length; i++)
- {
- var sb:ScaleBitmap = bitmaps[i] as ScaleBitmap;
- offset = offsets[sb];
- sb.x = offset.x;
- sb.y = offset.y;
- sb.setSize(_width - offset.x - offset.width, _height - offset.height - offset.y);
- }
-
- length = currentState.bitmapDraws.length;
- for (i = 0; i < length; i++)
- {
- var bdCont:Sprite = bitmapDraws[i] as Sprite;
- var bd:BitmapData = bitmapForDraw[bdCont];
- offset = offsets[bdCont];
- var g:Graphics = bdCont.graphics;
- g.clear();
- g.beginBitmapFill(bd);
- g.drawRect(offset.x, offset.y, _width - offset.x - offset.width, _height - offset.height - offset.y);
- g.endFill();
- }
- }
-
- private function alignCenter():void
- {
- this.x = stage.stageWidth / 2 - _width / 2;
- this.y = stage.stageHeight / 2 - _height / 2;
-
- actualWidth = _width;
- actualHeight = _height;
-
- var offset:Rectangle;
-
- var i:int;
- var length:int = bitmaps.length;
- for (i = 0; i < length; i++)
- {
- var sb:ScaleBitmap = bitmaps[i] as ScaleBitmap;
- offset = offsets[sb];
- sb.x = offset.x;
- sb.y = offset.y;
- sb.setSize(_width - offset.x - offset.width, _height - offset.height - offset.y);
- }
-
- length = bitmapDraws.length;
- for (i = 0; i < length; i++)
- {
- var bdCont:Sprite = bitmapDraws[i] as Sprite;
- var bd:BitmapData = bitmapForDraw[bdCont];
- offset = offsets[bdCont];
- var g:Graphics = bdCont.graphics;
- g.clear();
- g.beginBitmapFill(bd);
- g.drawRect(offset.x, offset.y, _width - offset.x - offset.width, _height - offset.height - offset.y);
- g.endFill();
- }
- }
-
- private function alignLeft():void
- {
- this.x = 0;
- this.y = 0;
-
- actualWidth = _width;
- actualHeight = stage.stageHeight;
-
- var offset:Rectangle;
-
- var i:int;
- var length:int = bitmaps.length;
- for (i = 0; i < length; i++)
- {
- var sb:ScaleBitmap = bitmaps[i] as ScaleBitmap;
- offset = offsets[sb];
- sb.x = offset.x;
- sb.y = offset.y;
- sb.setSize(_width - offset.x - offset.width, stage.stageHeight - offset.height - offset.y);
- }
-
- length = bitmapDraws.length;
- for (i = 0; i < length; i++)
- {
- var bdCont:Sprite = bitmapDraws[i] as Sprite;
- var bd:BitmapData = bitmapForDraw[bdCont];
- offset = offsets[bdCont];
- var g:Graphics = bdCont.graphics;
- g.clear();
- g.beginBitmapFill(bd);
- g.drawRect(offset.x, offset.y, _width - offset.x - offset.width, stage.stageHeight - offset.height - offset.y);
- g.endFill();
- }
- }
-
- private function alignRight():void
- {
- this.x = stage.stageWidth - _width;
- this.y = 0;
-
- actualWidth = _width;
- actualHeight = stage.stageHeight;
-
- var offset:Rectangle;
-
- var i:int;
- var length:int = bitmaps.length;
- for (i = 0; i < length; i++)
- {
- var sb:ScaleBitmap = bitmaps[i] as ScaleBitmap;
- offset = offsets[sb];
- sb.x = offset.x;
- sb.y = offset.y;
- sb.setSize(_width - offset.x - offset.width, stage.stageHeight - offset.height - offset.y);
- }
-
- length = bitmapDraws.length;
- for (i = 0; i < length; i++)
- {
- var bdCont:Sprite = bitmapDraws[i] as Sprite;
- var bd:BitmapData = bitmapForDraw[bdCont];
- offset = offsets[bdCont];
- var g:Graphics = bdCont.graphics;
- g.clear();
- g.beginBitmapFill(bd);
- g.drawRect(offset.x, offset.y, _width - offset.x - offset.width, stage.stageHeight - offset.height - offset.y);
- g.endFill();
- }
- }
-
- private function alignBottom():void
- {
- this.x = 0;
- this.y = stage.stageHeight - _height;
-
- actualWidth = stage.stageWidth;
- actualHeight = _height;
-
- var offset:Rectangle;
-
- var i:int;
- var length:int = bitmaps.length;
- for (i = 0; i < length; i++)
- {
- var sb:ScaleBitmap = bitmaps[i] as ScaleBitmap;
- offset = offsets[sb];
- sb.x = offset.x;
- sb.y = offset.y;
- sb.setSize(stage.stageWidth - offset.x - offset.width, _height - offset.height - offset.y);
- }
-
- length = bitmapDraws.length;
- for (i = 0; i < length; i++)
- {
- var bdCont:Sprite = bitmapDraws[i] as Sprite;
- var bd:BitmapData = bitmapForDraw[bdCont];
- offset = offsets[bdCont];
- var g:Graphics = bdCont.graphics;
- g.clear();
- g.beginBitmapFill(bd);
- g.drawRect(offset.x, offset.y, stage.stageWidth - offset.x - offset.width, _height - offset.height - offset.y);
- g.endFill();
- }
- }
-
- private function alignTop():void
- {
- this.x = 0;
- this.y = 0;
-
- actualWidth = stage.stageWidth;
- actualHeight = _height;
-
- var offset:Rectangle;
-
- var i:int;
- var length:int = bitmaps.length;
- for (i = 0; i < length; i++)
- {
- var sb:ScaleBitmap = bitmaps[i] as ScaleBitmap;
- offset = offsets[sb];
- sb.x = offset.x;
- sb.y = offset.y;
- sb.setSize(stage.stageWidth - offset.x - offset.width, _height - offset.height - offset.y);
- }
-
- length = bitmapDraws.length;
- for (i = 0; i < length; i++)
- {
- var bdCont:Sprite = bitmapDraws[i] as Sprite;
- var bd:BitmapData = bitmapForDraw[bdCont];
- offset = offsets[bdCont];
- var g:Graphics = bdCont.graphics;
- g.clear();
- g.beginBitmapFill(bd);
- g.drawRect(offset.x, offset.y, stage.stageWidth - offset.x - offset.width, _height - offset.height - offset.y);
- g.endFill();
- }
- }
-
- private function alignBottomLeft():void
- {
- this.x = 0;
- this.y = stage.stageHeight - _height;
-
- actualWidth = _width;
- actualHeight = _height;
-
- var offset:Rectangle;
-
- var i:int;
- var length:int = bitmaps.length;
- for (i = 0; i < length; i++)
- {
- var sb:ScaleBitmap = bitmaps[i] as ScaleBitmap;
- offset = offsets[sb];
- sb.x = offset.x;
- sb.y = offset.y;
- sb.setSize(_width - offset.x - offset.width, _height - offset.height - offset.y);
- }
-
- length = bitmapDraws.length;
- for (i = 0; i < length; i++)
- {
- var bdCont:Sprite = bitmapDraws[i] as Sprite;
- var bd:BitmapData = bitmapForDraw[bdCont];
- offset = offsets[bdCont];
- var g:Graphics = bdCont.graphics;
- g.clear();
- g.beginBitmapFill(bd);
- g.drawRect(offset.x, offset.y, _width - offset.x - offset.width, _height - offset.height - offset.y);
- g.endFill();
- }
- }
-
- private function alignBottomRight():void
- {
- this.x = stage.stageWidth - _width;
- this.y = stage.stageHeight - _height;
-
- actualWidth = _width;
- actualHeight = _height;
-
- var offset:Rectangle;
-
- var i:int;
- var length:int = bitmaps.length;
- for (i = 0; i < length; i++)
- {
- var sb:ScaleBitmap = bitmaps[i] as ScaleBitmap;
- offset = offsets[sb];
- sb.x = offset.x;
- sb.y = offset.y;
- sb.setSize(_width - offset.x - offset.width, _height - offset.height - offset.y);
- }
-
- length = bitmapDraws.length;
- for (i = 0; i < length; i++)
- {
- var bdCont:Sprite = bitmapDraws[i] as Sprite;
- var bd:BitmapData = bitmapForDraw[bdCont];
- offset = offsets[bdCont];
- var g:Graphics = bdCont.graphics;
- g.clear();
- g.beginBitmapFill(bd);
- g.drawRect(offset.x, offset.y, _width - offset.x - offset.width, _height - offset.height - offset.y);
- g.endFill();
- }
- }
-
- private function alignTopLeft():void
- {
- this.x = 0;
- this.y = 0;
-
- actualWidth = _width;
- actualHeight = _height;
-
- var offset:Rectangle;
-
- var i:int;
- var length:int = bitmaps.length;
- for (i = 0; i < length; i++)
- {
- var sb:ScaleBitmap = bitmaps[i] as ScaleBitmap;
- offset = offsets[sb];
- sb.x = offset.x;
- sb.y = offset.y;
- sb.setSize(_width - offset.x - offset.width, _height - offset.height - offset.y);
- }
-
- length = bitmapDraws.length;
- for (i = 0; i < length; i++)
- {
- var bdCont:Sprite = bitmapDraws[i] as Sprite;
- var bd:BitmapData = bitmapForDraw[bdCont];
- offset = offsets[bdCont];
- var g:Graphics = bdCont.graphics;
- g.clear();
- g.beginBitmapFill(bd);
- g.drawRect(offset.x, offset.y, _width - offset.x - offset.width, _height - offset.height - offset.y);
- g.endFill();
- }
- }
-
- private function alignTopRight():void
- {
- this.x = stage.stageWidth - _width;
- this.y = 0;
-
- actualWidth = _width;
- actualHeight = _height;
-
- var offset:Rectangle;
-
- var i:int;
- var length:int = bitmaps.length;
- for (i = 0; i < length; i++)
- {
- var sb:ScaleBitmap = bitmaps[i] as ScaleBitmap;
- offset = offsets[sb];
- sb.x = offset.x;
- sb.y = offset.y;
- sb.setSize(_width - offset.x - offset.width, _height - offset.height - offset.y);
- }
-
- length = bitmapDraws.length;
- for (i = 0; i < length; i++)
- {
- var bdCont:Sprite = bitmapDraws[i] as Sprite;
- var bd:BitmapData = bitmapForDraw[bdCont];
- offset = offsets[bdCont];
- var g:Graphics = bdCont.graphics;
- g.clear();
- g.beginBitmapFill(bd);
- g.drawRect(offset.x, offset.y, _width - offset.x - offset.width, _height - offset.height - offset.y);
- g.endFill();
- }
- }
-
- //////////////////////////////////////////////////////////////////////////////////////////////////
- // HANDLERS //
- //////////////////////////////////////////////////////////////////////////////////////////////////
-
- private function onAdded(e:Event):void
- {
-
- removeEventListener(Event.ADDED_TO_STAGE, onAdded);
- stage.addEventListener(Event.RESIZE, onResize);
- resize();
- }
-
- private function onResize(e:Event):void
- {
- resize();
- }
-
- //////////////////////////////////////////////////////////////////////////////////////////////////
- // GETTERS/SETTERS //
- //////////////////////////////////////////////////////////////////////////////////////////////////
-
- public function get stateIndex():int { return currentStateIndex; }
-
- public function get align():String { return _align; }
- public function set align(value:String):void { _align = value; }
-
- override public function get width():Number { return actualWidth; }
- override public function get height():Number { return actualHeight; }
-
- private function get bitmapDraws():/*Sprite*/Array { return currentState.bitmapDraws }
- private function get bitmaps():/*ScaleBitmap*/Array { return currentState.bitmaps }
- private function get offsets():Dictionary { return currentState.offsets }
- private function get bitmapForDraw():Dictionary { return currentState.bitmapForDraw }
- }
-
- }
-
- import flash.display.BitmapData;
- import flash.utils.Dictionary;
- import org.bytearray.display.ScaleBitmap;
-
- class SkinState
- {
-
- /**
- * Массив контейнеров для bitmapDraw
- */
- public var bitmapDraws:/*Sprite*/Array;
-
- /**
- * Массив scaleBitmap
- */
- public var bitmaps:/*ScaleBitmap*/Array;
-
- /**
- * Словарь смещений (ключ - контейнер, значение - Rectangle)
- */
- public var offsets:Dictionary;
-
- /**
- * Словарь картинок для отрисовки методом bitmapDraw (ключ - контейнер, значение - BitmapData)
- */
- public var bitmapForDraw:Dictionary;
-
- public var childs:Array;
-
- public var currentChildIndex:int;
-
- public function SkinState()
- {
- childs = [];
- bitmaps = [];
- bitmapDraws = [];
- offsets = new Dictionary();
- bitmapForDraw = new Dictionary();
- }
- }