implemented keybinds

This commit is contained in:
Andreas 2021-04-28 08:29:55 +02:00
parent 7fa7ece574
commit c3ac30208b
2 changed files with 50 additions and 24 deletions

View File

@ -1,5 +1,6 @@
package game;
import openfl.text.TextField;
import engine.enums.CVarFlag;
import engine.typedefs.CCmd;
import game.ui.console.Console;
@ -11,14 +12,10 @@ import engine.enums.CVarType;
import engine.typedefs.CVar;
import game.ui.ConVar;
class Input{
public static var keys:Map<Int,Bool> = [];
public static var keysLast:Map<Int,Bool> = [];
public static var bindMap:Map<String, String> = [];
//public static var bind:CVar = Convar.registerCVar("bind",CVarType.cCmd, null, ()->{
//
//});
public static var bindMap:Map<String, String> = ["~" => "toggleconsole", "\\" => "toggleconsole", "1" => "echo kak", "2" => "+attack"];
public static var cv_debugKeys = ConVar.registerCVar("cl_debuginput", CInt, 0, null, "print debug messages related to input to console", null, false, true, 0, 0, false);
public static var keyNameMap:Map<Int, String> = [
Kb.HOME => "HOME", Kb.END => "END", Kb.INSERT => "INSERT", Kb.DELETE => "DELETE", Kb.PAGE_UP => "PGUP", Kb.PAGE_DOWN => "PGDN",
@ -46,9 +43,14 @@ class Input{
Console.devMsg(Std.string(keycode));
});
public static function onKeyIsDown(e:KeyboardEvent){
if(!Std.isOfType(Lib.current.stage.focus,TextField)){
Console.consoleInstance.parseCmd(bindMap[keyNameMap[e.keyCode]]);
}
if(!keys[e.keyCode]){
if(cv_debugKeys.value > 0)
if(cv_debugKeys.value == 1)
Console.devMsg("triggered key: "+keyNameMap[e.keyCode]);
else if(cv_debugKeys.value > 1 )
Console.devMsg(""+e.keyCode);
}
keys[e.keyCode] = true;
var key:String = keyNameMap[e.keyCode];
@ -56,27 +58,38 @@ class Input{
}
public static function onKeyIsUp(e:KeyboardEvent){
if(bindMap[keyNameMap[e.keyCode]].indexOf("+") == 0){
Console.consoleInstance.parseCmd(StringTools.replace(bindMap[keyNameMap[e.keyCode]],"+","-"));
}
keys[e.keyCode] = false;
}
public static function resolveKeyName(key:Int):String
{
return keyNameMap[key];
}
// public static function bind(input:Dynamic, action:String):Void
// public static function resolveKeyName(key:Int):String
// {
// var key:Int;
// if(Std.is(input,Int)){
// key = input;
// }
// else if(Std.is(input, String)){
// var value:String = input;
// key = keyCodeMap[value.toUpperCase()];
// }
// return keyNameMap[key];
// }
public static function bindKey(input:String, action:String):Void
{
bindMap[input] = action;
}
private static var bind:CCmd = ConVar.registerCCmd("bind", (cArgs:Array<String>)->{
ConVar.runCmd("echo",cArgs);
cArgs[0] = cArgs[0].toUpperCase();
if(cArgs.length == 2){
if(keyCodeMap[cArgs[0]]!= null) {
if(cArgs[1].indexOf('"') == 0 && cArgs[1].lastIndexOf('"') == cArgs[1].length-1){
bindKey(cArgs[0], cArgs[1].substring(1,cArgs[1].length-1));
}
else{
bindKey(cArgs[0], cArgs[1]);
}
}
}
else if(cArgs.length == 1){
Console.devMsg(bindMap[cArgs[0]]);
}
else{
Console.devMsg("usage: bind <key> <command>");
}
});
public static function onEnterFrame()
{

View File

@ -26,6 +26,9 @@ class Console extends Sprite{
public var cAutoComp:TextField;
public static var consoleInstance:Console;
public var cvar_mat_consolebg:CVar;
public var cCmd_visible:CCmd = ConVar.registerCCmd("toggleconsole",(cArgs:Array<String>)->{
toggle();
});
public function new(){
super();
@ -88,8 +91,10 @@ class Console extends Sprite{
ConVar.registerCCmd("echo", (args:Array<String>) -> { Console.devMsg(args.join(" ").split('"').join(""));});
ConVar.registerCCmd("quit", (args:Array<String>) -> { Lib.application.window.close();});
}
public function parseCmd(cmd:String){
history.push(cmd);
public function parseCmd(cmd:String, bNoHist:Bool = false){
if(!bNoHist)
history.push(cmd);
cmd = cmd.split(";").join(" ; ");
var subStrings = [];
var startQuoteIndex:Int = cmd.indexOf('"');
@ -218,6 +223,14 @@ class Console extends Sprite{
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
{