forked from dmtr99/Radial_Menu_V2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Radial Menu.ah2
402 lines (340 loc) · 16.2 KB
/
Radial Menu.ah2
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
; This scripts creates a radial menu / pie menu
; The selected menu item is returned as a string.
; mbutton is used in this example to trigger the menu
#SingleInstance Force
#Requires AutoHotkey v2.0-beta.3
;#NoEnv
; Uncomment if Gdip.ahk is not in your standard library
#Include Gdip_All.ahk
mButton::
{
GMenu := Radial_Menu()
GMenu.SetSections("8")
GMenu.Add("Save", "Images/analysis_meas_distance16.gif", 1)
GMenu.Add("Save2", "Images/smt_flat_wall_mt.gif", 2)
GMenu.Add2("Save2special", "Images/analysis_meas_distance16.gif", 2)
GMenu.SetKey("mbutton")
GMenu.SetKeySpecial("Ctrl")
GMenu.Add("Save3", "", 3)
GMenu.Add("Save4", "Images/smt_flat_wall_mt.gif", 4)
GMenu.Add("Save5", "Images/smt_flat_wall_mt.gif", 5)
GMenu.Add2("Save5se", "Images/fbcp_asm_image.gif", 5)
GMenu.Add("Save6", "Images/smt_flat_wall_mt.gif", 6)
GMenu.Add("", "", 7)
GMenu.Add("Save8", "Images/smt_flat_wall_mt.gif", 8)
Result := GMenu.Show()
; This Result string can be used in if else statements, in this demo i just use a message box.
MsgBox(Result)
}
; return
Class Radial_Menu {
__New() {
This.Sections := "4"
This.RM_Key := "Capslock"
This.Sect := Map()
This.Sect_Name := Map()
This.Sect_Img := Map()
This.Sect_Name2 := Map()
This.Sect_Img2 := Map()
}
SetSections(Sections) {
This.Sections := Sections
}
SetKey(RM_Key) {
This.RM_Key := RM_Key
}
SetKeySpecial(RM_Key2) {
This.RM_Key2 := RM_Key2
}
Add(SectionName, SectionImg, ArcNr) {
if !HasProp(This.Sect,ArcNr){
This.Sect.%ArcNr% := Map()
}
if (SectionImg!=""){
This.Sect.%ArcNr%.Img := SectionImg
}
This.Sect.%ArcNr%.Name := SectionName
if (This.Sections < ArcNr) {
This.Sections := ArcNr
}
}
Add2(SectionName2, SectionImg2, ArcNr) {
if !HasProp(This.Sect, ArcNr) {
This.Sect.%ArcNr% := Map()
}
if (SectionImg2 != "") {
This.Sect.%ArcNr%.Img2 := SectionImg2
}
This.Sect.%ArcNr%.Name2 := SectionName2
if (This.Sections < ArcNr) {
This.Sections := ArcNr
}
}
Show() {
static
SectName := ""
CoordMode "Mouse", "Screen"
MouseGetPos(&X_Center, &Y_Center)
WinGetPos(&X_Win, &Y_Win, , , "A")
R_1 := 100
R_2 := R_1 * 0.2
Offset := 2
R_3 := R_1 + Offset * 2 + 10
X_Gui := X_Center - R_3
Y_Gui := Y_Center - R_3
Height_Gui := R_3 * 2
Width_Gui := R_3 * 2
Width := R_3 * 2
height := R_3 * 2
; Destroying old menu if exists
if WinExist("RM_Menu") {
WinClose("RM_Menu")
}
; Start gdi+
global pToken
tat := "var"
If !pToken := Gdip_Startup() {
MsgBox("Gdiplus failed to start.Please ensure you have gdiplus on your system", , 48)
ExitApp
}
OnExit((ExitReason, ExitCode) => Gdip_Shutdown(pToken))
; Create a layered window (+E0x80000 : must be used for UpdateLayeredWindow to work!) that is always on top (+AlwaysOnTop), has no taskbar entry or caption
Gui_Radial_Menu := Gui("-Caption +E0x80000 +LastFound +AlwaysOnTop +ToolWindow +OwnDialogs")
; Show the window
Gui_Radial_Menu.Title := "RM_Menu"
Gui_Radial_Menu.Show("NA x" . X_Gui . " y" . Y_Gui . " w" . Width_Gui . " h" . Height_Gui)
; Get a handle to this window we have created in order to update it later
hwnd1 := WinExist()
ColorBackGround := "FCFCFC"
ColorLineBackGround := "C6DFFC"
ColorSelected := "C6DFFC"
ColorLineSelected := "F5E5D6"
Loop This.Sections { ;Setting Bitmap images of sections
if HasProp(This.Sect.%A_Index%,"Img") {
if FileExist(This.Sect.%A_Index%.Img) {
This.Sect.%A_Index%.pBitmap := Gdip_CreateBitmapFromFile(This.Sect.%A_Index%.Img)
} else if (This.Sect.%A_Index%.Img != "" and IsObject(This.Sect.%A_Index%.Img)) {
This.Sect.%A_Index%.pBitmap := Gdip_CreateBitmapFromHBITMAP(This.Sect.%A_Index%.Img)
}
if HasProp(This.Sect.%A_Index%, "pBitmap"){
This.Sect.%A_Index%.bWidth := Gdip_GetImageWidth(This.Sect.%A_Index%.pBitmap)
This.Sect.%A_Index%.bHeight := Gdip_GetImageHeight(This.Sect.%A_Index%.pBitmap)
}
}
if HasProp(This.Sect.%A_Index%, "Img2") {
if FileExist(This.Sect.%A_Index%.Img2) {
This.Sect.%A_Index%.pBitmap2 := Gdip_CreateBitmapFromFile(This.Sect.%A_Index%.Img2)
} else if (This.Sect.%A_Index%.Img2 != "" and IsObject(This.Sect.%A_Index%.Img2)) {
This.Sect.%A_Index%.pBitmap2 := Gdip_CreateBitmapFromHBITMAP(This.Sect.%A_Index%.Img2)
}
if HasProp(This.Sect.%A_Index%, "pBitmap2") {
This.Sect.%A_Index%.bWidth2 := Gdip_GetImageWidth(This.Sect.%A_Index%.pBitmap2)
This.Sect.%A_Index%.bHeight2 := Gdip_GetImageHeight(This.Sect.%A_Index%.pBitmap2)
}
}
}
Counter := 0
loop This.Sections { ;Calculating Section Points
SectionAngle := 2 * 3.141592653589793 / This.Sections * (A_Index - 1)
This.Sect.%A_Index%.X_Bitmap := R_3 + (R_1 - 30) * cos(SectionAngle) - 8
This.Sect.%A_Index%.Y_Bitmap := R_3 + (R_1 - 30) * sin(SectionAngle) - 8
This.Sect.%A_Index%.PointsA := This.Gdip_GetPointsSection(R_3, R_3, R_1 + Offset * 2 + 10, R_1 + Offset * 2, This.Sections, Offset, A_Index)
This.Sect.%A_Index%.Points := This.Gdip_GetPointsSection(R_3, R_3, R_1, R_2, This.Sections, Offset, A_Index)
}
; Setting brushes and Pens
pBrush := Gdip_BrushCreateSolid("0xFF" ColorBackGround)
pBrushA := Gdip_BrushCreateSolid("0xFF" ColorSelected)
pBrushC := Gdip_BrushCreateSolid("0X01" ColorBackGround)
pPen := Gdip_CreatePen("0xFF" ColorLineBackGround, 1)
pPenA := Gdip_CreatePen("0xD2" ColorLineSelected, 1)
hdc := CreateCompatibleDC()
G := Gdip_GraphicsFromHDC(hdc)
Gdip_SetSmoothingMode(G, 4)
RM_KeyState_D := 0
Section_Mouse_Prev := -1
X_Mouse_P := -1
Y_Mouse_P := -1
Gdip_FillEllipse(G, pBrushC, R_3 - R_1, R_3 - R_1, 2 * R_1, 2 * R_1)
loop {
RM_KeyState := GetKeyState(This.RM_Key)
RM_KeyState := GetKeyState("mbutton")
RM_KeyState2 := GetKeyState(This.RM_Key2)
;ToolTip("RM_KeyState: " RM_KeyState This.RM_Key)
if !WinExist("RM_Menu") {
Exit
}
if (RM_KeyState = 1) {
RM_KeyState_D := 1
}
if (RM_KeyState = 0 and RM_KeyState_D = 1) {
Section_Mouse := This.RM_GetSection(This.Sections, R_2, X_Center, Y_Center)
MsgBox("Text")
SectName := HasProp(This.Sect.%Section_Mouse%, "Name") ? This.Sect.%Section_Mouse%.Name : ""
if (Section_Mouse != 0) {
break
}
RM_KeyState_D := 0
}
if (GetKeyState("LButton")) {
Section_Mouse := This.RM_GetSection(This.Sections, R_2, X_Center, Y_Center)
SectName := Section_Mouse = 0 ? "" : HasProp(This.Sect.%Section_Mouse%, "Name") ? This.Sect.%Section_Mouse%.Name: ""
break
}
if GetKeyState("Escape") {
Section_Mouse := 0
SectName := ""
break
}
CoordMode("Mouse", "Screen")
MouseGetPos(&X_Mouse, &Y_Mouse)
X_Rel := X_Mouse - X_Center
Y_Rel := Y_Mouse - Y_Center
Center_Distance := Sqrt(X_Rel * X_Rel + Y_Rel * Y_Rel)
Section_Mouse := This.RM_GetSection(This.Sections, R_2, X_Center, Y_Center)
if (Center_Distance > R_1) {
break
}
if (Section_Mouse = 0 or Section_Mouse = "") {
ToolTip()
SectName := ""
SectName_N := ""
}
else {
Counter++
SectName_N := HasProp(This.Sect.%Section_Mouse%, "Name") ? This.Sect.%Section_Mouse%.Name : ""
SectName2 := HasProp(This.Sect.%Section_Mouse%,"Name2") ? This.Sect.%Section_Mouse%.Name2 : ""
if (HasProp(This, "RM_Key2") and GetKeyState(This.RM_Key2, "P") and SectName2 != "") {
SectName_N := SectName2
}
if ((X_Mouse_P != X_Mouse) or (Y_Mouse_P != Y_Mouse) or SectName_N != SectName or Counter > 500) {
SectName := SectName_N
MouseGetPos(&X_Mouse_P, &Y_Mouse_P)
if (Counter > 500) {
ToolTip(SectName_N)
Counter := 0
}
}
}
if (Section_Mouse != Section_Mouse_Prev or A_Index = 1 or RM_KeyState2_Prev != RM_KeyState2) { ; Update GDIP
Gdip_GraphicsClear(G)
hbm := CreateDIBSection(Height_Gui, Height_Gui) ; Create a gdi bitmap with width and height of what we are going to draw into it. This is the entire drawing area for everything
hdc := CreateCompatibleDC() ; Get a device context compatible with the screen
obm := SelectObject(hdc, hbm) ; Select the bitmap into the device context
G := Gdip_GraphicsFromHDC(hdc) ; Get a pointer to the graphics of the bitmap, for use with drawing functions
; Set the smoothing mode to antialias = 4 to make shapes appear smother (only used for vector drawing and filling)
Gdip_SetSmoothingMode(G, 4)
Gdip_FillEllipse(G, pBrushC, R_3 - R_1, R_3 - R_1, 2 * R_1, 2 * R_1)
loop This.Sections {
Section := This.Sect.%A_Index%
SectionAngle := 2 * 3.141592653589793 / This.Sections * (A_Index - 1)
if (Section.Name = "") {
continue
}
If (A_Index = Section_Mouse) {
Gdip_FillPolygon(G, pBrushA, Section.Points)
Gdip_DrawLines(G, pPenA, Section.Points)
Gdip_FillPolygon(G, pBrushA, Section.PointsA)
Gdip_DrawLines(G, pPenA, Section.PointsA)
} else {
Gdip_FillPolygon(G, pBrush, Section.Points)
Gdip_DrawLines(G, pPen, Section.Points)
}
if (GetKeyState(This.RM_Key2, "P") and HasProp(Section, "pBitmap2")) {
Gdip_DrawImage(G, Section.pBitmap2, Section.X_Bitmap, Section.Y_Bitmap, 16, 16 * Section.bHeight2 / Section.bWidth2, 0, 0, Section.bWidth2, Section.bHeight2)
} else if HasProp(Section, "pBitmap"){
Gdip_DrawImage(G, Section.pBitmap, Section.X_Bitmap, Section.Y_Bitmap, 16, 16 * Section.bHeight / Section.bWidth, 0, 0, Section.bWidth, Section.bHeight)
}
if !HasProp(Section, "pBitmap") and HasProp(Section, "Name") and Section.Name != "" {
Gdip_TextToGraphics(G, Section.Name, "vCenter x" This.Sect.%A_Index%.X_Bitmap -20 + 8 " y" This.Sect.%A_Index%.Y_Bitmap -20 + 8, , 40, 40)
}
}
; Update the specified window we have created (hwnd1) with a handle to our bitmap (hdc), specifying the x,y,w,h we want it positioned on our screen
; So this will position our gui at (0,0) with the Width and Height specified earlier
UpdateLayeredWindow(hwnd1, hdc, X_Gui, Y_Gui, Width, Height)
SelectObject(hdc, obm) ; Select the object back into the hdc
DeleteObject(hbm) ; Now the bitmap may be deleted
DeleteDC(hdc) ; Also the device context related to the bitmap may be deleted
Gdip_DeleteGraphics(G) ; The graphics may now be deleted
}
RM_KeyState2_Prev := RM_KeyState2
Section_Mouse_Prev := Section_Mouse
}
Tooltip
SelectObject(hdc, obm) ; Select the object back into the hdc
DeleteObject(hbm) ; Now the bitmap may be deleted
DeleteDC(hdc) ; Also the device context related to the bitmap may be deleted
Gdip_DeleteGraphics(G) ; The graphics may now be deleted
loop This.Sections {
if HasProp(This.Sect.%A_Index%,"pBitmap"){
Gdip_DisposeImage(This.Sect.%A_Index%.pBitmap)
}
if HasProp(This.Sect.%A_Index%, "pBitmap2") {
Gdip_DisposeImage(This.Sect.%A_Index%.pBitmap2)
}
}
Gdip_DeleteBrush(pBrushC)
Gdip_DeleteBrush(pBrush)
Gdip_DeleteBrush(pBrushA)
Gdip_DeletePen(pPen)
Gdip_DeletePen(pPenA)
Gdip_Shutdown(pToken)
Gui_Radial_Menu.Destroy()
Return SectName
}
;#######################################################################
Gdip_GetPointsSection(X_Center, Y_Center, R_1, R_2, Sections, Offset, Section := "1") {
Section := Section - 1
SectionAngle := 2 * 3.141592653589793 / Sections
R_2_Min := 4 * Offset / Sin(SectionAngle)
R_2 := R_2 > R_2_Min ? R_2 : R_2_Min
SweepAngle := ACos((R_1 * cos(SectionAngle / 2) + Offset * sin(SectionAngle / 2)) / R_1) * 2
SweepAngle_2 := ACos((R_2 * cos(SectionAngle / 2) + Offset * sin(SectionAngle / 2)) / R_2) * 2
Loop_Sections := round(R_1 * SweepAngle)
StartAngle := -SweepAngle / 2 + SectionAngle * (Section)
loop Loop_Sections {
Angle := StartAngle + (A_Index - 1) * SweepAngle / (Loop_Sections - 1)
X_Arc := round(X_Center + R_1 * cos(Angle))
Y_Arc := round(Y_Center + R_1 * sin(Angle))
if (A_Index = 1) {
Points := X_Arc "," Y_Arc
X_Arc_Start := X_Arc
Y_Arc_Start := Y_Arc
continue
}
Points .= "|" X_Arc "," Y_Arc
}
Loop_Sections := round(R_2 * SweepAngle_2)
StartAngle_2 := SweepAngle_2 / 2 + SectionAngle * (Section)
loop Loop_Sections {
Angle := StartAngle_2 - (A_Index - 1) * SweepAngle_2 / (Loop_Sections - 1)
X_Arc := round(X_Center + R_2 * cos(Angle))
Y_Arc := round(Y_Center + R_2 * sin(Angle))
Points .= "|" X_Arc "," Y_Arc
}
Points .= "|" X_Arc_Start "," Y_Arc_Start
return Points
}
;#######################################################################
RM_GetSection(Sections, R_2, X_Center, Y_Center) {
CoordMode("Mouse", "Screen")
WinGetPos(&X_Win, &Y_Win, , , "RM_Menu")
MouseGetPos(&X_Mouse, &Y_Mouse)
X_Rel := X_Mouse - X_Center
Y_Rel := Y_Mouse - Y_Center
Distance_Center := Sqrt(X_Rel * X_Rel + Y_Rel * Y_Rel)
Section_Mouse := ""
X_Rel := X_Rel = 0 ? 0.01 : X_Rel ; (correction to prevent X to be 0)
Y_Rel := Y_Rel = 0 ? 0.01 : Y_Rel ; (correction to prevent X to be 0)
if (Distance_Center < R_2) {
Section_Mouse := 0
} else if (Distance_Center > R_2) {
a := X_Rel = 0 ? (Y_Rel = 0 ? 0 : Y_Rel > 0 ? 90 : 270) : atan(Y_Rel / X_Rel) * 57.2957795130823209 ; 180/pi
Angle := X_Rel < 0 ? 180 + a : a < 0 ? 360 + a : a
Section_Mouse := 1 + round(Angle / 360 * Sections)
if (Section_Mouse > Sections) {
Section_Mouse := 1
}
}
return Section_Mouse
}
}