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

GetText dialog UI improved

git-svn-id: http://svn.net-core.org/repos/t-engine4@1302 51575b47-30f0-44d4-a5cc-537603b46e54
parent c5969966
No related branches found
No related tags found
No related merge requests found
......@@ -275,7 +275,7 @@ end
function _M:databind()
local result = { }
for i, cntrl in pairs(self.controls or { }) do
if cntrl.type and cntrl.type=="TextBox" then
if cntrl.type and cntrl.type=="TextBox" then
result[cntrl.name] = cntrl.text
end
end
......
......@@ -23,8 +23,7 @@ require "engine.Mouse"
require "engine.KeyBind"
require "engine.interface.ControlCursorSupport"
--- Handles textbox input control
module(..., package.seeall, class.inherit(
engine.interface.ControlCursorSupport))
module(..., package.seeall, class.inherit(engine.interface.ControlCursorSupport))
tiles = engine.Tiles.new(16, 16)
......
......@@ -18,46 +18,105 @@
-- darkgod@te4.org
require "engine.class"
require "engine.Dialog"
local Module = require "engine.Module"
local Dialog = require "engine.Dialog"
local Button = require "engine.Button"
local TextBox = require "engine.TextBox"
module(..., package.seeall, class.inherit(engine.Dialog))
function _M:init(title, text, min, max, action)
engine.Dialog.init(self, title, 300, 100)
engine.Dialog.init(self, title, 320, 110)
self.text = text
self.min = min or 2
self.max = max or 25
self.action = action
self.name = ""
self:keyCommands({
_RETURN = function()
if self.name:len() >= self.min then
game:unregisterDialog(self)
action(self.name)
_DELETE = function()
if self.controls[self.state] and self.controls[self.state].delete then
self.controls[self.state]:delete()
end
self.changed = true
end,
_TAB = function()
self.state = self:changeFocus(true)
self.changed = true
end,
_DOWN = function()
self.state = self:changeFocus(true)
self.changed = true
end,
_UP = function()
self.state = self:changeFocus(false)
self.changed = true
end,
_RIGHT = function()
if self.state ~= "" and self.controls[self.state] and self.controls[self.state].moveRight then
self.controls[self.state]:moveRight(1)
else
self.state = self:changeFocus(true)
end
self.changed = true
end,
_LEFT = function()
if self.state ~= "" and self.controls[self.state] and self.controls[self.state].moveLeft then
self.controls[self.state]:moveLeft(1)
else
engine.Dialog:simplePopup("Error", "Must be between 2 and 25 characters.")
self.state = self:changeFocus(false)
end
self.changed = true
end,
_BACKSPACE = function()
self.name = self.name:sub(1, self.name:len() - 1)
if self.state ~= "" and self.controls[self.state] and self.controls[self.state].type=="TextBox" then
self.controls[self.state]:backSpace()
end
self.changed = true
end,
__TEXTINPUT = function(c)
if self.name:len() < self.max then
self.name = self.name .. c
self.changed = true
if self.state ~= "" and self.controls[self.state] and self.controls[self.state].type=="TextBox" then
self.controls[self.state]:textInput(c)
end
self.changed = true
end,
},{
_RETURN = function()
if self.state ~= "" and self.controls[self.state] and self.controls[self.state].type=="Button" then
self.controls[self.state]:fct()
elseif self.state ~= "" and self.controls[self.state] and self.controls[self.state].type=="TextBox" then
self:okclick()
end
self.changed = true
end,
}, {
EXIT = function()
game:unregisterDialog(self)
action(nil)
game:bindKeysToStep()
end
})
self:mouseZones{
{ x=0, y=0, w=game.w, h=game.h, mode={button=true}, norestrict=true, fct=function(button) if button == "left" then game:unregisterDialog(self) end end},
}
self:mouseZones{}
self:addControl(TextBox.new({name="name",title="Name:",min=self.min, max=self.max, x=10, y=5, w=290, h=30}, self, self.font, "name"))
self:addControl(Button.new("ok", "Ok", 50, 45, 50, 30, self, self.font, function() self:okclick() end))
self:addControl(Button.new("cancel", "Cancel", 220, 45, 50, 30, self, self.font, function() self:cancelclick() end))
self:focusControl("name")
end
function _M:okclick()
local results = self:databind()
self.name = results.name
if self.name:len() >= self.min then
game:unregisterDialog(self)
self.action(self.name)
else
engine.Dialog:simplePopup("Error", "Must be between 2 and 25 characters.")
end
end
function _M:cancelclick()
self.key:triggerVirtual("EXIT")
end
function _M:drawDialog(s, w, h)
s:drawColorStringCentered(self.font, self.text..":", 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())
self:drawControls(s)
self.changed = false
end
......@@ -28,13 +28,13 @@ tiles = engine.Tiles.new(16, 16)
-- This should work for anything that has a surface and x,y,w,h,font properties.
module(..., package.seeall, class.make)
function _M:startCursor()
function _M:startCursor()
self.cursorPosition = 0
self.maximumCurosrPosition = 0
self.focused = false
end
function _M:moveRight(x, add)
function _M:moveRight(x, add)
if add and self.cursorPosition + x > self.maximumCurosrPosition then self.maximumCurosrPosition = self.cursorPosition + x end
if self.cursorPosition + x <= self.maximumCurosrPosition then
self.cursorPosition = self.cursorPosition + x
......@@ -49,8 +49,9 @@ end
-- @param s surface to draw on
function _M:drawCursor(s, baseX, text)
local sw, sh = self.font:size(text:sub(1, self.cursorPosition))
local t = os.time() % 2
local sw, sh = self.font:size(text:sub(1, self.cursorPosition))
-- local t = os.time() % 2
local t = 0
if t < 1 and self.focused then
s:merge(tiles:get(nil, 0,0,0, 0,0,0, "cursor.png"), sw + baseX, self.y + self.h - sh - 2)
s:merge(tiles:get(nil, 0,0,0, 0,0,0, "cursor.png"), sw + baseX, self.y + self.h - sh - 10)
......
......@@ -34,15 +34,19 @@ function _M:init(runmod)
if self.controls[self.state] and self.controls[self.state].delete then
self.controls[self.state]:delete()
end
self.changed = true
end,
_TAB = function()
self.state = self:changeFocus(true)
self.changed = true
end,
_DOWN = function()
self.state = self:changeFocus(true)
self.changed = true
end,
_UP = function()
self.state = self:changeFocus(false)
self.changed = true
end,
_RIGHT = function()
if self.state ~= "" and self.controls[self.state] and self.controls[self.state].moveRight then
......@@ -50,6 +54,7 @@ function _M:init(runmod)
else
self.state = self:changeFocus(true)
end
self.changed = true
end,
_LEFT = function()
if self.state ~= "" and self.controls[self.state] and self.controls[self.state].moveLeft then
......@@ -57,16 +62,19 @@ function _M:init(runmod)
else
self.state = self:changeFocus(false)
end
self.changed = true
end,
_BACKSPACE = function()
if self.state ~= "" and self.controls[self.state] and self.controls[self.state].type=="TextBox" then
self.controls[self.state]:backSpace()
end
self.changed = true
end,
__TEXTINPUT = function(c)
if self.state ~= "" and self.controls[self.state] and self.controls[self.state].type=="TextBox" then
self.controls[self.state]:textInput(c)
end
self.changed = true
end,
_RETURN = function()
if self.state ~= "" and self.controls[self.state] and self.controls[self.state].type=="Button" then
......@@ -74,6 +82,7 @@ function _M:init(runmod)
elseif self.state ~= "" and self.controls[self.state] and self.controls[self.state].type=="TextBox" then
self:okclick()
end
self.changed = true
end,
}, {
EXIT = function()
......@@ -105,7 +114,6 @@ function _M:cancelclick()
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())
self:drawControls(s)
self.changed = false
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