ng release and blank controls
This commit is contained in:
35
source/io/newgrounds/swf/common/BaseAsset.hx
Normal file
35
source/io/newgrounds/swf/common/BaseAsset.hx
Normal file
@@ -0,0 +1,35 @@
|
||||
package io.newgrounds.swf.common;
|
||||
|
||||
import openfl.events.Event;
|
||||
import openfl.display.MovieClip;
|
||||
|
||||
class BaseAsset extends MovieClip {
|
||||
|
||||
var _coreReady:Bool = false;
|
||||
|
||||
public function new() {
|
||||
super();
|
||||
|
||||
setDefaults();
|
||||
|
||||
if (stage != null)
|
||||
onAdded(null);
|
||||
else
|
||||
addEventListener(Event.ADDED_TO_STAGE, onAdded);
|
||||
}
|
||||
|
||||
function setDefaults():Void { }
|
||||
|
||||
function onAdded(e:Event):Void {
|
||||
|
||||
if (NG.core != null)
|
||||
onReady();
|
||||
else
|
||||
NG.onCoreReady.add(onReady);
|
||||
}
|
||||
|
||||
function onReady():Void {
|
||||
|
||||
_coreReady = true;
|
||||
}
|
||||
}
|
||||
151
source/io/newgrounds/swf/common/Button.hx
Normal file
151
source/io/newgrounds/swf/common/Button.hx
Normal file
@@ -0,0 +1,151 @@
|
||||
package io.newgrounds.swf.common;
|
||||
|
||||
import openfl.display.Stage;
|
||||
import openfl.events.Event;
|
||||
import openfl.events.MouseEvent;
|
||||
import openfl.display.MovieClip;
|
||||
|
||||
class Button {
|
||||
|
||||
var _enabled:Bool;
|
||||
public var enabled(get, set):Bool;
|
||||
function get_enabled():Bool { return _enabled; }
|
||||
function set_enabled(value:Bool):Bool {
|
||||
|
||||
if (value != _enabled) {
|
||||
|
||||
_enabled = value;
|
||||
updateEnabled();
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
public var onClick:Void->Void;
|
||||
public var onOver:Void->Void;
|
||||
public var onOut:Void->Void;
|
||||
|
||||
var _target:MovieClip;
|
||||
var _down:Bool;
|
||||
var _over:Bool;
|
||||
var _foundLabels:Array<String>;
|
||||
|
||||
public function new(target:MovieClip, onClick:Void->Void = null, onOver:Void->Void = null, onOut:Void->Void = null) {
|
||||
|
||||
_target = target;
|
||||
this.onClick = onClick;
|
||||
this.onOver = onOver;
|
||||
this.onOut = onOut;
|
||||
|
||||
_foundLabels = new Array<String>();
|
||||
for (label in _target.currentLabels)
|
||||
_foundLabels.push(label.name);
|
||||
|
||||
_target.stop();
|
||||
_target.addEventListener(Event.ADDED_TO_STAGE, onAdded);
|
||||
if (target.stage != null)
|
||||
onAdded(null);
|
||||
|
||||
enabled = true;
|
||||
}
|
||||
|
||||
function onAdded(e:Event):Void {
|
||||
|
||||
var stage = _target.stage;
|
||||
stage.addEventListener(MouseEvent.MOUSE_UP, mouseHandler);
|
||||
_target.addEventListener(MouseEvent.MOUSE_OVER, mouseHandler);
|
||||
_target.addEventListener(MouseEvent.MOUSE_OUT, mouseHandler);
|
||||
_target.addEventListener(MouseEvent.MOUSE_DOWN, mouseHandler);
|
||||
_target.addEventListener(MouseEvent.CLICK, mouseHandler);
|
||||
|
||||
function selfRemoveEvent(e:Event):Void {
|
||||
|
||||
_target.removeEventListener(Event.REMOVED_FROM_STAGE, selfRemoveEvent);
|
||||
onRemove(e, stage);
|
||||
}
|
||||
_target.addEventListener(Event.REMOVED_FROM_STAGE, selfRemoveEvent);
|
||||
}
|
||||
|
||||
function onRemove(e:Event, stage:Stage):Void {
|
||||
|
||||
stage.removeEventListener(MouseEvent.MOUSE_UP, mouseHandler);
|
||||
_target.removeEventListener(MouseEvent.MOUSE_OVER, mouseHandler);
|
||||
_target.removeEventListener(MouseEvent.MOUSE_OUT, mouseHandler);
|
||||
_target.removeEventListener(MouseEvent.MOUSE_DOWN, mouseHandler);
|
||||
_target.removeEventListener(MouseEvent.CLICK, mouseHandler);
|
||||
}
|
||||
|
||||
function mouseHandler(event:MouseEvent):Void {
|
||||
|
||||
switch(event.type) {
|
||||
|
||||
case MouseEvent.MOUSE_OVER:
|
||||
|
||||
_over = true;
|
||||
|
||||
if (onOver != null)
|
||||
onOver();
|
||||
|
||||
case MouseEvent.MOUSE_OUT:
|
||||
|
||||
_over = false;
|
||||
|
||||
if (onOut != null)
|
||||
onOut();
|
||||
|
||||
case MouseEvent.MOUSE_DOWN:
|
||||
|
||||
_down = true;
|
||||
|
||||
case MouseEvent.MOUSE_UP:
|
||||
|
||||
_down = false;
|
||||
|
||||
case MouseEvent.CLICK:
|
||||
|
||||
if (enabled && onClick != null)
|
||||
onClick();
|
||||
}
|
||||
updateState();
|
||||
}
|
||||
|
||||
function updateEnabled():Void {
|
||||
|
||||
updateState();
|
||||
|
||||
_target.useHandCursor = enabled;
|
||||
_target.buttonMode = enabled;
|
||||
}
|
||||
|
||||
function updateState():Void {
|
||||
|
||||
var state = determineState();
|
||||
|
||||
if (_target.currentLabel != state && _foundLabels.indexOf(state) != -1)
|
||||
_target.gotoAndStop(state);
|
||||
}
|
||||
|
||||
function determineState():String {
|
||||
|
||||
if (enabled) {
|
||||
|
||||
if (_over)
|
||||
return _down ? "down" : "over";
|
||||
|
||||
return "up";
|
||||
|
||||
}
|
||||
return "disabled";
|
||||
}
|
||||
|
||||
public function destroy():Void {
|
||||
|
||||
_target.removeEventListener(Event.ADDED_TO_STAGE, onAdded);
|
||||
|
||||
_target = null;
|
||||
onClick = null;
|
||||
onOver = null;
|
||||
onOut = null;
|
||||
_foundLabels = null;
|
||||
}
|
||||
}
|
||||
88
source/io/newgrounds/swf/common/DropDown.hx
Normal file
88
source/io/newgrounds/swf/common/DropDown.hx
Normal file
@@ -0,0 +1,88 @@
|
||||
package io.newgrounds.swf.common;
|
||||
|
||||
|
||||
import haxe.ds.StringMap;
|
||||
|
||||
import openfl.display.MovieClip;
|
||||
import openfl.display.Sprite;
|
||||
import openfl.text.TextField;
|
||||
|
||||
class DropDown {
|
||||
|
||||
public var value(default, set):String;
|
||||
function set_value(v:String):String {
|
||||
|
||||
if (this.value == v)
|
||||
return v;
|
||||
|
||||
this.value = v;
|
||||
_selectedLabel.text = _values.get(v);
|
||||
|
||||
if (_onChange != null)
|
||||
_onChange();
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
var _choiceContainer:Sprite;
|
||||
var _selectedLabel:TextField;
|
||||
var _onChange:Void->Void;
|
||||
var _values:StringMap<String>;
|
||||
var _unusedChoices:Array<MovieClip>;
|
||||
|
||||
public function new(target:MovieClip, label:String = "", onChange:Void->Void = null) {
|
||||
|
||||
_onChange = onChange;
|
||||
|
||||
_selectedLabel = cast cast(target.getChildByName("currentItem"), MovieClip).getChildByName("label");
|
||||
_selectedLabel.text = label;
|
||||
|
||||
_values = new StringMap<String>();
|
||||
|
||||
new Button(cast target.getChildByName("button"), onClickExpand);
|
||||
new Button(cast target.getChildByName("currentItem"), onClickExpand);
|
||||
_choiceContainer = new Sprite();
|
||||
_choiceContainer.visible = false;
|
||||
target.addChild(_choiceContainer);
|
||||
|
||||
_unusedChoices = new Array<MovieClip>();
|
||||
while(true) {
|
||||
|
||||
var item:MovieClip = cast target.getChildByName('item${_unusedChoices.length}');
|
||||
if (item == null)
|
||||
break;
|
||||
|
||||
target.removeChild(item);
|
||||
_unusedChoices.push(item);
|
||||
}
|
||||
}
|
||||
|
||||
public function addItem(name:String, value:String):Void {
|
||||
|
||||
_values.set(value, name);
|
||||
|
||||
if (_unusedChoices.length == 0) {
|
||||
|
||||
NG.core.logError('cannot create another dropBox item max=${_choiceContainer.numChildren}');
|
||||
return;
|
||||
}
|
||||
|
||||
var button = _unusedChoices.shift();
|
||||
cast(button.getChildByName("label"), TextField).text = name;
|
||||
_choiceContainer.addChild(button);
|
||||
|
||||
new Button(button, onChoiceClick.bind(value));
|
||||
}
|
||||
|
||||
function onClickExpand():Void {
|
||||
|
||||
_choiceContainer.visible = !_choiceContainer.visible;
|
||||
}
|
||||
|
||||
function onChoiceClick(name:String):Void {
|
||||
|
||||
value = name;
|
||||
|
||||
_choiceContainer.visible = false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user