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() {
print_line(itos(Math::step_decimals( 0.0001 )));
return NULL;
{

View File

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

View File

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

View File

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

View File

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

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;
//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 ) {