Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ItemCard UI Helper #5454

Merged
merged 9 commits into from
Dec 25, 2022
37 changes: 36 additions & 1 deletion data/libs/autoload.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ math.clamp = function(v, min, max)
return math.min(max, math.max(v,min))
end

-- linearly interpolate between min and max according to V
math.lerp = function(min, max, v)
return min + (max - min) * v
end

-- calculate the interpolation factor of the given number v relative to min and max
math.invlerp = function(min, max, v)
return (v - min) / (max - min)
end

debug.deprecated = function(name)
local deprecated_function = debug.getinfo(2)
local caller = debug.getinfo(3)
Expand All @@ -38,7 +48,32 @@ end

-- a nice string interpolator
string.interp = function (s, t)
return (s:gsub('(%b{})', function(w) return t[w:sub(2,-2)] or w end))
local i = 0
return (s:gsub('(%b{})', function(w)
if #w > 2 then
return t[w:sub(2, -2)] or w
else
i = i + 1; return t[i] or w
end
end))
end

-- allow using string.interp via "s" % { t }
---@class string
---@operator mod(table): string
getmetatable("").__mod = string.interp

-- make a simple shallow copy of the passed-in table
-- does not copy metatable nor recurse into the table
---@generic T
---@param t T
---@return T
table.copy = function(t)
local ret = {}
for k, v in pairs(t) do
ret[k] = v
end
return ret
end

-- make import break. you should never import this file
Expand Down
14 changes: 14 additions & 0 deletions data/libs/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ end
-- Example:
-- > transformed = utils.map_table(t, function(k, v) return k .. "1", v end)
--
---@generic K, V, K2, V2
---@param table table<K, V>
---@param predicate fun(k: K, v: V): K2, V2
---@return table<K2, V2>
function utils.map_table(table, predicate)
local t = {}
for k, v in pairs(table) do
Expand All @@ -134,6 +138,10 @@ end
-- Example:
-- > transformed = utils.map_array(t, function(v) return v + 32 end)
--
---@generic T, T2
---@param array T[]
---@param predicate fun(v: T): T2
---@return T2[]
function utils.map_array(array, predicate)
local t = {}
for i, v in ipairs(array) do
Expand All @@ -153,6 +161,9 @@ end
-- Example:
-- > filtered = utils.filter_table(t, function (k, v) return true end)
--
---@generic K, V
---@param table table<K, V>
---@param predicate fun(k: K, v: V): boolean
function utils.filter_table(table, predicate)
local t = {}
for k, v in pairs(table) do
Expand All @@ -169,6 +180,9 @@ end
-- Example:
-- > filtered = utils.filter_array(t, function (i, v) return true end)
--
---@generic T
---@param array T[]
---@param predicate fun(v: T): boolean
function utils.filter_array(array, predicate)
local t = {}
for i, v in ipairs(array) do
Expand Down
26 changes: 26 additions & 0 deletions data/meta/Vector.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@
---@class Vector2
---@field x number
---@field y number
---
---@operator add: Vector2
---@operator add(number): Vector2
---@operator sub: Vector2
---@operator sub(number): Vector2
---@operator mul: Vector2
---@operator mul(number): Vector2
---@operator div: Vector2
---@operator div(number): Vector2
---@operator unm: Vector2
---@operator call: Vector2

---@class Vector2
local Vector2 = {}

---@param x number
Expand Down Expand Up @@ -49,6 +62,19 @@ function Vector2:right() end
---@field x number
---@field y number
---@field z number
---
---@operator add: Vector3
---@operator add(number): Vector3
---@operator sub: Vector3
---@operator sub(number): Vector3
---@operator mul: Vector3
---@operator mul(number): Vector3
---@operator div: Vector3
---@operator div(number): Vector3
---@operator unm: Vector3
---@operator call: Vector3

---@class Vector3
local Vector3 = {}

---@param x number
Expand Down
8 changes: 4 additions & 4 deletions data/pigui/libs/buttons.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ function ui.calcButtonSize(label, font, size)
return ui.calcTextSize(label, font, size) + ui.theme.styles.ButtonPadding * 2
end

function ui.getButtonHeight()
return ui.getTextLineHeight() + ui.theme.styles.ButtonPadding.y * 2
function ui.getButtonHeight(font)
return (font and font.size or ui.getTextLineHeight()) + ui.theme.styles.ButtonPadding.y * 2
end

function ui.getButtonHeightWithSpacing()
Expand Down Expand Up @@ -162,9 +162,9 @@ function ui.iconButton(icon, size, tooltip, variant, fg_color, frame_padding, ic
if pigui.IsItemHovered() then
local pos = tooltip:find("##") -- get position for id tag start
if not pos then
pigui.SetTooltip(tooltip)
ui.setTooltip(tooltip)
elseif pos > 1 then
pigui.SetTooltip(string.sub(tooltip, 1, pos - 1))
ui.setTooltip(string.sub(tooltip, 1, pos - 1))
end
end

Expand Down
15 changes: 9 additions & 6 deletions data/pigui/libs/forwarded.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ ui.setNextWindowPos = pigui.SetNextWindowPos ---@type fun(pos: Vector2, cond: st
ui.setNextWindowSize = pigui.SetNextWindowSize ---@type fun(size: Vector2, cond: string)
ui.setNextWindowSizeConstraints = pigui.SetNextWindowSizeConstraints ---@type fun(min: Vector2, max: Vector2)

-- Forwarded as-is for use in complicated layout primitives without introducing additional scopes
ui.beginGroup = pigui.BeginGroup
ui.endGroup = pigui.EndGroup

ui.dummy = pigui.Dummy
ui.newLine = pigui.NewLine
ui.spacing = pigui.Spacing
ui.text = pigui.Text
ui.combo = pigui.Combo
ui.combo = pigui.Combo ---@type fun(label: string, selected: integer, items: string[]): changed: boolean, selected: integer
ui.listBox = pigui.ListBox
ui.textWrapped = pigui.TextWrapped
ui.textColored = pigui.TextColored
Expand Down Expand Up @@ -60,14 +64,13 @@ ui.thrustIndicator = pigui.ThrustIndicator
ui.isMouseClicked = pigui.IsMouseClicked
ui.isMouseDoubleClicked = pigui.IsMouseDoubleClicked
ui.isMouseDown = pigui.IsMouseDown
ui.getMousePos = pigui.GetMousePos
ui.getMousePos = pigui.GetMousePos ---@type fun(): Vector2
ui.getMouseWheel = pigui.GetMouseWheel
--ui.setTooltip = maybeSetTooltip
ui.shouldDrawUI = pigui.ShouldDrawUI
ui.getWindowPos = pigui.GetWindowPos
ui.getWindowSize = pigui.GetWindowSize
ui.getWindowPos = pigui.GetWindowPos ---@type fun(): Vector2
ui.getWindowSize = pigui.GetWindowSize ---@type fun(): Vector2
-- available content region
ui.getContentRegion = pigui.GetContentRegion
ui.getContentRegion = pigui.GetContentRegion ---@type fun(): Vector2

-- Get the current height of a line of text (font.size)
ui.getTextLineHeight = pigui.GetTextLineHeight
Expand Down
Loading