The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

272 lines
10KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE examples.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. The code included in this file is provided under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  7. To use, copy, modify, and/or distribute this software for any purpose with or
  8. without fee is hereby granted provided that the above copyright notice and
  9. this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES,
  11. WHETHER EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR
  12. PURPOSE, ARE DISCLAIMED.
  13. ==============================================================================
  14. */
  15. /*******************************************************************************
  16. The block below describes the properties of this PIP. A PIP is a short snippet
  17. of code that can be read by the Projucer and used to generate a JUCE project.
  18. BEGIN_JUCE_PIP_METADATA
  19. name: KeyMappingsDemo
  20. version: 1.0.0
  21. vendor: JUCE
  22. website: http://juce.com
  23. description: Showcases key mapping features.
  24. dependencies: juce_core, juce_data_structures, juce_events, juce_graphics,
  25. juce_gui_basics, juce_gui_extra
  26. exporters: xcode_mac, vs2022, linux_make, androidstudio, xcode_iphone
  27. moduleFlags: JUCE_STRICT_REFCOUNTEDPOINTER=1
  28. type: Component
  29. mainClass: KeyMappingsDemo
  30. useLocalCopy: 1
  31. END_JUCE_PIP_METADATA
  32. *******************************************************************************/
  33. #pragma once
  34. #include "../Assets/DemoUtilities.h"
  35. /** A list of the command IDs that this demo can perform. */
  36. enum KeyPressCommandIDs
  37. {
  38. buttonMoveUp = 1,
  39. buttonMoveRight,
  40. buttonMoveDown,
  41. buttonMoveLeft,
  42. nextButtonColour,
  43. previousButtonColour,
  44. nextBackgroundColour,
  45. previousBackgroundColour
  46. };
  47. //==============================================================================
  48. /**
  49. This is a simple target for the key-presses which will live inside the demo component
  50. and contains a button that can be moved around with the arrow keys.
  51. */
  52. class KeyPressTarget final : public Component,
  53. public ApplicationCommandTarget
  54. {
  55. public:
  56. KeyPressTarget()
  57. {
  58. Array<Colour> coloursToUse { Colours::darkblue, Colours::darkgrey, Colours::red,
  59. Colours::green, Colours::blue, Colours::hotpink };
  60. colours.addArray (coloursToUse);
  61. addAndMakeVisible (button);
  62. }
  63. //==============================================================================
  64. void resized() override
  65. {
  66. auto bounds = getLocalBounds();
  67. // keep the button on-screen
  68. if (buttonX < -150 || buttonX > bounds.getWidth()
  69. || buttonY < -30 || buttonY > bounds.getHeight())
  70. {
  71. buttonX = bounds.getCentreX() - 75;
  72. buttonY = bounds.getCentreY() - 15;
  73. }
  74. button.setBounds (buttonX, buttonY, 150, 30);
  75. }
  76. void paint (Graphics& g) override
  77. {
  78. g.fillAll (colours.getUnchecked (backgroundColourIndex));
  79. }
  80. //==============================================================================
  81. /** No other command targets in this simple example so just return nullptr. */
  82. ApplicationCommandTarget* getNextCommandTarget() override { return nullptr; }
  83. void getAllCommands (Array<CommandID>& commands) override
  84. {
  85. Array<CommandID> ids { KeyPressCommandIDs::buttonMoveUp, KeyPressCommandIDs::buttonMoveRight,
  86. KeyPressCommandIDs::buttonMoveDown, KeyPressCommandIDs::buttonMoveLeft,
  87. KeyPressCommandIDs::nextButtonColour, KeyPressCommandIDs::previousButtonColour,
  88. KeyPressCommandIDs::nextBackgroundColour, KeyPressCommandIDs::previousBackgroundColour };
  89. commands.addArray (ids);
  90. }
  91. void getCommandInfo (CommandID commandID, ApplicationCommandInfo& result) override
  92. {
  93. switch (commandID)
  94. {
  95. case KeyPressCommandIDs::buttonMoveUp:
  96. result.setInfo ("Move up", "Move the button up", "Button", 0);
  97. result.addDefaultKeypress (KeyPress::upKey, 0);
  98. break;
  99. case KeyPressCommandIDs::buttonMoveRight:
  100. result.setInfo ("Move right", "Move the button right", "Button", 0);
  101. result.addDefaultKeypress (KeyPress::rightKey, 0);
  102. break;
  103. case KeyPressCommandIDs::buttonMoveDown:
  104. result.setInfo ("Move down", "Move the button down", "Button", 0);
  105. result.addDefaultKeypress (KeyPress::downKey, 0);
  106. break;
  107. case KeyPressCommandIDs::buttonMoveLeft:
  108. result.setInfo ("Move left", "Move the button left", "Button", 0);
  109. result.addDefaultKeypress (KeyPress::leftKey, 0);
  110. break;
  111. case KeyPressCommandIDs::nextButtonColour:
  112. result.setInfo ("Next colour", "Change the colour of the button to the next in the list", "Button", 0);
  113. result.addDefaultKeypress (KeyPress::rightKey, ModifierKeys::shiftModifier);
  114. break;
  115. case KeyPressCommandIDs::previousButtonColour:
  116. result.setInfo ("Previous colour", "Change the colour of the button to the previous in the list", "Button", 0);
  117. result.addDefaultKeypress (KeyPress::leftKey, ModifierKeys::shiftModifier);
  118. break;
  119. case KeyPressCommandIDs::nextBackgroundColour:
  120. result.setInfo ("Next colour", "Change the colour of the background to the next in the list", "Other", 0);
  121. result.addDefaultKeypress (KeyPress::rightKey, ModifierKeys::commandModifier);
  122. break;
  123. case KeyPressCommandIDs::previousBackgroundColour:
  124. result.setInfo ("Previous colour", "Change the colour of the background to the previous in the list", "Other", 0);
  125. result.addDefaultKeypress (KeyPress::leftKey, ModifierKeys::commandModifier);
  126. break;
  127. default:
  128. break;
  129. }
  130. }
  131. bool perform (const InvocationInfo& info) override
  132. {
  133. switch (info.commandID)
  134. {
  135. case KeyPressCommandIDs::buttonMoveUp:
  136. buttonY -= 5;
  137. resized();
  138. break;
  139. case KeyPressCommandIDs::buttonMoveRight:
  140. buttonX += 5;
  141. resized();
  142. break;
  143. case KeyPressCommandIDs::buttonMoveDown:
  144. buttonY += 5;
  145. resized();
  146. break;
  147. case KeyPressCommandIDs::buttonMoveLeft:
  148. buttonX -= 5;
  149. resized();
  150. break;
  151. case KeyPressCommandIDs::nextButtonColour:
  152. ++buttonColourIndex %= colours.size();
  153. button.setColour (TextButton::buttonColourId, colours.getUnchecked (buttonColourIndex));
  154. break;
  155. case KeyPressCommandIDs::previousButtonColour:
  156. --buttonColourIndex;
  157. if (buttonColourIndex < 0)
  158. buttonColourIndex = colours.size() - 1;
  159. button.setColour (TextButton::buttonColourId, colours.getUnchecked (buttonColourIndex));
  160. break;
  161. case KeyPressCommandIDs::nextBackgroundColour:
  162. ++backgroundColourIndex %= colours.size();
  163. repaint();
  164. break;
  165. case KeyPressCommandIDs::previousBackgroundColour:
  166. --backgroundColourIndex;
  167. if (backgroundColourIndex < 0)
  168. backgroundColourIndex = colours.size() - 1;
  169. repaint();
  170. break;
  171. default:
  172. return false;
  173. }
  174. return true;
  175. }
  176. private:
  177. TextButton button;
  178. int buttonX = -200, buttonY = -200;
  179. Array<Colour> colours;
  180. int buttonColourIndex = 0;
  181. int backgroundColourIndex = 1;
  182. };
  183. //==============================================================================
  184. class KeyMappingsDemo final : public Component
  185. {
  186. public:
  187. KeyMappingsDemo()
  188. {
  189. // register the commands that the target component can perform
  190. commandManager.registerAllCommandsForTarget (&keyTarget);
  191. setOpaque (true);
  192. addAndMakeVisible (keyMappingEditor);
  193. addAndMakeVisible (keyTarget);
  194. // add command manager key mappings as a KeyListener to the top-level component
  195. // so it is notified of key presses
  196. getTopLevelComponent()->addKeyListener (commandManager.getKeyMappings());
  197. setSize (500, 500);
  198. Timer::callAfterDelay (300, [this] { keyTarget.grabKeyboardFocus(); }); // ensure that key presses are sent to the KeyPressTarget object
  199. }
  200. void paint (Graphics& g) override
  201. {
  202. g.fillAll (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::windowBackground,
  203. Colour::greyLevel (0.93f)));
  204. }
  205. void resized() override
  206. {
  207. auto bounds = getLocalBounds();
  208. keyTarget .setBounds (bounds.removeFromTop (bounds.getHeight() / 2).reduced (4));
  209. keyMappingEditor.setBounds (bounds.reduced (4));
  210. }
  211. private:
  212. #if JUCE_DEMO_RUNNER
  213. ApplicationCommandManager& commandManager = getGlobalCommandManager();
  214. #else
  215. ApplicationCommandManager commandManager;
  216. #endif
  217. KeyMappingEditorComponent keyMappingEditor { *commandManager.getKeyMappings(), true};
  218. KeyPressTarget keyTarget;
  219. void lookAndFeelChanged() override
  220. {
  221. auto* lf = &LookAndFeel::getDefaultLookAndFeel();
  222. keyMappingEditor.setColours (lf->findColour (KeyMappingEditorComponent::backgroundColourId),
  223. lf->findColour (KeyMappingEditorComponent::textColourId));
  224. }
  225. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (KeyMappingsDemo)
  226. };