Skip to content

Commit

Permalink
Drycode fix for #361 (#362)
Browse files Browse the repository at this point in the history
* Drycode fix for #361

* Revert "Drycode fix for #361"

This reverts commit b5650d6.

* #361: Provide proper retro-compatibility for icons

- Added `Utils.getIconTexture(icon)` function to get a texture for all types of icons.
- Updated toolbar code to use `Utils.getIconTexture(icon)` instead of hard coded paths.

* Utils.getIconTexture fixes
  • Loading branch information
Meorawr authored and Solanya committed Sep 5, 2019
1 parent 8e36354 commit 31ad26d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 30 deletions.
16 changes: 15 additions & 1 deletion totalRP3/core/impl/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,21 @@ end
-- Return an texture text tag based on the given icon url and size. Nil safe.
function Utils.str.icon(iconPath, iconSize)
iconPath = iconPath or Globals.icons.default;
return Utils.str.texture("Interface\\ICONS\\" .. iconPath, iconSize);
return Utils.str.texture(Utils.getIconTexture(iconPath), iconSize);
end

--- Gives the full texture path of an individual icon.
--- Handle using icon as a string, a file ID or as an Ellyb.icon
--- @param icon string|Icon
--- @return string
function Utils.getIconTexture(icon)
if type(icon) == "table" and icon.isInstanceOf and icon:isInstanceOf(Ellyb.Icon) then
return icon:GetFileID()
elseif type(icon) == "number" then
return icon
else
return "Interface\\ICONS\\" .. tostring(icon)
end
end

-- Return a color tag based on a letter
Expand Down
47 changes: 18 additions & 29 deletions totalRP3/modules/toolbar/toolbar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ local function onStart()
-- imports
local Globals, Utils = TRP3_API.globals, TRP3_API.utils;
local loc = TRP3_API.loc;
local icon = Utils.str.icon;
local color = Utils.str.color;
local assert, pairs, tinsert, table, math = assert, pairs, tinsert, table, math;
local toolbarContainer, mainTooltip = TRP3_ToolbarContainer, TRP3_MainTooltip;
Expand All @@ -58,6 +57,15 @@ local function onStart()
local marginLeft = 7;
local marginTop = 7;


--- Small tool function to create a title string with the icon of the button in front of it, for the tooltip
--- @param buttonStructure {icon: string|Icon, tooltip: string, configText: string}
--- @return string
local function getTooltipTitleWithIcon(buttonStructure)
return Utils.str.icon(buttonStructure.icon, 25) .. " " .. (buttonStructure.tooltip or buttonStructure.configText);
end


local function buildToolbar()
local maxButtonPerLine = getConfigValue(CONFIG_ICON_MAX_PER_LINE);
local buttonSize = getConfigValue(CONFIG_ICON_SIZE);
Expand Down Expand Up @@ -94,8 +102,8 @@ local function onStart()
uiButton:RegisterForClicks("LeftButtonUp", "RightButtonUp");
tinsert(uiButtons, uiButton);
end
uiButton:SetNormalTexture("Interface\\ICONS\\".. (buttonStructure.icon or Globals.icons.default));
uiButton:SetPushedTexture("Interface\\ICONS\\".. (buttonStructure.icon or Globals.icons.default));
uiButton:SetNormalTexture(Utils.getIconTexture(buttonStructure.icon or Globals.icons.default));
uiButton:SetPushedTexture(Utils.getIconTexture(buttonStructure.icon or Globals.icons.default));
uiButton:GetPushedTexture():SetDesaturated(1);
uiButton:SetPoint("TOPLEFT", x, y);
uiButton:SetScript("OnClick", function(self, button)
Expand Down Expand Up @@ -128,8 +136,7 @@ local function onStart()
end
end);
if buttonStructure.tooltip then
local tooltipTitleWithIcon = icon(buttonStructure.icon, 25) .. " " .. buttonStructure.tooltip;
setTooltipForFrame(uiButton, uiButton, "LEFT", 0, 0, tooltipTitleWithIcon, buttonStructure.tooltipSub);
setTooltipForFrame(uiButton, uiButton, "LEFT", 0, 0, getTooltipTitleWithIcon(buttonStructure), buttonStructure.tooltipSub);
end
uiButton:SetWidth(buttonSize);
uiButton:SetHeight(buttonSize);
Expand Down Expand Up @@ -159,17 +166,6 @@ local function onStart()
end
end

--- Small tool function to create a title string with the icon of the button in front of it, for the tooltip
-- @param buttonStructure
--
local function getTooltipTitleWithIcon(buttonStructure)
if type(buttonStructure.icon) == "string" then
return icon(buttonStructure.icon, 25) .. " " .. (buttonStructure.tooltip or buttonStructure.configText);
else
return buttonStructure.icon:GenerateString(25) .. " " .. (buttonStructure.tooltip or buttonStructure.configText);
end
end

--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
-- Databroker integration
--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Expand All @@ -185,7 +181,7 @@ local function onStart()
TRP3_API.globals.addon_name_short .. "" .. buttonStructure.configText,
{
type= "data source",
icon = "Interface\\ICONS\\"..buttonStructure.icon,
icon = Utils.getIconTexture(buttonStructure.icon),
OnClick = function(Uibutton, button)
if buttonStructure.onClick then
buttonStructure.onClick(Uibutton, buttonStructure, button);
Expand All @@ -211,11 +207,8 @@ local function onStart()
local LDBButton = LDBObjects[buttonStructure.id];
assert(LDBButton, "Could not find a registered LDB object for id " .. buttonStructure.id)

if type(buttonStructure.icon) == "string" then
LDBButton.icon = "Interface\\ICONS\\" .. buttonStructure.icon;
else
LDBButton.icon = buttonStructure.icon:GetFileID();
end
LDBButton.icon = Utils.getIconTexture(buttonStructure.icon);

LDBButton.tooltipTitle = getTooltipTitleWithIcon(buttonStructure);
LDBButton.tooltipSub = buttonStructure.tooltipSub;

Expand Down Expand Up @@ -244,13 +237,9 @@ local function onStart()
--
local function updateToolbarButton(toolbarButton, buttonStructure)
-- Setting the textures
if type(buttonStructure.icon) == "string" then
toolbarButton:SetNormalTexture("Interface\\ICONS\\" .. buttonStructure.icon);
toolbarButton:SetPushedTexture("Interface\\ICONS\\" .. buttonStructure.icon);
else
toolbarButton:SetNormalTexture(buttonStructure.icon:GetFileID());
toolbarButton:SetPushedTexture(buttonStructure.icon:GetFileID());
end
toolbarButton:SetNormalTexture(Utils.getIconTexture(buttonStructure.icon));
toolbarButton:SetPushedTexture(Utils.getIconTexture(buttonStructure.icon));

toolbarButton:GetPushedTexture():SetDesaturated(1);
-- Refreshing the tooltip
setTooltipForFrame(toolbarButton, toolbarButton, "LEFT", 0, 0, getTooltipTitleWithIcon(buttonStructure), buttonStructure.tooltipSub);
Expand Down

0 comments on commit 31ad26d

Please sign in to comment.