Gave the image_window the ability to tie the mouse and keyboard events together

such that it is possible for a user to listen for both simultaneously.
This commit is contained in:
Davis King 2013-06-11 22:10:00 -04:00
parent 20c4918a28
commit a2b6ed1600
3 changed files with 129 additions and 23 deletions

View File

@ -6449,7 +6449,8 @@ namespace dlib
window_has_closed(false),
have_last_click(false),
mouse_btn(0),
clicked_signaler(this->wm)
clicked_signaler(this->wm),
tie_input_events(false)
{
gui_img.set_image_clicked_handler(*this, &image_window::on_image_clicked);
@ -6490,7 +6491,8 @@ namespace dlib
)
{
auto_mutex lock(wm);
while (have_last_keypress == false && !window_has_closed)
while (have_last_keypress == false && !window_has_closed &&
(have_last_click == false || !tie_input_events))
{
clicked_signaler.wait();
}
@ -6498,6 +6500,8 @@ namespace dlib
if (window_has_closed)
return false;
if (have_last_keypress)
{
// Mark that we are taking the key click so the next call to get_next_keypress()
// will have to wait for another click.
have_last_keypress = false;
@ -6506,6 +6510,13 @@ namespace dlib
state = next_state;
return true;
}
else
{
key = 0;
is_printable = true;
return false;
}
}
// ----------------------------------------------------------------------------------------
@ -6525,6 +6536,36 @@ namespace dlib
clicked_signaler.signal();
}
// ----------------------------------------------------------------------------------------
void image_window::
tie_events (
)
{
auto_mutex lock(wm);
tie_input_events = true;
}
// ----------------------------------------------------------------------------------------
void image_window::
untie_events (
)
{
auto_mutex lock(wm);
tie_input_events = false;
}
// ----------------------------------------------------------------------------------------
bool image_window::
events_tied (
) const
{
auto_mutex lock(wm);
return tie_input_events;
}
// ----------------------------------------------------------------------------------------
bool image_window::
@ -6533,8 +6574,11 @@ namespace dlib
unsigned long& mouse_button
)
{
p = point(-1,-1);
auto_mutex lock(wm);
while (have_last_click == false && !window_has_closed)
while (have_last_click == false && !window_has_closed &&
(have_last_keypress==false || !tie_input_events))
{
clicked_signaler.wait();
}
@ -6542,6 +6586,8 @@ namespace dlib
if (window_has_closed)
return false;
if (have_last_click)
{
// Mark that we are taking the point click so the next call to
// get_next_double_click() will have to wait for another click.
have_last_click = false;
@ -6549,6 +6595,11 @@ namespace dlib
p = last_clicked_point;
return true;
}
else
{
return false;
}
}
// ----------------------------------------------------------------------------------------

View File

@ -3565,7 +3565,8 @@ namespace dlib
have_last_click(false),
mouse_btn(0),
clicked_signaler(this->wm),
have_last_keypress(false)
have_last_keypress(false),
tie_input_events(false)
{
gui_img.set_image_clicked_handler(*this, &image_window::on_image_clicked);
set_image(img);
@ -3582,7 +3583,8 @@ namespace dlib
have_last_click(false),
mouse_btn(0),
clicked_signaler(this->wm),
have_last_keypress(false)
have_last_keypress(false),
tie_input_events(false)
{
gui_img.set_image_clicked_handler(*this, &image_window::on_image_clicked);
set_image(img);
@ -3752,6 +3754,15 @@ namespace dlib
unsigned long& mouse_button
);
void tie_events (
);
void untie_events (
);
bool events_tied (
) const;
bool get_next_double_click (
point& p
)
@ -3812,6 +3823,7 @@ namespace dlib
unsigned long next_key;
bool next_is_printable;
unsigned long next_state;
bool tie_input_events;
};
// ----------------------------------------------------------------------------------------

View File

@ -2802,6 +2802,7 @@ namespace dlib
/*!
INITIAL VALUE
- initially, this object is visible on the screen
- events_tied() == false
WHAT THIS OBJECT REPRESENTS
This is a simple window that is just a container for an image_display.
@ -3019,15 +3020,50 @@ namespace dlib
- removes all overlays from this object.
!*/
void tie_events (
);
/*!
ensures
- #events_tied() == true
!*/
void untie_events (
);
/*!
ensures
- #events_tied() == false
!*/
bool events_tied (
) const;
/*!
ensures
- returns true if and only if the get_next_double_click() and
get_next_keypress() events are tied together. If they are tied it means
that you can use a loop of the following form to listen for both events
simultaneously:
while (mywindow.get_next_double_click(p) || mywindow.get_next_keypress(key,printable))
{
if (p.x() < 0)
// Do something with the keyboard event
else
// Do something with the mouse event
}
!*/
bool get_next_double_click (
point& p
);
/*!
ensures
- This function blocks until the user double clicks on the image or the
window is closed by the user.
window is closed by the user. It will also unblock for a keyboard key
press if events_tied() == true.
- if (this function returns true) then
- This means the user double clicked the mouse.
- #p == the next image pixel the user clicked.
- else
- #p == point(-1,1)
!*/
bool get_next_double_click (
@ -3037,12 +3073,17 @@ namespace dlib
/*!
ensures
- This function blocks until the user double clicks on the image or the
window is closed by the user.
window is closed by the user. It will also unblock for a keyboard key
press if events_tied() == true.
- if (this function returns true) then
- This means the user double clicked the mouse.
- #p == the next image pixel the user clicked.
- #mouse_button == the mouse button which was used to double click.
This will be either dlib::base_window::LEFT,
dlib::base_window::MIDDLE, or dlib::base_window::RIGHT
- else
- #p == point(-1,1)
(Note that this point is outside any possible image)
!*/
bool get_next_keypress (
@ -3053,15 +3094,16 @@ namespace dlib
/*!
ensures
- This function blocks until the user presses a keyboard key or the
window is closed by the user.
window is closed by the user. It will also unblock for a mouse double
click if events_tied() == true.
- if (this function returns true) then
- This means the user pressed a keyboard key.
- The keyboard button press is recorded into #key, #is_printable, and
#state. In particular, these variables are populated with the three
identically named arguments to the base_window::on_keydown(key,is_printable,state)
event.
!*/
bool get_next_keypress (
unsigned long& key,
bool& is_printable
@ -3069,7 +3111,8 @@ namespace dlib
/*!
ensures
- This function blocks until the user presses a keyboard key or the
window is closed by the user.
window is closed by the user. It will also unblock for a mouse double
click if events_tied() == true.
- This function is the equivalent to calling get_next_keypress(key,is_printable,temp)
and then discarding temp.
!*/