Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow overwriting all style colors #397

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion include/SSVOpenHexagon/Data/StyleData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,20 @@ class StyleData
float _3dPulseSpeed{};
float _3dPerspectiveMult{};

sf::Color _mainOverrideColor;
sf::Color _playerOverrideColor;
sf::Color _textOverrideColor;
sf::Color _wallOverrideColor;
sf::Color _capOverrideColor;
sf::Color _3dOverrideColor;
std::vector<sf::Color> _overrideColors;
std::vector<sf::Color> _3dOverrideColors;

float bgTileRadius{10000.f};
unsigned int BGColorOffset{0};
float BGRotOff{0}; // In degrees

private:
sf::Color _3dOverrideColor;
ColorData mainColorData;
ColorData playerColor;
ColorData textColor;
Expand All @@ -102,6 +110,8 @@ class StyleData

std::vector<ColorData> colorDatas;

float last3dDepth{};

public:
explicit StyleData();
explicit StyleData(const ssvuj::Obj& mRoot);
Expand Down
29 changes: 20 additions & 9 deletions src/SSVOpenHexagon/Core/HGGraphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,19 +198,26 @@ void HexagonGame::draw()
const sf::Vector2f newPos(offset * cosRot, offset * sinRot);

sf::Color overrideColor;
bool mustOverride{styleData._3dOverrideColors[i].a != 0};

if(!Config::getBlackAndWhite())
{
overrideColor = Utils::getColorDarkened(
styleData.get3DOverrideColor(), styleData._3dDarkenMult);
overrideColor = mustOverride
? styleData._3dOverrideColors[i]
: Utils::getColorDarkened(
styleData.get3DOverrideColor(),
styleData._3dDarkenMult);
}
else
{
overrideColor = Utils::getColorDarkened(
sf::Color(255, 255, 255, styleData.getMainColor().a),
styleData._3dDarkenMult);
}
adjustAlpha(overrideColor, i);
if(!mustOverride)
{
adjustAlpha(overrideColor, i);
}

// Draw pivot layers
for(std::size_t k = j * numPivotQuads; k < (j + 1) * numPivotQuads;
Expand All @@ -220,7 +227,7 @@ void HexagonGame::draw()
pivotQuads3D[k].color = overrideColor;
}

if(styleData.get3DOverrideColor() == styleData.getMainColor())
if(styleData.get3DOverrideColor() == styleData.getMainColor() && !mustOverride)
{
overrideColor = Utils::getColorDarkened(
getColorWall(), styleData._3dDarkenMult);
Expand All @@ -237,7 +244,7 @@ void HexagonGame::draw()
}

// Apply player color if no 3D override is present.
if(styleData.get3DOverrideColor() == styleData.getMainColor())
if(styleData.get3DOverrideColor() == styleData.getMainColor() && !mustOverride)
{
overrideColor = Utils::getColorDarkened(
getColorPlayer(), styleData._3dDarkenMult);
Expand Down Expand Up @@ -596,7 +603,8 @@ void HexagonGame::updateText(ssvu::FT mFT)
}
}

void HexagonGame::drawText_TimeAndStatus(const sf::Color& offsetColor, const sf::RenderStates& mStates)
void HexagonGame::drawText_TimeAndStatus(
const sf::Color& offsetColor, const sf::RenderStates& mStates)
{
if(Config::getDrawTextOutlines())
{
Expand Down Expand Up @@ -701,14 +709,17 @@ static void drawTextMessagePBImpl(sf::Text& text, const sf::Color& offsetColor,
fRender(text);
}

void HexagonGame::drawText_Message(const sf::Color& offsetColor, const sf::RenderStates& mStates)
void HexagonGame::drawText_Message(
const sf::Color& offsetColor, const sf::RenderStates& mStates)
{
drawTextMessagePBImpl(messageText, offsetColor,
{Config::getWidth() / 2.f, Config::getHeight() / 5.5f}, getColorText(),
1.f /* outlineThickness */, [this, &mStates](sf::Text& t) { render(t, mStates); });
1.f /* outlineThickness */,
[this, &mStates](sf::Text& t) { render(t, mStates); });
}

void HexagonGame::drawText_PersonalBest(const sf::Color& offsetColor, const sf::RenderStates& mStates)
void HexagonGame::drawText_PersonalBest(
const sf::Color& offsetColor, const sf::RenderStates& mStates)
{
drawTextMessagePBImpl(pbText, offsetColor,
{Config::getWidth() / 2.f,
Expand Down
74 changes: 72 additions & 2 deletions src/SSVOpenHexagon/Core/LuaScripting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1156,16 +1156,42 @@ static void initStyleControl(Lua::LuaContext& lua, StyleData& styleData)
" color computed by the level style."));
};

const auto sdColorSetter =
[&lua, &styleData](const char* name, const char* docName, auto pmd)
{
addLuaFn(lua, name,
[&styleData, pmd](int r, int g, int b, int a)
{ (styleData.*pmd) = sf::Color(r, g, b, a); })
.arg("r")
.arg("g")
.arg("b")
.arg("a")
.doc(Utils::concat(
"Set the ", docName, " color (only used if `$3` is not 0)"));
};

sdColorGetter("s_getMainColor", "main", &StyleData::getMainColor);
sdColorGetter("s_getPlayerColor", "player", &StyleData::getPlayerColor);
sdColorGetter("s_getTextColor", "text", &StyleData::getTextColor);

sdColorGetter("s_getWallColor", "wall", &StyleData::getWallColor);
sdColorGetter(
"s_get3DOverrideColor", "3D override", &StyleData::get3DOverrideColor);

sdColorGetter("s_getCapColorResult", "cap color result",
&StyleData::getCapColorResult);

sdColorSetter("s_setMainOverrideColor", "main override",
&StyleData::_mainOverrideColor);
sdColorSetter("s_setPlayerOverrideColor", "player override",
&StyleData::_playerOverrideColor);
sdColorSetter("s_setTextOverrideColor", "text override",
&StyleData::_textOverrideColor);
sdColorSetter("s_setWallOverrideColor", "wall override",
&StyleData::_wallOverrideColor);
sdColorSetter(
"s_set3DOverrideColor", "3D override", &StyleData::_3dOverrideColor);
sdColorSetter(
"s_setCapOverrideColor", "cap override", &StyleData::_capOverrideColor);

addLuaFn(lua, "s_getColor",
[&styleData, &colorToTuple](int mIndex)
{
Expand All @@ -1180,6 +1206,50 @@ static void initStyleControl(Lua::LuaContext& lua, StyleData& styleData)
.doc(
"Return the current color with index `$0` computed by the level "
"style.");
addLuaFn(lua, "s_setOverrideColor",
[&styleData](int mIndex, int r, int g, int b, int a)
{
if(mIndex < styleData._overrideColors.size() && mIndex >= 0)
{
styleData._overrideColors[mIndex] = sf::Color(r, g, b, a);
}
else
{
ssvu::lo("hg::LuaScripting::initStyleControl")
<< "`s_setOverrideColor` failed, no color found at index "
<< mIndex << "\n";
}
})
.arg("index")
.arg("r")
.arg("g")
.arg("b")
.arg("a")
.doc("Set the color with index `$0` (only used if `$4` is not 0)");

addLuaFn(lua, "s_set3DLayerOverrideColor",
[&styleData](int mIndex, int r, int g, int b, int a)
{
if(mIndex < styleData._3dOverrideColors.size() && mIndex >= 0)
{
styleData._3dOverrideColors[mIndex] = sf::Color(r, g, b, a);
}
else
{
ssvu::lo("hg::LuaScripting::initStyleControl")
<< "`s_set3DLayerOverrideColor` failed, no color found at "
"index "
<< mIndex << "\n";
}
})
.arg("index")
.arg("r")
.arg("g")
.arg("b")
.arg("a")
.doc(
"Set the 3D layer color with index `$0` (only used if `$4` is not "
"0)");
}

static void initExecScript(Lua::LuaContext& lua, HGAssets& assets,
Expand Down
70 changes: 55 additions & 15 deletions src/SSVOpenHexagon/Data/StyleData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ StyleData::StyleData(const ssvuj::Obj& mRoot)
_3dPulseSpeed{ssvuj::getExtr<float>(mRoot, "3D_pulse_speed", 0.01f)},
_3dPerspectiveMult{
ssvuj::getExtr<float>(mRoot, "3D_perspective_multiplier", 1.f)},
_mainOverrideColor{sf::Color::Transparent},
_playerOverrideColor{sf::Color::Transparent},
_textOverrideColor{sf::Color::Transparent},
_wallOverrideColor{sf::Color::Transparent},
_capOverrideColor{sf::Color::Transparent},
_3dOverrideColor{ssvuj::getExtr<sf::Color>(
mRoot, "3D_override_color", sf::Color::Transparent)},
mainColorData{ssvuj::getObj(mRoot, "main")}, //
Expand All @@ -62,17 +67,25 @@ StyleData::StyleData(const ssvuj::Obj& mRoot)
textColor{
colorDataFromObjOrDefault(mRoot, "text_color", mainColorData)}, //
wallColor{colorDataFromObjOrDefault(mRoot, "wall_color", mainColorData)},
capColor{parseCapColor(ssvuj::getObj(mRoot, "cap_color"))}
capColor{parseCapColor(ssvuj::getObj(mRoot, "cap_color"))},
last3dDepth{_3dDepth}
{
currentHue = hueMin;

const auto& objColors(ssvuj::getObj(mRoot, "colors"));
const auto& colorCount(ssvuj::getObjSize(objColors));

colorDatas.reserve(colorCount);
_overrideColors.reserve(colorCount);
for(auto i(0u); i < colorCount; i++)
{
colorDatas.emplace_back(ssvuj::getObj(objColors, i));
_overrideColors.emplace_back(sf::Color::Transparent);
}
_3dOverrideColors.reserve(_3dDepth);
for(auto i(0u); i < _3dDepth; i++)
{
_3dOverrideColors.emplace_back(sf::Color::Transparent);
}
}

Expand Down Expand Up @@ -167,23 +180,46 @@ void StyleData::update(ssvu::FT mFT, float mMult)
pulseIncrement *= -1.f;
pulseFactor = pulseMax;
}

if(_3dDepth != last3dDepth)
{
_3dOverrideColors.clear();
for(auto i(0u); i < _3dDepth; i++)
{
_3dOverrideColors.emplace_back(sf::Color::Transparent);
}
last3dDepth = _3dDepth;
}
}

void StyleData::computeColors()
{
currentMainColor = calculateColor(currentHue, pulseFactor, mainColorData);
currentPlayerColor = calculateColor(currentHue, pulseFactor, playerColor);
currentTextColor = calculateColor(currentHue, pulseFactor, textColor);
currentWallColor = calculateColor(currentHue, pulseFactor, wallColor);
currentMainColor =
_mainOverrideColor.a != 0
? _mainOverrideColor
: calculateColor(currentHue, pulseFactor, mainColorData);
currentPlayerColor =
_playerOverrideColor.a != 0
? _playerOverrideColor
: calculateColor(currentHue, pulseFactor, playerColor);
currentTextColor = _textOverrideColor.a != 0
? _textOverrideColor
: calculateColor(currentHue, pulseFactor, textColor);
currentWallColor = _wallOverrideColor.a != 0
? _wallOverrideColor
: calculateColor(currentHue, pulseFactor, wallColor);

current3DOverrideColor =
_3dOverrideColor.a != 0 ? _3dOverrideColor : getMainColor();

currentColors.clear();

for(const ColorData& cd : colorDatas)
for(auto i(0u); i < colorDatas.size(); ++i)
{
currentColors.emplace_back(calculateColor(currentHue, pulseFactor, cd));
currentColors.emplace_back(
_overrideColors[i].a != 0
? _overrideColors[i]
: calculateColor(currentHue, pulseFactor, colorDatas[i]));
}

if(currentColors.size() > 1)
Expand Down Expand Up @@ -341,14 +377,18 @@ StyleData::getColors() const noexcept

sf::Color StyleData::getCapColorResult() const noexcept
{
return Utils::match(
capColor, //
[this](CapColorMode::Main) { return getMainColor(); }, //
[this](CapColorMode::MainDarkened)
{ return Utils::getColorDarkened(getMainColor(), 1.4f); }, //
[this](CapColorMode::ByIndex x) { return getColor(x._index); }, //
[this](const ColorData& data)
{ return calculateColor(currentHue, pulseFactor, data); });
return _capOverrideColor.a != 0
? _capOverrideColor
: Utils::match(
capColor, //
[this](CapColorMode::Main) { return getMainColor(); }, //
[this](CapColorMode::MainDarkened) {
return Utils::getColorDarkened(getMainColor(), 1.4f);
}, //
[this](CapColorMode::ByIndex x)
{ return getColor(x._index); }, //
[this](const ColorData& data)
{ return calculateColor(currentHue, pulseFactor, data); });
}

} // namespace hg