Vital
Loading...
Searching...
No Matches
open_gl_component.cpp
Go to the documentation of this file.
1#include "open_gl_component.h"
2
4#include "full_interface.h"
5#include "skin.h"
6
7namespace {
8 Rectangle<int> getGlobalBounds(Component* component, Rectangle<int> bounds) {
9 Component* parent = component->getParentComponent();
10 // Traverse up the hierarchy until we reach a FullInterface or the top-level
11 while (parent && dynamic_cast<FullInterface*>(component) == nullptr) {
12 bounds = bounds + component->getPosition();
13 component = parent;
14 parent = component->getParentComponent();
15 }
16
17 return bounds;
18 }
19
20 Rectangle<int> getGlobalVisibleBounds(Component* component, Rectangle<int> visible_bounds) {
21 Component* parent = component->getParentComponent();
22 // Intersect visible bounds with each parent's local bounds up to a FullInterface
23 while (parent && dynamic_cast<FullInterface*>(parent) == nullptr) {
24 visible_bounds = visible_bounds + component->getPosition();
25 parent->getLocalBounds().intersectRectangle(visible_bounds);
26 component = parent;
27 parent = component->getParentComponent();
28 }
29
30 return visible_bounds + component->getPosition();
31 }
32}
33
34OpenGlComponent::OpenGlComponent(String name) : Component(name), only_bottom_corners_(false),
35 parent_(nullptr), skin_override_(Skin::kNone),
36 num_voices_readout_(nullptr) {
37 background_color_ = Colours::transparentBlack;
38}
39
41
42bool OpenGlComponent::setViewPort(Component* component, Rectangle<int> bounds, OpenGlWrapper& open_gl) {
43 FullInterface* top_level = component->findParentComponentOfClass<FullInterface>();
44 float scale = open_gl.display_scale;
45 float resize_scale = top_level->getResizingScale();
46 float render_scale = 1.0f;
47 if (scale == 1.0f)
48 render_scale *= open_gl.context.getRenderingScale();
49
50 float gl_scale = render_scale * resize_scale;
51
52 Rectangle<int> top_level_bounds = top_level->getBounds();
53 Rectangle<int> global_bounds = getGlobalBounds(component, bounds);
54 Rectangle<int> visible_bounds = getGlobalVisibleBounds(component, bounds);
55
56 glViewport(gl_scale * global_bounds.getX(),
57 std::ceil(scale * render_scale * top_level_bounds.getHeight()) - gl_scale * global_bounds.getBottom(),
58 gl_scale * global_bounds.getWidth(), gl_scale * global_bounds.getHeight());
59
60 if (visible_bounds.getWidth() <= 0 || visible_bounds.getHeight() <= 0)
61 return false;
62
63 glScissor(gl_scale * visible_bounds.getX(),
64 std::ceil(scale * render_scale * top_level_bounds.getHeight()) - gl_scale * visible_bounds.getBottom(),
65 gl_scale * visible_bounds.getWidth(), gl_scale * visible_bounds.getHeight());
66
67 return true;
68}
69
70bool OpenGlComponent::setViewPort(Component* component, OpenGlWrapper& open_gl) {
71 return setViewPort(component, component->getLocalBounds(), open_gl);
72}
73
75 return setViewPort(this, open_gl);
76}
77
78void OpenGlComponent::setScissor(Component* component, OpenGlWrapper& open_gl) {
79 setScissorBounds(component, component->getLocalBounds(), open_gl);
80}
81
82void OpenGlComponent::setScissorBounds(Component* component, Rectangle<int> bounds, OpenGlWrapper& open_gl) {
83 if (component == nullptr)
84 return;
85
86 FullInterface* top_level = component->findParentComponentOfClass<FullInterface>();
87 float scale = open_gl.display_scale;
88 float resize_scale = top_level->getResizingScale();
89 float render_scale = 1.0f;
90 if (scale == 1.0f)
91 render_scale *= open_gl.context.getRenderingScale();
92
93 float gl_scale = render_scale * resize_scale;
94
95 Rectangle<int> top_level_bounds = top_level->getBounds();
96 Rectangle<int> visible_bounds = getGlobalVisibleBounds(component, bounds);
97
98 if (visible_bounds.getHeight() > 0 && visible_bounds.getWidth() > 0) {
99 glScissor(gl_scale * visible_bounds.getX(),
100 std::ceil(scale * render_scale * top_level_bounds.getHeight()) - gl_scale * visible_bounds.getBottom(),
101 gl_scale * visible_bounds.getWidth(), gl_scale * visible_bounds.getHeight());
102 }
103}
104
106 if (!isVisible())
107 return;
108
109 g.fillAll(findColour(Skin::kWidgetBackground, true));
110}
111
113 if (!isShowing())
114 return;
115
116 FullInterface* parent = findParentComponentOfClass<FullInterface>();
117 if (parent)
118 parent->repaintOpenGlBackground(this);
119}
120
122 if (corners_)
123 corners_->setBounds(getLocalBounds());
124
125 body_color_ = findColour(Skin::kBody, true);
126}
127
129 if (num_voices_readout_ == nullptr) {
130 SynthGuiInterface* parent = findParentComponentOfClass<SynthGuiInterface>();
131 if (parent)
132 num_voices_readout_ = parent->getSynth()->getStatusOutput("num_voices");
133 }
134
135 Component::parentHierarchyChanged();
136}
137
139 corners_ = std::make_unique<OpenGlCorners>();
140 addAndMakeVisible(corners_.get());
141}
142
147
149 if (corners_)
150 corners_->init(open_gl);
151}
152
153void OpenGlComponent::renderCorners(OpenGlWrapper& open_gl, bool animate, Colour color, float rounding) {
154 if (corners_) {
156 corners_->setBottomCorners(getLocalBounds(), rounding);
157 else
158 corners_->setCorners(getLocalBounds(), rounding);
159 corners_->setColor(color);
160 corners_->render(open_gl, animate);
161 }
162}
163
167
169 if (corners_)
170 corners_->destroy(open_gl);
171}
172
174 if (parent_)
175 return parent_->findValue(value_id);
176
177 VITAL_ASSERT(false);
178 return 0.0f;
179}
The main GUI container for the entire synthesizer interface.
Definition full_interface.h:61
void repaintOpenGlBackground(OpenGlComponent *component)
Repaints the background that shows behind OpenGL components.
Definition full_interface.cpp:338
float getResizingScale() const
Returns the scale factor for resizing operations.
Definition full_interface.h:492
bool only_bottom_corners_
Flag to round only the bottom corners.
Definition open_gl_component.h:253
static bool setViewPort(Component *component, Rectangle< int > bounds, OpenGlWrapper &open_gl)
Sets the OpenGL viewport to match a specified rectangle within a component.
Definition open_gl_component.cpp:42
void addRoundedCorners()
Adds rounded corners to the component's edges.
Definition open_gl_component.cpp:138
virtual void resized() override
Called when the component is resized.
Definition open_gl_component.cpp:121
void addBottomRoundedCorners()
Adds rounded corners only at the bottom of the component.
Definition open_gl_component.cpp:143
const vital::StatusOutput * num_voices_readout_
StatusOutput for voice count lookups.
Definition open_gl_component.h:258
void renderCorners(OpenGlWrapper &open_gl, bool animate, Colour color, float rounding)
Renders the corner shapes using the given color and rounding amount.
Definition open_gl_component.cpp:153
static void setScissorBounds(Component *component, Rectangle< int > bounds, OpenGlWrapper &open_gl)
Sets the OpenGL scissor region to a specified rectangle within a component.
Definition open_gl_component.cpp:82
static void setScissor(Component *component, OpenGlWrapper &open_gl)
Sets the OpenGL scissor region to the entire component's local bounds.
Definition open_gl_component.cpp:78
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
virtual void destroy(OpenGlWrapper &open_gl)
Destroys any OpenGL-specific resources allocated by this component.
Definition open_gl_component.cpp:168
OpenGlComponent(String name="")
Constructs an OpenGlComponent.
Definition open_gl_component.cpp:34
virtual ~OpenGlComponent()
Destructor.
Definition open_gl_component.cpp:40
virtual void paintBackground(Graphics &g)
Paints a standard background for the component.
Definition open_gl_component.cpp:105
Colour body_color_
The body color of the component.
Definition open_gl_component.h:255
virtual void init(OpenGlWrapper &open_gl)
Initializes any OpenGL-specific resources needed by the component.
Definition open_gl_component.cpp:148
Colour background_color_
The background color of the component.
Definition open_gl_component.h:254
void repaintBackground()
Requests a repaint of the component's background on the OpenGL layer.
Definition open_gl_component.cpp:112
std::unique_ptr< OpenGlCorners > corners_
Optional corners for rounded edges.
Definition open_gl_component.h:252
virtual void parentHierarchyChanged() override
Called when the component's parent hierarchy changes.
Definition open_gl_component.cpp:128
const SynthSection * parent_
Pointer to parent SynthSection for skin lookups.
Definition open_gl_component.h:256
Manages the overall color and value theme (or "skin") of the user interface.
Definition skin.h:24
ValueId
Identifiers for various UI scaling/spacing values and configuration constants.
Definition skin.h:70
@ kWidgetRoundedCorner
Definition skin.h:104
@ kWidgetBackground
Definition skin.h:173
@ kBody
Definition skin.h:129
const vital::StatusOutput * getStatusOutput(const std::string &name)
Retrieves a status output by name.
Definition synth_base.cpp:262
An interface class linking the Vital synthesizer backend (SynthBase) with a GUI.
Definition synth_gui_interface.h:56
SynthBase * getSynth()
Returns the SynthBase instance this interface is managing.
Definition synth_gui_interface.h:85
float findValue(Skin::ValueId value_id) const
Finds a value in the skin overrides or from the parent if not found locally.
Definition synth_section.cpp:18
#define VITAL_ASSERT(x)
Definition common.h:11
A helper struct containing references to OpenGL context, shaders, and display scale.
Definition shaders.h:174
OpenGLContext & context
The OpenGLContext for current rendering.
Definition shaders.h:181
float display_scale
Display scaling factor for high-DPI rendering.
Definition shaders.h:183