Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package arm;
- //Core
- import haxe.Json;
- import zui.Canvas.ElementType;
- import zui.Canvas.TCanvas;
- import zui.Canvas.TElement;
- import armory.trait.internal.CanvasScript;
- import armory.system.Event;
- import iron.data.Data;
- //Ours
- import libs.signal.Signal1;
- typedef TConsoleData = {
- var greeting:String;
- var commands: Array<TCommand>;
- }
- typedef TCommand = {
- var command:String;
- var desc:String;
- var event:String;
- }
- class ConsoleController extends iron.Trait {
- var consoleCanvas = null;
- var canvas:TCanvas = null;
- var commandJson:String = null;
- public var signal = new Signal1<String>();
- @prop
- var commandFile:String = null;
- @prop
- var terminalDir:String = "NB:\\Drone\\Terminal> "; // NOTE: why don't move this to JSON, like greetings?
- public function new() {
- super();
- notifyOnInit(function() {
- if (commandFile != null) {
- Data.getBlob(commandFile, function(b:kha.Blob) {
- commandJson = b.toString();
- });
- }
- consoleCanvas = new CanvasScript("Console");
- canvas = @:privateAccess consoleCanvas.canvas;
- var eventName = "consoleBtnFor" + object.name; // Unique event
- for (el in canvas.elements) {
- if (el.name == "Button") {
- el.event = eventName;
- break;
- }
- }
- Event.add(eventName, consoleBtnPress);
- var consoleDataJson:TConsoleData = Json.parse(commandJson);
- var root = consoleCanvas.getElement("Empty"); // NOTE: maybe have some nice names for elements?
- addLine(canvas, root.x, root.y + numbersOfLines, root.width, root.height, consoleDataJson.greeting);
- });
- }
- var numbersOfLines:Int = 0; // NOTE: it think this something else, like `lineTopMargin`
- public function consoleBtnPress() {
- if (commandJson == null || canvas == null) {
- return;
- }
- var consoleDataJson:TConsoleData = Json.parse(commandJson); // NOTE: this could be done one time, on init
- var commandInput = consoleCanvas.getHandle("Input").text; // NOTE: this could be done one time, on init
- var root = consoleCanvas.getElement("Empty"); // NOTE: this could be done one time, on init
- numbersOfLines += root.height;
- var commandFound = false;
- for (commandItem in consoleDataJson.commands) { // NOTE: we dont need to loop, if command is "-help", pls change order and use else if :)
- if (commandItem.command == commandInput) {
- addLine(canvas, root.x, root.y + numbersOfLines, root.width, root.height, terminalDir+" "+commandInput);
- commandFound = true;
- signal.dispatch(commandItem.event);
- break;
- }
- }
- if (commandInput == "-help"){
- addLine(canvas, root.x, root.y + numbersOfLines, root.width, root.height, terminalDir+" "+commandInput);
- for (commandItem in consoleDataJson.commands){
- numbersOfLines += root.height;
- addLine(canvas, root.x, root.y + numbersOfLines, root.width, root.height, terminalDir+" command: '" + commandItem.command+"'");
- numbersOfLines += root.height;
- addLine(canvas, root.x, root.y + numbersOfLines, root.width, root.height, terminalDir+" command description: " + commandItem.desc); // QUESTION: maybe have this in one line? Doesn't command+desc blend together?
- numbersOfLines += root.height;
- commandFound = true;
- }
- }
- if (!commandFound) {
- addLine(canvas, root.x, root.y + numbersOfLines, root.width, root.height, terminalDir+" "+commandInput);
- numbersOfLines += root.height;
- addLine(canvas, root.x, root.y + numbersOfLines, root.width, root.height, "'"+commandInput+"'"+" is not recognized as an internal or external command");
- numbersOfLines += root.height;
- addLine(canvas, root.x, root.y + numbersOfLines, root.width, root.height, "Stupid boi please enter '-help', to check all possible command"); // NOTE: cmoooon :D
- numbersOfLines += root.height;
- }
- }
- // NOTE: instead of passing root this and that, pass root it self + numbersOfLines, and calc inside fn, this way the function will look much shorter and nicer
- public function addLine(cCanvas:TCanvas, rootx:Float, rooty:Float, rootw:Int, rooth:Int, text:String) {
- var line:TElement = {
- id: 10002,
- type: ElementType.Text,
- name: "Terminal",
- text: text,
- x: rootx,
- y: rooty,
- width: rootw,
- height: rooth,
- color: -1
- }
- cCanvas.elements.push(line);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement