From a9769f9414e435e3ef282d50ee14e7814af8bf54 Mon Sep 17 00:00:00 2001
From: dg <dg@51575b47-30f0-44d4-a5cc-537603b46e54>
Date: Wed, 25 Nov 2009 14:03:00 +0000
Subject: [PATCH] dialogs!!!

git-svn-id: http://svn.net-core.org/repos/t-engine4@40 51575b47-30f0-44d4-a5cc-537603b46e54
---
 game/engine/Dialog.lua                  | 16 +++++++++---
 game/engine/Key.lua                     |  1 +
 game/engine/KeyCommand.lua              | 14 ++++++++++-
 game/modules/tome/class/Game.lua        |  2 ++
 game/modules/tome/class/Player.lua      |  4 +++
 game/modules/tome/dialogs/EnterName.lua | 33 +++++++++++++++++++++++++
 6 files changed, 66 insertions(+), 4 deletions(-)
 create mode 100644 game/modules/tome/dialogs/EnterName.lua

diff --git a/game/engine/Dialog.lua b/game/engine/Dialog.lua
index 567ab7e838..8c2a9e2955 100644
--- a/game/engine/Dialog.lua
+++ b/game/engine/Dialog.lua
@@ -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
diff --git a/game/engine/Key.lua b/game/engine/Key.lua
index 817c74fbd5..7becbdc44a 100644
--- a/game/engine/Key.lua
+++ b/game/engine/Key.lua
@@ -273,3 +273,4 @@ _EURO		= 321		-- Some european keyboards
 _UNDO		= 322		-- Atari keyboard has Undo
 
 __DEFAULT 	= -10000
+__TEXTINPUT 	= -10001
diff --git a/game/engine/KeyCommand.lua b/game/engine/KeyCommand.lua
index a6f1308ca2..54a67c41dd 100644
--- a/game/engine/KeyCommand.lua
+++ b/game/engine/KeyCommand.lua
@@ -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
diff --git a/game/modules/tome/class/Game.lua b/game/modules/tome/class/Game.lua
index 2aa4830955..f1a2b0c365 100644
--- a/game/modules/tome/class/Game.lua
+++ b/game/modules/tome/class/Game.lua
@@ -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)
diff --git a/game/modules/tome/class/Player.lua b/game/modules/tome/class/Player.lua
index 00a24c03a4..b1fc8c6246 100644
--- a/game/modules/tome/class/Player.lua
+++ b/game/modules/tome/class/Player.lua
@@ -23,3 +23,7 @@ function _M:die()
 	-- a tad brutal
 	os.exit()
 end
+
+function _M:setName(name)
+	self.name = name
+end
diff --git a/game/modules/tome/dialogs/EnterName.lua b/game/modules/tome/dialogs/EnterName.lua
new file mode 100644
index 0000000000..7b8c525344
--- /dev/null
+++ b/game/modules/tome/dialogs/EnterName.lua
@@ -0,0 +1,33 @@
+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
-- 
GitLab