Back to Home

ESO Lua File v101043

common/zo_colorpicker/zo_colorpicker_shared.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
--[[ Provides a simple color picker dialog
Usage:
colorPickerObject: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
]]
--
ZO_ColorPicker_Shared = ZO_Object:Subclass()
function ZO_ColorPicker_Shared:New(...)
    local object = ZO_Object.New(self)
    object:Initialize(...)
    return object
end
function ZO_ColorPicker_Shared:Initialize(control, dialogName)
    self.control = control
    self.dialogName = dialogName
    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.colorSelect:SetHandler("OnColorSelected", function(control, r, g, b) self:OnColorSet(r, g, b) end)
     self.valueSlider = content:GetNamedChild("Value")
     self.valueSlider:GetThumbTextureControl():SetDrawLayer(3)
     self.valueTexture = self.valueSlider:GetNamedChild("Texture")
    self.valueSlider:SetHandler("OnValueChanged", function(control, value) 
        self:OnValueSet(1 - value)
    end)
     self.alphaLabel = content:GetNamedChild("AlphaLabel")
     self.alphaSlider = content:GetNamedChild("Alpha")
    local alphaThumbTexture = self.alphaSlider:GetThumbTextureControl()
    alphaThumbTexture:SetTextureRotation(ZO_HALF_PI)
     alphaThumbTexture:SetDrawLayer(3)
     self.alphaTexture = self.alphaSlider:GetNamedChild("Texture")
    self.alphaSlider:SetHandler("OnValueChanged", function(control, value)
        self:OnAlphaSet(value)
    end)
     self.previewControl = content:GetNamedChild("Preview")
     self.previewInitialTexture = self.previewControl:GetNamedChild("TextureBottom")
    self.previewCurrentTexture = self.previewControl:GetNamedChild("TextureTop")
end
function ZO_ColorPicker_Shared:UpdateColors(r, g, b, a)
     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)
end
function ZO_ColorPicker_Shared:OnColorSet(r, g, b)
     self:UpdateColors(r, g, b, self.alphaSlider:GetValue())
end
function ZO_ColorPicker_Shared:OnValueSet(value)
     self.colorSelect:SetValue(value)
end
function ZO_ColorPicker_Shared:OnAlphaSet(value)
     local r, g, b = self.colorSelect:GetColorAsRGB()
     self:UpdateColors(r, g, b, value)
end
function ZO_ColorPicker_Shared: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_Shared: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_Shared:Confirm()
     if self.colorSelectedCallback then
          self.colorSelectedCallback(self:GetColors())
     end
    ZO_Dialogs_ReleaseDialog(self.dialogName)
end
function ZO_ColorPicker_Shared: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
    ZO_Dialogs_ReleaseDialog(self.dialogName)
end
function ZO_ColorPicker_Shared: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_ShowPlatformDialog(self.dialogName)
end
function ZO_ColorPicker_Shared:IsShown()
    return not self.control:IsHidden()
end
function ZO_ColorPicker_Shared:UpdateLayout()
    if self.hasAlpha then
         self.content:SetHeight(self.hasAlphaHeight)
    else
        self.content:SetHeight(self.doesntHaveAlphaHeight)
    end
     if self.hasAlpha then
          self.alphaLabel:SetHidden(false)
          self.alphaSlider:SetHidden(false)
        self.alphaSlider:SetMinMax(self.alphaLowerLimit, self.alphaUpperLimit)
     else
          self.alphaLabel:SetHidden(true)
          self.alphaSlider:SetHidden(true)
     end
end
function ZO_ColorPicker_Shared:SetHasAlphaContentHeight(height)
    self.hasAlphaHeight = height
end
function ZO_ColorPicker_Shared:SetDoesntHaveAlphaContentHeight(height)
    self.doesntHaveAlphaHeight = height
end
function ZO_ColorPicker_Shared:SetAlphaLimits(alphaLowerLimit, alphaUpperLimit)
    self.alphaLowerLimit = alphaLowerLimit or 0
    self.alphaUpperLimit = alphaUpperLimit or 1
    self:UpdateLayout()
end