Tercio@23: --[[----------------------------------------------------------------------------- Tercio@23: Checkbox Widget Tercio@23: -------------------------------------------------------------------------------]] Tercio@51: local Type, Version = "CheckBox", 23 Tercio@23: local AceGUI = LibStub and LibStub("AceGUI-3.0", true) Tercio@23: if not AceGUI or (AceGUI:GetWidgetVersion(Type) or 0) >= Version then return end Tercio@23: Tercio@23: -- Lua APIs Tercio@23: local select, pairs = select, pairs Tercio@23: Tercio@23: -- WoW APIs Tercio@23: local PlaySound = PlaySound Tercio@23: local CreateFrame, UIParent = CreateFrame, UIParent Tercio@23: Tercio@23: -- Global vars/functions that we don't upvalue since they might get hooked, or upgraded Tercio@23: -- List them here for Mikk's FindGlobals script Tercio@23: -- GLOBALS: SetDesaturation, GameFontHighlight Tercio@23: Tercio@23: --[[----------------------------------------------------------------------------- Tercio@23: Support functions Tercio@23: -------------------------------------------------------------------------------]] Tercio@23: local function AlignImage(self) Tercio@23: local img = self.image:GetTexture() Tercio@23: self.text:ClearAllPoints() Tercio@23: if not img then Tercio@23: self.text:SetPoint("LEFT", self.checkbg, "RIGHT") Tercio@23: self.text:SetPoint("RIGHT") Tercio@23: else Tercio@23: self.text:SetPoint("LEFT", self.image,"RIGHT", 1, 0) Tercio@23: self.text:SetPoint("RIGHT") Tercio@23: end Tercio@23: end Tercio@23: Tercio@23: --[[----------------------------------------------------------------------------- Tercio@23: Scripts Tercio@23: -------------------------------------------------------------------------------]] Tercio@23: local function Control_OnEnter(frame) Tercio@23: frame.obj:Fire("OnEnter") Tercio@23: end Tercio@23: Tercio@23: local function Control_OnLeave(frame) Tercio@23: frame.obj:Fire("OnLeave") Tercio@23: end Tercio@23: Tercio@23: local function CheckBox_OnMouseDown(frame) Tercio@23: local self = frame.obj Tercio@23: if not self.disabled then Tercio@23: if self.image:GetTexture() then Tercio@23: self.text:SetPoint("LEFT", self.image,"RIGHT", 2, -1) Tercio@23: else Tercio@23: self.text:SetPoint("LEFT", self.checkbg, "RIGHT", 1, -1) Tercio@23: end Tercio@23: end Tercio@23: AceGUI:ClearFocus() Tercio@23: end Tercio@23: Tercio@23: local function CheckBox_OnMouseUp(frame) Tercio@23: local self = frame.obj Tercio@23: if not self.disabled then Tercio@23: self:ToggleChecked() Tercio@23: Tercio@23: if self.checked then Tercio@51: PlaySound(856) -- SOUNDKIT.IG_MAINMENU_OPTION_CHECKBOX_ON Tercio@23: else -- for both nil and false (tristate) Tercio@51: PlaySound(857) -- SOUNDKIT.IG_MAINMENU_OPTION_CHECKBOX_OFF Tercio@23: end Tercio@23: Tercio@23: self:Fire("OnValueChanged", self.checked) Tercio@23: AlignImage(self) Tercio@23: end Tercio@23: end Tercio@23: Tercio@23: --[[----------------------------------------------------------------------------- Tercio@23: Methods Tercio@23: -------------------------------------------------------------------------------]] Tercio@23: local methods = { Tercio@23: ["OnAcquire"] = function(self) Tercio@23: self:SetType() Tercio@23: self:SetValue(false) Tercio@23: self:SetTriState(nil) Tercio@23: -- height is calculated from the width and required space for the description Tercio@23: self:SetWidth(200) Tercio@23: self:SetImage() Tercio@23: self:SetDisabled(nil) Tercio@23: self:SetDescription(nil) Tercio@23: end, Tercio@23: Tercio@23: -- ["OnRelease"] = nil, Tercio@23: Tercio@23: ["OnWidthSet"] = function(self, width) Tercio@23: if self.desc then Tercio@23: self.desc:SetWidth(width - 30) Tercio@23: if self.desc:GetText() and self.desc:GetText() ~= "" then Tercio@23: self:SetHeight(28 + self.desc:GetHeight()) Tercio@23: end Tercio@23: end Tercio@23: end, Tercio@23: Tercio@23: ["SetDisabled"] = function(self, disabled) Tercio@23: self.disabled = disabled Tercio@23: if disabled then Tercio@23: self.frame:Disable() Tercio@23: self.text:SetTextColor(0.5, 0.5, 0.5) Tercio@23: SetDesaturation(self.check, true) Tercio@23: if self.desc then Tercio@23: self.desc:SetTextColor(0.5, 0.5, 0.5) Tercio@23: end Tercio@23: else Tercio@23: self.frame:Enable() Tercio@23: self.text:SetTextColor(1, 1, 1) Tercio@23: if self.tristate and self.checked == nil then Tercio@23: SetDesaturation(self.check, true) Tercio@23: else Tercio@23: SetDesaturation(self.check, false) Tercio@23: end Tercio@23: if self.desc then Tercio@23: self.desc:SetTextColor(1, 1, 1) Tercio@23: end Tercio@23: end Tercio@23: end, Tercio@23: Tercio@23: ["SetValue"] = function(self,value) Tercio@23: local check = self.check Tercio@23: self.checked = value Tercio@23: if value then Tercio@23: SetDesaturation(self.check, false) Tercio@23: self.check:Show() Tercio@23: else Tercio@23: --Nil is the unknown tristate value Tercio@23: if self.tristate and value == nil then Tercio@23: SetDesaturation(self.check, true) Tercio@23: self.check:Show() Tercio@23: else Tercio@23: SetDesaturation(self.check, false) Tercio@23: self.check:Hide() Tercio@23: end Tercio@23: end Tercio@23: self:SetDisabled(self.disabled) Tercio@23: end, Tercio@23: Tercio@23: ["GetValue"] = function(self) Tercio@23: return self.checked Tercio@23: end, Tercio@23: Tercio@23: ["SetTriState"] = function(self, enabled) Tercio@23: self.tristate = enabled Tercio@23: self:SetValue(self:GetValue()) Tercio@23: end, Tercio@23: Tercio@23: ["SetType"] = function(self, type) Tercio@23: local checkbg = self.checkbg Tercio@23: local check = self.check Tercio@23: local highlight = self.highlight Tercio@23: Tercio@23: local size Tercio@23: if type == "radio" then Tercio@23: size = 16 Tercio@23: checkbg:SetTexture("Interface\\Buttons\\UI-RadioButton") Tercio@23: checkbg:SetTexCoord(0, 0.25, 0, 1) Tercio@23: check:SetTexture("Interface\\Buttons\\UI-RadioButton") Tercio@23: check:SetTexCoord(0.25, 0.5, 0, 1) Tercio@23: check:SetBlendMode("ADD") Tercio@23: highlight:SetTexture("Interface\\Buttons\\UI-RadioButton") Tercio@23: highlight:SetTexCoord(0.5, 0.75, 0, 1) Tercio@23: else Tercio@23: size = 24 Tercio@23: checkbg:SetTexture("Interface\\Buttons\\UI-CheckBox-Up") Tercio@23: checkbg:SetTexCoord(0, 1, 0, 1) Tercio@23: check:SetTexture("Interface\\Buttons\\UI-CheckBox-Check") Tercio@23: check:SetTexCoord(0, 1, 0, 1) Tercio@23: check:SetBlendMode("BLEND") Tercio@23: highlight:SetTexture("Interface\\Buttons\\UI-CheckBox-Highlight") Tercio@23: highlight:SetTexCoord(0, 1, 0, 1) Tercio@23: end Tercio@23: checkbg:SetHeight(size) Tercio@23: checkbg:SetWidth(size) Tercio@23: end, Tercio@23: Tercio@23: ["ToggleChecked"] = function(self) Tercio@23: local value = self:GetValue() Tercio@23: if self.tristate then Tercio@23: --cycle in true, nil, false order Tercio@23: if value then Tercio@23: self:SetValue(nil) Tercio@23: elseif value == nil then Tercio@23: self:SetValue(false) Tercio@23: else Tercio@23: self:SetValue(true) Tercio@23: end Tercio@23: else Tercio@23: self:SetValue(not self:GetValue()) Tercio@23: end Tercio@23: end, Tercio@23: Tercio@23: ["SetLabel"] = function(self, label) Tercio@23: self.text:SetText(label) Tercio@23: end, Tercio@23: Tercio@23: ["SetDescription"] = function(self, desc) Tercio@23: if desc then Tercio@23: if not self.desc then Tercio@23: local desc = self.frame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall") Tercio@23: desc:ClearAllPoints() Tercio@23: desc:SetPoint("TOPLEFT", self.checkbg, "TOPRIGHT", 5, -21) Tercio@23: desc:SetWidth(self.frame.width - 30) Tercio@23: desc:SetJustifyH("LEFT") Tercio@23: desc:SetJustifyV("TOP") Tercio@23: self.desc = desc Tercio@23: end Tercio@23: self.desc:Show() Tercio@23: --self.text:SetFontObject(GameFontNormal) Tercio@23: self.desc:SetText(desc) Tercio@23: self:SetHeight(28 + self.desc:GetHeight()) Tercio@23: else Tercio@23: if self.desc then Tercio@23: self.desc:SetText("") Tercio@23: self.desc:Hide() Tercio@23: end Tercio@23: --self.text:SetFontObject(GameFontHighlight) Tercio@23: self:SetHeight(24) Tercio@23: end Tercio@23: end, Tercio@23: Tercio@23: ["SetImage"] = function(self, path, ...) Tercio@23: local image = self.image Tercio@23: image:SetTexture(path) Tercio@23: Tercio@23: if image:GetTexture() then Tercio@23: local n = select("#", ...) Tercio@23: if n == 4 or n == 8 then Tercio@23: image:SetTexCoord(...) Tercio@23: else Tercio@23: image:SetTexCoord(0, 1, 0, 1) Tercio@23: end Tercio@23: end Tercio@23: AlignImage(self) Tercio@23: end Tercio@23: } Tercio@23: Tercio@23: --[[----------------------------------------------------------------------------- Tercio@23: Constructor Tercio@23: -------------------------------------------------------------------------------]] Tercio@23: local function Constructor() Tercio@23: local frame = CreateFrame("Button", nil, UIParent) Tercio@23: frame:Hide() Tercio@23: Tercio@23: frame:EnableMouse(true) Tercio@23: frame:SetScript("OnEnter", Control_OnEnter) Tercio@23: frame:SetScript("OnLeave", Control_OnLeave) Tercio@23: frame:SetScript("OnMouseDown", CheckBox_OnMouseDown) Tercio@23: frame:SetScript("OnMouseUp", CheckBox_OnMouseUp) Tercio@23: Tercio@23: local checkbg = frame:CreateTexture(nil, "ARTWORK") Tercio@23: checkbg:SetWidth(24) Tercio@23: checkbg:SetHeight(24) Tercio@23: checkbg:SetPoint("TOPLEFT") Tercio@23: checkbg:SetTexture("Interface\\Buttons\\UI-CheckBox-Up") Tercio@23: Tercio@23: local check = frame:CreateTexture(nil, "OVERLAY") Tercio@23: check:SetAllPoints(checkbg) Tercio@23: check:SetTexture("Interface\\Buttons\\UI-CheckBox-Check") Tercio@23: Tercio@23: local text = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlight") Tercio@23: text:SetJustifyH("LEFT") Tercio@23: text:SetHeight(18) Tercio@23: text:SetPoint("LEFT", checkbg, "RIGHT") Tercio@23: text:SetPoint("RIGHT") Tercio@23: Tercio@23: local highlight = frame:CreateTexture(nil, "HIGHLIGHT") Tercio@23: highlight:SetTexture("Interface\\Buttons\\UI-CheckBox-Highlight") Tercio@23: highlight:SetBlendMode("ADD") Tercio@23: highlight:SetAllPoints(checkbg) Tercio@23: Tercio@23: local image = frame:CreateTexture(nil, "OVERLAY") Tercio@23: image:SetHeight(16) Tercio@23: image:SetWidth(16) Tercio@23: image:SetPoint("LEFT", checkbg, "RIGHT", 1, 0) Tercio@23: Tercio@23: local widget = { Tercio@23: checkbg = checkbg, Tercio@23: check = check, Tercio@23: text = text, Tercio@23: highlight = highlight, Tercio@23: image = image, Tercio@23: frame = frame, Tercio@23: type = Type Tercio@23: } Tercio@23: for method, func in pairs(methods) do Tercio@23: widget[method] = func Tercio@23: end Tercio@23: Tercio@23: return AceGUI:RegisterAsWidget(widget) Tercio@23: end Tercio@23: Tercio@23: AceGUI:RegisterWidgetType(Type, Constructor, Version)