156 lines
5.4 KiB
Haxe
156 lines
5.4 KiB
Haxe
package game.ui;
|
|
|
|
import haxe.Constraints.Function;
|
|
import engine.typedefs.CVar;
|
|
import engine.typedefs.CCmd;
|
|
import engine.enums.CVarType;
|
|
import engine.enums.CVarFlag;
|
|
import engine.enums.CCmdType;
|
|
|
|
|
|
class ConVar{
|
|
static var CVarMap:Map<String, CVar> = [];
|
|
static var CCmdMap:Map<String, CCmd> = [];
|
|
/**
|
|
* Registers a new CVar
|
|
* @param _name The name of the CVar. This is the way it is called in the console or referred to in code.
|
|
* @param _type CVar type as defined in the CVarType enum.
|
|
* @param _value The default value for this CVar. This needs to be in line with its CVarType.
|
|
* @param _flags CVar flags as defined in the CVarFlags enum.
|
|
* @param _helpString Help string that gets printed out in console when no value is specified.
|
|
* @param _callback The function that gets called when the CVar is set. Calls empty Void when left undefined.
|
|
* @param _callOnCreate Whether the callback function gets called or not after registering.
|
|
* @param _bMin Specifies if the CVar has a minimum numeric value.
|
|
* @param _fMin Specifies the minimum numeric value.
|
|
* @param _fMax Specifies the maximum numeric value.
|
|
* @param _bMax Specifies if the CVar has a maximum numeric value.
|
|
*/
|
|
public static inline function registerCVar(_name:String, _type:CVarType, _value:Dynamic, ?_flags:CVarFlag, ?_helpString:String = "", ?_callback:Void->Void, ?_callOnCreate:Bool=false, _bMin:Bool=false, _fMin:Float=0, _fMax:Float=0, _bMax:Bool = false):CVar
|
|
{
|
|
if(CVarMap[_name]!=null || CCmdMap[_name]!=null){
|
|
Console.devMsg("Tried setting already defined convar: " + _name + ", returning null instead");
|
|
return null;
|
|
}
|
|
if(_bMin && _value < _fMin) _value = _fMin;
|
|
if(_bMax && _value > _fMax) _value = _fMax;
|
|
var cvar:CVar = CVarMap[_name] = {
|
|
name : _name,
|
|
type : _type,
|
|
value : _value,
|
|
flags: _flags,
|
|
helpString: _helpString,
|
|
bMin : _bMin,
|
|
fMin : _fMin,
|
|
fMax : _fMax,
|
|
bMax : _bMax,
|
|
callback : _callback == null ? ()->{} : _callback
|
|
}
|
|
if(_callback != null && _callOnCreate){
|
|
_callback();
|
|
}
|
|
return cvar;
|
|
}
|
|
public static inline function registerCCmd(_name:String, ?_callback:Array<String>->Void):CCmd
|
|
{
|
|
if(CVarMap[_name]!=null || CCmdMap[_name]!=null){
|
|
Console.devMsg("Tried setting already defined command: " + _name + ", returning null instead");
|
|
return null;
|
|
}
|
|
var cmd:CCmd = CCmdMap[_name] = {
|
|
name : _name,
|
|
callback : _callback
|
|
}
|
|
return cmd;
|
|
}
|
|
public static inline function setCVar(_name:String, _value:Dynamic):Void
|
|
{
|
|
var cv = getCVar(_name);
|
|
if(cv != null){
|
|
switch(cv.type){
|
|
case CInt,CFloat:
|
|
if(cv.bMax && _value > cv.fMax) _value = cv.fMax;
|
|
if(cv.bMin && _value < cv.fMin) _value = cv.fMin;
|
|
case CBool:
|
|
if(Std.is(_value, String)){
|
|
var v:String = _value;
|
|
_value = v.toLowerCase();
|
|
if(_value == "true" || _value == "1"){
|
|
_value = true;
|
|
}
|
|
else{
|
|
_value = false;
|
|
}
|
|
}
|
|
else if(Std.is(_value, Int) || Std.is(_value, Float)){
|
|
if(_value %2 == 0){
|
|
_value = false;
|
|
}
|
|
else{
|
|
_value = true;
|
|
}
|
|
}
|
|
else if(Std.is(_value, Bool)){
|
|
//do nothing
|
|
}
|
|
else{
|
|
_value = cv.value;
|
|
}
|
|
case CString:
|
|
_value = Std.string(_value);
|
|
|
|
}
|
|
cv.value = _value;
|
|
|
|
}
|
|
else{
|
|
Console.devMsg("trying to set null convar '"+_name+"'");
|
|
}
|
|
|
|
}
|
|
public static inline function isCVar(_name:String){
|
|
return (CVarMap[_name] != null);
|
|
|
|
}
|
|
public static inline function isCmd(_name:String){
|
|
return (CCmdMap[_name] != null);
|
|
}
|
|
public static inline function runCmd(_name:String, _args:Array<String>){
|
|
if(CCmdMap[_name] != null){
|
|
CCmdMap[_name].callback(_args);
|
|
}
|
|
}
|
|
public static var cmdList:CCmd = ConVar.registerCCmd("list", (cArgs:Array<String>)->{
|
|
var keys:Array<String> = [
|
|
for(iterator in [CCmdMap.keys(),CVarMap.keys()]){
|
|
for(key in iterator){
|
|
key;
|
|
}
|
|
}
|
|
];
|
|
keys.sort(function(a:String, b:String):Int {
|
|
a = a.toUpperCase();
|
|
b = b.toUpperCase();
|
|
if (a < b) {
|
|
return -1;
|
|
}
|
|
else if (a > b) {
|
|
return 1;
|
|
} else {
|
|
return 0;
|
|
}
|
|
});
|
|
for(key in keys){
|
|
if(CVarMap[key] != null){
|
|
Console.devMsg(key+" "+CVarMap[key].value);
|
|
}
|
|
else{
|
|
Console.devMsg(key);
|
|
}
|
|
}
|
|
});
|
|
public static inline function getCVar(_name:String):CVar
|
|
{
|
|
return CVarMap[_name];
|
|
}
|
|
|
|
} |