Skip to content

Commit

Permalink
List widget (will be parent to table and market widgets as well)
Browse files Browse the repository at this point in the history
Registering a tab view overwrites existing ones with the same id
  • Loading branch information
vakhoir committed Feb 23, 2020
1 parent 3e30b1c commit 7015838
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 139 deletions.
155 changes: 17 additions & 138 deletions data/pigui/libs/debug.lua
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
-- Copyright © 2008-2019 Pioneer Developers. See AUTHORS.txt for details
-- Licensed under the terms of the GPL v3. See licenses/GPL-3.txt

local Lang = import 'Lang'
local Game = import 'Game'
local Format = import 'Format'
local SpaceStation = import 'SpaceStation'

local StationView = import 'pigui/views/station-view'
local Table = import 'pigui/libs/table.lua'
local ChatForm = import 'pigui/libs/chat-form.lua'
local ModalWindow = import 'pigui/libs/modal-win.lua'
local PiImage = import 'ui/PiImage'

local ui = import 'pigui/pigui.lua'
local Lang = require 'Lang'
local Game = require 'Game'
local Format = require 'Format'
local SpaceStation = require 'SpaceStation'

local StationView = require 'pigui.views.station-view'
local Table = require 'pigui.libs.table'
local ChatForm = require 'pigui.libs.chat-form'
local ModalWindow = require 'pigui.libs.modal-win'
local List = require 'pigui.libs.list'
local PiImage = require 'ui.PiImage'

local ui = require 'pigui'
local pionillium = ui.fonts.pionillium
local orbiteer = ui.fonts.orbiteer
local l = Lang.GetResource("core")
Expand Down Expand Up @@ -50,132 +51,12 @@ local function adActive(ref, ad)
return not ((type(ad.isEnabled) == "function" and not ad.isEnabled(ref)) or Game.paused)
end

local defaultFuncs = {

initTable = function(self)
end,

onMouseOverItem = function(self, item)
end,

onClickItem = function (self, e)

end,

renderItem = function(self, item)

end,

itemFrame = function(self, item, min, max)

end,

-- sort items in the market table
sortingFunction = function(e1,e2)
return e1 < e2
end
}

local List = {}

function List.New(id, title, config)
local defaultSizes = ui.rescaleUI({
windowPadding = Vector2(14, 14),
itemSpacing = Vector2(4, 9),
}, Vector2(1600, 900))

local self
self = {
scroll = 0,
id = id,
title = title,
items = {},
itemsMeta = {},
size = config.size or Vector2(ui.screenWidth / 2,0),
flags = config.flags or ui.WindowFlags {"AlwaysUseWindowPadding"},
style = {
titleFont = config.titleFont or ui.fonts.orbiteer.xlarge,
highlightColor = config.highlightColor or Color(0,63,112),
styleVars = {
WindowPadding = config.windowPadding or defaultSizes.windowPadding,
ItemSpacing = config.itemSpacing or defaultSizes.itemSpacing,
},
styleColors = {},
},
funcs = {
beforeItems = config.beforeItems or function() end,
canDisplayItem = config.canDisplayItem or defaultFuncs.canDisplayItem,
beforeRenderItem = config.beforeRenderItem or function() end,
renderItem = config.renderItem or defaultFuncs.renderItem,
afterRenderItem = config.afterRenderItem or function() ui.dummy(vZero) end,
onMouseOverItem = config.onMouseOverItem or defaultFuncs.onMouseOverItem,
onClickItem = config.onClickItem or defaultFuncs.onClickItem,
sortingFunction = config.sortingFunction or defaultFuncs.sortingFunction,
},
}

setmetatable(self, {
__index = List,
class = "UI.List",
})

return self
end

function List:render()
ui.withStyleColorsAndVars(self.style.styleColors, self.style.styleVars, function()
ui.child("List##" .. self.id, self.size, self.flags, function()
local startPos
local endPos

local contentRegion = ui.getContentRegion()

self.funcs.beforeItems(self)

self.highlightStart = nil
self.highlightEnd = nil

for key, item in pairs(self.items) do
self.funcs.beforeRenderItem(self, item, key)

startPos = ui.getCursorScreenPos()
startPos.x = startPos.x - self.style.styleVars.WindowPadding.x / 2

self.funcs.renderItem(self, item, key)

endPos = ui.getCursorScreenPos()
endPos.x = endPos.x + contentRegion.x + self.style.styleVars.WindowPadding.x / 2

self.funcs.afterRenderItem(self, item, key)

if self.itemsMeta[key] == nil then
self.itemsMeta[key] = {}
end

self.itemsMeta[key].min = startPos
self.itemsMeta[key].max = endPos

if ui.isWindowHovered() and ui.isMouseHoveringRect(startPos, endPos, false) then
self.funcs.onMouseOverItem(self, item, key)
if ui.isMouseClicked(0) then
self.funcs.onClickItem(self, item, key)
end

self.highlightStart = startPos
self.highlightEnd = endPos
end
end
end)
end)
end

local
jobList = List.New("JobList", false, {
local jobList = List.New("JobList", false, {
size = Vector2(ui.screenWidth * 0.5, 0),
flags = ui.WindowFlags {"AlwaysUseWindowPadding", "NoScrollbar"},
style = {
windowPadding = widgetSizes.innerPadding,
--itemSpacing = Vector2(6, 20),
flags = ui.WindowFlags {"AlwaysUseWindowPadding", "NoScrollbar"},
},
beforeItems = function(self)
--scrollPos = math.ceil(-scrollPos * ui.getScrollMaxY())
Expand All @@ -190,7 +71,7 @@ jobList = List.New("JobList", false, {
scrollPosPrev = scrollPos
end,
beforeRenderItem = function(self, item, key)
if self.itemsMeta[key] ~= nil then
if self.itemsMeta and self.itemsMeta[key] ~= nil then
ui.addRectFilled(self.itemsMeta[key].min, self.itemsMeta[key].max, Color(8, 19, 40, 230), 0, 0)
ui.addRect(self.itemsMeta[key].min, self.itemsMeta[key].max, Color(25, 64, 90, 230), 0, 0, 2)
end
Expand Down Expand Up @@ -274,7 +155,7 @@ local function drawCommoditytView()
ui.sameLine()
ui.text("Type none")
ui.pushTextWrapPos(textWrapWidth)
jobList:render()
jobList:Render()
--print("end", scrollPos)
ui.popTextWrapPos()
end)
Expand All @@ -291,8 +172,6 @@ local function drawCommoditytView()
end)
end)
end)

StationView:shipSummary()
end)
end

Expand Down
150 changes: 150 additions & 0 deletions data/pigui/libs/list.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
-- Copyright © 2008-2020 Pioneer Developers. See AUTHORS.txt for details
-- Licensed under the terms of the GPL v3. See licenses/GPL-3.txt

local ui = require 'pigui'
local ModalWindow = require 'pigui.libs.modal-win'

local vZero = Vector2(0, 0)

local defaultFuncs = {
beforeItems = function()

end,
canDisplayItem = function()

end,
beforeRenderItem = function()

end,
renderItem = function()

end,
afterRenderItem = function()

end,
onMouseOverItem = function()

end,
onClickItem = function()

end,
afterItems = function()

end,

-- sort items in the market table
sortingFunction = function(e1,e2)
return e1 < e2
end
}

local ListWidget = {}

function ListWidget.New(id, title, config)
local defaultSizes = ui.rescaleUI({
padding = Vector2(14, 14),
itemSpacing = Vector2(4, 9),
}, Vector2(1600, 900))

local self
self = {
scroll = 0,
id = id,
popup = config.popup or ModalWindow.New('popupMsg' .. id, function()
ui.text(self.popup.msg)
ui.dummy(Vector2((ui.getContentRegion().x - 100) / 2, 0))
ui.sameLine()
if ui.button("OK", Vector2(100, 0)) then
self.popup:close()
end
end),
title = title,
items = {},
scrollReset = false,
itemTypes = config.itemTypes or {},
columnCount = config.columnCount or 0,
style = {
flags = config.style.flags or ui.WindowFlags {"AlwaysUseWindowPadding"},
size = config.style.size or Vector2(ui.screenWidth / 2,0),
titleFont = config.style.titleFont or ui.fonts.orbiteer.xlarge,
highlightColor = config.style.highlightColor or Color(0,63,112),
styleColors = config.style.styleColors or {},
styleVars = config.style.styleVars or {
WindowPadding = config.style.padding or defaultSizes.padding,
ItemSpacing = config.style.itemSpacing or defaultSizes.itemSpacing,
},
},
funcs = {
beforeItems = config.beforeItems or function() end,
canDisplayItem = config.canDisplayItem or defaultFuncs.canDisplayItem,
beforeRenderItem = config.beforeRenderItem or function() end,
renderItem = config.renderItem or defaultFuncs.renderItem,
afterRenderItem = config.afterRenderItem or function() ui.dummy(vZero) end,
onMouseOverItem = config.onMouseOverItem or defaultFuncs.onMouseOverItem,
onClickItem = config.onClickItem or defaultFuncs.onClickItem,
afterItems = config.afterItems or function() end,
sortingFunction = config.sortingFunction or defaultFuncs.sortingFunction,
},
}

setmetatable(self, {
__index = ListWidget,
class = "UI.ListWidget",
})

return self
end

function ListWidget:Render()
ui.withStyleColorsAndVars(self.style.styleColors, self.style.styleVars, function()
ui.child("List##" .. self.id, self.style.size, self.style.flags, function()
local startPos
local endPos

local contentRegion = ui.getContentRegion()

self.funcs.beforeItems(self)

self.highlightStart = nil
self.highlightEnd = nil

for key, item in pairs(self.items) do
self.funcs.beforeRenderItem(self, item, key)

startPos = ui.getCursorScreenPos()
startPos.x = startPos.x - self.style.styleVars.WindowPadding.x / 2

self.funcs.renderItem(self, item, key)

endPos = ui.getCursorScreenPos()
endPos.x = endPos.x + contentRegion.x + self.style.styleVars.WindowPadding.x / 2

self.funcs.afterRenderItem(self, item, key)

if self.itemsMeta == nil then
self.itemsMeta = {}
end

if self.itemsMeta[key] == nil then
self.itemsMeta[key] = {}
end

self.itemsMeta[key].min = startPos
self.itemsMeta[key].max = endPos

if ui.isWindowHovered() and ui.isMouseHoveringRect(startPos, endPos, false) then
self.funcs.onMouseOverItem(self, item, key)
if ui.isMouseClicked(0) then
self.funcs.onClickItem(self, item, key)
end

self.highlightStart = startPos
self.highlightEnd = endPos
end
end
end)
end)
end


return ListWidget
2 changes: 1 addition & 1 deletion data/pigui/modules/info-view/04-missions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ view = {
Debug:render()
end,
refresh = function()
Debug = import 'pigui/libs/debug'
Debug = require 'pigui.libs.debug'
view.showView = Debug.showView
Debug.scrollPos = 0
end,
Expand Down
8 changes: 8 additions & 0 deletions data/pigui/views/tab-view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ function PiGuiTabView.resize(self)
end

function PiGuiTabView.registerView(self, view)
for i, v in ipairs(self.tabs) do
if v.id == view.id then
self.tabs[i] = view
self:resize()
return
end
end

table.insert(self.tabs, view)
self.viewCount = self.viewCount + 1
self:resize()
Expand Down

0 comments on commit 7015838

Please sign in to comment.