WoW:Using the ColorPickerFrame: Difference between revisions

From AddOn Studio
Jump to navigation Jump to search
(colon entities)
Line 1: Line 1:
== What is it? ==
{{UIHowTo}}
'''ColorPickerFrame''' is a FrameXML-provided frame that is used by the UI to allow the user to customize color and opacity properties of various objects. The frame can also be used by third-party addons.


The ColorPickerFrame (See: [[XML_User_Interface#ColorSelect]]) is the default WoW frame to easily select color and opacity using a GUI.
== Overview ==
The ColorPickerFrame comes with customizable functions, which are next.
There is only one instance of the ColorPickerFrame, and, as such, only color selection request can be processed at a time. When the player interacts with the color picker, the frame calls functions assigned to the following variables:
; ColorPickerFrame.func () : Called when color selection is changed.
; ColorPickerFrame.opacityFunc () : Called when opacity is changed.
; ColorPickerFrame.cancelFunc (previousValues) : Called when the user cancels color modification. previousValues is the value assigned to ColorPickerFrame.previousValues.


== ColorPickerFrame functions ==
The opacity slider's visibility is controlled by the <code>ColorPickerFrame.hasOpacity</code> boolean value.


ColorPickerFrame.func;
== Showing the color picker ==
--Called on changing the color wheel
When you want to prompt the user to select a color, you need to assign your own callback functions to the ColorPickerFrame variables, set the currently selected color to your previous value, and show the ColorPickerFrame.


ColorPickerFrame.opacityFunc;
To set the initial color and opacity values, you need to:
--Called on changing the opacity slider
; ColorPickerFrame&#58;SetColorRGB(r,g,b) : Sets the initial color.
; ColorPickerFrame.opacity : Place initial opacity (if relevant) into this variable; ColorPickerFrame's OnShow script will update the relevant slider.


  ColorPickerFrame.cancelFunc;
The following example function performs all of the required tasks: sets the initial color, enables/disables the opacity control and registers your callback function before showing the color picker.
--Called on hitting CANCEL button OR pressing ESCAPE.
  function ShowColorPicker(r, g, b, a, changedCallback)
--cancelFunc is called with the argument ColorPickerFrame.previousValues so set that when
  ColorPickerFrame:SetColorRGB(r,g,b);
  --you show the frame
  ColorPickerFrame.hasOpacity, ColorPickerFrame.opacity = (a ~= nil), a;
  ColorPickerFrame.previousValue = {r,g,b,a};
  ColorPickerFrame.func, ColorPickerFrame.opacityFunc, ColorPickerFrame.cancelFunc =
  changedCallback, changedCallback, changedCallback;
  ColorPickerFrame:Hide(); -- Need to run the OnShow handler.
  ColorPickerFrame:Show();
  end


These functions can be specified by you to do something. The best example is setting some variables to the selected color as it changes. Check the following code:
== Responding to user interactions ==
The functions assigned to the ColorPickerFrame table will be called when the user alters the color; your registered callback function should handle the changed color by updating your internal color storage and the display. The function is called without arguments, except when the user cancels the color selection (in which case, the previousValue field on ColorPickerFrame is passed as the sole argument).


You can fetch the user-selected color values using two functions:
; r, g, b = ColorPickerFrame&#58;GetColorRGB() : returns the color component, each value in the [0, 1] range.
; a = OpacitySliderFrame&#58;GetValue() : returns the alpha component in the [0, 1] range.


ColorPickerFrame.func = MY_COLOR_FUNCTION
An example callback handler function is shown below.
  ColorPickerFrame.cancelFunc = MY_CANCEL_FUNCTION
  local r,g,b,a = 1, 0, 0, 1;
 
  function MY_COLOR_FUNCTION()
  local function myColorCallback(restore)
   local R,G,B = ColorPickerFrame:GetColorRGB();
  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.
  r, g, b, a = newR, newG, newB, newA;
  -- And update any UI elements that use this color...
  end
  end
function MY_CANCEL_FUNCTION(prevvals)
  local R,G,B = unpack(prevvals)
end
All the functions can be set in that manner.
== ColorPickerFrame Misc ==
ColorPickerFrame.hasOpacity = boolean;
--Sets whether or not to show the opacity slider.
--You MUST set this explicitly every time as the default UI will not reset it from a value
--set previously.


ColorPickerFrame.opacity = Alpha;
This can then be used with the previously described ShowColorPicker function:
--Sets the initial value of the opacity slider.
  ShowColorPicker(r, g, b, a, myColorCallback);
 
ColorPickerFrame.previousValues = {R, G, B}
--Sets the values passed to the cancelFunc when Esc is pressed or the close button is pressed.
 
ColorSwatch:SetTexture(R, G, B);
--Sets the color square to show specified color
 
  ColorPickerFrame:Show();
--Shows the frame
 
ColorPickerFrame:Hide();
--Hides the frame
 
ColorPickerFrame:SetColorRGB(R, G, B);
--Set the color in the color frame
 
local R,G,B = ColorPickerFrame:GetColorRGB();
--Gets the color from the color frame
 
local A = OpacitySliderFrame:GetValue();
--Gets the opacity from the opacity slider on the color frame.
 
== Examples of use ==
 
Open frame with specified values:
 
ColorPickerFrame:Show();
ColorPickerFrame:SetColorRGB(R, G, B);
 
Set R, G, and B to the ColorWheel's colors on changing the selected color
function MY_COLOR_FUNCTION()
  local R,G,B = ColorPickerFrame:GetColorRGB();
end
ColorPickerFrame.func = MY_COLOR_FUNCTION
[[Category:HOWTOs|Use the ColorPickerFrame]]

Revision as of 23:00, 12 July 2009

HOWTOs

ColorPickerFrame is a FrameXML-provided frame that is used by the UI to allow the user to customize color and opacity properties of various objects. The frame can also be used by third-party addons.

Overview

There is only one instance of the ColorPickerFrame, and, as such, only color selection request can be processed at a time. When the player interacts with the color picker, the frame calls functions assigned to the following variables:

ColorPickerFrame.func ()
Called when color selection is changed.
ColorPickerFrame.opacityFunc ()
Called when opacity is changed.
ColorPickerFrame.cancelFunc (previousValues)
Called when the user cancels color modification. previousValues is the value assigned to ColorPickerFrame.previousValues.

The opacity slider's visibility is controlled by the ColorPickerFrame.hasOpacity boolean value.

Showing the color picker

When you want to prompt the user to select a color, you need to assign your own callback functions to the ColorPickerFrame variables, set the currently selected color to your previous value, and show the ColorPickerFrame.

To set the initial color and opacity values, you need to:

ColorPickerFrame:SetColorRGB(r,g,b)
Sets the initial color.
ColorPickerFrame.opacity
Place initial opacity (if relevant) into this variable; ColorPickerFrame's OnShow script will update the relevant slider.

The following example function performs all of the required tasks: sets the initial color, enables/disables the opacity control and registers your callback function before showing the color picker.

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

Responding to user interactions

The functions assigned to the ColorPickerFrame table will be called when the user alters the color; your registered callback function should handle the changed color by updating your internal color storage and the display. The function is called without arguments, except when the user cancels the color selection (in which case, the previousValue field on ColorPickerFrame is passed as the sole argument).

You can fetch the user-selected color values using two functions:

r, g, b = ColorPickerFrame:GetColorRGB()
returns the color component, each value in the [0, 1] range.
a = OpacitySliderFrame:GetValue()
returns the alpha component in the [0, 1] range.

An example callback handler function is shown below.

local r,g,b,a = 1, 0, 0, 1;

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.
 r, g, b, a = newR, newG, newB, newA;
 -- And update any UI elements that use this color...
end

This can then be used with the previously described ShowColorPicker function:

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