summaryrefslogtreecommitdiff
path: root/scene/gui/spin_box.cpp
blob: 50b25fa7b4228fc8f3ad0de5287c2a41ca32daaa (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
/*************************************************************************/
/*  spin_box.cpp                                                         */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                      https://godotengine.org                          */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur.                 */
/* Copyright (c) 2014-2021 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 "spin_box.h"

#include "core/input/input.h"
#include "core/math/expression.h"

Size2 SpinBox::get_minimum_size() const {
	Size2 ms = line_edit->get_combined_minimum_size();
	ms.width += last_w;
	return ms;
}

void SpinBox::_value_changed(double) {
	String value = TS->format_number(String::num(get_value(), Math::range_step_decimals(get_step())));
	if (prefix != "") {
		value = prefix + " " + value;
	}
	if (suffix != "") {
		value += " " + suffix;
	}
	line_edit->set_text(value);
}

void SpinBox::_text_entered(const String &p_string) {
	Ref<Expression> expr;
	expr.instance();

	String num = TS->parse_number(p_string);
	// Ignore the prefix and suffix in the expression
	Error err = expr->parse(num.trim_prefix(prefix + " ").trim_suffix(" " + suffix));
	if (err != OK) {
		return;
	}

	Variant value = expr->execute(Array(), nullptr, false);
	if (value.get_type() != Variant::NIL) {
		set_value(value);
	}
	_value_changed(0);
}

LineEdit *SpinBox::get_line_edit() {
	return line_edit;
}

void SpinBox::_line_edit_input(const Ref<InputEvent> &p_event) {
}

void SpinBox::_range_click_timeout() {
	if (!drag.enabled && Input::get_singleton()->is_mouse_button_pressed(MOUSE_BUTTON_LEFT)) {
		bool up = get_local_mouse_position().y < (get_size().height / 2);
		set_value(get_value() + (up ? get_step() : -get_step()));

		if (range_click_timer->is_one_shot()) {
			range_click_timer->set_wait_time(0.075);
			range_click_timer->set_one_shot(false);
			range_click_timer->start();
		}

	} else {
		range_click_timer->stop();
	}
}

void SpinBox::_release_mouse() {
	if (drag.enabled) {
		drag.enabled = false;
		Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
		warp_mouse(drag.capture_pos);
	}
}

void SpinBox::_gui_input(const Ref<InputEvent> &p_event) {
	if (!is_editable()) {
		return;
	}

	Ref<InputEventMouseButton> mb = p_event;

	if (mb.is_valid() && mb->is_pressed()) {
		bool up = mb->get_position().y < (get_size().height / 2);

		switch (mb->get_button_index()) {
			case MOUSE_BUTTON_LEFT: {
				line_edit->grab_focus();

				set_value(get_value() + (up ? get_step() : -get_step()));

				range_click_timer->set_wait_time(0.6);
				range_click_timer->set_one_shot(true);
				range_click_timer->start();

				drag.allowed = true;
				drag.capture_pos = mb->get_position();
			} break;
			case MOUSE_BUTTON_RIGHT: {
				line_edit->grab_focus();
				set_value((up ? get_max() : get_min()));
			} break;
			case MOUSE_BUTTON_WHEEL_UP: {
				if (line_edit->has_focus()) {
					set_value(get_value() + get_step() * mb->get_factor());
					accept_event();
				}
			} break;
			case MOUSE_BUTTON_WHEEL_DOWN: {
				if (line_edit->has_focus()) {
					set_value(get_value() - get_step() * mb->get_factor());
					accept_event();
				}
			} break;
		}
	}

	if (mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == MOUSE_BUTTON_LEFT) {
		//set_default_cursor_shape(CURSOR_ARROW);
		range_click_timer->stop();
		_release_mouse();
		drag.allowed = false;
	}

	Ref<InputEventMouseMotion> mm = p_event;

	if (mm.is_valid() && mm->get_button_mask() & MOUSE_BUTTON_MASK_LEFT) {
		if (drag.enabled) {
			drag.diff_y += mm->get_relative().y;
			float diff_y = -0.01 * Math::pow(ABS(drag.diff_y), 1.8f) * SGN(drag.diff_y);
			set_value(CLAMP(drag.base_val + get_step() * diff_y, get_min(), get_max()));
		} else if (drag.allowed && drag.capture_pos.distance_to(mm->get_position()) > 2) {
			Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
			drag.enabled = true;
			drag.base_val = get_value();
			drag.diff_y = 0;
		}
	}
}

void SpinBox::_line_edit_focus_exit() {
	// discontinue because the focus_exit was caused by right-click context menu
	if (line_edit->get_menu()->is_visible()) {
		return;
	}

	_text_entered(line_edit->get_text());
}

inline void SpinBox::_adjust_width_for_icon(const Ref<Texture2D> &icon) {
	int w = icon->get_width();
	if ((w != last_w)) {
		line_edit->set_offset(SIDE_LEFT, 0);
		line_edit->set_offset(SIDE_RIGHT, -w);
		last_w = w;
	}
}

void SpinBox::_notification(int p_what) {
	if (p_what == NOTIFICATION_DRAW) {
		Ref<Texture2D> updown = get_theme_icon("updown");

		_adjust_width_for_icon(updown);

		RID ci = get_canvas_item();
		Size2i size = get_size();

		if (is_layout_rtl()) {
			updown->draw(ci, Point2i(0, (size.height - updown->get_height()) / 2));
		} else {
			updown->draw(ci, Point2i(size.width - updown->get_width(), (size.height - updown->get_height()) / 2));
		}

	} else if (p_what == NOTIFICATION_FOCUS_EXIT) {
		//_value_changed(0);
	} else if (p_what == NOTIFICATION_ENTER_TREE) {
		_adjust_width_for_icon(get_theme_icon("updown"));
		_value_changed(0);
	} else if (p_what == NOTIFICATION_EXIT_TREE) {
		_release_mouse();
	} else if (p_what == NOTIFICATION_TRANSLATION_CHANGED) {
		_value_changed(0);
	} else if (p_what == NOTIFICATION_THEME_CHANGED) {
		call_deferred("minimum_size_changed");
		get_line_edit()->call_deferred("minimum_size_changed");
	} else if (p_what == NOTIFICATION_LAYOUT_DIRECTION_CHANGED || p_what == NOTIFICATION_TRANSLATION_CHANGED) {
		update();
	}
}

void SpinBox::set_align(LineEdit::Align p_align) {
	line_edit->set_align(p_align);
}

LineEdit::Align SpinBox::get_align() const {
	return line_edit->get_align();
}

void SpinBox::set_suffix(const String &p_suffix) {
	suffix = p_suffix;
	_value_changed(0);
}

String SpinBox::get_suffix() const {
	return suffix;
}

void SpinBox::set_prefix(const String &p_prefix) {
	prefix = p_prefix;
	_value_changed(0);
}

String SpinBox::get_prefix() const {
	return prefix;
}

void SpinBox::set_editable(bool p_editable) {
	line_edit->set_editable(p_editable);
}

bool SpinBox::is_editable() const {
	return line_edit->is_editable();
}

void SpinBox::apply() {
	_text_entered(line_edit->get_text());
}

void SpinBox::_bind_methods() {
	//ClassDB::bind_method(D_METHOD("_value_changed"),&SpinBox::_value_changed);
	ClassDB::bind_method(D_METHOD("_gui_input"), &SpinBox::_gui_input);
	ClassDB::bind_method(D_METHOD("set_align", "align"), &SpinBox::set_align);
	ClassDB::bind_method(D_METHOD("get_align"), &SpinBox::get_align);
	ClassDB::bind_method(D_METHOD("set_suffix", "suffix"), &SpinBox::set_suffix);
	ClassDB::bind_method(D_METHOD("get_suffix"), &SpinBox::get_suffix);
	ClassDB::bind_method(D_METHOD("set_prefix", "prefix"), &SpinBox::set_prefix);
	ClassDB::bind_method(D_METHOD("get_prefix"), &SpinBox::get_prefix);
	ClassDB::bind_method(D_METHOD("set_editable", "editable"), &SpinBox::set_editable);
	ClassDB::bind_method(D_METHOD("is_editable"), &SpinBox::is_editable);
	ClassDB::bind_method(D_METHOD("apply"), &SpinBox::apply);
	ClassDB::bind_method(D_METHOD("get_line_edit"), &SpinBox::get_line_edit);

	ADD_PROPERTY(PropertyInfo(Variant::INT, "align", PROPERTY_HINT_ENUM, "Left,Center,Right,Fill"), "set_align", "get_align");
	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editable"), "set_editable", "is_editable");
	ADD_PROPERTY(PropertyInfo(Variant::STRING, "prefix"), "set_prefix", "get_prefix");
	ADD_PROPERTY(PropertyInfo(Variant::STRING, "suffix"), "set_suffix", "get_suffix");
}

SpinBox::SpinBox() {
	line_edit = memnew(LineEdit);
	add_child(line_edit);

	line_edit->set_anchors_and_offsets_preset(Control::PRESET_WIDE);
	line_edit->set_mouse_filter(MOUSE_FILTER_PASS);
	line_edit->set_align(LineEdit::ALIGN_LEFT);

	//connect("value_changed",this,"_value_changed");
	line_edit->connect("text_entered", callable_mp(this, &SpinBox::_text_entered), Vector<Variant>(), CONNECT_DEFERRED);
	line_edit->connect("focus_exited", callable_mp(this, &SpinBox::_line_edit_focus_exit), Vector<Variant>(), CONNECT_DEFERRED);
	line_edit->connect("gui_input", callable_mp(this, &SpinBox::_line_edit_input));

	range_click_timer = memnew(Timer);
	range_click_timer->connect("timeout", callable_mp(this, &SpinBox::_range_click_timeout));
	add_child(range_click_timer);
}