Vital
Loading...
Searching...
No Matches
wavetable_playhead.cpp
Go to the documentation of this file.
1/*
2Summary:
3
4WavetablePlayhead represents a movable playhead line indicating a current position within a set of frames. Users can interact with it using mouse events to set a specific frame. The class supports customizable tick marks at regular intervals and notifies listeners whenever the playhead is moved.
5 */
6
8
9#include "skin.h"
10
12 SynthSection("Playhead"), position_quad_(Shaders::kColorFragment),
13 padding_(0), num_positions_(num_positions), position_(0), drag_start_x_(0) {
15}
16
19
20 // Notify all registered listeners of the new position.
21 for (Listener* listener : listeners_)
22 listener->playheadMoved(position_);
23
25}
26
28 float active_width = getWidth() - 2 * padding_ + 1;
29 int x = (active_width * position_) / (num_positions_ - 1) - 0.5f + padding_;
30 position_quad_.setBounds(x, 0, 1, getHeight());
31}
32
33void WavetablePlayhead::mouseDown(const MouseEvent& event) {
34 mouseEvent(event);
35}
36
37void WavetablePlayhead::mouseDrag(const MouseEvent& event) {
38 mouseEvent(event);
39}
40
41void WavetablePlayhead::mouseEvent(const MouseEvent& event) {
42 float active_width = getWidth() - 2 * padding_ + 1;
43 int position = std::roundf((num_positions_ - 1) * (event.x - padding_) / active_width);
44 // Clamp the playhead position to the valid range and update.
45 setPosition(std::max(std::min(position, num_positions_ - 1), 0));
46}
47
49 float active_width = getWidth() - 2 * padding_ + 1;
50 g.setColour(findColour(Skin::kLightenScreen, true));
51 // Draw tick marks at regular intervals
52 int small_line_height = getHeight() / 3;
53 int big_line_height = 2 * small_line_height;
54 for (int i = 0; i < num_positions_; i += kLineSkip) {
55 int x = padding_ - 0.5f + i * active_width / (num_positions_ - 1);
56 if (i % kBigLineSkip == 0)
57 g.fillRect(x, getHeight() - big_line_height, 1, big_line_height);
58 else
59 g.fillRect(x, getHeight() - small_line_height, 1, small_line_height);
60 }
61
62 // Set the playhead line color
64}
65
force_inline void setColor(Colour color)
Sets the base color for the quads.
Definition open_gl_multi_quad.h:102
Manages and provides access to vertex and fragment shaders used by the OpenGL rendering pipeline.
Definition shaders.h:19
@ kWidgetPrimary1
Definition skin.h:165
@ kLightenScreen
Definition skin.h:141
Base class for all synthesizer sections, providing UI layout, painting, and interaction logic.
Definition synth_section.h:193
virtual void resized() override
Called when the component is resized. Arranges layout of child components.
Definition synth_section.cpp:35
void addOpenGlComponent(OpenGlComponent *open_gl_component, bool to_beginning=false)
Definition synth_section.cpp:489
A listener interface for objects interested in playhead position changes.
Definition wavetable_playhead.h:31
void mouseDrag(const MouseEvent &event) override
Handles mouse drag events, moving the playhead position accordingly.
Definition wavetable_playhead.cpp:37
void setPositionQuad()
Updates the visual position of the playhead quad based on the current position and size.
Definition wavetable_playhead.cpp:27
void resized() override
Called when the component is resized, updates the playhead position display.
Definition wavetable_playhead.cpp:66
static constexpr int kLineSkip
Definition wavetable_playhead.h:23
int num_positions_
Total number of positions (frames) available.
Definition wavetable_playhead.h:114
void paintBackground(Graphics &g) override
Paints the background ticks and line indicators for the playhead.
Definition wavetable_playhead.cpp:48
std::vector< Listener * > listeners_
Registered listeners to notify on position changes.
Definition wavetable_playhead.h:111
void mouseEvent(const MouseEvent &event)
Internal method for handling mouse events to change playhead position.
Definition wavetable_playhead.cpp:41
WavetablePlayhead(int num_positions)
Constructs a WavetablePlayhead.
Definition wavetable_playhead.cpp:11
int position()
Gets the current playhead position.
Definition wavetable_playhead.h:52
void setPosition(int position)
Sets the playhead to a specific position.
Definition wavetable_playhead.cpp:17
static constexpr int kBigLineSkip
Used to determine which ticks are larger (every kBigLineSkip) and which are smaller (every kLineSkip)...
Definition wavetable_playhead.h:22
float padding_
Extra horizontal padding for the display area.
Definition wavetable_playhead.h:113
OpenGlQuad position_quad_
The visual quad representing the playhead line.
Definition wavetable_playhead.h:109
int position_
Current playhead position.
Definition wavetable_playhead.h:115
void mouseDown(const MouseEvent &event) override
Handles mouse down events for interaction.
Definition wavetable_playhead.cpp:33