kick
|
00001 // 00002 // key_input.cpp 00003 // UnitTest_KickCPP 00004 // 00005 // Created by morten on 24/04/14. 00006 // 00007 // 00008 00009 #include "key_input.h" 00010 00011 namespace kick { 00012 std::set<Key> KeyInput::buttonDown; 00013 std::set<Key> KeyInput::buttonPressed; 00014 std::set<Key> KeyInput::buttonUp; 00015 00016 void KeyInput::reset(){ 00017 buttonDown.clear(); 00018 buttonUp.clear(); 00019 } 00020 00021 bool KeyInput::down(Key button) { 00022 return buttonDown.find(button) != buttonDown.end(); 00023 } 00024 00025 bool KeyInput::anyDown() { 00026 return !buttonDown.empty(); 00027 } 00028 00029 bool KeyInput::pressed(Key button) { 00030 return buttonPressed.find(button) != buttonPressed.end(); 00031 } 00032 00033 bool KeyInput::anyPressed() { 00034 return !buttonPressed.empty(); 00035 } 00036 00037 bool KeyInput::up(Key button) { 00038 return buttonUp.find(button) != buttonUp.end(); 00039 } 00040 00041 bool KeyInput::anyUp() { 00042 return !buttonUp.empty(); 00043 } 00044 00045 void KeyInput::pressBegin(Key k) { 00046 buttonDown.insert(k); 00047 buttonPressed.insert(k); 00048 } 00049 00050 void KeyInput::pressEnd(Key k) { 00051 buttonPressed.erase(k); 00052 buttonUp.insert(k); 00053 } 00054 00055 KeyMapping::KeyMapping(std::initializer_list<Key> keys) 00056 : keys(keys) { 00057 00058 } 00059 00060 KeyMapping::KeyMapping(std::initializer_list<KeyMapping> alternatives) 00061 { 00062 for (auto & a : alternatives){ 00063 this->alternatives.push_back(a); 00064 } 00065 } 00066 00067 bool KeyInput::pressed(const KeyMapping &mapping) { 00068 if (mapping.alternatives.size()>0){ 00069 for (auto & k : mapping.alternatives){ 00070 bool res = pressed(k); 00071 if (res){ 00072 return true; 00073 } 00074 } 00075 return false; 00076 } 00077 for (auto & k : mapping.keys){ 00078 if (!pressed(k)){ 00079 return false; 00080 } 00081 } 00082 return true; 00083 } 00084 00085 bool KeyInput::down(const KeyMapping &mapping) { 00086 if (mapping.alternatives.size()>0){ 00087 for (auto & k : mapping.alternatives){ 00088 bool res = down(k); 00089 if (res){ 00090 return true; 00091 } 00092 } 00093 return false; 00094 } 00095 if (!pressed(mapping)){ 00096 return false; 00097 } 00098 for (auto & k : mapping.keys){ 00099 if (down(k)){ 00100 return true; 00101 } 00102 } 00103 return false; 00104 } 00105 00106 00107 const std::set<Key> &KeyInput::buttonsDown() { return buttonDown;} 00108 00109 const std::set<Key> &KeyInput::buttonsPressed() { return buttonPressed;} 00110 00111 const std::set<Key> &KeyInput::buttonsUp() {return buttonUp;} 00112 }