-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.bas
More file actions
369 lines (310 loc) · 9.39 KB
/
Utils.bas
File metadata and controls
369 lines (310 loc) · 9.39 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
B4J=true
Group=Default Group
ModulesStructureVersion=1
Type=StaticCode
Version=8.9
@EndOfDesignText@
'Static code module
Sub Process_Globals
Private mPref As Map
End Sub
Sub LanguageHasSpace(lang As String) As Boolean
Dim languagesWithoutSpaceList As List
languagesWithoutSpaceList=File.ReadList(File.DirAssets,"languagesWithoutSpace.txt")
For Each code As String In languagesWithoutSpaceList
If lang.ToLowerCase.StartsWith(code) Then
Return False
End If
Next
Return True
End Sub
Sub projectSetting As Map
If Main.currentProject.IsInitialized Then
Return Main.currentProject.settings
End If
Dim settings As Map
settings.Initialize
Return settings
End Sub
Sub getSetting(key As String,default As Object) As Object
Dim settings As Map = projectSetting
If settings.ContainsKey(key) Then
Return settings.Get(key)
End If
Return default
End Sub
'find is the text within a whole text
Sub splitByFind(text As String,find As String,textSegments As List)
Dim textLeft As String
textLeft=text
Dim currentSegment As String
Dim length As Int
length=text.Length-find.Length
For i=0 To length
Log(i)
Dim endIndex As Int
endIndex=i+find.Length
currentSegment=text.SubString2(i,endIndex)
Log(currentSegment)
If currentSegment=find Then
Log(True)
Dim textBefore As String
Log(textLeft)
textBefore=textLeft.SubString2(0,textLeft.IndexOf(find))
If textBefore<>"" Then
textSegments.Add(textBefore)
End If
textSegments.Add(find)
textLeft=textLeft.SubString2(textLeft.IndexOf(find)+find.Length,textLeft.Length)
i = i + find.Length
End If
Next
textSegments.Add(textLeft)
End Sub
Sub MeasureMultilineTextHeight (Font As Font, Width As Double, Text As String) As Double
Try
Dim jo As JavaObject = Me
Return jo.RunMethod("MeasureMultilineTextHeight", Array(Font, Text, Width))
Catch
Log(LastException)
Return Font.Size
End Try
End Sub
public Sub ReportError(message As String)
Dim reporter As ErrorReporter
reporter.Initialize
reporter.ShowAndWait("error: "&message)
End Sub
Public Sub getDataShortsFromBytes(Bytes() As Byte) As Short()
Dim BC As ByteConverter
BC.LittleEndian = True
Return BC.ShortsFromBytes(Bytes)
End Sub
Sub JoinBytes(ListOfArraysOfBytes As List) As Byte()
Dim size As Int
For Each b() As Byte In ListOfArraysOfBytes
size = size + b.Length
Next
Dim result(size) As Byte
Dim index As Int
Dim bc As ByteConverter 'ByteConverter library
For Each b() As Byte In ListOfArraysOfBytes
bc.ArrayCopy(b, 0, result, index, b.Length)
index = index + b.Length
Next
Return result
End Sub
Public Sub RemoveBOM(s As String) As String
If s.StartsWith(Chr(0xFEFF)) Then
s = s.SubString(1)
End If
Return s
End Sub
'windows, mac or linux
Public Sub DetectOS As String
Dim os As String = GetSystemProperty("os.name", "").ToLowerCase
If os.Contains("win") Then
Return "windows"
Else If os.Contains("mac") Then
Return "mac"
Else
Return "linux"
End If
End Sub
Sub GetExtension(filename As String) As String
Try
filename=filename.SubString2(filename.LastIndexOf("."),filename.Length)
Catch
Log(LastException)
End Try
Return filename
End Sub
Sub GetFilenameWithoutExtension(filename As String) As String
Try
filename=filename.SubString2(0,filename.LastIndexOf("."))
Catch
Log(LastException)
End Try
Return filename
End Sub
Public Sub ShiftTimeString(str As String,offsetMS As Int) As String
Dim ms As Long = GetMillisecondsFromTimeString(str)
ms = ms + offsetMS
Return GetTimeStringFromMilliseconds(ms)
End Sub
Public Sub GetMillisecondsFromTimeString(str As String) As Long
Dim totalMilliseconds As Long
Dim hours As Int = str.SubString2(0,2)
Dim minutes As Int = str.SubString2(3,5)
Dim seconds As Int = str.SubString2(6,8)
Dim milliseconds As Int = str.SubString2(9,12)
totalMilliseconds = hours*60*60*1000 + minutes*60*1000 + seconds*1000 + milliseconds
Return totalMilliseconds
End Sub
'00:00:15,000
Public Sub GetTimeStringFromMilliseconds(ms As Long) As String
Dim totalSeconds As Int = ms / 1000
Dim msLeft As Int = ms - totalSeconds * 1000
Dim hours As Int = Floor(totalSeconds / 3600)
Dim secondsLeft As Int = totalSeconds - hours * 3600
Dim minutes As Int = Floor(secondsLeft / 60)
Dim seconds As Int = secondsLeft - minutes*60
Dim sb As StringBuilder
sb.Initialize
sb.Append(NumberFormat(hours,2,0))
sb.Append(":")
sb.Append(NumberFormat(minutes,2,0))
sb.Append(":")
sb.Append(NumberFormat(seconds,2,0))
sb.Append(",")
sb.Append(NumberFormat(msLeft,3,0))
Return sb.ToString
End Sub
'00:00:15
Public Sub GetCompactTimeStringFromMilliseconds(ms As Long) As String
Dim totalSeconds As Int = ms / 1000
Dim hours As Int = Floor(totalSeconds / 3600)
Dim secondsLeft As Int = totalSeconds - hours * 3600
Dim minutes As Int = Floor(secondsLeft / 60)
Dim seconds As Int = secondsLeft - minutes*60
Dim sb As StringBuilder
sb.Initialize
sb.Append(NumberFormat(hours,2,0))
sb.Append(":")
sb.Append(NumberFormat(minutes,2,0))
sb.Append(":")
sb.Append(NumberFormat(seconds,2,0))
Return sb.ToString
End Sub
Public Sub GetScreenPosition(n As Node) As Map
Dim m As Map = CreateMap("x": 0, "y": 0)
Dim x = 0, y = 0 As Double
Dim joNode = n, joScene, joStage As JavaObject
'Get the scene position:
joScene = joNode.RunMethod("getScene",Null)
If joScene.IsInitialized = False Then Return m
x = x + joScene.RunMethod("getX", Null)
y = y + joScene.RunMethod("getY", Null)
'Get the stage position:
joStage = joScene.RunMethod("getWindow", Null)
If joStage.IsInitialized = False Then Return m
x = x + joStage.RunMethod("getX", Null)
y = y + joStage.RunMethod("getY", Null)
'Get the node position in the scene:
Do While True
y = y + joNode.RunMethod("getLayoutY", Null)
x = x + joNode.RunMethod("getLayoutX", Null)
joNode = joNode.RunMethod("getParent", Null)
If joNode.IsInitialized = False Then Exit
Loop
m.Put("x", x)
m.Put("y", y)
Return m
End Sub
Public Sub resetPref
Dim mPref As Map
End Sub
Sub getPref(key As String,default As Object) As Object
Dim preferencesMap As Map=getPrefMap
If preferencesMap.ContainsKey(key) Then
Return preferencesMap.Get(key)
End If
Return default
End Sub
Sub getPrefMap As Map
Dim map1 As Map
If mPref.IsInitialized Then
map1 = mPref
Else
Dim preferencesMap As Map
If File.Exists(Main.prefPath,"") Then
preferencesMap = readJsonAsMap(File.ReadString(Main.prefPath,""))
Else
preferencesMap.Initialize
End If
mPref = preferencesMap
map1 = preferencesMap
End If
Return map1
End Sub
Sub readJsonAsMap(s As String) As Map
Dim json As JSONParser
json.Initialize(s)
Return json.NextObject
End Sub
Sub getMap(key As String,parentmap As Map) As Map
Dim emptymap As Map
emptymap.Initialize
Return parentmap.GetDefault(key,emptymap)
End Sub
Sub readLanguageCode(codesfilePath As String) As Map
Dim linesList As List
linesList=File.ReadList(File.DirAssets,"langcodes.txt")
If codesfilePath<>"" Then
If File.Exists(codesfilePath,"") Then
linesList=File.ReadList(codesfilePath,"")
End If
End If
Dim headsList As List
headsList.Initialize
headsList.AddAll(Regex.Split(" ",linesList.Get(0)))
'Log(headsList)
Dim langcodes As Map
langcodes.Initialize
Dim lineNum As Int=1
For Each line As String In linesList
If lineNum=1 Then
lineNum=lineNum+1
Continue
End If
Dim colIndex As Int=0
Dim code As String=Regex.Split(" ",line)(0)
Dim codesMap As Map
codesMap.Initialize
For Each value As String In Regex.Split(" ",line)
If colIndex=0 Then
colIndex=colIndex+1
Continue
End If
If value<>"" Then
codesMap.Put(headsList.Get(colIndex),value)
End If
colIndex=colIndex+1
Next
langcodes.Put(code,codesMap)
Next
Return langcodes
End Sub
#If JAVA
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javafx.scene.text.Font;
import javafx.scene.text.TextBoundsType;
public static double MeasureMultilineTextHeight(Font f, String text, double width) throws Exception {
Method m = Class.forName("com.sun.javafx.scene.control.skin.Utils").getDeclaredMethod("computeTextHeight",
Font.class, String.class, double.class, TextBoundsType.class);
m.setAccessible(true);
return (Double)m.invoke(null, f, text, width, TextBoundsType.LOGICAL);
}
private static boolean isChinese(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {
return true;
}
return false;
}
// 完整的判断中文汉字和符号
public static boolean isChinese(String strName) {
char[] ch = strName.toCharArray();
for (int i = 0; i < ch.length; i++) {
char c = ch[i];
if (isChinese(c)) {
return true;
}
}
return false;
}
#End If