OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4KeyboardInput Class Reference

#include <C4KeyboardInput.h>

Public Member Functions

 C4KeyboardInput ()
 
 ~C4KeyboardInput ()
 
void Clear ()
 
void RegisterKey (C4CustomKey *pRegKey)
 
void UnregisterKey (const StdStrBuf &rsName)
 
void UnregisterKeyBinding (C4CustomKey *pKey)
 
bool DoInput (const C4KeyCodeEx &InKey, C4KeyEventType InEvent, DWORD InScope, int32_t iStrength)
 
void CompileFunc (StdCompiler *pComp)
 
bool LoadCustomConfig ()
 
C4CustomKeyGetKeyByName (const char *szKeyName)
 
StdStrBuf GetKeyCodeNameByKeyName (const char *szKeyName, bool fShort=false, int32_t iIndex=0)
 
const C4KeyEventDataGetLastKeyExtraData () const
 
void SetLastKeyExtraData (const C4KeyEventData &data)
 

Static Public Attributes

static bool IsValid = false
 

Detailed Description

Definition at line 483 of file C4KeyboardInput.h.

Constructor & Destructor Documentation

C4KeyboardInput::C4KeyboardInput ( )
inline

Definition at line 502 of file C4KeyboardInput.h.

502 { IsValid = true; }
static bool IsValid
C4KeyboardInput::~C4KeyboardInput ( )
inline

Definition at line 503 of file C4KeyboardInput.h.

References Clear().

503 { Clear(); IsValid = false; }
static bool IsValid

Here is the call graph for this function:

Member Function Documentation

void C4KeyboardInput::Clear ( )

Definition at line 711 of file C4KeyboardInput.cpp.

Referenced by C4Game::Clear(), C4Game::InitKeyboard(), and ~C4KeyboardInput().

712 {
713  LastKeyExtraData = C4KeyEventData();
714  // release all keys - name map is guarantueed to contain them all
715  for (KeyNameMap::const_iterator i = KeysByName.begin(); i != KeysByName.end(); ++i)
716  i->second->Deref();
717  // clear maps
718  KeysByCode.clear();
719  KeysByName.clear();
720 }

Here is the caller graph for this function:

void C4KeyboardInput::CompileFunc ( StdCompiler pComp)

Definition at line 888 of file C4KeyboardInput.cpp.

References StdCompiler::isDeserializer(), StdCompiler::Name(), StdCompiler::NameEnd(), and StdCompiler::Value().

889 {
890  // compile all keys that are already defined
891  // no definition of new keys with current compiler...
892  pComp->Name("Keys");
893  try
894  {
895  for (KeyNameMap::const_iterator i = KeysByName.begin(); i != KeysByName.end(); ++i)
896  {
897  // naming done in C4CustomKey, because default is determined by key only
898  C4CustomKey::CodeList OldCodes = i->second->GetCodes();
899  pComp->Value(*i->second);
900  // resort in secondary map if key changed
901  if (pComp->isDeserializer())
902  {
903  const C4CustomKey::CodeList &rNewCodes = i->second->GetCodes();
904  if (!(OldCodes == rNewCodes)) UpdateKeyCodes(i->second, OldCodes, rNewCodes);
905  }
906  }
907  }
908  catch (StdCompiler::Exception *pEx)
909  {
910  pComp->NameEnd(true);
911  throw pEx;
912  }
913  pComp->NameEnd();
914 }
virtual bool Name(const char *szName)
Definition: StdCompiler.h:77
std::vector< C4KeyCodeEx > CodeList
void Value(const T &rStruct)
Definition: StdCompiler.h:161
virtual bool isDeserializer()
Definition: StdCompiler.h:53
virtual void NameEnd(bool fBreak=false)
Definition: StdCompiler.h:78

Here is the call graph for this function:

bool C4KeyboardInput::DoInput ( const C4KeyCodeEx InKey,
C4KeyEventType  InEvent,
DWORD  InScope,
int32_t  iStrength 
)

Definition at line 822 of file C4KeyboardInput.cpp.

References C4KeyCodeEx::dwShift, C4CustomKey::Execute(), C4KeyEventData::game_x, C4KeyEventData::game_y, C4CustomKey::GetPriority(), C4CustomKey::GetScope(), C4CustomKey::IsCodeMatched(), C4CustomKey::IsDown(), C4KeyEventData::iStrength, C4KeyCodeEx::Key, KEY_Any, KEY_CONTROLLER_AnyButton, KEY_Gamepad(), Key_IsGamepadAxis(), Key_IsGamepadButton(), KEYEV_Up, C4KeyEventData::KeyPos_None, C4CustomKey::PRIO_MoreThanMax, C4CustomKey::PRIO_None, C4KeyEventData::vp_x, and C4KeyEventData::vp_y.

Referenced by C4Game::DoKeyboardInput().

823 {
824  // store last-key-info
825  LastKeyExtraData.iStrength = (iStrength >= 0) ? iStrength : ((InEvent != KEYEV_Up) * 100);
826  LastKeyExtraData.game_x = LastKeyExtraData.game_y = LastKeyExtraData.vp_x = LastKeyExtraData.vp_y = C4KeyEventData::KeyPos_None;
827  // check all key events generated by this key: First the keycode itself, then any more generic key events like KEY_Any
828  const int32_t iKeyRangeMax = 5;
829  int32_t iKeyRangeCnt=0, j;
830  C4KeyCode FallbackKeys[iKeyRangeMax];
831  FallbackKeys[iKeyRangeCnt++] = InKey.Key;
832  if (Key_IsGamepadButton(InKey.Key))
833  {
834  // "any gamepad button"-event
835  FallbackKeys[iKeyRangeCnt++] = KEY_Gamepad(KEY_CONTROLLER_AnyButton);
836  }
837  else if (Key_IsGamepadAxis(InKey.Key))
838  {
839  // TODO: do we need "any axis" events?
840  }
841  if (InKey.Key != KEY_Any) FallbackKeys[iKeyRangeCnt++] = KEY_Any;
842  // now get key ranges for fallback chain
843  std::pair<KeyCodeMap::iterator, KeyCodeMap::iterator> KeyRanges[iKeyRangeMax];
844  assert(iKeyRangeCnt <= iKeyRangeMax);
845  for (int32_t i = 0; i<iKeyRangeCnt; ++i)
846  {
847  KeyRanges[i] = KeysByCode.equal_range(FallbackKeys[i]);
848  }
849  // check all assigned keys
850  // exec from highest to lowest priority
851  unsigned int uiLastPrio = C4CustomKey::PRIO_MoreThanMax;
852  for (;;)
853  {
854  KeyCodeMap::const_iterator i;
855  // get priority to exec
856  unsigned int uiExecPrio = C4CustomKey::PRIO_None, uiCurr;
857  for (j = 0; j < iKeyRangeCnt; ++j)
858  for (i = KeyRanges[j].first; i != KeyRanges[j].second; ++i)
859  {
860  uiCurr = i->second->GetPriority();
861  if (uiCurr > uiExecPrio && uiCurr < uiLastPrio) uiExecPrio = uiCurr;
862  }
863  // nothing with correct priority set left?
864  if (uiExecPrio == C4CustomKey::PRIO_None) break;
865  // exec all of this priority
866  for (j = 0; j < iKeyRangeCnt; ++j)
867  for (i = KeyRanges[j].first; i != KeyRanges[j].second; ++i)
868  {
869  C4CustomKey *pKey = i->second;
870  assert(pKey);
871  // check priority
872  if (pKey->GetPriority() == uiExecPrio)
873  // check scope and modifier
874  // (not on release of a key that has been down, because a key release might happen with a different modifier or in different scope than its pressing!)
875  if ((InEvent == KEYEV_Up && pKey->IsDown())
876  || ((pKey->GetScope() & InScope) && pKey->IsCodeMatched(C4KeyCodeEx(FallbackKeys[j], C4KeyShiftState(InKey.dwShift)))))
877  // exec it
878  if (pKey->Execute(InEvent, InKey))
879  return true;
880  }
881  // nothing found in this priority: exec next
882  uiLastPrio = uiExecPrio;
883  }
884  // no key matched or all returned false in Execute: Not processed
885  return false;
886 }
C4KeyScope GetScope() const
C4KeyShiftState
bool Key_IsGamepadButton(C4KeyCode key)
C4KeyCode KEY_Gamepad(uint8_t idButton)
C4KeyCode Key
bool IsCodeMatched(const C4KeyCodeEx &key) const
bool Key_IsGamepadAxis(C4KeyCode key)
unsigned int GetPriority() const
const C4KeyCode KEY_Any
const C4KeyCode KEY_CONTROLLER_AnyButton
bool Execute(C4KeyEventType eEv, C4KeyCodeEx key)
bool IsDown() const
unsigned long C4KeyCode

Here is the call graph for this function:

Here is the caller graph for this function:

C4CustomKey * C4KeyboardInput::GetKeyByName ( const char *  szKeyName)

Definition at line 930 of file C4KeyboardInput.cpp.

Referenced by GetKeyCodeNameByKeyName().

931 {
932  KeyNameMap::const_iterator i = KeysByName.find(szKeyName);
933  if (i == KeysByName.end()) return nullptr; else return (*i).second;
934 }

Here is the caller graph for this function:

StdStrBuf C4KeyboardInput::GetKeyCodeNameByKeyName ( const char *  szKeyName,
bool  fShort = false,
int32_t  iIndex = 0 
)

Definition at line 936 of file C4KeyboardInput.cpp.

References C4CustomKey::GetCodes(), GetKeyByName(), and C4KeyCodeEx::ToString().

Referenced by GetKeyboardInputName(), and C4FullScreen::ViewportCheck().

937 {
938  C4CustomKey *pKey = GetKeyByName(szKeyName);
939  if (pKey)
940  {
941  const C4CustomKey::CodeList &codes = pKey->GetCodes();
942  if ((size_t)iIndex < codes.size())
943  {
944  C4KeyCodeEx code = codes[iIndex];
945  return code.ToString(true, fShort);
946  }
947  }
948  // Error
949  return StdStrBuf();
950 }
C4CustomKey * GetKeyByName(const char *szKeyName)
std::vector< C4KeyCodeEx > CodeList
StdStrBuf ToString(bool fHumanReadable, bool fShort) const
const CodeList & GetCodes() const

Here is the call graph for this function:

Here is the caller graph for this function:

const C4KeyEventData& C4KeyboardInput::GetLastKeyExtraData ( ) const
inline

Definition at line 523 of file C4KeyboardInput.h.

523 { return LastKeyExtraData; }
bool C4KeyboardInput::LoadCustomConfig ( )

Definition at line 916 of file C4KeyboardInput.cpp.

References C4CFN_Extra, C4CFN_KeyConfig, DirSep, StdBuf::getData(), C4Group::LoadEntry(), LoadResStr(), LogF(), and C4Group::Open().

Referenced by C4Game::InitKeyboard().

917 {
918  // load from INI file (2do: load from registry)
919  C4Group GrpExtra;
920  if (!GrpExtra.Open(C4CFN_Extra)) return false;
921  StdBuf sFileContents;
922  if (!GrpExtra.LoadEntry(C4CFN_KeyConfig, &sFileContents)) return false;
923  StdStrBuf sFileContentsString((const char *) sFileContents.getData());
924  if (!CompileFromBuf_LogWarn<StdCompilerINIRead>(*this, sFileContentsString, "Custom keys from" C4CFN_Extra DirSep C4CFN_KeyConfig))
925  return false;
926  LogF(LoadResStr("IDS_PRC_LOADEDKEYCONF"), C4CFN_Extra DirSep C4CFN_KeyConfig);
927  return true;
928 }
#define C4CFN_Extra
Definition: C4Components.h:31
const void * getData() const
Definition: StdBuf.h:99
Definition: StdBuf.h:29
bool LoadEntry(const char *szEntryName, char **lpbpBuf, size_t *ipSize=nullptr, int iAppendZeros=0)
Definition: C4Group.cpp:1893
const char * LoadResStr(const char *id)
Definition: C4Language.h:83
bool Open(const char *szGroupName, bool fCreate=false)
Definition: C4Group.cpp:514
#define C4CFN_KeyConfig
Definition: C4Components.h:139
#define DirSep
bool LogF(const char *strMessage,...)
Definition: C4Log.cpp:260

Here is the call graph for this function:

Here is the caller graph for this function:

void C4KeyboardInput::RegisterKey ( C4CustomKey pRegKey)

Definition at line 749 of file C4KeyboardInput.cpp.

References C4CustomKey::Deref(), C4CustomKey::GetCodes(), StdStrBuf::getData(), C4CustomKey::GetName(), C4CustomKey::Ref(), and C4CustomKey::Update().

Referenced by C4KeyBinding::C4KeyBinding(), C4Game::InitKeyboard(), and UnregisterKeyBinding().

750 {
751  assert(pRegKey); if (!pRegKey) return;
752  // key will be added: ref it
753  pRegKey->Ref();
754  // search key of same name first
755  C4CustomKey *pDupKey = KeysByName[pRegKey->GetName().getData()];
756  if (pDupKey)
757  {
758  // key of this name exists: Merge them (old codes copied cuz they'll be overwritten)
759  C4CustomKey::CodeList OldCodes = pDupKey->GetCodes();
760  const C4CustomKey::CodeList &rNewCodes = pRegKey->GetCodes();
761  pDupKey->Update(pRegKey);
762  // update access map if key changed
763  if (!(OldCodes == rNewCodes)) UpdateKeyCodes(pDupKey, OldCodes, rNewCodes);
764  // key to be registered no longer used
765  pRegKey->Deref();
766  }
767  else
768  {
769  // new unique key: Insert into map
770  KeysByName[pRegKey->GetName().getData()] = pRegKey;
771  for (C4CustomKey::CodeList::const_iterator i = pRegKey->GetCodes().begin(); i != pRegKey->GetCodes().end(); ++i)
772  {
773  KeysByCode.insert(std::make_pair((*i).Key, pRegKey));
774  }
775  }
776 }
const char * getData() const
Definition: StdBuf.h:442
void Update(const C4CustomKey *pByKey)
std::vector< C4KeyCodeEx > CodeList
const StdStrBuf & GetName() const
const CodeList & GetCodes() const

Here is the call graph for this function:

Here is the caller graph for this function:

void C4KeyboardInput::SetLastKeyExtraData ( const C4KeyEventData data)
inline

Definition at line 524 of file C4KeyboardInput.h.

Referenced by C4PlayerControl::DoMouseInput().

524 { LastKeyExtraData=data; }

Here is the caller graph for this function:

void C4KeyboardInput::UnregisterKey ( const StdStrBuf rsName)

Definition at line 778 of file C4KeyboardInput.cpp.

References C4CustomKey::Deref(), C4CustomKey::GetCodes(), and StdStrBuf::getData().

Referenced by UnregisterKeyBinding().

779 {
780  // kill from name map
781  KeyNameMap::iterator in = KeysByName.find(rsName.getData());
782  if (in == KeysByName.end()) return;
783  C4CustomKey *pKey = in->second;
784  KeysByName.erase(in);
785  // kill all key bindings from key map
786  for (C4CustomKey::CodeList::const_iterator iCode = pKey->GetCodes().begin(); iCode != pKey->GetCodes().end(); ++iCode)
787  {
788  std::pair<KeyCodeMap::iterator, KeyCodeMap::iterator> KeyRange = KeysByCode.equal_range((*iCode).Key);
789  for (KeyCodeMap::iterator i = KeyRange.first; i != KeyRange.second; ++i)
790  if (i->second == pKey)
791  {
792  KeysByCode.erase(i);
793  break;
794  }
795  }
796  // release reference to key
797  pKey->Deref();
798 }
const char * getData() const
Definition: StdBuf.h:442
const CodeList & GetCodes() const

Here is the call graph for this function:

Here is the caller graph for this function:

void C4KeyboardInput::UnregisterKeyBinding ( C4CustomKey pKey)

Definition at line 800 of file C4KeyboardInput.cpp.

References StdStrBuf::getData(), C4CustomKey::GetName(), C4CustomKey::KillCallbacks(), RegisterKey(), and UnregisterKey().

Referenced by C4KeyBinding::~C4KeyBinding().

801 {
802  // find key in name map
803  KeyNameMap::iterator in = KeysByName.find(pUnregKey->GetName().getData());
804  if (in == KeysByName.end()) return;
805  C4CustomKey *pKey = in->second;
806  // is this key in the map?
807  if (pKey != pUnregKey)
808  {
809  // Other key is in the list: Just remove the callbacks
810  pKey->KillCallbacks(pUnregKey);
811  return;
812  }
813  // this key is in the list: Replace by a duplicate...
814  C4CustomKey *pNewKey = new C4CustomKey(*pUnregKey, true);
815  // ...without the own callbacks
816  pNewKey->KillCallbacks(pUnregKey);
817  // and replace current key by duplicate
818  UnregisterKey(pUnregKey->GetName());
819  RegisterKey(pNewKey);
820 }
void KillCallbacks(const C4CustomKey *pOfKey)
void UnregisterKey(const StdStrBuf &rsName)
void RegisterKey(C4CustomKey *pRegKey)

Here is the call graph for this function:

Here is the caller graph for this function:

Member Data Documentation

bool C4KeyboardInput::IsValid = false
static

Definition at line 500 of file C4KeyboardInput.h.

Referenced by C4KeyBinding::~C4KeyBinding().


The documentation for this class was generated from the following files: