annotate Libs/AceGUI-3.0/widgets/AceGUIWidget-ColorPicker.lua @ 3:1c0f88ab78b7

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