Skip to content

Commit

Permalink
settings_editor: add ability to reorder by dragging if we're displayi…
Browse files Browse the repository at this point in the history
…ng a list
  • Loading branch information
dcnieho committed Sep 28, 2024
1 parent 6a8ce0a commit e88f484
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions src/gazeMapper/GUI/_impl/settings_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,8 @@ def draw_list_set_editor(field_lbl: str, val: _T, f_type: typing.Type):
imgui.set_cursor_screen_pos(pos+(x_padding, 0))
to_remove = None
to_add = None
can_drag = isinstance(val,list)
reordering = False # TODO: this is a workaround for https://github.com/ocornut/imgui/pull/7961#issuecomment-2340980748, remove when no longer needed
for i,v in enumerate(val):
if i>0:
imgui.same_line()
Expand All @@ -536,18 +538,39 @@ def draw_list_set_editor(field_lbl: str, val: _T, f_type: typing.Type):
t_bb = imgui.internal.ImRect(t_pos, (t_pos.x+size.x, t_pos.y+size.y))
imgui.internal.item_size(size, 0)
# if visible
if imgui.internal.item_add(t_bb, imgui.get_id(f'{v_txt}##{field_lbl}')):
iid = imgui.get_id(f'{v_txt}##{field_lbl}')
if imgui.internal.item_add(t_bb, iid):
# enable interaction
if can_drag:
_, hovered, held = imgui.internal.button_behavior(t_bb, iid, False, False, imgui.internal.ButtonFlagsPrivate_.im_gui_button_flags_allow_overlap)
if held and hovered:
clr = imgui.get_color_u32(imgui.Col_.button_active)
elif hovered:
clr = imgui.get_color_u32(imgui.Col_.button_hovered)
else:
clr = imgui.get_color_u32(imgui.Col_.button)
else:
clr = imgui.get_color_u32(imgui.Col_.button)
# draw frame
imgui.internal.render_frame(t_bb.min, t_bb.max, imgui.get_color_u32(imgui.Col_.button), True, imgui.get_style().frame_rounding)
imgui.internal.render_frame(t_bb.min, t_bb.max, clr, True, imgui.get_style().frame_rounding)
# draw text on top
imgui.internal.render_text_clipped((t_bb.min.x+x_padding, t_bb.min.y), (t_bb.max.x-x_padding, t_bb.max.y), v_txt, None, size, imgui.get_style().button_text_align, t_bb)
imgui.set_cursor_screen_pos((t_bb.min.x+2*x_padding+t_size.x,t_pos.y))
if imgui.small_button(f'x##{field_lbl}_{v_txt}'):
if imgui.small_button(f'x##{field_lbl}_{v_txt}_{reordering}'):
to_remove = v

imgui.end_group()
imgui.pop_style_var()

# drag to swap item order
if can_drag and imgui.is_item_active() and not imgui.is_item_hovered():
i_next = i + (-1 if imgui.get_mouse_drag_delta(imgui.MouseButton_.left).y < 0 else 1)
if i_next >= 0 and i_next < len(val):
val[i] = val[i_next]
val[i_next] = v
imgui.reset_mouse_drag_delta()
reordering = True

# draw value adder, if needed
all_values = typing.get_args(typing.get_args(f_type)[0])
if (miss_values := list(set(all_values)-set(val))):
Expand Down

0 comments on commit e88f484

Please sign in to comment.