Skip to content

Commit

Permalink
Cogwheel model for spinner / Speed limit on idle.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mick Pearson committed May 21, 2019
1 parent 797c831 commit 0a744af
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 35 deletions.
Binary file modified bin/Win32/example.exe
Binary file not shown.
Binary file modified bin/Win32/glui32-sd.lib
Binary file not shown.
2 changes: 1 addition & 1 deletion include/GL/glui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4672,7 +4672,7 @@ class GLUI : public Event
inline static float get_version(){ return _this()._glui_version; }

//UNDOCUMENTED
LINKAGE static void glui_setIdleFuncIfNecessary();
LINKAGE static void glui_setIdleFuncIfNecessary(bool necessary=false);

#ifdef GLUI_GLUI_H___NODEPRECATED
inline UI *create_glui(C_String name, int flags=0, int x=-1, int y=-1);
Expand Down
2 changes: 1 addition & 1 deletion src/example/glui_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ namespace glui_example_1 //EXAMPLE #1
//2019: Hold freeglut to reasonable frame rate so not to degrade demo.
static int et = 0;
int now = glutGet(GLUT_ELAPSED_TIME);
if(now-et>15){ et = now; glutPostRedisplay(); }
if(now-et>10){ et = now; glutPostRedisplay(); }
}

/**************************************** myGlutReshape() *************/
Expand Down
49 changes: 34 additions & 15 deletions src/glui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,24 @@ struct UI::_glut_friends
/* Send idle event to each UI, then to the main window */
static void idle()
{
//Spinner needs a speed limit... besides GLUT will
//call this idle function at infinite rate. That's
//unacceptable.
static int was = 0;
int now = glutGet(GLUT_ELAPSED_TIME);
{
if(now-was<2)
{
#ifdef _WIN32
Sleep(1);
#else
usleep(1000);
#endif
now = glutGet(GLUT_ELAPSED_TIME);
}
}
was = now;

for(UI*ui=GLUI::first_ui();ui;/*ui=ui->next*/)
{
UI *swap = ui->next(); //_pending_idleclose
Expand Down Expand Up @@ -467,7 +485,7 @@ struct UI::_glut_friends
static void idle2(UI::Void_CB f)
{
if(GLUT._add_cb_to_glut_window(glutGetWindow(),GLUT::IDLE,f))
GLUI::glui_setIdleFuncIfNecessary();
GLUI::glui_setIdleFuncIfNecessary(f?true:false);
}
};

Expand Down Expand Up @@ -617,7 +635,7 @@ void UI::_init(const char *text, int subpos, int x, int y, int parent_window)
glutPassiveMotionFunc(_glut_friends::passive_motion);
glutEntryFunc(_glut_friends::entry);
glutVisibilityFunc(_glut_friends::visibility);
glutIdleFunc(_glut_friends::idle);
/* glutIdleFunc(_glut_friends::idle); // FIXME! 100% CPU usage! */
}
if(old_glut_window>0) glutSetWindow(old_glut_window);

Expand Down Expand Up @@ -1686,7 +1704,7 @@ void UI::close(bool now, bool detach)

if(detach) _glut_parent_id = -1;
_pending_idleclose = _pending_redisplay = true;
if(!now) GLUI::glui_setIdleFuncIfNecessary();
if(!now) GLUI::glui_setIdleFuncIfNecessary(true);
else _close_internal();
}
void UI::_close_internal()
Expand Down Expand Up @@ -2013,21 +2031,22 @@ void GLUI::_set_edited(UI::Control *c)

/************* GLUI::glui_setIdleFuncIfNecessary() ***********************/

void GLUI::glui_setIdleFuncIfNecessary()
void GLUI::glui_setIdleFuncIfNecessary(bool necessary)
{
bool necessary = false;
//NEW: Tying idle routines to life of windows/facilitating
//multiple idle routines.
//if(!GLUT._glut_idle_CB)
for(GLUT::Window*gw=GLUT::first_glut_window();gw;gw=gw->next())
{
if(gw->_glut_idle_CB) necessary = true;
}
if(!necessary) for(UI*ui=GLUI::first_ui();ui;ui=ui->next())
if(!necessary)
{
if(ui->_needs_idle()) necessary = true;
//NEW: Tying idle routines to life of windows/facilitating
//multiple idle routines.
//if(!GLUT._glut_idle_CB)
for(GLUT::Window*gw=GLUT::first_glut_window();gw;gw=gw->next())
{
if(gw->_glut_idle_CB) necessary = true;
}
if(!necessary) for(UI*ui=GLUI::first_ui();ui;ui=ui->next())
{
if(ui->_needs_idle()) necessary = true;
}
}

glutIdleFunc(necessary?UI::_glut_friends::idle:NULL);
}

Expand Down
17 changes: 15 additions & 2 deletions src/glui_edittext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -726,8 +726,21 @@ bool UI::EditText::_special_handler(int key, int modifiers)
{
case GLUT_KEY_PAGE_UP:
case GLUT_KEY_PAGE_DOWN: return true;
case GLUT_KEY_UP: key = GLUT_KEY_LEFT; break;
case GLUT_KEY_DOWN: key = GLUT_KEY_RIGHT; break;
case GLUT_KEY_UP: case GLUT_KEY_DOWN:

key = key==GLUT_KEY_UP?GLUT_KEY_LEFT:GLUT_KEY_RIGHT;

//Normally I'd choose GLUT_ACTIVE_CTRL for this, but
//GLUI is using that for reducing the increment.
if(modifiers&~GLUT_ACTIVE_SHIFT) if(Spinner*s=spinner)
{
if(data_type==EDIT_INT)
{
modifiers&=~GLUT_ACTIVE_CTRL;
}
return s->_special_handler(key,modifiers);
}
break;
}
return Text_Interface::_special_handler(key,modifiers);
}
Expand Down
2 changes: 1 addition & 1 deletion src/glui_scrollbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ bool UI::ScrollBar::_mouse_down_handler(int local_x, int local_y)
//last_update_time = GLUI_Time()-1;
state = find_arrow(local_x,local_y);
if(needs_idle())
GLUI::glui_setIdleFuncIfNecessary();
GLUI::glui_setIdleFuncIfNecessary(true);

/* printf("spinner: mouse down : %d/%d arrow:%d\n",local_x,local_y,
find_arrow(local_x,local_y)); */
Expand Down
68 changes: 53 additions & 15 deletions src/glui_spinner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ namespace GLUI_Library
{//-.
//<-'

static int glui_spinner_cogwheel = 0; //2019

/*********************************** GLUI_Spinner:_reset_growth() *************/

double &Spin_Interface::_growth(int inc)
Expand Down Expand Up @@ -136,30 +138,44 @@ g: if(g<0.001) g = 0.001;
bool UI::Spinner::_mouse_down_handler(int local_x, int local_y)
{
state = find_arrow(local_x,local_y);
if(!state) return true; //2019

if(needs_idle())
GLUI::glui_setIdleFuncIfNecessary();
GLUI::glui_setIdleFuncIfNecessary(true);

/* printf( "spinner: mouse down : %d/%d arrow:%d\n",local_x,local_y,find_arrow(local_x,local_y)); */

if(state!=STATE_UP&&state!=STATE_DOWN)
{
assert(0); //SCROLL?

return false; //return true; //???
}

_reset_growth();

//DUBIOUS
//NOTE: I'm going to try to add this logic to do_click.
//2019: Either do_click should set the value or it shouldn't be also
//taken. The demos have no problem incrementing... in fact they have
//the opposite problem.
/*** ints and floats behave a bit differently. When you click on
an int spinner, you expect the value to immediately go up by 1, whereas
for a float it'll go up only by a fractional amount. Therefore, we
go ahead and increment by one for int spinners ***/
if(data_type==SPIN_INT)
if(double step=state==STATE_UP?1:STATE_DOWN?-0.9:0)
{
set_float_val(float_val+step);
}
//if(data_type==SPIN_INT)
//if(double step=state==STATE_UP?1:STATE_DOWN?-0.9:0) //0.9???
//{
// set_float_val(float_val+step);
//}

glui_spinner_cogwheel = 0;

do_click();

do_click(); return false;
//2019: This is new... it should probably be GLUT_ELAPSED_TIME based.
glui_spinner_cogwheel = 45;

return false;
}

/******************************** GLUI_Spinner::mouse_up_handler() **********/
Expand Down Expand Up @@ -193,7 +209,7 @@ static int glui_spinner_last_y = 0;
bool UI::Spinner::_mouse_held_down_handler(int local_x, int local_y, bool inside)
{
if(state==STATE_NONE) return false;

/* printf("spinner: mouse held: %d/%d inside: %d\n",local_x,local_y,inside); */

if(state==STATE_BOTH) /* dragging? */
Expand Down Expand Up @@ -300,6 +316,9 @@ void UI::Spinner::_draw()

bool Spin_Interface::_special_handler(int key, int modifiers)
{
//NOTE: It's currently not really possible to do key input
//into a Spinner becaue the focus goes to the text control.

if(key>=GLUT_KEY_LEFT&&key<=GLUT_KEY_DOWN)
{
int x,y;
Expand All @@ -313,6 +332,9 @@ bool Spin_Interface::_special_handler(int key, int modifiers)
y = !horizontal?h-3:3;
xy:
x+=x_abs; y+=y_abs;

glui_spinner_cogwheel = 0; //NEW

if(GLUI::Click(this,x,y))
return true;
}
Expand Down Expand Up @@ -387,22 +409,38 @@ void UI::Spinner::do_click()
redraw(); return;
}

if(glui_spinner_cogwheel--) return;

glui_spinner_cogwheel = 10;

double cm_factor = 1;
switch(GLUI.curr_modifiers)
{
case GLUT_ACTIVE_SHIFT: cm_factor = 100; break;
case GLUT_ACTIVE_CTRL: cm_factor = 0.01; break;
case GLUT_ACTIVE_SHIFT: cm_factor = 10; break; //100 //TOO MUCH
case GLUT_ACTIVE_CTRL: cm_factor = 0.1; break; //0.01 //TOO LITTLE
}

double cmp = float_val; //limits

//if(data_type==SPIN_FLOAT||1) //???
if(int dir=state==STATE_UP?1:state==STATE_DOWN?-1:0)
{
double inc = _increase_growth();
double dir = state==STATE_UP?1:state==STATE_DOWN?-1:0;
set_float_val(float_val+inc*dir*cm_factor*speed);
double inc = _increase_growth()*dir*cm_factor*speed;

double new_val = float_val+inc;

if(data_type==SPIN_INT)
{
while(int_val==(int)new_val) //NEW
{
new_val+=inc;
}
new_val = (int)new_val; //NEW
}

/** Remember, edittext mirrors the float and int values ***/

set_float_val(new_val);
}

if(cmp!=float_val) do_callbacks();
Expand Down

0 comments on commit 0a744af

Please sign in to comment.