diff options
author | cflip <cflip@cflip.net> | 2021-12-22 15:20:13 -0700 |
---|---|---|
committer | cflip <cflip@cflip.net> | 2021-12-22 15:20:13 -0700 |
commit | 8844d54b4bd5535009a1b70888860490899ca0d5 (patch) | |
tree | a03cb2a663299f3bc61f5d981560c5e84e36a483 /gui.cpp | |
parent | d3fcd2d8dbff9362b47ab94c8b4cc24826077591 (diff) |
Refactoring and cleanup
Diffstat (limited to 'gui.cpp')
-rw-r--r-- | gui.cpp | 86 |
1 files changed, 86 insertions, 0 deletions
@@ -0,0 +1,86 @@ +#include "gui.h" + +#include <SDL2/SDL.h> +#include "sequence.h" + +void Knob::Draw(SDL_Renderer* renderer) +{ + constexpr int Radius = 25; + SDL_Rect rect = { m_x, m_y, Radius, Radius }; + SDL_RenderDrawRect(renderer, &rect); + + SDL_RenderDrawLine(renderer, m_x, m_y + (*value * valueScaling), m_x + Radius, m_y + (*value * valueScaling)); +} + +bool Knob::InBounds(int x, int y) +{ + constexpr int Radius = 25; + SDL_Rect rect = { m_x, m_y, Radius, Radius }; + SDL_Point point = { x, y }; + + return SDL_PointInRect(&point, &rect); +} + +GUI::GUI(Sequence& seq) +{ + int i = 0; + for (auto& step : seq.steps) { + ++i; + m_knobs.emplace_back(i * 30 + 10, 120, &step.carrierFreq, 0.1f); + m_knobs.emplace_back(i * 30 + 10, 180, &step.modFreq); + m_knobs.emplace_back(i * 30 + 10, 220, &step.modDepth, 3.0f); + } +} + +void GUI::OnMouseDown(int x, int y) +{ + for (Knob& knob : m_knobs) { + if (knob.InBounds(x, y)) { + m_activeKnob = &knob; + m_dragStart = y; + return; + } + } +} + +void GUI::OnMouseUp() +{ + m_activeKnob = nullptr; + m_dragStart = 0; +} + +void GUI::OnMouseMove(int x, int y) +{ + if (m_activeKnob) { + *m_activeKnob->value -= (m_dragStart - y) / m_activeKnob->valueScaling; + m_dragStart = y; + + if (*m_activeKnob->value < 0) + *m_activeKnob->value = 0; + } +} + +void GUI::Repaint(SDL_Renderer* renderer, int currentStep) +{ + SDL_Rect rect = { + (640 - 8 * (32 + 8)) / 2, + (320 - 32) / 2, + 32, + 32 + }; + + for (int i = 0; i < 8; i++) { + SDL_SetRenderDrawColor(renderer, 128, i * (255 / 8), 255, 255); + + if (i == currentStep) + SDL_RenderFillRect(renderer, &rect); + else + SDL_RenderDrawRect(renderer, &rect); + + rect.x += rect.w + 8; + } + + for (Knob& knob : m_knobs) { + knob.Draw(renderer); + } +}
\ No newline at end of file |