summaryrefslogtreecommitdiff
path: root/scene/gui/gradient_edit.cpp
blob: 1617405dd322eb332ff8c9b9fe4deebc90646b43 (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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*************************************************************************/
/*  gradient_edit.cpp                                                    */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                      https://godotengine.org                          */
/*************************************************************************/
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
/*                                                                       */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the       */
/* "Software"), to deal in the Software without restriction, including   */
/* without limitation the rights to use, copy, modify, merge, publish,   */
/* distribute, sublicense, and/or sell copies of the Software, and to    */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions:                                             */
/*                                                                       */
/* The above copyright notice and this permission notice shall be        */
/* included in all copies or substantial portions of the Software.       */
/*                                                                       */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
/*************************************************************************/

#include "gradient_edit.h"

#include "core/os/keyboard.h"

#ifdef TOOLS_ENABLED
#include "editor/editor_scale.h"
#define SPACING (3 * EDSCALE)
#define POINT_WIDTH (8 * EDSCALE)
#else
#define SPACING 3
#define POINT_WIDTH 8
#endif

GradientEdit::GradientEdit() {
	grabbed = -1;
	grabbing = false;
	set_focus_mode(FOCUS_ALL);

	popup = memnew(PopupPanel);
	picker = memnew(ColorPicker);
	popup->add_child(picker);

	add_child(popup);

	checker = Ref<ImageTexture>(memnew(ImageTexture));
	Ref<Image> img = memnew(Image(checker_bg_png));
}

int GradientEdit::_get_point_from_pos(int x) {
	int result = -1;
	int total_w = get_size().width - get_size().height - SPACING;
	float min_distance = 1e20;
	for (int i = 0; i < points.size(); i++) {
		//Check if we clicked at point
		float distance = ABS(x - points[i].offset * total_w);
		float min = (POINT_WIDTH / 2 * 1.7); //make it easier to grab
		if (distance <= min && distance < min_distance) {
			result = i;
			min_distance = distance;
		}
	}
	return result;
}

void GradientEdit::_show_color_picker() {
	if (grabbed == -1)
		return;
	picker->set_pick_color(points[grabbed].color);
	Size2 minsize = popup->get_contents_minimum_size();
	bool show_above = false;
	if (get_global_position().y + get_size().y + minsize.y > get_viewport_rect().size.y) {
		show_above = true;
	}
	if (show_above) {
		popup->set_position(get_screen_position() - Vector2(0, minsize.y));
	} else {
		popup->set_position(get_screen_position() + Vector2(0, get_size().y));
	}
	popup->popup();
}

GradientEdit::~GradientEdit() {
}

void GradientEdit::_gui_input(const Ref<InputEvent> &p_event) {
	Ref<InputEventKey> k = p_event;

	if (k.is_valid() && k->is_pressed() && k->get_keycode() == KEY_DELETE && grabbed != -1) {
		points.remove(grabbed);
		grabbed = -1;
		grabbing = false;
		update();
		emit_signal("ramp_changed");
		accept_event();
	}

	Ref<InputEventMouseButton> mb = p_event;
	//Show color picker on double click.
	if (mb.is_valid() && mb->get_button_index() == 1 && mb->is_doubleclick() && mb->is_pressed()) {
		grabbed = _get_point_from_pos(mb->get_position().x);
		_show_color_picker();
		accept_event();
	}

	//Delete point on right click
	if (mb.is_valid() && mb->get_button_index() == 2 && mb->is_pressed()) {
		grabbed = _get_point_from_pos(mb->get_position().x);
		if (grabbed != -1) {
			points.remove(grabbed);
			grabbed = -1;
			grabbing = false;
			update();
			emit_signal("ramp_changed");
			accept_event();
		}
	}

	//Hold alt key to duplicate selected color
	if (mb.is_valid() && mb->get_button_index() == 1 && mb->is_pressed() && mb->get_alt()) {
		int x = mb->get_position().x;
		grabbed = _get_point_from_pos(x);

		if (grabbed != -1) {
			int total_w = get_size().width - get_size().height - SPACING;
			Gradient::Point newPoint = points[grabbed];
			newPoint.offset = CLAMP(x / float(total_w), 0, 1);

			points.push_back(newPoint);
			points.sort();
			for (int i = 0; i < points.size(); ++i) {
				if (points[i].offset == newPoint.offset) {
					grabbed = i;
					break;
				}
			}

			emit_signal("ramp_changed");
			update();
		}
	}

	//select
	if (mb.is_valid() && mb->get_button_index() == 1 && mb->is_pressed()) {
		update();
		int x = mb->get_position().x;
		int total_w = get_size().width - get_size().height - SPACING;

		//Check if color selector was clicked.
		if (x > total_w + SPACING) {
			_show_color_picker();
			return;
		}

		grabbing = true;

		grabbed = _get_point_from_pos(x);
		//grab or select
		if (grabbed != -1) {
			return;
		}

		//insert
		Gradient::Point newPoint;
		newPoint.offset = CLAMP(x / float(total_w), 0, 1);

		Gradient::Point prev;
		Gradient::Point next;

		int pos = -1;
		for (int i = 0; i < points.size(); i++) {
			if (points[i].offset < newPoint.offset)
				pos = i;
		}

		if (pos == -1) {
			prev.color = Color(0, 0, 0);
			prev.offset = 0;
			if (points.size()) {
				next = points[0];
			} else {
				next.color = Color(1, 1, 1);
				next.offset = 1.0;
			}
		} else {
			if (pos == points.size() - 1) {
				next.color = Color(1, 1, 1);
				next.offset = 1.0;
			} else {
				next = points[pos + 1];
			}
			prev = points[pos];
		}

		newPoint.color = prev.color.lerp(next.color, (newPoint.offset - prev.offset) / (next.offset - prev.offset));

		points.push_back(newPoint);
		points.sort();
		for (int i = 0; i < points.size(); i++) {
			if (points[i].offset == newPoint.offset) {
				grabbed = i;
				break;
			}
		}

		emit_signal("ramp_changed");
	}

	if (mb.is_valid() && mb->get_button_index() == 1 && !mb->is_pressed()) {
		if (grabbing) {
			grabbing = false;
			emit_signal("ramp_changed");
		}
		update();
	}

	Ref<InputEventMouseMotion> mm = p_event;

	if (mm.is_valid() && grabbing) {
		int total_w = get_size().width - get_size().height - SPACING;

		int x = mm->get_position().x;

		float newofs = CLAMP(x / float(total_w), 0, 1);

		// Snap to "round" coordinates if holding Ctrl.
		// Be more precise if holding Shift as well
		if (mm->get_control()) {
			newofs = Math::stepify(newofs, mm->get_shift() ? 0.025 : 0.1);
		} else if (mm->get_shift()) {
			// Snap to nearest point if holding just Shift
			const float snap_threshold = 0.03;
			float smallest_ofs = snap_threshold;
			bool found = false;
			int nearest_point = 0;
			for (int i = 0; i < points.size(); ++i) {
				if (i != grabbed) {
					float temp_ofs = ABS(points[i].offset - newofs);
					if (temp_ofs < smallest_ofs) {
						smallest_ofs = temp_ofs;
						nearest_point = i;
						if (found)
							break;
						found = true;
					}
				}
			}
			if (found) {
				if (points[nearest_point].offset < newofs)
					newofs = points[nearest_point].offset + 0.00001;
				else
					newofs = points[nearest_point].offset - 0.00001;
				newofs = CLAMP(newofs, 0, 1);
			}
		}

		bool valid = true;
		for (int i = 0; i < points.size(); i++) {
			if (points[i].offset == newofs && i != grabbed) {
				valid = false;
				break;
			}
		}

		if (!valid || grabbed == -1) {
			return;
		}
		points.write[grabbed].offset = newofs;

		points.sort();
		for (int i = 0; i < points.size(); i++) {
			if (points[i].offset == newofs) {
				grabbed = i;
				break;
			}
		}

		emit_signal("ramp_changed");

		update();
	}
}

void GradientEdit::_notification(int p_what) {
	if (p_what == NOTIFICATION_ENTER_TREE) {
		if (!picker->is_connected("color_changed", callable_mp(this, &GradientEdit::_color_changed))) {
			picker->connect("color_changed", callable_mp(this, &GradientEdit::_color_changed));
		}
	}
	if (p_what == NOTIFICATION_DRAW) {
		int w = get_size().x;
		int h = get_size().y;

		if (w == 0 || h == 0)
			return; //Safety check. We have division by 'h'. And in any case there is nothing to draw with such size

		int total_w = get_size().width - get_size().height - SPACING;

		//Draw checker pattern for ramp
		_draw_checker(0, 0, total_w, h);

		//Draw color ramp
		Gradient::Point prev;
		prev.offset = 0;
		if (points.size() == 0)
			prev.color = Color(0, 0, 0); //Draw black rectangle if we have no points
		else
			prev.color = points[0].color; //Extend color of first point to the beginning.

		for (int i = -1; i < points.size(); i++) {
			Gradient::Point next;
			//If there is no next point
			if (i + 1 == points.size()) {
				if (points.size() == 0)
					next.color = Color(0, 0, 0); //Draw black rectangle if we have no points
				else
					next.color = points[i].color; //Extend color of last point to the end.
				next.offset = 1;
			} else {
				next = points[i + 1];
			}

			if (prev.offset == next.offset) {
				prev = next;
				continue;
			}

			Vector<Vector2> points;
			Vector<Color> colors;
			points.push_back(Vector2(prev.offset * total_w, h));
			points.push_back(Vector2(prev.offset * total_w, 0));
			points.push_back(Vector2(next.offset * total_w, 0));
			points.push_back(Vector2(next.offset * total_w, h));
			colors.push_back(prev.color);
			colors.push_back(prev.color);
			colors.push_back(next.color);
			colors.push_back(next.color);
			draw_primitive(points, colors, Vector<Point2>());
			prev = next;
		}

		//Draw point markers
		for (int i = 0; i < points.size(); i++) {
			Color col = points[i].color.contrasted();
			col.a = 0.9;

			draw_line(Vector2(points[i].offset * total_w, 0), Vector2(points[i].offset * total_w, h / 2), col);
			Rect2 rect = Rect2(points[i].offset * total_w - POINT_WIDTH / 2, h / 2, POINT_WIDTH, h / 2);
			draw_rect(rect, points[i].color, true);
			draw_rect(rect, col, false);
			if (grabbed == i) {
				rect = rect.grow(-1);
				if (has_focus()) {
					draw_rect(rect, Color(1, 0, 0, 0.9), false);
				} else {
					draw_rect(rect, Color(0.6, 0, 0, 0.9), false);
				}

				rect = rect.grow(-1);
				draw_rect(rect, col, false);
			}
		}

		//Draw "button" for color selector
		_draw_checker(total_w + SPACING, 0, h, h);
		if (grabbed != -1) {
			//Draw with selection color
			draw_rect(Rect2(total_w + SPACING, 0, h, h), points[grabbed].color);
		} else {
			//if no color selected draw grey color with 'X' on top.
			draw_rect(Rect2(total_w + SPACING, 0, h, h), Color(0.5, 0.5, 0.5, 1));
			draw_line(Vector2(total_w + SPACING, 0), Vector2(total_w + SPACING + h, h), Color(1, 1, 1, 0.6));
			draw_line(Vector2(total_w + SPACING, h), Vector2(total_w + SPACING + h, 0), Color(1, 1, 1, 0.6));
		}

		//Draw borders around color ramp if in focus
		if (has_focus()) {
			draw_line(Vector2(-1, -1), Vector2(total_w + 1, -1), Color(1, 1, 1, 0.6));
			draw_line(Vector2(total_w + 1, -1), Vector2(total_w + 1, h + 1), Color(1, 1, 1, 0.6));
			draw_line(Vector2(total_w + 1, h + 1), Vector2(-1, h + 1), Color(1, 1, 1, 0.6));
			draw_line(Vector2(-1, -1), Vector2(-1, h + 1), Color(1, 1, 1, 0.6));
		}
	}

	if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
		if (!is_visible()) {
			grabbing = false;
		}
	}
}

void GradientEdit::_draw_checker(int x, int y, int w, int h) {
	//Draw it with polygon to insert UVs for scale
	Vector<Vector2> backPoints;
	backPoints.push_back(Vector2(x, y));
	backPoints.push_back(Vector2(x, y + h));
	backPoints.push_back(Vector2(x + w, y + h));
	backPoints.push_back(Vector2(x + w, y));
	Vector<Color> colorPoints;
	colorPoints.push_back(Color(1, 1, 1, 1));
	colorPoints.push_back(Color(1, 1, 1, 1));
	colorPoints.push_back(Color(1, 1, 1, 1));
	colorPoints.push_back(Color(1, 1, 1, 1));
	Vector<Vector2> uvPoints;
	//Draw checker pattern pixel-perfect and scale it by 2.
	uvPoints.push_back(Vector2(x, y));
	uvPoints.push_back(Vector2(x, y + h * .5f / checker->get_height()));
	uvPoints.push_back(Vector2(x + w * .5f / checker->get_width(), y + h * .5f / checker->get_height()));
	uvPoints.push_back(Vector2(x + w * .5f / checker->get_width(), y));
	draw_polygon(backPoints, colorPoints, uvPoints, checker);
}

Size2 GradientEdit::get_minimum_size() const {
	return Vector2(0, 16);
}

void GradientEdit::_color_changed(const Color &p_color) {
	if (grabbed == -1)
		return;
	points.write[grabbed].color = p_color;
	update();
	emit_signal("ramp_changed");
}

void GradientEdit::set_ramp(const Vector<float> &p_offsets, const Vector<Color> &p_colors) {
	ERR_FAIL_COND(p_offsets.size() != p_colors.size());
	points.clear();
	for (int i = 0; i < p_offsets.size(); i++) {
		Gradient::Point p;
		p.offset = p_offsets[i];
		p.color = p_colors[i];
		points.push_back(p);
	}

	points.sort();
	update();
}

Vector<float> GradientEdit::get_offsets() const {
	Vector<float> ret;
	for (int i = 0; i < points.size(); i++)
		ret.push_back(points[i].offset);
	return ret;
}

Vector<Color> GradientEdit::get_colors() const {
	Vector<Color> ret;
	for (int i = 0; i < points.size(); i++)
		ret.push_back(points[i].color);
	return ret;
}

void GradientEdit::set_points(Vector<Gradient::Point> &p_points) {
	if (points.size() != p_points.size())
		grabbed = -1;
	points.clear();
	points = p_points;
}

Vector<Gradient::Point> &GradientEdit::get_points() {
	return points;
}

void GradientEdit::_bind_methods() {
	ClassDB::bind_method(D_METHOD("_gui_input"), &GradientEdit::_gui_input);
	ADD_SIGNAL(MethodInfo("ramp_changed"));
}