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

Quantity dialog supports up & down keys to increase/decrease

git-svn-id: http://svn.net-core.org/repos/t-engine4@1362 51575b47-30f0-44d4-a5cc-537603b46e54
parent b95d74cc
No related branches found
No related tags found
No related merge requests found
......@@ -19,94 +19,45 @@
require "engine.class"
local Module = require "engine.Module"
local Dialog = require "engine.Dialog"
local Button = require "engine.Button"
local NumberBox = require "engine.NumberBox"
local Dialog = require "engine.ui.Dialog"
local Button = require "engine.ui.Button"
local Numberbox = require "engine.ui.Numberbox"
module(..., package.seeall, class.inherit(engine.Dialog))
module(..., package.seeall, class.inherit(Dialog))
function _M:init(title, prompt, default, act)
engine.Dialog.init(self, title or "Quantity?", 320, 110)
self.prompt = prompt
self.act = act
self.qty = default
self.first = true
self:keyCommands({
_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
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=="NumberBox" 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=="NumberBox" 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=="NumberBox" then
self:okclick()
end
self.changed = true
end,
}, {
EXIT = function()
game:unregisterDialog(self)
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},
function _M:init(title, prompt, default, action)
self.action = action
Dialog.init(self, title or "Quantity", 320, 110)
local c_box = Numberbox.new{title=prompt and (prompt..": ") or "", number=default or 0, chars=10, fct=function(text) self:okclick() end}
self.c_box = c_box
local ok = require("engine.ui.Button").new{text="Accept", fct=function() self:okclick() end}
local cancel = require("engine.ui.Button").new{text="Cancel", fct=function() self:cancelclick() end}
self:loadUI{
{left=0, top=0, padding_h=10, ui=c_box},
{left=0, bottom=0, ui=ok},
{right=0, bottom=0, ui=cancel},
}
self:setFocus(c_box)
self:setupUI(true, true)
self:addControl(NumberBox.new({name="qty",title="",min=self.min, max=self.max, default=default, x=10, y=5, w=290, h=30}, self, self.font, "qty"))
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("qty")
self.key:addBinds{
EXIT = function() game:unregisterDialog(self) end,
}
end
function _M:okclick()
local results = self:databind()
self.qty = results.qty
self.act(self.qty)
game:unregisterDialog(self)
self.qty = self.c_box.number
if self.qty then
game:unregisterDialog(self)
self.action(self.qty)
else
Dialog:simplePopup("Error", "Enter a quantity.")
end
end
function _M:cancelclick()
self.key:triggerVirtual("EXIT")
end
function _M:drawDialog(s, w, h)
self:drawControls(s)
self.changed = false
end
......@@ -19,105 +19,47 @@
require "engine.class"
local Module = require "engine.Module"
local Dialog = require "engine.Dialog"
local Button = require "engine.Button"
local TextBox = require "engine.TextBox"
local Dialog = require "engine.ui.Dialog"
local Button = require "engine.ui.Button"
local Textbox = require "engine.ui.Textbox"
module(..., package.seeall, class.inherit(engine.Dialog))
module(..., package.seeall, class.inherit(Dialog))
function _M:init(title, text, min, max, action)
engine.Dialog.init(self, title, 320, 110)
self.text = text
self.action = action
self.min = min or 2
self.max = max or 25
self.action = action
self.name = ""
self:keyCommands({
_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
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
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)
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},
Dialog.init(self, title, 320, 110)
local c_box = Textbox.new{title=text..": ", text="", chars=30, max_len=max, fct=function(text) self:okclick() end}
self.c_box = c_box
local ok = require("engine.ui.Button").new{text="Accept", fct=function() self:okclick() end}
local cancel = require("engine.ui.Button").new{text="Cancel", fct=function() self:cancelclick() end}
self:loadUI{
{left=0, top=0, padding_h=10, ui=c_box},
{left=0, bottom=0, ui=ok},
{right=0, bottom=0, ui=cancel},
}
self:setFocus(c_box)
self:setupUI(true, true)
self:addControl(TextBox.new({name="name",title="",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")
self.key:addBinds{
EXIT = function() game:unregisterDialog(self) end,
}
end
function _M:okclick()
local results = self:databind()
self.name = results.name
self.name = self.c_box.text
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.")
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)
self:drawControls(s)
self.changed = false
end
......@@ -204,13 +204,13 @@ function _M:setupUI(resizex, resizey, on_resize)
local addw, addh = 0, 0
for i, ui in ipairs(self.uis) do
if ui.top then mh = math.max(mh, ui.top + ui.ui.h)
elseif ui.bottom then addh = math.max(addh, ui.bottom + ui.ui.h)
if ui.top then mh = math.max(mh, ui.top + ui.ui.h + (ui.padding_h or 0))
elseif ui.bottom then addh = math.max(addh, ui.bottom + ui.ui.h + (ui.padding_h or 0))
end
-- print("ui", ui.left, ui.right, ui.ui.w)
if ui.left then mw = math.max(mw, ui.left + ui.ui.w)
elseif ui.right then addw = math.max(addw, ui.right + ui.ui.w)
if ui.left then mw = math.max(mw, ui.left + ui.ui.w + (ui.padding_w or 0))
elseif ui.right then addw = math.max(addw, ui.right + ui.ui.w + (ui.padding_w or 0))
end
end
-- print("===", mw, addw)
......
-- TE4 - T-Engine 4
-- Copyright (C) 2009, 2010 Nicolas Casalini
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
--
-- Nicolas Casalini "DarkGod"
-- darkgod@te4.org
require "engine.class"
local Base = require "engine.ui.Base"
local Focusable = require "engine.ui.Focusable"
--- A generic UI textbox
module(..., package.seeall, class.inherit(Base, Focusable))
function _M:init(t)
self.title = assert(t.title, "no numberbox title")
self.number = t.number or 0
self.min = t.min or 0
self.max = t.max or 9999999
self.fct = assert(t.fct, "no numberbox fct")
self.chars = assert(t.chars, "no numberbox chars")
self.tmp = {}
local text = tostring(self.number)
for i = 1, #text do self.tmp[#self.tmp+1] = text:sub(i, i) end
self.cursor = #self.tmp + 1
self.scroll = 1
Base.init(self, t)
end
function _M:generate()
self.mouse:reset()
self.key:reset()
local ls, ls_w, ls_h = self:getImage("ui/textbox-left-sel.png")
local ms, ms_w, ms_h = self:getImage("ui/textbox-middle-sel.png")
local rs, rs_w, rs_h = self:getImage("ui/textbox-right-sel.png")
local l, l_w, l_h = self:getImage("ui/textbox-left.png")
local m, m_w, m_h = self:getImage("ui/textbox-middle.png")
local r, r_w, r_h = self:getImage("ui/textbox-right.png")
local c, c_w, c_h = self:getImage("ui/textbox-cursor.png")
self.h = r_h
-- Draw UI
local title_w = self.font:size(self.title:removeColorCodes())
self.w = title_w + self.chars * self.font_mono_w + ls_w + rs_w
local w, h = self.w, r_h
local fw, fh = w - title_w - ls_w - rs_w, self.font_h
self.fw, self.fh = fw, fh
self.text_x = ls_w + title_w
self.text_y = (h - fh) / 2
self.max_display = math.floor(fw / self.font_mono_w)
local ss = core.display.newSurface(w, h)
local s = core.display.newSurface(w, h)
self.text_surf = core.display.newSurface(fw, fh)
self.text_tex, self.text_tex_w, self.text_tex_h = s:glTexture()
self:updateText()
ss:merge(ls, title_w, 0)
for i = title_w + ls_w, w - rs_w do ss:merge(ms, i, 0) end
ss:merge(rs, w - rs_w, 0)
ss:drawColorStringBlended(self.font, self.title, 0, (h - fh) / 2, 255, 255, 255, true)
s:merge(l, title_w, 0)
for i = title_w + l_w, w - r_w do s:merge(m, i, 0) end
s:merge(r, w - r_w, 0)
s:drawColorStringBlended(self.font, self.title, 0, (h - fh) / 2, 255, 255, 255, true)
local cursor = core.display.newSurface(c_w, fh)
for i = 0, fh - 1 do cursor:merge(c, 0, i) end
self.cursor_tex, self.cursor_tex_w, self.cursor_tex_h = cursor:glTexture()
self.cursor_w, self.cursor_h = c_w, fh
-- Add UI controls
self.mouse:registerZone(title_w + ls_w, 0, fw, h, function(button, x, y, xrel, yrel, bx, by, event)
if event == "button" then
self.cursor = util.bound(math.floor(bx / self.font_mono_w) + self.scroll, 1, #self.tmp+1)
self:updateText()
end
end)
self.key:addBind("ACCEPT", function() self.fct(self.text) end)
self.key:addBind("MOVE_UP", function() self:updateText(1) end)
self.key:addBind("MOVE_DOWN", function() self:updateText(-1) end)
self.key:addBind("MOVE_LEFT", function() self.cursor = util.bound(self.cursor - 1, 1, #self.tmp+1) self.scroll = util.scroll(self.cursor, self.scroll, self.max_display) self:updateText() end)
self.key:addBind("MOVE_RIGHT", function() self.cursor = util.bound(self.cursor + 1, 1, #self.tmp+1) self.scroll = util.scroll(self.cursor, self.scroll, self.max_display) self:updateText() end)
self.key:addCommands{
_DELETE = function()
if self.cursor <= #self.tmp then
table.remove(self.tmp, self.cursor)
self:updateText()
end
end,
_BACKSPACE = function()
if self.cursor > 1 then
table.remove(self.tmp, self.cursor - 1)
self.cursor = self.cursor - 1
self.scroll = util.scroll(self.cursor, self.scroll, self.max_display)
self:updateText()
end
end,
_HOME = function()
self.cursor = 1
self.scroll = util.scroll(self.cursor, self.scroll, self.max_display)
self:updateText()
end,
_END = function()
self.cursor = #self.tmp + 1
self.scroll = util.scroll(self.cursor, self.scroll, self.max_display)
self:updateText()
end,
__TEXTINPUT = function(c)
if #self.tmp and (c == '0' or c == '1' or c == '2' or c == '3' or c == '4' or c == '5' or c == '6' or c == '7' or c == '8' or c == '9') then
table.insert(self.tmp, self.cursor, c)
self.cursor = self.cursor + 1
self.scroll = util.scroll(self.cursor, self.scroll, self.max_display)
self:updateText()
end
end,
}
self.tex, self.tex_w, self.tex_h = s:glTexture()
self.stex = ss:glTexture()
end
function _M:updateText(v)
local text = ""
if not v then
self.number = tonumber(table.concat(self.tmp))
for i = self.scroll, self.scroll + self.max_display - 1 do
if not self.tmp[i] then break end
text = text .. self.tmp[i]
end
else
self.number = util.bound(self.number + v, self.min, self.max)
text = tostring(self.number)
self.tmp = {}
for i = 1, #text do self.tmp[#self.tmp+1] = text:sub(i, i) end
self.cursor = #self.tmp + 1
end
self.text_surf:erase(0, 0, 0, 0)
self.text_surf:drawStringBlended(self.font_mono, text, 0, 0, 255, 255, 255, true)
self.text_surf:updateTexture(self.text_tex)
end
function _M:display(x, y)
if self.focused then
self.stex:toScreenFull(x, y, self.w, self.h, self.tex_w, self.tex_h)
else
self.tex:toScreenFull(x, y, self.w, self.h, self.tex_w, self.tex_h)
end
self.text_tex:toScreenFull(x + self.text_x, y + self.text_y, self.fw, self.fh, self.text_tex_w, self.text_tex_h)
self.cursor_tex:toScreenFull(x + self.text_x + (self.cursor-self.scroll) * self.font_mono_w, y + self.text_y, self.cursor_w, self.cursor_h, self.cursor_tex_w, self.cursor_tex_h)
end
......@@ -57,7 +57,6 @@ function _M:generate()
-- Draw UI
local title_w = self.font:size(self.title:removeColorCodes())
self.w = title_w + self.chars * self.font_mono_w + ls_w + rs_w
print("titl", self.title, title_w, self.w)
local w, h = self.w, r_h
local fw, fh = w - title_w - ls_w - rs_w, self.font_h
self.fw, self.fh = fw, fh
......
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