| ZackMercury |
22.05.2014 11:47 |
Цитата:
Лучше сюда выкладывать в теги as3
|
Окей.
S.as
Код AS3:
package com.general
{
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.text.Font;
import flash.text.TextField;
import flash.display.DisplayObject;
public class S
{
public static const RED:uint = 0xd04242;
public static const GREEN:uint = 0x4ade51;
public static const DARK_GREEN:uint = 0x68ab51;
public static const BLACK:uint = 0x000000;
public static const ORANGE:uint = 0xeba326;
public static const DARK_ORANGE:uint = 0xdc8620;
public static const YELLOW:uint = 0xebe426;
public static const LIGHT_BLUE:uint = 0xa9fff8;
public static const BLUE:uint = 0x4ac5ff;
public static const DARK_BLUE:uint = 0x6a93de;
public static const SIREN_BLUE:uint = 0xba7ffe;
public static const VIOLET:uint = 0x7d27ff;
public static const WHITE:uint = 0xf0fae3;
public static const CONSOLAS:int = 0;
public static const FINAL_FRONTIER:int = 1;
public static function getTextFormat(fontName:int):TextFormat
{
var font:Font;
switch (fontName)
{
case CONSOLAS:
font = new Consolas_font();
break;
case FINAL_FRONTIER:
font = new FinalFrontier_font();
break;
}
var tf:TextFormat = new TextFormat();
tf.font = font.fontName;
tf.size = 20;
tf.align = TextFormatAlign.RIGHT;
return tf;
}
public static function getRedText(font:int):TextFormat
{
var tf:TextFormat = getTextFormat(font);
tf.color = RED;
return tf;
}
public static function getWhiteText(font:int):TextFormat
{
var tf:TextFormat = getTextFormat(font);
tf.color = WHITE;
return tf;
}
public static function getText(color:uint, font:int, align:String, size:Number):TextFormat
{
var tf:TextFormat = getTextFormat(font);
tf.color = color;
tf.align = align;
tf.size = size;
return tf;
}
public static function appendPadding(tf:DisplayObject, width:int, height:int, padding:int):void
{
tf.height = height - padding * 2;
tf.width = width - padding * 2;
tf.x += padding;
tf.y += padding;
}
}
}
LiteButton.as
Код AS3:
package com.general
{
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFormatAlign;
import flash.events.MouseEvent;
import flash.geom.ColorTransform;
public class LiteButton extends Sprite
{
private const BUTTON_COLOR:uint = 0x384434;
private const BUTTON_BORDER_COLOR:uint = 0x172113;
private const TEXT_COLOR:uint = 0x9ca987;
private const ORIGINAL_CT:ColorTransform = new ColorTransform(1,1,1);
private const OVER_CT:ColorTransform = new ColorTransform(1.1, 1.1, 1.1);
private const HOLD_CT:ColorTransform = new ColorTransform(0.9, 0.9, 0.9);
private var _tf:TextField;
private var _onClick:Function;
private var _over:Boolean;
public function LiteButton(caption:String = "", width:int = 75, height:int = 25, onClick:Function = null)
{
trace("... LiteButton initialized.");
_onClick = onClick;
drawBackground(width, height);
drawTextField(caption);
super.buttonMode = true;
addListeners();
}
public function setCaption(caption:String):void
{
_tf.text = caption;
}
private function addListeners():void
{
super.addEventListener(MouseEvent.MOUSE_DOWN, darkerColor);
super.addEventListener(MouseEvent.MOUSE_UP, returnColor);
super.addEventListener(MouseEvent.ROLL_OVER, brighterColor);
super.addEventListener(MouseEvent.ROLL_OUT, returnColorFromRoll);
super.addEventListener(MouseEvent.CLICK, handleClick);
}
private function handleClick(e:MouseEvent):void
{
_onClick();
}
private function darkerColor(e:MouseEvent):void
{
super.transform.colorTransform = HOLD_CT;
}
private function brighterColor(e:MouseEvent):void
{
_over = true;
super.transform.colorTransform = OVER_CT;
}
private function returnColor(e:MouseEvent):void
{
if(_over) super.transform.colorTransform = OVER_CT;
else super.transform.colorTransform = ORIGINAL_CT;
}
private function returnColorFromRoll(e:MouseEvent):void
{
_over = false;
super.transform.colorTransform = ORIGINAL_CT;
}
private function drawBackground(w:int, h:int):void
{
graphics.lineStyle(1, BUTTON_BORDER_COLOR, 0.7);
graphics.beginFill(BUTTON_COLOR, 0.6);
graphics.drawRect(0, 0, w, h);
graphics.endFill();
}
private function drawTextField(text:String):void
{
_tf = new TextField();
_tf.defaultTextFormat = S.getText(TEXT_COLOR, S.CONSOLAS, TextFormatAlign.CENTER, 12);
_tf.mouseEnabled = false;
_tf.text = text;
_tf.width = super.width;
_tf.height = super.height;
addChild(_tf);
}
}
}
ScrollBar.as
Код AS3:
package com.general
{
import flash.display.Sprite;
import flash.text.TextField;
import flash.events.MouseEvent;
import flash.events.Event;
import com.engine.core.App;
public class ScrollBar extends Sprite
{
private const MIN_DRAG_HEIGHT:int = 9;
private var _tf:TextField;
private var _drag:Sprite;
private var _width:int;
private var _height:int;
public function ScrollBar(tf:TextField, width:int, height:int)
{
trace("... ScrollBar initialized.");
super();
_tf = tf;
_width = width;
_height = height;
_drag = new Sprite();
drawBackground();
drawDrag();
updateDrag();
_drag.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
_tf.addEventListener(Event.SCROLL, updateDrag);
}
private function drawDrag():void
{
_drag.graphics.lineStyle(1, 0x435249, 0.7);
_drag.graphics.beginFill(0xb9e6ca, 0.6);
_drag.graphics.drawRect(0, 0, _width, _height);
_drag.graphics.endFill();
}
private function startDragging(e:MouseEvent):void
{
_drag.removeEventListener(MouseEvent.MOUSE_DOWN, startDragging);
_tf.removeEventListener(Event.SCROLL, updateDrag);
stage.addEventListener(MouseEvent.MOUSE_MOVE, updateDragging);
stage.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
}
private function updateDragging(e:MouseEvent):void
{
if(mouseY > _drag.height / 2 && mouseY < _height - _drag.height / 2)
_drag.y = mouseY - _drag.height / 2;
else
{
if(mouseY <= _drag.height / 2)
_drag.y = 0;
else
_drag.y = _height - _drag.height;
}
_tf.scrollV = Math.round(_tf.maxScrollV * ((_drag.y) / (_height - _drag.height)));
}
private function stopDragging(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, updateDragging);
stage.removeEventListener(MouseEvent.MOUSE_UP, stopDragging);
_drag.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
_tf.addEventListener(Event.SCROLL, updateDrag);
}
private function drawBackground():void
{
super.graphics.lineStyle(1, 0x0d3d1f, 0.7);
super.graphics.beginFill(0x749380, 0.6);
super.graphics.drawRect(0, 0, _width, _height);
super.graphics.endFill();
super.cacheAsBitmap = true;
}
public function updateDrag(e:Event = null):void
{
_drag.scaleY = 1 - _tf.maxScrollV / _tf.height;
if(_drag.scaleY < 0.05)
{
_drag.scaleY = 1;
_drag.height = MIN_DRAG_HEIGHT;
}
_drag.cacheAsBitmap = true;
_drag.y = _tf.scrollV / _tf.maxScrollV * (_height - _drag.height);
addChild(_drag);
}
}
}
Console.as
Код AS3:
package com.general
{
import flash.display.Sprite;
import com.engine.core.App;
import flash.text.TextField;
import flash.text.TextFormatAlign;
import flash.text.TextFieldType;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;
public class Console extends Sprite
{
private const CONSOLE_VERSION:String = "Developed by SuriTheAngel. Version: 0.3";
private const CONSOLE_HEIGHT:int = 200;
private const PADDING:int = 5;
private const SCROLLBAR_WIDTH:int = 10;
private const FONT_SIZE:int = 10;
private const MAX_MESSAGES:int = 250;
private const PROMPT_LINE_HEIGHT:int = 20;
private const CLEAR_BTN_OFFSET:int = 5;
private var _messages:Vector.<String>;
private var _background:Sprite;
private var _scrollBar:ScrollBar;
private var _logField:TextField;
private var _paused:Boolean;
private var _pauseBtn:LiteButton;
private var _clearBtn:LiteButton;
private var _promptField:TextField;
public function Console()
{
_messages = new Vector.<String>();
_messages.push(CONSOLE_VERSION);
//Рисуем задний фон.
_background = new Sprite();
_background.graphics.lineStyle(1, 0x0d3d1f, 0.7);
_background.graphics.beginFill(0x415046, 0.6);
_background.graphics.drawRect(0, 0, App.SCR_W - 1, CONSOLE_HEIGHT - 1);
_background.graphics.endFill();
//Само окошко консоли, куда будут выводиться сообщения лога.
_logField = new TextField();
_logField.defaultTextFormat = S.getText(0xFFFFFF, S.CONSOLAS, TextFormatAlign.LEFT, FONT_SIZE);
S.appendPadding(_logField, App.SCR_W - SCROLLBAR_WIDTH, CONSOLE_HEIGHT - PROMPT_LINE_HEIGHT, PADDING);
//Скроллбар
_scrollBar = new ScrollBar(_logField, SCROLLBAR_WIDTH, _logField.height - PADDING * 2);
_scrollBar.x = App.SCR_W - 1 - SCROLLBAR_WIDTH;
_scrollBar.y += PADDING;
//Нижняя панель консоли (user_prompt)
_clearBtn = new LiteButton("Clear", 90, PROMPT_LINE_HEIGHT, clear);
_clearBtn.x = App.SCR_W - _clearBtn.width - CLEAR_BTN_OFFSET;
_clearBtn.y = CONSOLE_HEIGHT - _clearBtn.height - PADDING;
_pauseBtn = new LiteButton("Pause", 90, PROMPT_LINE_HEIGHT, playPause);
_pauseBtn.x = _clearBtn.x - _pauseBtn.width - PADDING;
_pauseBtn.y = _clearBtn.y;
_promptField = new TextField();
_promptField.defaultTextFormat = S.getText(S.WHITE, S.CONSOLAS, TextFormatAlign.LEFT, 12);
_promptField.background = true;
_promptField.backgroundColor = 0x364a2f;
_promptField.borderColor = 0x23331d;
_promptField.border = true;
_promptField.width = _pauseBtn.x - PADDING * 2;
_promptField.height = PROMPT_LINE_HEIGHT;
_promptField.x = PADDING;
_promptField.y = _pauseBtn.y;
_promptField.type = TextFieldType.INPUT;
_promptField.addEventListener(KeyboardEvent.KEY_DOWN, type);
//Добавляем элементы на сцену
//Верхняя часть консоли
super.addChild(_background);
super.addChild(_logField);
super.addChild(_scrollBar);
//Нижняя часть консоли
super.addChild(_clearBtn);
super.addChild(_pauseBtn);
super.addChild(_promptField);
super.addEventListener(Event.ADDED_TO_STAGE, focus);
super.addEventListener(Event.REMOVED_FROM_STAGE, unfocus);
}
private function focus(e:Event):void
{
stage.focus = _promptField;
}
private function unfocus(e:Event):void
{
App.pStage.focus = App.pStage;
_promptField.text = "";
}
private function type(e:KeyboardEvent):void
{
if(e.keyCode == Keyboard.ENTER)
{
prompt(_promptField.text);
_promptField.text = "";
}
}
public function log(message:String, color:uint = 0xFFFFFF):void
{
_messages.push("<font color=\"#"+color.toString(16).replace("0x", "") + "\">" + "<b>system: </b>" + message + "</font>");
if(!_paused) update();
}
public function prompt(message:String):void
{
_messages.push("<font color=\"#" + S.LIGHT_BLUE.toString(16).replace("0x", "") + "\">" + "<b>user_prompt: </b>" + message + "</font>");
CommandPrompt.applyCommand(message);
update();
}
public function clear():void
{
_messages = null;
_messages = new Vector.<String>();
update();
}
private function playPause():void
{
if(!_paused)
{
_paused = true;
_pauseBtn.setCaption("Resume");
}
else
{
_paused = false;
_pauseBtn.setCaption("Pause");
update();
}
}
private function update():void
{
if(_messages.length >= MAX_MESSAGES)
{
_messages.splice(0, 50);
}
var str:String = "";
for(var i:int = 0; i < _messages.length; i ++)
{
str += _messages[i];
if(i < _messages.length - 1) str += "\n";
}
_logField.htmlText = str;
_logField.scrollV = _logField.maxScrollV;
_scrollBar.updateDrag();
}
}
}
CommandPrompt.as
Код AS3:
package com.general
{
import com.adobe.utils.StringUtil;
import flash.display.StageDisplayState;
import com.engine.core.App;
import flash.display.StageScaleMode;
public class CommandPrompt
{
public static function applyCommand(command:String):void
{
command = command.toLowerCase();
command = StringUtil.trim(command);
if(command.indexOf("/") < 0)
{
App.console.log("Console doesn''t understand you.", S.RED);
return;
}
var operator;
if(command.indexOf(" ") > -1)
operator = command.substring(0, command.indexOf(" ")).replace("/", "");
else
operator = command.replace("/", "");
var property:String = StringUtil.trim(command.substr(command.indexOf(operator) + operator.length));
switch(operator)
{
case "set":
set(property);
break;
default:
App.console.log("Unknown command " + operator + ".", S.RED);
break;
}
}
private static function set(whatToSet:String):void
{
switch(whatToSet)
{
case "fullscreen":
App.pStage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
break;
case "screen-scale-true":
App.pStage.scaleMode = StageScaleMode.EXACT_FIT;
break;
case "screen-scale-false":
App.pStage.scaleMode = StageScaleMode.SHOW_ALL;
break;
default:
App.console.log("Unknown property " + whatToSet + ".", S.RED);
break;
}
}
}
}
|