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.

161 lines
4.9KB

  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: ComponentDemo
  20. version: 1.0.0
  21. vendor: JUCE
  22. website: http://juce.com
  23. description: Displays a grid of lights.
  24. dependencies: juce_core, juce_data_structures, juce_events, juce_graphics,
  25. juce_gui_basics
  26. exporters: xcode_mac, vs2017
  27. type: Component
  28. mainClass: ComponentDemo
  29. useLocalCopy: 1
  30. END_JUCE_PIP_METADATA
  31. *******************************************************************************/
  32. #pragma once
  33. //==============================================================================
  34. /**
  35. This class represents one of the individual lights in our grid.
  36. */
  37. class ToggleLightComponent : public Component
  38. {
  39. public:
  40. ToggleLightComponent() {}
  41. void paint (Graphics& g) override
  42. {
  43. // Only shows the red ellipse when the button is on.
  44. if (isOn)
  45. {
  46. g.setColour (getLookAndFeel().findColour (Slider::thumbColourId));
  47. g.fillEllipse (getLocalBounds().toFloat());
  48. }
  49. }
  50. void mouseEnter (const MouseEvent&) override
  51. {
  52. // button toggles state on mouse over.
  53. isOn = ! isOn;
  54. repaint();
  55. }
  56. private:
  57. // member variables for the Component
  58. bool isOn = false;
  59. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ToggleLightComponent)
  60. };
  61. //==============================================================================
  62. /**
  63. This is the parent class that holds multiple ToggleLightComponents in a grid.
  64. */
  65. class ToggleLightGridComponent : public Component
  66. {
  67. public:
  68. ToggleLightGridComponent()
  69. {
  70. // Adds the child light components and makes them visible
  71. // within this component.
  72. // (they currently rely on having a default constructor
  73. // so they dont have to be individually initialised)
  74. for (auto i = 0; i < numX * numY; ++i)
  75. addAndMakeVisible (toggleLights[i]);
  76. }
  77. void resized() override
  78. {
  79. // This creates a grid of rectangles to use as the bounds
  80. // for all of our lights. The grid is defined with the
  81. // width and height of this component.
  82. auto stepX = getWidth() / numX;
  83. auto stepY = getHeight() / numY;
  84. for (auto x = 0; x < numX; ++x)
  85. {
  86. for (auto y = 0; y < numY; ++y)
  87. {
  88. // creates the rectangle (x, y, width, height)
  89. Rectangle<int> elementBounds (x * stepX, y * stepY, stepX, stepY);
  90. // set the size and position of the Toggle light to this rectangle.
  91. toggleLights[x + numX * y].setBounds (elementBounds);
  92. }
  93. }
  94. }
  95. private:
  96. // member variables for the Component
  97. static const int numX = 20;
  98. static const int numY = 20;
  99. ToggleLightComponent toggleLights [numX * numY];
  100. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ToggleLightGridComponent)
  101. };
  102. //==============================================================================
  103. /*
  104. This component lives inside our window, and this is where you should put all
  105. your controls and content.
  106. */
  107. class ComponentDemo : public Component
  108. {
  109. public:
  110. //==============================================================================
  111. ComponentDemo()
  112. {
  113. // add the light grid to out main component.
  114. addAndMakeVisible (lightGrid);
  115. setSize (600, 600);
  116. }
  117. void paint (Graphics&) override {}
  118. void resized() override
  119. {
  120. // set the size of the grid to fill the whole window.
  121. lightGrid.setBounds (getLocalBounds());
  122. }
  123. private:
  124. //==============================================================================
  125. ToggleLightGridComponent lightGrid;
  126. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ComponentDemo)
  127. };