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 490 of file C4KeyboardInput.h.

Constructor & Destructor Documentation

C4KeyboardInput::C4KeyboardInput ( )
inline

Definition at line 509 of file C4KeyboardInput.h.

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

Definition at line 510 of file C4KeyboardInput.h.

References Clear().

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

Here is the call graph for this function:

Member Function Documentation

void C4KeyboardInput::Clear ( )

Definition at line 716 of file C4KeyboardInput.cpp.

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

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

Here is the caller graph for this function:

void C4KeyboardInput::CompileFunc ( StdCompiler pComp)

Definition at line 893 of file C4KeyboardInput.cpp.

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

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

Here is the call graph for this function:

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

Definition at line 827 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().

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

Referenced by GetKeyCodeNameByKeyName().

936 {
937  KeyNameMap::const_iterator i = KeysByName.find(szKeyName);
938  if (i == KeysByName.end()) return nullptr; else return (*i).second;
939 }

Here is the caller graph for this function:

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

Definition at line 941 of file C4KeyboardInput.cpp.

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

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

942 {
943  C4CustomKey *pKey = GetKeyByName(szKeyName);
944  if (pKey)
945  {
946  const C4CustomKey::CodeList &codes = pKey->GetCodes();
947  if ((size_t)iIndex < codes.size())
948  {
949  C4KeyCodeEx code = codes[iIndex];
950  return code.ToString(true, fShort);
951  }
952  }
953  // Error
954  return StdStrBuf();
955 }
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 530 of file C4KeyboardInput.h.

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

Definition at line 921 of file C4KeyboardInput.cpp.

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

Referenced by C4Game::InitKeyboard().

922 {
923  // load from INI file (2do: load from registry)
924  C4Group GrpExtra;
925  if (!GrpExtra.Open(C4CFN_Extra)) return false;
926  StdBuf sFileContents;
927  if (!GrpExtra.LoadEntry(C4CFN_KeyConfig, &sFileContents)) return false;
928  StdStrBuf sFileContentsString((const char *) sFileContents.getData());
929  if (!CompileFromBuf_LogWarn<StdCompilerINIRead>(*this, sFileContentsString, "Custom keys from" C4CFN_Extra DirSep C4CFN_KeyConfig))
930  return false;
931  LogF(LoadResStr("IDS_PRC_LOADEDKEYCONF"), C4CFN_Extra DirSep C4CFN_KeyConfig);
932  return true;
933 }
#define C4CFN_Extra
Definition: C4Components.h:31
const void * getData() const
Definition: StdBuf.h:107
Definition: StdBuf.h:37
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:253

Here is the call graph for this function:

Here is the caller graph for this function:

void C4KeyboardInput::RegisterKey ( C4CustomKey pRegKey)

Definition at line 754 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().

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

Referenced by C4PlayerControl::DoMouseInput().

531 { LastKeyExtraData=data; }

Here is the caller graph for this function:

void C4KeyboardInput::UnregisterKey ( const StdStrBuf rsName)

Definition at line 783 of file C4KeyboardInput.cpp.

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

Referenced by UnregisterKeyBinding().

784 {
785  // kill from name map
786  KeyNameMap::iterator in = KeysByName.find(rsName.getData());
787  if (in == KeysByName.end()) return;
788  C4CustomKey *pKey = in->second;
789  KeysByName.erase(in);
790  // kill all key bindings from key map
791  for (C4CustomKey::CodeList::const_iterator iCode = pKey->GetCodes().begin(); iCode != pKey->GetCodes().end(); ++iCode)
792  {
793  std::pair<KeyCodeMap::iterator, KeyCodeMap::iterator> KeyRange = KeysByCode.equal_range((*iCode).Key);
794  for (KeyCodeMap::iterator i = KeyRange.first; i != KeyRange.second; ++i)
795  if (i->second == pKey)
796  {
797  KeysByCode.erase(i);
798  break;
799  }
800  }
801  // release reference to key
802  pKey->Deref();
803 }
const char * getData() const
Definition: StdBuf.h:450
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 805 of file C4KeyboardInput.cpp.

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

Referenced by C4KeyBinding::~C4KeyBinding().

806 {
807  // find key in name map
808  KeyNameMap::iterator in = KeysByName.find(pUnregKey->GetName().getData());
809  if (in == KeysByName.end()) return;
810  C4CustomKey *pKey = in->second;
811  // is this key in the map?
812  if (pKey != pUnregKey)
813  {
814  // Other key is in the list: Just remove the callbacks
815  pKey->KillCallbacks(pUnregKey);
816  return;
817  }
818  // this key is in the list: Replace by a duplicate...
819  C4CustomKey *pNewKey = new C4CustomKey(*pUnregKey, true);
820  // ...without the own callbacks
821  pNewKey->KillCallbacks(pUnregKey);
822  // and replace current key by duplicate
823  UnregisterKey(pUnregKey->GetName());
824  RegisterKey(pNewKey);
825 }
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 507 of file C4KeyboardInput.h.

Referenced by C4KeyBinding::~C4KeyBinding().


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