Merge pull request #1362 from marauder2k9-torque/ColorPicker-refactor

Color picker refactor
This commit is contained in:
Brian Roberts 2025-01-22 17:33:35 -06:00 committed by GitHub
commit 71d08e9e0c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 1091 additions and 1590 deletions

View file

@ -1104,7 +1104,7 @@ DefineEngineFunction(ColorRGBToHSB, const char*, (ColorI color), ,
"@endtsexample\n"
"@ingroup Strings")
{
ColorI::Hsb hsb(color.getHSB());
ColorI::Hsb hsb(color.getHSB());
String s(String::ToString(hsb.hue) + " " + String::ToString(hsb.sat) + " " + String::ToString(hsb.brightness));
return Con::getReturnBuffer(s);
}

View file

@ -409,6 +409,27 @@ ConsoleSetType( TypeS32Vector )
else
Con::printf("Vector<S32> must be set as { a, b, c, ... } or \"a b c ...\"");
}
//-----------------------------------------------------------------------------
// TypeF64
//-----------------------------------------------------------------------------
ConsoleType(double, TypeF64, F64, "")
ImplementConsoleTypeCasters(TypeF64, F64)
ConsoleGetType(TypeF64)
{
static const U32 bufSize = 256;
char* returnBuffer = Con::getReturnBuffer(bufSize);
dSprintf(returnBuffer, bufSize, "%Lg", *((F64*)dptr));
return returnBuffer;
}
ConsoleSetType(TypeF64)
{
if (argc == 1)
*((F64*)dptr) = dAtod(argv[0]);
else
Con::printf("(TypeF64) Cannot set multiple args to a single F64.");
}
//-----------------------------------------------------------------------------
// TypeF32

View file

@ -66,6 +66,7 @@ DefineConsoleType( TypeBoolVector, Vector<bool>)
DefineConsoleType( TypeS8, S8 )
DefineConsoleType( TypeS32, S32 )
DefineConsoleType( TypeS32Vector, Vector<S32> )
DefineConsoleType( TypeF64, F64 )
DefineConsoleType( TypeF32, F32 )
DefineConsoleType( TypeF32Vector, Vector<F32> )
DefineUnmappedConsoleType( TypeString, const char * ) // plain UTF-8 strings are not supported in new interop

View file

@ -129,11 +129,11 @@ public:
struct Hsb
{
Hsb() :hue(0), sat(0), brightness(0){};
Hsb(U32 h, U32 s, U32 b) :hue(h), sat(s), brightness(b){};
Hsb(F64 h, F64 s, F64 b) :hue(h), sat(s), brightness(b){};
U32 hue; ///Hue
U32 sat; ///Saturation
U32 brightness; //Brightness/Value/Lightness
F64 hue; ///Hue
F64 sat; ///Saturation
F64 brightness; //Brightness/Value/Lightness
};
public:
@ -466,72 +466,48 @@ inline void ColorI::set(const ColorI& in_rCopy,
inline void ColorI::set(const Hsb& color)
{
U32 r = 0;
U32 g = 0;
U32 b = 0;
// Normalize the input HSB values
F64 H = (F64)color.hue / 360.0; // Hue: [0, 360] -> [0, 1]
F64 S = (F64)color.sat / 100.0; // Saturation: [0, 100] -> [0, 1]
F64 B = (F64)color.brightness / 100.0; // Brightness: [0, 100] -> [0, 1]
F64 L = ((F64)color.brightness) / 100.0;
F64 S = ((F64)color.sat) / 100.0;
F64 H = ((F64)color.hue) / 360.0;
F64 r = 0.0, g = 0.0, b = 0.0;
if (color.sat == 0)
{
r = color.brightness;
g = color.brightness;
b = color.brightness;
}
else
{
F64 temp1 = 0;
if (L < 0.50)
{
temp1 = L*(1 + S);
}
else
{
temp1 = L + S - (L*S);
}
if(S == 0.0)
{
// Achromatic case (grey scale)
r = g = b = B;
}
else
{
// Compute chroma
F64 C = B * S;
F64 temp2 = 2.0*L - temp1;
// Intermediate value for the hue
F64 X = C * (1 - mFabsD(mFmodD(H * 6.0, 2.0) - 1));
F64 temp3 = 0;
for (S32 i = 0; i < 3; i++)
{
switch (i)
{
case 0: // red
{
temp3 = H + 0.33333;
if (temp3 > 1.0)
temp3 -= 1.0;
HSLtoRGB_Subfunction(r, temp1, temp2, temp3);
break;
}
case 1: // green
{
temp3 = H;
HSLtoRGB_Subfunction(g, temp1, temp2, temp3);
break;
}
case 2: // blue
{
temp3 = H - 0.33333;
if (temp3 < 0)
temp3 += 1;
HSLtoRGB_Subfunction(b, temp1, temp2, temp3);
break;
}
default:
{
// Minimum component
F64 m = B - C;
}
}
}
}
red = (U32)((((F64)r) / 100) * 255);
green = (U32)((((F64)g) / 100) * 255);
blue = (U32)((((F64)b) / 100) * 255);
alpha = 255;
// Assign r, g, b based on the hue sector
if (H >= 0.0 && H < 1.0 / 6.0) { r = C; g = X; b = 0.0; }
else if (H >= 1.0 / 6.0 && H < 2.0 / 6.0) { r = X; g = C; b = 0.0; }
else if (H >= 2.0 / 6.0 && H < 3.0 / 6.0) { r = 0.0; g = C; b = X; }
else if (H >= 3.0 / 6.0 && H < 4.0 / 6.0) { r = 0.0; g = X; b = C; }
else if (H >= 4.0 / 6.0 && H < 5.0 / 6.0) { r = X; g = 0.0; b = C; }
else if (H >= 5.0 / 6.0 && H <= 1.0) { r = C; g = 0.0; b = X; }
// Add the minimum component to normalize to the brightness
r += m;
g += m;
b += m;
}
// Convert normalized [0.0, 1.0] RGB values to integer [0, 255]
red = static_cast<U32>(r * 255.0 + 0.5);
green = static_cast<U32>(g * 255.0 + 0.5);
blue = static_cast<U32>(b * 255.0 + 0.5);
alpha = 255; // Set alpha to fully opaque
}
// This is a subfunction of HSLtoRGB
@ -744,70 +720,46 @@ inline U16 ColorI::get4444() const
inline ColorI::Hsb ColorI::getHSB() const
{
F64 rPercent = ((F64)red) / 255;
F64 gPercent = ((F64)green) / 255;
F64 bPercent = ((F64)blue) / 255;
// Normalize RGB values to [0, 1]
F64 rPercent = (F64)red / 255.0;
F64 gPercent = (F64)green / 255.0;
F64 bPercent = (F64)blue / 255.0;
F64 maxColor = 0.0;
if ((rPercent >= gPercent) && (rPercent >= bPercent))
maxColor = rPercent;
if ((gPercent >= rPercent) && (gPercent >= bPercent))
maxColor = gPercent;
if ((bPercent >= rPercent) && (bPercent >= gPercent))
maxColor = bPercent;
// Find the max and min values among the normalized RGB values
F64 maxColor = mMax(rPercent, mMax(gPercent, bPercent));
F64 minColor = mMin(rPercent, mMin(gPercent, bPercent));
F64 minColor = 0.0;
if ((rPercent <= gPercent) && (rPercent <= bPercent))
minColor = rPercent;
if ((gPercent <= rPercent) && (gPercent <= bPercent))
minColor = gPercent;
if ((bPercent <= rPercent) && (bPercent <= gPercent))
minColor = bPercent;
// Initialize H, S, B
F64 H = 0.0, S = 0.0, B = maxColor;
F64 H = 0.0;
F64 S = 0.0;
F64 B = 0.0;
// Compute saturation
F64 delta = maxColor - minColor;
if (delta > 0.0)
{
S = delta / maxColor; // Saturation
B = (maxColor + minColor) / 2.0;
// Compute hue
if (fabs(maxColor - rPercent) < 1e-6)
{
H = 60.0 * ((gPercent - bPercent) / delta);
}
else if (fabs(maxColor - gPercent) < 1e-6)
{
H = 60.0 * (((bPercent - rPercent) / delta) + 2.0);
}
else if (fabs(maxColor - bPercent) < 1e-6)
{
H = 60.0 * (((rPercent - gPercent) / delta) + 4.0);
}
}
if (maxColor == minColor)
{
H = 0.0;
S = 0.0;
}
else
{
if (B < 0.50)
{
S = (maxColor - minColor) / (maxColor + minColor);
}
else
{
S = (maxColor - minColor) / (2.0 - maxColor - minColor);
}
if (maxColor == rPercent)
{
H = (gPercent - bPercent) / (maxColor - minColor);
}
if (maxColor == gPercent)
{
H = 2.0 + (bPercent - rPercent) / (maxColor - minColor);
}
if (maxColor == bPercent)
{
H = 4.0 + (rPercent - gPercent) / (maxColor - minColor);
}
}
// Prepare the output HSB struct
ColorI::Hsb val;
val.hue = H; // Round to nearest integer
val.sat = S * 100.0; // Convert to percentage
val.brightness = B * 100.0; // Convert to percentage
ColorI::Hsb val;
val.sat = (U32)(S * 100);
val.brightness = (U32)(B * 100);
H = H*60.0;
if (H < 0.0)
H += 360.0;
val.hue = (U32)H;
return val;
return val;
}
inline String ColorI::getHex() const

View file

@ -540,35 +540,35 @@ void GFXDrawUtil::drawRect( const Point2F &upperLeft, const Point2F &lowerRight,
//-----------------------------------------------------------------------------
// Draw Rectangle Fill
//-----------------------------------------------------------------------------
void GFXDrawUtil::drawRectFill(const RectF& rect, const ColorI& color, const F32& borderSize, const ColorI& borderColor)
void GFXDrawUtil::drawRectFill(const RectF& rect, const ColorI& color, const F32& borderSize, const ColorI& borderColor, bool gradientFill)
{
drawRoundedRect(0.0f, rect.point, Point2F(rect.extent.x + rect.point.x - 1, rect.extent.y + rect.point.y - 1), color, borderSize, borderColor);
drawRoundedRect(0.0f, rect.point, Point2F(rect.extent.x + rect.point.x - 1, rect.extent.y + rect.point.y - 1), color, borderSize, borderColor, gradientFill);
}
void GFXDrawUtil::drawRectFill(const Point2I& upperLeft, const Point2I& lowerRight, const ColorI& color, const F32& borderSize, const ColorI& borderColor)
void GFXDrawUtil::drawRectFill(const Point2I& upperLeft, const Point2I& lowerRight, const ColorI& color, const F32& borderSize, const ColorI& borderColor, bool gradientFill)
{
drawRoundedRect(0.0f, Point2F((F32)upperLeft.x, (F32)upperLeft.y), Point2F((F32)lowerRight.x, (F32)lowerRight.y), color, borderSize, borderColor);
drawRoundedRect(0.0f, Point2F((F32)upperLeft.x, (F32)upperLeft.y), Point2F((F32)lowerRight.x, (F32)lowerRight.y), color, borderSize, borderColor, gradientFill);
}
void GFXDrawUtil::drawRectFill(const RectI& rect, const ColorI& color, const F32& borderSize, const ColorI& borderColor)
void GFXDrawUtil::drawRectFill(const RectI& rect, const ColorI& color, const F32& borderSize, const ColorI& borderColor, bool gradientFill)
{
drawRoundedRect(0.0f, rect.point, Point2I(rect.extent.x + rect.point.x - 1, rect.extent.y + rect.point.y - 1), color, borderSize, borderColor);
drawRoundedRect(0.0f, rect.point, Point2I(rect.extent.x + rect.point.x - 1, rect.extent.y + rect.point.y - 1), color, borderSize, borderColor, gradientFill);
}
void GFXDrawUtil::drawRectFill(const Point2F& upperLeft, const Point2F& lowerRight, const ColorI& color, const F32& borderSize,const ColorI& borderColor)
void GFXDrawUtil::drawRectFill(const Point2F& upperLeft, const Point2F& lowerRight, const ColorI& color, const F32& borderSize,const ColorI& borderColor, bool gradientFill)
{
// draw a rounded rect with 0 radiuse.
drawRoundedRect(0.0f, upperLeft, lowerRight, color, borderSize, borderColor);
drawRoundedRect(0.0f, upperLeft, lowerRight, color, borderSize, borderColor, gradientFill);
}
void GFXDrawUtil::drawRoundedRect(const F32& cornerRadius, const RectI& rect, const ColorI& color, const F32& borderSize, const ColorI& borderColor)
void GFXDrawUtil::drawRoundedRect(const F32& cornerRadius, const RectI& rect, const ColorI& color, const F32& borderSize, const ColorI& borderColor, bool gradientFill)
{
drawRoundedRect(cornerRadius, rect.point, Point2I(rect.extent.x + rect.point.x - 1, rect.extent.y + rect.point.y - 1), color, borderSize, borderColor);
drawRoundedRect(cornerRadius, rect.point, Point2I(rect.extent.x + rect.point.x - 1, rect.extent.y + rect.point.y - 1), color, borderSize, borderColor, gradientFill);
}
void GFXDrawUtil::drawRoundedRect(const F32& cornerRadius, const Point2I& upperLeft, const Point2I& lowerRight, const ColorI& color, const F32& borderSize, const ColorI& borderColor)
void GFXDrawUtil::drawRoundedRect(const F32& cornerRadius, const Point2I& upperLeft, const Point2I& lowerRight, const ColorI& color, const F32& borderSize, const ColorI& borderColor, bool gradientFill)
{
drawRoundedRect(cornerRadius, Point2F((F32)upperLeft.x, (F32)upperLeft.y), Point2F((F32)lowerRight.x, (F32)lowerRight.y), color, borderSize, borderColor);
drawRoundedRect(cornerRadius, Point2F((F32)upperLeft.x, (F32)upperLeft.y), Point2F((F32)lowerRight.x, (F32)lowerRight.y), color, borderSize, borderColor, gradientFill);
}
void GFXDrawUtil::drawRoundedRect(const F32& cornerRadius,
@ -576,7 +576,8 @@ void GFXDrawUtil::drawRoundedRect(const F32& cornerRadius,
const Point2F& lowerRight,
const ColorI& color,
const F32& borderSize,
const ColorI& borderColor)
const ColorI& borderColor,
bool gradientFill)
{
// NorthWest and NorthEast facing offset vectors
@ -595,6 +596,14 @@ void GFXDrawUtil::drawRoundedRect(const F32& cornerRadius,
for (S32 i = 0; i < 4; i++)
verts[i].color = color;
if (gradientFill)
{
verts[0].texCoord.set(0.0f, 0.0f); // top left
verts[1].texCoord.set(1.0f, 0.0f); // top right
verts[2].texCoord.set(0.0f, 1.0f); // bottom left
verts[3].texCoord.set(1.0f, 1.0f); // bottom right
}
verts.unlock();
mDevice->setVertexBuffer(verts);
@ -623,6 +632,14 @@ void GFXDrawUtil::drawRoundedRect(const F32& cornerRadius,
mRoundRectangleShaderConsts->setSafe(mRoundRectangleShader->getShaderConstHandle("$sizeUni"), size);
mRoundRectangleShaderConsts->setSafe(mRoundRectangleShader->getShaderConstHandle("$borderSize"), borderSize);
mRoundRectangleShaderConsts->setSafe(mRoundRectangleShader->getShaderConstHandle("$borderCol"), borderColor);
if (gradientFill)
{
mRoundRectangleShaderConsts->setSafe(mRoundRectangleShader->getShaderConstHandle("$gradientFill"), 1.0f);
}
else
{
mRoundRectangleShaderConsts->setSafe(mRoundRectangleShader->getShaderConstHandle("$gradientFill"), 0.0f);
}
Point2F rectCenter((F32)(topLeftCorner.x + (size.x / 2.0)), (F32)(topLeftCorner.y + (size.y / 2.0)));
mRoundRectangleShaderConsts->setSafe(mRoundRectangleShader->getShaderConstHandle("$rectCenter"), rectCenter);

View file

@ -57,13 +57,13 @@ public:
// Draw Rectangles : FILL
//-----------------------------------------------------------------------------
void drawRectFill(const Point2F& upperL, const Point2F& lowerR, const ColorI& color, const F32& borderSize = 0.0f, const ColorI& borderColor = ColorI(0, 0, 0, 0));
void drawRectFill(const RectF& rect, const ColorI& color, const F32& borderSize = 0.0f, const ColorI& borderColor = ColorI(0, 0, 0, 0));
void drawRectFill(const Point2I& upperLeft, const Point2I& lowerRight, const ColorI& color, const F32& borderSize = 0.0f, const ColorI& borderColor = ColorI(0, 0, 0, 0));
void drawRectFill(const RectI& rect, const ColorI& color, const F32& borderSize = 0.0f, const ColorI& borderColor = ColorI(0, 0, 0, 0));
void drawRoundedRect(const F32& cornerRadius, const RectI& rect, const ColorI& color, const F32& borderSize = 0.0f, const ColorI& borderColor = ColorI(0, 0, 0, 0));
void drawRoundedRect(const F32& cornerRadius, const Point2I& upperLeft, const Point2I& lowerRight, const ColorI& color, const F32& borderSize = 0.0f, const ColorI& borderColor = ColorI(0, 0, 0, 0));
void drawRoundedRect(const F32& cornerRadius, const Point2F& upperLeft, const Point2F& lowerRight, const ColorI& color, const F32& borderSize = 0.0f, const ColorI& borderColor = ColorI(0, 0, 0, 0));
void drawRectFill(const Point2F& upperL, const Point2F& lowerR, const ColorI& color, const F32& borderSize = 0.0f, const ColorI& borderColor = ColorI(0, 0, 0, 0), bool gradientFill = false);
void drawRectFill(const RectF& rect, const ColorI& color, const F32& borderSize = 0.0f, const ColorI& borderColor = ColorI(0, 0, 0, 0), bool gradientFill = false);
void drawRectFill(const Point2I& upperLeft, const Point2I& lowerRight, const ColorI& color, const F32& borderSize = 0.0f, const ColorI& borderColor = ColorI(0, 0, 0, 0), bool gradientFill = false);
void drawRectFill(const RectI& rect, const ColorI& color, const F32& borderSize = 0.0f, const ColorI& borderColor = ColorI(0, 0, 0, 0), bool gradientFill = false);
void drawRoundedRect(const F32& cornerRadius, const RectI& rect, const ColorI& color, const F32& borderSize = 0.0f, const ColorI& borderColor = ColorI(0, 0, 0, 0), bool gradientFill = false);
void drawRoundedRect(const F32& cornerRadius, const Point2I& upperLeft, const Point2I& lowerRight, const ColorI& color, const F32& borderSize = 0.0f, const ColorI& borderColor = ColorI(0, 0, 0, 0), bool gradientFill = false);
void drawRoundedRect(const F32& cornerRadius, const Point2F& upperLeft, const Point2F& lowerRight, const ColorI& color, const F32& borderSize = 0.0f, const ColorI& borderColor = ColorI(0, 0, 0, 0), bool gradientFill = false);
void draw2DSquare(const Point2F& screenPoint, F32 width, F32 spinAngle = 0.0f);

File diff suppressed because it is too large Load diff

View file

@ -32,25 +32,22 @@
/// This control draws a box containing a color specified by mPickColor,
/// in a way according to one of the PickMode enum's, stored as mDisplayMode.
///
/// The color the box represents is stored as mBaseColour (for pPallete, pBlendColorRange),
/// The color the box represents is stored as mBaseColour (for pPalette, pBlendColorRange),
/// whilst the color chosen by the box is stored as mPickColor.
///
/// Whenever the control is clicked, it will do one of many things :
///
/// -# If its in pPallete mode, execute the regular "command"
/// -# If its in pBlendColorRange mode, update the selector position. The position will be re-read upon the next render. In addition, a cross will be drawn where the color has been selected from. As with 1, "command" will be executed.
/// -# If its in pHorizColorRange or pVertColorRange mode, it will function in a similar manner to 2, but the selector will resemble a horizontal or vertical bar.
/// -# If its in pHorizAlphaRange or pVertAlphaRange mode, it will also function the same way as 3
/// -# If its in pPalette mode, execute the regular "command"
/// -# If its in pBlendRange mode, update the selector position. The position will be re-read upon the next render. In addition, a cross will be drawn where the color has been selected from. As with 1, "command" will be executed.
/// -# If its in pHueRange or pAlphaRange mode, it will function in a similar manner to 2, but the selector will resemble a horizontal or vertical bar.
/// -# If its in pAlphaRange mode, it will also function the same way as 3
/// -# If its in pDropperBackground mode, nothing will happen
///
/// Colours are drawn in different ways according to mDisplayMode:
///
/// -# With pPallete, a box with a blank color, mBaseColor is drawn.
/// -# With pHorizColorRange, a horizontal box with colors blending in the range, mColorRange.
/// -# With pVertColorRange, a vertical box with colors blending in the range, mColorRange.
/// -# With pBlendColorRange, a box, the bottom colors being black, but the top left being white, and the top right being mBaseColor.
/// -# With pHorizAlphaRange, a horizontal box with black blending with an alpha from 0 to 255
/// -# With pVertAlphaRange, a vertical box with black blending with an apha from 0 to 255
/// -# With pPalette, a box with a blank color, mBaseColor is drawn.
/// -# With pHueRange, a box containing the hue range 0-360.
/// -# With pAlphaRange, a box containing the alpha range 0-255.
/// -# With pDropperBackground, nothing is drawn
class GuiColorPickerCtrl : public GuiControl
{
@ -59,14 +56,10 @@ class GuiColorPickerCtrl : public GuiControl
public:
enum PickMode
{
pPallet = 0, ///< We just have a solid color; We just act like a pallet
pHorizColorRange, ///< We have a range of base colors going horizontally
pVertColorRange, ///< We have a range of base colors going vertically
pHorizColorBrightnessRange, ///< HorizColorRange with brightness
pVertColorBrightnessRange, ///< VertColorRange with brightness
pBlendColorRange, ///< We have a box which shows a range in brightness of the color
pHorizAlphaRange, ///< We have a box which shows a range in alpha going horizontally
pVertAlphaRange, ///< We have a box which shows a range in alpha going vertically
pPalette = 0, ///< We just have a solid color; We just act like a pallet
pBlendRange, ///< The full range of brightness and saturation.
pHueRange, ///< The full hue range 0-360.
pAlphaRange, ///< The full alpha range 0-255.
pDropperBackground ///< The control does not draw anything; Only does something when you click, or move the mouse (when active)
};
@ -77,37 +70,63 @@ class GuiColorPickerCtrl : public GuiControl
};
protected:
/// @name Core Rendering functions
/// @{
void renderColorBox(RectI &bounds); ///< Function that draws the actual color box
void drawSelector(RectI &bounds, Point2I &selectorPos, SelectorMode mode); /// < Function that draws the selection indicator
void drawBlendBox(RectI &bounds, LinearColorF &c1, LinearColorF &c2, LinearColorF &c3, LinearColorF &c4);
void drawBlendRangeBox(RectI &bounds, bool vertical, U8 numColors, ColorI *colors);
/// @}
/// <summary>
/// Render the hue gradient for pBlendRange mode.
/// </summary>
/// <param name="bounds">The bounds of the ctrl.</param>
void renderBlendRange(RectI& bounds);
/// <summary>
/// Render the selector for pBlendRange mode.
/// </summary>
/// <param name="bounds">The bounds of the ctrl.</param>
void renderBlendSelector(RectI& bounds);
/// <summary>
/// Render the hue gradient for pHueRange mode.
/// </summary>
/// <param name="bounds">The bounds of the ctrl.</param>
/// <param name="numColours">The number of splits in the gradient. 7 as default.</param>
void renderHueGradient(RectI& bounds, U32 numColours);
/// <summary>
/// Render the selector for pHueRange mode.
/// </summary>
/// <param name="bounds">The bounds of the ctrl.</param>
void renderHueSelector(RectI& bounds);
/// <summary>
/// Render the alpha gradient for pAlphaRange mode.
/// </summary>
/// <param name="bounds">The bounds of the ctrl.</param>
void renderAlphaGradient(RectI& bounds);
/// <summary>
/// Render the selector for pAlphaRange mode.
/// </summary>
/// <param name="bounds">The bounds of the ctrl.</param>
void renderAlphaSelector(RectI& bounds);
/// @name Core Variables
/// @{
LinearColorF mPickColor; ///< Color that has been picked from control
LinearColorF mBaseColor; ///< Colour we display (in case of pallet and blend mode)
PickMode mDisplayMode; ///< Current color display mode of the selector
Point2I mSelectorPos; ///< Current position of the selector
bool mPositionChanged; ///< Current position has changed since last render?
SelectorMode mSelectorMode; ///< Current color display mode of the selector
F64 mSelectedHue;
F64 mSelectedSaturation;
F64 mSelectedBrightness;
F64 mSelectedAlpha;
bool mMouseOver; ///< Mouse is over?
bool mMouseDown; ///< Mouse button down?
bool mActionOnMove; ///< Perform onAction() when position has changed?
bool mSelectColor;
LinearColorF mSetColor;
GBitmap* mBitmap;
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.
GFXStateBlockRef mStateBlock;
static ColorI mColorRange[7]; ///< Color range for pHorizColorRange and pVertColorRange
/// @}
public:
@ -120,23 +139,7 @@ class GuiColorPickerCtrl : public GuiControl
static void initPersistFields();
void onRender(Point2I offset, const RectI &updateRect) override;
bool mShowReticle; ///< Show reticle on render
/// @name Color Value Functions
/// @{
/// NOTE: setValue only sets baseColor, since setting pickColor wouldn't be useful
void setValue(LinearColorF &value) {mBaseColor = value;}
/// NOTE: getValue() returns baseColor if pallet (since pallet controls can't "pick" colours themselves)
LinearColorF getValue() { return mDisplayMode == pPallet ? mBaseColor : mPickColor; }
const char *getScriptValue() override;
void setScriptValue(const char *value) override;
void updateColor() {mPositionChanged = true;}
/// @}
/// @name Selector Functions
/// @{
void setSelectorPos(const Point2I &pos); ///< Set new pos (in local coords)
void setSelectorPos(const LinearColorF & color);
Point2I getSelectorPos() {return mSelectorPos;}
/// @}
/// @name Input Events
@ -149,9 +152,40 @@ class GuiColorPickerCtrl : public GuiControl
void onMouseEnter(const GuiEvent &) override;
void onMouseLeave(const GuiEvent &) override;
/// @}
// script getters and setters
/// <summary>
/// Set the selected hue.
/// </summary>
/// <param name="hueValue">Hue value, 0 - 360.</param>
void setSelectedHue(const F64& hueValue);
F64 getSelectedHue() { return mSelectedHue; }
/// <summary>
/// Set the selected brightness.
/// </summary>
/// <param name="brightValue">Brightness value, 0 - 100.</param>
void setSelectedBrightness(const F64& brightValue);
F64 getSelectedBrightness() { return mSelectedBrightness; }
/// <summary>
/// Set the selected saturation.
/// </summary>
/// <param name="satValue">Saturation value, 0 - 100.</param>
void setSelectedSaturation(const F64& satValue);
F64 getSelectedSaturation() { return mSelectedSaturation; }
/// <summary>
/// Set the selected alpha.
/// </summary>
/// <param name="alphaValue">Alpha value, 0 - 255.</param>
void setSelectedAlpha(const F64& alphaValue);
F64 getSelectedAlpha() { return mSelectedAlpha; }
};
typedef GuiColorPickerCtrl::PickMode GuiColorPickMode;
typedef GuiColorPickerCtrl::SelectorMode GuiColorSelectorMode;
DefineEnumType( GuiColorPickMode );
DefineEnumType(GuiColorSelectorMode);
#endif

View file

@ -156,10 +156,10 @@ singleton ShaderData( CubemapSaveShader )
//-----------------------------------------------------------------------------
singleton ShaderData( RoundedRectangleGUI )
{
DXVertexShaderFile = $Core::CommonShaderPath @ "/fixedFunction/colorV.hlsl";
DXVertexShaderFile = $Core::CommonShaderPath @ "/fixedFunction/addColorTextureV.hlsl";
DXPixelShaderFile = $Core::CommonShaderPath @ "/fixedFunction/roundedRectangleP.hlsl";
OGLVertexShaderFile = $Core::CommonShaderPath @ "/fixedFunction/gl/colorV.glsl";
OGLVertexShaderFile = $Core::CommonShaderPath @ "/fixedFunction/gl/addColorTextureV.glsl";
OGLPixelShaderFile = $Core::CommonShaderPath @ "/fixedFunction/gl/roundedRectangleP.glsl";
pixVersion = 3.0;

View file

@ -20,6 +20,7 @@
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
in vec4 color;
in vec2 texCoord;
out vec4 OUT_col;
@ -29,6 +30,7 @@ uniform vec2 oneOverViewport;
uniform float radius;
uniform float borderSize;
uniform vec4 borderCol;
uniform float gradientFill;
float RoundedRectSDF(vec2 p, vec2 size, float radius)
{
@ -57,13 +59,19 @@ void main()
float cornerRadius = radius;
// if ((p.y < 0.0 && p.x < 0.0) || // top left corner
// (p.y < 0.0 && p.x > 0.0) || // top right corner
// (p.y > 0.0 && p.x > 0.0) || // bottom right corner.
// (p.y > 0.0 && p.x < 0.0)) // bottom left corner
// {
// cornerRadius = radius;
// }
vec4 baseColor = color;
if(gradientFill > 0.5f)
{
float blendX = (1.0 - texCoord.x);
float blendY = texCoord.y;
float gamma = 2.4;
blendX = pow(abs(blendX), gamma);
blendY = pow(abs(blendY), 1/gamma);
vec4 interpolatedColor = mix(mix(baseColor,vec4(1.0f, 1.0f, 1.0f, 1.0f), blendX),vec4(0.0f, 0.0f, 0.0f, 1.0f), blendY);
baseColor = interpolatedColor;
}
if(cornerRadius > 0.0 || halfBorder > 0.0)
{
@ -73,26 +81,20 @@ void main()
{
if(sdf < 0.0)
{
// if ((p.y >= -halfSize.y - radius + halfBorder && p.y <= -halfSize.y + radius - halfBorder) || // top border
// (p.y >= halfSize.y - radius + halfBorder && p.y <= halfSize.y + radius - halfBorder) || // bottom border
// (p.x >= -halfSize.x - radius + halfBorder && p.x <= -halfSize.x + radius - halfBorder) || // left border
// (p.x >= halfSize.x - radius + halfBorder && p.x <= halfSize.x + radius - halfBorder) ) { // right border
// }
toColor = color;
sdf = abs(sdf) / borderSize;
toColor = baseColor;
sdf = abs(sdf) - borderSize;
}
}
else{
fromColor = color;
fromColor = baseColor;
}
float alpha = smoothstep(-1.0, 1.0, sdf);
OUT_col = mix(fromColor, toColor, alpha);
OUT_col = mix(fromColor, baseColor, alpha);
}
else
{
OUT_col = color;
OUT_col = baseColor;
}
}

View file

@ -26,14 +26,17 @@ struct Conn
{
float4 HPOS : TORQUE_POSITION;
float4 color : COLOR;
float2 texCoord : TEXCOORD0;
};
uniform float2 sizeUni;
uniform float2 rectCenter;
uniform float2 oneOverViewport;
uniform float radius;
uniform float borderSize;
uniform float4 borderCol;
uniform float gradientFill;
float RoundedRectSDF(float2 p, float2 size, float radius)
{
@ -62,13 +65,19 @@ float4 main(Conn IN) : TORQUE_TARGET0
float cornerRadius = radius;
// if ((p.y < 0.0 && p.x < 0.0) || // top left corner
// (p.y < 0.0 && p.x > 0.0) || // top right corner
// (p.y > 0.0 && p.x > 0.0) || // bottom right corner.
// (p.y > 0.0 && p.x < 0.0)) // bottom left corner
// {
// cornerRadius = radius;
// }
float4 baseColor = IN.color;
if(gradientFill > 0.5f)
{
float blendX = 1.0 - IN.texCoord.x;
float blendY = IN.texCoord.y;
float gamma = 2.4;
blendX = pow(abs(blendX), gamma);
blendY = pow(abs(blendY), 1/gamma);
float4 interpolatedColor = lerp(lerp(baseColor,float4(1.0f, 1.0f, 1.0f, 1.0f), blendX),float4(0.0f, 0.0f, 0.0f, 1.0f), blendY);
baseColor = interpolatedColor;
}
if(cornerRadius > 0.0 || halfBorder > 0.0)
{
@ -78,19 +87,13 @@ float4 main(Conn IN) : TORQUE_TARGET0
{
if(sdf < 0.0)
{
// if ((p.y >= -halfSize.y - radius + halfBorder && p.y <= -halfSize.y + radius - halfBorder) || // top border
// (p.y >= halfSize.y - radius + halfBorder && p.y <= halfSize.y + radius - halfBorder) || // bottom border
// (p.x >= -halfSize.x - radius + halfBorder && p.x <= -halfSize.x + radius - halfBorder) || // left border
// (p.x >= halfSize.x - radius + halfBorder && p.x <= halfSize.x + radius - halfBorder) ) { // right border
// }
toColor = IN.color;
sdf = abs(sdf) / borderSize;
toColor = baseColor;
sdf = abs(sdf) - borderSize;
}
}
else{
fromColor = IN.color;
fromColor = baseColor;
}
float alpha = smoothstep(-1.0, 1.0, sdf);
@ -98,6 +101,6 @@ float4 main(Conn IN) : TORQUE_TARGET0
}
else
{
return IN.color;
return baseColor;
}
}

File diff suppressed because it is too large Load diff