|
| 1 | +/* Key Binding Option |
| 2 | + * |
| 3 | + * From: https://github.com/PokemonAutomation/Arduino-Source |
| 4 | + * |
| 5 | + */ |
| 6 | + |
| 7 | +#include "Common/Cpp/Json/JsonValue.h" |
| 8 | +#include "Common/Cpp/Containers/Pimpl.tpp" |
| 9 | +#include "Common/Cpp/Concurrency/SpinLock.h" |
| 10 | +#include "KeyBindingOption.h" |
| 11 | + |
| 12 | +namespace PokemonAutomation{ |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +struct KeyBindingCell::Data{ |
| 17 | + const uint32_t m_default; |
| 18 | + |
| 19 | + mutable SpinLock m_lock; |
| 20 | + uint32_t m_current; |
| 21 | + std::string m_current_text; |
| 22 | + |
| 23 | + Data(uint32_t default_value) |
| 24 | + : m_default(default_value) |
| 25 | + , m_current(default_value) |
| 26 | + {} |
| 27 | +}; |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | +KeyBindingCell::~KeyBindingCell(){ |
| 32 | + |
| 33 | +} |
| 34 | +KeyBindingCell::KeyBindingCell(LockMode lock_while_program_is_running) |
| 35 | + : ConfigOption(lock_while_program_is_running) |
| 36 | + , m_data(CONSTRUCT_TOKEN, 0) |
| 37 | +{} |
| 38 | + |
| 39 | + |
| 40 | +KeyBindingCell::operator uint32_t() const{ |
| 41 | + ReadSpinLock lg(m_data->m_lock); |
| 42 | + return m_data->m_current; |
| 43 | +} |
| 44 | +KeyBindingCell::operator std::string() const{ |
| 45 | + ReadSpinLock lg(m_data->m_lock); |
| 46 | + return m_data->m_current_text; |
| 47 | +} |
| 48 | +void KeyBindingCell::set(uint32_t key){ |
| 49 | + { |
| 50 | + ReadSpinLock lg(m_data->m_lock); |
| 51 | + m_data->m_current = key; |
| 52 | + } |
| 53 | + report_value_changed(this); |
| 54 | +} |
| 55 | +void KeyBindingCell::set(std::string text){ |
| 56 | + ReadSpinLock lg(m_data->m_lock); |
| 57 | + m_data->m_current_text = std::move(text); |
| 58 | +} |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | +void KeyBindingCell::load_json(const JsonValue& json){ |
| 63 | + set((uint32_t)json.to_integer_default(0)); |
| 64 | +} |
| 65 | +JsonValue KeyBindingCell::to_json() const{ |
| 66 | + ReadSpinLock lg(m_data->m_lock); |
| 67 | + return m_data->m_current; |
| 68 | +} |
| 69 | + |
| 70 | +void KeyBindingCell::restore_defaults(){ |
| 71 | + set(m_data->m_default); |
| 72 | +} |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | +} |
0 commit comments