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

plop

git-svn-id: http://svn.net-core.org/repos/t-engine4@1367 51575b47-30f0-44d4-a5cc-537603b46e54
parent d82ed3a6
No related branches found
No related tags found
No related merge requests found
game/engines/default/data/gfx/ui/minus.png

365 B

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

375 B

-- 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 list
module(..., package.seeall, class.inherit(Base, Focusable))
function _M:init(t)
self.tree = assert(t.tree, "no tree tree")
self.columns = assert(t.columns, "no list columns")
self.w = assert(t.width, "no tree width")
self.h = t.height
self.nb_items = t.nb_items
assert(self.h or self.nb_items, "no tree height/nb_items")
self.fct = t.fct
self.display_prop = t.display_prop or "name"
self.scrollbar = t.scrollbar
self.all_clicks = t.all_clicks
self.level_offset = t.level_offset or 12
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
local ls, ls_w, ls_h = _M:getImage("ui/selection-left-sel.png")
local ms, ms_w, ms_h = _M:getImage("ui/selection-middle-sel.png")
local rs, rs_w, rs_h = _M:getImage("ui/selection-right-sel.png")
local l, l_w, l_h = _M:getImage("ui/selection-left.png")
local m, m_w, m_h = _M:getImage("ui/selection-middle.png")
local r, r_w, r_h = _M:getImage("ui/selection-right.png")
local plus, plus_w, plus_h = _M:getImage("ui/plus.png")
local minus, minus_w, minus_h = _M:getImage("ui/minus.png")
function _M:drawItem(item)
item.cols = {}
for i, col in ipairs(self.columns) do
local level = item.level
local color = item.color or {255,255,255}
local text = tostring(item[col.display_prop])
local ss = core.display.newSurface(col.width, self.fh)
local sus = core.display.newSurface(col.width, self.fh)
local s = core.display.newSurface(col.width, self.fh)
local offset = 0
if i == 1 then
offset = level * self.level_offset
if item.nodes then offset = offset + plus_w end
end
local startx = ls_w + offset
if i == 1 and item.nodes then ss:merge(item.shown and minus or plus, 0, 0) end
ss:merge(ls, offset, 0)
for i = startx, col.width - rs_w do ss:merge(ms, i, 0) end
ss:merge(rs, col.width - rs_w, 0)
ss:drawColorStringBlended(self.font, text, startx, (self.fh - self.font_h) / 2, color[1], color[2], color[3], nil, col.width - startx - rs_w)
s:erase(0, 0, 0)
if i == 1 and item.nodes then s:merge(item.shown and minus or plus, 0, 0) end
s:drawColorStringBlended(self.font, text, startx, (self.fh - self.font_h) / 2, color[1], color[2], color[3], nil, col.width - startx - rs_w)
if i == 1 and item.nodes then sus:merge(item.shown and minus or plus, 0, 0) end
sus:merge(l, offset, 0)
for i = startx, col.width - r_w do sus:merge(m, i, 0) end
sus:merge(r, col.width - r_w, 0)
sus:drawColorStringBlended(self.font, text, startx, (self.fh - self.font_h) / 2, color[1], color[2], color[3], nil, col.width - startx - rs_w)
item.cols[i] = {}
item.cols[i]._tex, item.cols[i]._tex_w, item.cols[i]._tex_h = s:glTexture()
item.cols[i]._stex = ss:glTexture()
item.cols[i]._sustex = sus:glTexture()
end
end
function _M:generate()
self.mouse:reset()
self.key:reset()
local fw, fh = self.w, ls_h
self.fw, self.fh = fw, fh
if not self.h then self.h = self.nb_items * fh end
self.max_display = math.floor(self.h / fh)
-- 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)
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 tree items
local recurs recurs = function(list, level)
for i, item in ipairs(list) do
item.level = level
self:drawItem(item)
if item.nodes then recurs(item.nodes, level+1) end
end
end
recurs(self.tree, 0)
-- Add UI controls
self.mouse:registerZone(0, 0, self.w, self.h, 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 (self.all_clicks or button == "left") and button ~= "wheelup" and button ~= "wheeldown" and event == "button" then self:onUse(button) 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,
}
self:outputList()
end
function _M:outputList()
local flist = {}
self.list = flist
local recurs recurs = function(list)
for i, item in ipairs(list) do
flist[#flist+1] = item
if item.nodes and item.shown then recurs(item.nodes) end
end
end
recurs(self.tree)
self.max = #self.list
self.sel = util.bound(self.sel or 1, 1, self.max)
self.scroll = self.scroll or 1
end
function _M:onUse(...)
local item = self.list[self.sel]
if not item then return end
if item.fct then item:fct()
else self.fct(self, item, self.sel, ...) end
end
function _M:display(x, y)
local bx, by = x, y
local max = math.min(self.scroll + self.max_display - 1, self.max)
for i = self.scroll, max do
local x = x
for j = 1, #self.columns do
local col = self.columns[j]
local item = self.list[i]
if not item then break end
if self.sel == i then
if self.focused then
item.cols[j]._stex:toScreenFull(x, y, col.width, self.fh, item.cols[j]._tex_w, item.cols[j]._tex_h)
else
item.cols[j]._sustex:toScreenFull(x, y, col.width, self.fh, item.cols[j]._tex_w, item.cols[j]._tex_h)
end
else
item.cols[j]._tex:toScreenFull(x, y, col.width, self.fh, item.cols[j]._tex_w, item.cols[j]._tex_h)
end
x = x + col.width
end
y = y + self.fh
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
......@@ -291,7 +291,9 @@ function _M:selectStepNew()
for i, mod in ipairs(self.mod_list) do
mod.fct = function()
self:registerDialog(require('special.mainmenu.dialogs.EnterName').new(mod))
self:registerDialog(require('engine.dialogs.GetText').new("Enter your character's name", "Name", 2, 25, function(text)
Module:instanciate(mod, text, true)
end))
end
mod.onSelect = function()
display_module.title = mod.long_name
......
......@@ -32,6 +32,7 @@ local ListColumns = require "engine.ui.ListColumns"
local Button = require "engine.ui.Button"
local Textzone = require "engine.ui.Textzone"
local Textbox = require "engine.ui.Textbox"
local TreeList = require "engine.ui.TreeList"
local Dialog = require "engine.ui.Dialog"
module(..., package.seeall, class.inherit(engine.Game, engine.interface.GameMusic))
......@@ -41,98 +42,50 @@ function _M:init()
self.refuse_threads = true
local text = Textzone.new{width=390, height=200, scrollbar=true, text=[[Pplopppo
zejkfzejkfh #RED#zmkfhzekhfkjfhkjqerhfgq#LAST# jfh qjzfh qzejfh #BLUE#qjzfh
flkzerjflm qzfj #WHITE#zeklfj qlfjkql ql
zf ze
fze fqefqzfjhjqzkef
fqzef
zef qzekjfhqjkzfh
ze fzef zejkhzfekjh ze
fze
fze
zefze fze zef
ze fzef zefz e
zlfjklhzqfjkqhzefkhqzefjkqh z
zef qzekjfhqjkzfh
ze fzef zejkhzfekjh ze
fze
fze
zefze fze zef
ze fzef zefz e
zlfjklhzqfjkqhzefkhqzefjkqh z
zef qzekjfhqjkzfh
ze fzef zejkhzfekjh ze
fze
fze
zefze fze zef
ze fzef zefz e
zlfjklhzqfjkqhzefkhqzefjkqh z
]]}
local box = Textbox.new{title="Name: ", text="", chars=10, max_len=30, fct=function(text) print("got", text) end}
local b1 = Button.new{text="Ok", fct=function() print"OK" end}
local b2 = Button.new{text="Cancel", fct=function() print"KO" end}
local listc = ListColumns.new{width=390, height=200, sortable=true, scrollbar=true, columns={
{name="Name", width=90, display_prop="name", sort="name"},
{name="Encumber", width=10, display_prop="encumberance", sort="encumberance"},
}, list={
{name="toto", encumberance=20},
{name="tutu", encumberance=50},
{name="plopzor #GOLD#Robe of the Archmage#WHITE#!", encumberance=20},
{name="MOUHAHAHAHAH!", encumberance=20},
{name="toto", encumberance=20},
{name="tutu", encumberance=50},
{name="plopzor #GOLD#Robe of the Archmage#WHITE#!", encumberance=20},
{name="MOUHAHAHAHAH!", encumberance=20},
{name="toto", encumberance=20},
{name="tutu", encumberance=50},
{name="plopzor #GOLD#Robe of the Archmage#WHITE#!", encumberance=20},
{name="MOUHAHAHAHAH!", encumberance=20},
{name="toto", encumberance=20},
{name="tutu", encumberance=50},
{name="plopzor #GOLD#Robe of the Archmage#WHITE#!", encumberance=20},
{name="MOUHAHAHAHAH!", encumberance=20},
}, fct=function(item) print(item.name) end}
local list = List.new{width=200, height=200, scrollbar=true, list={
{name="toto", encumberance=20},
{name="tutu", encumberance=50},
{name="plopzor #GOLD#Robe of the Archmage#WHITE#!", encumberance=20},
{name="MOUHAHAHAHAH!", encumberance=20},
{name="toto", encumberance=20},
{name="tutu", encumberance=50},
{name="plopzor #GOLD#Robe of the Archmage#WHITE#!", encumberance=20},
{name="MOUHAHAHAHAH!", encumberance=20},
{name="toto", encumberance=20},
{name="tutu", encumberance=50},
{name="plopzor #GOLD#Robe of the Archmage#WHITE#!", encumberance=20},
{name="MOUHAHAHAHAH!", encumberance=20},
{name="toto", encumberance=20},
{name="tutu", encumberance=50},
{name="plopzor #GOLD#Robe of the Archmage#WHITE#!", encumberance=20},
{name="MOUHAHAHAHAH!", encumberance=20},
}, fct=function(item) print(item.name) end}
local tree = TreeList.new{width=390, height=200, sortable=true, scrollbar=true, all_clicks=true, columns={
{width=90, display_prop="name", sort="name"},
{width=10, display_prop="val", sort="val"},
}, tree={
{name="#{bold}#Node 1#{normal}#", val="plop", shown=true, nodes={
{name="toto", val=20},
{name="tutu", val=50},
{name="plopzor #GOLD#Robe of the Archmage#WHITE#!", val=20},
{name="MOUHAHAHAHAH!", val=20},
}},
{name="#{bold}#Node 2#{normal}#", val="plop", shown=false, nodes={
{name="toto", val=20},
{name="tutu", val=50},
{name="plopzor #GOLD#Robe of the Archmage#WHITE#!", val=20},
}},
{name="#{bold}#Node 3#{normal}#", val="plop", shown=true, nodes={
{name="MOUHAHAHAHAH!", val=20},
{name="toto", val=20},
{name="tutu", val=50},
{name="plopzor #GOLD#Robe of the Archmage#WHITE#!", val=20},
{name="MOUHAHAHAHAH!", val=20},
{name="toto", val=20},
}},
{name="#{bold}#Node 4#{normal}#", val="plop", shown=true, nodes={
{name="tutu", val=50},
{name="plopzor #GOLD#Robe of the Archmage#WHITE#!", val=20},
{name="MOUHAHAHAHAH!", val=20},
}},
}, fct=function(self, item, sel, v)
if item.nodes then
item.shown = not item.shown
self:drawItem(item)
self:outputList()
else
item.val = item.val + (v == "left" and 1 or -1)
self:drawItem(item)
end
end}
local d = Dialog.new("Test UI", 800, 500)
d:loadUI{
{left=0, top=0, ui=listc},
{left=400, top=0, ui=text},
{left=5, bottom=10 + b1.h, ui=box},
{left=box.w + 10, bottom=10 + b1.h, ui=list},
{left=5, bottom=5, ui=b1},
{right=5, bottom=5, ui=b2},
{left=0, top=0, ui=tree},
}
d:setupUI()
self:registerDialog(d)
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 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(runmod)
engine.Dialog.init(self, "Enter your character's name?", 320, 110)
self.runmod = runmod
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)
game:bindKeysToStep()
end
})
self:mouseZones{}
self:addControl(TextBox.new({name="name",title="Name:",min=2, max=25, 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() >= 2 then
game:unregisterDialog(self)
Module:instanciate(self.runmod, self.name, true)
else
engine.Dialog:simplePopup("Error", "Character name 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
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