ShowEquipInven.lua
8.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
-- TE4 - T-Engine 4
-- Copyright (C) 2009, 2010, 2011 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 Dialog = require "engine.ui.Dialog"
local ListColumns = require "engine.ui.ListColumns"
local Textzone = require "engine.ui.Textzone"
local TextzoneList = require "engine.ui.TextzoneList"
local Separator = require "engine.ui.Separator"
module(..., package.seeall, class.inherit(Dialog))
function _M:init(title, actor, filter, action, on_select)
self.action = action
self.filter = filter
self.actor = actor
self.on_select = on_select
Dialog.init(self, title or "Inventory", math.max(800, game.w * 0.8), math.max(600, game.h * 0.8))
self.max_h = 0
-- self.c_desc = TextzoneList.new{width=self.iw, height=self.max_h*self.font_h, no_color_bleed=true}
self.c_inven = ListColumns.new{width=math.floor(self.iw / 2 - 10), height=self.ih - self.max_h*self.font_h - 10, sortable=true, scrollbar=true, columns={
{name="", width={20,"fixed"}, display_prop="char", sort="id"},
{name="", width={24,"fixed"}, display_prop="object", direct_draw=function(item, x, y) if item.object then item.object:toScreen(nil, x+4, y, 16, 16) end end},
{name="Inventory", width=72, display_prop="name", sort="name"},
{name="Category", width=20, display_prop="cat", sort="cat"},
{name="Enc.", width=8, display_prop="encumberance", sort="encumberance"},
}, list={}, fct=function(item, sel, button, event) self:use(item, button, event) end, select=function(item, sel) self:select(item) end}
self.c_equip = ListColumns.new{width=math.floor(self.iw / 2 - 10), height=self.ih - self.max_h*self.font_h - 10, scrollbar=true, columns={
{name="", width={20,"fixed"}, display_prop="char"},
{name="", width={8+16,"fixed"}, display_prop="object", direct_draw=function(item, x, y) if item.object then item.object:toScreen(nil, x+4, y, 16, 16) end end},
{name="Equipment", width=72, display_prop="name"},
{name="Category", width=20, display_prop="cat"},
{name="Enc.", width=8, display_prop="encumberance"},
}, list={}, fct=function(item, sel, button, event) self:use(item, button, event) end, select=function(item, sel) self:select(item) end}
self:generateList()
self:loadUI{
{left=0, top=0, ui=self.c_equip},
{right=0, top=0, ui=self.c_inven},
-- {left=0, bottom=0, ui=self.c_desc},
{hcenter=0, top=5, ui=Separator.new{dir="horizontal", size=self.ih - 10}},
-- {left=5, bottom=self.c_desc.h, ui=Separator.new{dir="vertical", size=self.iw - 10}},
}
self:setFocus(self.c_inven)
self:setupUI()
self.key:addCommands{
__TEXTINPUT = function(c)
local list
if self.focus_ui and self.focus_ui.ui == self.c_inven then list = self.c_inven.list
elseif self.focus_ui and self.focus_ui.ui == self.c_equip then list = self.c_equip.list
end
if list and list.chars[c] then
self:use(list[list.chars[c]])
end
end,
}
self.key:addBinds{
HOTKEY_1 = function() self:defineHotkey(1) end,
HOTKEY_2 = function() self:defineHotkey(2) end,
HOTKEY_3 = function() self:defineHotkey(3) end,
HOTKEY_4 = function() self:defineHotkey(4) end,
HOTKEY_5 = function() self:defineHotkey(5) end,
HOTKEY_6 = function() self:defineHotkey(6) end,
HOTKEY_7 = function() self:defineHotkey(7) end,
HOTKEY_8 = function() self:defineHotkey(8) end,
HOTKEY_9 = function() self:defineHotkey(9) end,
HOTKEY_10 = function() self:defineHotkey(10) end,
HOTKEY_11 = function() self:defineHotkey(11) end,
HOTKEY_12 = function() self:defineHotkey(12) end,
HOTKEY_SECOND_1 = function() self:defineHotkey(13) end,
HOTKEY_SECOND_2 = function() self:defineHotkey(14) end,
HOTKEY_SECOND_3 = function() self:defineHotkey(15) end,
HOTKEY_SECOND_4 = function() self:defineHotkey(16) end,
HOTKEY_SECOND_5 = function() self:defineHotkey(17) end,
HOTKEY_SECOND_6 = function() self:defineHotkey(18) end,
HOTKEY_SECOND_7 = function() self:defineHotkey(19) end,
HOTKEY_SECOND_8 = function() self:defineHotkey(20) end,
HOTKEY_SECOND_9 = function() self:defineHotkey(21) end,
HOTKEY_SECOND_10 = function() self:defineHotkey(22) end,
HOTKEY_SECOND_11 = function() self:defineHotkey(23) end,
HOTKEY_SECOND_12 = function() self:defineHotkey(24) end,
HOTKEY_THIRD_1 = function() self:defineHotkey(25) end,
HOTKEY_THIRD_2 = function() self:defineHotkey(26) end,
HOTKEY_THIRD_3 = function() self:defineHotkey(27) end,
HOTKEY_THIRD_4 = function() self:defineHotkey(28) end,
HOTKEY_THIRD_5 = function() self:defineHotkey(29) end,
HOTKEY_THIRD_6 = function() self:defineHotkey(30) end,
HOTKEY_THIRD_7 = function() self:defineHotkey(31) end,
HOTKEY_THIRD_8 = function() self:defineHotkey(32) end,
HOTKEY_THIRD_9 = function() self:defineHotkey(33) end,
HOTKEY_THIRD_10 = function() self:defineHotkey(34) end,
HOTKEY_THIRD_11 = function() self:defineHotkey(35) end,
HOTKEY_THIRD_12 = function() self:defineHotkey(36) end,
ACCEPT = function()
if self.focus_ui and self.focus_ui.ui == self.c_inven then self:use(self.c_inven.list[self.c_inven.sel])
elseif self.focus_ui and self.focus_ui.ui == self.c_equip then self:use(self.c_equip.list[self.c_equip.sel])
end
end,
EXIT = function() game:unregisterDialog(self) end,
}
end
function _M:defineHotkey(id)
if not self.actor or not self.actor.hotkey then return end
local item = nil
if self.focus_ui and self.focus_ui.ui == self.c_inven then item = self.c_inven.list[self.c_inven.sel]
elseif self.focus_ui and self.focus_ui.ui == self.c_equip then item = self.c_equip.list[self.c_equip.sel]
end
if not item or not item.object then return end
self.actor.hotkey[id] = {"inventory", item.object:getName{no_add_name=true, no_count=true}}
self:simplePopup("Hotkey "..id.." assigned", item.object:getName{no_add_name=true, no_count=true}:capitalize().." assigned to hotkey "..id)
self.actor.changed = true
end
function _M:select(item)
if self.cur_item == item then return end
if item then
if self.on_select then self.on_select(item) end
end
self.cur_item = item
end
function _M:use(item, button, event)
if item then
if self.action(item.object, item.inven, item.item, button, event) then
game:unregisterDialog(self)
end
end
end
function _M:generateList()
-- Makes up the list
self.equip_list = {}
local list = self.equip_list
local chars = {}
local i = 1
self.max_h = 0
for inven_id = 1, #self.actor.inven_def do
if self.actor.inven[inven_id] and (self.actor.inven_def[inven_id].is_worn or self.actor.inven_def[inven_id].is_shown_equip) then
list[#list+1] = { id=#list+1, char="", name=tstring{{"font", "bold"}, self.actor.inven_def[inven_id].name, {"font", "normal"}}, color={0x90, 0x90, 0x90}, inven=inven_id, cat="", encumberance="", desc=self.actor.inven_def[inven_id].description }
self.max_h = math.max(self.max_h, #self.actor.inven_def[inven_id].description:splitLines(self.iw - 10, self.font))
for item, o in ipairs(self.actor.inven[inven_id]) do
if not self.filter or self.filter(o) then
local char = self:makeKeyChar(i)
local enc = 0
o:forAllStack(function(o) enc=enc+o.encumber end)
list[#list+1] = { id=#list+1, char=char, name=o:getName(), color=o:getDisplayColor(), object=o, inven=inven_id, item=item, cat=o.subtype, encumberance=enc, desc=o:getDesc() }
chars[char] = #list
i = i + 1
end
end
end
end
list.chars = chars
self.equip_list = list
-- Makes up the list
self.inven_list = {}
local list = self.inven_list
local chars = {}
local i = 1
for item, o in ipairs(self.actor:getInven("INVEN") or {}) do
if not self.filter or self.filter(o) then
local char = self:makeKeyChar(i)
local enc = 0
o:forAllStack(function(o) enc=enc+o.encumber end)
list[#list+1] = { id=#list+1, char=char, name=o:getName(), color=o:getDisplayColor(), object=o, inven=self.actor.INVEN_INVEN, item=item, cat=o.subtype, encumberance=enc, desc=o:getDesc() }
chars[char] = #list
i = i + 1
end
end
list.chars = chars
self.c_inven:setList(self.inven_list)
self.c_equip:setList(self.equip_list)
end
function _M:on_recover_focus()
self:generateList()
end