f2424372e1ae5a915281cd20854aadec82ca73ff
[wowui.git] / libs / AceGUI-3.0 / widgets / AceGUIWidget-ColorPicker.lua
1 --[[-----------------------------------------------------------------------------
2 ColorPicker Widget
3 -------------------------------------------------------------------------------]]
4 local Type, Version = "ColorPicker", 21
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:SetClampedToScreen(true)
55
56                 ColorPickerFrame.func = function()
57                         local r, g, b = ColorPickerFrame:GetColorRGB()
58                         local a = 1 - OpacitySliderFrame:GetValue()
59                         ColorCallback(self, r, g, b, a)
60                 end
61
62                 ColorPickerFrame.hasOpacity = self.HasAlpha
63                 ColorPickerFrame.opacityFunc = function()
64                         local r, g, b = ColorPickerFrame:GetColorRGB()
65                         local a = 1 - OpacitySliderFrame:GetValue()
66                         ColorCallback(self, r, g, b, a, true)
67                 end
68
69                 local r, g, b, a = self.r, self.g, self.b, self.a
70                 if self.HasAlpha then
71                         ColorPickerFrame.opacity = 1 - (a or 0)
72                 end
73                 ColorPickerFrame:SetColorRGB(r, g, b)
74
75                 ColorPickerFrame.cancelFunc = function()
76                         ColorCallback(self, r, g, b, a, true)
77                 end
78
79                 ShowUIPanel(ColorPickerFrame)
80         end
81         AceGUI:ClearFocus()
82 end
83
84 --[[-----------------------------------------------------------------------------
85 Methods
86 -------------------------------------------------------------------------------]]
87 local methods = {
88         ["OnAcquire"] = function(self)
89                 self:SetHeight(24)
90                 self:SetWidth(200)
91                 self:SetHasAlpha(false)
92                 self:SetColor(0, 0, 0, 1)
93                 self:SetDisabled(nil)
94                 self:SetLabel(nil)
95         end,
96
97         -- ["OnRelease"] = nil,
98
99         ["SetLabel"] = function(self, text)
100                 self.text:SetText(text)
101         end,
102
103         ["SetColor"] = function(self, r, g, b, a)
104                 self.r = r
105                 self.g = g
106                 self.b = b
107                 self.a = a or 1
108                 self.colorSwatch:SetVertexColor(r, g, b, a)
109         end,
110
111         ["SetHasAlpha"] = function(self, HasAlpha)
112                 self.HasAlpha = HasAlpha
113         end,
114
115         ["SetDisabled"] = function(self, disabled)
116                 self.disabled = disabled
117                 if self.disabled then
118                         self.frame:Disable()
119                         self.text:SetTextColor(0.5, 0.5, 0.5)
120                 else
121                         self.frame:Enable()
122                         self.text:SetTextColor(1, 1, 1)
123                 end
124         end
125 }
126
127 --[[-----------------------------------------------------------------------------
128 Constructor
129 -------------------------------------------------------------------------------]]
130 local function Constructor()
131         local frame = CreateFrame("Button", nil, UIParent)
132         frame:Hide()
133
134         frame:EnableMouse(true)
135         frame:SetScript("OnEnter", Control_OnEnter)
136         frame:SetScript("OnLeave", Control_OnLeave)
137         frame:SetScript("OnClick", ColorSwatch_OnClick)
138
139         local colorSwatch = frame:CreateTexture(nil, "OVERLAY")
140         colorSwatch:SetWidth(19)
141         colorSwatch:SetHeight(19)
142         colorSwatch:SetTexture("Interface\\ChatFrame\\ChatFrameColorSwatch")
143         colorSwatch:SetPoint("LEFT")
144
145         local texture = frame:CreateTexture(nil, "BACKGROUND")
146         texture:SetWidth(16)
147         texture:SetHeight(16)
148         texture:SetTexture(1, 1, 1)
149         texture:SetPoint("CENTER", colorSwatch)
150         texture:Show()
151
152         local checkers = frame:CreateTexture(nil, "BACKGROUND")
153         checkers:SetWidth(14)
154         checkers:SetHeight(14)
155         checkers:SetTexture("Tileset\\Generic\\Checkers")
156         checkers:SetTexCoord(.25, 0, 0.5, .25)
157         checkers:SetDesaturated(true)
158         checkers:SetVertexColor(1, 1, 1, 0.75)
159         checkers:SetPoint("CENTER", colorSwatch)
160         checkers:Show()
161
162         local text = frame:CreateFontString(nil,"OVERLAY","GameFontHighlight")
163         text:SetHeight(24)
164         text:SetJustifyH("LEFT")
165         text:SetTextColor(1, 1, 1)
166         text:SetPoint("LEFT", colorSwatch, "RIGHT", 2, 0)
167         text:SetPoint("RIGHT")
168
169         --local highlight = frame:CreateTexture(nil, "HIGHLIGHT")
170         --highlight:SetTexture("Interface\\QuestFrame\\UI-QuestTitleHighlight")
171         --highlight:SetBlendMode("ADD")
172         --highlight:SetAllPoints(frame)
173
174         local widget = {
175                 colorSwatch = colorSwatch,
176                 text        = text,
177                 frame       = frame,
178                 type        = Type
179         }
180         for method, func in pairs(methods) do
181                 widget[method] = func
182         end
183
184         return AceGUI:RegisterAsWidget(widget)
185 end
186
187 AceGUI:RegisterWidgetType(Type, Constructor, Version)