This repository has been archived by the owner on Jan 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
flxu.monkey
530 lines (411 loc) · 12 KB
/
flxu.monkey
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
Strict
Import mojo
Import reflection
Import flxextern
Import flxpoint
Import flxg
Import system.flximagedata
Alias MonkeyAbs = monkey.math.Abs
Alias MonkeyFloor = monkey.math.Floor
Alias MonkeyCeil = monkey.math.Ceil
Alias MonkeyMin = monkey.math.Min
Alias MonkeyMax = monkey.math.Max
Alias MonkeyGetClass = reflection.GetClass
#If TARGET <> "psm" And TARGET <> "xna"
Alias FlxOpenURL = mojo.app.OpenUrl
#End
Class FlxU
Private
Global _objClass:ClassInfo
Public
Function OpenURL:Void(url:String)
FlxOpenURL(url)
End Function
Function Abs:Float(value:Float)
Return MonkeyAbs(value)
End Function
Function Floor:Float(value:Float)
Return MonkeyFloor(value)
End Function
Function Ceil:Float(value:Float)
Return MonkeyCeil(value)
End Function
Function Round:Float(value:Float)
If (value > 0) Then
Return Int(value + .5)
Else
Return Int(value - .5)
End If
End Function
Function Min:Float(number1:Float, number2:Float)
Return MonkeyMin(number1, number2)
End Function
Function Max:Float(number1:Float, number2:Float)
Return MonkeyMax(number1, number2)
End Function
Function Bound:Float(value:Float, min:Float, max:Float)
Return Clamp(value, min, max)
End Function
Function Srand:Float(seed:Float)
Return Float(seed Shr 8 & $ffffff)/$1000000
End Function
#Rem
Function Shuffle:Object[](objects:Object[], howManyTimes:Int)
Shuffle is not currently supported in Monkey. Use FlxArray.Shuffle method
End Function
#End
#Rem
Function GetRandom:FlxBasic(objects:FlxBasic[], startIndex:Int = 0, length:Int = 0)
GetRandom is not currently supported in Monkey. Use FlxArray.GetRandom method
End Function
#End
Function GetTicks:Int()
Return Millisecs()
End Function
Function FormatTicks:String(startTicks:Int, endTicks:Int)
Return ((endTicks - startTicks) / 1000) + "s"
End Function
Function MakeColor:Int(red:Int, green:Int, blue:Int, alpha:Float = 1)
If (alpha <= 1) alpha *= 255
Return (alpha & $FF) Shl 24 | (red & $FF) Shl 16 | (green & $FF) Shl 8 | (blue & $FF)
End Function
Function MakeColorFromHSB:Int(hue:Float, saturation:Float, brightness:Float, alpha:Float = 1)
Local red:Float
Local green:Float
Local blue:Float
If (saturation = 0)
red = brightness
green = brightness
blue = brightness
Else
If (hue = 360) hue = 0
Local slice:Int = hue / 60
Local hf:Float = hue / 60 - slice
Local aa:Float = brightness * (1 - saturation)
Local bb:Float = brightness * (1 - saturation * hf)
Local cc:Float = brightness * (1 - saturation * (1.0 - hf))
Select (slice)
Case 0
red = brightness
green = cc
blue = aa
Case 1
red = bb
green = brightness
blue = aa
Case 2
red = aa
green = brightness
blue = cc
Case 3
red = aa
green = bb
blue = brightness
Case 4
red = cc
green = aa
blue = brightness
Case 5
red = brightness
green = aa
blue = bb
Default
red = 0
green = 0
blue = 0
End Select
End If
If (alpha <= 1) alpha *= 255
Return (alpha & $FF) Shl 24 | int(red * 255) Shl 16 | int(green * 255) Shl 8 | int(blue * 255)
End Function
Function GetRGBA:Float[](color:Int, resluts:Float[] = [])
If (resluts.Length() <> 4) resluts = resluts.Resize(4)
resluts[0] = (color Shr 16) & $FF
resluts[1] = (color Shr 8) & $FF
resluts[2] = color & $FF
resluts[3] = Float((color Shr 24) & $FF) / 255
Return resluts
End Function
Function GetHSB:Float[](color:Int, resluts:Float[] = [])
If (resluts.Length() <> 4) resluts = resluts.Resize(4)
Local red:Float = Float((color Shr 16) & $FF) / 255
Local green:Float = Float((color Shr 8) & $FF) / 255
Local blue:Float = Float(color & $FF) / 255
Local m:Float = MonkeyMax(red, green)
Local dmax:Float = MonkeyMax(m, blue)
m = MonkeyMin(red, green)
Local dmin:Float = MonkeyMin(m, blue)
Local range:Float = dmax - dmin
resluts[2] = dmax
resluts[1] = 0
resluts[0] = 0
If (dmax <> 0) resluts[1] = range / dmax
If (resluts[1] <> 0) Then
If (red = dmax) Then
resluts[0] = (green - blue) / range
ElseIf (green = dmax) Then
resluts[0] = 2 + (blue - red) / range
ElseIf (blue = dmax) Then
resluts[0] = 4 + (red - green) / range
End If
resluts[0] *= 60
If (resluts[0] < 0) resluts[0] += 360
End If
resluts[3] = Float((color Shr 24) & $FF) / 255
Return resluts
End Function
Function FormatTime:String(seconds:Float, showMS:Bool = False)
Local timeString:StringStack = New StringStack()
timeString.Push(Int(seconds / 60) + ":")
Local timeStringHepler:Int = Int(seconds) Mod 60
If (timeStringHepler < 10) Then
timeString.Push("0")
End If
timeString.Push(timeStringHepler)
If (showMS) Then
timeString.Push(".")
timeStringHepler = (seconds - Int(seconds)) * 100
If (timeStringHepler < 10) Then
timeString.Push("0")
End If
timeString.Push(timeStringHepler)
End If
Return timeString.Join("")
End Function
Function FormatMoney:String(amount:Float, showDecimal:Bool = True, englishStyle:Bool = True)
Local helper:Int
Local sgn:Int = Sgn(amount)
Local showSgn:Bool = False
amount = Abs(amount)
Local intAmount:Int = amount
Local comma:String = ""
Local result:String = ""
Local zeroes:String = ""
If (intAmount = 0) result = "0"
While (intAmount > 0)
If (result.Length() > 0 And comma.Length() <= 0) Then
If (englishStyle) Then
comma = ","
Else
comma = "."
End If
End If
helper = intAmount - Int(intAmount / 1000) * 1000
intAmount /= 1000
zeroes = ""
If (intAmount > 0) Then
If (helper < 100) Then
zeroes = "0"
End If
If (helper < 10) Then
zeroes = "00"
End If
End If
result = zeroes + helper + comma + result
showSgn = True
Wend
If (showDecimal) Then
Local floatPart:String
intAmount = Round(amount * 100) - (Int(amount) * 100)
If (intAmount <> 0) showSgn = True
If (englishStyle) Then
floatPart = "."
Else
floatPart = ","
End If
If (intAmount < 10) Then
floatPart += "0"
End If
floatPart += intAmount
If (floatPart.Length() > 3) floatPart = floatPart[ .. 3]
result += floatPart
End If
If (sgn < 0 And showSgn) Then
Return "-" + result
End If
Return result
End Function
Function FormatDate:String(format:String, date:Int[])
format = Format(format, "y", date[0])
format = Format(format, "M", date[1])
format = Format(format, "d", date[2])
format = Format(format, "h", date[3])
format = Format(format, "m", date[4])
format = Format(format, "s", date[5])
Return Format(format, "S", date[6])
End Function
Function FormatDate:String(format:String = "dd/MM/yyyy hh:mm:ss.SSS")
Return FormatDate(format, GetDate())
End Function
Function Format:String(text:String, placeHolder:String, value:String)
Local index:Int = text.Find(placeHolder)
Local offset:Int = index
Local char:Int
Local length:Int = text.Length()
While (index >= 0)
char = text[index]
offset += 1
While (offset < length And text[offset] = char)
offset += 1
Wend
If (value.Length() < offset - index) Then
Local l:Int = value.Length()
While (l < offset - index)
value = "0" + value
l += 1
Wend
ElseIf(value.Length() > offset - index) Then
value = value[ (value.Length() - (offset - index)) ..]
End If
text = text.Replace(text[index .. offset], value)
index = text.Find(placeHolder)
offset = index
Wend
Return text
End Function
Function GetClassName:String(obj:Object, simple:Bool = False)
If (simple) Then
Local name:String = MonkeyGetClass(obj).Name
Return name[name.FindLast(".")+1..]
End If
Return MonkeyGetClass(obj).Name
End Function
Function CompareClassNames:Bool(object1:Object, object2:Object)
Return (MonkeyGetClass(object1).Name = MonkeyGetClass(object2).Name)
End Function
Function GetClass:ClassInfo(name:String)
Return MonkeyGetClass(name)
End Function
Function GetObjectClass:ClassInfo()
If (_objClass <> Null)
Return _objClass
End If
_objClass = MonkeyGetClass("monkey.lang.Object")
Return _objClass
End Function
Function ComputeVelocity:Float(velocity:Float, acceleration:Float = 0, drag:Float = 0, max:Float = 10000)
If (acceleration <> 0) Then
velocity += acceleration * FlxG.Elapsed
ElseIf (drag <> 0) Then
drag *= FlxG.Elapsed
If (velocity - drag > 0) Then
velocity -= drag
ElseIf (velocity + drag < 0) Then
velocity += drag
Else
velocity = 0
End If
End If
If (velocity <> 0 And max <> 10000) Then
If (velocity > max) Then
velocity = max
ElseIf (velocity < -max) Then
velocity = -max
End If
End If
Return velocity
End Function
Function RotatePoint:FlxPoint(x:Float, y:Float, pivotX:Float, pivotY:Float, angle:Float, point:FlxPoint = Null)
Local sin:Float = 0
Local cos:Float = 0
Local radians:Float = angle * -0.017453293
While (radians < -PI)
radians += TWOPI
Wend
While (radians > PI)
radians = radians - TWOPI
Wend
If (radians < 0) Then
sin = 1.27323954 * radians + .405284735 * radians * radians
If (sin < 0) Then
sin = .225 * (sin * -sin - sin) + sin
Else
sin = .225 * (sin * sin - sin) + sin
End If
Else
sin = 1.27323954 * radians - 0.405284735 * radians * radians
If (sin < 0) Then
sin = .225 * (sin * -sin - sin) + sin
Else
sin = .225 * (sin * sin - sin) + sin
End If
End If
radians += HALFPI
If (radians > PI) radians -= TWOPI
If (radians < 0) Then
cos = 1.27323954 * radians + 0.405284735 * radians * radians
If (cos < 0) Then
cos = .225 * (cos * -cos - cos) + cos
Else
cos = .225 * (cos * cos - cos) + cos
End If
Else
cos = 1.27323954 * radians - 0.405284735 * radians * radians
If (cos < 0) Then
cos = .225 * (cos * -cos - cos) + cos
Else
cos = .225 * (cos * cos - cos) + cos
End If
End If
Local dx:Float = x - pivotX
Local dy:Float = pivotY + y
If (point = Null) point = New FlxPoint()
point.x = pivotX + cos * dx - sin * dy
point.y = pivotY - sin * dx - cos * dy
Return point
End Function
Function GetAngle:Float(point1:FlxPoint, point2:FlxPoint)
Local x:Float = point2.x - point1.x
Local y:Float = point2.y - point1.y
If (x = 0 And y = 0) Return 0
Local c1:Float = PI * .25
Local c2:Float = 3 * c1
Local ay:Float = MonkeyAbs(y)
Local angle:Float = 0
If (x >= 0) Then
angle = c1 - c1 * ((x - ay) / (x + ay))
Else
angle = c2 - c1 * ((x + ay) / (ay - x))
End If
If (y < 0) Then
angle = -angle * 57.2957796
Else
angle = angle * 57.2957796
End If
If (angle > 90) Then
angle = angle - 270
Else
angle += 90
End If
Return angle
End Function
Function GetDistance:Float(point1:FlxPoint, point2:FlxPoint)
Local dx:Float = point1.x - point2.x
Local dy:Float = point1.y - point2.y
Return Sqrt(dx * dx + dy * dy)
End Function
Function GetDistance:Float(x1:Float, y1:Float, x2:Float, y2:Float)
Local dx:Float = x1 - x2
Local dy:Float = y1 - y2
Return Sqrt(dx * dx + dy * dy)
End Function
Function SetImageMask:Image(image:Image, maskColor:Int)
Local bitmapData:FlxImageData = FlxImageData.FromImage(image, True)
bitmapData.ColorReplace(maskColor, 0)
Local result:Image = bitmapData.Image
bitmapData.Destroy()
Return result
End Function
Function SetImagePadding:Image(image:Image, paddings:Int = Image.XYPadding)
Local paddingX:Int, paddingY:Int
If (paddings & Image.XPadding) paddingX = 1
If (paddings & Image.YPadding) paddingY = 1
Return FlxU.SetImagePadding(image, paddingX, paddingY)
End Function
Function SetImagePadding:Image(image:Image, paddingX:Int, paddingY:Int)
Local bitmapData:FlxImageData = FlxImageData.FromImage(image, False, paddingX, paddingY)
Local result:Image = bitmapData.Image
bitmapData.Destroy()
Return result
End Function
End Class