|
| 1 | +/* |
| 2 | + ============================================================================== |
| 3 | +
|
| 4 | + This file is part of the YUP library. |
| 5 | + Copyright (c) 2025 - [email protected] |
| 6 | +
|
| 7 | + YUP is an open source library subject to open-source licensing. |
| 8 | +
|
| 9 | + The code included in this file is provided under the terms of the ISC license |
| 10 | + http://www.isc.org/downloads/software-support-policy/isc-license. Permission |
| 11 | + to use, copy, modify, and/or distribute this software for any purpose with or |
| 12 | + without fee is hereby granted provided that the above copyright notice and |
| 13 | + this permission notice appear in all copies. |
| 14 | +
|
| 15 | + YUP IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER |
| 16 | + EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE |
| 17 | + DISCLAIMED. |
| 18 | +
|
| 19 | + ============================================================================== |
| 20 | +*/ |
| 21 | + |
| 22 | +#pragma once |
| 23 | + |
| 24 | +class SliderDemo : public yup::Component |
| 25 | +{ |
| 26 | +public: |
| 27 | + SliderDemo() |
| 28 | + : Component ("SliderDemo") |
| 29 | + { |
| 30 | + setupSliders(); |
| 31 | + setupLabels(); |
| 32 | + } |
| 33 | + |
| 34 | +private: |
| 35 | + void setupSliders() |
| 36 | + { |
| 37 | + // Horizontal Linear Slider |
| 38 | + horizontalSlider = std::make_unique<yup::Slider>(yup::Slider::LinearHorizontal); |
| 39 | + horizontalSlider->setRange(0.0, 100.0); |
| 40 | + horizontalSlider->setValue(50.0); |
| 41 | + horizontalSlider->onValueChanged = [this](double value) { |
| 42 | + horizontalLabel->setText("Horizontal: " + yup::String(value, 1), yup::dontSendNotification); |
| 43 | + }; |
| 44 | + addAndMakeVisible(horizontalSlider.get()); |
| 45 | + |
| 46 | + // Vertical Linear Slider |
| 47 | + verticalSlider = std::make_unique<yup::Slider>(yup::Slider::LinearVertical); |
| 48 | + verticalSlider->setRange(0.0, 100.0); |
| 49 | + verticalSlider->setValue(30.0); |
| 50 | + verticalSlider->onValueChanged = [this](double value) { |
| 51 | + verticalLabel->setText("Vertical: " + yup::String(value, 1), yup::dontSendNotification); |
| 52 | + }; |
| 53 | + addAndMakeVisible(verticalSlider.get()); |
| 54 | + |
| 55 | + // Rotary Slider (Horizontal Drag) |
| 56 | + rotarySlider = std::make_unique<yup::Slider>(yup::Slider::RotaryHorizontalDrag); |
| 57 | + rotarySlider->setRange(0.0, 100.0); |
| 58 | + rotarySlider->setValue(70.0); |
| 59 | + rotarySlider->onValueChanged = [this](double value) { |
| 60 | + rotaryLabel->setText("Rotary: " + yup::String(value, 1), yup::dontSendNotification); |
| 61 | + }; |
| 62 | + addAndMakeVisible(rotarySlider.get()); |
| 63 | + |
| 64 | + // Bar Horizontal Slider |
| 65 | + barHorizontalSlider = std::make_unique<yup::Slider>(yup::Slider::LinearBarHorizontal); |
| 66 | + barHorizontalSlider->setRange(0.0, 100.0); |
| 67 | + barHorizontalSlider->setValue(75.0); |
| 68 | + barHorizontalSlider->onValueChanged = [this](double value) { |
| 69 | + barHorizontalLabel->setText("Bar H: " + yup::String(value, 0) + "%", yup::dontSendNotification); |
| 70 | + }; |
| 71 | + addAndMakeVisible(barHorizontalSlider.get()); |
| 72 | + |
| 73 | + // Bar Vertical Slider |
| 74 | + barVerticalSlider = std::make_unique<yup::Slider>(yup::Slider::LinearBarVertical); |
| 75 | + barVerticalSlider->setRange(0.0, 10.0); |
| 76 | + barVerticalSlider->setValue(6.0); |
| 77 | + barVerticalSlider->onValueChanged = [this](double value) { |
| 78 | + barVerticalLabel->setText("Bar V: " + yup::String(value, 1), yup::dontSendNotification); |
| 79 | + }; |
| 80 | + addAndMakeVisible(barVerticalSlider.get()); |
| 81 | + |
| 82 | + // Two Value Horizontal Slider |
| 83 | + twoValueSlider = std::make_unique<yup::Slider>(yup::Slider::TwoValueHorizontal); |
| 84 | + twoValueSlider->setRange(0.0, 100.0); |
| 85 | + twoValueSlider->setMinValue(25.0); |
| 86 | + twoValueSlider->setMaxValue(75.0); |
| 87 | + twoValueSlider->onValueChanged = [this](double) { |
| 88 | + twoValueLabel->setText("Range: " + yup::String(twoValueSlider->getMinValue(), 0) + |
| 89 | + "-" + yup::String(twoValueSlider->getMaxValue(), 0), yup::dontSendNotification); |
| 90 | + }; |
| 91 | + addAndMakeVisible(twoValueSlider.get()); |
| 92 | + } |
| 93 | + |
| 94 | + void setupLabels() |
| 95 | + { |
| 96 | + // Title |
| 97 | + titleLabel = std::make_unique<yup::Label>("title"); |
| 98 | + titleLabel->setText("YUP Slider Demo", yup::dontSendNotification); |
| 99 | + addAndMakeVisible(titleLabel.get()); |
| 100 | + |
| 101 | + // Value labels |
| 102 | + horizontalLabel = std::make_unique<yup::Label>("value1"); |
| 103 | + horizontalLabel->setText("Horizontal: 50.0", yup::dontSendNotification); |
| 104 | + addAndMakeVisible(horizontalLabel.get()); |
| 105 | + |
| 106 | + verticalLabel = std::make_unique<yup::Label>("value2"); |
| 107 | + verticalLabel->setText("Vertical: 30.0", yup::dontSendNotification); |
| 108 | + addAndMakeVisible(verticalLabel.get()); |
| 109 | + |
| 110 | + rotaryLabel = std::make_unique<yup::Label>("value3"); |
| 111 | + rotaryLabel->setText("Rotary: 70.0", yup::dontSendNotification); |
| 112 | + addAndMakeVisible(rotaryLabel.get()); |
| 113 | + |
| 114 | + barHorizontalLabel = std::make_unique<yup::Label>("value4"); |
| 115 | + barHorizontalLabel->setText("Bar H: 75%", yup::dontSendNotification); |
| 116 | + addAndMakeVisible(barHorizontalLabel.get()); |
| 117 | + |
| 118 | + barVerticalLabel = std::make_unique<yup::Label>("value5"); |
| 119 | + barVerticalLabel->setText("Bar V: 6.0", yup::dontSendNotification); |
| 120 | + addAndMakeVisible(barVerticalLabel.get()); |
| 121 | + |
| 122 | + twoValueLabel = std::make_unique<yup::Label>("value6"); |
| 123 | + twoValueLabel->setText("Range: 25-75", yup::dontSendNotification); |
| 124 | + addAndMakeVisible(twoValueLabel.get()); |
| 125 | + } |
| 126 | + |
| 127 | + void resized() override |
| 128 | + { |
| 129 | + auto bounds = getLocalBounds(); |
| 130 | + auto margin = 20; |
| 131 | + auto sliderHeight = 60; |
| 132 | + auto labelHeight = 25; |
| 133 | + auto spacing = 10; |
| 134 | + |
| 135 | + int y = margin; |
| 136 | + |
| 137 | + // Title |
| 138 | + titleLabel->setBounds(yup::Rectangle<float>( |
| 139 | + static_cast<float>(margin), |
| 140 | + static_cast<float>(y), |
| 141 | + static_cast<float>(bounds.getWidth() - 2 * margin), |
| 142 | + 30.0f)); |
| 143 | + y += 40; |
| 144 | + |
| 145 | + // Layout in a 2x3 grid |
| 146 | + auto sliderWidth = (bounds.getWidth() - 3 * margin) / 2; |
| 147 | + auto columnHeight = sliderHeight + labelHeight + spacing; |
| 148 | + |
| 149 | + // Left column |
| 150 | + horizontalSlider->setBounds(yup::Rectangle<float>( |
| 151 | + static_cast<float>(margin), |
| 152 | + static_cast<float>(y), |
| 153 | + static_cast<float>(sliderWidth), |
| 154 | + static_cast<float>(sliderHeight))); |
| 155 | + horizontalLabel->setBounds(yup::Rectangle<float>( |
| 156 | + static_cast<float>(margin), |
| 157 | + static_cast<float>(y + sliderHeight + 5), |
| 158 | + static_cast<float>(sliderWidth), |
| 159 | + static_cast<float>(labelHeight))); |
| 160 | + |
| 161 | + barHorizontalSlider->setBounds(yup::Rectangle<float>( |
| 162 | + static_cast<float>(margin), |
| 163 | + static_cast<float>(y + columnHeight), |
| 164 | + static_cast<float>(sliderWidth), |
| 165 | + static_cast<float>(sliderHeight))); |
| 166 | + barHorizontalLabel->setBounds(yup::Rectangle<float>( |
| 167 | + static_cast<float>(margin), |
| 168 | + static_cast<float>(y + columnHeight + sliderHeight + 5), |
| 169 | + static_cast<float>(sliderWidth), |
| 170 | + static_cast<float>(labelHeight))); |
| 171 | + |
| 172 | + twoValueSlider->setBounds(yup::Rectangle<float>( |
| 173 | + static_cast<float>(margin), |
| 174 | + static_cast<float>(y + 2 * columnHeight), |
| 175 | + static_cast<float>(sliderWidth), |
| 176 | + static_cast<float>(sliderHeight))); |
| 177 | + twoValueLabel->setBounds(yup::Rectangle<float>( |
| 178 | + static_cast<float>(margin), |
| 179 | + static_cast<float>(y + 2 * columnHeight + sliderHeight + 5), |
| 180 | + static_cast<float>(sliderWidth), |
| 181 | + static_cast<float>(labelHeight))); |
| 182 | + |
| 183 | + // Right column |
| 184 | + auto rightX = margin + sliderWidth + margin; |
| 185 | + |
| 186 | + verticalSlider->setBounds(yup::Rectangle<float>( |
| 187 | + static_cast<float>(rightX), |
| 188 | + static_cast<float>(y), |
| 189 | + 80.0f, |
| 190 | + static_cast<float>(columnHeight))); |
| 191 | + verticalLabel->setBounds(yup::Rectangle<float>( |
| 192 | + static_cast<float>(rightX + 90), |
| 193 | + static_cast<float>(y), |
| 194 | + static_cast<float>(sliderWidth - 90), |
| 195 | + static_cast<float>(labelHeight))); |
| 196 | + |
| 197 | + rotarySlider->setBounds(yup::Rectangle<float>( |
| 198 | + static_cast<float>(rightX), |
| 199 | + static_cast<float>(y + columnHeight), |
| 200 | + 80.0f, |
| 201 | + 80.0f)); |
| 202 | + rotaryLabel->setBounds(yup::Rectangle<float>( |
| 203 | + static_cast<float>(rightX + 90), |
| 204 | + static_cast<float>(y + columnHeight), |
| 205 | + static_cast<float>(sliderWidth - 90), |
| 206 | + static_cast<float>(labelHeight))); |
| 207 | + |
| 208 | + barVerticalSlider->setBounds(yup::Rectangle<float>( |
| 209 | + static_cast<float>(rightX), |
| 210 | + static_cast<float>(y + 2 * columnHeight), |
| 211 | + 60.0f, |
| 212 | + static_cast<float>(columnHeight))); |
| 213 | + barVerticalLabel->setBounds(yup::Rectangle<float>( |
| 214 | + static_cast<float>(rightX + 70), |
| 215 | + static_cast<float>(y + 2 * columnHeight), |
| 216 | + static_cast<float>(sliderWidth - 70), |
| 217 | + static_cast<float>(labelHeight))); |
| 218 | + } |
| 219 | + |
| 220 | + void paint(yup::Graphics& g) override |
| 221 | + { |
| 222 | + g.setFillColor(yup::Colors::dimgray); |
| 223 | + g.fillAll(); |
| 224 | + |
| 225 | + // Draw section dividers |
| 226 | + g.setStrokeColor(yup::Colors::gray.withAlpha(0.3f)); |
| 227 | + g.setStrokeWidth(1.0f); |
| 228 | + |
| 229 | + auto bounds = getLocalBounds(); |
| 230 | + auto margin = 20; |
| 231 | + |
| 232 | + // Horizontal line under title |
| 233 | + g.strokeLine(static_cast<float>(margin), 70.0f, static_cast<float>(bounds.getWidth() - margin), 70.0f); |
| 234 | + |
| 235 | + // Vertical line separating columns |
| 236 | + auto centerX = bounds.getWidth() / 2; |
| 237 | + g.strokeLine(static_cast<float>(centerX), 80.0f, static_cast<float>(centerX), static_cast<float>(bounds.getHeight() - margin)); |
| 238 | + } |
| 239 | + |
| 240 | +private: |
| 241 | + // Title |
| 242 | + std::unique_ptr<yup::Label> titleLabel; |
| 243 | + |
| 244 | + // Sliders |
| 245 | + std::unique_ptr<yup::Slider> horizontalSlider; |
| 246 | + std::unique_ptr<yup::Slider> verticalSlider; |
| 247 | + std::unique_ptr<yup::Slider> rotarySlider; |
| 248 | + std::unique_ptr<yup::Slider> barHorizontalSlider; |
| 249 | + std::unique_ptr<yup::Slider> barVerticalSlider; |
| 250 | + std::unique_ptr<yup::Slider> twoValueSlider; |
| 251 | + |
| 252 | + // Labels |
| 253 | + std::unique_ptr<yup::Label> horizontalLabel; |
| 254 | + std::unique_ptr<yup::Label> verticalLabel; |
| 255 | + std::unique_ptr<yup::Label> rotaryLabel; |
| 256 | + std::unique_ptr<yup::Label> barHorizontalLabel; |
| 257 | + std::unique_ptr<yup::Label> barVerticalLabel; |
| 258 | + std::unique_ptr<yup::Label> twoValueLabel; |
| 259 | + |
| 260 | + YUP_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(SliderDemo) |
| 261 | +}; |
0 commit comments