Browse Source

Changed the way the step decimals are computed to a safer way, fixes many issues.

Juan Linietsky 9 years ago
parent
commit
9151eb591d

+ 1 - 0
bin/tests/test_math.cpp

@@ -113,6 +113,7 @@ uint32_t ihash3( uint32_t a)
 
 
 MainLoop* test() {
 MainLoop* test() {
 
 
+	print_line(itos(Math::step_decimals( 0.0001 )));
 	return NULL;
 	return NULL;
 
 
 	{
 	{

+ 20 - 16
core/math/math_funcs.cpp

@@ -206,25 +206,29 @@ double Math::ceil(double p_x) {
 	return ::ceil(p_x);
 	return ::ceil(p_x);
 }
 }
 
 
-int Math::decimals(double p_step) {
-
-	int max=4;
-	double llimit = Math::pow(0.1,max);
-	double ulimit = 1.0-llimit;
-	int i=0;
-	while( max) {
-
-		float d = absf(p_step) - Math::floor(absf(p_step));
+int Math::step_decimals(double p_step) {
+
+	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
+	};
 
 
-		if (d<llimit || d>ulimit)
-			break;
-		p_step*=10.0;
-		max--;
-		i++;
+	double as=absf(p_step);
+	for(int i=0;i<maxn;i++) {
+		if (as>=sd[i]) {
+			return i;
+		}
 	}
 	}
 
 
-	return i;
-
+	return maxn;
 }
 }
 
 
 double Math::ease(double p_x, double p_c) {
 double Math::ease(double p_x, double p_c) {

+ 1 - 1
core/math/math_funcs.h

@@ -66,7 +66,7 @@ public:
 	static double floor(double p_x);
 	static double floor(double p_x);
 	static double ceil(double p_x);
 	static double ceil(double p_x);
 	static double ease(double p_x, double p_c);
 	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 double stepify(double p_value,double p_step);
 	static void seed(uint32_t x=0);
 	static void seed(uint32_t x=0);
 	static void randomize();
 	static void randomize();

+ 1 - 1
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: {
 		case MATH_DECIMALS: {
 			VALIDATE_ARG_COUNT(1);
 			VALIDATE_ARG_COUNT(1);
 			VALIDATE_ARG_NUM(0);
 			VALIDATE_ARG_NUM(0);
-			r_ret=Math::decimals(*p_args[0]);
+			r_ret=Math::step_decimals(*p_args[0]);
 		} break;
 		} break;
 		case MATH_STEPIFY: {
 		case MATH_STEPIFY: {
 			VALIDATE_ARG_COUNT(2);
 			VALIDATE_ARG_COUNT(2);

+ 1 - 1
scene/gui/spin_box.cpp

@@ -39,7 +39,7 @@ Size2 SpinBox::get_minimum_size() const {
 
 
 void SpinBox::_value_changed(double) {
 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!="")
 	if (prefix!="")
 		value=prefix+" "+value;
 		value=prefix+" "+value;
 	if (suffix!="")
 	if (suffix!="")

+ 4 - 4
scene/gui/tree.cpp

@@ -1179,8 +1179,8 @@ int Tree::draw_item(const Point2i& p_pos,const Point2& p_draw_ofs, const Size2&
 
 
 						Ref<Texture> updown = cache.updown;
 						Ref<Texture> 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());
 						font->draw( ci, text_pos, valtext, col, item_rect.size.x-updown->get_width());
 
 
 						if (!p_item->cells[i].editable)
 						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 {
 					}  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)
 						if (select_mode==SELECT_MULTI && get_tree()->get_last_event_id() == focus_in_id)
 							bring_up_editor=false;
 							bring_up_editor=false;
 
 
@@ -2521,7 +2521,7 @@ bool Tree::edit_selected() {
 		text_editor->set_pos( textedpos );
 		text_editor->set_pos( textedpos );
 		text_editor->set_size( rect.size);
 		text_editor->set_size( rect.size);
 		text_editor->clear();
 		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();
 		text_editor->select_all();
 
 
 		if (c.mode==TreeItem::CELL_MODE_RANGE || c.mode==TreeItem::CELL_MODE_RANGE_EXPRESSION ) {
 		if (c.mode==TreeItem::CELL_MODE_RANGE || c.mode==TreeItem::CELL_MODE_RANGE_EXPRESSION ) {