-
Notifications
You must be signed in to change notification settings - Fork 7
/
base.util.registry.imageloader.bmx
297 lines (236 loc) · 9.64 KB
/
base.util.registry.imageloader.bmx
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
Rem
====================================================================
ImageLoader extension for Registry utility
====================================================================
Allows loading of "image" in config files.
====================================================================
If not otherwise stated, the following code is available under the
following licence:
LICENCE: zlib/libpng
Copyright (C) 2002-2019 Ronny Otto, digidea.de
This software is provided 'as-is', without any express or
implied warranty. In no event will the authors be held liable
for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it
and redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you
must not claim that you wrote the original software. If you use
this software in a product, an acknowledgment in the product
documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
====================================================================
EndRem
SuperStrict
Import BRL.PNGLoader
Import BRL.JPGLoader
Import "base.util.registry.bmx"
Import "base.gfx.sprite.bmx"
'register this loader
new TRegistryImageLoader.Init()
'loader caring about "<image>"-types
Type TRegistryImageLoader extends TRegistryBaseLoader
Field _createdDefaults:int = FALSE
Global keyImageLS:TLowerString = new TLowerString.Create("image")
Global keyPixmapLS:TLowerString = new TLowerString.Create("pixmap")
Method Init:Int()
name = "Image"
resourceNames = "image|pixmap"
if not registered then Register()
End Method
Method GetConfigFromXML:TData(loader:TRegistryLoader, node:TxmlNode)
'=== LOAD IMAGE DATA ===
local data:TData = new TData
data.Add("url", TXmlHelper.FindValue(node, "url", ""))
if data.GetString("url") = ""
TLogger.Log("TRegistryImageLoader.LoadFromXML", "Node ~q<"+node.GetName()+">~q contained no or empty url field. Skipped.", LOG_WARNING)
Return NULL
Endif
'process given relative-url
data.AddString("url", loader.GetURI(data.GetString("url")))
'emit loader event for loading screen
'self.doLoadElement("image resource", _url, "loading", self.currentItemNumber)
data.Add("name", TXmlHelper.FindValue(node, "name", node.GetName()))
'use url as name if none was given
if data.GetString("name") = "" or data.GetString("name").ToUpper() = "IMAGE" then data.Add("name", data.GetString("url"))
data.AddNumber("flags", GetImageFlags(node))
'batch load some field names
local fieldNames:String[]
fieldNames :+ ["img"]
fieldNames :+ ["frames|f", "frameW|cellwidth|cw", "frameH|cellheight|ch"]
fieldNames :+ ["r", "g", "b"]
TXmlHelper.LoadValuesToData(node, data, fieldNames)
'direct load or threaded possible?
'solange threaded n bissl buggy - immer direkt laden
Local directLoadNeeded:Int = True
If data.GetInt("r",-1) >= 0 And data.GetInt("g",-1) >= 0 And data.GetInt("b",-1) >= 0 then directLoadNeeded = true
If TXmlHelper.FindChild(node, "scripts") Then directLoadNeeded = True
If TXmlHelper.FindChild(node,"colorize") Then directLoadNeeded = True
'check if there are additional scripts to process the image
'or create copies
local scriptsData:TData[] = LoadScriptsDataFromXML(node)
if len(scriptsData)>0 then data.Add("scriptsData", scriptsData)
return data
End Method
Method GetNameFromConfig:String(data:TData)
' return data.GetString(","unknown image")
return data.GetString(keyNameLS,"unknown image")
End Method
Method LoadFromConfig:object(data:TData, resourceName:string)
resourceName = resourceName.ToLower()
local pixmap:TPixmap = LoadPixmap(data.GetString("url"))
if not pixmap
TLogger.Log("TRegistryImageLoader.LoadFromConfig()", "File ~q"+data.GetString("url")+"~q is missing or corrupt.", LOG_ERROR)
return Null
endif
'colorize if needed
If data.GetInt("r",-1) >= 0 And data.GetInt("g",-1) >= 0 And data.GetInt("r",-1) >= 0
pixmap = ColorizePixmapCopy( pixmap, TColor.Create(data.GetInt("r"), data.GetInt("g"), data.GetInt("b")) )
Endif
'add to registry
local name:string = GetNameFromConfig(data)
if resourceName = "image"
local img:TImage = LoadImage(pixmap, data.GetInt("flags"))
if not img
TLogger.Log("TRegistryImageLoader.LoadFromConfig()", "File ~q"+data.GetString("url")+"~q could not be loaded as image.", LOG_ERROR)
return Null
endif
GetRegistry().Set(name, img)
'load potential new sprites from scripts
LoadScriptResults(data, img)
'indicate that the loading was successful
return img
else
GetRegistry().Set(name, pixmap)
'indicate that the loading was successful
return pixmap
endif
End Method
'creates default resources: tpixmap, timage, tsprite
Method CreateDefaultResource:Int()
if _createdDefaults then return FALSE
'create a base image
'this contains a simple checkerboard and a border indicating
'9patch marks
' - top / left : mark stretchable area
' X0X bottom / right : mark content area
' |000|
' x0x
' -
'so we end with a 30+2 x 30+x pimxap (10px pattern)
local pix:TPixmap = CreatePixmap(32,32, PF_RGBA8888)
local black:int = ARGB_COLOR(255, 0, 0, 0)
local white:int = ARGB_COLOR(255, 255, 255, 255)
local red:int = ARGB_COLOR(255, 255, 0, 0)
pix.ClearPixels(ARGB_COLOR(0, 0, 0, 0))
'marks line
for local i:int= 12 to 18
pix.WritePixel(i , 0, black)
pix.WritePixel(i , 31, black)
pix.WritePixel(0 , i, black)
pix.WritePixel(31, i, black)
Next
'pattern - 4 rects
for local i:int = 1 to 10
for local j:int = 1 to 10
pix.WritePixel(i , j, red)
pix.WritePixel(i+10 , j, white)
pix.WritePixel(i+20 , j, red)
Next
for local j:int = 11 to 20
pix.WritePixel(i , j, white)
pix.WritePixel(i+10 , j, white)
pix.WritePixel(i+20 , j, white)
Next
for local j:int = 21 to 30
pix.WritePixel(i , j, red)
pix.WritePixel(i+10 , j, white)
pix.WritePixel(i+20 , j, red)
Next
Next
local img:Timage = LoadImage(pix, DYNAMICIMAGE | FILTEREDIMAGE)
'=== ADD DEFAULTS TO REGISTRY ===
GetRegistry().SetDefault("pixmap", pix)
GetRegistry().SetDefault("image", img)
_createdDefaults = TRUE
End Method
Function GetImageFlags:Int(childNode:TxmlNode)
Local flags:Int = 0
Local flagsstring:String = TXmlHelper.FindValue(childNode, "flags", "")
If flagsstring <> ""
Local flagsarray:String[] = flagsstring.split(",")
For Local flag:String = EachIn flagsarray
flag = Upper(flag.Trim())
If flag = "MASKEDIMAGE" Then flags = flags | MASKEDIMAGE
If flag = "DYNAMICIMAGE" Then flags = flags | DYNAMICIMAGE
If flag = "FILTEREDIMAGE" Then flags = flags | FILTEREDIMAGE
Next
Else
flags = 0
EndIf
Return flags
End Function
'parses scripts data of a given node (if <scripts> exists within)
'returns an array of TData containing script variables
Function LoadScriptsDataFromXML:TData[](node:TxmlNode)
Local scripts:TxmlNode = TXmlHelper.FindChild(node, "scripts")
If not scripts then return Null
'TLogger.log("TRegistryImageLoader.ParseScripts()", "found script block.", LOG_LOADING | LOG_DEBUG, TRUE)
local datas:TData[]
For Local script:TxmlNode = EachIn TXmlHelper.GetNodeChildElements(scripts)
local data:TData = new TData
'only add to data if the fields exist in the xml
local fieldNames:String[]
fieldNames :+ ["do", "src", "dest"]
fieldNames :+ ["r", "g", "b"]
fieldNames :+ ["x", "y", "w", "h"]
fieldNames :+ ["frames"]
fieldNames :+ ["offsetTop", "offsetLeft", "offsetBottom", "offsetRight"]
TXmlHelper.LoadValuesToData(script, data, fieldNames)
'skip if invalid RGB data is provided
If data.GetInt("r",-1) < 0 then continue
If data.GetInt("g",-1) < 0 then continue
If data.GetInt("b",-1) < 0 then continue
'add script data
datas :+ [data]
Next
'if there exists valid scriptdata ... return it
'if len(datas) > 0 then
return datas
End Function
'runs all array children in "scriptsData" of the given dataset
Method LoadScriptResults:int(data:Tdata, parent:object)
local scriptsData:TData[] = TData[](data.Get("scriptsData", new TData[0]))
if not scriptsData or scriptsData.length = 0 then return False
For local scriptData:TData = eachin scriptsData
RunScriptData(scriptData, parent)
Next
return True
End Method
'running a script configured with values contained in a data-object
'objects are directly created within the function and added to
'the registry
Function RunScriptData:int(data:TData, parent:object)
local dest:String = data.GetString("dest").toLower()
local color:TColor = TColor.Create(data.GetInt("r"), data.GetInt("g"), data.GetInt("b"))
Select data.GetString("do").toUpper()
'Create a colorized copy of the given image
case "COLORIZECOPY"
'check prerequisites
If dest = "" or not TImage(parent) then return FALSE
local img:Timage = ColorizeImageCopy(TImage(parent), color)
'add copied image to registry
if img then GetRegistry().Set(dest, img)
End Select
End Function
End Type
'===== CONVENIENCE REGISTRY ACCESSORS =====
Function GetImageFromRegistry:TImage(name:object, defaultNameOrSprite:object = Null)
Return TImage( GetRegistry().Get(name, defaultNameOrSprite, TRegistryImageLoader.keyImageLS) )
End Function
Function GetPixmapFromRegistry:TPixmap(name:object, defaultNameOrSprite:object = Null)
Return TPixmap( GetRegistry().Get(name, defaultNameOrSprite, TRegistryImageLoader.keyPixmapLS) )
End Function