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.

265 lines
10KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE examples.
  4. Copyright (c) 2017 - ROLI Ltd.
  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, vs2017, linux_make, androidstudio, xcode_iphone
  27. type: Component
  28. mainClass: KeyMappingsDemo
  29. useLocalCopy: 1
  30. END_JUCE_PIP_METADATA
  31. *******************************************************************************/
  32. #pragma once
  33. #include "../Assets/DemoUtilities.h"
  34. /** A list of the command IDs that this demo can perform. */
  35. enum KeyPressCommandIDs
  36. {
  37. buttonMoveUp = 1,
  38. buttonMoveRight,
  39. buttonMoveDown,
  40. buttonMoveLeft,
  41. nextButtonColour,
  42. previousButtonColour,
  43. nextBackgroundColour,
  44. previousBackgroundColour
  45. };
  46. //==============================================================================
  47. /**
  48. This is a simple target for the key-presses which will live inside the demo component
  49. and contains a button that can be moved around with the arrow keys.
  50. */
  51. class KeyPressTarget : public Component,
  52. public ApplicationCommandTarget
  53. {
  54. public:
  55. KeyPressTarget()
  56. {
  57. Array<Colour> coloursToUse { Colours::darkblue, Colours::darkgrey, Colours::red,
  58. Colours::green, Colours::blue, Colours::hotpink };
  59. colours.addArray (coloursToUse);
  60. addAndMakeVisible (button);
  61. }
  62. //==============================================================================
  63. void resized() override
  64. {
  65. auto bounds = getLocalBounds();
  66. // keep the button on-screen
  67. if (buttonX < -150 || buttonX > bounds.getWidth()
  68. || buttonY < -30 || buttonY > bounds.getHeight())
  69. {
  70. buttonX = bounds.getCentreX() - 75;
  71. buttonY = bounds.getCentreY() - 15;
  72. }
  73. button.setBounds (buttonX, buttonY, 150, 30);
  74. }
  75. void paint (Graphics& g) override
  76. {
  77. g.fillAll (colours.getUnchecked (backgroundColourIndex));
  78. }
  79. //==============================================================================
  80. /** No other command targets in this simple example so just return nullptr. */
  81. ApplicationCommandTarget* getNextCommandTarget() override { return nullptr; }
  82. void getAllCommands (Array<CommandID>& commands) override
  83. {
  84. Array<CommandID> ids { KeyPressCommandIDs::buttonMoveUp, KeyPressCommandIDs::buttonMoveRight,
  85. KeyPressCommandIDs::buttonMoveDown, KeyPressCommandIDs::buttonMoveLeft,
  86. KeyPressCommandIDs::nextButtonColour, KeyPressCommandIDs::previousButtonColour,
  87. KeyPressCommandIDs::nextBackgroundColour, KeyPressCommandIDs::previousBackgroundColour };
  88. commands.addArray (ids);
  89. }
  90. void getCommandInfo (CommandID commandID, ApplicationCommandInfo& result) override
  91. {
  92. switch (commandID)
  93. {
  94. case KeyPressCommandIDs::buttonMoveUp:
  95. result.setInfo ("Move up", "Move the button up", "Button", 0);
  96. result.addDefaultKeypress (KeyPress::upKey, 0);
  97. break;
  98. case KeyPressCommandIDs::buttonMoveRight:
  99. result.setInfo ("Move right", "Move the button right", "Button", 0);
  100. result.addDefaultKeypress (KeyPress::rightKey, 0);
  101. break;
  102. case KeyPressCommandIDs::buttonMoveDown:
  103. result.setInfo ("Move down", "Move the button down", "Button", 0);
  104. result.addDefaultKeypress (KeyPress::downKey, 0);
  105. break;
  106. case KeyPressCommandIDs::buttonMoveLeft:
  107. result.setInfo ("Move left", "Move the button left", "Button", 0);
  108. result.addDefaultKeypress (KeyPress::leftKey, 0);
  109. break;
  110. case KeyPressCommandIDs::nextButtonColour:
  111. result.setInfo ("Next colour", "Change the colour of the button to the next in the list", "Button", 0);
  112. result.addDefaultKeypress (KeyPress::rightKey, ModifierKeys::shiftModifier);
  113. break;
  114. case KeyPressCommandIDs::previousButtonColour:
  115. result.setInfo ("Previous colour", "Change the colour of the button to the previous in the list", "Button", 0);
  116. result.addDefaultKeypress (KeyPress::leftKey, ModifierKeys::shiftModifier);
  117. break;
  118. case KeyPressCommandIDs::nextBackgroundColour:
  119. result.setInfo ("Next colour", "Change the colour of the background to the next in the list", "Other", 0);
  120. result.addDefaultKeypress (KeyPress::rightKey, ModifierKeys::commandModifier);
  121. break;
  122. case KeyPressCommandIDs::previousBackgroundColour:
  123. result.setInfo ("Previous colour", "Change the colour of the background to the previous in the list", "Other", 0);
  124. result.addDefaultKeypress (KeyPress::leftKey, ModifierKeys::commandModifier);
  125. break;
  126. default:
  127. break;
  128. }
  129. }
  130. bool perform (const InvocationInfo& info) override
  131. {
  132. switch (info.commandID)
  133. {
  134. case KeyPressCommandIDs::buttonMoveUp:
  135. buttonY -= 5;
  136. resized();
  137. break;
  138. case KeyPressCommandIDs::buttonMoveRight:
  139. buttonX += 5;
  140. resized();
  141. break;
  142. case KeyPressCommandIDs::buttonMoveDown:
  143. buttonY += 5;
  144. resized();
  145. break;
  146. case KeyPressCommandIDs::buttonMoveLeft:
  147. buttonX -= 5;
  148. resized();
  149. break;
  150. case KeyPressCommandIDs::nextButtonColour:
  151. ++buttonColourIndex %= colours.size();
  152. button.setColour (TextButton::buttonColourId, colours.getUnchecked (buttonColourIndex));
  153. break;
  154. case KeyPressCommandIDs::previousButtonColour:
  155. --buttonColourIndex;
  156. if (buttonColourIndex < 0)
  157. buttonColourIndex = colours.size() - 1;
  158. button.setColour (TextButton::buttonColourId, colours.getUnchecked (buttonColourIndex));
  159. break;
  160. case KeyPressCommandIDs::nextBackgroundColour:
  161. ++backgroundColourIndex %= colours.size();
  162. repaint();
  163. break;
  164. case KeyPressCommandIDs::previousBackgroundColour:
  165. --backgroundColourIndex;
  166. if (backgroundColourIndex < 0)
  167. backgroundColourIndex = colours.size() - 1;
  168. repaint();
  169. break;
  170. default:
  171. return false;
  172. }
  173. return true;
  174. }
  175. private:
  176. TextButton button;
  177. int buttonX = -200, buttonY = -200;
  178. Array<Colour> colours;
  179. int buttonColourIndex = 0;
  180. int backgroundColourIndex = 1;
  181. };
  182. //==============================================================================
  183. class KeyMappingsDemo : public Component
  184. {
  185. public:
  186. KeyMappingsDemo()
  187. {
  188. // register the commands that the target component can perform
  189. commandManager.registerAllCommandsForTarget (&keyTarget);
  190. setOpaque (true);
  191. addAndMakeVisible (keyMappingEditor);
  192. addAndMakeVisible (keyTarget);
  193. // add command manager key mappings as a KeyListener to the top-level component
  194. // so it is notified of key presses
  195. getTopLevelComponent()->addKeyListener (commandManager.getKeyMappings());
  196. setSize (500, 500);
  197. Timer::callAfterDelay (300, [this] { keyTarget.grabKeyboardFocus(); }); // ensure that key presses are sent to the KeyPressTarget object
  198. }
  199. void paint (Graphics& g) override
  200. {
  201. g.fillAll (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::windowBackground,
  202. Colour::greyLevel (0.93f)));
  203. }
  204. void resized() override
  205. {
  206. auto bounds = getLocalBounds();
  207. keyTarget .setBounds (bounds.removeFromTop (bounds.getHeight() / 2).reduced (4));
  208. keyMappingEditor.setBounds (bounds.reduced (4));
  209. }
  210. private:
  211. ApplicationCommandManager commandManager;
  212. KeyMappingEditorComponent keyMappingEditor { *commandManager.getKeyMappings(), true};
  213. KeyPressTarget keyTarget;
  214. void lookAndFeelChanged() override
  215. {
  216. auto* lf = &LookAndFeel::getDefaultLookAndFeel();
  217. keyMappingEditor.setColours (lf->findColour (KeyMappingEditorComponent::backgroundColourId),
  218. lf->findColour (KeyMappingEditorComponent::textColourId));
  219. }
  220. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (KeyMappingsDemo)
  221. };