Skip to content
Snippets Groups Projects
Commit a9769f94 authored by dg's avatar dg
Browse files

dialogs!!!

git-svn-id: http://svn.net-core.org/repos/t-engine4@40 51575b47-30f0-44d4-a5cc-537603b46e54
parent 536f545e
No related branches found
No related tags found
No related merge requests found
......@@ -7,9 +7,19 @@ module(..., package.seeall, class.make)
tiles = engine.Tiles.new(16, 16)
--- Create a calendar
-- @param definition the file to load that returns a table containing calendar months
-- @param datestring a string to format the date when requested, in the format "%s %s %s %d %d", standing for, day, month, year, hour, minute
--- Requests a simple, press any key, dialog
function _M:simplePopup(title, text)
local font = core.display.newFont("/data/font/Vera.ttf", 12)
local w, h = font:size(text)
local d = new(title, w + 8, h + 25, nil, nil, nil, font)
d:keyCommands{__DEFAULT=function() game:unregisterDialog(d) end}
d.drawDialog = function(self, s)
s:drawColorStringCentered(self.font, text, 2, 2, self.iw - 2, self.ih - 2)
end
game:registerDialog(d)
end
--- Create a Dialog
function _M:init(title, w, h, x, y, alpha, font)
self.title = title
self.w, self.h = w, h
......
......@@ -273,3 +273,4 @@ _EURO = 321 -- Some european keyboards
_UNDO = 322 -- Atari keyboard has Undo
__DEFAULT = -10000
__TEXTINPUT = -10001
......@@ -7,10 +7,14 @@ module(..., package.seeall, class.inherit(engine.Key))
function _M:init()
engine.Key.init(self)
self.commands = {}
self.on_input = false
end
function _M:receiveKey(sym, ctrl, shift, alt, meta, unicode)
if not self.commands[sym] and not self.commands[self.__DEFAULT] then return end
if not self.commands[sym] and not self.commands[self.__DEFAULT] then
if self.on_input and unicode then self.on_input(unicode) end
return
end
if self.commands[sym] and (ctrl or shift or alt or meta) and not self.commands[sym].anymod then
local mods = {}
if alt then mods[#mods+1] = "alt" end
......@@ -36,6 +40,8 @@ function _M:addCommand(sym, mods, fct, anymod)
if type(sym) == "string" then sym = self[sym] end
if not sym then return end
if sym == self.__TEXTINPUT then return self:setTextInput(mods) end
self.commands[sym] = self.commands[sym] or {}
if not fct then
self.commands[sym].plain = mods
......@@ -80,3 +86,9 @@ function _M:addCommands(t)
self:addCommands{[alias[1]] = self.commands[self[alias[2]]].plain}
end
end
--- Receieves any unbound keys as UTF8 characters (if possible)
-- @param fct the function to call for each key, get a single parameter to pass the UTF8 string
function _M:setTextInput(fct)
self.on_input = fct
end
......@@ -43,6 +43,8 @@ function _M:run()
-- Ok everything is good to go, activate the game in the engine!
self:setCurrent()
self:registerDialog(require('mod.dialogs.EnterName').new())
end
function _M:changeLevel(lev)
......
......@@ -23,3 +23,7 @@ function _M:die()
-- a tad brutal
os.exit()
end
function _M:setName(name)
self.name = name
end
require "engine.class"
require "engine.Dialog"
module(..., package.seeall, class.inherit(engine.Dialog))
function _M:init()
engine.Dialog.init(self, "Enter your name?", 300, 100)
self.name = ""
self:keyCommands{
_RETURN = function()
if self.name:len() >= 3 then
game:unregisterDialog(self)
game.player:setName(self.name)
else
engine.Dialog:simplePopup("Error", "Character name must be between 3 and 25 characters.")
end
end,
_BACKSPACE = function()
self.name = self.name:sub(1, self.name:len() - 1)
end,
__TEXTINPUT = function(c)
if self.name:len() < 25 then
self.name = self.name .. c
self.changed = true
end
end,
}
end
function _M:drawDialog(s, w, h)
s:drawColorStringCentered(self.font, "Enter your characters name:", 2, 2, self.iw - 2, self.ih - 2 - self.font:lineSkip())
s:drawColorStringCentered(self.font, self.name, 2, 2 + self.font:lineSkip(), self.iw - 2, self.ih - 2 - self.font:lineSkip())
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment