Skip to content

Instantly share code, notes, and snippets.

@AxGord
Last active December 19, 2015 01:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AxGord/5874288 to your computer and use it in GitHub Desktop.
Save AxGord/5874288 to your computer and use it in GitHub Desktop.
Build simple help System for your apps.
package ;
import com.bit101.components.TextArea;
import com.bit101.components.List;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.filters.BlurFilter;
import flash.filters.GlowFilter;
import flash.geom.Rectangle;
import flash.net.URLLoader;
import flash.events.Event;
import flash.net.URLRequest;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.display.BlendMode;
import haxe.xml.Fast;
import flash.Lib;
/**
* Build simple help System for your apps.
* How use: [var ref = new Reference(Reference.border(new Rectangle(0, 0, FLTools.width, FLTools.height), 100), 'reference.xml', true);
* openHelpButton.addEventListener(MouseEvent.CLICK, ref.show);]
* Xml: [<reference title="TITLE"><el title="TITLE 1">HTML Code</el><el title="TITLE 2">HTML Code</el></reference>]
* @author AxGord
*/
class Reference extends Sprite {
static var styleLink = new TextFormat(20, 0x0000FF, false, false, true);
static var styleText = new TextFormat(20, 0x000000);
static var styleTitle = new TextFormat(20, 0x000000, true);
static var styleClose = new TextFormat(20, 0xCC0000, true);
static var titleGlow = new GlowFilter(0xFFFFFF, 1, 10, 10, 2, 3);
private var list:Sprite;
private var content:Sprite;
private var closeField:TextField;
private var titleField:TextField;
private var textArea:TextArea;
private var subTitleField:TextField;
private var li:List;
private var data:Array<String>;
private var rect:Rectangle;
private var rAfterReady:Rectangle;
public function new(rect:Rectangle, file:String, close:Bool = false) {
super();
list = new Sprite();
content = new Sprite();
data = [];
this.rect = rect;
x = rect.x;
y = rect.y;
var l = new URLLoader();
l.addEventListener(Event.COMPLETE, complite);
l.load(new URLRequest(file));
list.x = 0;
list.y = 0;
addChild(list);
content.x = 0;
content.y = 0;
addChild(content);
{
var t = new TextField();
t.selectable = false;
t.defaultTextFormat = styleTitle;
t.text = '←';
content.visible = false;
t.width = rect.width;
t.x = 0;
t.y = 0;
t.autoSize = TextFieldAutoSize.LEFT;
t.background = false;
var o:Sprite = new Sprite();
o.addChild(t);
content.addChild(o);
o.buttonMode = true;
o.mouseChildren = false;
o.addEventListener(MouseEvent.CLICK, back);
}
if (close) {
var t = new TextField();
t.selectable = false;
t.defaultTextFormat = styleClose;
t.text = 'x';
t.width = rect.width;
t.autoSize = TextFieldAutoSize.RIGHT;
t.background = false;
closeField = t;
var o:Sprite = new Sprite();
o.addChild(t);
addChild(o);
o.buttonMode = true;
o.mouseChildren = false;
o.addEventListener(MouseEvent.CLICK, hide);
visible = false;
}
Lib.current.stage.addChild(this);
}
private function back(_) {
list.visible = true;
content.visible = false;
content.removeChildAt(1);
content.removeChildAt(1);
}
public function hide(?_) {
Lib.current.filters = [];
Lib.current.blendMode = BlendMode.NORMAL;
Lib.current.mouseEnabled = true;
Lib.current.mouseChildren = true;
visible = false;
}
public function show(?_) {
var filter = new BlurFilter();
filter.quality = 3;
Lib.current.filters = [filter];
Lib.current.blendMode = BlendMode.DARKEN;
Lib.current.mouseEnabled = false;
Lib.current.mouseChildren = false;
visible = true;
}
private function complite(event:Event):Void {
var l:URLLoader = cast(event.target, URLLoader);
var x:Fast = new Fast(Xml.parse(l.data));
var titles:Array<String> = [];
var t = new TextField();
t.width = rect.width;
t.selectable = false;
t.autoSize = TextFieldAutoSize.CENTER;
t.defaultTextFormat = styleTitle;
t.text = x.node.reference.att.title;
t.background = false;
t.filters = [titleGlow];
list.addChild(t);
var y:Float = t.height;
for (e in x.node.reference.nodes.el) {
titles.push(e.att.title);
data.push(e.innerHTML);
}
li = new List(list, 0, 40, titles);
li.addEventListener(Event.SELECT, sel);
li.width = rect.width;
li.height = rect.height - li.y;
titleField = t;
if (rAfterReady != null) {
setRect(rAfterReady);
rAfterReady = null;
}
}
private function sel(ev:Event):Void {
open(li.getSelectedItem(), data[li.getSelectedIndex()]);
}
private function open(title:String,data:String) {
list.visible = false;
content.visible = true;
{
var t = new TextField();
t.width = rect.width;
t.selectable = false;
t.autoSize = TextFieldAutoSize.CENTER;
t.defaultTextFormat = styleTitle;
t.text = title;
t.filters = [titleGlow];
content.addChild(t);
subTitleField = t;
}
{
var t = new TextArea(content, 0, 40, data);
t.html = true;
t.width = rect.width;
t.height = rect.height - t.y;
textArea = t;
}
}
public function setRect(r:Rectangle):Void {
if (li == null) {
rAfterReady = r;
return;
}
x = r.x;
y = r.y;
if (closeField != null) {
closeField.x = r.width - closeField.width;
}
titleField.x = r.width / 2 - titleField.width / 2;
li.width = r.width;
li.height = r.height - 40;
if (textArea != null) {
textArea.width = r.width;
textArea.height = r.height - 40;
}
rect = r;
if (subTitleField != null) {
subTitleField.x = r.width / 2 - subTitleField.width / 2;
}
}
static public function border(rect:Rectangle, x:Float, ?y:Float):Rectangle {
var r = rect.clone();
if (y == null) y = x;
r.x += x;
r.y += y;
r.width -= x * 2;
r.height -= y * 2;
return r;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment