2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* range.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2019-01-01 11:53:14 +00:00
|
|
|
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 01:10:30 +00: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. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "range.h"
|
|
|
|
|
2018-09-23 18:59:35 +00:00
|
|
|
String Range::get_configuration_warning() const {
|
|
|
|
String warning = Control::get_configuration_warning();
|
|
|
|
|
|
|
|
if (shared->exp_ratio && shared->min <= 0) {
|
|
|
|
if (warning != String()) {
|
2019-07-08 22:17:04 +00:00
|
|
|
warning += "\n\n";
|
2018-09-23 18:59:35 +00:00
|
|
|
}
|
2019-07-08 22:17:04 +00:00
|
|
|
warning += TTR("If \"Exp Edit\" is enabled, \"Min Value\" must be greater than 0.");
|
2018-09-23 18:59:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return warning;
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void Range::_value_changed_notify() {
|
|
|
|
|
|
|
|
_value_changed(shared->val);
|
2017-03-05 15:44:50 +00:00
|
|
|
emit_signal("value_changed", shared->val);
|
2014-02-10 01:10:30 +00:00
|
|
|
update();
|
2017-04-03 16:34:44 +00:00
|
|
|
_change_notify("value");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Range::Shared::emit_value_changed() {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (Set<Range *>::Element *E = owners.front(); E; E = E->next()) {
|
|
|
|
Range *r = E->get();
|
2014-11-06 00:20:42 +00:00
|
|
|
if (!r->is_inside_tree())
|
2014-02-10 01:10:30 +00:00
|
|
|
continue;
|
|
|
|
r->_value_changed_notify();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-06 12:54:13 +00:00
|
|
|
void Range::_changed_notify(const char *p_what) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-06-16 15:00:49 +00:00
|
|
|
emit_signal("changed");
|
2014-02-10 01:10:30 +00:00
|
|
|
update();
|
2016-01-06 12:54:13 +00:00
|
|
|
_change_notify(p_what);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2016-01-06 12:54:13 +00:00
|
|
|
void Range::Shared::emit_changed(const char *p_what) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (Set<Range *>::Element *E = owners.front(); E; E = E->next()) {
|
|
|
|
Range *r = E->get();
|
2014-11-06 00:20:42 +00:00
|
|
|
if (!r->is_inside_tree())
|
2014-02-10 01:10:30 +00:00
|
|
|
continue;
|
2016-01-06 12:54:13 +00:00
|
|
|
r->_changed_notify(p_what);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-04 04:16:14 +00:00
|
|
|
void Range::set_value(double p_val) {
|
2019-06-16 15:00:49 +00:00
|
|
|
|
2018-09-24 16:09:00 +00:00
|
|
|
if (shared->step > 0)
|
|
|
|
p_val = Math::round(p_val / shared->step) * shared->step;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2018-09-24 16:09:00 +00:00
|
|
|
if (_rounded_values)
|
2014-02-10 01:10:30 +00:00
|
|
|
p_val = Math::round(p_val);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2018-05-16 12:13:41 +00:00
|
|
|
if (!shared->allow_greater && p_val > shared->max - shared->page)
|
2017-03-05 15:44:50 +00:00
|
|
|
p_val = shared->max - shared->page;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2018-05-16 12:13:41 +00:00
|
|
|
if (!shared->allow_lesser && p_val < shared->min)
|
2017-03-05 15:44:50 +00:00
|
|
|
p_val = shared->min;
|
2016-01-08 12:07:15 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (shared->val == p_val)
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
shared->val = p_val;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
shared->emit_value_changed();
|
|
|
|
}
|
|
|
|
void Range::set_min(double p_min) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
shared->min = p_min;
|
2017-01-04 04:16:14 +00:00
|
|
|
set_value(shared->val);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-04-03 16:34:44 +00:00
|
|
|
shared->emit_changed("min");
|
2018-09-23 18:59:35 +00:00
|
|
|
|
|
|
|
update_configuration_warning();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
void Range::set_max(double p_max) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
shared->max = p_max;
|
2017-01-04 04:16:14 +00:00
|
|
|
set_value(shared->val);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-04-03 16:34:44 +00:00
|
|
|
shared->emit_changed("max");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
void Range::set_step(double p_step) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
shared->step = p_step;
|
2017-04-03 16:34:44 +00:00
|
|
|
shared->emit_changed("step");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
void Range::set_page(double p_page) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
shared->page = p_page;
|
2017-01-04 04:16:14 +00:00
|
|
|
set_value(shared->val);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-04-03 16:34:44 +00:00
|
|
|
shared->emit_changed("page");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-01-04 04:16:14 +00:00
|
|
|
double Range::get_value() const {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return shared->val;
|
|
|
|
}
|
|
|
|
double Range::get_min() const {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return shared->min;
|
|
|
|
}
|
|
|
|
double Range::get_max() const {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return shared->max;
|
|
|
|
}
|
|
|
|
double Range::get_step() const {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return shared->step;
|
|
|
|
}
|
|
|
|
double Range::get_page() const {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return shared->page;
|
|
|
|
}
|
|
|
|
|
2017-01-13 17:08:30 +00:00
|
|
|
void Range::set_as_ratio(double p_value) {
|
2016-10-15 16:20:27 +00:00
|
|
|
|
|
|
|
double v;
|
|
|
|
|
2018-05-15 20:12:35 +00:00
|
|
|
if (shared->exp_ratio && get_min() >= 0) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-05-15 20:12:35 +00:00
|
|
|
double exp_min = get_min() == 0 ? 0.0 : Math::log(get_min()) / Math::log((double)2);
|
2017-03-05 15:44:50 +00:00
|
|
|
double exp_max = Math::log(get_max()) / Math::log((double)2);
|
|
|
|
v = Math::pow(2, exp_min + (exp_max - exp_min) * p_value);
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
2016-10-15 16:20:27 +00:00
|
|
|
|
|
|
|
double percent = (get_max() - get_min()) * p_value;
|
|
|
|
if (get_step() > 0) {
|
|
|
|
double steps = round(percent / get_step());
|
|
|
|
v = steps * get_step() + get_min();
|
|
|
|
} else {
|
|
|
|
v = percent + get_min();
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2018-05-16 12:13:41 +00:00
|
|
|
v = CLAMP(v, get_min(), get_max());
|
2017-03-05 15:44:50 +00:00
|
|
|
set_value(v);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2017-01-13 17:08:30 +00:00
|
|
|
double Range::get_as_ratio() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-05-15 20:12:35 +00:00
|
|
|
if (shared->exp_ratio && get_min() >= 0) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-05-15 20:12:35 +00:00
|
|
|
double exp_min = get_min() == 0 ? 0.0 : Math::log(get_min()) / Math::log((double)2);
|
2017-03-05 15:44:50 +00:00
|
|
|
double exp_max = Math::log(get_max()) / Math::log((double)2);
|
2018-05-16 12:13:41 +00:00
|
|
|
float value = CLAMP(get_value(), shared->min, shared->max);
|
|
|
|
double v = Math::log(value) / Math::log((double)2);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return (v - exp_min) / (exp_max - exp_min);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2018-05-16 12:13:41 +00:00
|
|
|
float value = CLAMP(get_value(), shared->min, shared->max);
|
|
|
|
return (value - get_min()) / (get_max() - get_min());
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Range::_share(Node *p_range) {
|
|
|
|
|
2017-08-24 20:58:51 +00:00
|
|
|
Range *r = Object::cast_to<Range>(p_range);
|
2014-02-10 01:10:30 +00:00
|
|
|
ERR_FAIL_COND(!r);
|
|
|
|
share(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Range::share(Range *p_range) {
|
|
|
|
|
|
|
|
ERR_FAIL_NULL(p_range);
|
|
|
|
|
|
|
|
p_range->_ref_shared(shared);
|
|
|
|
p_range->_changed_notify();
|
|
|
|
p_range->_value_changed_notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Range::unshare() {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Shared *nshared = memnew(Shared);
|
|
|
|
nshared->min = shared->min;
|
|
|
|
nshared->max = shared->max;
|
|
|
|
nshared->val = shared->val;
|
|
|
|
nshared->step = shared->step;
|
|
|
|
nshared->page = shared->page;
|
2018-05-16 12:13:41 +00:00
|
|
|
nshared->allow_greater = shared->allow_greater;
|
|
|
|
nshared->allow_lesser = shared->allow_lesser;
|
2014-02-10 01:10:30 +00:00
|
|
|
_unref_shared();
|
|
|
|
_ref_shared(nshared);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Range::_ref_shared(Shared *p_shared) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (shared && p_shared == shared)
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
_unref_shared();
|
2017-03-05 15:44:50 +00:00
|
|
|
shared = p_shared;
|
2014-02-10 01:10:30 +00:00
|
|
|
shared->owners.insert(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Range::_unref_shared() {
|
|
|
|
|
2017-09-28 07:45:05 +00:00
|
|
|
if (shared) {
|
|
|
|
shared->owners.erase(this);
|
|
|
|
if (shared->owners.size() == 0) {
|
|
|
|
memdelete(shared);
|
|
|
|
shared = NULL;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Range::_bind_methods() {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_value"), &Range::get_value);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_min"), &Range::get_min);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_max"), &Range::get_max);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_step"), &Range::get_step);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_page"), &Range::get_page);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_as_ratio"), &Range::get_as_ratio);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_value", "value"), &Range::set_value);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_min", "minimum"), &Range::set_min);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_max", "maximum"), &Range::set_max);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_step", "step"), &Range::set_step);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_page", "pagesize"), &Range::set_page);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_as_ratio", "value"), &Range::set_as_ratio);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_use_rounded_values", "enabled"), &Range::set_use_rounded_values);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_using_rounded_values"), &Range::is_using_rounded_values);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_exp_ratio", "enabled"), &Range::set_exp_ratio);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_ratio_exp"), &Range::is_ratio_exp);
|
2018-05-16 12:13:41 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_allow_greater", "allow"), &Range::set_allow_greater);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_greater_allowed"), &Range::is_greater_allowed);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_allow_lesser", "allow"), &Range::set_allow_lesser);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_lesser_allowed"), &Range::is_lesser_allowed);
|
2017-03-05 15:44:50 +00:00
|
|
|
|
2017-08-09 11:19:41 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("share", "with"), &Range::_share);
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("unshare"), &Range::unshare);
|
|
|
|
|
|
|
|
ADD_SIGNAL(MethodInfo("value_changed", PropertyInfo(Variant::REAL, "value")));
|
|
|
|
ADD_SIGNAL(MethodInfo("changed"));
|
|
|
|
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::REAL, "min_value"), "set_min", "get_min");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::REAL, "max_value"), "set_max", "get_max");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::REAL, "step"), "set_step", "get_step");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::REAL, "page"), "set_page", "get_page");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::REAL, "value"), "set_value", "get_value");
|
2018-01-11 22:35:12 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::REAL, "ratio", PROPERTY_HINT_RANGE, "0,1,0.01", 0), "set_as_ratio", "get_as_ratio");
|
2017-03-05 15:44:50 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "exp_edit"), "set_exp_ratio", "is_ratio_exp");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "rounded"), "set_use_rounded_values", "is_using_rounded_values");
|
2018-08-16 12:24:17 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_greater"), "set_allow_greater", "is_greater_allowed");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_lesser"), "set_allow_lesser", "is_lesser_allowed");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 17:08:30 +00:00
|
|
|
void Range::set_use_rounded_values(bool p_enable) {
|
2015-12-27 23:12:13 +00:00
|
|
|
|
|
|
|
_rounded_values = p_enable;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 17:08:30 +00:00
|
|
|
bool Range::is_using_rounded_values() const {
|
2015-12-27 23:12:13 +00:00
|
|
|
|
|
|
|
return _rounded_values;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 17:08:30 +00:00
|
|
|
void Range::set_exp_ratio(bool p_enable) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
shared->exp_ratio = p_enable;
|
2018-09-23 18:59:35 +00:00
|
|
|
|
|
|
|
update_configuration_warning();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 17:08:30 +00:00
|
|
|
bool Range::is_ratio_exp() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-01-13 17:08:30 +00:00
|
|
|
return shared->exp_ratio;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2018-05-16 12:13:41 +00:00
|
|
|
void Range::set_allow_greater(bool p_allow) {
|
2019-06-16 15:00:49 +00:00
|
|
|
|
2018-05-16 12:13:41 +00:00
|
|
|
shared->allow_greater = p_allow;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Range::is_greater_allowed() const {
|
2019-06-16 15:00:49 +00:00
|
|
|
|
2018-05-16 12:13:41 +00:00
|
|
|
return shared->allow_greater;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Range::set_allow_lesser(bool p_allow) {
|
2019-06-16 15:00:49 +00:00
|
|
|
|
2018-05-16 12:13:41 +00:00
|
|
|
shared->allow_lesser = p_allow;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Range::is_lesser_allowed() const {
|
2019-06-16 15:00:49 +00:00
|
|
|
|
2018-05-16 12:13:41 +00:00
|
|
|
return shared->allow_lesser;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Range::Range() {
|
2019-06-16 15:00:49 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
shared = memnew(Shared);
|
2017-03-05 15:44:50 +00:00
|
|
|
shared->min = 0;
|
|
|
|
shared->max = 100;
|
2017-07-21 11:57:46 +00:00
|
|
|
shared->val = 0;
|
|
|
|
shared->step = 1;
|
2017-03-05 15:44:50 +00:00
|
|
|
shared->page = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
shared->owners.insert(this);
|
2017-03-05 15:44:50 +00:00
|
|
|
shared->exp_ratio = false;
|
2018-05-16 12:13:41 +00:00
|
|
|
shared->allow_greater = false;
|
|
|
|
shared->allow_lesser = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
_rounded_values = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Range::~Range() {
|
|
|
|
|
|
|
|
_unref_shared();
|
|
|
|
}
|