Skip to content

Instantly share code, notes, and snippets.

@arieh
Last active May 9, 2018 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arieh/ab07a6e128b249b1301f3bed87fa3e16 to your computer and use it in GitHub Desktop.
Save arieh/ab07a6e128b249b1301f3bed87fa3e16 to your computer and use it in GitHub Desktop.
local TMW = TMW
if not TMW then return end
local L = TMW.L
local Type = TMW.Classes.IconType:New("scriptbar")
Type.name = "Script Bar"
Type.desc = "A Bar that can be controlled by LUA scripts"
Type.menuIcon = "Interface/Icons/inv_box_04"
Type.unitType = "unitid"
Type.hasNoGCD = true
Type.canControlGroup = true
Type.menuSpaceBefore = true
local STATE_SHOW = TMW.CONST.STATE.DEFAULT_SHOW
local STATE_HIDE = TMW.CONST.STATE.DEFAULT_HIDE
Type:SetAllowanceForView("icon", false)
Type:UsesAttributes("value, maxValue, valueColor")
Type:UsesAttributes("state")
Type:UsesAttributes("texture")
local function Value_OnUpdate(icon)
if icon.script_values.changed then
icon:YieldInfo(true, icon.show, icon.script_values.current, icon.script_values.max, icon.script_values.colors)
icon.script_values.changed = false
return
end
end
function Type:HandleYieldedInfo(icon, iconToSet, show, value, maxValue, valueColor)
local state = STATE_HIDE;
if show then state = STATE_SHOW end
iconToSet:SetInfo("state; value, maxValue, valueColor;",
state,
value, maxValue, valueColor
)
end
function Type:Setup(icon)
icon:SetInfo("texture", "Interface/Icons/inv_box_04")
icon:SetUpdateMethod("auto")
icon:SetUpdateFunction(Value_OnUpdate)
icon.script_values = {
max = 100,
current = 0,
changed = true,
colors = {"#ffff1200", "#ffffff00", "#ff00ff00"},
show = true
}
local values = icon.script_values
function icon:setMaxValue(value)
values.max = value
values.changed = true
end
function icon:setCurrentValue(value)
if (value > values.max) return
values.current = value
values.changed = true
end
function icon:setBarColors(startColor, midColor, lastColor)
local start = startColor or values.colors[0]
local mid = midColor or values.colors[1]
local last = lastColor or values.colors[2]
values.colors = {start, mid, last}
end
icon:Update()
end
TMW:RegisterCallback("TMW_CONFIG_ICON_TYPE_CHANGED", function(event, icon, type, oldType)
local icspv = icon:GetSettingsPerView()
if type == Type.type then
icon:GetSettings().CustomTex = "NONE"
local layout = TMW.TEXT:GetTextLayoutForIcon(icon)
if layout == "bar1" or layout == "bar2" then
icspv.Texts[1] = "[(Value / ValueMax * 100):Round:Percent]"
icspv.Texts[2] = "[Value:Short \"/\" ValueMax:Short]"
end
elseif oldType == Type.type then
if icspv.Texts[1] == "[(Value / ValueMax * 100):Round:Percent]" then
icspv.Texts[1] = nil
end
if icspv.Texts[2] == "[Value:Short \"/\" ValueMax:Short]" then
icspv.Texts[2] = nil
end
end
end)
Type:Register(157)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment