Pros:
- Easy to implement
- Keybindings are easy to manage inside the class
- Multiple keybinds for same button
Cons:
- Lots and lots of API calls to check key states
- Will become more difficult to change if performance becomes an issue
- Will need N polls per frame, N -> number of binding
- It's sort-of Observer-patttern
- Keybindings are easily visible inside the class
- Can easily be refactored into either of the others by being a middle ground.
- Multiple keybinds for same button
- Still need to do lots and lots of checks
- Still needs 1 API-poll + N pulls per frame
- It's a correct Observer-pattern
- An effecient implementation will only notify changes, reducing CPU-cycles needed
- It requires an interface/mediator/semaphore-style variable
- If we want to reduce notifications, we increase complexity in KeyboardManager
- Multiple keybinding leads to drastically increased complexity.
//Vehicle.cpp
void Vehicle::UpdatePoll(int time){
if(GetKeyState(VK_RETURN){ //Do something ? }
}
//Vehicle.cpp
void Vehicle::UpdatePull(int time){
if (kbManager->IsKey(VK_RETURN){ ... }
}
//Keyboardmanager.cpp
void KBManager::Update(){
localKeyStates = GetKeyStates();
}
UINT KBManager::IsKey(UINT virtualKey){
return localKeyStates[virtualKey];
}
//Vehicle.cpp inherits KeyObserver
Vehicle::Vehicle(){
kbManager->Register(VK_RETURN, this);
}
void Vehicle::UpdateKeyState(UINT keyVal, bool state){
if (keyVal == VK_RETURN) this->keyReturn = state;
}
void Vehicle::Update(int time){
if(this->keyReturn) { ... }
}
//Keyboardmanager.cpp
void KBManager::Register(UINT virtualKey, KeyObserver& obj){
this->BindingList[virtualKey] = obj;
}
void KBManager::Update(int time){
this->localKeyStates = GetKeyStates();
for_each (UINT key : BindingList.Keys){
Notify(key);
}
}
void KBManager::Notify(UINT key){
if (IsChanged(localKeyStates[key])){
BindingList[key]->Update(key, localKeyStates[key]);
}
}