From 9151eb591dcab408d3a7e4d9e3b3874c2e281acf Mon Sep 17 00:00:00 2001 From: Juan Linietsky Date: Tue, 26 Jul 2016 17:24:34 -0300 Subject: [PATCH] Changed the way the step decimals are computed to a safer way, fixes many issues. --- bin/tests/test_math.cpp | 1 + core/math/math_funcs.cpp | 34 +++++++++++++++++-------------- core/math/math_funcs.h | 2 +- modules/gdscript/gd_functions.cpp | 2 +- scene/gui/spin_box.cpp | 2 +- scene/gui/tree.cpp | 8 ++++---- 6 files changed, 27 insertions(+), 22 deletions(-) diff --git a/bin/tests/test_math.cpp b/bin/tests/test_math.cpp index 8e08969fa4c..4b686e8af88 100644 --- a/bin/tests/test_math.cpp +++ b/bin/tests/test_math.cpp @@ -113,6 +113,7 @@ uint32_t ihash3( uint32_t a) MainLoop* test() { + print_line(itos(Math::step_decimals( 0.0001 ))); return NULL; { diff --git a/core/math/math_funcs.cpp b/core/math/math_funcs.cpp index 0fbd0312142..64615fe6b4d 100644 --- a/core/math/math_funcs.cpp +++ b/core/math/math_funcs.cpp @@ -206,25 +206,29 @@ double Math::ceil(double p_x) { return ::ceil(p_x); } -int Math::decimals(double p_step) { +int Math::step_decimals(double p_step) { - int max=4; - double llimit = Math::pow(0.1,max); - double ulimit = 1.0-llimit; - int i=0; - while( max) { + static const int maxn=9; + static const double sd[maxn]={ + 0.9999, // somehow compensate for floating point error + 0.09999, + 0.009999, + 0.0009999, + 0.00009999, + 0.000009999, + 0.0000009999, + 0.00000009999, + 0.000000009999 + }; - float d = absf(p_step) - Math::floor(absf(p_step)); - - if (dulimit) - break; - p_step*=10.0; - max--; - i++; + double as=absf(p_step); + for(int i=0;i=sd[i]) { + return i; + } } - return i; - + return maxn; } double Math::ease(double p_x, double p_c) { diff --git a/core/math/math_funcs.h b/core/math/math_funcs.h index 2e1b9c989e0..fc76d96b2ee 100644 --- a/core/math/math_funcs.h +++ b/core/math/math_funcs.h @@ -66,7 +66,7 @@ public: static double floor(double p_x); static double ceil(double p_x); static double ease(double p_x, double p_c); - static int decimals(double p_step); + static int step_decimals(double p_step); static double stepify(double p_value,double p_step); static void seed(uint32_t x=0); static void randomize(); diff --git a/modules/gdscript/gd_functions.cpp b/modules/gdscript/gd_functions.cpp index b9815a5efd8..a565e866d03 100644 --- a/modules/gdscript/gd_functions.cpp +++ b/modules/gdscript/gd_functions.cpp @@ -304,7 +304,7 @@ void GDFunctions::call(Function p_func,const Variant **p_args,int p_arg_count,Va case MATH_DECIMALS: { VALIDATE_ARG_COUNT(1); VALIDATE_ARG_NUM(0); - r_ret=Math::decimals(*p_args[0]); + r_ret=Math::step_decimals(*p_args[0]); } break; case MATH_STEPIFY: { VALIDATE_ARG_COUNT(2); diff --git a/scene/gui/spin_box.cpp b/scene/gui/spin_box.cpp index 2b64d36a819..98e1a32aef0 100644 --- a/scene/gui/spin_box.cpp +++ b/scene/gui/spin_box.cpp @@ -39,7 +39,7 @@ Size2 SpinBox::get_minimum_size() const { void SpinBox::_value_changed(double) { - String value = String::num(get_val(),Math::decimals(get_step())); + String value = String::num(get_val(),Math::step_decimals(get_step())); if (prefix!="") value=prefix+" "+value; if (suffix!="") diff --git a/scene/gui/tree.cpp b/scene/gui/tree.cpp index 82459ba0ab1..487f62ed441 100644 --- a/scene/gui/tree.cpp +++ b/scene/gui/tree.cpp @@ -1179,8 +1179,8 @@ int Tree::draw_item(const Point2i& p_pos,const Point2& p_draw_ofs, const Size2& Ref updown = cache.updown; - //String valtext = String::num( p_item->cells[i].val, Math::decimals( p_item->cells[i].step ) ); - String valtext = rtos( p_item->cells[i].val ); + String valtext = String::num( p_item->cells[i].val, Math::step_decimals( p_item->cells[i].step ) ); + //String valtext = rtos( p_item->cells[i].val ); font->draw( ci, text_pos, valtext, col, item_rect.size.x-updown->get_width()); if (!p_item->cells[i].editable) @@ -1746,7 +1746,7 @@ int Tree::propagate_mouse_event(const Point2i &p_pos,int x_ofs,int y_ofs,bool p_ } else { - editor_text=String::num( p_item->cells[col].val, Math::decimals( p_item->cells[col].step ) ); + editor_text=String::num( p_item->cells[col].val, Math::step_decimals( p_item->cells[col].step ) ); if (select_mode==SELECT_MULTI && get_tree()->get_last_event_id() == focus_in_id) bring_up_editor=false; @@ -2521,7 +2521,7 @@ bool Tree::edit_selected() { text_editor->set_pos( textedpos ); text_editor->set_size( rect.size); text_editor->clear(); - text_editor->set_text( c.mode==TreeItem::CELL_MODE_STRING?c.text:rtos(c.val) ); + text_editor->set_text( c.mode==TreeItem::CELL_MODE_STRING?c.text:String::num( c.val, Math::step_decimals( c.step ) ) ); text_editor->select_all(); if (c.mode==TreeItem::CELL_MODE_RANGE || c.mode==TreeItem::CELL_MODE_RANGE_EXPRESSION ) {