976f219 - Add major aura indicator
[wowui.git] / OmaRFConfig / libs / AceGUI-3.0 / widgets / AceGUIWidget-ColorPicker.lua
1 --[[-----------------------------------------------------------------------------
2 ColorPicker Widget
3 -------------------------------------------------------------------------------]]
4 local Type, Version = "ColorPicker", 23
5 local AceGUI = LibStub and LibStub("AceGUI-3.0", true)
6 if not AceGUI or (AceGUI:GetWidgetVersion(Type) or 0) >= Version then return end
7
8 -- Lua APIs
9 local pairs = pairs
10
11 -- WoW APIs
12 local CreateFrame, UIParent = CreateFrame, UIParent
13
14 -- Global vars/functions that we don't upvalue since they might get hooked, or upgraded
15 -- List them here for Mikk's FindGlobals script
16 -- GLOBALS: ShowUIPanel, HideUIPanel, ColorPickerFrame, OpacitySliderFrame
17
18 --[[-----------------------------------------------------------------------------
19 Support functions
20 -------------------------------------------------------------------------------]]
21 local function ColorCallback(self, r, g, b, a, isAlpha)
22         if not self.HasAlpha then
23                 a = 1
24         end
25         self:SetColor(r, g, b, a)
26         if ColorPickerFrame:IsVisible() then
27                 --colorpicker is still open
28                 self:Fire("OnValueChanged", r, g, b, a)
29         else
30                 --colorpicker is closed, color callback is first, ignore it,
31                 --alpha callback is the final call after it closes so confirm now
32                 if isAlpha then
33                         self:Fire("OnValueConfirmed", r, g, b, a)
34                 end
35         end
36 end
37
38 --[[-----------------------------------------------------------------------------
39 Scripts
40 -------------------------------------------------------------------------------]]
41 local function Control_OnEnter(frame)
42         frame.obj:Fire("OnEnter")
43 end
44
45 local function Control_OnLeave(frame)
46         frame.obj:Fire("OnLeave")
47 end
48
49 local function ColorSwatch_OnClick(frame)
50         HideUIPanel(ColorPickerFrame)
51         local self = frame.obj
52         if not self.disabled then
53                 ColorPickerFrame:SetFrameStrata("FULLSCREEN_DIALOG")
54                 ColorPickerFrame:SetFrameLevel(frame:GetFrameLevel() + 10)
55                 ColorPickerFrame:SetClampedToScreen(true)
56
57                 ColorPickerFrame.func = function()
58                         local r, g, b = ColorPickerFrame:GetColorRGB()
59                         local a = 1 - OpacitySliderFrame:GetValue()
60                         ColorCallback(self, r, g, b, a)
61                 end
62
63                 ColorPickerFrame.hasOpacity = self.HasAlpha
64                 ColorPickerFrame.opacityFunc = function()
65                         local r, g, b = ColorPickerFrame:GetColorRGB()
66                         local a = 1 - OpacitySliderFrame:GetValue()
67                         ColorCallback(self, r, g, b, a, true)
68                 end
69
70                 local r, g, b, a = self.r, self.g, self.b, self.a
71                 if self.HasAlpha then
72                         ColorPickerFrame.opacity = 1 - (a or 0)
73                 end
74                 ColorPickerFrame:SetColorRGB(r, g, b)
75
76                 ColorPickerFrame.cancelFunc = function()
77                         ColorCallback(self, r, g, b, a, true)
78                 end
79
80                 ShowUIPanel(ColorPickerFrame)
81         end
82         AceGUI:ClearFocus()
83 end
84
85 --[[-----------------------------------------------------------------------------
86 Methods
87 -------------------------------------------------------------------------------]]
88 local methods = {
89         ["OnAcquire"] = function(self)
90                 self:SetHeight(24)
91                 self:SetWidth(200)
92                 self:SetHasAlpha(false)
93                 self:SetColor(0, 0, 0, 1)
94                 self:SetDisabled(nil)
95                 self:SetLabel(nil)
96         end,
97
98         -- ["OnRelease"] = nil,
99
100         ["SetLabel"] = function(self, text)
101                 self.text:SetText(text)
102         end,
103
104         ["SetColor"] = function(self, r, g, b, a)
105                 self.r = r
106                 self.g = g
107                 self.b = b
108                 self.a = a or 1
109                 self.colorSwatch:SetVertexColor(r, g, b, a)
110         end,
111
112         ["SetHasAlpha"] = function(self, HasAlpha)
113                 self.HasAlpha = HasAlpha
114         end,
115
116         ["SetDisabled"] = function(self, disabled)
117                 self.disabled = disabled
118                 if self.disabled then
119                         self.frame:Disable()
120                         self.text:SetTextColor(0.5, 0.5, 0.5)
121                 else
122                         self.frame:Enable()
123                         self.text:SetTextColor(1, 1, 1)
124                 end
125         end
126 }
127
128 --[[-----------------------------------------------------------------------------
129 Constructor
130 -------------------------------------------------------------------------------]]
131 local function Constructor()
132         local frame = CreateFrame("Button", nil, UIParent)
133         frame:Hide()
134
135         frame:EnableMouse(true)
136         frame:SetScript("OnEnter", Control_OnEnter)
137         frame:SetScript("OnLeave", Control_OnLeave)
138         frame:SetScript("OnClick", ColorSwatch_OnClick)
139
140         local colorSwatch = frame:CreateTexture(nil, "OVERLAY")
141         colorSwatch:SetWidth(19)
142         colorSwatch:SetHeight(19)
143         colorSwatch:SetTexture("Interface\\ChatFrame\\ChatFrameColorSwatch")
144         colorSwatch:SetPoint("LEFT")
145
146         local texture = frame:CreateTexture(nil, "BACKGROUND")
147         texture:SetWidth(16)
148         texture:SetHeight(16)
149         texture:SetColorTexture(1, 1, 1)
150         texture:SetPoint("CENTER", colorSwatch)
151         texture:Show()
152
153         local checkers = frame:CreateTexture(nil, "BACKGROUND")
154         checkers:SetWidth(14)
155         checkers:SetHeight(14)
156         checkers:SetTexture("Tileset\\Generic\\Checkers")
157         checkers:SetTexCoord(.25, 0, 0.5, .25)
158         checkers:SetDesaturated(true)
159         checkers:SetVertexColor(1, 1, 1, 0.75)
160         checkers:SetPoint("CENTER", colorSwatch)
161         checkers:Show()
162
163         local text = frame:CreateFontString(nil,"OVERLAY","GameFontHighlight")
164         text:SetHeight(24)
165         text:SetJustifyH("LEFT")
166         text:SetTextColor(1, 1, 1)
167         text:SetPoint("LEFT", colorSwatch, "RIGHT", 2, 0)
168         text:SetPoint("RIGHT")
169
170         --local highlight = frame:CreateTexture(nil, "HIGHLIGHT")
171         --highlight:SetTexture("Interface\\QuestFrame\\UI-QuestTitleHighlight")
172         --highlight:SetBlendMode("ADD")
173         --highlight:SetAllPoints(frame)
174
175         local widget = {
176                 colorSwatch = colorSwatch,
177                 text        = text,
178                 frame       = frame,
179                 type        = Type
180         }
181         for method, func in pairs(methods) do
182                 widget[method] = func
183         end
184
185         return AceGUI:RegisterAsWidget(widget)
186 end
187
188 AceGUI:RegisterWidgetType(Type, Constructor, Version)