yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
resize_handles.h
Go to the documentation of this file.
1#ifndef YAZE_APP_GUI_WIDGETS_RESIZE_HANDLES_H_
2#define YAZE_APP_GUI_WIDGETS_RESIZE_HANDLES_H_
3
4#include <cstdint>
5
6#include "imgui/imgui.h"
7#include "imgui/imgui_internal.h"
8
9namespace yaze {
10namespace gui {
11
12// Which handle zones to draw and hit-test. Corners take priority over edges
13// when the mouse overlaps both.
14enum class HandleMask : std::uint8_t {
15 kNone = 0,
16 kNW = 1 << 0,
17 kNE = 1 << 1,
18 kSW = 1 << 2,
19 kSE = 1 << 3,
20 kN = 1 << 4,
21 kE = 1 << 5,
22 kS = 1 << 6,
23 kW = 1 << 7,
24 kCorners = kNW | kNE | kSW | kSE,
25 kEdges = kN | kE | kS | kW,
27};
28
30 return static_cast<HandleMask>(static_cast<std::uint8_t>(a) |
31 static_cast<std::uint8_t>(b));
32}
34 return static_cast<HandleMask>(static_cast<std::uint8_t>(a) &
35 static_cast<std::uint8_t>(b));
36}
37constexpr bool Has(HandleMask all, HandleMask flag) {
38 return (static_cast<std::uint8_t>(all) & static_cast<std::uint8_t>(flag)) !=
39 0;
40}
41
42// Which zone the user is hitting. At most one value is returned per hit test.
43enum class HandleZone : std::uint8_t {
44 kNone = 0,
45 kNW,
46 kN,
47 kNE,
48 kW,
49 kE,
50 kSW,
51 kS,
52 kSE,
53};
54
56 float handle_size = 8.0f;
57 float min_width = 4.0f;
58 float min_height = 4.0f;
59 // If alpha == 0, the theme's selection_handle color is used at draw time.
60 ImVec4 color = ImVec4(0.0f, 0.0f, 0.0f, 0.0f);
61};
62
63// Draws eight resize handles around `*rect` and services mouse drag.
64//
65// Returns true on the frame the user commits a drag (mouse release inside an
66// active handle). Mid-drag frames mutate `*rect` but return false. Frames
67// with no active drag leave `*rect` untouched.
68//
69// `mask` — which zones are active (corners only, edges only, or all).
70// `snap` — when > 0, each moving coordinate is rounded to a multiple of this.
71// `id` — scope for ImGui hit-test state. Pass 0 to auto-derive from the
72// rect position.
73// `style` — sizing + minimum-rect + color overrides.
74bool ResizeHandles(ImRect* rect, HandleMask mask = HandleMask::kAll,
75 float snap = 0.0f, ImGuiID id = 0,
76 const ResizeHandleStyle& style = {});
77
78// ---------------------------------------------------------------------------
79// Internal helpers exposed for tests.
80// ---------------------------------------------------------------------------
81namespace resize_handles_internal {
82
83// Round `v` to the nearest multiple of `snap`. When `snap <= 0` returns `v`
84// unchanged.
85float SnapCoord(float v, float snap);
86
87// Returns the zone under `mouse` given `rect`, mask, and handle size.
88// Corners take priority over edges when they overlap. Returns `kNone` when
89// no zone is hit or when the hit zone is disabled by `mask`.
90HandleZone HitTestZone(ImVec2 mouse, const ImRect& rect, HandleMask mask,
91 float handle_size);
92
93// Applies a drag delta to `*rect` at the given zone, snapping each moving
94// coordinate and enforcing minimum size. When `zone == kNone` the rect is
95// not touched.
96void ApplyDragDelta(ImRect* rect, HandleZone zone, ImVec2 delta, float snap,
97 float min_width, float min_height);
98
99// Returns the theme's selection_handle color as an ImVec4. Read at call time
100// so theme swaps take effect immediately.
101ImVec4 GetDefaultHandleColor();
102
103} // namespace resize_handles_internal
104
105} // namespace gui
106} // namespace yaze
107
108#endif // YAZE_APP_GUI_WIDGETS_RESIZE_HANDLES_H_
float SnapCoord(float v, float snap)
void ApplyDragDelta(ImRect *rect, HandleZone zone, ImVec2 delta, float snap, float min_width, float min_height)
HandleZone HitTestZone(ImVec2 mouse, const ImRect &rect, HandleMask mask, float handle_size)
constexpr HandleMask operator&(HandleMask a, HandleMask b)
constexpr bool Has(HandleMask all, HandleMask flag)
bool ResizeHandles(ImRect *rect, HandleMask mask, float snap, ImGuiID id, const ResizeHandleStyle &style)
constexpr HandleMask operator|(HandleMask a, HandleMask b)