summaryrefslogtreecommitdiff
path: root/plugingui/drumkittab.cc
blob: c371dc9d1534c544290d2d5bb5a2336e060737b8 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* -*- Mode: c++ -*- */
/***************************************************************************
 *            drumkittab.cc
 *
 *  Fri Jun  8 21:50:03 CEST 2018
 *  Copyright 2018 André Nusser
 *  andre.nusser@googlemail.com
 ****************************************************************************/

/*
 *  This file is part of DrumGizmo.
 *
 *  DrumGizmo is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  DrumGizmo is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with DrumGizmo; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
#include "drumkittab.h"

#include <iomanip>
#include <sstream>

#include "cpp11fix.h" // required for c++11
#include "painter.h"
#include "settings.h"

namespace GUI
{

DrumkitTab::DrumkitTab(Widget* parent,
                       Settings& settings/*,
                       SettingsNotifier& settings_notifier,
                       Config& config*/)
	: Widget(parent)
	, settings(settings)
	/*, settings_notifier(settings_notifier)
	, config(config)*/
{
	loadImageFiles("/home/chaot/Programming/drumgizmo/crocellkit01.png", "/home/chaot/Programming/drumgizmo/crocellkit01_map.png");

	velocity_label.move(10, height()-velocity_label.height()-5);
	updateVelocityLabel();
	velocity_label.resizeToText();

	instrument_name_label.move(velocity_label.width()+30, height()-instrument_name_label.height()-5);
	updateInstrumentLabel();
}

void DrumkitTab::resize(std::size_t width, std::size_t height)
{
	Widget::resize(width, height);

	if (drumkit_image) {
		Painter painter(*this);
		painter.clear();

		drumkit_image_x = (this->width()-drumkit_image->width())/2;
		drumkit_image_y = (this->height()-drumkit_image->height())/2;
		painter.drawImage(drumkit_image_x, drumkit_image_y, *drumkit_image);
	}

	velocity_label.move(10, height-velocity_label.height()-5);
	instrument_name_label.move(velocity_label.width()+30, height-instrument_name_label.height()-5);
}

void DrumkitTab::buttonEvent(ButtonEvent* buttonEvent)
{
	if (map_image) {
		if (buttonEvent->button == MouseButton::right) {
			if (buttonEvent->direction == GUI::Direction::down) {
				Painter painter(*this);
				painter.drawImage(drumkit_image_x, drumkit_image_y, *map_image);
				redraw();

				shows_overlay = true;
				return;
			}
			if (buttonEvent->direction == GUI::Direction::up) {
				Painter painter(*this);
				painter.clear();
				painter.drawImage(drumkit_image_x, drumkit_image_y, *drumkit_image);
				redraw();

				shows_overlay = false;
				return;
			}
		}
	}

	if (buttonEvent->button == MouseButton::left &&
	    buttonEvent->direction == GUI::Direction::down)
	{
		triggerAudition(buttonEvent->x, buttonEvent->y);
	}
}

void DrumkitTab::scrollEvent(ScrollEvent* scrollEvent)
{
	current_velocity -=  0.01*scrollEvent->delta;
	current_velocity = std::max(std::min(current_velocity, 1.0f), 0.0f);
	updateVelocityLabel();
	velocity_label.resizeToText();

	triggerAudition(scrollEvent->x, scrollEvent->y);
}

void DrumkitTab::mouseLeaveEvent()
{
	if (map_image && shows_overlay) {
		Painter painter(*this);
		painter.clear();
		painter.drawImage(drumkit_image_x, drumkit_image_y, *drumkit_image);
		redraw();

		shows_overlay = false;
	}
}

void DrumkitTab::triggerAudition(int x, int y)
{
	auto map_colour = map_image->getPixel(x - drumkit_image_x, y - drumkit_image_y);
	if (map_colour.alpha() == 0.0) { return; }

	auto it = colour_to_instrument.find(map_colour);
	if (it == colour_to_instrument.end())
	{
		return;
	}

	++settings.audition_counter;
	settings.audition_instrument = it->second;
	settings.audition_velocity = current_velocity;

	current_instrument = it->second;
	updateInstrumentLabel();
}

void DrumkitTab::updateVelocityLabel()
{
	std::stringstream stream;
	stream << std::fixed << std::setprecision(2) << current_velocity;
	velocity_label.setText("Velocity: " + stream.str());
}

void DrumkitTab::updateInstrumentLabel()
{
	instrument_name_label.setText("Instrument: " + current_instrument);
	instrument_name_label.resizeToText();
}

void DrumkitTab::loadImageFiles(std::string const& image_file, std::string const& map_file)
{
	drumkit_image = std::make_unique<Image>(image_file);
	map_image = std::make_unique<Image>(map_file);

	// TODO: actually use mapping from drumkit file here
	colour_to_instrument = {
		{Colour(0), "Snare"},
		{Colour(255./255, 15./255, 55./255), "KdrumL"},
		{Colour(154./255, 153./255, 33./255), "HihatClosed"},
		{Colour(248./255, 221./255, 37./255), "Tom4"}
	};
}

} // GUI::