2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* spin_box.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:11:45 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2017-01-01 22:01:57 +01:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-08 00:45:00 +02:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 02:10:30 +01:00
|
|
|
/* */
|
|
|
|
/* 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"
|
2015-08-29 06:43:21 +02:00
|
|
|
#include "os/input.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Size2 SpinBox::get_minimum_size() const {
|
|
|
|
|
|
|
|
Size2 ms = line_edit->get_combined_minimum_size();
|
2017-03-19 00:36:26 +01:00
|
|
|
ms.width += last_w;
|
2014-02-10 02:10:30 +01:00
|
|
|
return ms;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpinBox::_value_changed(double) {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
String value = String::num(get_val(), Math::step_decimals(get_step()));
|
|
|
|
if (prefix != "")
|
|
|
|
value = prefix + " " + value;
|
|
|
|
if (suffix != "")
|
|
|
|
value += " " + suffix;
|
2014-02-10 02:10:30 +01:00
|
|
|
line_edit->set_text(value);
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void SpinBox::_text_entered(const String &p_string) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
//if (!p_string.is_numeric())
|
|
|
|
// return;
|
2016-11-04 18:34:19 +01:00
|
|
|
String value = p_string;
|
2017-03-19 00:36:26 +01:00
|
|
|
if (prefix != "" && p_string.begins_with(prefix))
|
|
|
|
value = p_string.substr(prefix.length(), p_string.length() - prefix.length());
|
|
|
|
set_val(value.to_double());
|
2014-02-10 02:10:30 +01:00
|
|
|
_value_changed(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
LineEdit *SpinBox::get_line_edit() {
|
|
|
|
|
|
|
|
return line_edit;
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void SpinBox::_line_edit_input(const InputEvent &p_event) {
|
2015-08-29 06:43:21 +02:00
|
|
|
}
|
|
|
|
|
2015-12-08 19:04:56 +01:00
|
|
|
void SpinBox::_range_click_timeout() {
|
|
|
|
|
|
|
|
if (!drag.enabled && Input::get_singleton()->is_mouse_button_pressed(BUTTON_LEFT)) {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
bool up = get_local_mouse_pos().y < (get_size().height / 2);
|
|
|
|
set_val(get_val() + (up ? get_step() : -get_step()));
|
2015-12-08 19:04:56 +01:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void SpinBox::_input_event(const InputEvent &p_event) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-11-24 19:15:42 +01:00
|
|
|
if (!is_editable()) {
|
|
|
|
return;
|
|
|
|
}
|
2017-03-19 00:36:26 +01:00
|
|
|
if (p_event.type == InputEvent::MOUSE_BUTTON && p_event.mouse_button.pressed) {
|
|
|
|
const InputEventMouseButton &mb = p_event.mouse_button;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
bool up = mb.y < (get_size().height / 2);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
switch (mb.button_index) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
case BUTTON_LEFT: {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
set_val(get_val() + (up ? get_step() : -get_step()));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2015-12-08 19:04:56 +01:00
|
|
|
range_click_timer->set_wait_time(0.6);
|
|
|
|
range_click_timer->set_one_shot(true);
|
|
|
|
range_click_timer->start();
|
|
|
|
|
2016-11-24 19:15:42 +01:00
|
|
|
line_edit->grab_focus();
|
2014-02-10 02:10:30 +01:00
|
|
|
} break;
|
|
|
|
case BUTTON_RIGHT: {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
set_val((up ? get_max() : get_min()));
|
2016-11-24 19:15:42 +01:00
|
|
|
line_edit->grab_focus();
|
2014-02-10 02:10:30 +01:00
|
|
|
} break;
|
|
|
|
case BUTTON_WHEEL_UP: {
|
2016-11-24 19:15:42 +01:00
|
|
|
if (line_edit->has_focus()) {
|
2017-05-13 06:02:26 +02:00
|
|
|
|
|
|
|
set_val(get_val() + get_step() * mb.factor);
|
2016-11-24 19:15:42 +01:00
|
|
|
accept_event();
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
} break;
|
|
|
|
case BUTTON_WHEEL_DOWN: {
|
2016-11-24 19:15:42 +01:00
|
|
|
if (line_edit->has_focus()) {
|
2017-05-13 06:02:26 +02:00
|
|
|
|
|
|
|
set_val(get_val() - get_step() * mb.factor);
|
2016-11-24 19:15:42 +01:00
|
|
|
accept_event();
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
2015-08-29 06:43:21 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (p_event.type == InputEvent::MOUSE_BUTTON && p_event.mouse_button.pressed && p_event.mouse_button.button_index == 1) {
|
2015-08-29 06:43:21 +02:00
|
|
|
|
|
|
|
//set_default_cursor_shape(CURSOR_VSIZE);
|
2017-03-19 00:36:26 +01:00
|
|
|
Vector2 cpos = Vector2(p_event.mouse_button.x, p_event.mouse_button.y);
|
|
|
|
drag.mouse_pos = cpos;
|
2015-08-29 06:43:21 +02:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (p_event.type == InputEvent::MOUSE_BUTTON && !p_event.mouse_button.pressed && p_event.mouse_button.button_index == 1) {
|
2015-08-29 06:43:21 +02:00
|
|
|
|
|
|
|
//set_default_cursor_shape(CURSOR_ARROW);
|
2015-12-08 19:04:56 +01:00
|
|
|
range_click_timer->stop();
|
|
|
|
|
2015-08-29 06:43:21 +02:00
|
|
|
if (drag.enabled) {
|
2017-03-19 00:36:26 +01:00
|
|
|
drag.enabled = false;
|
2015-08-29 06:43:21 +02:00
|
|
|
Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
|
|
|
|
warp_mouse(drag.capture_pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (p_event.type == InputEvent::MOUSE_MOTION && p_event.mouse_button.button_mask & 1) {
|
2015-08-29 06:43:21 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Vector2 cpos = Vector2(p_event.mouse_motion.x, p_event.mouse_motion.y);
|
2015-08-29 06:43:21 +02:00
|
|
|
if (drag.enabled) {
|
|
|
|
|
|
|
|
float diff_y = drag.mouse_pos.y - cpos.y;
|
2017-03-19 00:36:26 +01:00
|
|
|
diff_y = Math::pow(ABS(diff_y), 1.8) * SGN(diff_y);
|
|
|
|
diff_y *= 0.1;
|
2015-08-29 06:43:21 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
drag.mouse_pos = cpos;
|
|
|
|
drag.base_val = CLAMP(drag.base_val + get_step() * diff_y, get_min(), get_max());
|
2015-08-29 06:43:21 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
set_val(drag.base_val);
|
2015-08-29 06:43:21 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
} else if (drag.mouse_pos.distance_to(cpos) > 2) {
|
2015-08-29 06:43:21 +02:00
|
|
|
|
|
|
|
Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
|
2017-03-19 00:36:26 +01:00
|
|
|
drag.enabled = true;
|
|
|
|
drag.base_val = get_val();
|
|
|
|
drag.mouse_pos = cpos;
|
|
|
|
drag.capture_pos = cpos;
|
2015-08-29 06:43:21 +02:00
|
|
|
}
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SpinBox::_line_edit_focus_exit() {
|
|
|
|
|
|
|
|
_text_entered(line_edit->get_text());
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpinBox::_notification(int p_what) {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (p_what == NOTIFICATION_DRAW) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Ref<Texture> updown = get_icon("updown");
|
|
|
|
|
|
|
|
int w = updown->get_width();
|
2017-03-19 00:36:26 +01:00
|
|
|
if (w != last_w) {
|
|
|
|
line_edit->set_margin(MARGIN_RIGHT, w);
|
|
|
|
last_w = w;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
RID ci = get_canvas_item();
|
|
|
|
Size2i size = get_size();
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
updown->draw(ci, Point2i(size.width - updown->get_width(), (size.height - updown->get_height()) / 2));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
} else if (p_what == NOTIFICATION_FOCUS_EXIT) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
//_value_changed(0);
|
2017-03-19 00:36:26 +01:00
|
|
|
} else if (p_what == NOTIFICATION_ENTER_TREE) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
_value_changed(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void SpinBox::set_suffix(const String &p_suffix) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
suffix = p_suffix;
|
2014-02-10 02:10:30 +01:00
|
|
|
_value_changed(0);
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
String SpinBox::get_suffix() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return suffix;
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void SpinBox::set_prefix(const String &p_prefix) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
prefix = p_prefix;
|
2014-02-10 02:10:30 +01:00
|
|
|
_value_changed(0);
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
String SpinBox::get_prefix() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
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::_bind_methods() {
|
|
|
|
|
|
|
|
//ObjectTypeDB::bind_method(_MD("_value_changed"),&SpinBox::_value_changed);
|
2017-03-19 00:36:26 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("_input_event"), &SpinBox::_input_event);
|
|
|
|
ObjectTypeDB::bind_method(_MD("_text_entered"), &SpinBox::_text_entered);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_suffix", "suffix"), &SpinBox::set_suffix);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_suffix"), &SpinBox::get_suffix);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_prefix", "prefix"), &SpinBox::set_prefix);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_prefix"), &SpinBox::get_prefix);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_editable", "editable"), &SpinBox::set_editable);
|
|
|
|
ObjectTypeDB::bind_method(_MD("is_editable"), &SpinBox::is_editable);
|
|
|
|
ObjectTypeDB::bind_method(_MD("_line_edit_focus_exit"), &SpinBox::_line_edit_focus_exit);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_line_edit"), &SpinBox::get_line_edit);
|
|
|
|
ObjectTypeDB::bind_method(_MD("_line_edit_input"), &SpinBox::_line_edit_input);
|
|
|
|
ObjectTypeDB::bind_method(_MD("_range_click_timeout"), &SpinBox::_range_click_timeout);
|
|
|
|
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editable"), _SCS("set_editable"), _SCS("is_editable"));
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "prefix"), _SCS("set_prefix"), _SCS("get_prefix"));
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "suffix"), _SCS("set_suffix"), _SCS("get_suffix"));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SpinBox::SpinBox() {
|
|
|
|
|
|
|
|
last_w = 0;
|
2017-03-19 00:36:26 +01:00
|
|
|
line_edit = memnew(LineEdit);
|
2014-02-10 02:10:30 +01:00
|
|
|
add_child(line_edit);
|
|
|
|
|
|
|
|
line_edit->set_area_as_parent_rect();
|
|
|
|
//connect("value_changed",this,"_value_changed");
|
2017-03-19 00:36:26 +01:00
|
|
|
line_edit->connect("text_entered", this, "_text_entered", Vector<Variant>(), CONNECT_DEFERRED);
|
|
|
|
line_edit->connect("focus_exit", this, "_line_edit_focus_exit", Vector<Variant>(), CONNECT_DEFERRED);
|
|
|
|
line_edit->connect("input_event", this, "_line_edit_input");
|
|
|
|
drag.enabled = false;
|
2015-12-08 19:04:56 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
range_click_timer = memnew(Timer);
|
|
|
|
range_click_timer->connect("timeout", this, "_range_click_timeout");
|
2015-12-08 19:04:56 +01:00
|
|
|
add_child(range_click_timer);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|