WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Colorpicker? (https://www.wowinterface.com/forums/showthread.php?t=58006)

LudiusMaximus 05-20-20 12:22 PM

Colorpicker?
 
Can anybody tell me how to use this?
https://wow.gamepedia.com/Using_the_ColorPickerFrame
seems to be outdated (global ShowColorPicker() does not exist any more)
just as everything I could find on the forums...

Xrystal 05-20-20 02:09 PM

I've not played with it myself but this is Blizzards files for it which has changed at least once for at least the last 3 expansions.

https://www.townlong-yak.com/framexm...ickerFrame.xml

ShowColorPicker is on that page you linked .. it's one you code in yourself it seems.

LudiusMaximus 05-20-20 04:22 PM

You are right. I completely missed that definition of ShowColorPicker() further above on the wiki page. Thanks!

LudiusMaximus 07-12-20 04:43 PM

Finally got back to this, and two questions arose:

Code:

function ShowColorPicker(r, g, b, a, changedCallback)
 ColorPickerFrame.hasOpacity, ColorPickerFrame.opacity = (a ~= nil), a;
 ColorPickerFrame.previousValues = {r,g,b,a};
 ColorPickerFrame.func, ColorPickerFrame.opacityFunc, ColorPickerFrame.cancelFunc =
  changedCallback, changedCallback, changedCallback;
 ColorPickerFrame:SetColorRGB(r,g,b);
 ColorPickerFrame:Hide(); -- Need to run the OnShow handler.
 ColorPickerFrame:Show();
end

local function myColorCallback(restore)
 local newR, newG, newB, newA;
 if restore then
  -- The user bailed, we extract the old color from the table created by ShowColorPicker.
  newR, newG, newB, newA = unpack(restore);
 else
  -- Something changed
  newA, newR, newG, newB = OpacitySliderFrame:GetValue(), ColorPickerFrame:GetColorRGB();
 end
 
 -- Update our internal storage.
 print(newR, newG, newB, newA);
end

ShowColorPicker(r, g, b, a, myColorCallback);


1st Question:
Code:

local myPackedColor = {1, 1, 1, 1};
ShowColorPicker(unpack(myPackedColor), myColorCallback);

When I do this, ShowColorPicker() only gets the first return value of unpack() as its first argument, the second argument is already the myColorCallback function. Is there a correct way of doing this? For other functions like e.g. SetColorTexture(unpack(myPackedColor)) it works.


2nd Question:
Is there a way to pass additional arguments to the changedCallback function? Or do I have to do this with "public" variables?

Fizzlemizz 07-12-20 08:09 PM

1. The vararg needs to go at th end.
Code:

local function ShowColorPicker(changedCallback, r, g, b, a)
    ....
end

Code:

local myPackedColor = {1, 1, 1, 1};
ShowColorPicker(myColorCallback, unpack(myPackedColor));

2. I'm not sure what you mean by "public"
Code:

local function changedCallback(self)
  if self.WhatEveryYouWantToCheck == xxx then
  end
end

The above is pretty public but you could use either local variables or variables in your addon table if you wanted to set them in a different .lua module

Code:

local addonName, addonTable = ...
local a, b, c
local function changedCallback(self)
  if a == x and b = y and not c then
      ...
  end
  if addonTable.SomeVariable == x then
      ...
  end
end


LudiusMaximus 07-13-20 04:47 AM

Thanks for the reply!
Quote:

Originally Posted by Fizzlemizz (Post 336326)
1. The vararg needs to go at th end.

Ah, that makes sense! :-)


Quote:

Originally Posted by Fizzlemizz (Post 336326)
2. I'm not sure what you mean by "public"

What I tried to say was, that I would prefer passing something from ShowColorPicker() to changedCallback() in a variable that is only accessible by these two functions.
Quote:

Originally Posted by Fizzlemizz (Post 336326)
Code:

local function changedCallback(self)
  if self.WhatEveryYouWantToCheck == xxx then
  end
end


What exactly is this "self" refering to here? The ColorPicker? In the example it seems like the only argument of the callback functions is "restore" if any. Or does this only apply to the cancelFunc?

Fizzlemizz 07-13-20 10:08 AM

Quote:

Originally Posted by LudiusMaximus (Post 336328)
What exactly is this "self" refering to here?

My bad, I was thinking of my derived ColourPicker button.

I guess the question becomes, what are you doing in ShowColorPicker to gather the variables you want to pass that you couldn't do directly in changedCallback?

If you're calling ShowColorPicker() as the result of a button click (like a target box to change colours as the picker changes), you can set the button as a key of the ColorPickerFrame.

Code:

local function myColorCallback()
    TextureToColour = ColorPickerFrame.colourBox
    local r, g, b = ColorPickerFrame:GetColorRGB()
    TextureToColour:SetVertexColor(r, g, b)
end

local function myColorCancel()
    TextureToColour = ColorPickerFrame.colourBox
    TextureToColour:SetVertexColor(unpack(TextureToColour.previousValues))
end

local function ShowColorPicker(self, r, g, b, a)
        self.previousValues = {r,g,b,a};
        ColorPickerFrame.colourBox = self
        ColorPickerFrame.hasOpacity, ColorPickerFrame.opacity = (a ~= nil), a;
        ColorPickerFrame:SetColorRGB(r,g,b);
        ColorPickerFrame.func, ColorPickerFrame.opacityFunc = myColorCallback, myColorCallback
        ColorPickerFrame.cancelFunc = myColorCancel
        ColorPickerFrame:Show()
end
local button = CreateFrame("Button", nil, UIParent)
button:SetSize(25, 25)
button:SetPoint("LEFT")
button.Texture = button:CreateTexture()
button.Texture:SetAllPoints()
button.Texture:SetTexture("Interface/BUTTONS/WHITE8X8")
button.Texture:SetVertexColor(1, 1, 0)
button:SetScript("OnClick", function(self)
        ShowColorPicker(self.Texture, self.Texture:GetVertexColor())
end)


LudiusMaximus 07-13-20 02:39 PM

All right. I mean, what I basically want to do is to use the color picker in several different places, changing a different color each time. So I have to let ColorPicker() know what variable it should change. Passing arguments to ColorPicker() like ColorPicker("variableName") ist straight forward, but my question was how I could pass "variableName" on to the callback functions.

Now I understand that I have to do this with another (what I called "public" as it can be accessed from anywhere in my addon) variable (be it ColorPicker.tmpVariableName or local tmpVariableName).

Thanks!


All times are GMT -6. The time now is 02:43 AM.

vBulletin © 2020, Jelsoft Enterprises Ltd
© 2004 - 2020 MMOUI