Posts tagged with “lua”
X-Fi 2: Dharma
Yep, seems like I’m really fascinated with programming my new MP3-Player, the Creative Zen X-Fi 2. Strange thing is that I don’t have any ideas for a game on this little gadget, apart from classics like Snake and Tetris. So I’m just spending my time with developing my GUI framework further which should come in handy if I really want to start scripting a real game. I really like developing frameworks, creating whole classes and defining APIs, so other people can hook into and use them.
The framework now consists of several different widgets, slowly growing in size: boxes, images, texts, sliders/progress bars, textboxes – and now even a virtual keyboard with several layouts! I just wish I could actually use the framework for something bigger … Nevertheless, you can grab the code from my GitHub Dharma repo.
Apart from the whole X-Fi 2 stuff, I want to actually update my website. It now merely consists of this blog without all the ‘About me’ and ‘Projects’-sections. Problem is that while Chyrp is an awesome little content management system, it does not have a lot of plugins by default, e.g. for a portfolio-page. Seems like I need to create one on my own, but then I need to figure out its API before I can start. Did I mention that I prefer to write my own frameworks instead of using predefined ones?
03:30 PM | 0 Comments | Tags: web design, x-fi 2, luaProgramming the Creative Zen X-Fi 2
A few days ago I bought a new portable media player, the Creative Zen X-Fi 2. I looked for a cheaper alternative than the iPod Touch and came across this one. It features 32 gb of memory, microSD-slot, touchscreen and (woo!) accelerometer. But the best thing is: It can be programmed by using Lua! After I read this, I immediately bought it, because I wanted to apply my Lua knowledge on a far more simple gadget. The WoW API provides an exhaustive GUI framework, but I prefer to write my own frameworks and the Creative Zen only has a few rectangle-, text- and pixel-functions. Really simple stuff.
So I started to create the GUI by providing a core system with classes, called Dharma – I really like Object-oriented Programming. It became more complex and now serves as the base for all my X-Fi 2 applications, handles input events and drawing of widgets.
My recent test, however, goes in another direction: 3D. I already started back in WoW with a simple wireframe 3D engine a few months ago to display coordinates in the world frame, so I just had to copy the code over and adjust it (it’s both Lua after all!). So far, the engine works smooth on the little thing, but I don’t expect solid textures and 3D games, because the CPU is not the fastest.
Anyone got an idea for a good game on a touch-based small device? I don’t know any and it drives me mad that I can’t really use my framework as a basis for something actually useful.
07:08 PM | 0 Comments | Tags: lua, x-fi 2WoW Lua: Positioning text in a frame
A small sketch I did for someone to show the principle of anchoring text frames correctly in the World of Warcraft API.
05:12 PM | 0 Comments | Tags: lua, wow interfaceLua: Small tagging system
Just came across another small trick while updating LibCargoShip-2.0 some time ago.
Small introduction:
Imagine you have a string with tags like “[name] wants to eat [food].”. Of course, you could replace both tags with “%s” and use a format instead, e.g.
cci lang=“lua”:format(“Cargor”, “a cookie”)[/cci]. While this method is recommended for most cases, there is also the possibility that your input string is “[food] is the thing [name] wants to eat.”. You notice: The arguments are swapped, so format wouldn’t work on these dynamic strings where you don’t know which tag comes where.
And now to the code!
local tagString1 = “[name] wants to eat [food]”
local tagString2 = “[food] is the thing [name] wants to eat.”
local function tagger(word)
if word == “name” then
return “Cargor”
else
return “a cookie”
end
local tagged1 = tagString1:gsub(”[(.-)]”, tagger)
— “Cargor wants to eat a cookie.”
local tagged2 = tagString2:gsub(”[(.-)]”, tagger)
— “a cookie is the thing Cargor wants to eat.”
The “gsub”-function (global substitute) finds all occurrences of a word in [] brackets and then calls the function “tagger” with the word as its argument. And this function then returns the replacement. Easy, isn’t it?
Of course, there are a lot ways to extend it:
You maybe want to store your words in a table and then just call different functions depending on them:
local tags = {
[“name”] = function() return UnitName(“player”) end,
[“food”] = function() return GetContainerItemLink(0, 1) end,
}
local function tagger(word) return tags[word] end
Or you refine the search pattern and take two or more arguments:
local function tagger(word, count)
local text = (word == “name” and “Cargor” or “cookies”)
if count then
text = count..” “..text
end
return text
end
local tagString = “[name] wants to eat [food:20]”
local tagged = tagString:gsub(”[(.):?(.)]”, tagger)
— “Cargor wants to eat 20 cookies.”
Lua: Config-table defaults
While rewriting rothar’s new WoW AddOn RingMod which creates circular statusbars to fit in a small library, I discovered a good technique to provide default values for custom config-tables.
Let’s assume we got a custom table which holds some options like:
local config = {
width = 30,
height = 20,
enableMouse = false,
}
Normally one would now check in his/her addon whether the values are actually set and provide default values, like:
frame:SetWidth(config.width or 1)
frame:SetHeight(config.height or 1)
frame:EnableMouse(config.enableMouse or true)
This of course causes a lot additional code to add into the function and it gets confusing very quick. And do you notice something? Even if someone sets config.enableMouse to ‘false’, our default entry still switches to ‘true’, because the statement “ ‘false’ or ‘true’ “ results in ‘true’ every time.
A cleaner and easier way would be to use Lua’s powerful metatables. We define a defaults-table at the beginning and then use metatables’ __index-entry to point at it. This means, every time an entry in our config-table is ‘nil’, Lua fetches the value from the defaults. Useful, eh?
— Our defaults values
local defaults = {__index = {
height = 1,
width = 1,
enableMouse = false,
}}
— Now add metatable magic to our config-table from above
setmetatable(config, defaults)
— And from this time on we can write:
frame:SetWidth(config.width)
frame:SetHeight(config.height)
frame:EnableMouse(config.enableMouse)
By the way, my library can be found under LibRingBars-1.0 @ github.
07:22 PM | 0 Comments | Tags: lua