Skip to content

Commit

Permalink
Reduce lag when hovering saved games list
Browse files Browse the repository at this point in the history
- Use ImGui tooltip delay function to avoid parsing the savefile unless the user really wants to see the hover tooltip
- Expose SetItemTooltip / ImGuiHoveredFlags functionality
  • Loading branch information
sturnclaw committed Oct 6, 2023
1 parent ac9f7ea commit b1bd8a1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
1 change: 1 addition & 0 deletions data/pigui/libs/forwarded.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ ui.selectable = pigui.Selectable
ui.progressBar = pigui.ProgressBar
ui.plotHistogram = pigui.PlotHistogram
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
30 changes: 15 additions & 15 deletions data/pigui/modules/saveloadgame.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ local function getSaveTooltip(name)
else
ret = ret .. "\n" .. lc.SHIP .. ": " .. lc.UNKNOWN
end


if stats.flight_state then
ret = ret .. "\n"..lui.FLIGHT_STATE..": "
ret = ret .. (rawget(lc, string.upper(stats.flight_state)) or
Expand All @@ -75,7 +75,7 @@ local function getSaveTooltip(name)

if stats.docked_at then ret = ret .. "\n"..lui.DOCKED_AT..": " .. stats.docked_at end
if stats.frame then ret = ret .. "\n"..lui.VICINITY_OF..": " .. stats.frame end

saveFileCache[name].ret = ret
return ret
end
Expand All @@ -84,7 +84,7 @@ local function shouldDisplayThisSave(f)
if(string.len(searchSave) < minSearchTextLength) then
return true
end

return not caseSensitive and string.find(string.lower(f.name), string.lower(searchSave), 1, true) ~= nil or
string.find(f.name, searchSave, 1, true) ~= nil
end
Expand All @@ -94,9 +94,9 @@ local function displaySave(f)
selectedSave = f.name
saveIsValid = pcall(Game.SaveGameStats, f.name)
end
if Engine.pigui.IsItemHovered() then
local tooltip = getSaveTooltip(f.name)
Engine.pigui.SetTooltip(tooltip)

if ui.isItemHovered("ForTooltip") then
ui.setTooltip(getSaveTooltip(f.name))
end

ui.nextColumn()
Expand Down Expand Up @@ -170,24 +170,24 @@ end
ui.saveLoadWindow = ModalWindow.New("LoadGame", function()
local saving = ui.saveLoadWindow.mode == "SAVE"
local searchTextSize = ui.calcTextSize(searchText, pionillium.medium.name, pionillium.medium.size)

local txt_width = winSize.x - (ui.getWindowPadding().x + optionButtonSize.x + ui.getItemSpacing().x) * 2

drawSearchHeader(txt_width)

ui.separator()

local saveFilesSearchHeaderHeight = (searchTextSize.y * 2 + ui.getItemSpacing().y * 2 + ui.getWindowPadding().y * 2)
local saveFilesChildWindowHeight = (optionButtonSize.y + (saving and searchTextSize.y or 0) + ui.getItemSpacing().y * 2 + ui.getWindowPadding().y * 2)

local saveFilesChildWindowSize = Vector2(0, (winSize.y - saveFilesChildWindowHeight) - saveFilesSearchHeaderHeight)

ui.child("savefiles", saveFilesChildWindowSize, function()
showSaveFiles()
end)

ui.separator()

-- a little padding just before the window border, so that the cancel button will not be cut out
txt_width = txt_width / 1.03
if saving then
Expand All @@ -207,4 +207,4 @@ end)

ui.saveLoadWindow.mode = "LOAD"

return {}
return {}
19 changes: 16 additions & 3 deletions src/lua/LuaPiGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,11 @@ static Type parse_imgui_flags(lua_State *l, int index, LuaFlags<Type> &lookupTab
theFlags = static_cast<Type>(lua_tointeger(l, index));
} else if (lua_istable(l, index)) {
theFlags = lookupTable.LookupTable(l, index);
} else if (lua_isstring(l, index)) {
theFlags = lookupTable.LookupEnum(l, index);
} else {
luaL_traceback(l, l, NULL, 1);
Error("Expected a table or integer, got %s.\n%s\n", luaL_typename(l, index), lua_tostring(l, -1));
Error("Expected a table, string, or integer, got %s.\n%s\n", luaL_typename(l, index), lua_tostring(l, -1));
}
return theFlags;
}
Expand Down Expand Up @@ -383,7 +385,8 @@ static LuaFlags<ImGuiHoveredFlags_> imguiHoveredFlagsTable = {
{ "AllowWhenBlockedByActiveItem", ImGuiHoveredFlags_AllowWhenBlockedByActiveItem },
{ "AllowWhenOverlapped", ImGuiHoveredFlags_AllowWhenOverlapped },
{ "AllowWhenDisabled", ImGuiHoveredFlags_AllowWhenDisabled },
{ "RectOnly", ImGuiHoveredFlags_RectOnly }
{ "RectOnly", ImGuiHoveredFlags_RectOnly },
{ "ForTooltip", ImGuiHoveredFlags_ForTooltip }
};

void pi_lua_generic_pull(lua_State *l, int index, ImGuiHoveredFlags_ &theflags)
Expand Down Expand Up @@ -1740,7 +1743,8 @@ static int l_pigui_end_child(lua_State *l)
static int l_pigui_is_item_hovered(lua_State *l)
{
PROFILE_SCOPED()
LuaPush(l, ImGui::IsItemHovered());
int flags = LuaPull<ImGuiHoveredFlags_>(l, 1, ImGuiHoveredFlags_None);
LuaPush(l, ImGui::IsItemHovered(flags));
return 1;
}

Expand Down Expand Up @@ -1861,6 +1865,14 @@ static int l_pigui_set_tooltip(lua_State *l)
return 0;
}

static int l_pigui_set_item_tooltip(lua_State *l)
{
PROFILE_SCOPED()
std::string text = LuaPull<std::string>(l, 1);
ImGui::SetItemTooltip("%s", text.c_str());
return 0;
}

static int l_pigui_begin_tooltip(lua_State *l)
{
PROFILE_SCOPED()
Expand Down Expand Up @@ -3299,6 +3311,7 @@ void LuaObject<PiGui::Instance>::RegisterClass()
{ "PopFont", l_pigui_pop_font },
{ "CalcTextSize", l_pigui_calc_text_size },
{ "SetTooltip", l_pigui_set_tooltip },
{ "SetItemTooltip", l_pigui_set_item_tooltip },
{ "BeginTooltip", l_pigui_begin_tooltip },
{ "EndTooltip", l_pigui_end_tooltip },
{ "Checkbox", l_pigui_checkbox },
Expand Down

0 comments on commit b1bd8a1

Please sign in to comment.