Vital
Loading...
Searching...
No Matches
open_gl_image_component.h
Go to the documentation of this file.
1#pragma once
2
3#include "open_gl_component.h"
4#include "fonts.h"
5#include "open_gl_image.h"
6
7class SynthSection;
8
19public:
24 OpenGlImageComponent(String name = "");
25
29 virtual ~OpenGlImageComponent() = default;
30
35 virtual void paintBackground(Graphics& g) override {
36 redrawImage(false);
37 }
38
46 virtual void paintToImage(Graphics& g) {
47 Component* component = component_ ? component_ : this;
49 component->paintEntireComponent(g, false);
50 else
51 component->paint(g);
52 }
53
58 virtual void init(OpenGlWrapper& open_gl) override;
59
65 virtual void render(OpenGlWrapper& open_gl, bool animate) override;
66
71 virtual void destroy(OpenGlWrapper& open_gl) override;
72
77 virtual void redrawImage(bool force);
78
83 void setComponent(Component* component) { component_ = component; }
84
89 void setScissor(bool scissor) { image_.setScissor(scissor); }
90
95 void setUseAlpha(bool use_alpha) { image_.setUseAlpha(use_alpha); }
96
101 void setColor(Colour color) { image_.setColor(color); }
102
107 OpenGlImage& image() { return image_; }
108
113 void setActive(bool active) { active_ = active; }
114
119 void setStatic(bool static_image) { static_image_ = static_image; }
120
125 void paintEntireComponent(bool paint_entire_component) { paint_entire_component_ = paint_entire_component; }
126
131 bool isActive() const { return active_; }
132
133protected:
134 Component* component_;
135 bool active_;
138 std::unique_ptr<Image> draw_image_;
140
141 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(OpenGlImageComponent)
142};
143
144
152template <class ComponentType>
153class OpenGlAutoImageComponent : public ComponentType {
154public:
155 using ComponentType::ComponentType;
156
157 virtual void mouseDown(const MouseEvent& e) override {
158 ComponentType::mouseDown(e);
159 redoImage();
160 }
161
162 virtual void mouseUp(const MouseEvent& e) override {
163 ComponentType::mouseUp(e);
164 redoImage();
165 }
166
167 virtual void mouseDoubleClick(const MouseEvent& e) override {
168 ComponentType::mouseDoubleClick(e);
169 redoImage();
170 }
171
172 virtual void mouseEnter(const MouseEvent& e) override {
173 ComponentType::mouseEnter(e);
174 redoImage();
175 }
176
177 virtual void mouseExit(const MouseEvent& e) override {
178 ComponentType::mouseExit(e);
179 redoImage();
180 }
181
182 virtual void mouseWheelMove(const MouseEvent& e, const MouseWheelDetails& wheel) override {
183 ComponentType::mouseWheelMove(e, wheel);
184 redoImage();
185 }
186
192
196 virtual void redoImage() { image_component_.redrawImage(true); }
197
198protected:
200};
201
202
209class OpenGlTextEditor : public OpenGlAutoImageComponent<TextEditor>, public TextEditor::Listener {
210public:
216 monospace_ = false;
218 addListener(this);
219 }
220
226 OpenGlTextEditor(String name, wchar_t password_char) : OpenGlAutoImageComponent(name, password_char) {
227 monospace_ = false;
229 addListener(this);
230 }
231
232 bool keyPressed(const KeyPress& key) override {
233 bool result = TextEditor::keyPressed(key);
234 redoImage();
235 return result;
236 }
237
238 void textEditorTextChanged(TextEditor&) override { redoImage(); }
239 void textEditorFocusLost(TextEditor&) override { redoImage(); }
240
241 virtual void mouseDrag(const MouseEvent& e) override {
242 TextEditor::mouseDrag(e);
243 redoImage();
244 }
245
249 void applyFont() {
250 Font font;
251 if (monospace_)
252 font = Fonts::instance()->monospace().withPointHeight(getHeight() / 2.0f);
253 else
254 font = Fonts::instance()->proportional_light().withPointHeight(getHeight() / 2.0f);
255
256 applyFontToAllText(font);
257 redoImage();
258 }
259
260 void visibilityChanged() override {
261 TextEditor::visibilityChanged();
262
263 if (isVisible() && !isMultiLine())
264 applyFont();
265 }
266
267 void resized() override {
268 TextEditor::resized();
269 if (isMultiLine()) {
271 setIndents(indent, indent);
272 return;
273 }
274
275 if (monospace_)
276 setIndents(getHeight() * 0.2, getHeight() * 0.17);
277 else
278 setIndents(getHeight() * 0.2, getHeight() * 0.15);
279 if (isVisible())
280 applyFont();
281 }
282
287 monospace_ = true;
288 }
289
290private:
291 bool monospace_;
292
293 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(OpenGlTextEditor)
294};
295
296
302public:
314
320 PlainTextComponent(String name, String text) : OpenGlImageComponent(name), text_(std::move(text)),
321 text_size_(1.0f), font_type_(kRegular),
322 justification_(Justification::centred),
323 buffer_(0) {
324 setInterceptsMouseClicks(false, false);
325 }
326
327 void resized() override {
329 redrawImage(true);
330 }
331
336 void setText(String text) {
337 if (text_ == text)
338 return;
339
340 text_ = text;
341 redrawImage(true);
342 }
343
348 String getText() const { return text_; }
349
350 void paintToImage(Graphics& g) override {
351 g.setColour(Colours::white);
352
353 if (font_type_ == kTitle)
354 g.setFont(Fonts::instance()->proportional_title().withPointHeight(text_size_));
355 else if (font_type_ == kLight)
356 g.setFont(Fonts::instance()->proportional_light().withPointHeight(text_size_));
357 else if (font_type_ == kRegular)
358 g.setFont(Fonts::instance()->proportional_regular().withPointHeight(text_size_));
359 else
360 g.setFont(Fonts::instance()->monospace().withPointHeight(text_size_));
361
362 Component* component = component_ ? component_ : this;
363
364 g.drawFittedText(text_, buffer_, 0, component->getWidth() - 2 * buffer_,
365 component->getHeight(), justification_, false);
366 }
367
372 void setTextSize(float size) {
373 text_size_ = size;
374 redrawImage(true);
375 }
376
381 void setFontType(FontType font_type) {
382 font_type_ = font_type;
383 }
384
389 void setJustification(Justification justification) {
390 justification_ = justification;
391 }
392
397 void setBuffer(int buffer) { buffer_ = buffer; }
398
399private:
400 String text_;
401 float text_size_;
402 FontType font_type_;
403 Justification justification_;
404 int buffer_;
405
406 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(PlainTextComponent)
407};
408
409
418public:
423 PlainShapeComponent(String name) : OpenGlImageComponent(name), justification_(Justification::centred) {
424 setInterceptsMouseClicks(false, false);
425 }
426
427 void paintToImage(Graphics& g) override {
428 Component* component = component_ ? component_ : this;
429 Rectangle<float> bounds = component->getLocalBounds().toFloat();
430 Path shape = shape_;
431 shape.applyTransform(shape.getTransformToScaleToFit(bounds, true, justification_));
432
433 g.setColour(Colours::white);
434 g.fillPath(shape);
435 }
436
441 void setShape(Path shape) {
442 shape_ = shape;
443 redrawImage(true);
444 }
445
450 void setJustification(Justification justification) { justification_ = justification; }
451
452private:
453 Path shape_;
454 Justification justification_;
455
456 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(PlainShapeComponent)
457};
Font & monospace()
Returns a reference to the monospace font.
Definition fonts.h:46
Font & proportional_light()
Returns a reference to the proportional light font.
Definition fonts.h:28
static Fonts * instance()
Gets the singleton instance of the Fonts class.
Definition fonts.h:52
A template class that wraps a given ComponentType and automatically redraws an OpenGlImageComponent o...
Definition open_gl_image_component.h:153
virtual void mouseEnter(const MouseEvent &e) override
Definition open_gl_image_component.h:172
virtual void mouseDoubleClick(const MouseEvent &e) override
Definition open_gl_image_component.h:167
virtual void mouseDown(const MouseEvent &e) override
Definition open_gl_image_component.h:157
virtual void redoImage()
Redraws the image after a state change.
Definition open_gl_image_component.h:196
virtual void mouseExit(const MouseEvent &e) override
Definition open_gl_image_component.h:177
virtual void mouseWheelMove(const MouseEvent &e, const MouseWheelDetails &wheel) override
Definition open_gl_image_component.h:182
OpenGlImageComponent * getImageComponent()
Gets the underlying OpenGlImageComponent.
Definition open_gl_image_component.h:191
OpenGlImageComponent image_component_
The OpenGlImageComponent for rendering this component.
Definition open_gl_image_component.h:199
virtual void mouseUp(const MouseEvent &e) override
Definition open_gl_image_component.h:162
A base component class that integrates JUCE's Component with OpenGL rendering.
Definition open_gl_component.h:20
virtual void resized() override
Called when the component is resized.
Definition open_gl_component.cpp:121
float findValue(Skin::ValueId value_id)
Finds a float value from the skin associated with this component's parent.
Definition open_gl_component.cpp:173
A component that uses OpenGL to render a cached image of a JUCE component or custom drawing.
Definition open_gl_image_component.h:18
void setStatic(bool static_image)
Sets whether the image should be treated as static (not redrawn unless forced).
Definition open_gl_image_component.h:119
void setComponent(Component *component)
Sets the component to be drawn into the OpenGL image. If not set, uses this component.
Definition open_gl_image_component.h:83
bool paint_entire_component_
If true, paint entire component hierarchy to image.
Definition open_gl_image_component.h:137
void setActive(bool active)
Sets whether this component is active (rendered) or not.
Definition open_gl_image_component.h:113
OpenGlImage image_
The OpenGlImage used to upload and draw the cached image.
Definition open_gl_image_component.h:139
bool static_image_
Whether the image is static or updated on events.
Definition open_gl_image_component.h:136
virtual void init(OpenGlWrapper &open_gl) override
Initializes any OpenGL resources for rendering this component.
Definition open_gl_image_component.cpp:57
std::unique_ptr< Image > draw_image_
The cached image that stores the drawn component.
Definition open_gl_image_component.h:138
bool isActive() const
Checks if this component is currently active.
Definition open_gl_image_component.h:131
virtual void paintToImage(Graphics &g)
Renders the associated component (or itself) into the provided Graphics context.
Definition open_gl_image_component.h:46
virtual ~OpenGlImageComponent()=default
Destructor.
void paintEntireComponent(bool paint_entire_component)
Controls whether paintToImage should paint the entire component hierarchy or just itself.
Definition open_gl_image_component.h:125
virtual void destroy(OpenGlWrapper &open_gl) override
Destroys OpenGL-related resources used by this component.
Definition open_gl_image_component.cpp:69
Component * component_
The component being drawn into the image (if any).
Definition open_gl_image_component.h:134
virtual void paintBackground(Graphics &g) override
Paints the background by redrawing the image if needed.
Definition open_gl_image_component.h:35
OpenGlImageComponent(String name="")
Constructs an OpenGlImageComponent.
Definition open_gl_image_component.cpp:3
void setScissor(bool scissor)
Enables or disables scissor testing when drawing the image.
Definition open_gl_image_component.h:89
void setUseAlpha(bool use_alpha)
Enables or disables alpha blending for the image.
Definition open_gl_image_component.h:95
virtual void redrawImage(bool force)
Redraws the image if necessary, creating or updating the internal Image.
Definition open_gl_image_component.cpp:16
void setColor(Colour color)
Sets a color tint for the image.
Definition open_gl_image_component.h:101
virtual void render(OpenGlWrapper &open_gl, bool animate) override
Renders the image using OpenGL.
Definition open_gl_image_component.cpp:61
bool active_
Whether this component is active and should render.
Definition open_gl_image_component.h:135
OpenGlImage & image()
Provides access to the underlying OpenGlImage.
Definition open_gl_image_component.h:107
A utility class for rendering a single image using OpenGL.
Definition open_gl_image.h:16
void setScissor(bool scissor)
Enables or disables scissor test when drawing the image.
Definition open_gl_image.h:156
void setUseAlpha(bool use_alpha)
Enables or disables alpha blending.
Definition open_gl_image.h:150
void setColor(Colour color)
Sets the color tint applied to the image.
Definition open_gl_image.h:92
A text editor that uses an OpenGlImageComponent for rendering and updates the image on text changes.
Definition open_gl_image_component.h:209
void resized() override
Definition open_gl_image_component.h:267
OpenGlTextEditor(String name, wchar_t password_char)
Constructs an OpenGlTextEditor with a given name and password character.
Definition open_gl_image_component.h:226
virtual void mouseDrag(const MouseEvent &e) override
Definition open_gl_image_component.h:241
void applyFont()
Applies the appropriate font based on monospace setting and component size.
Definition open_gl_image_component.h:249
OpenGlTextEditor(String name)
Constructs an OpenGlTextEditor with a given name.
Definition open_gl_image_component.h:215
void setMonospace()
Sets the text editor to use a monospace font.
Definition open_gl_image_component.h:286
void visibilityChanged() override
Definition open_gl_image_component.h:260
bool keyPressed(const KeyPress &key) override
Definition open_gl_image_component.h:232
void textEditorFocusLost(TextEditor &) override
Definition open_gl_image_component.h:239
void textEditorTextChanged(TextEditor &) override
Definition open_gl_image_component.h:238
A component that draws a shape into an OpenGlImageComponent.
Definition open_gl_image_component.h:417
void setJustification(Justification justification)
Sets the justification used when scaling the shape.
Definition open_gl_image_component.h:450
void paintToImage(Graphics &g) override
Renders the associated component (or itself) into the provided Graphics context.
Definition open_gl_image_component.h:427
PlainShapeComponent(String name)
Constructs a PlainShapeComponent with a given name.
Definition open_gl_image_component.h:423
void setShape(Path shape)
Sets the shape to be drawn.
Definition open_gl_image_component.h:441
A text component rendered into an OpenGlImageComponent with configurable font and justification.
Definition open_gl_image_component.h:301
void resized() override
Called when the component is resized.
Definition open_gl_image_component.h:327
FontType
Different font types available for text rendering.
Definition open_gl_image_component.h:307
@ kLight
Definition open_gl_image_component.h:309
@ kRegular
Definition open_gl_image_component.h:310
@ kMono
Definition open_gl_image_component.h:311
@ kTitle
Definition open_gl_image_component.h:308
@ kNumFontTypes
Definition open_gl_image_component.h:312
void setText(String text)
Sets the displayed text and redraws the image.
Definition open_gl_image_component.h:336
void setTextSize(float size)
Sets the size of the text in points.
Definition open_gl_image_component.h:372
void setBuffer(int buffer)
Sets a buffer (padding) around the text.
Definition open_gl_image_component.h:397
PlainTextComponent(String name, String text)
Constructs a PlainTextComponent.
Definition open_gl_image_component.h:320
void paintToImage(Graphics &g) override
Renders the associated component (or itself) into the provided Graphics context.
Definition open_gl_image_component.h:350
void setJustification(Justification justification)
Sets the text justification (e.g., centered, left, right).
Definition open_gl_image_component.h:389
String getText() const
Gets the current displayed text.
Definition open_gl_image_component.h:348
void setFontType(FontType font_type)
Sets the font type (Title, Light, Regular, Mono).
Definition open_gl_image_component.h:381
@ kLabelBackgroundRounding
Definition skin.h:74
Base class for all synthesizer sections, providing UI layout, painting, and interaction logic.
Definition synth_section.h:193
A helper struct containing references to OpenGL context, shaders, and display scale.
Definition shaders.h:174