Try handling colors in console

This commit is contained in:
Learath 2019-04-25 14:48:53 +03:00
parent 5e5def314f
commit 0f781df1dd
9 changed files with 120 additions and 2 deletions

View file

@ -200,6 +200,11 @@ inline vec3 UnpackColor(int v)
return vec3(fmod(((v>>16)&0xff)/255.0f, 1.0f), ((v>>8)&0xff)/255.0f, 0.5f+(v&0xff)/255.0f*0.5f);
}
inline int PackColor(vec3 hsl)
{
return ((int)(hsl.h * 255.0f) << 16) + ((int)(hsl.s * 255.0f) << 8) + (int)(hsl.l * 255.0f);
}
inline vec4 Color3to4(vec3 col)
{
return vec4(col[0], col[1], col[2], 1.0f);

View file

@ -45,6 +45,7 @@ public:
virtual int GetInteger(unsigned Index) = 0;
virtual float GetFloat(unsigned Index) = 0;
virtual const char *GetString(unsigned Index) = 0;
virtual int GetColor(unsigned Index) = 0;
int NumArguments() const { return m_NumArgs; }
int m_ClientID;

View file

@ -50,11 +50,13 @@ public:
virtual void Reset()
{
#define MACRO_CONFIG_INT(Name,ScriptName,def,min,max,flags,desc) g_Config.m_##Name = def;
#define MACRO_CONFIG_COL(...) MACRO_CONFIG_INT(__VA_ARGS__)
#define MACRO_CONFIG_STR(Name,ScriptName,len,def,flags,desc) str_copy(g_Config.m_##Name, def, len);
#include "config_variables.h"
#undef MACRO_CONFIG_INT
#undef MACRO_CONFIG_COL
#undef MACRO_CONFIG_STR
}
@ -74,11 +76,13 @@ public:
char aEscapeBuf[1024*2];
#define MACRO_CONFIG_INT(Name,ScriptName,def,min,max,flags,desc) if((flags)&CFGFLAG_SAVE) { str_format(aLineBuf, sizeof(aLineBuf), "%s %i", #ScriptName, g_Config.m_##Name); WriteLine(aLineBuf); }
#define MACRO_CONFIG_COL(...) MACRO_CONFIG_INT(__VA_ARGS__)
#define MACRO_CONFIG_STR(Name,ScriptName,len,def,flags,desc) if((flags)&CFGFLAG_SAVE) { EscapeParam(aEscapeBuf, g_Config.m_##Name, sizeof(aEscapeBuf)); str_format(aLineBuf, sizeof(aLineBuf), "%s \"%s\"", #ScriptName, aEscapeBuf); WriteLine(aLineBuf); }
#include "config_variables.h"
#undef MACRO_CONFIG_INT
#undef MACRO_CONFIG_COL
#undef MACRO_CONFIG_STR
for(int i = 0; i < m_NumCallbacks; i++)

View file

@ -12,9 +12,11 @@
struct CConfiguration
{
#define MACRO_CONFIG_INT(Name,ScriptName,Def,Min,Max,Save,Desc) int m_##Name;
#define MACRO_CONFIG_COL(...) MACRO_CONFIG_INT(__VA_ARGS__)
#define MACRO_CONFIG_STR(Name,ScriptName,Len,Def,Save,Desc) char m_##Name[Len]; // Flawfinder: ignore
#include "config_variables.h"
#undef MACRO_CONFIG_INT
#undef MACRO_CONFIG_COL
#undef MACRO_CONFIG_STR
};

View file

@ -3,6 +3,8 @@
#include <new>
#include <base/math.h>
#include <base/vmath.h>
#include <base/color.h>
#include <base/system.h>
#include <engine/storage.h>
@ -35,6 +37,63 @@ float CConsole::CResult::GetFloat(unsigned Index)
return str_tofloat(m_apArgs[Index]);
}
int CConsole::CResult::GetColor(unsigned Index)
{
if(Index >= m_NumArgs)
return -1;
const char *pStr = m_apArgs[Index];
if(str_isallnum(pStr)) // Teeworlds Color (Packed HSL)
{
return str_toint(pStr);
}
else if(*pStr == 'r') // Hex RGB
{
vec3 rgb;
int Len = str_length(pStr);
if(Len == 4)
{
unsigned Num = str_toint_base(pStr + 1, 16);
rgb.r = ((Num >> 8) & 0x0F + (Num >> 4) & 0xF0) / 255.0f;
rgb.g = ((Num >> 4) & 0x0F + (Num >> 0) & 0xF0) / 255.0f;
rgb.b = ((Num >> 0) & 0x0F + (Num << 4) & 0xF0) / 255.0f;
}
else if(Len == 7)
{
unsigned Num = str_toint_base(pStr + 1, 16);
rgb.r = ((Num >> 16) & 0xFF) / 255.0f;
rgb.g = ((Num >> 8) & 0xFF) / 255.0f;
rgb.b = ((Num >> 0) & 0xFF) / 255.0f;
}
else
{
return -1;
}
return PackColor(RgbToHsl(rgb));
}
else if(!str_comp_nocase(pStr, "red"))
return PackColor(vec3(0.0f/6.0f, 1, .5f));
else if(!str_comp_nocase(pStr, "yellow"))
return PackColor(vec3(1.0f/6.0f, 1, .5f));
else if(!str_comp_nocase(pStr, "green"))
return PackColor(vec3(2.0f/6.0f, 1, .5f));
else if(!str_comp_nocase(pStr, "cyan"))
return PackColor(vec3(3.0f/6.0f, 1, .5f));
else if(!str_comp_nocase(pStr, "blue"))
return PackColor(vec3(4.0f/6.0f, 1, .5f));
else if(!str_comp_nocase(pStr, "magenta"))
return PackColor(vec3(5.0f/6.0f, 1, .5f));
else if(!str_comp_nocase(pStr, "white"))
return PackColor(vec3(0, 0, 1));
else if(!str_comp_nocase(pStr, "gray"))
return PackColor(vec3(0, 0, .5f));
else if(!str_comp_nocase(pStr, "black"))
return PackColor(vec3(0, 0, 0));
return -1;
}
const IConsole::CCommandInfo *CConsole::CCommand::NextCommandInfo(int AccessLevel, int FlagMask) const
{
const CCommand *pInfo = m_pNext;
@ -682,6 +741,37 @@ static void IntVariableCommand(IConsole::IResult *pResult, void *pUserData)
}
}
static void ColVariableCommand(IConsole::IResult *pResult, void *pUserData)
{
CIntVariableData *pData = (CIntVariableData *)pUserData;
if(pResult->NumArguments())
{
int Val = pResult->GetColor(0);
// do clamping
if(pData->m_Min != pData->m_Max)
{
if (Val < pData->m_Min)
Val = pData->m_Min;
if (pData->m_Max != 0 && Val > pData->m_Max)
Val = pData->m_Max;
}
*(pData->m_pVariable) = Val;
if(pResult->m_ClientID != IConsole::CLIENT_ID_GAME)
pData->m_OldValue = Val;
}
else
{
char aBuf[32];
str_format(aBuf, sizeof(aBuf), "Value: %d", *(pData->m_pVariable));
pData->m_pConsole->Print(IConsole::OUTPUT_LEVEL_STANDARD, "console", aBuf);
}
}
static void StrVariableCommand(IConsole::IResult *pResult, void *pUserData)
{
CStrVariableData *pData = (CStrVariableData *)pUserData;
@ -837,6 +927,12 @@ CConsole::CConsole(int FlagMask)
Register(#ScriptName, "?i", Flags, IntVariableCommand, &Data, Desc); \
}
#define MACRO_CONFIG_COL(Name,ScriptName,Def,Min,Max,Flags,Desc) \
{ \
static CIntVariableData Data = { this, &g_Config.m_##Name, Min, Max, Def }; \
Register(#ScriptName, "?i", Flags, ColVariableCommand, &Data, Desc); \
}
#define MACRO_CONFIG_STR(Name,ScriptName,Len,Def,Flags,Desc) \
{ \
static char OldValue[Len] = Def; \
@ -847,6 +943,7 @@ CConsole::CConsole(int FlagMask)
#include "config_variables.h"
#undef MACRO_CONFIG_INT
#undef MACRO_CONFIG_COL
#undef MACRO_CONFIG_STR
// DDRace
@ -1110,6 +1207,8 @@ void CConsole::ResetServerGameSettings()
} \
}
#define MACRO_CONFIG_COL(...) MACRO_CONFIG_INT(__VA_ARGS__)
#define MACRO_CONFIG_STR(Name,ScriptName,Len,Def,Flags,Desc) \
{ \
if(((Flags) & (CFGFLAG_SERVER|CFGFLAG_GAME)) == (CFGFLAG_SERVER|CFGFLAG_GAME)) \
@ -1131,6 +1230,7 @@ void CConsole::ResetServerGameSettings()
#include "config_variables.h"
#undef MACRO_CONFIG_INT
#undef MACRO_CONFIG_COL
#undef MACRO_CONFIG_STR
}

View file

@ -119,6 +119,7 @@ class CConsole : public IConsole
virtual const char *GetString(unsigned Index);
virtual int GetInteger(unsigned Index);
virtual float GetFloat(unsigned Index);
virtual int GetColor(unsigned Index);
// DDRace

View file

@ -115,6 +115,8 @@ void CTeeHistorian::WriteHeader(const CGameInfo *pGameInfo)
First = false; \
}
#define MACRO_CONFIG_COL(...) MACRO_CONFIG_INT(__VA_ARGS__)
#define MACRO_CONFIG_STR(Name,ScriptName,Len,Def,Flags,Desc) \
if((Flags)&CFGFLAG_SERVER && !((Flags)&CFGFLAG_NONTEEHISTORIC) && str_comp(pGameInfo->m_pConfig->m_##Name, (Def)) != 0) \
{ \
@ -129,6 +131,7 @@ void CTeeHistorian::WriteHeader(const CGameInfo *pGameInfo)
#include <engine/shared/config_variables.h>
#undef MACRO_CONFIG_INT
#undef MACRO_CONFIG_COL
#undef MACRO_CONFIG_STR
str_format(aJson, sizeof(aJson), "},\"tuning\":{");

View file

@ -77,8 +77,8 @@ MACRO_CONFIG_INT(ClAutoStatboardScreenshotMax, cl_auto_statboard_screenshot_max,
MACRO_CONFIG_INT(ClDefaultZoom, cl_default_zoom, 10, 0, 20, CFGFLAG_CLIENT|CFGFLAG_SAVE, "Default zoom level (10 default, min 0, max 20)")
MACRO_CONFIG_INT(ClPlayerUseCustomColor, player_use_custom_color, 0, 0, 1, CFGFLAG_CLIENT|CFGFLAG_SAVE, "Toggles usage of custom colors")
MACRO_CONFIG_INT(ClPlayerColorBody, player_color_body, 65408, 0, 0xFFFFFF, CFGFLAG_CLIENT|CFGFLAG_SAVE, "Player body color")
MACRO_CONFIG_INT(ClPlayerColorFeet, player_color_feet, 65408, 0, 0xFFFFFF, CFGFLAG_CLIENT|CFGFLAG_SAVE, "Player feet color")
MACRO_CONFIG_COL(ClPlayerColorBody, player_color_body, 65408, 0, 0xFFFFFF, CFGFLAG_CLIENT|CFGFLAG_SAVE, "Player body color")
MACRO_CONFIG_COL(ClPlayerColorFeet, player_color_feet, 65408, 0, 0xFFFFFF, CFGFLAG_CLIENT|CFGFLAG_SAVE, "Player feet color")
MACRO_CONFIG_STR(ClPlayerSkin, player_skin, 24, "default", CFGFLAG_CLIENT|CFGFLAG_SAVE, "Player skin")
MACRO_CONFIG_STR(ClSkinPrefix, cl_skin_prefix, 100, "", CFGFLAG_CLIENT|CFGFLAG_SAVE, "Replace the skins by skins with this prefix (e.g. kitty, coala, santa)")
MACRO_CONFIG_INT(ClFatSkins, cl_fat_skins, 0, 0, 1, CFGFLAG_CLIENT|CFGFLAG_SAVE, "Enable fat skins")

View file

@ -33,10 +33,12 @@ protected:
mem_zero(&m_Config, sizeof(m_Config));
#define MACRO_CONFIG_INT(Name,ScriptName,Def,Min,Max,Save,Desc) \
m_Config.m_##Name = (Def);
#define MACRO_CONFIG_COL(...) MACRO_CONFIG_INT(__VA_ARGS__)
#define MACRO_CONFIG_STR(Name,ScriptName,Len,Def,Save,Desc) \
str_copy(m_Config.m_##Name, (Def), sizeof(m_Config.m_##Name));
#include <engine/shared/config_variables.h>
#undef MACRO_CONFIG_STR
#undef MACRO_CONFIG_COL
#undef MACRO_CONFIG_INT
RegisterUuids(&m_UuidManager);