Skip to content

Instantly share code, notes, and snippets.

Created July 8, 2016 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/ce6f48186f8e9e485dffa9db167e5b3d to your computer and use it in GitHub Desktop.
Save anonymous/ce6f48186f8e9e485dffa9db167e5b3d to your computer and use it in GitHub Desktop.
#ifdef __CLIENT
/*
TODO : UseItem* should parse pids from binding params. >DONE<
Test that binding doesn't override an old one.
Add the rest of the keys to keymap
*/
#include "client_keybinding_h.fos"
#define ARG1 (0)
#define ARG2 (1)
#define ARG3 (2)
#define ARG4 (3)
#define ARG5 (4)
dictionary bindings;
dictionary keymap;
const string@[] AllowedKeys = {
"FogOfWar",
"ReloadWeapon",
"AimHead",
"AimEyes",
"AimTorso",
"AimRightArm",
"AimLeftArm",
"AimRightLeg",
"AimLeftLeg",
"AimGroin",
"UseItem1",
"UseItem2",
"UseItem3",
"UseItem4",
"UseItem5",
"UseItem6",
"UseItem7",
"UseItem8",
"UseItem9"
};
class bindkey {
uint8 key;
bool ctrl;
bool alt;
bool shift;
uint16 proto;
void init () {
this.ctrl = false;
this.alt = false;
this.shift = false;
this.key = 0;
this.proto = 0;
}
void run (uint bindproto)
{
switch(bindproto) {
case _FogOfWar:
break;
case _ReloadWeapon:
break;
case _AimHead:
break;
case _AimEyes:
break;
case _AimTorso:
break;
case _AimRightArm:
break;
case _AimLeftArm:
break;
case _AimRightLeg:
break;
case _AimLeftLeg:
break;
case _AimGroin:
break;
case _UseItem1:
Log("UseItem1 has been called.");
break;
case _UseItem2:
break;
case _UseItem3:
break;
case _UseItem4:
break;
case _UseItem5:
break;
case _UseItem6:
break;
case _UseItem7:
break;
case _UseItem8:
break;
case _UseItem9:
break;
default:
Log("Wrong bindproto used.");
break;
}
}
}
void InitKeyBinding() // export
{
if( ReadKeybindingConfig() )
Message("keybinding.cfg has been successfuly loaded.");
else
Message("Your binding is fugging broken my friend. :)");
return;
}
void InitKeyMap()
{
keymap.set("A", DIK_A);
keymap.set("B", DIK_B);
keymap.set("C", DIK_C);
keymap.set("D", DIK_D);
keymap.set("E", DIK_E);
keymap.set("F", DIK_F);
keymap.set("G", DIK_G);
keymap.set("H", DIK_H);
keymap.set("I", DIK_I);
keymap.set("J", DIK_J);
keymap.set("K", DIK_K);
keymap.set("L", DIK_L);
keymap.set("M", DIK_M);
keymap.set("N", DIK_N);
keymap.set("O", DIK_O);
keymap.set("P", DIK_P);
keymap.set("Q", DIK_Q);
keymap.set("R", DIK_R);
keymap.set("S", DIK_S);
keymap.set("T", DIK_T);
keymap.set("U", DIK_U);
keymap.set("V", DIK_V);
keymap.set("W", DIK_W);
keymap.set("X", DIK_X);
keymap.set("Y", DIK_Y);
keymap.set("Z", DIK_Z);
//keymap.set("&", DIK_1);
//keymap.set("Shift", DIK_LSHIFT);
//keymap.set("Ctrl", DIK_LCONTROL);
//keymap.set("Alt", DIK_LMENU);
}
void InitDefaultBinding()
{
// name, ctrl, alt, shift, key
SetBinding( "FogOfWar", false, true , false , DIK_F);
SetBinding( "ReloadWeapon", false, false , false , DIK_R);
}
bool StrToInt(string@ s, uint16& inout val)
{
int temp = 0;
if(!StrToInt(s, temp))
return false;
val = temp;
return true;
}
string@ NameArg( uint argn )
{
string argstr;
switch (argn) {
case ARG1:
argstr = "First";
break;
case ARG2:
argstr = "Second";
break;
case ARG3:
argstr = "Third";
break;
case ARG4:
argstr = "Fourth";
break;
case ARG5:
argstr = "Last";
break;
default:
break;
}
return argstr;
}
#define isLast #(i, params) ( i == (params.length()-1) )
// Format:
// Mod=Ctrl Alt or Shift
//
// (UseItem*) AllowedKeys=Mod Mod Key Proto
// (UseItem*) AllowedKeys=Mod Key Proto
// (UseItem*) AllowedKeys=Key Proto
//
// AllowedKeys=Mod Mod Key
// AllowedKeys=Mod Key
// AllowedKeys=Key
//
// 1 2 3 4 5 6 7 8 9 0 outside of numpad should be used in combination with modifiers
// to avoid trigger hardcoded skills
// A, used to show aim from weapon.
// M, used to switch cursor state. (hexes, arrow)
// COMMA, SEMICOLON, used to turn character direction
// :, game time
// P, pipboy screen
// O, option/escape screen
// I, inventory screen
// Z zoom
// Q,W linetracer
// S skill screen
// F FIx screen
// G, to grab item from ground
// N, switch weapon's mode
// B, change used hand slot
// C, character screen
bool ReadKeybindingConfig()
{
InitKeyMap();
InitDefaultBinding();
file f;
string str;
int l = 0;
if( f.open("keybinding.cfg", "r") >= 0 )
{
l = f.readString( f.getSize(), str );
f.close();
}
else
return false;
if( l <= 10 )
return false;
string@[]@ lines = split(str, "\n");
for( uint i = 0; i < lines.length(); ++i )
{
string@[]@ bindline = split( lines[i], "=" );
// Avoid blah blah and bleh
if ( AllowedKeyExists(bindline[0]) )
{
string@[]@ params = split( bindline[1], " " );
if( params.length() > 5 ) {
Message(bindline[0]+": Binding line can't have more than 5 args.");
break; // skip, a binding line can't have more than 5 params
}
bindkey keyhandle;
for ( uint i = 0; i < params.length(); ++i ) {
if ( i == 0 )
keyhandle.init();
if( params[i].length() > 5 ) {
Message(bindline[0]+": "+NameArg(i)+" arg's lenght is wrong.");
break; // skip, a binding arg can't have more than 5 characters.
}
// modifier ctrl, alt, shift
if( (i == 0 || i == 1) && (params[i].length() >= 3) && !isLast(i,params) ) {
if( params[i] == "Ctrl" ) {
keyhandle.ctrl = true;
continue;
}
else if ( params[i] == "Alt" ) {
keyhandle.alt = true;
continue;
}
else if( params[i] == "Shift" ) {
keyhandle.shift = true;
continue;
}
else {
Message(bindline[0]+": "+NameArg(i)+" arg isn't a modifier key.");
break;
}
}
// should be a key starting* or in between*
// cannot set 2 keys on a keyhandle!
else if ( params[i].length() == 1 && !isLast(i,params) ) {
if( keyhandle.key != 0 ) { // skip binding
Message(bindline[0]+": Can't set 2 keys on same binding.");
break;
}
else if( keymap.get( params[i], keyhandle.key ) ) {
continue;
}
else { // skip binding
Message(bindline[0]+": "+NameArg(i)+" arg isn't an handled key.");
break;
//Message( i+": "+params[i]+" "+params[i].length());
}
}
// parse last params avoiding "\n"
// can be Key or Proto
else if ( isLast( i, params ) ) {
if( keymap.get( substring(params[i], 0, (params[i].length()-1)), keyhandle.key ) ) {
if( SetBinding( bindline[0], keyhandle ) ) // sets the actual keyhandle object state to the global bindings dictionary
break;
}
else if( StrToInt( substring(params[i], 0, (params[i].length()-1)), keyhandle.proto ) ) {
if(keyhandle.key == 0) {
Message(bindline[0]+": Can't set proto without a previous key set.");
break;
}
else if( substring(bindline[0], 0, (bindline[0].length()-1)) != "UseItem" ) {
Message(bindline[0]+": Can't set proto outside of UseItem* binding.");
break;
}
if( SetBinding( bindline[0], keyhandle ) ) // sets the actual keyhandle object state to the global bindings dictionary
break;
}
else {
Message(bindline[0]+": "+NameArg(i)+" arg isn't an integer or handled key.");
break;
}
}
} // for bindline
} // is allowed binding name
} // for lines
return true;
}
bool SetBinding(string& name, bindkey@ keys)
{
bindings.set(name, keys);
return bindings.exists(name);
}
bool SetBinding(string& name, bool ctrl, bool alt, bool shift, uint8 key)
{
bindkey keys;
keys.ctrl = ctrl;
keys.alt = alt;
keys.shift = shift;
keys.key = key;
bindings.set(name, keys);
return bindings.exists(name);
}
void TryBindings(bool ctrl, bool alt, bool shift, uint8 key) // export
{
bindkey obj;
for (uint i = 0; AllowedKeys.length() > i; i++)
{
if( bindings.exists(AllowedKeys[i]) )
{
bindings.get(AllowedKeys[i], obj);
if( ctrl == obj.ctrl && alt == obj.alt && shift == obj.shift ) {
if( key == obj.key ) {
obj.run(i);
break;
}
}
}
}
}
void TryBinding(uint bindproto, bool ctrl, bool alt, bool shift, uint8 key) // export
{
bindkey obj;
if( bindings.exists(AllowedKeys[bindproto]) )
{
bindings.get(AllowedKeys[bindproto], obj);
if( ctrl == obj.ctrl && alt == obj.alt && shift == obj.shift ) {
if( key == obj.key ) {
obj.run(bindproto);
}
}
}
}
bool AllowedKeyExists(string@ name)
{
bool found = false;
for (uint i = 0; AllowedKeys.length() > i; i++)
{
if (name == AllowedKeys[i])
{
found = true;
break;
}
}
return found;
}
#endif
#include "_client_defines.fos"
// bindprotos
#define _FogOfWar (0)
#define _ReloadWeapon (1)
#define _AimHead (2)
#define _AimEyes (3)
#define _AimTorso (4)
#define _AimRightArm (5)
#define _AimLeftArm (6)
#define _AimRightLeg (7)
#define _AimLeftLeg (8)
#define _AimGroin (9)
#define _UseItem1 (10)
#define _UseItem2 (11)
#define _UseItem3 (12)
#define _UseItem4 (13)
#define _UseItem5 (14)
#define _UseItem6 (15)
#define _UseItem7 (16)
#define _UseItem8 (17)
#define _UseItem9 (18)
#ifdef _CLIENT_KEYBINDING
import void InitKeyBinding() from "client_keybinding";
import void TryBindings(bool ctrl, bool alt, bool shift, uint8 key) from "client_keybinding";
import void TryBinding(uint bindproto, bool ctrl, bool alt, bool shift, uint8 key) from "client_keybinding";
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment