hotkeys.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. HotkeySettings::HotkeySettings(View* parent) : PanelItem(parent, Size{~0, ~0}) {
  2. setCollapsible().setVisible(false);
  3. headerLabel.setText("Hotkeys").setFont(Font().setBold());
  4. hotkeyList.setBatchable();
  5. hotkeyList.onActivate([&](auto cell) { eventAssign(); });
  6. hotkeyList.onChange([&] { eventChange(); });
  7. assignButton.setText("Assign").onActivate([&] { eventAssign(); });
  8. clearButton.setText("Clear").onActivate([&] { eventClear(); });
  9. }
  10. auto HotkeySettings::show() -> void {
  11. refresh();
  12. setVisible(true);
  13. }
  14. auto HotkeySettings::hide() -> void {
  15. setVisible(false);
  16. assigning.reset();
  17. }
  18. auto HotkeySettings::refresh() -> void {
  19. hotkeyList.reset().setEnabled();
  20. hotkeyList.append(TableViewColumn().setText("Name"));
  21. hotkeyList.append(TableViewColumn().setText("Mapping").setExpandable());
  22. for(auto& hotkey : hotkeys.hotkeys) {
  23. TableViewItem item{&hotkeyList};
  24. item.setAttribute<InputHotkey*>("hotkey", hotkey);
  25. TableViewCell name{&item};
  26. name.setText(hotkey->name).setFont(Font().setBold());
  27. TableViewCell value{&item};
  28. }
  29. update();
  30. hotkeyList.doChange();
  31. }
  32. auto HotkeySettings::update() -> void {
  33. for(auto& item : hotkeyList.items()) {
  34. auto value = item.cell(1);
  35. auto hotkey = item.attribute<InputHotkey*>("hotkey");
  36. if(hotkey->device) {
  37. auto device = hotkey->device->name();
  38. if(device == "Keyboard") value.setIcon(Icon::Device::Keyboard);
  39. else if(device == "Mouse") value.setIcon(Icon::Device::Mouse);
  40. else if(device == "Joypad") value.setIcon(Icon::Device::Joypad);
  41. else value.setIcon(Icon::Action::Close);
  42. string label;
  43. if(hotkey->device->isJoypad()) label.append(hotkey->device->group(hotkey->groupID).name(), ".");
  44. label.append(hotkey->device->group(hotkey->groupID).input(hotkey->inputID).name());
  45. value.setText(label);
  46. } else {
  47. value.setIcon(Icon::Action::Close);
  48. value.setText("(unmapped)");
  49. }
  50. }
  51. hotkeyList.resizeColumns();
  52. }
  53. auto HotkeySettings::eventAssign() -> void {
  54. inputManager.poll();
  55. assigningQueue = hotkeyList.batched();
  56. eventAssignNext();
  57. }
  58. auto HotkeySettings::eventAssignNext() -> void {
  59. if(!assigningQueue) {
  60. hotkeyList.selectNone().setFocused();
  61. return eventChange();
  62. }
  63. //use the viewport to sink inputs away from the table view during assignment
  64. program.viewport.setFocused();
  65. auto item = assigningQueue.takeFirst();
  66. hotkeyList.selectNone();
  67. item.setSelected().setFocused();
  68. auto hotkey = item.attribute<InputHotkey*>("hotkey");
  69. item.cell(1).setIcon(Icon::Go::Right).setText("(assign)");
  70. assigning = *hotkey;
  71. eventChange();
  72. }
  73. auto HotkeySettings::eventClear() -> void {
  74. if(auto batched = hotkeyList.batched()) {
  75. for(auto& item : batched) {
  76. auto hotkey = item.attribute<InputHotkey*>("hotkey");
  77. hotkey->identifier = {};
  78. }
  79. hotkeys.bind();
  80. update();
  81. }
  82. }
  83. auto HotkeySettings::eventChange() -> void {
  84. auto batched = hotkeyList.batched();
  85. assignButton.setEnabled((bool)batched);
  86. clearButton.setEnabled((bool)batched);
  87. }
  88. auto HotkeySettings::eventInput(shared_pointer<HID::Device> device, uint group, uint input, int16_t oldValue, int16_t newValue) -> void {
  89. if(!assigning) return;
  90. bool allow = false;
  91. if(device->isKeyboard()) {
  92. if(oldValue == 0 && newValue == 1) allow = true;
  93. }
  94. if(device->isJoypad() && group == HID::Joypad::Button) {
  95. if(oldValue == 0 && newValue == 1) allow = true;
  96. }
  97. if(!allow) return;
  98. assigning->identifier = {};
  99. if(device->group(group).input(input).name() != "Escape") {
  100. assigning->identifier.append(hex(device->pathID()), "/");
  101. assigning->identifier.append(hex(device->vendorID()), "/");
  102. assigning->identifier.append(hex(device->productID()), "/");
  103. assigning->identifier.append(device->group(group).name(), "/");
  104. assigning->identifier.append(device->group(group).input(input).name());
  105. }
  106. assigning.reset();
  107. hotkeys.bind();
  108. update();
  109. eventAssignNext();
  110. }