Skip to content
Snippets Groups Projects
Commit 23bc3493 authored by dg's avatar dg
Browse files

list

git-svn-id: http://svn.net-core.org/repos/t-engine4@1350 51575b47-30f0-44d4-a5cc-537603b46e54
parent ae465d0d
No related branches found
No related tags found
No related merge requests found
...@@ -47,7 +47,8 @@ function _M:resize(w, h, x, y, alpha) ...@@ -47,7 +47,8 @@ function _M:resize(w, h, x, y, alpha)
self.w, self.h = math.floor(w), math.floor(h) self.w, self.h = math.floor(w), math.floor(h)
self.display_x = math.floor(x or (gamew - self.w) / 2) self.display_x = math.floor(x or (gamew - self.w) / 2)
self.display_y = math.floor(y or (gameh - self.h) / 2) self.display_y = math.floor(y or (gameh - self.h) / 2)
self.iw, self.ih = w - 2 * 5, h - 8 - 16 - 3 self.ix, self.iy = 5, 22 + 3
self.iw, self.ih = w - 2 * 5, h - 8 - 22 - 3
self:generate() self:generate()
end end
...@@ -105,11 +106,11 @@ function _M:loadUI(t) ...@@ -105,11 +106,11 @@ function _M:loadUI(t)
for i, ui in ipairs(t) do for i, ui in ipairs(t) do
self.uis[#self.uis+1] = ui self.uis[#self.uis+1] = ui
local ux, uy = 0, 0 local ux, uy = self.ix, self.iy
if ui.top then uy = uy + ui.top if ui.top then uy = uy + ui.top
elseif ui.bottom then uy = uy + self.h - ui.bottom - ui.ui.h end elseif ui.bottom then uy = uy + self.ih - ui.bottom - ui.ui.h end
if ui.left then ux = ux + ui.left if ui.left then ux = ux + ui.left
elseif ui.right then ux = ux + self.w - ui.right - ui.ui.w end elseif ui.right then ux = ux + self.iw - ui.right - ui.ui.w end
ui.x = ux ui.x = ux
ui.y = uy ui.y = uy
ui.ui.mouse.delegate_offset_x = ux ui.ui.mouse.delegate_offset_x = ux
......
...@@ -25,51 +25,80 @@ local Focusable = require "engine.ui.Focusable" ...@@ -25,51 +25,80 @@ local Focusable = require "engine.ui.Focusable"
module(..., package.seeall, class.inherit(Base, Focusable)) module(..., package.seeall, class.inherit(Base, Focusable))
function _M:init(t) function _M:init(t)
self.text = assert(t.text, "no button text") self.list = assert(t.list, "no list list")
self.fct = assert(t.fct, "no button fct") self.w = assert(t.width, "no list width")
self.h = assert(t.height, "no list height")
self.fct = t.fct
self.display_prop = t.display_prop or "name"
Base.init(self, t) Base.init(self, t)
end end
function _M:generate() function _M:generate()
local ls, ls_w, ls_h = self:getImage("ui/button-left-sel.png") self.sel = 1
local ms, ms_w, ms_h = self:getImage("ui/button-middle-sel.png") self.scroll = 1
local rs, rs_w, rs_h = self:getImage("ui/button-right-sel.png") self.max = #self.list
local l, l_w, l_h = self:getImage("ui/button-left.png")
local m, m_w, m_h = self:getImage("ui/button-middle.png")
local r, r_w, r_h = self:getImage("ui/button-right.png")
-- Draw UI local ls, ls_w, ls_h = self:getImage("ui/selection-left-sel.png")
self.font:setStyle("bold") local ms, ms_w, ms_h = self:getImage("ui/selection-middle-sel.png")
local w, h = self.font:size(self.text:removeColorCodes()) local rs, rs_w, rs_h = self:getImage("ui/selection-right-sel.png")
local fw, fh = w + ls_w + rs_w, ls_h -- local l, l_w, l_h = self:getImage("ui/button-left.png")
local ss = core.display.newSurface(fw, fh) -- local m, m_w, m_h = self:getImage("ui/button-middle.png")
local s = core.display.newSurface(fw, fh) -- local r, r_w, r_h = self:getImage("ui/button-right.png")
ss:merge(ls, 0, 0) -- Draw the list items
for i = ls_w, fw - rs_w do ss:merge(ms, i, 0) end local fw, fh = self.w, ls_h
ss:merge(rs, fw - rs_w, 0) self.fw, self.fh = fw, fh
ss:drawColorStringBlended(self.font, self.text, ls_w, (fh - h) / 2, 255, 255, 255)
s:merge(l, 0, 0) self.max_display = math.floor(self.h / fh)
for i = l_w, fw - r_w do s:merge(m, i, 0) end
s:merge(r, fw - r_w, 0) for i, item in ipairs(self.list) do
s:drawColorStringBlended(self.font, self.text, ls_w, (fh - h) / 2, 255, 255, 255) local text = item[self.display_prop]
self.font:setStyle("normal") local ss = 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:merge(l, 0, 0)
-- for i = l_w, fw - r_w do s:merge(m, i, 0) end
-- s:merge(r, fw - r_w, 0)
s:drawColorStringBlended(self.font, text, ls_w, (fh - self.font_h) / 2, 255, 255, 255, nil, fw - ls_w - rs_w)
item._tex, item._tex_w, item._tex_h = s:glTexture()
item._stex = ss:glTexture()
end
-- Add UI controls -- Add UI controls
self.mouse:registerZone(0, 0, fw, fh, function(button, x, y, xrel, yrel, bx, by, event) if button == "left" and event == "button" then self.fct() end end) self.mouse:registerZone(0, 0, self.w, self.h, function(button, x, y, xrel, yrel, bx, by, event)
self.key:addBind("ACCEPT", function() self.fct() end) self.sel = util.bound(self.scroll + math.floor(by / self.fh), 1, self.max)
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,
}
end
self.tex, self.tex_w, self.tex_h = s:glTexture() function _M:onUse()
self.stex = ss:glTexture() local item = self.list[self.sel]
self.w, self.h = fw, fh if not item then return end
if item.fct then item:fct()
else self.fct(item) end
end end
function _M:display(x, y) function _M:display(x, y)
if self.focused then for i = 1, self.max do
self.stex:toScreenFull(x, y, self.w, self.h, self.tex_w, self.tex_h) local item = self.list[i]
else if self.sel == i then
self.tex:toScreenFull(x, y, self.w, self.h, self.tex_w, self.tex_h) item._stex:toScreenFull(x, y, self.fw, self.fh, item._tex_w, item._tex_h)
else
item._tex:toScreenFull(x, y, self.fw, self.fh, item._tex_w, item._tex_h)
end
y = y + self.fh
end end
end end
...@@ -27,6 +27,7 @@ local Tooltip = require "engine.Tooltip" ...@@ -27,6 +27,7 @@ local Tooltip = require "engine.Tooltip"
local ButtonList = require "engine.ButtonList" local ButtonList = require "engine.ButtonList"
local DownloadDialog = require "engine.dialogs.DownloadDialog" local DownloadDialog = require "engine.dialogs.DownloadDialog"
local List = require "engine.ui.List"
local Button = require "engine.ui.Button" local Button = require "engine.ui.Button"
local Dialog = require "engine.ui.Dialog" local Dialog = require "engine.ui.Dialog"
...@@ -39,9 +40,16 @@ function _M:init() ...@@ -39,9 +40,16 @@ function _M:init()
local b1 = Button.new{text="Ok", fct=function() print"OK" end} local b1 = Button.new{text="Ok", fct=function() print"OK" end}
local b2 = Button.new{text="Cancel", fct=function() print"KO" end} local b2 = Button.new{text="Cancel", fct=function() print"KO" end}
local list = List.new{width=200, height=200, list={
{name="toto"},
{name="tutu"},
{name="plopzor #GOLD#Robe of the Archmage#WHITE#!"},
{name="MOUHAHAHAHAH!"},
}, fct=function(item) print(item.name) end}
local d = Dialog.new("plop", 400, 300) local d = Dialog.new("Test UI", 400, 300)
d:loadUI{ d:loadUI{
{left=0, top=0, ui=list},
{left=10, bottom=10, ui=b1}, {left=10, bottom=10, ui=b1},
{right=10, bottom=10, ui=b2}, {right=10, bottom=10, ui=b2},
} }
......
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