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

nice dialogs, special keyhandlers for dialogs

git-svn-id: http://svn.net-core.org/repos/t-engine4@39 51575b47-30f0-44d4-a5cc-537603b46e54
parent 4c269cc3
No related branches found
No related tags found
No related merge requests found
game/data/gfx/border_1.png

246 B

game/data/gfx/border_2.png

170 B

game/data/gfx/border_3.png

227 B

game/data/gfx/border_4.png

169 B

game/data/gfx/border_6.png

168 B

game/data/gfx/border_7.png

206 B

game/data/gfx/border_8.png

171 B

game/data/gfx/border_9.png

253 B

require "engine.class"
require "engine.Tiles"
require "engine.KeyCommand"
--- Handles dialog windows
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
......@@ -12,9 +16,10 @@ function _M:init(title, w, h, x, y, alpha, font)
self.x = x or (game.w - self.w) / 2
self.y = y or (game.h - self.h) / 2
self.font = font
if not font then self.font = core.display.newFont("/data/font/VeraMono.ttf", 12) end
if not font then self.font = core.display.newFont("/data/font/Vera.ttf", 12) end
self.surface = core.display.newSurface(w, h)
self.internal_surface = core.display.newSurface(w, h - 5 - self.font:height())
self.iw, self.ih = w - 2 * 5, h - 8 - 16 - 3
self.internal_surface = core.display.newSurface(self.iw, self.ih)
self.surface:alpha(alpha or 220)
self.changed = true
end
......@@ -24,14 +29,42 @@ function _M:display()
local s = self.surface
s:erase()
s:drawColorString(self.font, self.title, 2, 0, 255,255,255)
s:merge(tiles:get(nil, 0,0,0, 0,0,0, "border_7.png"), 0, 0)
s:merge(tiles:get(nil, 0,0,0, 0,0,0, "border_9.png"), self.w - 9, 0)
s:merge(tiles:get(nil, 0,0,0, 0,0,0, "border_1.png"), 0, self.h - 9)
s:merge(tiles:get(nil, 0,0,0, 0,0,0, "border_3.png"), self.w - 9, self.h - 9)
for i = 8, self.w - 9 do
s:merge(tiles:get(nil, 0,0,0, 0,0,0, "border_8.png"), i, 0)
s:merge(tiles:get(nil, 0,0,0, 0,0,0, "border_8.png"), i, 20)
s:merge(tiles:get(nil, 0,0,0, 0,0,0, "border_8.png"), i, self.h - 3)
end
for i = 8, self.h - 9 do
s:merge(tiles:get(nil, 0,0,0, 0,0,0, "border_4.png"), 0, i)
s:merge(tiles:get(nil, 0,0,0, 0,0,0, "border_4.png"), self.w - 3, i)
end
local tw, th = self.font:size(self.title)
s:drawColorString(self.font, self.title, (self.w - tw) / 2, 4, 255,255,255)
self.internal_surface:erase()
self:drawDialog(self.internal_surface)
s:merge(self.internal_surface, 0, 5 + self.font:height())
s:merge(self.internal_surface, 5, 20 + 3)
return self.surface
end
function _M:drawDialog(s)
end
function _M:keyCommands(t)
self.old_key = game.key
game.key = engine.KeyCommand.new()
game.key:addCommands(t)
game.key:setCurrent()
end
function _M:unload()
game.key = self.old_key
game.key:setCurrent()
end
......@@ -54,4 +54,5 @@ function _M:unregisterDialog(d)
if not self.dialogs[d] then return end
table.remove(self.dialogs, self.dialogs[d])
self.dialogs[d] = nil
d:unload()
end
......@@ -271,3 +271,5 @@ _MENU = 319
_POWER = 320 -- Power Macintosh power key
_EURO = 321 -- Some european keyboards
_UNDO = 322 -- Atari keyboard has Undo
__DEFAULT = -10000
......@@ -10,8 +10,8 @@ function _M:init()
end
function _M:receiveKey(sym, ctrl, shift, alt, meta, unicode)
if not self.commands[sym] then return end
if (ctrl or shift or alt or meta) and not self.commands[sym].anymod then
if not self.commands[sym] and not self.commands[self.__DEFAULT] then 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
if ctrl then mods[#mods+1] = "ctrl" end
......@@ -21,8 +21,10 @@ function _M:receiveKey(sym, ctrl, shift, alt, meta, unicode)
if self.commands[sym][mods] then
self.commands[sym][mods](sym, ctrl, shift, alt, meta, unicode)
end
elseif self.commands[sym].plain then
elseif self.commands[sym] and self.commands[sym].plain then
self.commands[sym].plain(sym, ctrl, shift, alt, meta, unicode)
elseif self.commands[self.__DEFAULT] and self.commands[self.__DEFAULT].plain then
self.commands[self.__DEFAULT].plain(sym, ctrl, shift, alt, meta, unicode)
end
end
......
......@@ -141,6 +141,9 @@ do
local tmps = core.display.newSurface(1, 1)
getmetatable(tmps).__index.drawColorString = function(s, font, str, x, y, r, g, b)
local list = str:split("#%x%x%x%x%x%x#", true)
r = r or 255
g = g or 255
b = b or 255
for i, v in ipairs(list) do
local _, _, nr, ng, nb = v:find("^#(%x%x)(%x%x)(%x%x)#")
if nr and ng and nb then
......@@ -152,4 +155,11 @@ getmetatable(tmps).__index.drawColorString = function(s, font, str, x, y, r, g,
end
end
end
getmetatable(tmps).__index.drawColorStringCentered = function(s, font, str, dx, dy, dw, dh, r, g, b)
local w, h = font:size(str)
local x, y = dx + (dw - w) / 2, dy + (dh - h) / 2
s:drawColorString(font, str, x, y, r, g, b)
end
end
......@@ -4,9 +4,17 @@ require "engine.Dialog"
module(..., package.seeall, class.inherit(engine.Dialog))
function _M:init()
engine.Dialog.init(self, "Realy exit ToME?", 200, 150)
engine.Dialog.init(self, "Realy exit ToME?", 300, 100)
self:keyCommands{
_y = function()
os.exit()
end,
__DEFAULT = function()
game:unregisterDialog(self)
end,
}
end
function _M:drawDialog(s)
s:drawString(self.font, "Press Y to quit, any other keys to stay", 2, 2, 255,255,255)
function _M:drawDialog(s, w, h)
s:drawColorStringCentered(self.font, "Press Y to quit, any other keys to stay", 2, 2, self.iw - 2, self.ih - 2)
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