use new decorator syntax

This commit is contained in:
Andreas Schaafsma 2025-03-11 15:30:36 +01:00
parent 2383b5f026
commit ac5787e2fb

View File

@ -3,6 +3,7 @@ package game.ui.console;
import game.ui.text.TextFormats; import game.ui.text.TextFormats;
import assets.Fonts; import assets.Fonts;
import openfl.Lib; import openfl.Lib;
import engine.ui.UIPane.PaneLayout;
import openfl.events.Event; import openfl.events.Event;
import openfl.text.TextFieldAutoSize; import openfl.text.TextFieldAutoSize;
import openfl.text.TextField; import openfl.text.TextField;
@ -17,377 +18,380 @@ import engine.typedefs.console.CCmd;
import game.ui.console.elements.ConsoleInput; import game.ui.console.elements.ConsoleInput;
import engine.ui.UIPane; import engine.ui.UIPane;
class Console extends Sprite {
public var textFormat:TextFormat;
public var cOut:TextField;
public var cIn:TextField;
public var cInput:ConsoleInput;
public var cAutoComp:TextField;
public static var consoleInstance:Console;
@:convar({
name: "mat_consolebg",
type: "CString",
value: "0x222222",
help: "console background color",
callback: () -> {
if (Console.consoleInstance != null){
Console.consoleInstance.cOut.backgroundColor = Std.parseInt(Console.consoleInstance.cvar_mat_consolebg.value);
Console.consoleInstance.cIn.backgroundColor = Std.parseInt(Console.consoleInstance.cvar_mat_consolebg.value);
}
}
})
public static var scvar_mat_consolebg:CVar;
class Console extends Sprite{
public var textFormat:TextFormat;
public var cOut:TextField;
public var cIn:TextField;
public var cInput:ConsoleInput;
public var cAutoComp:TextField;
public static var consoleInstance:Console;
public var cvar_mat_consolebg:CVar; public var cvar_mat_consolebg:CVar;
public var ccmd_visible:CCmd = ConVar.registerCCmd("toggleconsole",(cArgs:Array<String>)->{
toggle();
});
public function new(){
super();
consoleInstance = this; public var ccmd_visible:CCmd = ConVar.registerCCmd("toggleconsole", (cArgs:Array<String>) -> {
toggle();
});
public function new() {
super();
cvar_mat_consolebg = scvar_mat_consolebg;
consoleInstance = this;
var consolePane = new UIPane("yeeto", {width: 800, height: 600, minWidth: 400, minHeight: 300, maxWidth: 1200, maxHeight: 800});
consolePane.layout = VERTICAL;
consolePane.layout = VERTICAL;
// graphics.beginFill(0x555555);
// graphics.drawRect(0,0,800,600);
cInput = new ConsoleInput();
cIn = cInput.tf;
// cIn.setTextFormat(textFormat);
cInput.y = 600 - cInput.height - 12;
cInput.x = 12;
var consolePane:UIPane = new UIPane("yeeto",{width:800, height:600}); cOut = new TextField();
cAutoComp = new TextField();
this.addChild(consolePane.sprite); drawFields(cOut, cAutoComp);
consolePane.layout = VERTICAL;
//graphics.beginFill(0x555555); cOut.addEventListener(Event.CHANGE, onOutputTextChange);
//graphics.drawRect(0,0,800,600); cIn.addEventListener(Event.CHANGE, onInputTextChange);
this.addChild(cOut);
this.addChild(cInput);
this.addChild(cAutoComp);
ConVar.registerCCmd("echo", (args:Array<String>) -> {
Console.devMsg(args.join(" ").split('"').join(""));
});
ConVar.registerCCmd("quit", (args:Array<String>) -> {
Lib.application.window.close();
});
}
cInput = new ConsoleInput(); public function drawFields(cOut:TextField, cAutoComp:TextField) {
cIn = cInput.tf; cOut.text = "hConsole Initialized\n";
cOut.defaultTextFormat = TextFormats.getFormats().cOutputFmt;
cOut.wordWrap = true;
// cOut.autoSize = TextFieldAutoSize.LEFT;
cOut.multiline = true;
cOut.background = true;
trace(cvar_mat_consolebg);
cOut.backgroundColor = Std.parseInt(cvar_mat_consolebg.value);
cOut.width = 800 - 24;
cOut.height = 600 - cIn.height - 38;
cOut.y = 12;
cOut.x = 12;
//cIn.setTextFormat(textFormat); cAutoComp.text = "";
cInput.y = 600-cInput.height-12; cAutoComp.defaultTextFormat = TextFormats.getFormats().cInputFmt;
cInput.x = 12; cAutoComp.wordWrap = false;
cAutoComp.multiline = true;
cAutoComp.background = true;
cAutoComp.backgroundColor = 0x11111100;
cAutoComp.border = true;
cAutoComp.borderColor = 0x55555500;
cAutoComp.selectable = false;
cAutoComp.width = 400;
cAutoComp.height = 32 * 5;
cAutoComp.visible = false;
cAutoComp.x = 0;
cAutoComp.y = 600;
}
cvar_mat_consolebg = ConVar.registerCVar("mat_consolebg",CString,"0x222222",null,"console background color", ()->{ public function parseCmd(cmd:String, bNoHist:Bool = false) {
cOut.backgroundColor = Std.parseInt(cvar_mat_consolebg.value); if (!bNoHist)
cIn.backgroundColor = Std.parseInt(cvar_mat_consolebg.value); history.push(cmd);
},false,false,0,0,false);
cOut = new TextField(); cmd = cmd.split(";").join(" ; ");
cAutoComp = new TextField(); var subStrings = [];
var startQuoteIndex:Int = cmd.indexOf('"');
var endQuoteIndex:Int;
if (startQuoteIndex != -1) {
while ((startQuoteIndex = cmd.indexOf('"')) > -1) {
// push start of the cmd up until the quotes start
subStrings.push(cmd.substring(0, startQuoteIndex));
// find next quote
endQuoteIndex = cmd.indexOf('"', startQuoteIndex + 1) + 1;
if (endQuoteIndex == 0) {
cmd += '"';
endQuoteIndex = cmd.length;
}
// push quote content
subStrings.push(cmd.substring(startQuoteIndex, endQuoteIndex));
cmd = cmd.substr(endQuoteIndex);
}
}
subStrings.push(cmd);
// Split args
var newSubStrings = [];
for (subString in subStrings) {
if (subString.indexOf('"') == -1) {
// split spaced args
for (str in subString.split(" ")) {
// we want to discard empty strings
if (str != "")
newSubStrings.push(str);
}
} else {
newSubStrings.push(subString);
}
}
var commands = [];
// split off additional commands
while (newSubStrings.length > 0) {
for (i in 0...newSubStrings.length) {
if (newSubStrings[i] == ";") {
commands.push(newSubStrings.splice(0, i));
newSubStrings.shift();
break;
}
if (newSubStrings.length - 1 == i) {
commands.push(newSubStrings);
newSubStrings = [];
}
}
}
trace(commands);
execCommands(commands);
}
drawFields(cOut, cAutoComp); public function execCommands(commands:Array<Array<String>>) {
for (command in commands) {
// Store command name, value and args
var cName:String = command[0];
var cValue:String = command[1];
var cArgs:Array<String> = command.slice(1);
// Check if convar with that name actually exists
if (ConVar.isCVar(cName)) {
// Get reference to convar object
var cv:CVar = ConVar.getCVar(command[0]);
// Check number of params
if (command.length == 1) {
// No params after the cmd, print help string
devMsg(cv.name + " - " + cv.helpString);
devMsg(cv.name + " = " + cv.value);
} else {
// Check for convar type and set value accordingly
switch (cv.type) {
case CInt:
ConVar.setCVar(cName, Std.parseInt(cValue));
trace(cValue);
break;
case CFloat:
ConVar.setCVar(cName, Std.parseFloat(cValue));
break;
case CBool:
cValue = cValue.toLowerCase();
if (cValue == "1" || cValue == "true") {
ConVar.setCVar(cName, true);
} else if (cValue == "0" || cValue == "false") {
ConVar.setCVar(cName, false);
}
break;
case CString:
ConVar.setCVar(cName, cValue);
break;
}
}
}
// convar is actually a command, run it.
else if (ConVar.isCmd(cName)) {
ConVar.runCmd(cName, cArgs);
}
// convar doesn't exist
else {
devMsg("unkown command: " + command[0]);
}
}
}
public function submitInput() {
cOut.appendText(">" + cInput.getText() + "\n");
parseCmd(cInput.getText());
cInput.setText("");
cAutoComp.visible = false;
cOut.scrollV = cOut.maxScrollV;
histSelect = -1;
}
cOut.addEventListener(Event.CHANGE, onOutputTextChange); public function onOutputTextChange(e:Event) {
cIn.addEventListener(Event.CHANGE, onInputTextChange); cOut.scrollV = cOut.maxScrollV;
this.addChild(cOut); }
this.addChild(cInput);
this.addChild(cAutoComp);
ConVar.registerCCmd("echo", (args:Array<String>) -> { Console.devMsg(args.join(" ").split('"').join(""));});
ConVar.registerCCmd("quit", (args:Array<String>) -> { Lib.application.window.close();});
}
public function drawFields(cOut:TextField, cAutoComp:TextField){
cOut.text = "hConsole Initialized\n"; public function onInputTextChange(e:Event) {
cOut.defaultTextFormat = TextFormats.getFormats().cOutputFmt; if (cInput.getText() != "") {
cOut.wordWrap = true; cAutoComp.text = "";
//cOut.autoSize = TextFieldAutoSize.LEFT; for (string in (autocompleteList = getCompList())) {
cOut.multiline = true; cAutoComp.text += string + "\n";
cOut.background = true; trace(string);
cOut.backgroundColor = Std.parseInt(cvar_mat_consolebg.value); }
cOut.width = 800 - 24; cAutoComp.visible = true;
cOut.height = 600 - cIn.height - 38; } else {
cOut.y = 12; cAutoComp.visible = false;
cOut.x = 12; autocompleteList = history;
}
}
cAutoComp.text = ""; public static function toggle():Void {
cAutoComp.defaultTextFormat = TextFormats.getFormats().cInputFmt; consoleInstance.visible = !consoleInstance.visible;
cAutoComp.wordWrap = false; if (!consoleInstance.visible) {
cAutoComp.multiline = true; if (Lib.current.stage.focus == consoleInstance.cIn) {
cAutoComp.background = true; Lib.current.stage.focus = null;
cAutoComp.backgroundColor = 0x11111100; }
cAutoComp.border = true; } else {
cAutoComp.borderColor = 0x55555500; Lib.current.stage.focus = consoleInstance.cIn;
cAutoComp.selectable = false; }
cAutoComp.width = 400; }
cAutoComp.height = 32*5;
cAutoComp.visible = false;
cAutoComp.x = 0;
cAutoComp.y = 600;
}
public static function devMsg(msg:String):Void {
consoleInstance.cOut.appendText(msg + "\n");
consoleInstance.cOut.scrollV = consoleInstance.cOut.maxScrollV;
}
public function parseCmd(cmd:String, bNoHist:Bool = false){ public static var history:Array<String> = [];
if(!bNoHist) public static var histSelect:Int = -1;
history.push(cmd); public static var tmpInput:String = "";
public static var autocompleteList:Array<String> = [];
public static var compSelect:Int = -1;
cmd = cmd.split(";").join(" ; "); public static function getCompList():Array<String> {
var subStrings = []; // Split words
var startQuoteIndex:Int = cmd.indexOf('"'); var inp:Array<String> = consoleInstance.cInput.getText().split(" ");
var endQuoteIndex:Int; var inpStripped:Array<String> = [
if(startQuoteIndex != -1){ for (word in inp) {
while((startQuoteIndex = cmd.indexOf('"')) > -1){ if (word != "" && word != " ") {
//push start of the cmd up until the quotes start word;
subStrings.push(cmd.substring(0,startQuoteIndex)); }
//find next quote }
endQuoteIndex = cmd.indexOf('"',startQuoteIndex+1)+1; ];
if(endQuoteIndex == 0){ // Stop if input is empty
cmd+='"'; if (inp.length == 0)
endQuoteIndex = cmd.length; return [];
} var cVars:Array<String> = ConVar.getCVarNames();
//push quote content var cVarFiltered:Array<String> = [];
subStrings.push(cmd.substring(startQuoteIndex,endQuoteIndex)); // Loop through convars
cmd = cmd.substr(endQuoteIndex); for (cVar in cVars) {
} // if there's one word just check if the convar starts with the input string
} if (inp.length == 1) {
subStrings.push(cmd); // Check if the cvar starts with the input
//Split args if (cVar.indexOf(inp[0]) == 0) {
var newSubStrings = []; cVarFiltered.push(cVar);
for(subString in subStrings){ }
if(subString.indexOf('"') == -1){ }
// split spaced args // User pressed space at least once but entered no other words. Check if first word occurs at any position
for(str in subString.split(" ")){ else if (inpStripped.length == 1 && inp.length > 1) {
//we want to discard empty strings if (cVar.indexOf(inp[0]) > -1) {
if(str != "") cVarFiltered.push(cVar);
newSubStrings.push(str); }
} }
// Multiple words, check for cvars that contain all of them
else if (inpStripped.length > 1) {
var bWordNotPresent:Bool = false;
for (word in inpStripped) {
if (cVar.indexOf(word) == -1) {
bWordNotPresent = true;
}
}
if (!bWordNotPresent)
cVarFiltered.push(cVar);
}
}
return cVarFiltered;
}
} public static function histPrev():Void {
else{ // Only complete if input field is empty or scrolling through hist
newSubStrings.push(subString); if (consoleInstance.cInput.getText() == "" || histSelect != -1) {
} // Store current input in tmpInput
} if (histSelect == -1)
var commands = []; tmpInput = consoleInstance.cInput.getText();
//split off additional commands // Only go through history if history is not empty
while(newSubStrings.length > 0){ if (history.length != 0) {
for(i in 0...newSubStrings.length){ // Check if currently selecting a history entry
if(newSubStrings[i] == ";"){ if (histSelect >= 0) {
commands.push(newSubStrings.splice(0,i)); histSelect--;
newSubStrings.shift(); }
break; // Not currently selecting a history entry
} else {
if(newSubStrings.length-1 == i){ // Put selector to the newest
commands.push(newSubStrings); histSelect = history.length - 1;
newSubStrings = []; }
} if (histSelect != -1) {
} // Put correct history entry in the input field
} consoleInstance.cInput.setText(history[histSelect]);
trace(commands); } else {
execCommands(commands); // Restore tmp input
} consoleInstance.cInput.setText(tmpInput);
public function execCommands(commands:Array<Array<String>>){ }
for(command in commands){ }
// Store command name, value and args // History is empty
var cName:String = command[0]; else {
var cValue:String = command[1]; // Do nothing
var cArgs:Array<String> = command.slice(1); return;
//Check if convar with that name actually exists }
if(ConVar.isCVar(cName)){ }
//Get reference to convar object // Now we need to descend through the autocomplete list
var cv:CVar = ConVar.getCVar(command[0]); else {
//Check number of params if (autocompleteList.length == 0) {
if(command.length == 1){ return;
//No params after the cmd, print help string } else {
devMsg(cv.name+" - "+cv.helpString); // Check if currently selecting an autocomplete entry
devMsg(cv.name+" = "+cv.value); if (compSelect > -1) {}
} // Not currently selecting an autocomplete entry
else{ else {}
//Check for convar type and set value accordingly }
switch(cv.type){ }
case CInt: }
ConVar.setCVar(cName,Std.parseInt(cValue));
trace(cValue);
break;
case CFloat:
ConVar.setCVar(cName,Std.parseFloat(cValue));
break;
case CBool:
cValue = cValue.toLowerCase();
if(cValue == "1" || cValue == "true"){
ConVar.setCVar(cName,true);
}
else if(cValue == "0" || cValue == "false"){
ConVar.setCVar(cName,false);
}
break;
case CString:
ConVar.setCVar(cName,cValue);
break;
}
} public static function histNext():Void {
// Only complete if input field is empty or scrolling through hist
} if (consoleInstance.cIn.text == "" || histSelect != -1) {
//convar is actually a command, run it. // Store current input in tmpInput
else if(ConVar.isCmd(cName)){ if (histSelect == -1)
ConVar.runCmd(cName,cArgs); tmpInput = consoleInstance.cIn.text;
} // Only go through history if history is not empty
//convar doesn't exist if (history.length != 0) {
else{ // Check if currently selecting a history entry
devMsg("unkown command: "+command[0]); if (histSelect < history.length - 1) {
} histSelect++;
} }
} // Otherwise wrap around
public function submitInput(){ else {
cOut.appendText(">"+cInput.getText()+"\n"); // Put selector to no selection
parseCmd(cInput.getText()); histSelect = -1;
cInput.setText(""); }
cAutoComp.visible = false; if (histSelect != -1) {
cOut.scrollV = cOut.maxScrollV; // Put correct history entry in the input field
histSelect = -1; consoleInstance.cIn.text = history[histSelect];
} } else {
public function onOutputTextChange(e:Event){ // Restore tmp input
cOut.scrollV = cOut.maxScrollV; consoleInstance.cIn.text = tmpInput;
} }
public function onInputTextChange(e:Event){ }
if(cInput.getText() != ""){ // History is empty
cAutoComp.text = ""; else {
for(string in (autocompleteList = getCompList())){ // Do nothing
cAutoComp.text += string+"\n"; return;
trace(string); }
} }
cAutoComp.visible = true; }
}
else{
cAutoComp.visible = false;
autocompleteList = history;
}
}
public static function toggle():Void
{
consoleInstance.visible = !consoleInstance.visible;
if(!consoleInstance.visible){
if(Lib.current.stage.focus == consoleInstance.cIn){
Lib.current.stage.focus = null;
}
}
else{
Lib.current.stage.focus = consoleInstance.cIn;
}
}
public static function devMsg(msg:String):Void
{
consoleInstance.cOut.appendText(msg+"\n");
consoleInstance.cOut.scrollV = consoleInstance.cOut.maxScrollV;
}
public static var history:Array<String> = [];
public static var histSelect:Int = -1;
public static var tmpInput:String = "";
public static var autocompleteList:Array<String> = [];
public static var compSelect:Int = -1;
public static function getCompList():Array<String>
{
// Split words
var inp:Array<String> = consoleInstance.cInput.getText().split(" ");
var inpStripped:Array<String> = [
for(word in inp){
if(word != "" && word != " "){
word;
}
}
];
// Stop if input is empty
if(inp.length == 0) return [];
var cVars:Array<String> = ConVar.getCVarNames();
var cVarFiltered:Array<String> = [];
// Loop through convars
for(cVar in cVars){
//if there's one word just check if the convar starts with the input string
if(inp.length == 1)
{
// Check if the cvar starts with the input
if(cVar.indexOf(inp[0]) == 0){
cVarFiltered.push(cVar);
}
}
// User pressed space at least once but entered no other words. Check if first word occurs at any position
else if(inpStripped.length == 1 && inp.length > 1){
if(cVar.indexOf(inp[0]) > -1){
cVarFiltered.push(cVar);
}
}
// Multiple words, check for cvars that contain all of them
else if(inpStripped.length > 1)
{
var bWordNotPresent:Bool = false;
for(word in inpStripped){
if(cVar.indexOf(word) == -1){
bWordNotPresent = true;
}
}
if(!bWordNotPresent)
cVarFiltered.push(cVar);
}
}
return cVarFiltered;
}
public static function histPrev():Void
{
// Only complete if input field is empty or scrolling through hist
if(consoleInstance.cInput.getText() == "" || histSelect != -1){
// Store current input in tmpInput
if(histSelect == -1) tmpInput = consoleInstance.cInput.getText();
// Only go through history if history is not empty
if(history.length != 0){
// Check if currently selecting a history entry
if(histSelect >= 0){
histSelect--;
}
// Not currently selecting a history entry
else{
// Put selector to the newest
histSelect = history.length -1;
}
if(histSelect != -1){
// Put correct history entry in the input field
consoleInstance.cInput.setText(history[histSelect]);
}
else{
// Restore tmp input
consoleInstance.cInput.setText(tmpInput);
}
}
// History is empty
else{
// Do nothing
return;
}
}
// Now we need to descend through the autocomplete list
else{
if(autocompleteList.length == 0){
return;
}
else{
// Check if currently selecting an autocomplete entry
if(compSelect > -1){
}
// Not currently selecting an autocomplete entry
else{
}
}
}
}
public static function histNext():Void
{
// Only complete if input field is empty or scrolling through hist
if(consoleInstance.cIn.text == "" || histSelect != -1){
// Store current input in tmpInput
if(histSelect == -1) tmpInput = consoleInstance.cIn.text;
// Only go through history if history is not empty
if(history.length != 0){
// Check if currently selecting a history entry
if(histSelect < history.length -1){
histSelect++;
}
// Otherwise wrap around
else{
// Put selector to no selection
histSelect = -1;
}
if(histSelect != -1){
// Put correct history entry in the input field
consoleInstance.cIn.text = history[histSelect];
}
else{
// Restore tmp input
consoleInstance.cIn.text = tmpInput;
}
}
// History is empty
else{
// Do nothing
return;
}
}
}
} }