2014-02-09 22:10:30 -03:00
|
|
|
/**************************************************************************/
|
|
|
|
/* range.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* 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-05 00:50:27 +01:00
|
|
|
|
2014-02-09 22:10:30 -03:00
|
|
|
#include "range.h"
|
|
|
|
|
2025-08-12 04:02:26 -07:00
|
|
|
#include "thirdparty/misc/r128.h"
|
|
|
|
|
|
|
|
double Range::_snapped_r128(double p_value, double p_step) {
|
2025-08-22 13:32:50 -07:00
|
|
|
if (p_step == 0.0) {
|
|
|
|
return p_value;
|
2025-08-12 04:02:26 -07:00
|
|
|
}
|
2025-08-22 13:32:50 -07:00
|
|
|
if (p_value > (1e18 * p_step) || p_value < -(1e18 * p_step)) {
|
|
|
|
// If the value goes outside of the range R128 supports, fallback to normal snapping.
|
|
|
|
return Math::snapped(p_value, p_step);
|
|
|
|
}
|
|
|
|
// Rescale values to better utilize R128's range before snapping.
|
|
|
|
// R128 is fixed-precision with 64 bits after the decimal point, but double already uses 53 of those,
|
|
|
|
// so a step size finer than 2^-11 will lose precision, and in practice even 1e-3 can be problematic.
|
|
|
|
// By rescaling the value and step, we can shift precision into the higher bits (effectively turning R128 into a makeshift float).
|
2025-08-29 20:31:29 -07:00
|
|
|
const int decimals = MIN(18, 14 - Math::floor(std::log10(MAX(Math::abs(p_value), p_step))));
|
2025-08-22 13:32:50 -07:00
|
|
|
const double scale = Math::pow(10.0, decimals);
|
|
|
|
p_value *= scale;
|
|
|
|
p_step *= scale;
|
|
|
|
// All these lines are the equivalent of: p_value = Math::floor(p_value / p_step + 0.5) * p_step;
|
|
|
|
// Convert to String to force rounding to a decimal value (not a binary one).
|
|
|
|
String step_str = String::num(p_step);
|
|
|
|
String value_str = String::num(p_value);
|
|
|
|
R128 step_r128;
|
|
|
|
R128 value_r128;
|
|
|
|
const R128 half_r128 = R128(0.5);
|
|
|
|
r128FromString(&step_r128, step_str.ascii().get_data(), nullptr);
|
|
|
|
r128FromString(&value_r128, value_str.ascii().get_data(), nullptr);
|
|
|
|
r128Div(&value_r128, &value_r128, &step_r128);
|
|
|
|
r128Add(&value_r128, &value_r128, &half_r128);
|
|
|
|
r128Floor(&value_r128, &value_r128);
|
|
|
|
r128Mul(&value_r128, &value_r128, &step_r128);
|
|
|
|
if (scale != 1.0) {
|
|
|
|
const R128 scale_r128 = R128(scale);
|
|
|
|
r128Div(&value_r128, &value_r128, &scale_r128);
|
|
|
|
}
|
|
|
|
p_value = (double)value_r128;
|
2025-08-12 04:02:26 -07:00
|
|
|
return p_value;
|
|
|
|
}
|
|
|
|
|
2024-02-17 19:03:21 +01:00
|
|
|
PackedStringArray Range::get_configuration_warnings() const {
|
2024-06-19 16:40:50 +02:00
|
|
|
PackedStringArray warnings = Control::get_configuration_warnings();
|
2018-09-23 20:59:35 +02:00
|
|
|
|
2025-04-12 10:15:19 +02:00
|
|
|
if (shared->exp_ratio && shared->min < 0) {
|
|
|
|
warnings.push_back(RTR("If \"Exp Edit\" is enabled, \"Min Value\" must be greater or equal to 0."));
|
2018-09-23 20:59:35 +02:00
|
|
|
}
|
|
|
|
|
2020-10-29 05:01:28 -05:00
|
|
|
return warnings;
|
2018-09-23 20:59:35 +02:00
|
|
|
}
|
|
|
|
|
2022-03-10 08:17:38 +01:00
|
|
|
void Range::_value_changed(double p_value) {
|
|
|
|
GDVIRTUAL_CALL(_value_changed, p_value);
|
|
|
|
}
|
2025-03-21 16:42:23 +02:00
|
|
|
|
2014-02-09 22:10:30 -03:00
|
|
|
void Range::_value_changed_notify() {
|
|
|
|
_value_changed(shared->val);
|
2024-05-14 11:42:00 +02:00
|
|
|
emit_signal(SceneStringName(value_changed), shared->val);
|
2025-03-21 16:42:23 +02:00
|
|
|
queue_accessibility_update();
|
2022-08-13 23:21:24 +02:00
|
|
|
queue_redraw();
|
2014-02-09 22:10:30 -03:00
|
|
|
}
|
|
|
|
|
2025-03-21 16:42:23 +02:00
|
|
|
void Range::_accessibility_action_inc(const Variant &p_data) {
|
|
|
|
double step = ((shared->step > 0) ? shared->step : 1);
|
|
|
|
set_value(shared->val + step);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Range::_accessibility_action_dec(const Variant &p_data) {
|
|
|
|
double step = ((shared->step > 0) ? shared->step : 1);
|
|
|
|
set_value(shared->val - step);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Range::_accessibility_action_set_value(const Variant &p_data) {
|
|
|
|
double new_val = p_data;
|
|
|
|
set_value(new_val);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Range::_notification(int p_what) {
|
|
|
|
ERR_MAIN_THREAD_GUARD;
|
|
|
|
switch (p_what) {
|
|
|
|
case NOTIFICATION_ACCESSIBILITY_UPDATE: {
|
|
|
|
RID ae = get_accessibility_element();
|
|
|
|
ERR_FAIL_COND(ae.is_null());
|
|
|
|
|
|
|
|
DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_SPIN_BUTTON);
|
|
|
|
DisplayServer::get_singleton()->accessibility_update_set_num_value(ae, shared->val);
|
|
|
|
DisplayServer::get_singleton()->accessibility_update_set_num_range(ae, shared->min, shared->max);
|
|
|
|
if (shared->step > 0) {
|
|
|
|
DisplayServer::get_singleton()->accessibility_update_set_num_step(ae, shared->step);
|
|
|
|
} else {
|
|
|
|
DisplayServer::get_singleton()->accessibility_update_set_num_step(ae, 1);
|
|
|
|
}
|
|
|
|
DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_DECREMENT, callable_mp(this, &Range::_accessibility_action_dec));
|
|
|
|
DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_INCREMENT, callable_mp(this, &Range::_accessibility_action_inc));
|
|
|
|
DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SET_VALUE, callable_mp(this, &Range::_accessibility_action_set_value));
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-09 22:10:30 -03:00
|
|
|
void Range::Shared::emit_value_changed() {
|
2022-05-18 17:43:40 -06:00
|
|
|
for (Range *E : owners) {
|
|
|
|
Range *r = E;
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!r->is_inside_tree()) {
|
2014-02-09 22:10:30 -03:00
|
|
|
continue;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-09 22:10:30 -03:00
|
|
|
r->_value_changed_notify();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-04-12 10:42:13 +02:00
|
|
|
void Range::_changed_notify() {
|
2024-05-13 16:56:03 +02:00
|
|
|
emit_signal(CoreStringName(changed));
|
2022-08-13 23:21:24 +02:00
|
|
|
queue_redraw();
|
2014-02-09 22:10:30 -03:00
|
|
|
}
|
|
|
|
|
2025-04-12 10:42:13 +02:00
|
|
|
void Range::Shared::emit_changed() {
|
2022-05-18 17:43:40 -06:00
|
|
|
for (Range *E : owners) {
|
|
|
|
Range *r = E;
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!r->is_inside_tree()) {
|
2014-02-09 22:10:30 -03:00
|
|
|
continue;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2025-04-12 10:42:13 +02:00
|
|
|
r->_changed_notify();
|
2014-02-09 22:10:30 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-02 15:15:36 +02:00
|
|
|
void Range::Shared::redraw_owners() {
|
|
|
|
for (Range *E : owners) {
|
|
|
|
Range *r = E;
|
|
|
|
if (!r->is_inside_tree()) {
|
|
|
|
continue;
|
|
|
|
}
|
2025-03-21 16:42:23 +02:00
|
|
|
r->queue_accessibility_update();
|
2023-01-02 15:15:36 +02:00
|
|
|
r->queue_redraw();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-04 01:16:14 -03:00
|
|
|
void Range::set_value(double p_val) {
|
2022-10-31 23:12:53 +01:00
|
|
|
double prev_val = shared->val;
|
2023-01-02 15:15:36 +02:00
|
|
|
_set_value_no_signal(p_val);
|
2022-10-31 23:12:53 +01:00
|
|
|
|
|
|
|
if (shared->val != prev_val) {
|
|
|
|
shared->emit_value_changed();
|
|
|
|
}
|
2025-03-21 16:42:23 +02:00
|
|
|
queue_accessibility_update();
|
2022-10-31 23:12:53 +01:00
|
|
|
}
|
|
|
|
|
2023-01-02 15:15:36 +02:00
|
|
|
void Range::_set_value_no_signal(double p_val) {
|
2025-06-09 18:57:24 +08:00
|
|
|
shared->val = _calc_value(p_val, shared->step);
|
|
|
|
}
|
|
|
|
|
|
|
|
double Range::_calc_value(double p_val, double p_step) const {
|
|
|
|
if (p_step > 0) {
|
2025-07-29 15:30:05 -07:00
|
|
|
// Subtract min to support cases like min = 0.1, step = 0.2, snaps to 0.1, 0.3, 0.5, etc.
|
2025-08-12 04:02:26 -07:00
|
|
|
p_val = _snapped_r128(p_val - shared->min, p_step) + shared->min;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (_rounded_values) {
|
2014-02-09 22:10:30 -03:00
|
|
|
p_val = Math::round(p_val);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!shared->allow_greater && p_val > shared->max - shared->page) {
|
2014-02-09 22:10:30 -03:00
|
|
|
p_val = shared->max - shared->page;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!shared->allow_lesser && p_val < shared->min) {
|
2014-02-09 22:10:30 -03:00
|
|
|
p_val = shared->min;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2025-06-09 18:57:24 +08:00
|
|
|
return p_val;
|
2014-02-09 22:10:30 -03:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2023-01-02 15:15:36 +02:00
|
|
|
void Range::set_value_no_signal(double p_val) {
|
|
|
|
double prev_val = shared->val;
|
|
|
|
_set_value_no_signal(p_val);
|
|
|
|
|
|
|
|
if (shared->val != prev_val) {
|
|
|
|
shared->redraw_owners();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-09 22:10:30 -03:00
|
|
|
void Range::set_min(double p_min) {
|
2022-03-16 15:50:48 +08:00
|
|
|
if (shared->min == p_min) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-09 22:10:30 -03:00
|
|
|
shared->min = p_min;
|
2022-10-20 11:32:47 +08:00
|
|
|
shared->max = MAX(shared->max, shared->min);
|
|
|
|
shared->page = CLAMP(shared->page, 0, shared->max - shared->min);
|
2017-01-04 01:16:14 -03:00
|
|
|
set_value(shared->val);
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2025-04-12 10:42:13 +02:00
|
|
|
shared->emit_changed();
|
2018-09-23 20:59:35 +02:00
|
|
|
|
2020-10-29 05:01:28 -05:00
|
|
|
update_configuration_warnings();
|
2025-03-21 16:42:23 +02:00
|
|
|
|
|
|
|
queue_accessibility_update();
|
2014-02-09 22:10:30 -03:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2014-02-09 22:10:30 -03:00
|
|
|
void Range::set_max(double p_max) {
|
2022-10-20 11:32:47 +08:00
|
|
|
double max_validated = MAX(p_max, shared->min);
|
|
|
|
if (shared->max == max_validated) {
|
2022-03-16 15:50:48 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-20 11:32:47 +08:00
|
|
|
shared->max = max_validated;
|
|
|
|
shared->page = CLAMP(shared->page, 0, shared->max - shared->min);
|
2017-01-04 01:16:14 -03:00
|
|
|
set_value(shared->val);
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2025-04-12 10:42:13 +02:00
|
|
|
shared->emit_changed();
|
2025-03-21 16:42:23 +02:00
|
|
|
|
|
|
|
queue_accessibility_update();
|
2014-02-09 22:10:30 -03:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2014-02-09 22:10:30 -03:00
|
|
|
void Range::set_step(double p_step) {
|
2022-03-16 15:50:48 +08:00
|
|
|
if (shared->step == p_step) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-09 22:10:30 -03:00
|
|
|
shared->step = p_step;
|
2025-04-12 10:42:13 +02:00
|
|
|
shared->emit_changed();
|
2025-03-21 16:42:23 +02:00
|
|
|
|
|
|
|
queue_accessibility_update();
|
2014-02-09 22:10:30 -03:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2014-02-09 22:10:30 -03:00
|
|
|
void Range::set_page(double p_page) {
|
2022-10-20 11:32:47 +08:00
|
|
|
double page_validated = CLAMP(p_page, 0, shared->max - shared->min);
|
|
|
|
if (shared->page == page_validated) {
|
2022-03-16 15:50:48 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-20 11:32:47 +08:00
|
|
|
shared->page = page_validated;
|
2017-01-04 01:16:14 -03:00
|
|
|
set_value(shared->val);
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2025-04-12 10:42:13 +02:00
|
|
|
shared->emit_changed();
|
2025-03-21 16:42:23 +02:00
|
|
|
|
|
|
|
queue_accessibility_update();
|
2014-02-09 22:10:30 -03:00
|
|
|
}
|
|
|
|
|
2017-01-04 01:16:14 -03:00
|
|
|
double Range::get_value() const {
|
2014-02-09 22:10:30 -03:00
|
|
|
return shared->val;
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2014-02-09 22:10:30 -03:00
|
|
|
double Range::get_min() const {
|
|
|
|
return shared->min;
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2014-02-09 22:10:30 -03:00
|
|
|
double Range::get_max() const {
|
|
|
|
return shared->max;
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2014-02-09 22:10:30 -03:00
|
|
|
double Range::get_step() const {
|
|
|
|
return shared->step;
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2014-02-09 22:10:30 -03:00
|
|
|
double Range::get_page() const {
|
|
|
|
return shared->page;
|
|
|
|
}
|
|
|
|
|
2017-01-13 14:08:30 -03:00
|
|
|
void Range::set_as_ratio(double p_value) {
|
2016-10-15 18:20:27 +02:00
|
|
|
double v;
|
|
|
|
|
2018-05-15 17:12:35 -03:00
|
|
|
if (shared->exp_ratio && get_min() >= 0) {
|
|
|
|
double exp_min = get_min() == 0 ? 0.0 : Math::log(get_min()) / Math::log((double)2);
|
2017-01-14 14:35:39 -06:00
|
|
|
double exp_max = Math::log(get_max()) / Math::log((double)2);
|
2016-10-15 18:20:27 +02:00
|
|
|
v = Math::pow(2, exp_min + (exp_max - exp_min) * p_value);
|
2014-02-09 22:10:30 -03:00
|
|
|
} else {
|
2016-10-15 18:20:27 +02:00
|
|
|
double percent = (get_max() - get_min()) * p_value;
|
|
|
|
if (get_step() > 0) {
|
2025-03-19 14:18:09 -05:00
|
|
|
double steps = std::round(percent / get_step());
|
2016-10-15 18:20:27 +02:00
|
|
|
v = steps * get_step() + get_min();
|
|
|
|
} else {
|
|
|
|
v = percent + get_min();
|
|
|
|
}
|
2014-02-09 22:10:30 -03:00
|
|
|
}
|
2018-05-16 09:13:41 -03:00
|
|
|
v = CLAMP(v, get_min(), get_max());
|
2017-01-04 01:16:14 -03:00
|
|
|
set_value(v);
|
2014-02-09 22:10:30 -03:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2017-01-13 14:08:30 -03:00
|
|
|
double Range::get_as_ratio() const {
|
2021-01-15 23:13:08 +01:00
|
|
|
if (Math::is_equal_approx(get_max(), get_min())) {
|
|
|
|
// Avoid division by zero.
|
|
|
|
return 1.0;
|
|
|
|
}
|
2019-11-20 16:22:16 +01:00
|
|
|
|
2018-05-15 17:12:35 -03:00
|
|
|
if (shared->exp_ratio && get_min() >= 0) {
|
|
|
|
double exp_min = get_min() == 0 ? 0.0 : Math::log(get_min()) / Math::log((double)2);
|
2017-01-14 14:35:39 -06:00
|
|
|
double exp_max = Math::log(get_max()) / Math::log((double)2);
|
2025-07-15 16:39:24 +02:00
|
|
|
double value = CLAMP(get_value(), shared->min, shared->max);
|
2018-05-16 09:13:41 -03:00
|
|
|
double v = Math::log(value) / Math::log((double)2);
|
2014-02-09 22:10:30 -03:00
|
|
|
|
2019-08-09 01:10:05 -07:00
|
|
|
return CLAMP((v - exp_min) / (exp_max - exp_min), 0, 1);
|
2014-02-09 22:10:30 -03:00
|
|
|
} else {
|
2025-07-15 16:39:24 +02:00
|
|
|
double value = CLAMP(get_value(), shared->min, shared->max);
|
2019-08-09 01:10:05 -07:00
|
|
|
return CLAMP((value - get_min()) / (get_max() - get_min()), 0, 1);
|
2014-02-09 22:10:30 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Range::_share(Node *p_range) {
|
2017-08-24 22:58:51 +02:00
|
|
|
Range *r = Object::cast_to<Range>(p_range);
|
2023-06-06 14:59:54 +02:00
|
|
|
ERR_FAIL_NULL(r);
|
2014-02-09 22:10:30 -03:00
|
|
|
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() {
|
|
|
|
Shared *nshared = memnew(Shared);
|
|
|
|
nshared->min = shared->min;
|
|
|
|
nshared->max = shared->max;
|
|
|
|
nshared->val = shared->val;
|
|
|
|
nshared->step = shared->step;
|
|
|
|
nshared->page = shared->page;
|
2019-11-10 09:49:13 +01:00
|
|
|
nshared->exp_ratio = shared->exp_ratio;
|
2018-05-16 09:13:41 -03:00
|
|
|
nshared->allow_greater = shared->allow_greater;
|
|
|
|
nshared->allow_lesser = shared->allow_lesser;
|
2014-02-09 22:10:30 -03:00
|
|
|
_unref_shared();
|
|
|
|
_ref_shared(nshared);
|
2025-03-21 16:42:23 +02:00
|
|
|
queue_accessibility_update();
|
2014-02-09 22:10:30 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Range::_ref_shared(Shared *p_shared) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (shared && p_shared == shared) {
|
2014-02-09 22:10:30 -03:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-09 22:10:30 -03:00
|
|
|
|
|
|
|
_unref_shared();
|
|
|
|
shared = p_shared;
|
|
|
|
shared->owners.insert(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Range::_unref_shared() {
|
2017-09-28 00:45:05 -07:00
|
|
|
if (shared) {
|
|
|
|
shared->owners.erase(this);
|
2025-03-20 00:07:31 +08:00
|
|
|
if (shared->owners.is_empty()) {
|
2017-09-28 00:45:05 -07:00
|
|
|
memdelete(shared);
|
2020-04-02 01:20:12 +02:00
|
|
|
shared = nullptr;
|
2017-09-28 00:45:05 -07:00
|
|
|
}
|
2014-02-09 22:10:30 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Range::_bind_methods() {
|
2017-02-13 12:47:24 +01: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);
|
2022-10-31 23:12:53 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_value_no_signal", "value"), &Range::set_value_no_signal);
|
2017-02-13 12:47:24 +01:00
|
|
|
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 09:13:41 -03: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 16:44:50 +01:00
|
|
|
|
2017-08-09 13:19:41 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("share", "with"), &Range::_share);
|
2017-02-13 12:47:24 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("unshare"), &Range::unshare);
|
2017-03-05 16:44:50 +01:00
|
|
|
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 15:20:53 -03:00
|
|
|
ADD_SIGNAL(MethodInfo("value_changed", PropertyInfo(Variant::FLOAT, "value")));
|
2014-02-09 22:10:30 -03:00
|
|
|
ADD_SIGNAL(MethodInfo("changed"));
|
2017-03-05 16:44:50 +01:00
|
|
|
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 15:20:53 -03:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "min_value"), "set_min", "get_min");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "max_value"), "set_max", "get_max");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "step"), "set_step", "get_step");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "page"), "set_page", "get_page");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "value"), "set_value", "get_value");
|
2021-06-17 19:10:18 -04:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "ratio", PROPERTY_HINT_RANGE, "0,1,0.01", PROPERTY_USAGE_NONE), "set_as_ratio", "get_as_ratio");
|
2017-02-12 01:11:37 +01: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 14:24:17 +02: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");
|
2021-10-09 17:31:29 +02:00
|
|
|
|
2022-03-26 16:48:43 +01:00
|
|
|
GDVIRTUAL_BIND(_value_changed, "new_value");
|
2022-03-10 08:17:38 +01:00
|
|
|
|
2021-10-09 17:31:29 +02:00
|
|
|
ADD_LINKED_PROPERTY("min_value", "value");
|
|
|
|
ADD_LINKED_PROPERTY("min_value", "max_value");
|
|
|
|
ADD_LINKED_PROPERTY("min_value", "page");
|
|
|
|
ADD_LINKED_PROPERTY("max_value", "value");
|
|
|
|
ADD_LINKED_PROPERTY("max_value", "page");
|
2014-02-09 22:10:30 -03:00
|
|
|
}
|
|
|
|
|
2017-01-13 14:08:30 -03:00
|
|
|
void Range::set_use_rounded_values(bool p_enable) {
|
2015-12-28 00:12:13 +01:00
|
|
|
_rounded_values = p_enable;
|
2014-02-09 22:10:30 -03:00
|
|
|
}
|
|
|
|
|
2017-01-13 14:08:30 -03:00
|
|
|
bool Range::is_using_rounded_values() const {
|
2015-12-28 00:12:13 +01:00
|
|
|
return _rounded_values;
|
2014-02-09 22:10:30 -03:00
|
|
|
}
|
|
|
|
|
2017-01-13 14:08:30 -03:00
|
|
|
void Range::set_exp_ratio(bool p_enable) {
|
2022-03-16 15:50:48 +08:00
|
|
|
if (shared->exp_ratio == p_enable) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-13 14:08:30 -03:00
|
|
|
shared->exp_ratio = p_enable;
|
2018-09-23 20:59:35 +02:00
|
|
|
|
2020-10-29 05:01:28 -05:00
|
|
|
update_configuration_warnings();
|
2014-02-09 22:10:30 -03:00
|
|
|
}
|
|
|
|
|
2017-01-13 14:08:30 -03:00
|
|
|
bool Range::is_ratio_exp() const {
|
|
|
|
return shared->exp_ratio;
|
2014-02-09 22:10:30 -03:00
|
|
|
}
|
|
|
|
|
2018-05-16 09:13:41 -03:00
|
|
|
void Range::set_allow_greater(bool p_allow) {
|
|
|
|
shared->allow_greater = p_allow;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Range::is_greater_allowed() const {
|
|
|
|
return shared->allow_greater;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Range::set_allow_lesser(bool p_allow) {
|
|
|
|
shared->allow_lesser = p_allow;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Range::is_lesser_allowed() const {
|
|
|
|
return shared->allow_lesser;
|
|
|
|
}
|
|
|
|
|
2014-02-09 22:10:30 -03:00
|
|
|
Range::Range() {
|
|
|
|
shared = memnew(Shared);
|
|
|
|
shared->owners.insert(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
Range::~Range() {
|
|
|
|
_unref_shared();
|
|
|
|
}
|