TradeView = Subview:extend { drawParent = true, --default? have = {}, onNew = function (self) -- create all the interface bits from the planet -- and add them to the view local boxL = the.app.width / 2 - 225 local boxT = the.app.height / 2 - 150 local boxR = boxL + 450 local boxB = boxT + 300 local lh = 17 -- line height self:add(Fill:new{ fill = {255,255,255}, x = boxL - 1, y = boxT - 1, width = 452, height = 302, }) self:add(Fill:new{ fill = {0,0,0}, x = boxL, y = boxT, width = 450, height = 300, }) self:add(Text:new{ text = 'Name', x = boxL + 10, y = boxT + 10, width = 380 }) self:add(Text:new{ text = 'Price', x = boxL + 10, y = boxT + 10, width = 300, align = 'right' }) self:add(Text:new{ text = 'Have', align = 'right', y = boxT + 10, x = boxR - 40 }) for i, good in ipairs(self.planet.goods) do local t = Text:new{ text = good[1], x = boxL + 10, y = boxT + 10 + i * lh, width = 380 } self:add(t) t = Text:new{ text = good[2], x = boxL + 10, y = boxT + 10 + i * lh, width = 300, align = 'right' } self:add(t) local b = Button:new{ x = boxR - 130, y = boxT + 10 + i * lh - 1, label = Text:new{ text = 'BUY', x = 1 }, onMouseDown = function (self) the.player:buy(good[1], good[2]) end } local tw, th = b.label:getSize() b.background = Fill:new{ fill = {100,100,100}, width = tw + 3, height = th } self:add(b) b = Button:new{ x = boxR - 90, y = boxT + 10 + i * lh - 1, label = Text:new{ text = 'SELL', x = 1 }, onMouseDown = function (self) the.player:sell(good[1], good[2]) end } local tw, th = b.label:getSize() b.background = Fill:new{ fill = {100,100,100}, width = tw + 3, height = th } self:add(b) local have = Text:new{ x = boxR - 110, y = boxT + 10 + i * lh, width = 100, align = 'right', } self.have[good[1]] = have self:add(have) end local b = Button:new{ x = boxL, y = boxT, label = Text:new{ text = 'Close', x = 3, y = 2 }, onMouseDown = function() self:close() end } local tw, th = b.label:getSize() b.background = Fill:new{ fill = {100,100,100}, width = tw + 7, height = th + 4 } b.x = boxR - b.background.width - 10 b.y = boxB - b.background.height - 10 self:add(b) local sh = the.player.shield if sh.strength < sh.max then sh.strength = sh.max self:add(Text:new{ text = 'Your shield has been recharged', width = 450, align = 'center', x = boxL, y = boxB - 4 * th - 12 }) end -- local cost = (sh.max - sh.strength) * sh.cost -- b = Button:new{ -- x = boxL, y = boxT, -- width = 800, -- -- WTF: trailing space makes it not wrap? -- label = Text:new{ text = 'Repair shields (' .. cost .. ') ', -- x = 3, y = 2 }, -- onMouseDown = function(self) -- if the.player.money >= cost and sh.strength < sh.max then -- the.player.money = the.player.money - cost -- sh.strength = sh.max -- self.visible = false -- end -- end -- } -- local tw, th = b.label:getSize() -- b.background = Fill:new{ -- fill = {100,100,100}, -- width = tw + 3, -- height = th + 4 -- } -- b.x = boxR - b.background.width - 60 -- b.y = boxB - b.background.height - 10 -- if sh.strength < sh.max then -- self:add(b) -- end self.playerMoney = Text:new{ text = 'Your money: ', width = 200, x = boxL + 10, y = boxB - th - 10 } self:add(self.playerMoney) self.availSpace = Text:new{ text = 'Available cargo space: ', width = 200, x = boxL + 10, y = boxB - 2 * th - 12 } self:add(self.availSpace) if the.player.kills > 0 then -- local award = the.player.kills * 100 -- self:add(Text:new{ -- text = 'You have been awarded ' .. -- award .. -- ' credits for killing bandits.', -- width = 450, -- align = 'center', -- x = boxL, -- y = boxB - 4 * th - 12 -- }) -- the.player.money = the.player.money + award the.player.kills = 0 end -- give the buttons a cycle to get out of the T/L corner self:update(0) end, activate = function (self) the.cursor.visible = false love.mouse.setVisible(true) love.mouse.setGrab(false) love.mouse.setPosition(the.app.width / 2, the.app.height / 2) Subview.activate(self) end, close = function (self) the.player:save() the.cursor.visible = true love.mouse.setVisible(false) love.mouse.setGrab(true) love.mouse.setPosition(the.app.width / 2, the.app.height / 2) self:deactivate() end, onUpdate = function (self) if the.keys:justPressed('escape') then self:close() end self.playerMoney.text = 'Your money: ' .. the.player.money self.availSpace.text = 'Available cargo space: ' .. the.player:availableSpace() for good, have in pairs(self.have) do have.text = the.player.goods[good] or 0 end end }