Skip to content
Snippets Groups Projects
Commit 2d11cb49 authored by dg's avatar dg
Browse files

textbox / textzone

git-svn-id: http://svn.net-core.org/repos/t-engine4@1354 51575b47-30f0-44d4-a5cc-537603b46e54
parent ab2aa4ee
No related branches found
No related tags found
No related merge requests found
Showing
with 283 additions and 40 deletions
game/engines/default/data/gfx/ui/button-left-sel.png

408 B | W: | H:

game/engines/default/data/gfx/ui/button-left-sel.png

467 B | W: | H:

game/engines/default/data/gfx/ui/button-left-sel.png
game/engines/default/data/gfx/ui/button-left-sel.png
game/engines/default/data/gfx/ui/button-left-sel.png
game/engines/default/data/gfx/ui/button-left-sel.png
  • 2-up
  • Swipe
  • Onion skin
game/engines/default/data/gfx/ui/button-middle-sel.png

238 B | W: | H:

game/engines/default/data/gfx/ui/button-middle-sel.png

244 B | W: | H:

game/engines/default/data/gfx/ui/button-middle-sel.png
game/engines/default/data/gfx/ui/button-middle-sel.png
game/engines/default/data/gfx/ui/button-middle-sel.png
game/engines/default/data/gfx/ui/button-middle-sel.png
  • 2-up
  • Swipe
  • Onion skin
game/engines/default/data/gfx/ui/button-right-sel.png

427 B | W: | H:

game/engines/default/data/gfx/ui/button-right-sel.png

484 B | W: | H:

game/engines/default/data/gfx/ui/button-right-sel.png
game/engines/default/data/gfx/ui/button-right-sel.png
game/engines/default/data/gfx/ui/button-right-sel.png
game/engines/default/data/gfx/ui/button-right-sel.png
  • 2-up
  • Swipe
  • Onion skin
game/engines/default/data/gfx/ui/scrollbar-sel.png

295 B

game/engines/default/data/gfx/ui/scrollbar.png

213 B

game/engines/default/data/gfx/ui/selection-left-column.png

363 B | W: | H:

game/engines/default/data/gfx/ui/selection-left-column.png

339 B | W: | H:

game/engines/default/data/gfx/ui/selection-left-column.png
game/engines/default/data/gfx/ui/selection-left-column.png
game/engines/default/data/gfx/ui/selection-left-column.png
game/engines/default/data/gfx/ui/selection-left-column.png
  • 2-up
  • Swipe
  • Onion skin
game/engines/default/data/gfx/ui/selection-middle-column.png

248 B | W: | H:

game/engines/default/data/gfx/ui/selection-middle-column.png

228 B | W: | H:

game/engines/default/data/gfx/ui/selection-middle-column.png
game/engines/default/data/gfx/ui/selection-middle-column.png
game/engines/default/data/gfx/ui/selection-middle-column.png
game/engines/default/data/gfx/ui/selection-middle-column.png
  • 2-up
  • Swipe
  • Onion skin
game/engines/default/data/gfx/ui/selection-right-column.png

379 B | W: | H:

game/engines/default/data/gfx/ui/selection-right-column.png

358 B | W: | H:

game/engines/default/data/gfx/ui/selection-right-column.png
game/engines/default/data/gfx/ui/selection-right-column.png
game/engines/default/data/gfx/ui/selection-right-column.png
game/engines/default/data/gfx/ui/selection-right-column.png
  • 2-up
  • Swipe
  • Onion skin
game/engines/default/data/gfx/ui/textbox-cursor.png

172 B

game/engines/default/data/gfx/ui/textbox-left-sel.png

416 B

game/engines/default/data/gfx/ui/textbox-left.png

314 B

game/engines/default/data/gfx/ui/textbox-middle-sel.png

222 B

game/engines/default/data/gfx/ui/textbox-middle.png

207 B

game/engines/default/data/gfx/ui/textbox-right-sel.png

445 B

game/engines/default/data/gfx/ui/textbox-right.png

330 B

......@@ -30,6 +30,9 @@ local cache = {}
-- Default font
_M.font = core.display.newFont("/data/font/Vera.ttf", 12)
_M.font_h = _M.font:lineSkip()
_M.font_mono = core.display.newFont("/data/font/VeraMono.ttf", 12)
_M.font_mono_w = _M.font_mono:size(" ")
_M.font_mono_h = _M.font_mono:lineSkip()
function _M:init(t, no_gen)
self.mouse = Mouse.new()
......
......@@ -88,7 +88,7 @@ function _M:generate()
self.mouse:registerZone(0, 0, gamew, gameh, function() self.key:triggerVirtual("EXIT") end)
self.mouse:registerZone(self.display_x, self.display_y, self.w, self.h, function(...) self:mouseEvent(...) end)
self.key.receiveKey = function(_, ...) self:keyEvent(...) end
self.key:addCommand("__TAB", function(...) self.key:triggerVirtual("MOVE_DOWN") end)
self.key:addCommand("_TAB", function(...) self.key:triggerVirtual("MOVE_DOWN") end)
self.key:addBinds{
MOVE_UP = function() self:setFocus(util.boundWrap(self.focus_ui_id - 1, 1, #self.uis)) end,
MOVE_DOWN = function() self:setFocus(util.boundWrap(self.focus_ui_id + 1, 1, #self.uis)) end,
......
......@@ -78,7 +78,7 @@ function _M:generate()
-- Add UI controls
self.mouse:registerZone(0, 0, self.w, self.h, function(button, x, y, xrel, yrel, bx, by, event)
self.sel = util.bound(self.scroll + math.floor(by / self.fh), 1, self.max)
if button ~= "wheelup" and button ~= "wheeldown" then self.sel = util.bound(self.scroll + math.floor(by / self.fh), 1, self.max) end
if button == "left" and event == "button" then self:onUse()
elseif button == "wheelup" and event == "button" then self.key:triggerVirtual("MOVE_UP")
elseif button == "wheeldown" and event == "button" then self.key:triggerVirtual("MOVE_DOWN")
......
......@@ -29,8 +29,15 @@ function _M:init(t)
self.columns = assert(t.columns, "no list columns")
self.w = assert(t.width, "no list width")
self.h = assert(t.height, "no list height")
self.sortable = t.sortable
self.scrollbar = t.scrollbar
self.fct = t.fct
self.display_prop = t.display_prop or "name"
local w = self.w
if self.scrollbar then w = w - 10 end
for j, col in ipairs(self.columns) do
col.width = w * col.width / 100
end
Base.init(self, t)
end
......@@ -39,6 +46,7 @@ function _M:generate()
self.sel = 1
self.scroll = 1
self.max = #self.list
self:selectColumn(1, true)
local ls, ls_w, ls_h = self:getImage("ui/selection-left-sel.png")
local ms, ms_w, ms_h = self:getImage("ui/selection-middle-sel.png")
......@@ -54,17 +62,35 @@ function _M:generate()
local cm, cm_w, cm_h = self:getImage("ui/selection-middle-column.png")
local cr, cr_w, cr_h = self:getImage("ui/selection-right-column.png")
local fw, fh = self.w, ls_h
self.fw, self.fh = fw, fh
local fh = ls_h
self.fh = fh
self.max_display = math.floor(self.h / fh) - 1
-- Draw the scrollbar
if self.scrollbar then
local sb, sb_w, sb_h = self:getImage("ui/scrollbar.png")
local ssb, ssb_w, ssb_h = self:getImage("ui/scrollbar-sel.png")
self.scrollbar = { bar = {}, sel = {} }
self.scrollbar.sel.w, self.scrollbar.sel.h, self.scrollbar.sel.tex, self.scrollbar.sel.texw, self.scrollbar.sel.texh = ssb_w, ssb_h, ssb:glTexture()
local s = core.display.newSurface(sb_w, self.h - fh)
s:erase(200,0,0)
for i = 0, self.h - fh do s:merge(sb, 0, i) end
self.scrollbar.bar.w, self.scrollbar.bar.h, self.scrollbar.bar.tex, self.scrollbar.bar.texw, self.scrollbar.bar.texh = ssb_w, self.h - fh, s:glTexture()
end
-- Draw the list columns
for i, col in ipairs(self.columns) do
local colx = 0
for j, col in ipairs(self.columns) do
local fw = col.width
col.fw = fw
local text = col.name
local ss = core.display.newSurface(fw, fh)
local s = core.display.newSurface(fw, fh)
self.font:setStyle("bold")
ss:merge(cls, 0, 0)
for i = cls_w, fw - crs_w do ss:merge(cms, i, 0) end
ss:merge(crs, fw - crs_w, 0)
......@@ -74,49 +100,76 @@ function _M:generate()
for i = cl_w, fw - cr_w do s:merge(cm, i, 0) end
s:merge(cr, fw - cr_w, 0)
s:drawColorStringBlended(self.font, text, cl_w, (fh - self.font_h) / 2, 255, 255, 255, nil, fw - cl_w - cr_w)
self.font:setStyle("normal")
col._tex, col._tex_w, col._tex_h = s:glTexture()
col._stex = ss:glTexture()
end
-- Draw the list items
for i, item in ipairs(self.list) do
local text = item[self.display_prop]
local ss = core.display.newSurface(fw, fh)
local sus = core.display.newSurface(fw, fh)
local s = core.display.newSurface(fw, fh)
ss:merge(ls, 0, 0)
for i = ls_w, fw - rs_w do ss:merge(ms, i, 0) end
ss:merge(rs, fw - rs_w, 0)
ss:drawColorStringBlended(self.font, text, ls_w, (fh - self.font_h) / 2, 255, 255, 255, nil, fw - ls_w - rs_w)
s:erase(0, 0, 0)
s:drawColorStringBlended(self.font, text, ls_w, (fh - self.font_h) / 2, 255, 255, 255, nil, fw - ls_w - rs_w)
sus:merge(l, 0, 0)
for i = l_w, fw - r_w do sus:merge(m, i, 0) end
sus:merge(r, fw - r_w, 0)
sus:drawColorStringBlended(self.font, text, ls_w, (fh - self.font_h) / 2, 255, 255, 255, nil, fw - ls_w - rs_w)
-- Draw the list items
for i, item in ipairs(self.list) do
local text = tostring(item[col.display_prop or col.sort])
local ss = core.display.newSurface(fw, fh)
local sus = core.display.newSurface(fw, fh)
local s = core.display.newSurface(fw, fh)
ss:merge(ls, 0, 0)
for i = ls_w, fw - rs_w do ss:merge(ms, i, 0) end
ss:merge(rs, fw - rs_w, 0)
ss:drawColorStringBlended(self.font, text, ls_w, (fh - self.font_h) / 2, 255, 255, 255, nil, fw - ls_w - rs_w)
s:erase(0, 0, 0)
s:drawColorStringBlended(self.font, text, ls_w, (fh - self.font_h) / 2, 255, 255, 255, nil, fw - ls_w - rs_w)
sus:merge(l, 0, 0)
for i = l_w, fw - r_w do sus:merge(m, i, 0) end
sus:merge(r, fw - r_w, 0)
sus:drawColorStringBlended(self.font, text, ls_w, (fh - self.font_h) / 2, 255, 255, 255, nil, fw - ls_w - rs_w)
item._tex = item._tex or {}
item._stex = item._stex or {}
item._sustex = item._sustex or {}
item._tex[j] = {s:glTexture()}
item._stex[j] = {ss:glTexture()}
item._sustex[j] = {sus:glTexture()}
end
item._tex, item._tex_w, item._tex_h = s:glTexture()
item._stex = ss:glTexture()
item._sustex = sus:glTexture()
self.mouse:registerZone(colx, 0, col.width, self.fh, function(button, x, y, xrel, yrel, bx, by, event)
if button == "left" and event == "button" then self:selectColumn(j) end
end)
colx = colx + col.width
end
-- Add UI controls
self.mouse:registerZone(0, 0, self.w, self.h, function(button, x, y, xrel, yrel, bx, by, event)
self.mouse:registerZone(0, self.fh, self.w, self.h - self.fh, function(button, x, y, xrel, yrel, bx, by, event)
if button == "wheelup" and event == "button" then self.scroll = util.bound(self.scroll - 1, 1, self.max - self.max_display + 1)
elseif button == "wheeldown" and event == "button" then self.scroll = util.bound(self.scroll + 1, 1, self.max - self.max_display + 1) end
self.sel = util.bound(self.scroll + math.floor(by / self.fh), 1, self.max)
if button == "left" and event == "button" then self:onUse()
elseif button == "wheelup" and event == "button" then self.key:triggerVirtual("MOVE_UP")
elseif button == "wheeldown" and event == "button" then self.key:triggerVirtual("MOVE_DOWN")
end
if button == "left" and event == "button" then self:onUse() end
end)
self.key:addBinds{
ACCEPT = function() self:onUse() end,
MOVE_UP = function() self.sel = util.boundWrap(self.sel - 1, 1, self.max) self.scroll = util.scroll(self.sel, self.scroll, self.max_display) end,
MOVE_DOWN = function() self.sel = util.boundWrap(self.sel + 1, 1, self.max) self.scroll = util.scroll(self.sel, self.scroll, self.max_display) end,
}
self.key:addCommands{
_HOME = function()
self.sel = 1
self.scroll = util.scroll(self.sel, self.scroll, self.max_display)
end,
_END = function()
self.sel = self.max
self.scroll = util.scroll(self.sel, self.scroll, self.max_display)
end,
_PAGEUP = function()
self.sel = util.bound(self.sel - self.max_display, 1, self.max)
self.scroll = util.scroll(self.sel, self.scroll, self.max_display)
end,
_PAGEDOWN = function()
self.sel = util.bound(self.sel + self.max_display, 1, self.max)
self.scroll = util.scroll(self.sel, self.scroll, self.max_display)
end,
}
end
function _M:onUse()
......@@ -126,27 +179,58 @@ function _M:onUse()
else self.fct(item) end
end
function _M:selectColumn(i)
function _M:selectColumn(i, force)
if not self.sortable and not force then return end
local col = self.columns[i]
self.cur_col = i
if not col then return end
if self.cur_col ~= i then
self.cur_col = i
self.sort_reverse = false
else
self.sort_reverse = not self.sort_reverse
end
local fct = col.sort
if type(fct) == "string" then fct = function(a, b) return a[col.sort] < b[col.sort] end end
if self.sort_reverse then local old=fct fct = function(a, b) return old(b, a) end end
table.sort(self.list, fct)
end
function _M:display(x, y)
local bx, by = x, y
for j = 1, #self.columns do
local y = y
local col = self.columns[j]
if self.cur_col == j then
col._stex:toScreenFull(x, y, col.fw, self.fh, col._tex_w, col._tex_h)
else
col._tex:toScreenFull(x, y, col.fw, self.fh, col._tex_w, col._tex_h)
end
local y = y + self.fh
local max = math.min(self.scroll + self.max_display - 1, self.max)
for i = self.scroll, max do
local item = self.list[i]
if self.sel == i then
if self.focused then
item._stex:toScreenFull(x, y, self.fw, self.fh, item._tex_w, item._tex_h)
item._stex[j][1]:toScreenFull(x, y, col.fw, self.fh, item._stex[j][2], item._stex[j][3])
else
item._sustex:toScreenFull(x, y, self.fw, self.fh, item._tex_w, item._tex_h)
item._sustex[j][1]:toScreenFull(x, y, col.fw, self.fh, item._sustex[j][2], item._sustex[j][3])
end
else
item._tex:toScreenFull(x, y, self.fw, self.fh, item._tex_w, item._tex_h)
item._tex[j][1]:toScreenFull(x, y, col.fw, self.fh, item._tex[j][2], item._tex[j][3])
end
y = y + self.fh
end
x = x + col.width
end
if self.focused and self.scrollbar then
local pos = self.sel * (self.h - self.fh) / self.max
self.scrollbar.bar.tex:toScreenFull(bx + self.w - self.scrollbar.bar.w, by + self.fh, self.scrollbar.bar.w, self.scrollbar.bar.h, self.scrollbar.bar.texw, self.scrollbar.bar.texh)
self.scrollbar.sel.tex:toScreenFull(bx + self.w - self.scrollbar.sel.w, by + self.fh + pos, self.scrollbar.sel.w, self.scrollbar.sel.h, self.scrollbar.sel.texw, self.scrollbar.sel.texh)
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"
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 textbox title")
self.text = t.text or ""
self.hide = t.hide
self.max_len = t.max_len or 999
self.fct = assert(t.fct, "no textbox fct")
self.chars = assert(t.chars, "no textbox chars")
self.tmp = {}
for i = 1, #self.text do self.tmp[#self.tmp+1] = self.text:sub(i, i) end
self.cursor = #self.tmp + 1
self.scroll = 1
Base.init(self, t)
end
function _M:generate()
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
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
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_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 < self.max_len 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()
self.text = table.concat(self.tmp)
local text = ""
for i = self.scroll, self.scroll + self.max_display - 1 do
if not self.tmp[i] then break end
if not self.hide then text = text .. self.tmp[i]
else text = text .. "*" end
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
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