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