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