Skip to content

Commit

Permalink
Bulletin board view
Browse files Browse the repository at this point in the history
Module adjustments to make them compatible with the pigui Bulletin Board
Allow passing Table to PiguiFace constructor
Optional targetResolution parameter to allow rescaling ui into smaller windows
  • Loading branch information
vakhoir committed Feb 17, 2020
1 parent f86be7c commit b1ae9f1
Show file tree
Hide file tree
Showing 9 changed files with 270 additions and 76 deletions.
92 changes: 48 additions & 44 deletions data/libs/ui/PiguiFace.lua
Original file line number Diff line number Diff line change
@@ -1,81 +1,85 @@
-- Copyright © 2008-2020 Pioneer Developers. See AUTHORS.txt for details
-- Licensed under the terms of the GPL v3. See licenses/GPL-3.txt

local Face = import 'PiGui.Modules.Face'
local FaceTextureGenerator = import 'PiGui.Modules.Face'
local Character = import 'Character'
local ui = import 'pigui/pigui.lua'
local colors = ui.theme.colors
local orbiteer = ui.fonts.orbiteer
local charInfoFlags = ui.WindowFlags {"AlwaysUseWindowPadding", "NoScrollbar"}

local testCharacter = function (character)
if not (character and (type(character)=='table') and (getmetatable(character).class == 'Character'))
local ensureCharacter = function (character)
if not (character and (type(character)=='table') and getmetatable(character) and (getmetatable(character).class == 'Character'))
then
error ('Character object expected')
return Character.New(character)
end
end

local getStyleVar = function(style, key, defaultValue)
return (style and (type(style)=='table')) and style[key] or defaultValue
return character
end

local PiGuiFace = {}

function PiGuiFace.New (character, style)
testCharacter(character)

character = ensureCharacter(character)
style = style or {}
local faceDesc = character.faceDescription and character.faceDescription or {
FEATURE_GENDER = character.female and 1 or 0,
FEATURE_ARMOUR = character.armour and 1 or 0,
}

local faceTexGen = FaceTextureGenerator.New(faceDesc, character.seed)
local piguiFace = {
widget = Face.New(faceDesc, character.seed),
character = character
faceTextureGenerator = faceTexGen,
character = character,
texture = {
id = faceTexGen.textureId,
uv = faceTexGen.textureSize,
},
style = {
size = style.size or ui.rescaleUI(Vector2(320, 320), Vector2(1600, 900)),
windowPadding = style.windowPadding or Vector2(0,0),
itemSpacing = style.itemSpacing or Vector2(0,0),
showCharInfo = style.showCharInfo == nil and true or style.showCharInfo,
charInfoPadding = style.charInfoPadding or ui.rescaleUI(Vector2(24,24), Vector2(1600, 900)),
charInfoBgColor = style.bgColor or Color(0,0,0,160),
nameFont = style.nameFont or orbiteer.xlarge,
titleFont = style.titleFont or orbiteer.large,
}
}

piguiFace.texture = {
id = piguiFace.widget.textureId,
uv = piguiFace.widget.textureSize
}
piguiFace.style.charInfoHeight = math.ceil(
piguiFace.style.nameFont.size + (character.title and piguiFace.style.titleFont.size or 0)
+ piguiFace.style.charInfoPadding.y*2
)

piguiFace.style = {
windowPadding = getStyleVar(style, 'windowPadding', Vector2(0,0)),
itemSpacing = getStyleVar(style, 'itemSpacing', Vector2(0,0)),
bgColor = getStyleVar(style, 'bgColor', Color(0,0,0,160)),
nameFont = getStyleVar(style, 'nameFont', orbiteer.xlarge),
titleFont = getStyleVar(style, 'titleFont', orbiteer.large),
}
setmetatable(piguiFace, {
__index = PiGuiFace,
class = "UI.PiGuiFace",
})

piguiFace.style.infoDetailsHeight = math.ceil(
piguiFace.style.nameFont.size + piguiFace.style.titleFont.size
+ getStyleVar(style, 'infoDetailsPadding',48) * (ui.screenHeight / 1200)
)
return piguiFace
end

piguiFace.Draw = function(self, faceSize)
ui.image(self.texture.id, faceSize, Vector2(0.0, 0.0), self.texture.uv, colors.white)
function PiGuiFace:render ()
ui.image(self.texture.id, self.style.size, Vector2(0.0, 0.0), self.texture.uv, colors.white)

if(self.style.showCharInfo) then
local lastPos = ui.getCursorPos()
ui.setCursorPos(lastPos - Vector2(0.0, self.style.infoDetailsHeight + self.style.itemSpacing.y))
ui.withStyleColors({ChildWindowBg = self.style.bgColor}, function()
ui.withStyleVars({WindowPadding = self.style.windowPadding, ItemSpacing = self.style.itemSpacing}, function()
ui.child("PlayerInfoDetails", Vector2(faceSize.x, self.style.infoDetailsHeight), {"AlwaysUseWindowPadding", "NoScrollbar"}, function ()
ui.withFont(self.style.nameFont.name, self.style.nameFont.size, function()
ui.text(self.character.name)
end)
ui.setCursorPos(lastPos - Vector2(0.0, self.style.charInfoHeight + self.style.itemSpacing.y))
ui.withStyleColorsAndVars({ChildWindowBg = self.style.charInfoBgColor}, {WindowPadding = self.style.charInfoPadding, ItemSpacing = self.style.itemSpacing}, function ()
ui.child("PlayerInfoDetails", Vector2(self.style.size.x, self.style.charInfoHeight), charInfoFlags, function ()
ui.withFont(self.style.nameFont.name, self.style.nameFont.size, function()
ui.text(self.character.name)
end)
if self.character.title then
ui.withFont(self.style.titleFont.name, self.style.titleFont.size, function()
ui.text(self.character.title or '')
ui.text(self.character.title)
end)
end)
end
end)
end)
ui.setCursorPos(lastPos)
end

setmetatable(piguiFace, {
__index = PiGuiFace,
class = "UI.PiGuiFace",
})

return piguiFace
end

return PiGuiFace
5 changes: 3 additions & 2 deletions data/modules/BreakdownServicing/BreakdownServicing.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ local NameGen = import("NameGen")
local Format = import("Format")
local Serializer = import("Serializer")
local Equipment = import("Equipment")
local Character = import("Character")

local l = Lang.GetResource("module-breakdownservicing")
local lui = Lang.GetResource("ui-core")
Expand Down Expand Up @@ -129,7 +130,7 @@ local onChat = function (form, ref, option)
if option == 0 then
-- Initial proposal
form:SetTitle(ad.title)
form:SetFace({ female = ad.isfemale, seed = ad.faceseed, name = ad.name })
form:SetFace(Character.New({ female = ad.isfemale, seed = ad.faceseed, name = ad.name }))
-- Replace token with details of last service (which might have
-- been seconds ago)
form:SetMessage(string.interp(message, {
Expand All @@ -149,7 +150,7 @@ local onChat = function (form, ref, option)
-- Yes please, service my engine
form:Clear()
form:SetTitle(ad.title)
form:SetFace({ female = ad.isfemale, seed = ad.faceseed, name = ad.name })
form:SetFace(Character.New({ female = ad.isfemale, seed = ad.faceseed, name = ad.name }))
if Game.player:GetMoney() >= price then -- We did check earlier, but...
-- Say thanks
form:SetMessage(ad.response)
Expand Down
28 changes: 23 additions & 5 deletions data/modules/GoodsTrader/GoodsTrader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ local NameGen = import("NameGen")
local Rand = import("Rand")
local Serializer = import("Serializer")
local Equipment = import("Equipment")
local Character = import("Character")

local l = Lang.GetResource("module-goodstrader")

Expand All @@ -27,7 +28,7 @@ local onChat = function (form, ref, option)

form:Clear()
form:SetTitle(ad.flavour)
form:SetFace({ seed = ad.faceseed })
form:SetFace(ad.trader)
form:SetMessage(l.WELCOME_TO..ad.flavour..".\n"..ad.slogan)

local onClick = function (ref)
Expand All @@ -49,6 +50,12 @@ local onChat = function (form, ref, option)
end
end,

canDisplayItem = function (ref, commodity)
if ads[ref].stock[commodity] then
return true
end
end,

-- how much of this commodity do we have in stock?
getStock = function (ref, commodity)
return ads[ref].stock[commodity]
Expand All @@ -75,13 +82,23 @@ local onChat = function (form, ref, option)
end,

-- do something when we buy this commodity
bought = function (ref, commodity)
ads[ref].stock[commodity] = ads[ref].stock[commodity] + 1
bought = function (ref, commodity, market)
local count = 1
if market.tradeAmount ~= nil then
count = market.tradeAmount
end

ads[ref].stock[commodity] = ads[ref].stock[commodity] - count
end,

-- do something when we sell this commodity
sold = function (ref, commodity)
ads[ref].stock[commodity] = ads[ref].stock[commodity] - 1
sold = function (ref, commodity, market)
local count = 1
if market.tradeAmount ~= nil then
count = market.tradeAmount
end

ads[ref].stock[commodity] = ads[ref].stock[commodity] + count
end,
})

Expand Down Expand Up @@ -125,6 +142,7 @@ local onCreateBB = function (station)
slogan = slogan,
ispolice = ispolice,
faceseed = rand:Integer(),
trader = Character.New(),
}

ad.stock = {}
Expand Down
4 changes: 2 additions & 2 deletions data/pigui/libs/chat-form.lua
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ function ChatForm:Clear ()
self.tradeFuncs = nil
end

local tradeFuncKeys = { "canTrade", "getStock", "getBuyPrice", "getSellPrice", "onClickBuy", "onClickSell", "canDisplayItem" }
local tradeFuncKeys = { "canTrade", "getStock", "getBuyPrice", "getSellPrice", "onClickBuy", "onClickSell", "canDisplayItem", "bought", "sold"}
function ChatForm:AddGoodsTrader (funcs)
self.equipWidgetConfig = {
stationColumns = { "icon", "name", "price", "stock" },
Expand All @@ -148,7 +148,7 @@ function ChatForm:AddGoodsTrader (funcs)
local fn = funcs[key]
if fn then
self.market.funcs[key] = function (s, e)
return fn(self.ref, e)
return fn(self.ref, e, s)
end
end
end
Expand Down
15 changes: 8 additions & 7 deletions data/pigui/libs/commodity-market.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ local Equipment = import 'Equipment'
local ui = import 'pigui/pigui.lua'
local pionillium = ui.fonts.pionillium
local orbiteer = ui.fonts.orbiteer
local ModalWindow = import 'pigui/libs/modal-win.lua'
local MarketWidget = import 'pigui/libs/equipment-market.lua'

local l = Lang.GetResource("ui-core")
Expand Down Expand Up @@ -126,7 +125,8 @@ function CommodityMarketWidget:ChangeTradeAmount(delta)
local stock

if self.tradeModeBuy then
stock = Game.player:GetDockedWith():GetEquipmentStock(self.selectedItem)
price = self.funcs.getBuyPrice(self, self.selectedItem)
stock = self.funcs.getStock(self, self.selectedItem)
if stock == 0 then
self.tradeText = l.NONE_FOR_SALE_IN_THIS_STATION
self.tradeTextColor = textColorError
Expand All @@ -138,6 +138,7 @@ function CommodityMarketWidget:ChangeTradeAmount(delta)
return
end
else
price = self.funcs.getSellPrice(self, self.selectedItem)
stock = Game.player:CountEquip(self.selectedItem)
end

Expand Down Expand Up @@ -194,8 +195,8 @@ end
function CommodityMarketWidget:DoBuy()
if not self.funcs.onClickBuy(self, self.selectedItem) then return end

local price = Game.player:GetDockedWith():GetEquipmentPrice(self.selectedItem)
local stock = Game.player:GetDockedWith():GetEquipmentStock(self.selectedItem)
local price = self.funcs.getBuyPrice(self, self.selectedItem)
local stock = self.funcs.getStock(self, self.selectedItem)
local playerfreecargo = Game.player.totalCargo - Game.player.usedCargo
local orderAmount = price * self.tradeAmount

Expand Down Expand Up @@ -224,7 +225,7 @@ function CommodityMarketWidget:DoBuy()
--all checks passed
assert(Game.player:AddEquip(self.selectedItem, self.tradeAmount, "cargo") == self.tradeAmount)
Game.player:AddMoney(-orderAmount) --grab the money
Game.player:GetDockedWith():AddEquipmentStock(self.selectedItem, -self.tradeAmount)
self.funcs.bought(self, self.selectedItem, self.tradeAmount)
self:ChangeTradeAmount(-self.tradeAmount) --reset the trade amount

--update market rows
Expand All @@ -235,13 +236,13 @@ end

--player clicked the confirm sale button
function CommodityMarketWidget:DoSell()
local price = Game.player:GetDockedWith():GetEquipmentPrice(self.selectedItem)
local price = self.funcs.getSellPrice(self, self.selectedItem)
local orderamount = price * self.tradeAmount

--if commodity price is negative (radioactives, garbage), player needs to have enough cash
Game.player:RemoveEquip(self.selectedItem, self.tradeAmount, "cargo")
Game.player:AddMoney(orderamount) --grab the money
Game.player:GetDockedWith():AddEquipmentStock(self.selectedItem, self.tradeAmount)
self.funcs.sold(self, self.selectedItem, self.tradeAmount)
self:ChangeTradeAmount(-self.tradeAmount) --reset the trade amount

--if player sold all his cargo, switch to buy panel
Expand Down
12 changes: 10 additions & 2 deletions data/pigui/libs/equipment-market.lua
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ local defaultFuncs = {

-- do something when we buy this commodity
bought = function (self, e)
Game.player:GetDockedWith():AddEquipmentStock(e, -1)
local count = -1
if self.tradeAmount ~= nil then
count = self.tradeAmount
end
Game.player:GetDockedWith():AddEquipmentStock(e, count)
end,

-- do something when a "sell" button is clicked
Expand Down Expand Up @@ -132,7 +136,11 @@ local defaultFuncs = {

-- do something when we sell this items
sold = function (self, e)
Game.player:GetDockedWith():AddEquipmentStock(e, 1)
local count = -1
if self.tradeAmount ~= nil then
count = self.tradeAmount
end
Game.player:GetDockedWith():AddEquipmentStock(e, count)
end,

initTable = function(self)
Expand Down
Loading

0 comments on commit b1ae9f1

Please sign in to comment.