-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.agc
More file actions
384 lines (353 loc) · 12.2 KB
/
Copy pathmain.agc
File metadata and controls
384 lines (353 loc) · 12.2 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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// Project: AGKPythonPluginTest
// Created: 2017-11-14
// Copyright 2017 Adam Biser
#option_explicit
#import_plugin PythonPlugin As Py
#constant KEY_ESCAPE 27
#constant NEWLINE CHR(10)
#constant WINDOW_WIDTH 1024
#constant WINDOW_HEIGHT 768
#constant STATUS_X 0
#constant STATUS_Y 200
#constant STATUS_WIDTH 1024
#constant STATUS_HEIGHT 568
// Set up window and display.
SetWindowTitle("Python 3 Example")
SetWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT, 0)
SetVirtualResolution(WINDOW_WIDTH, WINDOW_HEIGHT)
SetSyncRate(30, 0)
UseNewDefaultFonts(1)
SetPrintSize(20)
SetErrorMode(2) // show all errors
CreateStatusArea()
// Virtual buttons
#constant CREATE_INTEGER_BUTTON 1
#constant CHANGE_NAME_BUTTON 2
#constant RUN_FILE_BUTTON 3
#constant BUILD_VALUE_BUTTON 4
#constant SIMPLE_STRING_BUTTON 5
#constant CALL_FUNCTION_BUTTON 6
#constant CAUSE_ERROR_BUTTON 7
global buttonText as string[6] = ["Create Int", "Change_Name", "Run File", "Build_Value", "Simple_String", "Call_Function", "Cause_Error"]
x as integer
for x = 0 to buttonText.length
CreateButton(x + 1, 50 + x * 100, 50, ReplaceString(buttonText[x], "_", NEWLINE, -1))
next
Py.Py_SetProgramName("UsePythonFromAGK")
Py.Py_SetPythonHome("media") // Does not affect file paths, just imports.
Py.Py_Initialize()
// Show general information
AddStatus("Py_GetProgramName: " + Py.Py_GetProgramName())
AddStatus("Py_IsInitialized: " + TF(Py.Py_IsInitialized()))
AddStatus("Py_GetVersion: " + Py.Py_GetVersion())
AddStatus("Py_GetProgramFullPath: " + Py.Py_GetProgramFullPath())
AddStatus("Py_GetPythonHome: " + Py.Py_GetPythonHome())
//~ AddStatus("Py_GetCopyright: " + Py.Py_GetCopyright())
//
// The main loop
//
do
Sync()
CheckMouseWheel()
CheckInput()
if GetRawKeyPressed(KEY_ESCAPE)
exit
endif
loop
Py.Py_Finalize()
Quit()
Function CheckInput()
if GetVirtualButtonPressed(CREATE_INTEGER_BUTTON)
AddStatus("---------------------------")
CreateInteger()
endif
if GetVirtualButtonPressed(CHANGE_NAME_BUTTON)
AddStatus("---------------------------")
ChangeName()
endif
if GetVirtualButtonPressed(RUN_FILE_BUTTON)
AddStatus("---------------------------")
RunFile()
endif
if GetVirtualButtonPressed(BUILD_VALUE_BUTTON)
AddStatus("---------------------------")
BuildValue()
endif
if GetVirtualButtonPressed(SIMPLE_STRING_BUTTON)
AddStatus("---------------------------")
RunSimpleString()
endif
if GetVirtualButtonPressed(CALL_FUNCTION_BUTTON)
AddStatus("---------------------------")
CallPythonFunction()
endif
if GetVirtualButtonPressed(CAUSE_ERROR_BUTTON)
AddStatus("---------------------------")
CausePythonError()
endif
EndFunction
//
// Creates a PyLong object that is initialized to 500, prints its value, then destroys the PyObject.
//
Function CreateInteger()
handle as integer
handle = Py.PyLong_FromLong(500)
AddStatus("PyLong_AsLong: Handle " + str(handle) + " = " + str(Py.PyLong_AsLong(handle)))
// Remember to DECREF PyObjects that you create!
Py.Py_DECREF(handle)
AddStatus("Py_REFCNT: " + str(Py.Py_REFCNT(handle)))
EndFunction
//
// Creates a PyUnicode "name" object that starts as one value and is then changed by running a script string.
//
Function ChangeName()
// Set up a dictionary of local values.
hLocals as integer
hLocals = Py.PyDict_New()
hNameValue as integer
hNameValue = Py.PyUnicode_FromString("Alex")
Py.PyDict_SetItemHandle(hLocals, "name", hNameValue)
AddStatus("hLocals: " + str(hLocals) + ", type: " + Py.Py_TYPE_NAME(hLocals))
AddStatus("hNameValue: " + str(hNameValue) + ", type: " + Py.Py_TYPE_NAME(hNameValue))
AddStatus("Name Before: " + Py.PyDict_GetItemString(hLocals, "name"))
// Run a simple script from a string to set the name.
script as string
script = "name = 'Bob'"
hResult as integer
hResult = Py.PyRun_String(script, 0, hLocals)
AddStatus("hResult: " + str(hResult) + ", type: " + Py.Py_TYPE_NAME(hResult))
AddStatus("Name After: " + Py.PyDict_GetItemString(hLocals, "name"))
// Remember to DECREF PyObjects that you create!
Py.Py_DECREF(hResult)
Py.Py_DECREF(hLocals)
Py.Py_DECREF(hNameValue)
AddStatus("Py_REFCNT hLocals: " + str(Py.Py_REFCNT(hLocals)))
AddStatus("Py_REFCNT hNameValue: " + str(Py.Py_REFCNT(hNameValue)))
AddStatus("Py_REFCNT hResult: " + str(Py.Py_REFCNT(hResult)))
EndFunction
//
// Runs a file passing in a "name" and reading the "result" set by the script.
//
Function RunFile()
hLocals as integer
hLocals = Py.PyDict_New()
hNameValue as integer
hNameValue = Py.PyUnicode_FromString("Alex")
Py.PyDict_SetItemHandle(hLocals, "name", hNameValue)
AddStatus("hLocals: " + str(hLocals) + ", type: " + Py.Py_TYPE_NAME(hLocals))
AddStatus("hNameValue: " + str(hNameValue) + ", type: " + Py.Py_TYPE_NAME(hNameValue))
// Run a simple script file to get a result.
// Since the file uses import, this needs to use GetMainModuleDict to get the __main__ dict and use it as globals.
hResult as integer
hResult = Py.PyRun_File("media/run.py", Py.GetMainModuleDict(), hLocals)
AddStatus("hResult: " + str(hResult) + ", type: " + Py.Py_TYPE_NAME(hResult))
AddStatus("Result text: " + Py.PyDict_GetItemString(hLocals, "result"))
// Remember to DECREF PyObjects that you create!
Py.Py_DECREF(hResult)
Py.Py_DECREF(hLocals)
Py.Py_DECREF(hNameValue)
AddStatus("Py_REFCNT hLocals: " + str(Py.Py_REFCNT(hLocals)))
AddStatus("Py_REFCNT hNameValue: " + str(Py.Py_REFCNT(hNameValue)))
AddStatus("Py_REFCNT hResult: " + str(Py.Py_REFCNT(hResult)))
EndFunction
//
// Build a PyObject value from a format string and csv string.
// NOTE: This is limited to strings, integers, and floats.
//
Function BuildValue()
AddStatus("Creating a tuple using Py_BuildValue")
hValue as integer
hValue = Py.Py_BuildValue("ssiif", "hello_this_is long_text,'test,this',2,365,2.5886")
AddStatus("Py_BuildValue handle: " + str(hValue) + ", type: " + Py.Py_TYPE_NAME(hValue))
AddStatus("Py_BuildValue repr: " + Py.PyObject_Repr(hValue))
x as integer
for x = 0 to Py.PyTuple_Size(hValue) - 1
hItem as integer
hItem = Py.PyTuple_GetItemHandle(hValue, x) // This returns a BORROWED ref.
AddStatus("Item " + str(x) + " handle: " + str(hItem) + ", Py_REFCNT: " + str(Py.Py_REFCNT(hItem)) + ", type : " + Py.Py_TYPE_NAME(hItem) + ", repr: " + Py.PyObject_Repr(hItem))
next
// Remember to DECREF PyObjects that you create!
Py.Py_DECREF(hValue)
AddStatus("Py_REFCNT hValue: " + str(Py.Py_REFCNT(hValue)))
EndFunction
//
// Another example of running a script from a string, this time using PyRun_SimpleString.
// Can't do any interaction when using PyRun_SimpleString. Use PyRun_String.
//
Function RunSimpleString()
script as string
script = script + "from time import time,ctime" + NEWLINE
script = script + "result = 'The time is {}, and here\'s a number: {}'.format(ctime(time()), 2.5)"
AddStatus("Script:" + NEWLINE + script)
// Return: 0 = Success, -1 = Failure
AddStatus("PyRun_SimpleString: " + str(Py.PyRun_SimpleString(script)))
EndFunction
//
// How to call a function in a module, pass it some parameters, and get its return value.
//
Function CallPythonFunction()
// Don't need "media." because of Py_SetPythonHome call above.
hModule as integer
hModule = Py.PyImport_Import("module")
AddStatus("PyImport_Import hModule: " + str(hModule) + ", type: " + Py.Py_TYPE_NAME(hModule))
if hModule
hFunc as integer
hFunc = Py.PyObject_GetAttrHandle(hModule, "test")
AddStatus("PyObject_GetAttrHandle hFunc: " + str(hFunc) + ", type: " + Py.Py_TYPE_NAME(hFunc))
if hFunc
hArgs as integer
hArgs = Py.Py_BuildValue("si", "Alex,100")
AddStatus("Py_BuildValue hArgs: " + str(hArgs) + ", type: " + Py.Py_TYPE_NAME(hArgs))
if hArgs
AddStatus("Arguments: " + Py.PyObject_Repr(hArgs))
hResult as integer
hResult = Py.PyObject_Call(hFunc, hArgs, 0)
AddStatus("PyObject_Call hResult: " + str(hResult) + ", type: " + Py.Py_TYPE_NAME(hResult))
if hResult
AddStatus("RESULT: " + Py.PyUnicode_AsString(hResult))
Py.Py_DECREF(hResult)
endif
Py.Py_DECREF(hArgs)
endif
Py.Py_DECREF(hFunc)
endif
Py.Py_DECREF(hModule)
endif
AddStatus("Py_REFCNT hModule: " + str(Py.Py_REFCNT(hModule)))
AddStatus("Py_REFCNT hFunc: " + str(Py.Py_REFCNT(hFunc)))
AddStatus("Py_REFCNT hArgs: " + str(Py.Py_REFCNT(hArgs)))
AddStatus("Py_REFCNT hResult: " + str(Py.Py_REFCNT(hResult)))
EndFunction
//
// Shows how Python errors are reported.
//
Function CausePythonError()
// This causes an error because "name" is not defined.
script as string
script = "response = 'This is going to cause a problem, {}.'.format(name)"
hResult as integer
hResult = Py.PyRun_String(script, 0, 0)
if GetErrorOccurred()
AddStatus("ERROR:" + NEWLINE + GetLastError())
endif
AddStatus("hResult: " + str(hResult) + ", type: " + Py.Py_TYPE_NAME(hResult))
// Remember to DECREF PyObjects that you create!
if hResult
Py.Py_DECREF(hResult)
endif
AddStatus("Py_REFCNT hResult: " + str(Py.Py_REFCNT(hResult)))
EndFunction
//---------------------------------------------------------------------
//
// Keep the UI stuff separate to highligh the plugin code.
//
// The type holds IDs for all text controls.
global text as TextControls
Type TextControls
status as integer
EndType
Function CreateStatusArea()
CreateTextEx(STATUS_X, STATUS_Y - 25, "Status Messages (Scrollable)")
spriteID as integer
spriteID = CreateSprite(CreateImageColor(32, 32, 32, 255))
SetSpritePosition(spriteID, STATUS_X, STATUS_Y)
SetSpriteSize(spriteID, STATUS_WIDTH, STATUS_HEIGHT)
// Set up scrolling status text
text.status = CreateText("")
SetTextPosition(text.status, STATUS_X, STATUS_Y)
SetTextScissor(text.status, STATUS_X, STATUS_Y, STATUS_X + STATUS_WIDTH, STATUS_Y + STATUS_HEIGHT)
SetTextSize(text.status, 18)
SetTextVisible(text.status, 1)
EndFunction
Function CreateTextEx(x as float, y as float, text as string)
id as integer
id = CreateText(text)
SetTextPosition(id, x, y)
SetTextSize(id, 20)
EndFunction id
Function CreateButton(id as integer, x as integer, y as integer, txt as string)
AddVirtualButton(id, x, y, 80)
//~ SetVirtualButtonSize(id, 150, 80)
SetVirtualButtonText(id, txt)
//~ SetButtonEnabled(id, 0)
EndFunction
Function SetButtonEnabled(id as integer, enabled as integer)
SetVirtualButtonActive(id, enabled)
if enabled
SetVirtualButtonColor(id, 192, 192, 192)
else
SetVirtualButtonColor(id, 64, 64, 64)
endif
EndFunction
Function GetDateFromUnix(unix as integer)
text as string
text = PadStr(GetYearFromUnix(unix), 4)
text = text + "-" + PadStr(GetMonthFromUnix(unix), 2)
text = text + "-" + PadStr(GetDaysFromUnix(unix), 2)
text = text + " " + PadStr(GetHoursFromUnix(unix), 2)
text = text + ":" + PadStr(GetMinutesFromUnix(unix), 2)
text = text + ":" + PadStr(GetSecondsFromUnix(unix), 2)
EndFunction text
Function PadStr(number as integer, length as integer)
text as string
text = str(number)
if len(text) < length
text = ReplaceString(Spaces(length - len(text)), " ", "0", -1) + text
endif
EndFunction text
Function AddStatus(status as string)
SetTextString(text.status, GetTextString(text.status) + GetDateFromUnix(GetUnixTime()) + ": " + status + NEWLINE)
// Scroll the text upward.
height as float
height = GetTextTotalHeight(text.status)
if height < STATUS_HEIGHT
height = STATUS_HEIGHT
endif
SetTextPosition(text.status, STATUS_X, STATUS_Y - (height - STATUS_HEIGHT))
EndFunction
// Scrollable status window.
Function CheckMouseWheel()
delta as float
delta = GetRawMouseWheelDelta()
if delta <> 0
mouseX as float
mouseY as float
mouseX = GetPointerX()
mouseY = GetPointerY()
if mouseX < STATUS_X or mouseY < STATUS_Y or mouseX >= STATUS_X + STATUS_WIDTH or mouseY >= STATUS_Y + STATUS_HEIGHT
ExitFunction
endif
newY as float
newY = GetTextY(text.status) + GetTextSize(text.status) * delta
if newY >= STATUS_Y //WINDOW_HEIGHT
newY = STATUS_Y
else
height as float
height = GetTextTotalHeight(text.status)
if height < STATUS_HEIGHT
height = STATUS_HEIGHT
endif
//~ if newY - GetTextSize(text.status) + height < STATUS_Y
if newY + height < WINDOW_HEIGHT
newY = WINDOW_HEIGHT - height
endif
endif
SetTextY(text.status, newY)
endif
EndFunction
Function TF(value as integer)
if value
ExitFunction "TRUE"
endif
EndFunction "FALSE"
Function Quit()
// Cleanup
x as integer
for x = 1 to 100
DeleteVirtualButton(x)
next
DeleteAllText()
DeleteAllImages()
DeleteAllSprites()
end
EndFunction