summaryrefslogtreecommitdiff
path: root/plugingui/powerwidget.cc
blob: 0a565e6a9adff08e43b6c4bf24ce969273aaa90f (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/* -*- Mode: c++ -*- */
/***************************************************************************
 *            powerwidget.cc
 *
 *  Fri Apr 24 17:30:45 CEST 2020
 *  Copyright 2020 Bent Bisballe Nyeng
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *  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 "powerwidget.h"

#include "painter.h"

#include <notifier.h>
#include <settings.h>
#include <colour.h>
#include <powermap.h>

#include <hugin.hpp>
#include <cmath>

#include <translation.h>

PowerWidget::PowerWidget(GUI::Widget* parent,
                         Settings& settings,
                         SettingsNotifier& settings_notifier)
	: GUI::Widget(parent)
	, canvas(this, settings, settings_notifier)
	, settings(settings)
{
	canvas.move(7, 7);

	CONNECT(&shelf_checkbox, stateChangedNotifier, this, &PowerWidget::chk_shelf);

	shelf_label.setText(_("Shelf"));
	shelf_label.setAlignment(GUI::TextAlignment::center);
	shelf_label.resize(59, 16);
	shelf_checkbox.resize(59, 40);

	CONNECT(&settings_notifier, powermap_shelf, &shelf_checkbox,
	        &GUI::CheckBox::setChecked);
}

void PowerWidget::chk_shelf(bool v)
{
	settings.powermap_shelf.store(v);
}

void PowerWidget::repaintEvent(GUI::RepaintEvent *repaintEvent)
{
	GUI::Painter p(*this);
	box.setSize(width() - 59 - 64, height());
	p.drawImage(0, 0, box);
}

void PowerWidget::resize(std::size_t width, std::size_t height)
{
	Widget::resize(width, height);
	if(width < (14 + 59 + 64) || height < 14)
	{
		canvas.resize(1, 1);
		return;
	}
	canvas.resize(width - 14 - 59 - 64, height - 14);

	shelf_label.move(width - 59 + 5 - 32 , 0);
	shelf_checkbox.move(width - 59 + 5 - 32, 16);
}

PowerWidget::Canvas::Canvas(GUI::Widget* parent,
                            Settings& settings,
                            SettingsNotifier& settings_notifier)
	: GUI::Widget(parent)
	, settings_notifier(settings_notifier)
	, settings(settings)
{
	CONNECT(this, settings_notifier.enable_powermap,
	        this, &PowerWidget::Canvas::parameterChangedBool);
	CONNECT(this, settings_notifier.powermap_fixed0_x,
	        this, &PowerWidget::Canvas::parameterChangedFloat);
	CONNECT(this, settings_notifier.powermap_fixed0_y,
	        this, &PowerWidget::Canvas::parameterChangedFloat);
	CONNECT(this, settings_notifier.powermap_fixed1_x,
	        this, &PowerWidget::Canvas::parameterChangedFloat);
	CONNECT(this, settings_notifier.powermap_fixed1_y,
	        this, &PowerWidget::Canvas::parameterChangedFloat);
	CONNECT(this, settings_notifier.powermap_fixed2_x,
	        this, &PowerWidget::Canvas::parameterChangedFloat);
	CONNECT(this, settings_notifier.powermap_fixed2_y,
	        this, &PowerWidget::Canvas::parameterChangedFloat);
	CONNECT(this, settings_notifier.powermap_shelf,
	        this, &PowerWidget::Canvas::parameterChangedBool);
	CONNECT(this, settings_notifier.powermap_input,
	        this, &PowerWidget::Canvas::parameterChangedFloat);
	CONNECT(this, settings_notifier.powermap_output,
	        this, &PowerWidget::Canvas::parameterChangedFloat);

	parameterChangedFloat(0);
}

void PowerWidget::Canvas::repaintEvent(GUI::RepaintEvent *repaintEvent)
{
	if(width() < 1 || height() < 1)
	{
		return;
	}

	const float x0 = brd;
	const float y0 = brd;
	const float width0 = (int)width() - 2 * brd;
	const float height0 = (int)height() - 2 * brd;

	GUI::Painter p(*this);

	p.clear();

	p.setColour(GUI::Colour(1.0f, 1.0f, 1.0f, 0.2f));
	p.drawRectangle(x0, y0 + height0, x0 + width0, y0);

	if(enabled)
	{
		// draw 1:1 line in grey in the background to indicate where 1:1 is
		p.setColour(GUI::Colour(0.5));
		p.drawLine(x0, y0 + height0, x0 + width0, y0);
	}

	if(enabled)
	{
		// enabled green
		p.setColour(GUI::Colour(0.0f, 1.0f, 0.0f, 1.0f));
	}
	else
	{
		// disabled grey
		p.setColour(GUI::Colour(0.5f));
	}

	// Draw very short line segments across the region
	std::pair<int, int> old{};
	for(std::size_t x = 0; x < width0; ++x)
	{
		int y = power_map.map((float)x / width0) * height0;
		if(x > 0)
		{
			p.drawLine(x0 + old.first, y0 + old.second, x0 + x, y0 + height0 - y);
		}
		old = { x, height0 - y };
	}

	int x = width0;
	int y = power_map.map((float)x / width0) * height0;
	p.drawLine(x0 + old.first, y0 + old.second, x0 + x, y0 + height0 - y);
	old = { x, height0 - y };

	if(!enabled)
	{
		// draw 1:1 line in green in the foreground
		p.setColour(GUI::Colour(0.0f, 1.0f, 0.0f, 1.0f));
		p.drawLine(x0, y0 + height0, x0 + width0, y0);
	}

	// draw the input/output of the last hit
	if(settings.powermap_input.load() != -1 && settings.powermap_output.load() != -1)
	{
		p.setColour(GUI::Colour(.8f, 0.0f, .2f, .5f));
		p.drawLine(x0 + settings.powermap_input.load()*width0, y0 + height0,
				   x0 + settings.powermap_input.load()*width0, y0);
		p.drawLine(x0, y0 + height0 - settings.powermap_output.load()*height0,
				   x0 + width0, y0 + height0 - settings.powermap_output.load()*height0);
	}

	// draw the fixed nodes of the spline
	float rad = radius * width();
	p.setColour(GUI::Colour{0.0f, 1.0f, 0.0f, 0.7f});
	p.drawFilledCircle(x0 + std::round(settings.powermap_fixed0_x.load() * width0),
	                   y0 + height0 - std::round(settings.powermap_fixed0_y.load() * height0), rad);
	p.drawCircle(x0 + std::round(power_map.getFixed0().in * width0),
	             y0 + height0 - std::round(power_map.getFixed0().out * height0), rad + 1);

	p.setColour(GUI::Colour{1.0f, 1.0f, 0.0f, 0.7f});
	p.drawFilledCircle(x0 + std::round(settings.powermap_fixed1_x.load() * width0),
	                   y0 + height0 - std::round(settings.powermap_fixed1_y.load() * height0), rad);
	p.drawCircle(x0 + std::round(power_map.getFixed1().in * width0),
	             y0 + height0 - std::round(power_map.getFixed1().out * height0), rad + 1);

	p.setColour(GUI::Colour{1.0f, 0.0f, 0.0f, 0.7f});
	p.drawFilledCircle(x0 + std::round(settings.powermap_fixed2_x.load() * width0),
	                   y0 + height0 - std::round(settings.powermap_fixed2_y.load() * height0), rad);
	p.drawCircle(x0 + std::round(power_map.getFixed2().in * width0),
	             y0 + height0 - std::round(power_map.getFixed2().out * height0), rad + 1);

	p.setColour(GUI::Colour(1.0f, 1.0f, 1.0f, 0.2f));
	p.drawText(width() / 2 - (font.textWidth(_("in")) / 2), height() - 8, font, _("in"));
	p.drawText(8, height() / 2 - (font.textWidth(_("out")) / 2), font, _("out"), false, true);
}

void PowerWidget::Canvas::buttonEvent(GUI::ButtonEvent* buttonEvent)
{
	const float x0 = brd;
	const float y0 = brd;
	const float width0 = (int)width() - 2 * brd;
	const float height0 = (int)height() - 2 * brd;

	float mx0 = (float)(buttonEvent->x - x0) / width0;
	float my0 = (float)(((int)height() - buttonEvent->y) - y0) / height0;

	float radius_x = radius * 2;
	float radius_y = radius * width0 / height0 * 2;

	switch(buttonEvent->direction)
	{
	case GUI::Direction::up:
		in_point = -1;
		break;
	case GUI::Direction::down:
		if(std::abs(mx0 - settings.powermap_fixed0_x.load()) < radius_x &&
		   std::abs(my0 - settings.powermap_fixed0_y.load()) < radius_y)
		{
			in_point = 0;
		}

		if(std::abs(mx0 - settings.powermap_fixed1_x.load()) < radius_x &&
		   std::abs(my0 - settings.powermap_fixed1_y.load()) < radius_y)
		{
			in_point = 1;
		}

		if(std::abs(mx0 - settings.powermap_fixed2_x.load()) < radius_x &&
		   std::abs(my0 - settings.powermap_fixed2_y.load()) < radius_y)
		{
			in_point = 2;
		}
		break;
	}
}

namespace
{
float clamp(float val, float min, float max)
{
	return std::max(min, std::min(max, val));
}
}

void PowerWidget::Canvas::mouseMoveEvent(GUI::MouseMoveEvent* mouseMoveEvent)
{
	const float x0 = brd;
	const float y0 = brd;
	const float width0 = (int)width() - 2 * brd;
	const float height0 = (int)height() - 2 * brd;

	float mx0 = (float)(mouseMoveEvent->x - x0) / width0;
	float my0 = (float)(((int)height() - mouseMoveEvent->y) - y0) / height0;

	switch(in_point)
	{
	case 0:
		settings.powermap_fixed0_x.store(clamp(mx0, 0, 1));
		settings.powermap_fixed0_y.store(clamp(my0, 0, 1));
		redraw();
		break;
	case 1:
		settings.powermap_fixed1_x.store(clamp(mx0, 0, 1));
		settings.powermap_fixed1_y.store(clamp(my0, 0, 1));
		redraw();
		break;
	case 2:
		settings.powermap_fixed2_x.store(clamp(mx0, 0, 1));
		settings.powermap_fixed2_y.store(clamp(my0, 0, 1));
		redraw();
		break;
	default:
		break;
	}
/*
	switch(in_point)
	{
	case 0:
		settings.fixed0_x.store(clamp((float)mouseMoveEvent->x / width()));
		settings.fixed0_y.store(1.0f - clamp((float)mouseMoveEvent->y / height()));
		redraw();
		break;
	case 1:
		settings.fixed1_x.store(clamp((float)mouseMoveEvent->x / width()));
		settings.fixed1_y.store(1.0f - clamp((float)mouseMoveEvent->y / height()));
		redraw();
		break;
	case 2:
		settings.fixed2_x.store(clamp((float)mouseMoveEvent->x / width()));
		settings.fixed2_y.store(1.0f - clamp((float)mouseMoveEvent->y / height()));
		redraw();
		break;
	default:
		break;
	}
*/
}

void PowerWidget::Canvas::mouseLeaveEvent()
{
	//in_point = -1;
}

void PowerWidget::Canvas::parameterChangedFloat(float)
{
	power_map.setFixed0({settings.powermap_fixed0_x.load(), settings.powermap_fixed0_y.load()});
	power_map.setFixed1({settings.powermap_fixed1_x.load(), settings.powermap_fixed1_y.load()});
	power_map.setFixed2({settings.powermap_fixed2_x.load(), settings.powermap_fixed2_y.load()});
	power_map.setShelf(settings.powermap_shelf.load());
	enabled = settings.enable_powermap.load();
	redraw();
}

void PowerWidget::Canvas::parameterChangedBool(bool)
{
	parameterChangedFloat(0);
}