summaryrefslogtreecommitdiff
path: root/gui.cpp
blob: 5e1479d400c2e2522cb9d8e3f12671b205c3efbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#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)
	: m_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 = {
		40,
		40,
		32,
		32
	};

	for (int i = 0; i < 8; i++) {
		SDL_SetRenderDrawColor(renderer, m_sequence.steps[i].modDepth * 8, m_sequence.steps[i].modFreq * 2, m_sequence.steps[i].carrierFreq / 3, 255);

		if (i == currentStep)
			SDL_RenderFillRect(renderer, &rect);
		else
			SDL_RenderDrawRect(renderer, &rect);

		rect.x += rect.w + 8;
	}

	SDL_SetRenderDrawColor(renderer, 127, 127, 255, 255);

	for (Knob& knob : m_knobs) {
		knob.Draw(renderer);
	}
}