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

This commit is contained in:
Juan Linietsky 2016-07-26 17:24:34 -03:00
parent 8d4d167234
commit 9151eb591d
6 changed files with 27 additions and 22 deletions

View file

@ -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;
{ {

View file

@ -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 Math::step_decimals(double p_step) {
int max=4; static const int maxn=9;
double llimit = Math::pow(0.1,max); static const double sd[maxn]={
double ulimit = 1.0-llimit; 0.9999, // somehow compensate for floating point error
int i=0; 0.09999,
while( max) { 0.009999,
0.0009999,
0.00009999,
0.000009999,
0.0000009999,
0.00000009999,
0.000000009999
};
float d = absf(p_step) - Math::floor(absf(p_step)); double as=absf(p_step);
for(int i=0;i<maxn;i++) {
if (d<llimit || d>ulimit) if (as>=sd[i]) {
break; return i;
p_step*=10.0; }
max--;
i++;
} }
return i; return maxn;
} }
double Math::ease(double p_x, double p_c) { double Math::ease(double p_x, double p_c) {

View file

@ -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();

View file

@ -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);

View file

@ -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!="")

View file

@ -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 = String::num( p_item->cells[i].val, Math::step_decimals( p_item->cells[i].step ) );
String valtext = rtos( p_item->cells[i].val ); //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 ) {