tercio@0: --[[----------------------------------------------------------------------------- tercio@0: ColorPicker Widget tercio@0: -------------------------------------------------------------------------------]] Tercio@18: local Type, Version = "ColorPicker", 23 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 pairs = pairs tercio@0: tercio@0: -- WoW APIs 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: ShowUIPanel, HideUIPanel, ColorPickerFrame, OpacitySliderFrame tercio@0: tercio@0: --[[----------------------------------------------------------------------------- tercio@0: Support functions tercio@0: -------------------------------------------------------------------------------]] tercio@0: local function ColorCallback(self, r, g, b, a, isAlpha) tercio@0: if not self.HasAlpha then tercio@0: a = 1 tercio@0: end tercio@0: self:SetColor(r, g, b, a) tercio@0: if ColorPickerFrame:IsVisible() then tercio@0: --colorpicker is still open tercio@0: self:Fire("OnValueChanged", r, g, b, a) tercio@0: else tercio@0: --colorpicker is closed, color callback is first, ignore it, tercio@0: --alpha callback is the final call after it closes so confirm now tercio@0: if isAlpha then tercio@0: self:Fire("OnValueConfirmed", r, g, b, a) tercio@0: end 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 ColorSwatch_OnClick(frame) tercio@0: HideUIPanel(ColorPickerFrame) tercio@0: local self = frame.obj tercio@0: if not self.disabled then tercio@0: ColorPickerFrame:SetFrameStrata("FULLSCREEN_DIALOG") Tercio@18: ColorPickerFrame:SetFrameLevel(frame:GetFrameLevel() + 10) tercio@0: ColorPickerFrame:SetClampedToScreen(true) tercio@0: tercio@0: ColorPickerFrame.func = function() tercio@0: local r, g, b = ColorPickerFrame:GetColorRGB() tercio@0: local a = 1 - OpacitySliderFrame:GetValue() tercio@0: ColorCallback(self, r, g, b, a) tercio@0: end tercio@0: tercio@0: ColorPickerFrame.hasOpacity = self.HasAlpha tercio@0: ColorPickerFrame.opacityFunc = function() tercio@0: local r, g, b = ColorPickerFrame:GetColorRGB() tercio@0: local a = 1 - OpacitySliderFrame:GetValue() tercio@0: ColorCallback(self, r, g, b, a, true) tercio@0: end tercio@0: tercio@0: local r, g, b, a = self.r, self.g, self.b, self.a tercio@0: if self.HasAlpha then tercio@0: ColorPickerFrame.opacity = 1 - (a or 0) tercio@0: end tercio@0: ColorPickerFrame:SetColorRGB(r, g, b) tercio@0: tercio@0: ColorPickerFrame.cancelFunc = function() tercio@0: ColorCallback(self, r, g, b, a, true) tercio@0: end tercio@0: tercio@0: ShowUIPanel(ColorPickerFrame) tercio@0: end tercio@0: AceGUI:ClearFocus() tercio@0: end tercio@0: tercio@0: --[[----------------------------------------------------------------------------- tercio@0: Methods tercio@0: -------------------------------------------------------------------------------]] tercio@0: local methods = { tercio@0: ["OnAcquire"] = function(self) tercio@0: self:SetHeight(24) tercio@0: self:SetWidth(200) tercio@0: self:SetHasAlpha(false) tercio@0: self:SetColor(0, 0, 0, 1) tercio@0: self:SetDisabled(nil) tercio@0: self:SetLabel(nil) tercio@0: end, tercio@0: tercio@0: -- ["OnRelease"] = nil, tercio@0: tercio@0: ["SetLabel"] = function(self, text) tercio@0: self.text:SetText(text) tercio@0: end, tercio@0: tercio@0: ["SetColor"] = function(self, r, g, b, a) tercio@0: self.r = r tercio@0: self.g = g tercio@0: self.b = b tercio@0: self.a = a or 1 tercio@0: self.colorSwatch:SetVertexColor(r, g, b, a) tercio@0: end, tercio@0: tercio@0: ["SetHasAlpha"] = function(self, HasAlpha) tercio@0: self.HasAlpha = HasAlpha tercio@0: end, tercio@0: tercio@0: ["SetDisabled"] = function(self, disabled) tercio@0: self.disabled = disabled tercio@0: if self.disabled then tercio@0: self.frame:Disable() tercio@0: self.text:SetTextColor(0.5, 0.5, 0.5) tercio@0: else tercio@0: self.frame:Enable() tercio@0: self.text:SetTextColor(1, 1, 1) tercio@0: end 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("OnClick", ColorSwatch_OnClick) tercio@0: tercio@0: local colorSwatch = frame:CreateTexture(nil, "OVERLAY") tercio@0: colorSwatch:SetWidth(19) tercio@0: colorSwatch:SetHeight(19) tercio@0: colorSwatch:SetTexture("Interface\\ChatFrame\\ChatFrameColorSwatch") tercio@0: colorSwatch:SetPoint("LEFT") tercio@0: tercio@0: local texture = frame:CreateTexture(nil, "BACKGROUND") tercio@0: texture:SetWidth(16) tercio@0: texture:SetHeight(16) Tercio@18: texture:SetColorTexture(1, 1, 1) tercio@0: texture:SetPoint("CENTER", colorSwatch) tercio@0: texture:Show() tercio@0: tercio@0: local checkers = frame:CreateTexture(nil, "BACKGROUND") tercio@0: checkers:SetWidth(14) tercio@0: checkers:SetHeight(14) tercio@0: checkers:SetTexture("Tileset\\Generic\\Checkers") tercio@0: checkers:SetTexCoord(.25, 0, 0.5, .25) tercio@0: checkers:SetDesaturated(true) tercio@0: checkers:SetVertexColor(1, 1, 1, 0.75) tercio@0: checkers:SetPoint("CENTER", colorSwatch) tercio@0: checkers:Show() tercio@0: tercio@0: local text = frame:CreateFontString(nil,"OVERLAY","GameFontHighlight") tercio@0: text:SetHeight(24) tercio@0: text:SetJustifyH("LEFT") tercio@0: text:SetTextColor(1, 1, 1) tercio@0: text:SetPoint("LEFT", colorSwatch, "RIGHT", 2, 0) tercio@0: text:SetPoint("RIGHT") tercio@0: tercio@0: --local highlight = frame:CreateTexture(nil, "HIGHLIGHT") tercio@0: --highlight:SetTexture("Interface\\QuestFrame\\UI-QuestTitleHighlight") tercio@0: --highlight:SetBlendMode("ADD") tercio@0: --highlight:SetAllPoints(frame) tercio@0: tercio@0: local widget = { tercio@0: colorSwatch = colorSwatch, tercio@0: text = text, 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)