Advertisement
Guest User

Untitled

a guest
Feb 11th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 4.23 KB | None | 0 0
  1. package arm;
  2.  
  3. //Core
  4. import haxe.Json;
  5. import zui.Canvas.ElementType;
  6. import zui.Canvas.TCanvas;
  7. import zui.Canvas.TElement;
  8. import armory.trait.internal.CanvasScript;
  9. import armory.system.Event;
  10. import iron.data.Data;
  11. //Ours
  12. import libs.signal.Signal1;
  13.  
  14. typedef TConsoleData = {
  15.     var greeting:String;
  16.     var commands: Array<TCommand>;
  17. }
  18. typedef TCommand = {
  19.     var command:String;
  20.     var desc:String;
  21.     var event:String;
  22. }
  23.  
  24. class ConsoleController extends iron.Trait {
  25.     var consoleCanvas = null;
  26.     var canvas:TCanvas = null;
  27.     var commandJson:String = null;
  28.  
  29.     public var signal = new Signal1<String>();
  30.  
  31.     @prop
  32.     var commandFile:String = null;
  33.  
  34.     @prop
  35.     var terminalDir:String = "NB:\\Drone\\Terminal> "; // NOTE: why don't move this to JSON, like greetings?
  36.  
  37.     public function new() {
  38.         super();
  39.  
  40.         notifyOnInit(function() {
  41.             if (commandFile != null) {
  42.                 Data.getBlob(commandFile, function(b:kha.Blob) {
  43.                     commandJson = b.toString();
  44.                 });
  45.             }
  46.             consoleCanvas = new CanvasScript("Console");
  47.             canvas = @:privateAccess consoleCanvas.canvas;
  48.  
  49.             var eventName = "consoleBtnFor" + object.name; // Unique event
  50.  
  51.             for (el in canvas.elements) {
  52.                 if (el.name == "Button") {
  53.                     el.event = eventName;
  54.                     break;
  55.                 }
  56.             }
  57.  
  58.             Event.add(eventName, consoleBtnPress);
  59.             var consoleDataJson:TConsoleData = Json.parse(commandJson);
  60.             var root = consoleCanvas.getElement("Empty"); // NOTE: maybe have some nice names for elements?
  61.             addLine(canvas, root.x, root.y + numbersOfLines, root.width, root.height, consoleDataJson.greeting);
  62.         });
  63.     }
  64.  
  65.     var numbersOfLines:Int = 0; // NOTE: it think this something else, like `lineTopMargin`
  66.  
  67.     public function consoleBtnPress() {
  68.         if (commandJson == null || canvas == null) {
  69.             return;
  70.         }
  71.  
  72.         var consoleDataJson:TConsoleData = Json.parse(commandJson); // NOTE: this could be done one time, on init
  73.  
  74.         var commandInput = consoleCanvas.getHandle("Input").text; // NOTE: this could be done one time, on init
  75.         var root = consoleCanvas.getElement("Empty"); // NOTE: this could be done one time, on init
  76.  
  77.         numbersOfLines += root.height;
  78.  
  79.         var commandFound = false;
  80.  
  81.         for (commandItem in consoleDataJson.commands) { // NOTE: we dont need to loop, if command is "-help", pls change order and use else if :)
  82.             if (commandItem.command == commandInput) {
  83.                 addLine(canvas, root.x, root.y + numbersOfLines, root.width, root.height, terminalDir+" "+commandInput);
  84.                 commandFound = true;
  85.                 signal.dispatch(commandItem.event);
  86.                 break;
  87.             }
  88.         }
  89.         if (commandInput == "-help"){
  90.             addLine(canvas, root.x, root.y + numbersOfLines, root.width, root.height, terminalDir+" "+commandInput);
  91.             for (commandItem in consoleDataJson.commands){
  92.                 numbersOfLines += root.height;
  93.                 addLine(canvas, root.x, root.y + numbersOfLines, root.width, root.height, terminalDir+" command: '" + commandItem.command+"'");
  94.                 numbersOfLines += root.height;
  95.                 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?
  96.                 numbersOfLines += root.height;
  97.                 commandFound = true;
  98.             }
  99.         }
  100.  
  101.         if (!commandFound) {
  102.             addLine(canvas, root.x, root.y + numbersOfLines, root.width, root.height, terminalDir+" "+commandInput);
  103.             numbersOfLines += root.height;
  104.             addLine(canvas, root.x, root.y + numbersOfLines, root.width, root.height, "'"+commandInput+"'"+" is not recognized as an internal or external command");
  105.             numbersOfLines += root.height;
  106.             addLine(canvas, root.x, root.y + numbersOfLines, root.width, root.height, "Stupid boi please enter '-help', to check all possible command"); // NOTE: cmoooon :D
  107.             numbersOfLines += root.height;
  108.         }
  109.     }
  110.  
  111.     // 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
  112.     public function addLine(cCanvas:TCanvas, rootx:Float, rooty:Float, rootw:Int, rooth:Int, text:String) {
  113.         var line:TElement = {
  114.             id: 10002,
  115.             type: ElementType.Text,
  116.             name: "Terminal",
  117.             text: text,
  118.             x: rootx,
  119.             y: rooty,
  120.             width: rootw,
  121.             height: rooth,
  122.             color: -1
  123.         }
  124.         cCanvas.elements.push(line);
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement