Skip to content

Commit

Permalink
Merge pull request pioneerspacesim#5937 from Web-eWorks/microfixes
Browse files Browse the repository at this point in the history
Misc. improvements
  • Loading branch information
Webster Sheets authored Oct 13, 2024
2 parents b9917dd + 73671cf commit 2884328
Show file tree
Hide file tree
Showing 17 changed files with 319 additions and 40 deletions.
4 changes: 4 additions & 0 deletions data/lang/core/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1843,6 +1843,10 @@
"description": "Duration unit: one 7-day week",
"message": "w"
},
"UNIT_CUBIC_METERS": {
"description": "Volume unit: cubic meter",
"message": ""
},
"UNKNOWN": {
"description": "",
"message": "<unknown>"
Expand Down
2 changes: 2 additions & 0 deletions data/libs/Event.lua
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ Event.New = function()
--
-- stable
--
---@param name string
---@param cb function
self.Register = function (name, cb)
super.Register(self, name, package.modulename(2), cb)
end
Expand Down
24 changes: 18 additions & 6 deletions data/libs/autoload.lua
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,15 @@ end
---@param predicate nil|fun(k: K, v: V): any, any
---@return table
table.merge = function(a, b, predicate)
for k, v in pairs(b) do
if predicate then k, v = predicate(k, v) end
a[k] = v
if predicate then
for k, v in pairs(b) do
k, v = predicate(k, v)
a[k] = v
end
else
for k, v in pairs(b) do
a[k] = v
end
end
return a
end
Expand All @@ -102,9 +108,15 @@ end
---@param predicate nil|fun(v: T): any
---@return table
table.append = function(a, b, predicate)
for _, v in ipairs(b) do
if predicate then v = predicate(v) end
table.insert(a, v)
if predicate then
for _, v in ipairs(b) do
v = predicate(v)
table.insert(a, v)
end
else
for _, v in ipairs(b) do
table.insert(a, v)
end
end
return a
end
Expand Down
68 changes: 66 additions & 2 deletions data/libs/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,58 @@ utils.find_if = function(t, predicate)
return nil
end

--
-- Function: best_score
--
-- Returns the value in the passed table with the highest score according to the passed scoring function
--
-- Iteration order is undefined (uses pairs() internally).
--
---@generic K, V
---@param t table<K, V>
---@param score_fun fun(k: K, v: V): number?
---@return V?, number
utils.best_score = function(t, score_fun)
local score = 0.0
local best_val = nil

for k, v in pairs(t) do
local new_score = score_fun(k, v)
if new_score and new_score > score then
score = new_score
best_val = v
end
end

return best_val, score
end

--
-- Function: least_score
--
-- Returns the value in the passed table with the lowest score according to the passed scoring function
--
-- Iteration order is undefined (uses pairs() internally).
--
---@generic K, V
---@param t table<K, V>
---@param score_fun fun(k: K, v: V): number?
---@return V?, number
utils.least_score = function(t, score_fun)
local score = math.huge
local best_val = nil

for k, v in pairs(t) do
local new_score = score_fun(k, v)
if new_score and new_score < score then
score = new_score
best_val = v
end
end

return best_val, score
end

--
-- Function: stable_sort
--
Expand Down Expand Up @@ -326,7 +378,7 @@ end
--
local object = {}

object.meta = { __index = object, class="object" }
object.meta = { __index = object, class="object", inherits = { ["object"] = true } }

--
-- Function: New
Expand Down Expand Up @@ -390,7 +442,10 @@ end
utils.inherits = function (baseClass, name)
local new_class = {}
local base_class = baseClass or object
new_class.meta = { __index = new_class, class=name }
new_class.meta = { __index = new_class, class=name, inherits = { [name] = true } }

-- Allow quick lookup by parent class
table.merge(new_class.meta.inherits, base_class.meta.inherits)

-- generic constructor
function new_class.New(...)
Expand All @@ -406,6 +461,10 @@ utils.inherits = function (baseClass, name)
return new_class
end

function new_class:IsA(typename)
return new_class.meta.inherits[typename]
end

function new_class.Unserialize(data)
local tmp = base_class.Unserialize(data)
setmetatable(tmp, new_class.meta)
Expand Down Expand Up @@ -494,6 +553,11 @@ utils.proto = function(classname)
return newProto
end

-- Return a copy of a with the values from b added to it
function utils.mixin(a, b)
return table.merge(table.copy(a), b)
end

--
-- Function: print_r
--
Expand Down
15 changes: 15 additions & 0 deletions data/meta/CoreObject/Ship.meta.lua
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,18 @@ function Ship:GetShieldsPercent() end
-- Sets the thruster fuel tank of the ship to the given percentage of its maximum.
---@param percent number
function Ship:SetFuelPercent(percent) end

-- Update ship properties after changing ship equipment or cargo
function Ship:UpdateEquipStats() end

-- Is this ship currently docked with anything?
---@return boolean
function Ship:IsDocked() end

-- Is this ship currently landed on a planet?
---@return boolean
function Ship:IsLanded() end

-- Get the starport this ship is docked with, if any
---@return SpaceStation?
function Ship:GetDockedWith() end
5 changes: 5 additions & 0 deletions data/meta/Engine.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ local Engine = {}

-- TODO: add information about Engine methods

-- Get a model file by name
---@param name string
---@return SceneGraph.Model model
function Engine.GetModel(name) end

return Engine
23 changes: 23 additions & 0 deletions data/meta/Model.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
-- Copyright © 2008-2024 Pioneer Developers. See AUTHORS.txt for details
-- Licensed under the terms of the GPL v3. See licenses/GPL-3.txt

-- This file implements type information about C++ classes for Lua static analysis

---@meta

---@class SceneGraph.Model
---
---@field name string
---@field pattern integer Current pattern applied to the model
---@field numPatterns integer Number of patterns supported by the model

---@class SceneGraph.Model
local Model = {}

-- Set the pattern currently applied to the model
---@param idx integer
function Model:SetPattern(idx) end

-- Set debug flags used when rendering this model
---@param flags ModelDebugFlags[]
function Model:SetDebugFlags(flags) end
8 changes: 6 additions & 2 deletions data/modules/MissionUtils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ local utils = require "utils"
local AU = 149598000000
local AU_sqrt = math.sqrt(AU)

local Days = 24*60*60
local Hours = 60*60
local Days = 24*Hours
local Weeks = 7*Days

local MissionUtils = {
AU = AU,
Days = Days
Days = Days,
Hours = Hours,
Weeks = Weeks,
}

---@class MissionUtils.Calculator
Expand Down
16 changes: 10 additions & 6 deletions data/pigui/libs/buttons.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ end
--
-- Function: ui.button
--
-- > clicked = ui.button(label, button_size, variant, tooltip)
-- > clicked = ui.button(label, button_size, variant, tooltip, padding)
--
-- Example:
--
Expand All @@ -74,27 +74,31 @@ end
-- variant - [optional] Table, color variants used for this button;
-- contains Color fields 'normal', 'hovered', and 'active'
-- tooltip - [optional] string, mouseover text
-- padding - [optional] Vector2, size of padding on each side of button
--
-- Returns:
--
-- clicked - true if button was clicked
--
function ui.button(label, button_size, variant, tooltip)
function ui.button(label, button_size, variant, tooltip, padding)
if variant then
pigui.PushStyleColor("Button", variant.normal)
pigui.PushStyleColor("ButtonHovered", variant.hovered)
pigui.PushStyleColor("ButtonActive", variant.active)
end

pigui.PushStyleVar("FramePadding", ui.theme.styles.ButtonPadding)
pigui.PushStyleVar("FramePadding", padding or ui.theme.styles.ButtonPadding)
local res = pigui.Button(label, button_size or Vector2(0, 0))
pigui.PopStyleVar(1)

if variant then
pigui.PopStyleColor(3)
end

if pigui.IsItemHovered() and tooltip then pigui.SetTooltip(tooltip) end
if tooltip then
ui.setItemTooltip(tooltip)
end

return res
end

Expand Down Expand Up @@ -185,8 +189,8 @@ function ui.iconButton(id, icon, tooltip, variant, size, padding, flags)
pigui:PopFont()
end

if tooltip and pigui.IsItemHovered() then
ui.setTooltip(tooltip)
if tooltip then
ui.setItemTooltip(tooltip)
end

return ret
Expand Down
5 changes: 3 additions & 2 deletions data/pigui/libs/forwarded.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ ui.setScrollHereY = pigui.SetScrollHereY
ui.selectable = pigui.Selectable
ui.progressBar = pigui.ProgressBar
ui.plotHistogram = pigui.PlotHistogram
ui.setTooltip = pigui.SetTooltip
ui.setItemTooltip = pigui.SetItemTooltip
-- ui.setTooltip = pigui.SetTooltip
-- ui.setItemTooltip = pigui.SetItemTooltip
ui.addCircle = pigui.AddCircle
ui.addCircleFilled = pigui.AddCircleFilled
ui.addRect = pigui.AddRect ---@type fun(a: Vector2, b: Vector2, col: Color, rounding: number, edges: integer, thickness: number)
Expand Down Expand Up @@ -106,6 +106,7 @@ ui.isMouseDoubleClicked = pigui.IsMouseDoubleClicked
ui.isMouseHoveringRect = pigui.IsMouseHoveringRect
ui.collapsingHeader = pigui.CollapsingHeader
ui.treeNode = pigui.TreeNode
ui.treePush = pigui.TreePush
ui.treePop = pigui.TreePop
ui.beginPopupModal = pigui.BeginPopupModal
ui.endPopup = pigui.EndPopup
Expand Down
6 changes: 3 additions & 3 deletions data/pigui/libs/icons.lua
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function ui.addIcon(position, icon, color, size, anchor_horizontal, anchor_verti
end
if tooltip and (ui.isMouseHoveringWindow() or not ui.isAnyWindowHovered()) and tooltip ~= "" then
if pigui.IsMouseHoveringRect(pos, pos + size, true) then
ui.maybeSetTooltip(tooltip)
ui.setTooltip(tooltip)
end
end
return size
Expand Down Expand Up @@ -155,7 +155,7 @@ function ui.addWideIcon(position, icon, color, size, anchor_horizontal, anchor_v
end
if tooltip and (ui.isMouseHoveringWindow() or not ui.isAnyWindowHovered()) and tooltip ~= "" then
if pigui.IsMouseHoveringRect(pos, pos + size, true) then
ui.maybeSetTooltip(tooltip)
ui.setTooltip(tooltip)
end
end

Expand Down Expand Up @@ -185,7 +185,7 @@ function ui.addIconSimple(pos, icon, size, color, tooltip)

if tooltip and (ui.isMouseHoveringWindow() or not ui.isAnyWindowHovered()) then
if pigui.IsMouseHoveringRect(pos, pos + size, true) then
ui.maybeSetTooltip(tooltip)
ui.setTooltip(tooltip)
end
end
end
Expand Down
22 changes: 19 additions & 3 deletions data/pigui/libs/text.lua
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ ui.addStyledText = function(position, anchor_horizontal, anchor_vertical, text,

if tooltip and (ui.isMouseHoveringWindow() or not ui.isAnyWindowHovered()) and tooltip ~= "" then
if pigui.IsMouseHoveringRect(position, position + size, true) then
ui.maybeSetTooltip(tooltip)
ui.setTooltip(tooltip)
end
end

Expand All @@ -446,12 +446,28 @@ end
-- tooltip - string, tooltip text to display to the user
-- font - optional font table, used to display the given tooltip
--
function ui.maybeSetTooltip(tooltip, font)
function ui.setTooltip(tooltip, font)
if not Input.GetMouseCaptured() then
ui.withFont(font or ui.fonts.pionillium.details, function()
pigui.SetTooltip(tooltip)
end)
end
end

ui.setTooltip = ui.maybeSetTooltip
--
-- Function: setItemTooltip
--
-- Displays a tooltip in the UI if the last submitted "item" is hovered with a short delay.
-- The function does not display a tooltip if the mouse is currently captured by the game.
--
-- Parameters:
-- tooltip - string, tooltip text to display to the user
-- font - optional font table, used to display the given tooltip
--
function ui.setItemTooltip(tooltip, font)
if not Input.GetMouseCaptured() then
ui.withFont(font or ui.fonts.pionillium.details, function()
pigui.SetItemTooltip(tooltip)
end)
end
end
Loading

0 comments on commit 2884328

Please sign in to comment.