Vital
Loading...
Searching...
No Matches
phase_editor.cpp
Go to the documentation of this file.
1#include "phase_editor.h"
2
3#include "skin.h"
4#include "synth_constants.h"
5#include "shaders.h"
6#include "utils.h"
7
8PhaseEditor::PhaseEditor() : OpenGlMultiQuad(kNumLines, Shaders::kColorFragment) {
9 phase_ = 0.0f;
10 max_tick_height_ = kDefaultHeightPercent;
11 setInterceptsMouseClicks(true, false);
12}
13
15
16void PhaseEditor::mouseDown(const MouseEvent& e) {
17 last_edit_position_ = e.getPosition();
18}
19
20void PhaseEditor::mouseUp(const MouseEvent& e) {
21 updatePhase(e);
22
23 // Notify listeners that mouse is now up, finalizing the change.
24 for (Listener* listener : listeners_)
25 listener->phaseChanged(phase_, true);
26}
27
28void PhaseEditor::mouseDrag(const MouseEvent& e) {
29 // Update phase on each mouse drag movement, listeners get notified with mouse_up = false.
30 updatePhase(e);
31}
32
33void PhaseEditor::updatePhase(const MouseEvent& e) {
34 int difference = e.getPosition().x - last_edit_position_.x;
35 // Convert horizontal drag into a phase change based on component width.
36 phase_ += (2.0f * vital::kPi * difference) / getWidth();
37 last_edit_position_ = e.getPosition();
38
39 for (Listener* listener : listeners_)
40 listener->phaseChanged(phase_, false);
41
42 updatePositions();
43}
44
45void PhaseEditor::updatePositions() {
46 float width = 2.0f / getWidth();
47 for (int i = 0; i < kNumLines; ++i) {
48 // Compute the fractional position along the x-axis and wrap phase around [0,1].
49 float fraction = phase_ / (2.0f * vital::kPi) + (1.0f * i) / kNumLines;
50 fraction -= floorf(fraction);
51
52 float height = max_tick_height_ * 2.0f;
53 // Reduce height for certain divisions, making a structured pattern of lines.
54 for (int div = 2; div < kNumLines; div *= 2) {
55 if (i % div)
56 height /= 2.0f;
57 }
58
59 setQuad(i, 2.0f * fraction - 1.0f, -1.0f, width, height);
60 }
61}
62
63void PhaseEditor::setPhase(float phase) {
64 phase_ = phase;
65 updatePositions();
66}
A component for rendering multiple quads using OpenGL, with customizable colors, rounding,...
Definition open_gl_multi_quad.h:16
void setQuad(int i, float x, float y, float w, float h)
Sets the position and size of a quad in normalized device space.
Definition open_gl_multi_quad.h:313
Interface for receiving notifications when the phase value changes.
Definition phase_editor.h:26
virtual ~PhaseEditor()
Destructor.
Definition phase_editor.cpp:14
void setPhase(float phase)
Sets the current phase value and updates the display.
Definition phase_editor.cpp:63
void mouseDrag(const MouseEvent &e) override
Called while the mouse is dragged horizontally.
Definition phase_editor.cpp:28
void mouseUp(const MouseEvent &e) override
Called when the mouse is released after interaction.
Definition phase_editor.cpp:20
void mouseDown(const MouseEvent &e) override
Called when the mouse is pressed down on the component.
Definition phase_editor.cpp:16
PhaseEditor()
Constructs a PhaseEditor.
Definition phase_editor.cpp:8
static constexpr float kDefaultHeightPercent
Definition phase_editor.h:20
static constexpr int kNumLines
Definition phase_editor.h:18
Manages and provides access to vertex and fragment shaders used by the OpenGL rendering pipeline.
Definition shaders.h:19
constexpr mono_float kPi
Pi constant.
Definition common.h:36
Provides various utility functions, classes, and constants for audio, math, and general-purpose opera...