annotate Libs/AceGUI-3.0/widgets/AceGUIWidget-ColorPicker.lua @ 0:c6ff7ba0e8f6

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