diff options
author | David Walter Seikel | 2014-03-22 12:55:53 +1000 |
---|---|---|
committer | David Walter Seikel | 2014-03-22 12:55:53 +1000 |
commit | 1792ab068b78dd91c5cc678d4979f9bd0cf05a2c (patch) | |
tree | fc5d9b80b574379aede197ba1b639cd6e5eb3b1e /ClientHamr | |
parent | And correct the comment to match. (diff) | |
download | SledjHamr-1792ab068b78dd91c5cc678d4979f9bd0cf05a2c.zip SledjHamr-1792ab068b78dd91c5cc678d4979f9bd0cf05a2c.tar.gz SledjHamr-1792ab068b78dd91c5cc678d4979f9bd0cf05a2c.tar.bz2 SledjHamr-1792ab068b78dd91c5cc678d4979f9bd0cf05a2c.tar.xz |
Thing metatable fleshed out and used for initial Thing setup.
Diffstat (limited to 'ClientHamr')
-rw-r--r-- | ClientHamr/GuiLua/skang.lua | 82 |
1 files changed, 46 insertions, 36 deletions
diff --git a/ClientHamr/GuiLua/skang.lua b/ClientHamr/GuiLua/skang.lua index ee508cb..7f174a0 100644 --- a/ClientHamr/GuiLua/skang.lua +++ b/ClientHamr/GuiLua/skang.lua | |||
@@ -194,21 +194,42 @@ Thing = | |||
194 | { | 194 | { |
195 | __index = function (table, key) | 195 | __index = function (table, key) |
196 | -- This only works for keys that don't exist. By definition a value of nil means it doesn't exist. | 196 | -- This only works for keys that don't exist. By definition a value of nil means it doesn't exist. |
197 | return ThingSpace.parameters[key].default | 197 | -- TODO - Should allow looking up via alias as well somehow. |
198 | -- Very slow, but uses less memory. Would only need to be done if an alias is used, and they are mostly for shortcuts. | ||
199 | return ThingSpace.things[key].default | ||
198 | end, | 200 | end, |
199 | 201 | ||
200 | __newindex = function (table, key, value) | 202 | __newindex = function (table, key, value) |
201 | -- TODO - maybe if this is trying to set a command to a non function value, bitch and don't do it? | 203 | local thing = ThingSpace.things[key] |
202 | rawset(table, key, value) | 204 | |
203 | local command = ThingSpace.commands[key] | 205 | if 'function' == type(value) then |
204 | -- TODO - If found, and value is a function, then command.func = value | 206 | thing.default = nil |
205 | local param = ThingSpace.parameters[key] | 207 | thing.func = value |
206 | -- TODO - If found, go through it's linked things and set them to. | 208 | ThingSpace.commands[key] = thing |
207 | local widget = ThingSpace.widgets[key] | 209 | ThingSpace.parameters[key] = nil |
208 | -- TODO - If found, go through it's linked things and set them to. | 210 | setmetatable(thing, {__call = Thing._call}) |
211 | local types = '' | ||
212 | for i, v in ipairs(thing.types) do | ||
213 | if 1 ~= i then types = types .. v .. ', ' end | ||
214 | end | ||
215 | print(thing.module._NAME .. '.' .. key .. '(' .. types .. ') -> ' .. thing.help) | ||
216 | else | ||
217 | if ThingSpace.parameters[key] ~= thing then | ||
218 | ThingSpace.commands[key] = nil | ||
219 | ThingSpace.parameters[key] = thing | ||
220 | setmetatable(thing, nil) | ||
221 | end | ||
222 | print(thing.types[1] .. ' ' .. thing.module._NAME .. '.' .. thing.names[1] .. ' = ' .. value .. ' -> ' .. thing.help) | ||
223 | -- TODO - Go through it's linked things and set them to. | ||
224 | end | ||
225 | -- TODO - Should set all aliases here, then __index doesn't have to do a linear search through all of ThingSpace. | ||
226 | -- Uses more memory, and allows bypassing the Thing by setting the alias. | ||
227 | -- Which is a problem anyway, so have to deal with twiddling aliases. | ||
228 | rawset(table, thing.names[1], value) | ||
209 | end, | 229 | end, |
210 | 230 | ||
211 | __call = function (func, ...) | 231 | -- Not an actual metatable event until it gets set as the metatable above. |
232 | _call = function (func, ...) | ||
212 | return func.func(...) | 233 | return func.func(...) |
213 | end, | 234 | end, |
214 | } | 235 | } |
@@ -221,7 +242,7 @@ setmetatable(_M, {__index=Thing}) | |||
221 | moduleBegin = function (name, author, copyright, version, timestamp, skin) | 242 | moduleBegin = function (name, author, copyright, version, timestamp, skin) |
222 | local result = skangModuleBegin(name, author, copyright, version, timestamp, skin) | 243 | local result = skangModuleBegin(name, author, copyright, version, timestamp, skin) |
223 | ThingSpace.modules[name] = {module = result, name = name, } | 244 | ThingSpace.modules[name] = {module = result, name = name, } |
224 | setmetatable(result, {__index=Thing}) | 245 | setmetatable(result, Thing) |
225 | return result | 246 | return result |
226 | end | 247 | end |
227 | 248 | ||
@@ -230,6 +251,14 @@ moduleEnd = function (module) | |||
230 | setfenv(2, module.savedEnvironment) | 251 | setfenv(2, module.savedEnvironment) |
231 | end | 252 | end |
232 | 253 | ||
254 | --[[ TODO - Users might want to use two or more copies of this module. Keep that in mind. local a = require 'test', b = require 'test' might handle that though? | ||
255 | Not unless skang.thing() knows about a and b, which it wont. | ||
256 | Both a and b get the same table, not different copies of it. | ||
257 | Perhaps clone the table if it exists? Only clone the parameters, the rest can be linked back to the original. | ||
258 | Then we have to deal with widgets linking to specific clones. | ||
259 | Actually, not sure matrix-RAD solved that either. lol | ||
260 | ]] | ||
261 | |||
233 | 262 | ||
234 | --[[ TODO - It might be worth it to combine parameters and commands, since in Lua, functions are first class types like numbers and strings. | 263 | --[[ TODO - It might be worth it to combine parameters and commands, since in Lua, functions are first class types like numbers and strings. |
235 | Merging widgets might work to. B-) | 264 | Merging widgets might work to. B-) |
@@ -246,31 +275,19 @@ end | |||
246 | Default being a function makes this Thing a command. | 275 | Default being a function makes this Thing a command. |
247 | Default for a widget could be the default creation arguments - '"Press me", 1, 1, 10, 50' | 276 | Default for a widget could be the default creation arguments - '"Press me", 1, 1, 10, 50' |
248 | 277 | ||
249 | skang.newThing(_M, 'foo,s,fooAlias', 'string', 'Nope', '"button", "The foo :"' 1, 1, 10, 50', 'Default', function () print(_M.foo) end, 'Foo is a bar, not the drinking type.') | 278 | skang.thing(_M, 'foo,s,fooAlias', 'Foo is a bar, not the drinking type.', function () print('foo') end, nil, '"button", "The foo :"' 1, 1, 10, 50') |
250 | myButton = skang.widget('foo') -- Gets the default widget creation arguments. | 279 | myButton = skang.widget('foo') -- Gets the default widget creation arguments. |
251 | myButton:colour(1, 2, 3, 4) | 280 | myButton:colour(1, 2, 3, 4) |
252 | myEditor = skang.widget('foo', "edit", "Edit foo :", 5, 15, 10, 100) | 281 | myEditor = skang.widget('foo', "edit", "Edit foo :", 5, 15, 10, 100) |
253 | myEditor:colour(1, 2, 3, 4, 5, 6, 7, 8) | 282 | myEditor:colour(1, 2, 3, 4, 5, 6, 7, 8) |
254 | myButton = 'Not default' -- myEditor and _M.foo change to. | 283 | myButton = 'Not default' -- myEditor and _M.foo change to. Though now _M.foo is a command, not a parameter, so maybe don't change that. |
255 | -- Though the 'quit' Thing could have a function that does quitting, this is just an example of NOT linking to a Thing. | 284 | -- Though the 'quit' Thing could have a function that does quitting, this is just an example of NOT linking to a Thing. |
256 | -- If we had linked to this theoretical 'quit' Thing, then pushing that Quit button would invoke it's Thing function. | 285 | -- If we had linked to this theoretical 'quit' Thing, then pushing that Quit button would invoke it's Thing function. |
257 | quitter = skang.widget(nil, 'button', 'Quit', 0.5, 0.5, 0.5, 0.5) | 286 | quitter = skang.widget(nil, 'button', 'Quit', 0.5, 0.5, 0.5, 0.5) |
258 | quitter:action('quit') | 287 | quitter:action('quit') |
259 | ]] | 288 | ]] |
260 | 289 | ||
261 | 290 | -- skang.thing() stashes the default value into _M['bar'], and the details into ThingSpace.things['bar']. | |
262 | -- skang.thing stashes the default value into _M['bar'], and the details into ThingSpace.things['bar']. | ||
263 | -- TODO - If it's not required, and there's no default, then skip setting _M['bar']. | ||
264 | -- TODO - Could even use __index to skip setting it if it's not required and there is a default. | ||
265 | -- TODO - if default is a function, or a pre existing module[name] is a function, then set the Thing .func to that function. | ||
266 | -- TODO - Users might want to use two or more copies of this module. Keep that in mind. local a = require 'test', b = require 'test' might handle that though? | ||
267 | -- Not unless skang.newParam() knows about a and b, which it wont. | ||
268 | -- Both a and b get the same table, not different copies of it. | ||
269 | -- Perhaps clone the table if it exists? There is no Lua table cloner, would have to write one. Only clone the parameters, the rest can be linked back to the original. | ||
270 | -- Then we have to deal with widgets linking to specific clones. | ||
271 | -- Actually, not sure matrix-RAD solved that either. lol | ||
272 | -- TODO - This could even be done with an array of these arguments, not including the _M. | ||
273 | |||
274 | -- names - a comma seperated list of names, aliasas, and shortcuts. The first one is the official name. | 291 | -- names - a comma seperated list of names, aliasas, and shortcuts. The first one is the official name. |
275 | -- help - help text describing this Thing. | 292 | -- help - help text describing this Thing. |
276 | -- default - the default value. This could be a funcion, making this a command. | 293 | -- default - the default value. This could be a funcion, making this a command. |
@@ -303,17 +320,10 @@ thing = function (module, names, help, default, types, widget, required, acl, bo | |||
303 | end | 320 | end |
304 | 321 | ||
305 | -- Set it all up. | 322 | -- Set it all up. |
306 | module[name] = default | 323 | -- TODO - might want to merge into pre existing Thing instead of over writing like this. |
307 | ThingSpace.things[name] = {module = module, names = n, help = help, default = default, types = t, widget = widget, required = required, acl = acl, boss = boss, } | 324 | ThingSpace.things[name] = {module = module, names = n, help = help, default = default, types = t, widget = widget, required = required, acl = acl, boss = boss, } |
308 | setmetatable(ThingSpace.things[name], Thing) | 325 | -- This triggers the Thing.__newindex metamethod above. |
309 | if 'function' == t[1] then | 326 | module[name] = default |
310 | ThingSpace.things[name].func = default | ||
311 | ThingSpace.commands[name] = ThingSpace.things[name] | ||
312 | print(name .. '(' .. types .. ') -> ' .. help) | ||
313 | else | ||
314 | ThingSpace.parameters[name] = ThingSpace.things[name] | ||
315 | print(t[1] .. ' ' .. name .. ' -> ' .. help) | ||
316 | end | ||
317 | end | 327 | end |
318 | 328 | ||
319 | -- TODO - Some function stubs, for now. Fill them up later. | 329 | -- TODO - Some function stubs, for now. Fill them up later. |