Logo

index : binary-tree

---

  • summary
  • about
  • tree
  • log
  • branches
<< path: root/things/binary-tree.git/html/src/input.jai blob: 5c7ba376795b692d63249d5fb0f9c81a6da2284a [raw] [clear marker]

        
0
1
2Mode :: enum #specified {
3 TRAD_TREE :: 0;
4 HYPER_TREE :: 1;
5 CELLS :: 2;
6}
7
8Gui_Feature :: enum_flags {
9 TOOLTIP;
10}
11
12
13input :: () {
14
15 /* ----------- *
16 * Zoom & Drag *
17 * ----------- */
18
19 if IsKeyDown(.LEFT_SHIFT) {
20 dragging = false;
21 if !selecting then SetMouseCursor(.DEFAULT);
22
23 if IsMouseButtonPressed(.LEFT) {
24 selecting = true;
25 selection.x = mouse_world.x;
26 selection.y = mouse_world.y;
27 SetMouseCursor(.RESIZE_NESW);
28 }
29 }
30
31 if IsMouseButtonPressed(.LEFT) {
32 drag_start = mouse_screen;
33 dragging = true;
34 SetMouseCursor(.RESIZE_ALL);
35 }
36
37 if IsMouseButtonReleased(.LEFT) {
38 dragging = false;
39 SetMouseCursor(.DEFAULT);
40
41 if selecting {
42 selecting = false;
43 zoom_to_selection();
44 }
45 }
46
47 if IsMouseButtonPressed(.RIGHT) {
48 if selecting then selecting = false;
49 }
50
51
52 if dragging {
53 camera.target.x -= (mouse_screen.x - drag_start.x) / camera.zoom;
54 camera.target.y -= (mouse_screen.y - drag_start.y) / camera.zoom;
55 drag_start = mouse_screen;
56 }
57
58
59 wheel := GetMouseWheelMove();
60 if wheel != 0.0 {
61 before := Vector2.{
62 camera.target.x + (mouse_screen.x - camera.offset.x) / camera.zoom,
63 camera.target.y + (mouse_screen.y - camera.offset.y) / camera.zoom
64 };
65
66 camera.zoom *= (1.0 + wheel * 0.1);
67
68 // TODO: Maybe lerp?
69
70 //! t := 1.0 - exp(-8.0 * frame_time);
71 //! camera.zoom *= lerp(thing1, thing2, t);
72 camera.zoom = clamp(camera.zoom, 0.1, 5.0);
73
74 camera.target.x = before.x - (mouse_screen.x - camera.offset.x) / camera.zoom;
75 camera.target.y = before.y - (mouse_screen.y - camera.offset.y) / camera.zoom;
76 }
77
78
79 /* ----- *
80 * Modes *
81 * ----- */
82
83 if IsKeyPressed(.F1) {
84 if mode == .TRAD_TREE {
85 vtree_fit_zoom();
86 } else {
87 mode = .TRAD_TREE;
88 state_switch(mode);
89 }
90 }
91 else if IsKeyPressed(.F2) {
92 if mode == .HYPER_TREE {
93 htree_fit_zoom();
94 } else {
95 mode = .HYPER_TREE;
96 state_switch(mode);
97 }
98 }
99 else if IsKeyPressed(.F3) {
100 if mode == .CELLS {
101 cells_fit_zoom();
102 } else {
103 mode = .CELLS;
104 state_switch(mode);
105 }
106 }
107
108 /* ---------------------- *
109 * Context Based Features *
110 * ---------------------- */
111
112 if mode == {
113 case .CELLS;
114 if IsKeyPressed(.KEY_T) then flag_toggle(.TOOLTIP);
115 }
116}
117
118input_draw_selection :: () {
119 if !selecting return;
120
121 using selection;
122 width = mouse_world.x - x;
123 height = mouse_world.y - y;
124
125 selection_normalized = normalize_selection(selection);
126 DrawRectangleLinesEx(selection_normalized, 2.0, GREEN);
127}
128
129flag_has :: (flag: Gui_Feature) -> bool {
130 return cast(bool, gui_feature & flag);
131}
132
133flag_add :: (flag: Gui_Feature) {
134 gui_feature |= flag;
135}
136
137flag_remove :: (flag: Gui_Feature) {
138 gui_feature &= ~flag;
139}
140
141flag_toggle :: (flag: Gui_Feature) {
142 gui_feature ^= flag;
143}
144
145
146#scope_file
147
148
149drag_start: Vector2;
150dragging: bool;
151
152selection: Rectangle;
153selection_normalized: Rectangle;
154selecting: bool;
155
156
157normalize_selection :: (using selection: Rectangle) -> Rectangle {
158 nx := ifx width < 0 then x + width else x;
159 ny := ifx height < 0 then y + height else y;
160
161 return { nx, ny, abs(width), abs(height) };
162}
163
164zoom_to_selection :: () {
165 using selection_normalized;
166
167 center := Vector2.{ x + width / 2.0, y + height / 2.0 };
168 zoom_x := cast(float, GetScreenWidth() / width);
169 zoom_y := cast(float, GetScreenHeight() / height);
170
171 new_zoom := min(zoom_x, zoom_y) * 0.9;
172
173 camera.offset = { GetScreenWidth() / 2.0, GetScreenHeight() / 2.0 };
174 camera.target = center;
175 camera.zoom = new_zoom;
176}
177
178
Copyright 2026  E766CB298A6D1E64 | Git-Thing heavily inspired by cgit