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

GetQuantity dialog has got an improved UI

git-svn-id: http://svn.net-core.org/repos/t-engine4@1303 51575b47-30f0-44d4-a5cc-537603b46e54
parent 4b1f3318
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" or cntrl.type=="NumberBox") then
result[cntrl.name] = cntrl.text
end
end
......@@ -285,7 +285,7 @@ end
function _M:drawControls(s)
for i, cntrl in pairs(self.controls or { }) do
cntrl:drawControl(s)
cntrl:drawControl(s)
end
end
......
-- 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"
require "engine.Tiles"
require "engine.Mouse"
require "engine.KeyBind"
require "engine.interface.ControlCursorSupport"
--- Handles numberbox input control
module(..., package.seeall, class.inherit(engine.interface.ControlCursorSupport))
tiles = engine.Tiles.new(16, 16)
function _M:init(dialogdef, owner, font, mask, fct)
--name, title, min, max, x, y, w, height
self.type = "NumberBox"
self.name = dialogdef.name
self.title = dialogdef.title
self.min = dialogdef.min or 2
self.max = dialogdef.max or 25
self.h = dialogdef.h or 30
self.font = font
self.w = dialogdef.w or 200
self.x = dialogdef.x
self.y = dialogdef.y
self.private = dialogdef.private
self.text = dialogdef.default or 0
self.owner = owner
self.btn = {
h = dialogdef.h,
mouse_over= function(button)
if self.owner.state ~= self.name then self.focused=true self.owner:focusControl(self.name) end
if button == "right" then
self.text=0
self.ownwer.changed=true
end
end
}
self.owner.mouse:registerZone(self.owner.display_x + self.x, self.owner.display_y + self.y + self.h, self.w, self.h, self.btn.mouse_over)
self:startCursor()
self:moveRight(1, true)
end
function _M:backSpace()
--[[
if (self.cursorPosition==0) then return end
local temptext = self.text:sub(1, self.cursorPosition - 1)
if self.cursorPosition < self.maximumCurosrPosition then temptext = temptext..self.text:sub(self.cursorPosition + 1, self.text:len()) end
self.text = temptext
self.maximumCurosrPosition = self.maximumCurosrPosition - 1
self.cursorPosition = self.cursorPosition - 1
]]
local b = tostring(self.text)
b = b:sub(1, b:len() - 1)
if b == '' then self.text = 0
else self.text = tonumber(b)
end
self.cursorPosition = b:len() + 1
end
function _M:textInput(c)
if not (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 return end
if self.text >= 10000000 then return end
local b = tostring(self.text)
if self.text == 0 then b = "" end
if self.first then
self.text = tonumber(c)
self.first = false
else
self.text = tonumber(b .. c)
end
self.cursorPosition = b:len() + 1
--[[
if self.text:len() < self.max then
local temp=nil
if self.cursorPosition < self.maximumCurosrPosition then temp=self.text:sub(self.cursorPosition + 1, self.text:len()) end
self.text = self.text:sub(1,self.cursorPosition) .. c
if temp then self.text=self.text..temp end
self.owner.changed = true
self:moveRight(1, true)
end
]]
end
function _M:unFocus()
self.focused = false
end
function _M:drawControl(s)
local r, g, b
local w = self.w
local h = self.h
local tw, th = self.font:size(self.title)
tw = tw + 10
local title=self.title
if self.owner.state==self.name then
title = title.."*"
end
r, g, b = s:drawColorStringBlended(self.font, title, self.x, self.y + ((h - th) / 2), r, g, b)
s:merge(tiles:get(nil, 0,0,0, 0,0,0, "border_7"..(sel and "_sel" or "")..".png"), self.x + tw, self.y)
s:merge(tiles:get(nil, 0,0,0, 0,0,0, "border_9"..(sel and "_sel" or "")..".png"), w + self.x - 8, self.y)
s:merge(tiles:get(nil, 0,0,0, 0,0,0, "border_1"..(sel and "_sel" or "")..".png"), self.x + tw, self.y + h - 8)
s:merge(tiles:get(nil, 0,0,0, 0,0,0, "border_3"..(sel and "_sel" or "")..".png"), self.x + w - 8, self.y + h - 8)
for i = 8, w - tw - 9 do
s:merge(tiles:get(nil, 0,0,0, 0,0,0, "border_8.png"), self.x + tw + i, self.y)
s:merge(tiles:get(nil, 0,0,0, 0,0,0, "border_8.png"), self.x + tw + i, self.y + h - 3)
end
for i = 8, h - 9 do
s:merge(tiles:get(nil, 0,0,0, 0,0,0, "border_4.png"), self.x + tw, self.y + i)
s:merge(tiles:get(nil, 0,0,0, 0,0,0, "border_4.png"), self.x + w - 3, self.y + i)
end
local text = tostring(self.text)
if text=="" then text=self.mask or "" end
if self.private then text = text:gsub('.', '*') end
local sw, sh = self.font:size(text)
local baseX = self.x + tw + 10
s:drawColorStringBlended(self.font, text, baseX, self.y + h - sh - 8)
self:drawCursor(s, baseX, text)
end
......@@ -76,8 +76,7 @@ function _M:interact(who)
local d; d = ShowStore.new("Store: "..self.name, store, inven, nil, nil, function(what, o, item)
if what == "buy" then
if o:getNumber() > 1 then
local q = GetQuantity.new(nil, nil, function(qty) self:doBuy(who, o, item, qty, d) end)
q.qty = o:getNumber()
local q = GetQuantity.new(nil, nil, o:getNumber(), function(qty) self:doBuy(who, o, item, qty, d) end)
game:registerDialog(q)
else
self:doBuy(who, o, item, 1, d)
......@@ -85,8 +84,7 @@ function _M:interact(who)
else
if o:getNumber() > 1 then
local q
q = GetQuantity.new(nil, nil, function(qty) self:doSell(who, o, item, qty, d) end)
q.qty = o:getNumber()
q = GetQuantity.new(nil, nil, o:getNumber(), function(qty) self:doSell(who, o, item, qty, d) end)
game:registerDialog(q)
else
self:doSell(who, o, item, 1, d)
......
......@@ -18,53 +18,95 @@
-- 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 NumberBox = require "engine.NumberBox"
module(..., package.seeall, class.inherit(engine.Dialog))
function _M:init(title, prompt, act)
engine.Dialog.init(self, title or "Quantity?", 300, 100)
function _M:init(title, prompt, default, act)
engine.Dialog.init(self, title or "Quantity?", 320, 110)
self.prompt = prompt
self.act = act
self.qty = 0
self.qty = default
self.first = true
self:keyCommands{
_ESCAPE = function()
game:unregisterDialog(self)
self:keyCommands({
_TAB = function()
self.state = self:changeFocus(true)
self.changed = true
end,
_RETURN = function()
game:unregisterDialog(self)
act(self.qty)
_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()
local b = tostring(self.qty)
b = b:sub(1, b:len() - 1)
if b == '' then self.qty = 0
else self.qty = tonumber(b)
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 not (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 return end
if self.qty >= 10000000 then return end
local b = tostring(self.qty)
if self.qty == 0 then b = "" end
if self.first then
self.qty = tonumber(c)
self.first = false
else
self.qty = tonumber(b .. 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},
}
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")
end
function _M:okclick()
local results = self:databind()
self.qty = results.qty
self.act(self.qty)
game:unregisterDialog(self)
end
function _M:cancelclick()
self.key:triggerVirtual("EXIT")
end
function _M:drawDialog(s, w, h)
s:drawColorStringBlendedCentered(self.font, self.prompt or "Quantity:", 2, 2, self.iw - 2, self.ih - 2 - self.font:lineSkip())
s:drawColorStringBlendedCentered(self.font, tostring(self.qty), 2, 2 + self.font:lineSkip(), self.iw - 2, self.ih - 2 - self.font:lineSkip())
self:drawControls(s)
self.changed = false
end
......@@ -90,12 +90,13 @@ function _M:init(title, text, min, max, action)
}, {
EXIT = function()
game:unregisterDialog(self)
game:bindKeysToStep()
end
})
self:mouseZones{}
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: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(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")
......
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