Skip to content

Commit

Permalink
Fix broken color picker in Retail
Browse files Browse the repository at this point in the history
  • Loading branch information
seblindfors committed Jan 20, 2024
1 parent a675186 commit 9b7b0f5
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 34 deletions.
51 changes: 34 additions & 17 deletions ConsolePort/Controller/Convenience.lua
Expand Up @@ -59,9 +59,12 @@ end

-- Use color picker frame with sticks
if ColorPickerFrame then
local OkayButton = ColorPickerOkayButton or ColorPickerFrame.Footer.OkayButton;
local CancelButton = ColorPickerCancelButton or ColorPickerFrame.Footer.CancelButton;

local controls = {
PAD1 = ColorPickerOkayButton;
PAD2 = ColorPickerCancelButton;
PAD1 = OkayButton;
PAD2 = CancelButton;
};
local tooltipLines = {
PADLSTICKUP = CreateColor(CPAPI.HSV2RGB(270, 0.5, 1)):WrapTextInColorCode(L'Purple');
Expand All @@ -78,39 +81,53 @@ if ColorPickerFrame then
end

-- Handle color change
local delta, lightness, oldNode = 40, 1;
local delta, lightness, opacityInversion, oldNode = 40, 1, ColorPickerFrameMixin and 1 or -1;

local SetColorRGB, GetColorRGB, SetOpacity, GetOpacity;
if ColorPickerFrameMixin then
SetColorRGB = GenerateClosure(ColorPickerFrame.Content.ColorPicker.SetColorRGB, ColorPickerFrame.Content.ColorPicker)
GetColorRGB = GenerateClosure(ColorPickerFrame.Content.ColorPicker.GetColorRGB, ColorPickerFrame.Content.ColorPicker)
SetOpacity = GenerateClosure(ColorPickerFrame.Content.ColorPicker.SetColorAlpha, ColorPickerFrame.Content.ColorPicker)
GetOpacity = GenerateClosure(ColorPickerFrame.Content.ColorPicker.GetColorAlpha, ColorPickerFrame.Content.ColorPicker)
else
SetColorRGB = GenerateClosure(ColorPickerFrame.SetColorRGB, ColorPickerFrame)
GetColorRGB = GenerateClosure(ColorPickerFrame.GetColorRGB, ColorPickerFrame)
SetOpacity = GenerateClosure(OpacitySliderFrame.SetValue, OpacitySliderFrame)
GetOpacity = GenerateClosure(OpacitySliderFrame.GetValue, OpacitySliderFrame)
end


local function ColorPickerStickToRGB(self, x, y)
local function ColorPickerStickToRGB(x, y)
local radius, theta = CPAPI.XY2Polar(x, y)
local deg = CPAPI.Rad2Deg(theta)
local r, g, b = CPAPI.HSV2RGB(deg, radius, lightness)
self:SetColorRGB(r, g, b)
SetColorRGB(r, g, b)
end

local function ColorPickerStickSaturation(self, y)
local r, g, b = self:GetColorRGB()
local function ColorPickerStickSaturation(y)
local r, g, b = GetColorRGB()
lightness = Clamp(lightness + y / delta, 0, 1);
-- Handle case where we're picking a shade of gray
if (r == g and g == b) then
self:SetColorRGB(lightness, lightness, lightness)
SetColorRGB(lightness, lightness, lightness)
end
end

local function OpacitySliderStickValue(self, x)
local opacityDelta = -x / delta;
local a = self:GetValue()
self:SetValue(a + opacityDelta)
local function OpacitySliderStickValue(x)
local opacityDelta = x * opacityInversion / delta;
local a = GetOpacity()
SetOpacity(a + opacityDelta)
end

-- Scripts
ColorPickerFrame:SetScript('OnGamePadStick', function(self, stick, x, y, len)
if ( stick == 'Left' ) then
ColorPickerStickToRGB(self, x, y)
ColorPickerStickToRGB(x, y)
elseif ( stick == 'Right' and len > .1 ) then
if (math.abs(x) > math.abs(y) and OpacitySliderFrame and OpacitySliderFrame:IsShown()) then
OpacitySliderStickValue(OpacitySliderFrame, x)
if (math.abs(x) > math.abs(y)) then
OpacitySliderStickValue(x)
else
ColorPickerStickSaturation(self, y)
ColorPickerStickSaturation(y)
end
end
end)
Expand All @@ -121,7 +138,7 @@ if ColorPickerFrame then
end)
ColorPickerFrame:HookScript('OnShow', function(self)
oldNode = ConsolePort:GetCursorNode()
ConsolePort:SetCursorNodeIfActive(ColorPickerOkayButton, true)
ConsolePort:SetCursorNodeIfActive(OkayButton, true)

local device = db('Gamepad/Active')
if device then
Expand Down
63 changes: 46 additions & 17 deletions ConsolePort_Config/Widgets.lua
Expand Up @@ -795,37 +795,66 @@ local Color = CreateWidget('Color', Widget, {
function Color:OnClick(button)
self:Uncheck()
if (button == 'LeftButton') then
local color, swatch, opacity = ColorPickerFrame, ColorSwatch, OpacitySliderFrame;
local picker, opacity = ColorPickerFrame, OpacitySliderFrame or OpacityFrameSlider;

local function GetOpacity()
if picker.GetColorAlpha then
return 1 - picker:GetColorAlpha()
end
return opacity:GetValue()
end

local function UnpackOldColor(old)
if #old > 0 then
return unpack(old)
end
return old.r, old.g, old.b, old.a;
end

local function OnColorChanged()
if (color.owner == self) then
local r, g, b = color:GetColorRGB()
local a = opacity:GetValue()
if (picker.extraInfo == self) then
local r, g, b = picker:GetColorRGB()
local a = GetOpacity()
self:Set(r, g, b, 1 - a)
self:OnValueChanged(self:Get())
end
end

local function OnColorCancel(oldColor)
if (color.owner == self) then
self:Set(unpack(oldColor))
if (picker.extraInfo == self) then
self:Set(UnpackOldColor(oldColor))
self:OnValueChanged(self:Get())
end
end

local r, g, b, a = self:GetRGBA(self:Get())
color:Hide()
color:SetColorRGB(r, g, b, a)
color.hasOpacity = true;
color.opacity = 1 - (a or 0);
color.previousValues = {r, g, b, a};
color.func = OnColorChanged;
color.cancelFunc = OnColorCancel;
color.opacityFunc = OnColorChanged;
color.owner = self;
color:Show()
swatch:SetColorTexture(r, g, b)
self:SetupColorPickerAndShow(picker, {
r = r, g = g, b = b, a = a,
opacity = a or 0,
hasOpacity = true,
swatchFunc = OnColorChanged,
opacityFunc = OnColorChanged,
cancelFunc = OnColorCancel,
extraInfo = self,
}, ColorSwatch)
end
end

function Color:SetupColorPickerAndShow(picker, info, swatch)
if picker.SetupColorPickerAndShow then
return picker:SetupColorPickerAndShow(info)
end
picker:Hide()
picker:SetColorRGB(info.r, info.g, info.b, info.a)
picker.hasOpacity = info.hasOpacity;
picker.opacity = 1 - info.opacity;
picker.previousValues = {info.r, info.g, info.b, info.a};
picker.func = info.swatchFunc;
picker.cancelFunc = info.cancelFunc;
picker.opacityFunc = info.swatchFunc;
picker.extraInfo = self;
picker:Show()
swatch:SetColorTexture(info.r, info.g, info.b)
end

function Color:Set(r, g, b, a)
Expand Down

0 comments on commit 9b7b0f5

Please sign in to comment.