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.

163 lines
5.0KB

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