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