T2D style 'Stock colors'

This commit implements stock colors as they are implemented in T2D.
It allows for using predefined stock colors e.g.:
``` %object.color = "OrangeRed"; ```
Instead of
``` %object.color = "255 69 0"; ```
This commit is contained in:
Lukas Jørgensen 2014-01-03 00:30:16 +01:00 committed by Lukas Joergensen
parent 938e28b6f3
commit 9ebfd0f6b3
3 changed files with 604 additions and 4 deletions

View file

@ -29,6 +29,7 @@
#include "core/color.h"
#include "console/simBase.h"
#include "math/mRect.h"
#include "core/strings/stringUnit.h"
//-----------------------------------------------------------------------------
// TypeString
@ -567,7 +568,17 @@ ImplementConsoleTypeCasters( TypeColorF, ColorF )
ConsoleGetType( TypeColorF )
{
ColorF * color = (ColorF*)dptr;
// Fetch color.
const ColorF* color = (ColorF*)dptr;
// Fetch stock color name.
StringTableEntry colorName = StockColor::name( *color );
// Write as color name if was found.
if ( colorName != StringTable->EmptyString() )
return colorName;
// Format as color components.
char* returnBuffer = Con::getReturnBuffer(256);
dSprintf(returnBuffer, 256, "%g %g %g %g", color->red, color->green, color->blue, color->alpha);
return(returnBuffer);
@ -578,6 +589,22 @@ ConsoleSetType( TypeColorF )
ColorF *tmpColor = (ColorF *) dptr;
if(argc == 1)
{
// Is only a single argument passed?
if ( StringUnit::getUnitCount( argv[0], " " ) == 1 )
{
// Is this a stock color name?
if ( !StockColor::isColor(argv[0]) )
{
// No, so warn.
Con::warnf( "TypeColorF() - Invalid single argument of '%s' could not be interpreted as a stock color name. Using default.", argv[0] );
}
// Set stock color (if it's invalid we'll get the default.
tmpColor->set( argv[0] );
return;
}
tmpColor->set(0, 0, 0, 1);
F32 r,g,b,a;
S32 args = dSscanf(argv[0], "%g %g %g %g", &r, &g, &b, &a);
@ -602,7 +629,7 @@ ConsoleSetType( TypeColorF )
tmpColor->alpha = dAtof(argv[3]);
}
else
Con::printf("Color must be set as { r, g, b [,a] }");
Con::printf("Color must be set as { r, g, b [,a] }, { r g b [b] } or { stockColorName }");
}
//-----------------------------------------------------------------------------
@ -613,7 +640,17 @@ ImplementConsoleTypeCasters( TypeColorI, ColorI )
ConsoleGetType( TypeColorI )
{
ColorI *color = (ColorI *) dptr;
// Fetch color.
ColorI* color = (ColorI*)dptr;
// Fetch stock color name.
StringTableEntry colorName = StockColor::name( *color );
// Write as color name if was found.
if ( colorName != StringTable->EmptyString() )
return colorName;
// Format as color components.
char* returnBuffer = Con::getReturnBuffer(256);
dSprintf(returnBuffer, 256, "%d %d %d %d", color->red, color->green, color->blue, color->alpha);
return returnBuffer;
@ -624,6 +661,22 @@ ConsoleSetType( TypeColorI )
ColorI *tmpColor = (ColorI *) dptr;
if(argc == 1)
{
// Is only a single argument passed?
if ( StringUnit::getUnitCount( argv[0], " " ) == 1 )
{
// Is this a stock color name?
if ( !StockColor::isColor(argv[0]) )
{
// No, so warn.
Con::warnf( "TypeColorF() - Invalid single argument of '%s' could not be interpreted as a stock color name. Using default.", argv[0] );
}
// Set stock color (if it's invalid we'll get the default.
tmpColor->set( argv[0] );
return;
}
tmpColor->set(0, 0, 0, 255);
S32 r,g,b,a;
S32 args = dSscanf(argv[0], "%d %d %d %d", &r, &g, &b, &a);
@ -648,7 +701,7 @@ ConsoleSetType( TypeColorI )
tmpColor->alpha = dAtoi(argv[3]);
}
else
Con::printf("Color must be set as { r, g, b [,a] }");
Con::printf("Color must be set as { r, g, b [,a] }, { r g b [b] } or { stockColorName }");
}
//-----------------------------------------------------------------------------