Skip to content

Commit

Permalink
Even better image zooming
Browse files Browse the repository at this point in the history
  • Loading branch information
Willy-JL committed Nov 24, 2022
1 parent 96f6c53 commit 25cbd5b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
2 changes: 1 addition & 1 deletion modules/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ async def connect():
"update_keep_image": f'INTEGER DEFAULT {int(False)}',
"use_parser_processes": f'INTEGER DEFAULT {int(True)}',
"vsync_ratio": f'INTEGER DEFAULT 1',
"zoom_amount": f'INTEGER DEFAULT 4',
"zoom_area": f'INTEGER DEFAULT 50',
"zoom_times": f'REAL DEFAULT 4.0',
"zoom_enabled": f'INTEGER DEFAULT {int(True)}'
}
)
Expand Down
44 changes: 33 additions & 11 deletions modules/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -1219,8 +1219,12 @@ def popup_content():
if width < avail.x:
imgui.set_cursor_pos_x((avail.x - width + imgui.style.scrollbar_size) / 2)
image_pos = imgui.get_cursor_screen_pos()
image.render(width, height, rounding=globals.settings.style_corner_radius)

imgui.begin_child("###image_zoomer", width=width, height=height + 1.0, flags=imgui.WINDOW_NO_SCROLLBAR)
imgui.dummy(width + 2.0, height)
imgui.set_scroll_x(1.0)
imgui.set_cursor_screen_pos(image_pos)
image.render(width, height, rounding=globals.settings.style_corner_radius)
if imgui.is_item_hovered():
# Image popup
if imgui.is_mouse_down():
Expand All @@ -1240,18 +1244,24 @@ def popup_content():
fg_draw_list.add_image_rounded(image.texture_id, (x, y), pos2, rounding=rounding, flags=flags)
# Zoom
elif globals.settings.zoom_enabled:
if diff := int(imgui.get_scroll_x() - 1.0):
if imgui.is_key_down(glfw.KEY_LEFT_ALT):
globals.settings.zoom_area = min(max(globals.settings.zoom_area + diff, 1), 200)
else:
globals.settings.zoom_times = min(max(globals.settings.zoom_times * (-diff / 50.0 + 1.0), 1), 20)
zoom_popup[0] = True
out_size = min(*imgui.io.display_size) * globals.settings.zoom_area / 100
in_size = out_size / globals.settings.zoom_amount
in_size = out_size / globals.settings.zoom_times
mouse_pos = imgui.io.mouse_pos
imgui.set_next_window_position(*mouse_pos, pivot_x=0.5, pivot_y=0.5)
off_x = utils.map_range(in_size, 0.0, width, 0.0, 1.0) / 2.0
off_y = utils.map_range(in_size, 0.0, height, 0.0, 1.0) / 2.0
x = utils.map_range(mouse_pos.x, image_pos.x, image_pos.x + width, 0.0, 1.0)
y = utils.map_range(mouse_pos.y, image_pos.y, image_pos.y + height, 0.0, 1.0)
imgui.set_next_window_position(*mouse_pos, pivot_x=0.5, pivot_y=0.5)
imgui.begin_tooltip()
image.render(out_size, out_size, (x - off_x, y - off_y), (x + off_x, y + off_y), rounding=globals.settings.style_corner_radius)
imgui.end_tooltip()
imgui.end_child()
imgui.push_text_wrap_pos()

imgui.push_font(self.big_font)
Expand Down Expand Up @@ -2435,24 +2445,36 @@ def callback(selected: str):
)
draw_settings_checkbox("update_keep_image")

draw_settings_label("Zoom on hover:")
draw_settings_label(
"Zoom on hover:",
"Allow zooming header images inside info popups.\n"
"Tip: hold shift and scroll while hovering the image to change the zoom amount, or hold shift and alt while "
"scrolling to change the zoom area."
)
draw_settings_checkbox("zoom_enabled")

if not set.zoom_enabled:
utils.push_disabled()

draw_settings_label("Zoom amount:")
changed, value = imgui.drag_int("###zoom_amount", set.zoom_amount, change_speed=0.1, min_value=1, max_value=20, format="%dx")
set.zoom_amount = min(max(value, 1), 20)
if changed:
async_thread.run(db.update_settings("zoom_amount"))

draw_settings_label("Zoom area:")
draw_settings_label(
"Zoom area:",
"The size of the zoom popup compared to the main window size (uses the shorter of the two window dimensions). "
"Default 50%."
)
changed, value = imgui.drag_int("###zoom_area", set.zoom_area, change_speed=0.1, min_value=1, max_value=200, format="%d%%")
set.zoom_area = min(max(value, 1), 200)
if changed:
async_thread.run(db.update_settings("zoom_area"))

draw_settings_label(
"Zoom times:",
"How many times to magnify the zoomed area of the image. Default 4x."
)
changed, value = imgui.drag_float("###zoom_times", set.zoom_times, change_speed=0.02, min_value=1, max_value=20, format="%.1fx")
set.zoom_times = min(max(value, 1), 20)
if changed:
async_thread.run(db.update_settings("zoom_times"))

if not set.zoom_enabled:
utils.pop_disabled()

Expand Down
2 changes: 1 addition & 1 deletion modules/structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,8 @@ class Settings:
update_keep_image : bool
use_parser_processes : bool
vsync_ratio : int
zoom_amount : int
zoom_area : int
zoom_times : float
zoom_enabled : bool


Expand Down

0 comments on commit 25cbd5b

Please sign in to comment.