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