forked from apoguita/Py4GW
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathradial menu.py
More file actions
366 lines (285 loc) · 12.7 KB
/
Copy pathradial menu.py
File metadata and controls
366 lines (285 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import PyImGui
import math
from Py4GWCoreLib.py4gwcorelib_src.Color import Color, ColorPalette
from dataclasses import dataclass
from abc import ABC, abstractmethod
class Shape(ABC):
def __init__(self, color: int = 0xFFFFFFFF):
self.color = color
self.hovered_color = 0xFFFFFFFF
self.is_visible = True
self.is_hovered = False
@abstractmethod
def draw(self):
"""Each shape must implement its own ImGui drawing logic."""
pass
@abstractmethod
def update_hover(self, mouse_x: float, mouse_y: float):
"""Each shape must implement its own mathematical hitbox."""
pass
def is_clicked(self, button: int = 0) -> bool:
"""
Concrete method inherited by all shapes.
Returns True if the shape is hovered and the specific mouse button is clicked.
0: Left, 1: Right, 2: Middle
"""
if self.is_visible and self.is_hovered:
return PyImGui.is_mouse_clicked(button)
return False
class RingSector(Shape):
def __init__(self, x, y, inner_r, outer_r, start_deg, end_deg, color, hovered_color=0xFFFFFFFF, outline_col=0xFF000000):
super().__init__(color)
self.hovered_color = hovered_color
self.center = [x, y]
self.inner_r = inner_r
self.outer_r = outer_r
self.start_deg = start_deg
self.end_deg = end_deg
self.outline_col = outline_col
self.border_thickness = 2.0
def update_hover(self, mouse_x, mouse_y):
dx, dy = mouse_x - self.center[0], mouse_y - self.center[1]
dist_sq = dx*dx + dy*dy
# Distance check
if not (self.inner_r**2 <= dist_sq <= self.outer_r**2):
self.is_hovered = False
return
# Angle check
angle = math.degrees(math.atan2(dy, dx)) % 360
if self.start_deg < self.end_deg:
self.is_hovered = self.start_deg <= angle <= self.end_deg
else:
self.is_hovered = angle >= self.start_deg or angle <= self.end_deg
def draw(self):
if not self.is_visible: return
if self.is_hovered:
draw_col = self.hovered_color
else:
draw_col = self.color
s_rad, e_rad = math.radians(self.start_deg), math.radians(self.end_deg)
# Fill
thickness = self.outer_r - self.inner_r
mid_r = self.inner_r + (thickness / 2)
PyImGui.path_clear()
PyImGui.path_arc_to(self.center[0], self.center[1], mid_r, s_rad, e_rad, 64)
PyImGui.path_stroke(draw_col, False, thickness)
# Borders (Curved)
PyImGui.path_clear()
PyImGui.path_arc_to(self.center[0], self.center[1], self.outer_r, s_rad, e_rad, 64)
PyImGui.path_stroke(self.outline_col, False, self.border_thickness)
PyImGui.path_clear()
PyImGui.path_arc_to(self.center[0], self.center[1], self.inner_r, s_rad, e_rad, 64)
PyImGui.path_stroke(self.outline_col, False, self.border_thickness)
# Side Lines
if abs(self.end_deg - self.start_deg) < 360.0:
for rad in [s_rad, e_rad]:
PyImGui.path_clear()
PyImGui.path_line_to(self.center[0] + math.cos(rad) * self.inner_r,
self.center[1] + math.sin(rad) * self.inner_r)
PyImGui.path_line_to(self.center[0] + math.cos(rad) * self.outer_r,
self.center[1] + math.sin(rad) * self.outer_r)
PyImGui.path_stroke(self.outline_col, False, self.border_thickness)
class Circle(Shape):
def __init__(self, x, y, radius, color, hovered_color,outline_col=0xFF000000):
super().__init__(color)
self.center = [float(x), float(y)]
self.radius = float(radius)
self.outline_col = outline_col
self.border_thickness = 2.0
self.segments = 64
self.hovered_color = hovered_color
def update_hover(self, mouse_x: float, mouse_y: float):
dx, dy = mouse_x - self.center[0], mouse_y - self.center[1]
self.is_hovered = (dx**2 + dy**2) <= (self.radius**2)
def draw(self):
if not self.is_visible: return
if self.is_hovered:
draw_col = self.hovered_color
else:
draw_col = self.color
# Draw filled circle primitive
PyImGui.draw_list_add_circle_filled(
self.center[0], self.center[1],
self.radius, draw_col, self.segments
)
# Draw outline primitive
if self.border_thickness > 0:
PyImGui.draw_list_add_circle(
self.center[0], self.center[1],
self.radius, self.outline_col,
self.segments, self.border_thickness
)
class Rectangle(Shape):
def __init__(self, x, y, width, height, color, hovered_color,outline_col=0xFF000000, rounding=0.0):
super().__init__(color)
self.pos = [float(x), float(y)]
self.size = [float(width), float(height)]
self.outline_col = outline_col
self.rounding = rounding
self.border_thickness = 2.0
self.hovered_color = hovered_color
def update_hover(self, mouse_x: float, mouse_y: float):
# AABB (Axis-Aligned Bounding Box) detection
self.is_hovered = (self.pos[0] <= mouse_x <= self.pos[0] + self.size[0] and
self.pos[1] <= mouse_y <= self.pos[1] + self.size[1])
def draw(self):
if not self.is_visible: return
if self.is_hovered:
draw_col = self.hovered_color
else:
draw_col = self.color
x2, y2 = self.pos[0] + self.size[0], self.pos[1] + self.size[1]
# Draw filled rect
PyImGui.draw_list_add_rect_filled(
self.pos[0], self.pos[1], x2, y2,
draw_col, self.rounding, 15 # 15 = all corners
)
# Draw outline
PyImGui.draw_list_add_rect(
self.pos[0], self.pos[1], x2, y2,
self.outline_col, self.rounding, 15, self.border_thickness
)
class Triangle(Shape):
def __init__(self, p1, p2, p3, color, hovered_color, outline_col=0xFF000000):
super().__init__(color)
self.points = [p1, p2, p3] # Each point is [x, y]
self.outline_col = outline_col
self.border_thickness = 2.0
self.hovered_color = hovered_color
def update_hover(self, mouse_x: float, mouse_y: float):
# Point-in-triangle test (Sign method)
def sign(p1, p2, p3):
return (p1[0] - p3[0]) * (p2[1] - p3[1]) - (p2[0] - p3[0]) * (p1[1] - p3[1])
d1 = sign([mouse_x, mouse_y], self.points[0], self.points[1])
d2 = sign([mouse_x, mouse_y], self.points[1], self.points[2])
d3 = sign([mouse_x, mouse_y], self.points[2], self.points[0])
has_neg = (d1 < 0) or (d2 < 0) or (d3 < 0)
has_pos = (d1 > 0) or (d2 > 0) or (d3 > 0)
self.is_hovered = not (has_neg and has_pos)
def draw(self):
if not self.is_visible: return
draw_col = 0xFFFFFFFF if self.is_hovered else self.color
p = self.points
PyImGui.draw_list_add_triangle_filled(
p[0][0], p[0][1], p[1][0], p[1][1], p[2][0], p[2][1], draw_col
)
PyImGui.draw_list_add_triangle(
p[0][0], p[0][1], p[1][0], p[1][1], p[2][0], p[2][1],
self.outline_col, self.border_thickness
)
#region old ring
color_pick = ColorPalette.GetColor("Green")
def draw_radial_interaction_layer():
global color_pick
center_x, center_y = 400,300
degree_start, degree_end = 2.0, 88.0
inner_radius, outer_radius = 50, 100
color = color_pick.to_color()
hovered_color = ColorPalette.GetColor("LightBlue").to_color()
margin = 20.0
diameter = (outer_radius * 2) + margin
x_left = center_x - outer_radius - (margin / 2)
y_top = center_y - outer_radius - (margin / 2)
PyImGui.set_next_window_pos(x_left, y_top)
PyImGui.set_next_window_size(diameter, diameter)
flags = (PyImGui.WindowFlags.NoTitleBar |
PyImGui.WindowFlags.NoResize |
PyImGui.WindowFlags.NoMove |
PyImGui.WindowFlags.NoBackground |
PyImGui.WindowFlags.NoScrollbar)
if PyImGui.begin("##RadialInteractionLayer", flags):
io = PyImGui.get_io()
mouse_x, mouse_y = io.mouse_pos_x, io.mouse_pos_y
circle = Circle(center_x, center_y, inner_radius -4, color, hovered_color)
circle.update_hover(mouse_x, mouse_y)
circle.draw()
sector = RingSector(center_x,center_y,float(inner_radius), float(outer_radius),degree_start,degree_end,color, hovered_color)
sector.update_hover(mouse_x, mouse_y)
sector.draw()
degree_start, degree_end = 92.0, 178.0
sector2 = RingSector(center_x,center_y,float(inner_radius),float(outer_radius),degree_start,degree_end,color, hovered_color)
sector2.update_hover(mouse_x, mouse_y)
sector2.draw()
degree_start, degree_end = 182.0, 268.0
sector3 = RingSector(center_x,center_y,float(inner_radius),float(outer_radius),degree_start,degree_end,color, hovered_color)
sector3.update_hover(mouse_x, mouse_y)
sector3.draw()
degree_start, degree_end = 272.0, 358.0
sector4 = RingSector(center_x,center_y,float(inner_radius),float(outer_radius),degree_start,degree_end,color, hovered_color)
sector4.update_hover(mouse_x, mouse_y)
sector4.draw()
PyImGui.end()
def draw_rectangle_layer():
global color_pick
# 1. Configuration (Matching your style)
color = color_pick.to_color()
hovered_color = ColorPalette.GetColor("LightBlue").to_color()
left = 500
top = 250
width = 100
height = 100
margin = 10.0 # Small margin to ensure borders aren't clipped
# 2. Setup the window specifically for this rectangle
# This prevents the "narrow" clipping issue by making the window wide enough
PyImGui.set_next_window_pos(left - (margin / 2), top - (margin / 2))
PyImGui.set_next_window_size(width + margin, height + margin)
flags = (PyImGui.WindowFlags.NoTitleBar |
PyImGui.WindowFlags.NoResize |
PyImGui.WindowFlags.NoMove |
PyImGui.WindowFlags.NoBackground |
PyImGui.WindowFlags.NoScrollbar)
if PyImGui.begin("##RectangleInteractionLayer", flags):
io = PyImGui.get_io()
mouse_x, mouse_y = io.mouse_pos_x, io.mouse_pos_y
# 3. Create and process the rectangle
rect = Rectangle(left, top, width, height, color, hovered_color)
rect.update_hover(mouse_x, mouse_y)
if rect.is_clicked(0):
print(f"Rectangle clicked at {left}, {top}")
rect.draw()
PyImGui.end()
def draw_triangle_layer():
global color_pick
color = color_pick.to_color()
hovered_color = ColorPalette.GetColor("LightBlue").to_color()
# Define 3 points for the triangle
p1 = [550, 140]
p2 = [500, 240]
p3 = [600, 240]
# Calculate bounding box for the window
xs = [p1[0], p2[0], p3[0]]
ys = [p1[1], p2[1], p3[1]]
left, top = min(xs), min(ys)
width, height = max(xs) - left, max(ys) - top
margin = 10.0
PyImGui.set_next_window_pos(left - (margin / 2), top - (margin / 2))
PyImGui.set_next_window_size(width + margin, height + margin)
flags = (PyImGui.WindowFlags.NoTitleBar |
PyImGui.WindowFlags.NoResize |
PyImGui.WindowFlags.NoMove |
PyImGui.WindowFlags.NoBackground |
PyImGui.WindowFlags.NoScrollbar)
if PyImGui.begin("##TriangleInteractionLayer", flags):
io = PyImGui.get_io()
mouse_x, mouse_y = io.mouse_pos_x, io.mouse_pos_y
tri = Triangle(p1, p2, p3, color, hovered_color)
tri.update_hover(mouse_x, mouse_y)
if tri.is_clicked(0):
print("Triangle clicked!")
tri.draw()
PyImGui.end()
def draw_window():
global color_pick
if PyImGui.begin("Radial Menu Example"):
current_color_normalized = color_pick.color_tuple
new_color_tuple = PyImGui.color_edit4("Color", current_color_normalized)
if new_color_tuple != current_color_normalized:
color_pick = Color.from_tuple_normalized(new_color_tuple)
PyImGui.end()
draw_radial_interaction_layer()
draw_rectangle_layer()
draw_triangle_layer()
def main():
draw_window()
if __name__ == "__main__":
main()