2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* range.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 21:01:57 +00:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#include "range.h"
|
|
|
|
|
|
|
|
void Range::_value_changed_notify() {
|
|
|
|
|
|
|
|
_value_changed(shared->val);
|
2017-03-18 23:36:26 +00:00
|
|
|
emit_signal("value_changed", shared->val);
|
2014-02-10 01:10:30 +00:00
|
|
|
update();
|
|
|
|
_change_notify("range/value");
|
|
|
|
}
|
|
|
|
|
|
|
|
void Range::Shared::emit_value_changed() {
|
|
|
|
|
2017-03-18 23:36:26 +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
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
emit_signal("changed", shared->val);
|
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-18 23:36:26 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Range::set_val(double p_val) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +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
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (p_val > shared->max - shared->page)
|
|
|
|
p_val = shared->max - shared->page;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (p_val < shared->min)
|
|
|
|
p_val = shared->min;
|
2016-01-08 12:07:15 +00:00
|
|
|
|
2017-03-18 23:36:26 +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-18 23:36:26 +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-18 23:36:26 +00:00
|
|
|
shared->min = p_min;
|
2014-02-10 01:10:30 +00:00
|
|
|
set_val(shared->val);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2016-01-06 12:54:13 +00:00
|
|
|
shared->emit_changed("range/min");
|
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-18 23:36:26 +00:00
|
|
|
shared->max = p_max;
|
2014-02-10 01:10:30 +00:00
|
|
|
set_val(shared->val);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2016-01-06 12:54:13 +00:00
|
|
|
shared->emit_changed("range/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-18 23:36:26 +00:00
|
|
|
shared->step = p_step;
|
2016-01-06 12:54:13 +00:00
|
|
|
shared->emit_changed("range/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-18 23:36:26 +00:00
|
|
|
shared->page = p_page;
|
2014-02-10 01:10:30 +00:00
|
|
|
set_val(shared->val);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2016-01-06 12:54:13 +00:00
|
|
|
shared->emit_changed("range/page");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double Range::get_val() 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Range::set_unit_value(double p_value) {
|
2016-10-15 16:20:27 +00:00
|
|
|
|
|
|
|
double v;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (shared->exp_unit_value && get_min() > 0) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
double exp_min = Math::log(get_min()) / Math::log(2);
|
|
|
|
double exp_max = Math::log(get_max()) / Math::log(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
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
set_val(v);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
double Range::get_unit_value() const {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (shared->exp_unit_value && get_min() > 0) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
double exp_min = Math::log(get_min()) / Math::log(2);
|
|
|
|
double exp_max = Math::log(get_max()) / Math::log(2);
|
|
|
|
double v = Math::log(get_val()) / Math::log(2);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return (v - exp_min) / (exp_max - exp_min);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
return (get_val() - get_min()) / (get_max() - get_min());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Range::_share(Node *p_range) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Range *r = p_range->cast_to<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-18 23:36:26 +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;
|
2014-02-10 01:10:30 +00:00
|
|
|
_unref_shared();
|
|
|
|
_ref_shared(nshared);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Range::_ref_shared(Shared *p_shared) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (shared && p_shared == shared)
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
_unref_shared();
|
2017-03-18 23:36:26 +00:00
|
|
|
shared = p_shared;
|
2014-02-10 01:10:30 +00:00
|
|
|
shared->owners.insert(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Range::_unref_shared() {
|
|
|
|
|
|
|
|
shared->owners.erase(this);
|
2017-03-18 23:36:26 +00:00
|
|
|
if (shared->owners.size() == 0) {
|
2014-02-10 01:10:30 +00:00
|
|
|
memdelete(shared);
|
2017-03-18 23:36:26 +00:00
|
|
|
shared = NULL;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Range::_bind_methods() {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("get_val"), &Range::get_val);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_value"), &Range::get_val);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_min"), &Range::get_min);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_max"), &Range::get_max);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_step"), &Range::get_step);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_page"), &Range::get_page);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_unit_value"), &Range::get_unit_value);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_val", "value"), &Range::set_val);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_value", "value"), &Range::set_val);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_min", "minimum"), &Range::set_min);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_max", "maximum"), &Range::set_max);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_step", "step"), &Range::set_step);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_page", "pagesize"), &Range::set_page);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_unit_value", "value"), &Range::set_unit_value);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_rounded_values", "enabled"), &Range::set_rounded_values);
|
|
|
|
ObjectTypeDB::bind_method(_MD("is_rounded_values"), &Range::is_rounded_values);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_exp_unit_value", "enabled"), &Range::set_exp_unit_value);
|
|
|
|
ObjectTypeDB::bind_method(_MD("is_unit_value_exp"), &Range::is_unit_value_exp);
|
|
|
|
|
|
|
|
ObjectTypeDB::bind_method(_MD("share", "with"), &Range::_share);
|
|
|
|
ObjectTypeDB::bind_method(_MD("unshare"), &Range::unshare);
|
|
|
|
|
|
|
|
ADD_SIGNAL(MethodInfo("value_changed", PropertyInfo(Variant::REAL, "value")));
|
|
|
|
ADD_SIGNAL(MethodInfo("changed"));
|
|
|
|
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::REAL, "range/min"), _SCS("set_min"), _SCS("get_min"));
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::REAL, "range/max"), _SCS("set_max"), _SCS("get_max"));
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::REAL, "range/step"), _SCS("set_step"), _SCS("get_step"));
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::REAL, "range/page"), _SCS("set_page"), _SCS("get_page"));
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::REAL, "range/value"), _SCS("set_val"), _SCS("get_val"));
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "range/exp_edit"), _SCS("set_exp_unit_value"), _SCS("is_unit_value_exp"));
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "range/rounded"), _SCS("set_rounded_values"), _SCS("is_rounded_values"));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2015-12-27 23:12:13 +00:00
|
|
|
void Range::set_rounded_values(bool p_enable) {
|
|
|
|
|
|
|
|
_rounded_values = p_enable;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2015-12-27 23:12:13 +00:00
|
|
|
bool Range::is_rounded_values() const {
|
|
|
|
|
|
|
|
return _rounded_values;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Range::set_exp_unit_value(bool p_enable) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
shared->exp_unit_value = p_enable;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Range::is_unit_value_exp() const {
|
|
|
|
|
|
|
|
return shared->exp_unit_value;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Range::Range() {
|
2014-02-10 01:10:30 +00:00
|
|
|
shared = memnew(Shared);
|
2017-03-18 23:36:26 +00:00
|
|
|
shared->min = 0;
|
|
|
|
shared->max = 100;
|
|
|
|
shared->val =
|
|
|
|
shared->step = 1;
|
|
|
|
shared->page = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
shared->owners.insert(this);
|
2017-03-18 23:36:26 +00:00
|
|
|
shared->exp_unit_value = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
_rounded_values = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Range::~Range() {
|
|
|
|
|
|
|
|
_unref_shared();
|
|
|
|
}
|