From d627aea9ec045a940fbc3c996898a22b48cc6261 Mon Sep 17 00:00:00 2001 From: Davis King Date: Sat, 4 Mar 2017 07:58:40 -0500 Subject: [PATCH] Made the image_display widget draw mouse cross-hairs when the user holds shift. --- dlib/gui_widgets/widgets.cpp | 62 ++++++++++++++++++++++++++++++++++-- dlib/gui_widgets/widgets.h | 1 + 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/dlib/gui_widgets/widgets.cpp b/dlib/gui_widgets/widgets.cpp index 611703064..bdb4c13a4 100644 --- a/dlib/gui_widgets/widgets.cpp +++ b/dlib/gui_widgets/widgets.cpp @@ -6012,7 +6012,8 @@ namespace dlib part_width(15), // width part circles are drawn on the screen overlay_editing_enabled(true), highlight_timer(*this, &image_display::timer_event_unhighlight_rect), - highlighted_rect(std::numeric_limits::max()) + highlighted_rect(std::numeric_limits::max()), + holding_shift_key(false) { enable_mouse_drag(); @@ -6250,16 +6251,26 @@ namespace dlib const point origin(total_rect().tl_corner()); // draw the image on the screen + const double scale = zoom_out_scale/(double)zoom_in_scale; const rectangle img_area = total_rect().intersect(area); for (long row = img_area.top(); row <= img_area.bottom(); ++row) { + const long rc = row-c.top(); + const long rimg = (row-origin.y())*scale; for (long col = img_area.left(); col <= img_area.right(); ++col) { - assign_pixel(c[row-c.top()][col-c.left()], - img[(row-origin.y())*zoom_out_scale/zoom_in_scale][(col-origin.x())*zoom_out_scale/zoom_in_scale]); + assign_pixel(c[rc][col-c.left()], + img[rimg][(col-origin.x())*scale]); } } + // draw the mouse cross-hairs + if (holding_shift_key && total_rect().contains(lastx,lasty) ) + { + draw_line(c, point(lastx,-10000), point(lastx,100000),rgb_pixel(255,255,0), area); + draw_line(c, point(-10000,lasty), point(100000,lasty),rgb_pixel(255,255,0), area); + } + // now draw all the overlay rectangles for (unsigned long i = 0; i < overlay_rects.size(); ++i) { @@ -6393,6 +6404,20 @@ namespace dlib { scrollable_region::on_keydown(key,is_printable, state); + if (!is_printable && key==base_window::KEY_SHIFT) + { + if (!holding_shift_key) + { + holding_shift_key = true; + parent.invalidate_rectangle(rect); + } + } + else if (holding_shift_key) + { + holding_shift_key = false; + parent.invalidate_rectangle(rect); + } + if (!is_printable && !hidden && enabled && rect_is_selected && (key == base_window::KEY_BACKSPACE || key == base_window::KEY_DELETE)) { @@ -6459,6 +6484,16 @@ namespace dlib { scrollable_region::on_mouse_down(btn, state, x, y, is_double_click); + if (state&base_window::SHIFT) + { + holding_shift_key = true; + } + else if (holding_shift_key) + { + holding_shift_key = false; + parent.invalidate_rectangle(rect); + } + if (rect.contains(x,y) == false || hidden || !enabled) return; @@ -6757,6 +6792,16 @@ namespace dlib { scrollable_region::on_mouse_up(btn,state,x,y); + if (state&base_window::SHIFT) + { + holding_shift_key = true; + } + else if (holding_shift_key) + { + holding_shift_key = false; + parent.invalidate_rectangle(rect); + } + if (drawing_rect && btn == base_window::LEFT && (state&base_window::SHIFT) && !hidden && enabled) { @@ -6817,6 +6862,17 @@ namespace dlib { scrollable_region::on_mouse_move(state,x,y); + if (enabled && !hidden) + { + if (holding_shift_key) + parent.invalidate_rectangle(rect); + + if (state&base_window::SHIFT) + holding_shift_key = true; + else if (holding_shift_key) + holding_shift_key = false; + } + if (drawing_rect) { if ((state&base_window::LEFT) && (state&base_window::SHIFT) && !hidden && enabled) diff --git a/dlib/gui_widgets/widgets.h b/dlib/gui_widgets/widgets.h index 5bf442032..2246bfecc 100644 --- a/dlib/gui_widgets/widgets.h +++ b/dlib/gui_widgets/widgets.h @@ -3593,6 +3593,7 @@ namespace dlib bool overlay_editing_enabled; timer highlight_timer; unsigned long highlighted_rect; + bool holding_shift_key; bool moving_overlay; unsigned long moving_rect;