Back to Home

ESO Lua File v100018

libraries/zo_colorpicker/zo_colorpicker.lua

[◄ back to folders ]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
--[[ Provides a simple color picker dialog
Usage:
COLOR_PICKER:Show(colorSelectedCallback[, r][, g][, b][, a])
*colorSelectedCallback - Called when a value is picked, signature: colorSelectedCallback(newR, newG, newB, newA)
*r - Optional starting red value (default: 1)
*g - Optional starting green value (default: 1)
*b - Optional starting blue value (default: 1)
*a - Optional starting alpha value, if no alpha value is provided the alpha slider is not shown, if one is provided the alpha slider is shown
]]
--
local FULL_WIDTH = 390
local FULL_HEIGHT = 225
local SMALL_WIDTH = 390
local SMALL_HEIGHT = 200
ZO_ColorPicker = ZO_Object:Subclass()
function ZO_ColorPicker:New(control)
     local picker = ZO_Object.New(self)
     picker:Initialize(control)
     return picker
end
function ZO_ColorPicker:Initialize(control)
     self.control = control
    local content = self.control:GetNamedChild("Content")
    self.content = content
     self.colorSelect = content:GetNamedChild("ColorSelect")
     self.colorSelectThumb = self.colorSelect:GetNamedChild("Thumb")
     self.colorSelect:SetColorWheelThumbTextureControl(self.colorSelectThumb)
     self.valueSlider = content:GetNamedChild("Value")
     self.valueSlider:GetThumbTextureControl():SetDrawLayer(3)
     self.valueTexture = self.valueSlider:GetNamedChild("Texture")
     self.alphaLabel = content:GetNamedChild("AlphaLabel")
     self.alphaSlider = content:GetNamedChild("Alpha")
     self.alphaSlider:GetThumbTextureControl():SetDrawLayer(3)
     self.alphaTexture = self.alphaSlider:GetNamedChild("Texture")
     local preview = content:GetNamedChild("Preview")
     self.previewInitialTexture = preview:GetNamedChild("TextureBottom")
    self.previewCurrentTexture = preview:GetNamedChild("TextureTop")
    local function SetColorFromSpinner(r, g, b, a)
        if not self.isUpdatingColors then
            self:SetColor(r, g, b, a)
        end
    end
    local spinners = content:GetNamedChild("Spinners")
    self.redSpinner = ZO_Spinner:New(spinners:GetNamedChild("Red"), 0, 255)
    self.redSpinner:RegisterCallback("OnValueChanged", function(value)
        local r, g, b, a = self:GetColors()
        SetColorFromSpinner(value / 255, g, b, a)
    end)
    self.redSpinner:SetNormalColor(ZO_ColorDef:New(1, .2, .2, 1))
    self.greenSpinner = ZO_Spinner:New(spinners:GetNamedChild("Green"), 0, 255)
    self.greenSpinner:RegisterCallback("OnValueChanged", function(value)
        local r, g, b, a = self:GetColors()
        SetColorFromSpinner(r, value / 255, b, a)
    end)
    self.greenSpinner:SetNormalColor(ZO_ColorDef:New(.2, 1, .2, 1))
    self.blueSpinner = ZO_Spinner:New(spinners:GetNamedChild("Blue"), 0, 255)
    self.blueSpinner:RegisterCallback("OnValueChanged", function(value)
        local r, g, b, a = self:GetColors()
        SetColorFromSpinner(r, g, value / 255, a)
    end)
    self.blueSpinner:SetNormalColor(ZO_ColorDef:New(.2, .2, 1, 1))
    self.alphaSpinner = ZO_Spinner:New(spinners:GetNamedChild("Alpha"), 0, 255)
    self.alphaSpinner:RegisterCallback("OnValueChanged", function(value)
        local r, g, b, a = self:GetColors()
        SetColorFromSpinner(r, g, b, value / 255)
    end)
    ZO_Dialogs_RegisterCustomDialog("COLOR_PICKER",
    {
        customControl = control,
        title =
        {
            text = SI_WINDOW_TITLE_COLOR_PICKER,
        },
        buttons =
        {
            {
                control =   control:GetNamedChild("Accept"),
                text =      SI_DIALOG_ACCEPT,
                keybind =   "DIALOG_PRIMARY",
                callback =  function() self:Confirm() end,
            },  
            {
                control =   control:GetNamedChild("Cancel"),
                text =      SI_DIALOG_CANCEL,
                keybind =   "DIALOG_NEGATIVE",
                callback =  function() COLOR_PICKER:Cancel() end,
            },
        }
    })
end
function ZO_ColorPicker:UpdateColors(r, g, b, a)
    self.isUpdatingColors = true
     local fullR, fullG, fullB = self.colorSelect:GetFullValuedColorAsRGB()
     self.valueTexture:SetGradientColors(ORIENTATION_VERTICAL, 0, 0, 0, 1, fullR, fullG, fullB, 1)
     if self.hasAlpha then
          self.previewCurrentTexture:SetColor(r, g, b, a)
     else
          self.previewCurrentTexture:SetColor(r, g, b, 1)
     end
    --Even though the alpha is 1.0 at the right, a lower value gives a better visual
     self.alphaTexture:SetGradientColors(ORIENTATION_HORIZONTAL, r, g, b, 0, r, g, b, .85)
    self.redSpinner:SetValue(r * 255)
    self.greenSpinner:SetValue(g * 255)
    self.blueSpinner:SetValue(b * 255)
    if self.hasAlpha then
        self.alphaSpinner:SetValue(a * 255)
    end
    self.isUpdatingColors = false
     if self.colorSelectedCallback then
          if self.hasAlpha then
               self.colorSelectedCallback(r, g, b, a)
          else
               self.colorSelectedCallback(r, g, b)
          end
     end
end
function ZO_ColorPicker:OnColorSet(r, g, b)
     self:UpdateColors(r, g, b, self.alphaSlider:GetValue())
end
function ZO_ColorPicker:OnValueSet(value)
     self.colorSelect:SetValue(value)
end
function ZO_ColorPicker:OnAlphaSet(value)
     local r, g, b = self.colorSelect:GetColorAsRGB()
     self:UpdateColors(r, g, b, value)
end
function ZO_ColorPicker:SetColor(r, g, b, a)
     self.colorSelect:SetColorAsRGB(r, g, b)
     self.valueSlider:SetValue(1 - self.colorSelect:GetValue())
     if self.hasAlpha then
          self.alphaSlider:SetValue(a or 1)
     end
     self:UpdateColors(r, g, b, a or 1)
end
function ZO_ColorPicker:GetColors()
     local r, g, b = self.colorSelect:GetColorAsRGB()
     if self.hasAlpha then
          return r, g, b, self.alphaSlider:GetValue()
     end
     return r, g, b
end
function ZO_ColorPicker:Confirm()
    ZO_Dialogs_ReleaseDialog("COLOR_PICKER")
end
function ZO_ColorPicker:Cancel()
     if self.colorSelectedCallback then
          if self.hasAlpha then
               self.colorSelectedCallback(self.initialR, self.initialG, self.initialB, self.initialA)
          else
               self.colorSelectedCallback(self.initialR, self.initialG, self.initialB)
          end
     end
     self:Confirm()
end
function ZO_ColorPicker:Show(colorSelectedCallback, r, g, b, a)
     self.initialR = r or 1
     self.initialG = g or 1
     self.initialB = b or 1
     self.initialA = a or 1
     self.hasAlpha = a ~= nil
    self:SetAlphaLimits(0, 1)
     self:SetColor(self.initialR, self.initialG, self.initialB, self.initialA)
    self.previewInitialTexture:SetColor(self.initialR, self.initialG, self.initialB, self.initialA)
    ZO_Dialogs_ShowDialog("COLOR_PICKER")
end
function ZO_ColorPicker:IsShown()
    return not self.control:IsHidden()
end
function ZO_ColorPicker:UpdateLayout()
     self.content:SetHeight(self.hasAlpha and FULL_HEIGHT or SMALL_HEIGHT)
     if self.hasAlpha then
          self.alphaLabel:SetHidden(false)
          self.alphaSlider:SetHidden(false)
        self.alphaSpinner:GetControl():SetHidden(false)
        self.alphaSlider:SetMinMax(self.alphaLowerLimit, self.alphaUpperLimit)
     else
          self.alphaLabel:SetHidden(true)
          self.alphaSlider:SetHidden(true)
        self.alphaSpinner:GetControl():SetHidden(true)
     end
end
function ZO_ColorPicker:SetAlphaLimits(alphaLowerLimit, alphaUpperLimit)
    self.alphaLowerLimit = alphaLowerLimit or 0
    self.alphaUpperLimit = alphaUpperLimit or 1
    self:UpdateLayout()
end
--[[ XML Handlers ]]--
     COLOR_PICKER = ZO_ColorPicker:New(self)
end