Skip to content

Commit

Permalink
Item ID tooltip
Browse files Browse the repository at this point in the history
  • Loading branch information
Silverfeelin committed Jun 21, 2019
1 parent 7470c24 commit 9cee74e
Show file tree
Hide file tree
Showing 11 changed files with 276 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .metadata
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"QuickbarMini",
"StardustLib"
],
"version" : "1.4.0",
"version" : "1.4.1",
"link" : "steam://url/CommunityFilePage/733665104",
"steamContentId" : "733665104"
}
29 changes: 27 additions & 2 deletions interface/sip/sip.config
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,14 @@
"vAnchor": "bottom",
"zlevel": 22,
"value": "^white;^shadow;No item selected.",
"visible": true
"visible": true,
"data": {
"tooltip": {
"callback": "itemTooltip",
"direction": "top",
"position": [255, 197]
}
}
},
"sipLabelSelectionDescription": {
"type": "label",
Expand Down Expand Up @@ -1128,10 +1135,23 @@
]
}
}
},
"tooltipCanvas": {
"type": "canvas",
"rect": [0, 0, 337, 244],
"captureMouseEvents": false,
"mouseTransparent": true,
"zlevel": 99
},
"tooltipLabel": {
"type": "label",
"hAnchor": "mid",
"vAnchor": "mid",
"visible": false
}
},

"scripts": ["/scripts/spawnableItemPack.lua"],
"scripts": ["/scripts/spawnableItemPack.lua", "/scripts/sip/tooltip/tooltip.lua"],
"scriptDelta": 1,

"scriptWidgetCallbacks": [
Expand Down Expand Up @@ -1241,5 +1261,10 @@
"base": "/interface/sip/color.png?replace;000000=00AE2C",
"hover": "/interface/sip/color.png?replace;000000=00AE2C",
"pressed": "/interface/sip/color.png?replace;000000=00AE2C"
},

"tooltip": {
"canvas": "tooltipCanvas",
"label": "tooltipLabel"
}
}
Binary file added interface/tooltip/tabdown.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added interface/tooltip/tableft.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added interface/tooltip/tabright.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added interface/tooltip/tabup.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 47 additions & 0 deletions scripts/sip/tooltip/scriptHooks.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
-- Prevent loading if another scriptHooks has been loaded (i.e. same script in a different folder)
if hook then return end

local hooks = {}

--- Loops over all functions in hook.
-- Returns the first non-nil value after calling all hooks.
local function loop(hook, ...)
local ret
for k,v in pairs(hook) do
if v then
local r = k(...)
if type(ret) == "nil" then ret = r end
end
end
return ret
end

--- Creates a new empty hook for the given global function.
-- The return hook is a key-based table for functions to call.
local function createHook(name)
if hooks[name] then return hooks[name] end
local hook = {}
hooks[name] = hook

if _ENV[name] then
local old = _ENV[name]
_ENV[name] = function(...)
old(...)
return loop(hook, ...)
end
else
_ENV[name] = function(...)
return loop(hook, ...)
end
end

return hook
end

--- Hook a function to the global function.
-- @param name Global function name.
-- @param func Function to hook.
function hook(name, func)
local hook = createHook(name)
hook[func] = true
end
11 changes: 11 additions & 0 deletions scripts/sip/tooltip/tooltip.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"default": {
"color": "white",
"background": "#1E1E1E",
"border": "#007F0E",
"borderWidth": 1,
"direction": "top",
"padding": 2,
"margin": 2
}
}
173 changes: 173 additions & 0 deletions scripts/sip/tooltip/tooltip.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
-- Prevent loading if another scriptHooks has been loaded (i.e. same script in a different folder)
if tooltip then return end
require "/scripts/sip/tooltip/scriptHooks.lua"
require "/scripts/sip/tooltip/tooltip_util.lua"
require "/scripts/vec2.lua"

tooltip = {}
tooltip.util = tooltipUtil

--- Initializes the tooltip library.
function tooltip.init()
tooltip.paneConfig = config.getParameter("tooltip", { canvas = "tooltipCanvas", label = "tooltipLabel" })
tooltip.config = root.assetJson("/scripts/sip/tooltip/tooltip.config")

tooltip.canvas = widget.bindCanvas(tooltip.paneConfig.canvas)
tooltip.label = tooltip.paneConfig.label
end

--- Draws a tooltip (if any).
-- Prevents drawing the same tooltip multiple times.
-- @param screenPosition Screen position.
function tooltip.cursorOverride(screenPosition)
local w = widget.getChildAt(screenPosition)
local t, pos
local tipped = false
if w then
w = w:sub(2)
local d = widget.getData(w)
if type(d) == "table" and type(d.tooltip) == "table" then
t = d.tooltip
end
end

if t and tooltip.previousWidget ~= w then
-- Only draw new tooltip if w is different.
tooltip.canvas:clear()
pos = t.position or widget.getPosition(w)
local size = widget.getSize(w)

tooltip.draw(t, pos, size)
elseif not t and tooltip.previousWidget then
-- Clear tooltip when no longer hovering.
tooltip.canvas:clear()
end
tooltip.previousWidget = w
end

--- Draws a tooltip next to the given widget.
-- @param ttip Tooltip data.
-- @param wPos Target widget position.
-- @param wSize Target widget size.
function tooltip.draw(ttip, wPos, wSize)
local c = tooltip.canvas

local text = tooltip.getText(ttip)
if text == "" then return end

widget.setText(tooltip.label, text)
local labelSize = widget.getSize(tooltip.label)

-- Params
local margin = tooltip.parameter(ttip, "margin")
local padding = tooltip.parameter(ttip, "padding")
local border = tooltip.parameter(ttip, "border")
local background = tooltip.parameter(ttip, "background")
local borderWidth = tooltip.parameter(ttip, "borderWidth")
local position = tooltip.parameter(ttip, "position")
local fontSize = tooltip.parameter(ttip, "fontSize") or 7
local p = position or tooltip.offset(wPos, wSize, labelSize, ttip.direction or "top", margin + padding)

-- Label rect
local rect = {
p[1] - labelSize[1]/2,
p[2] - labelSize[2]/2,
p[1] + labelSize[1]/2,
p[2] + labelSize[2]/2
}

-- Border
local padRect = tooltipUtil.growRect(rect, padding)
c:drawRect(padRect, border)

-- Background
padRect = tooltipUtil.growRect(padRect, -borderWidth)
c:drawRect(padRect, background)

-- Arrow
local tabPos, tabRot = tooltip.drawTab(ttip, p, vec2.add(labelSize,padding * 2))

-- Text
c:drawText(text, {position = p, horizontalAnchor = "mid", verticalAnchor = "mid"}, fontSize, tooltip.parameter(ttip, "color"))
end

--- Gets the text for a tooltip.
-- @param Tooltip.
-- @return Tooltip text, callback return value or an empty string.
function tooltip.getText(ttip)
return ttip.text or ttip.callback and _ENV[ttip.callback]() or ""
end

--- Gets a parameter from the tooltip, or the default value.
-- @param ttip Tooltip.
-- @param param Tooltip parameter name.
function tooltip.parameter(ttip, param)
return ttip[param] or tooltip.config.default[param]
end

-- Draws a small arrow pointing to the widget.
-- @param ttip Tooltip
-- @param tPos Center of tooltip.
-- @param tSize Size of tooltip (border rect)
function tooltip.drawTab(ttip, tPos, tSize)
local border = tooltip.parameter(ttip, "border")
local background = tooltip.parameter(ttip, "background")

local pos = {}
local dir = string.format("?replace;ffffff=%s;000000=%s", background:sub(2), border:sub(2))
local tab

if ttip.direction == "left" then -- Right
pos[1] = tPos[1] + tSize[1] / 2
pos[2] = tPos[2]
tab = "/interface/tooltip/tabright.png"
elseif ttip.direction == "right" then -- Left
pos[1] = tPos[1] - tSize[1] / 2
pos[2] = tPos[2]
tab = "/interface/tooltip/tableft.png"
elseif ttip.direction == "top" then -- Bottom
pos[1] = tPos[1]
pos[2] = tPos[2] - tSize[2] / 2
tab = "/interface/tooltip/tabdown.png"
elseif ttip.direction == "bottom" then -- Top
pos[1] = tPos[1]
pos[2] = tPos[2] + tSize[2] / 2
tab = "/interface/tooltip/tabup.png"
end

tooltip.canvas:drawImage(tab .. dir, pos, nil, nil, true)
end

--- Offsets a tooltip to line up next to the widget
-- For example, when passing (buttonPos, buttonSize, tooltipSize, "top", 2), the returned position will center the tooltip above the button with a 2 pixel gap.
-- @param wPos widget.getPosition (bottom left corner)
-- @param wSize widget.getSize
-- @param tooltipSize Size of the tooltip (border rect)
-- @param [distance=0] amount of pixels between widget and tooltip.
-- This should usually be margin + padding.
function tooltip.offset(wPos, wSize, tooltipSize, direction, distance)
local newPos = {}
if not distance then distance = 0 end

if direction == "left" then -- Left center
newPos[1] = wPos[1] - tooltipSize[1] / 2 - distance
newPos[2] = wPos[2] + wSize[2] / 2
elseif direction == "right" then -- Right center
newPos[1] = wPos[1] + wSize[1] + tooltipSize[1] / 2 + distance
newPos[2] = wPos[2] + wSize[2] / 2
elseif direction == "top" then -- Center top
newPos[1] = wPos[1] + wSize[1] / 2
newPos[2] = wPos[2] + wSize[2] + tooltipSize[2] / 2 + distance
elseif direction == "bottom" then -- Center bottom
newPos[1] = wPos[1] + wSize[1] / 2
newPos[2] = wPos[2] - tooltipSize[2] / 2 - distance
else
error(string.format("Direction %s not supported.", direction))
end

return newPos
end

-- Hook functions
hook("init", tooltip.init)
hook("cursorOverride", tooltip.cursorOverride)
11 changes: 11 additions & 0 deletions scripts/sip/tooltip/tooltip_util.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
tooltipUtil = {}

function tooltipUtil.growRect(rect, size)
if type(size) == "number" then size = {size, size} end
return {
rect[1] - size[1],
rect[2] - size[2],
rect[3] + size[1],
rect[4] + size[2]
}
end
6 changes: 6 additions & 0 deletions scripts/spawnableItemPack.lua
Original file line number Diff line number Diff line change
Expand Up @@ -528,3 +528,9 @@ function sip.updateRarityFilters()
})
end
end

function itemTooltip()
local item = sip.getSelectedItem()
if not item or not item.name or item.name == "" then return "" end
return string.format("^gray;Item identifier\n^white;%s", item.name)
end

0 comments on commit 9cee74e

Please sign in to comment.