Eye dropper functionality

Adds eye dropper functionality
adds eye dropper button image supplied by jeff
adds a few missing asset files (due to in asset browser scan and add all loose files)
This commit is contained in:
marauder2k7 2025-01-23 10:18:18 +00:00
parent d5d7f9b91b
commit 46192c3709
46 changed files with 292 additions and 106 deletions

View file

@ -29,6 +29,7 @@
#include "gui/controls/guiColorPicker.h" #include "gui/controls/guiColorPicker.h"
#include "gfx/primBuilder.h" #include "gfx/primBuilder.h"
#include "gfx/gfxDrawUtil.h" #include "gfx/gfxDrawUtil.h"
#include "postFx/postEffectManager.h"
IMPLEMENT_CONOBJECT(GuiColorPickerCtrl); IMPLEMENT_CONOBJECT(GuiColorPickerCtrl);
@ -47,15 +48,23 @@ GuiColorPickerCtrl::GuiColorPickerCtrl()
mActive = true; mActive = true;
mSelectorGap = 1; mSelectorGap = 1;
mActionOnMove = false; mActionOnMove = false;
mDropperActive = false;
mShowReticle = true; mShowReticle = true;
mSelectedHue = 0; mSelectedHue = 0;
mSelectedAlpha = 255; mSelectedAlpha = 255;
mSelectedSaturation = 100; mSelectedSaturation = 100;
mSelectedBrightness = 100; mSelectedBrightness = 100;
eyeDropperPos = Point2I::Zero;
eyeDropperCap = NULL;
} }
GuiColorPickerCtrl::~GuiColorPickerCtrl() GuiColorPickerCtrl::~GuiColorPickerCtrl()
{ {
if (eyeDropperCap != NULL)
{
delete eyeDropperCap;
eyeDropperCap = NULL;
}
} }
ImplementEnumType( GuiColorPickMode, ImplementEnumType( GuiColorPickMode,
@ -314,6 +323,32 @@ void GuiColorPickerCtrl::renderAlphaSelector(RectI& bounds)
GFX->getDrawUtil()->drawRectFill(selectorRect, currentColor, 2.0f, ColorI::WHITE); GFX->getDrawUtil()->drawRectFill(selectorRect, currentColor, 2.0f, ColorI::WHITE);
} }
void GuiColorPickerCtrl::renderEyeDropper()
{
if (eyeDropperCap != NULL)
{
GFX->getDrawUtil()->drawBitmap(eyeHandle, getRoot()->getPosition());
Point2I resolution = getRoot()->getExtent();
Point2I magnifierSize(100, 100);
Point2I magnifierPosition = eyeDropperPos + Point2I(20, 20);
// Adjust position to ensure magnifier stays on screen
if (magnifierPosition.x + magnifierSize.x > resolution.x)
magnifierPosition.x = eyeDropperPos.x - magnifierSize.x - 20;
if (magnifierPosition.y + magnifierSize.y > resolution.y)
magnifierPosition.y = eyeDropperPos.y - magnifierSize.y - 20;
RectI magnifierBounds(magnifierPosition, magnifierSize);
ColorI currentColor;
currentColor.set(ColorI::Hsb(mSelectedHue, mSelectedSaturation, mSelectedBrightness));
currentColor.alpha = mSelectedAlpha;
GFX->getDrawUtil()->drawRectFill(magnifierBounds, currentColor, 2.0f, ColorI::BLACK);
}
}
void GuiColorPickerCtrl::onRender(Point2I offset, const RectI& updateRect) void GuiColorPickerCtrl::onRender(Point2I offset, const RectI& updateRect)
{ {
if (mStateBlock.isNull()) if (mStateBlock.isNull())
@ -356,6 +391,15 @@ void GuiColorPickerCtrl::onRender(Point2I offset, const RectI& updateRect)
if (mShowReticle) renderAlphaSelector(boundsRect); if (mShowReticle) renderAlphaSelector(boundsRect);
break; break;
} }
case GuiColorPickerCtrl::pDropperBackground:
{
if (mDropperActive)
{
// Render the magnified view of our currently selected color.
renderEyeDropper();
}
break;
}
default: default:
break; break;
} }
@ -364,72 +408,31 @@ void GuiColorPickerCtrl::onRender(Point2I offset, const RectI& updateRect)
renderChildControls(offset, updateRect); renderChildControls(offset, updateRect);
} }
Point2I GuiColorPickerCtrl::findColor(const LinearColorF & color, const Point2I& offset, const Point2I& resolution, GBitmap& bmp)
{
Point2I ext = getExtent();
Point2I closestPos(-1, -1);
/* Debugging
char filename[256];
dSprintf( filename, 256, "%s.%s", "colorPickerTest", "png" );
// Open up the file on disk.
FileStream fs;
if ( !fs.open( filename, Torque::FS::File::Write ) )
Con::errorf( "GuiObjectView::saveAsImage() - Failed to open output file '%s'!", filename );
else
{
// Write it and close.
bmp.writeBitmap( "png", fs );
fs.close();
}
*/
ColorI tmp;
U32 buf_x;
U32 buf_y;
LinearColorF curColor;
F32 val(10000.0f);
F32 closestVal(10000.0f);
bool closestSet = false;
for (S32 x = 0; x < ext.x; x++)
{
for (S32 y = 0; y < ext.y; y++)
{
buf_x = offset.x + x;
buf_y = (resolution.y - (offset.y + y));
buf_y = resolution.y - buf_y;
//Get the color at that position
bmp.getColor(buf_x, buf_y, tmp);
curColor = (LinearColorF)tmp;
//Evaluate how close the color is to our desired color
val = mFabs(color.red - curColor.red) + mFabs(color.green - curColor.green) + mFabs(color.blue - curColor.blue);
if (!closestSet)
{
closestVal = val;
closestPos.set(x, y);
closestSet = true;
}
else if (val < closestVal)
{
closestVal = val;
closestPos.set(x, y);
}
}
}
return closestPos;
}
void GuiColorPickerCtrl::onMouseDown(const GuiEvent &event) void GuiColorPickerCtrl::onMouseDown(const GuiEvent &event)
{ {
if (!mActive) if (!mActive)
return; return;
// we need to do this first.
if (mDisplayMode == GuiColorPickerCtrl::pDropperBackground) {
if (mDropperActive)
{
mDropperActive = false;
//getRoot()->pushObjectToBack(this);
onAction();
mouseUnlock();
if (eyeDropperCap != NULL)
{
delete eyeDropperCap;
eyeDropperCap = NULL;
}
}
return;
}
mouseLock(this); mouseLock(this);
@ -574,6 +577,29 @@ void GuiColorPickerCtrl::onMouseDragged(const GuiEvent &event)
void GuiColorPickerCtrl::onMouseMove(const GuiEvent &event) void GuiColorPickerCtrl::onMouseMove(const GuiEvent &event)
{ {
if (mDisplayMode != pDropperBackground)
return;
if (!mDropperActive)
return;
// should not need globalToLocal as we are capturing the whole screen.
eyeDropperPos = globalToLocalCoord(event.mousePoint);
if (eyeDropperCap != NULL)
{
// Sample the pixel color at the mouse position. Mouse position should translate directly.
ColorI sampledColor;
eyeDropperCap->getColor(eyeDropperPos.x, eyeDropperPos.y, sampledColor);
// Convert the sampled color to HSB
ColorI::Hsb hsb = sampledColor.getHSB();
mSelectedHue = hsb.hue;
mSelectedSaturation = hsb.sat;
mSelectedBrightness = hsb.brightness;
mSelectedAlpha = sampledColor.alpha;
}
} }
void GuiColorPickerCtrl::onMouseEnter(const GuiEvent &event) void GuiColorPickerCtrl::onMouseEnter(const GuiEvent &event)
@ -587,6 +613,15 @@ void GuiColorPickerCtrl::onMouseLeave(const GuiEvent &)
mMouseOver = false; mMouseOver = false;
} }
void GuiColorPickerCtrl::onMouseUp(const GuiEvent&)
{
//if we released the mouse within this control, perform the action
if (mActive && mMouseDown)
mMouseDown = false;
mouseUnlock();
}
void GuiColorPickerCtrl::setSelectedHue(const F64& hueValue) void GuiColorPickerCtrl::setSelectedHue(const F64& hueValue)
{ {
if (hueValue < 0) if (hueValue < 0)
@ -656,13 +691,34 @@ void GuiColorPickerCtrl::setSelectedAlpha(const F64& alphaValue)
mSelectedAlpha = alphaValue; mSelectedAlpha = alphaValue;
} }
void GuiColorPickerCtrl::onMouseUp(const GuiEvent &) void GuiColorPickerCtrl::activateEyeDropper()
{ {
//if we released the mouse within this control, perform the action // Make sure we are a pDropperBackground
if (mActive && mMouseDown) if (mDisplayMode == GuiColorPickerCtrl::pDropperBackground)
mMouseDown = false; {
mouseLock(this); // take over!
mouseUnlock(); setFirstResponder(); // we need this to be first responder regardless.
//getRoot()->bringObjectToFront(this);
mDropperActive = true;
// Set up our resolution.
Point2I resolution = getRoot()->getExtent();
// Texture handle to resolve the target to.
eyeHandle.set(resolution.x, resolution.y, GFXFormatR8G8B8A8_SRGB, &GFXRenderTargetSRGBProfile, avar("%s() - bb (line %d)", __FUNCTION__, __LINE__));
// Get our active render target (should be backbuffer).
eyeHandle = PFXMGR->getBackBufferTex();
if (eyeHandle.isValid())
{
eyeDropperCap = new GBitmap(eyeHandle.getWidth(), eyeHandle.getHeight(), false, GFXFormatR8G8B8A8);
eyeHandle.copyToBmp(eyeDropperCap);
}
}
} }
/// <summary> /// <summary>
@ -673,6 +729,14 @@ DefineEngineMethod(GuiColorPickerCtrl, executeUpdate, void, (), , "Execute the o
object->onAction(); object->onAction();
} }
/// <summary>
/// This command should only be used with guiColorPicker in pDropperBackground mode.
/// </summary>
DefineEngineMethod(GuiColorPickerCtrl, activateEyeDropper, void, (), , "Activate the dropper mode.")
{
object->activateEyeDropper();
}
DefineEngineMethod(GuiColorPickerCtrl, setSelectedHue, void, (F64 hueValue), , "Sets the selected hue value should be 0-360.") DefineEngineMethod(GuiColorPickerCtrl, setSelectedHue, void, (F64 hueValue), , "Sets the selected hue value should be 0-360.")
{ {
object->setSelectedHue(hueValue); object->setSelectedHue(hueValue);

View file

@ -108,23 +108,27 @@ class GuiColorPickerCtrl : public GuiControl
/// <param name="bounds">The bounds of the ctrl.</param> /// <param name="bounds">The bounds of the ctrl.</param>
void renderAlphaSelector(RectI& bounds); void renderAlphaSelector(RectI& bounds);
void renderEyeDropper();
/// @name Core Variables /// @name Core Variables
/// @{ /// @{
PickMode mDisplayMode; ///< Current color display mode of the selector PickMode mDisplayMode; ///< Current color display mode of the selector
SelectorMode mSelectorMode; ///< Current color display mode of the selector SelectorMode mSelectorMode; ///< Current color display mode of the selector
F64 mSelectedHue; F64 mSelectedHue;
F64 mSelectedSaturation; F64 mSelectedSaturation;
F64 mSelectedBrightness; F64 mSelectedBrightness;
F64 mSelectedAlpha; F64 mSelectedAlpha;
Point2I eyeDropperPos;
GBitmap* eyeDropperCap;
GFXTexHandle eyeHandle;
bool mMouseOver; ///< Mouse is over? bool mDropperActive; ///< Is the eye dropper active?
bool mMouseDown; ///< Mouse button down? bool mMouseOver; ///< Mouse is over?
bool mActionOnMove; ///< Perform onAction() when position has changed? bool mMouseDown; ///< Mouse button down?
bool mShowReticle; ///< Show reticle on render bool mActionOnMove; ///< Perform onAction() when position has changed?
bool mShowReticle; ///< Show reticle on render
Point2I findColor(const LinearColorF & color, const Point2I& offset, const Point2I& resolution, GBitmap& bmp); S32 mSelectorGap; ///< The half-way "gap" between the selector pos and where the selector is allowed to draw.
S32 mSelectorGap; ///< The half-way "gap" between the selector pos and where the selector is allowed to draw.
GFXStateBlockRef mStateBlock; GFXStateBlockRef mStateBlock;
/// @} /// @}
@ -181,6 +185,9 @@ class GuiColorPickerCtrl : public GuiControl
/// <param name="alphaValue">Alpha value, 0 - 255.</param> /// <param name="alphaValue">Alpha value, 0 - 255.</param>
void setSelectedAlpha(const F64& alphaValue); void setSelectedAlpha(const F64& alphaValue);
F64 getSelectedAlpha() { return mSelectedAlpha; } F64 getSelectedAlpha() { return mSelectedAlpha; }
void activateEyeDropper();
}; };
typedef GuiColorPickerCtrl::PickMode GuiColorPickMode; typedef GuiColorPickerCtrl::PickMode GuiColorPickMode;

View file

@ -1,36 +1,25 @@
//--- OBJECT WRITE BEGIN --- //--- OBJECT WRITE BEGIN ---
$guiContent = new GuiColorPickerCtrl(ColorPickerDlg,EditorGuiGroup) { $guiContent = new GuiColorPickerCtrl(ColorPickerDlg,EditorGuiGroup) {
displayMode = "Dropper"; // this makes the background visible displayMode = "Dropper";
actionOnMove = "1"; extent = "1024 768";
position = "0 0"; profile = "GuiDefaultProfile";
extent = "1024 768"; command = "%selHue = ColorPickerDlg.getSelectedHue();\n%selSat = ColorPickerDlg.getSelectedSaturation();\n%selBright = ColorPickerDlg.getSelectedBrightness();\n%selAlpha = ColorPickerDlg.getSelectedAlpha();\n\nColorHueRange.setSelectedHue(%selHue);\nColorHueRange.executeUpdate();\n\nColorBlendRange.setSelectedBrightness(%selBright);\nColorBlendRange.setSelectedSaturation(%selSat);\nColorBlendRange.executeUpdate();\n\nColorAlphaRange.setSelectedAlpha(%selAlpha);\nColorAlphaRange.executeUpdate();\n\nColorEyeDropperButton.setStateOn(false);";
minExtent = "8 2"; tooltipProfile = "GuiToolTipProfile";
horizSizing = "right"; isContainer = "1";
vertSizing = "bottom";
profile = "ToolsGuiDefaultProfile";
visible = "1";
active = "1";
Clickable = "1";
AffectChildren = "1";
tooltipProfile = "GuiToolTipProfile";
hovertime = "1000";
isContainer = "1";
canSave = "1";
canSaveDynamicFields = "0";
new GuiWindowCtrl() { new GuiWindowCtrl() {
text = " "; text = " ";
resizeWidth = "0"; resizeWidth = "0";
resizeHeight = "0"; resizeHeight = "0";
canMinimize = "0"; canMinimize = "0";
canMaximize = "0"; canMaximize = "0";
closeCommand = "DoColorPickerCancelCallback(); ColorPickerDlg.getRoot().popDialog(ColorPickerDlg);"; closeCommand = "DoColorPickerCancelCallback(); ColorPickerDlg.getRoot().popDialog(ColorPickerDlg);";
position = "33 33"; position = "33 33";
extent = "271 574"; extent = "271 574";
horizSizing = "windowRelative"; horizSizing = "windowRelative";
vertSizing = "windowRelative"; vertSizing = "windowRelative";
profile = "ToolsGuiWindowProfile"; profile = "ToolsGuiWindowProfile";
tooltipProfile = "ToolsGuiToolTipProfile"; tooltipProfile = "ToolsGuiToolTipProfile";
new GuiStackControl() { new GuiStackControl() {
padding = "5"; padding = "5";
@ -268,16 +257,25 @@ $guiContent = new GuiColorPickerCtrl(ColorPickerDlg,EditorGuiGroup) {
text = "Apply"; text = "Apply";
position = "211 1"; position = "211 1";
extent = "52 20"; extent = "52 20";
command = "DoColorPickerCallback();";
profile = "ToolsGuiButtonProfile"; profile = "ToolsGuiButtonProfile";
command = "DoColorPickerCallback();";
tooltipProfile = "GuiToolTipProfile"; tooltipProfile = "GuiToolTipProfile";
}; };
new GuiButtonCtrl() { new GuiButtonCtrl() {
text = "Cancel"; text = "Cancel";
position = "211 24"; position = "211 24";
extent = "52 20"; extent = "52 20";
command = "DoColorPickerCancelCallback();";
profile = "ToolsGuiButtonProfile"; profile = "ToolsGuiButtonProfile";
command = "DoColorPickerCancelCallback();";
tooltipProfile = "GuiToolTipProfile";
};
new GuiBitmapButtonCtrl(ColorEyeDropperButton) {
BitmapAsset = "ToolsModule:eyedropper_n_image";
buttonType = "ToggleButton";
position = "223 56";
extent = "25 25";
profile = "ToolsGuiCheckBoxProfile";
command = "ColorPickerDlg.activateEyeDropper();";
tooltipProfile = "GuiToolTipProfile"; tooltipProfile = "GuiToolTipProfile";
}; };
}; };

View file

@ -0,0 +1,3 @@
<ImageAsset
AssetName="add-simgroup-btn_ctrl_i_image"
imageFile="@assetFile=add-simgroup-btn_ctrl_i.png"/>

View file

@ -0,0 +1,3 @@
<ImageAsset
AssetName="add-simgroup-btn_i_image"
imageFile="@assetFile=add-simgroup-btn_i.png"/>

View file

@ -0,0 +1,3 @@
<ImageAsset
AssetName="camera-btn_i_image"
imageFile="@assetFile=camera-btn_i.png"/>

View file

@ -0,0 +1,3 @@
<ImageAsset
AssetName="clear-icon_i_image"
imageFile="@assetFile=clear-icon_i.png"/>

View file

@ -0,0 +1,3 @@
<ImageAsset
AssetName="delete_i_image"
imageFile="@assetFile=delete_i.png"/>

View file

@ -0,0 +1,3 @@
<ImageAsset
AssetName="dropdown-button-arrow_d_image"
imageFile="@assetFile=dropdown-button-arrow_d.png"/>

View file

@ -0,0 +1,3 @@
<ImageAsset
AssetName="dropdown-button-arrow_h_image"
imageFile="@assetFile=dropdown-button-arrow_h.png"/>

View file

@ -0,0 +1,3 @@
<ImageAsset
AssetName="dropdown-button-arrow_i_image"
imageFile="@assetFile=dropdown-button-arrow_i.png"/>

View file

@ -0,0 +1,3 @@
<ImageAsset
AssetName="dropdown-button-arrow_n_image"
imageFile="@assetFile=dropdown-button-arrow_n.png"/>

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

View file

@ -0,0 +1,3 @@
<ImageAsset
AssetName="eyedropper_d_image"
imageFile="@assetFile=eyedropper_d.png"/>

Binary file not shown.

After

Width:  |  Height:  |  Size: 1 KiB

View file

@ -0,0 +1,3 @@
<ImageAsset
AssetName="eyedropper_h_image"
imageFile="@assetFile=eyedropper_h.png"/>

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

View file

@ -0,0 +1,3 @@
<ImageAsset
AssetName="eyedropper_i_image"
imageFile="@assetFile=eyedropper_i.png"/>

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

View file

@ -0,0 +1,3 @@
<ImageAsset
AssetName="eyedropper_n_image"
imageFile="@assetFile=eyedropper_n.png"/>

View file

@ -0,0 +1,3 @@
<ImageAsset
AssetName="folder_d_image"
imageFile="@assetFile=folder_d.png"/>

View file

@ -0,0 +1,3 @@
<ImageAsset
AssetName="folder_h_image"
imageFile="@assetFile=folder_h.png"/>

View file

@ -0,0 +1,3 @@
<ImageAsset
AssetName="folder_i_image"
imageFile="@assetFile=folder_i.png"/>

View file

@ -0,0 +1,3 @@
<ImageAsset
AssetName="folder_n_image"
imageFile="@assetFile=folder_n.png"/>

View file

@ -0,0 +1,3 @@
<ImageAsset
AssetName="images_menuGrid_d_image"
imageFile="@assetFile=menuGrid_d.png"/>

View file

@ -0,0 +1,3 @@
<ImageAsset
AssetName="images_menuGrid_h_image"
imageFile="@assetFile=menuGrid_h.png"/>

View file

@ -0,0 +1,3 @@
<ImageAsset
AssetName="images_menuGrid_image"
imageFile="@assetFile=menuGrid.png"/>

View file

@ -0,0 +1,3 @@
<ImageAsset
AssetName="images_menuGrid_n_image"
imageFile="@assetFile=menuGrid_n.png"/>

View file

@ -0,0 +1,3 @@
<ImageAsset
AssetName="layers-btn_i_image"
imageFile="@assetFile=layers-btn_i.png"/>

View file

@ -0,0 +1,3 @@
<ImageAsset
AssetName="lock_i_image"
imageFile="@assetFile=lock_i.png"/>

View file

@ -0,0 +1,3 @@
<ImageAsset
AssetName="new-folder-btn_i_image"
imageFile="@assetFile=new-folder-btn_i.png"/>

View file

@ -0,0 +1,3 @@
<ImageAsset
AssetName="new_i_image"
imageFile="@assetFile=new_i.png"/>

View file

@ -0,0 +1,3 @@
<ImageAsset
AssetName="numericslider_image"
imageFile="@assetFile=numericslider.png"/>

View file

@ -0,0 +1,3 @@
<ImageAsset
AssetName="open-file_i_image"
imageFile="@assetFile=open-file_i.png"/>

View file

@ -0,0 +1,3 @@
<ImageAsset
AssetName="reset-icon_i_image"
imageFile="@assetFile=reset-icon_i.png"/>

View file

@ -0,0 +1,3 @@
<ImageAsset
AssetName="selector-button_image"
imageFile="@assetFile=selector-button.png"/>

View file

@ -0,0 +1,3 @@
<ImageAsset
AssetName="tab-border_image"
imageFile="@assetFile=tab-border.png"/>

View file

@ -0,0 +1,3 @@
<ImageAsset
AssetName="textEdit_black_image"
imageFile="@assetFile=textEdit_black.png"/>

View file

@ -0,0 +1,3 @@
<ImageAsset
AssetName="textEdit_blue_image"
imageFile="@assetFile=textEdit_blue.png"/>

View file

@ -0,0 +1,3 @@
<ImageAsset
AssetName="textEdit_cyan_image"
imageFile="@assetFile=textEdit_cyan.png"/>

View file

@ -0,0 +1,3 @@
<ImageAsset
AssetName="textEdit_green_image"
imageFile="@assetFile=textEdit_green.png"/>

View file

@ -0,0 +1,3 @@
<ImageAsset
AssetName="textEdit_magenta_image"
imageFile="@assetFile=textEdit_magenta.png"/>

View file

@ -0,0 +1,3 @@
<ImageAsset
AssetName="textEdit_red_image"
imageFile="@assetFile=textEdit_red.png"/>

View file

@ -0,0 +1,3 @@
<ImageAsset
AssetName="textEdit_white_image"
imageFile="@assetFile=textEdit_white.png"/>

View file

@ -0,0 +1,3 @@
<ImageAsset
AssetName="textEdit_yellow_image"
imageFile="@assetFile=textEdit_yellow.png"/>

View file

@ -0,0 +1,3 @@
<ImageAsset
AssetName="uv-editor-btn_i_image"
imageFile="@assetFile=uv-editor-btn_i.png"/>