| 1 | //************************************ bs::framework - Copyright 2018 Marko Pintera **************************************// |
|---|---|
| 2 | //*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********// |
| 3 | #include "GUI/BsShortcutManager.h" |
| 4 | #include "Input/BsInput.h" |
| 5 | |
| 6 | using namespace std::placeholders; |
| 7 | |
| 8 | namespace bs |
| 9 | { |
| 10 | ShortcutManager::ShortcutManager() |
| 11 | { |
| 12 | mOnButtonDownConn = Input::instance().onButtonDown.connect(std::bind(&ShortcutManager::onButtonDown, this, _1)); |
| 13 | } |
| 14 | |
| 15 | ShortcutManager::~ShortcutManager() |
| 16 | { |
| 17 | mOnButtonDownConn.disconnect(); |
| 18 | } |
| 19 | |
| 20 | void ShortcutManager::addShortcut(const ShortcutKey& key, std::function<void()> callback) |
| 21 | { |
| 22 | mShortcuts[key] = callback; |
| 23 | } |
| 24 | |
| 25 | void ShortcutManager::removeShortcut(const ShortcutKey& key) |
| 26 | { |
| 27 | mShortcuts.erase(key); |
| 28 | } |
| 29 | |
| 30 | void ShortcutManager::onButtonDown(const ButtonEvent& event) |
| 31 | { |
| 32 | UINT32 modifiers = 0; |
| 33 | if (Input::instance().isButtonHeld(BC_LSHIFT) || Input::instance().isButtonHeld(BC_RSHIFT)) |
| 34 | modifiers |= (UINT32)ButtonModifier::Shift; |
| 35 | |
| 36 | if (Input::instance().isButtonHeld(BC_LCONTROL) || Input::instance().isButtonHeld(BC_RCONTROL)) |
| 37 | modifiers |= (UINT32)ButtonModifier::Ctrl; |
| 38 | |
| 39 | if (Input::instance().isButtonHeld(BC_LMENU) || Input::instance().isButtonHeld(BC_RMENU)) |
| 40 | modifiers |= (UINT32)ButtonModifier::Alt; |
| 41 | |
| 42 | ShortcutKey searchKey((ButtonModifier)modifiers, event.buttonCode); |
| 43 | |
| 44 | auto iterFind = mShortcuts.find(searchKey); |
| 45 | if (iterFind != mShortcuts.end()) |
| 46 | { |
| 47 | if (iterFind->second != nullptr) |
| 48 | iterFind->second(); |
| 49 | } |
| 50 | } |
| 51 | } |