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 pack devs to specify font and font size for messages #379

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
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
Binary file not shown.
12 changes: 12 additions & 0 deletions _RELEASE/Packs/experimental/Levels/font.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"id": "font",
"name": "Test custom fonts",
"description": "",
"author": "Baum",
"menuPriority": 10,
"selectable": true,
"styleId": "sandbox",
"musicId": "jackRussel",
"luaFile": "Scripts/Levels/font.lua",
"difficultyMults": []
}
23 changes: 23 additions & 0 deletions _RELEASE/Packs/experimental/Scripts/Levels/font.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function onLoad()
e_messageAdd("This text is changing size!", 180)
e_wait(180)
e_eval("sizeChange = false")
e_eval("u_setMessageFont(\"freesansbold.ttf\")")
e_messageAdd("This text is using a custom font!", 200)
end

fontSize = 32
dir = 1
sizeChange = true
function onUpdate(frametime)
if sizeChange then
fontSize = fontSize + frametime * 2 * dir
if fontSize > 100 then
dir = -1
end
if fontSize < 10 then
dir = 1
end
end
u_setMessageFontSize(fontSize)
end
1 change: 1 addition & 0 deletions include/SSVOpenHexagon/Core/HGStatus.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ struct HexagonGameStatus
float radius{75};
float fastSpin{0};
float cameraShake{0};
float messageTextCharacterSize{32.f};
bool hasDied{false};
StateChange mustStateChange{StateChange::None};
bool scoreInvalid{false};
Expand Down
2 changes: 2 additions & 0 deletions include/SSVOpenHexagon/Global/Assets.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ class HGAssets
const std::string& mPackId, const ssvufs::Path& mPath);
void loadPackAssets_loadCustomSounds(
const std::string& mPackId, const ssvufs::Path& mPath);
void loadPackAssets_loadCustomFonts(
const std::string& mPackId, const ssvufs::Path& mPath);

[[nodiscard]] std::string getCurrentLocalProfileFilePath();

Expand Down
2 changes: 1 addition & 1 deletion src/SSVOpenHexagon/Core/HGGraphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ void HexagonGame::updateText(ssvu::FT mFT)
fpsText.setCharacterSize(getScaledCharacterSize(20.f));
}

messageText.setCharacterSize(getScaledCharacterSize(32.f));
messageText.setCharacterSize(getScaledCharacterSize(status.messageTextCharacterSize));
messageText.setOrigin({ssvs::getGlobalWidth(messageText) / 2.f, 0.f});

const float growth = std::sin(pbTextGrowth);
Expand Down
55 changes: 55 additions & 0 deletions src/SSVOpenHexagon/Core/HGScripting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,61 @@ void HexagonGame::initLua_Utils()
.doc(
"Force-swaps (180 degrees) the player when invoked. If `$0` is "
"`true`, the swap sound will be played.");

addLuaFn(lua, "u_setMessageFont", //
[this](const std::string& mFontId)
{
messageText.setFont(assets.getFontOrNullFont(
Utils::concat(getPackId(), "_", mFontId)));
})
.arg("fontId")
.doc(
"Sets the font messages will use to `$0` which is the filename of "
"a file inside the `Fonts` folder in your pack.");

addLuaFn(lua, "u_setDependencyMessageFont", //
[this](const std::string& mPackDisambiguator,
const std::string& mPackName, const std::string& mPackAuthor,
const std::string& mFontId)
{
try
{
const PackData& dependencyData =
Utils::findDependencyPackDataOrThrow(assets, getPackData(),
mPackDisambiguator, mPackName, mPackAuthor);
const auto path =
Utils::concat(dependencyData.id, "_", mFontId);
messageText.setFont(assets.getFontOrNullFont(path));
}
catch(const std::runtime_error& err)
{
ssvu::lo("hg::Utils::findDependencyPackDataOrThrow")
<< "Fatal error while looking for Lua dependency\nError: "
<< err.what() << std::endl;

throw;
}
catch(...)
{
ssvu::lo("hg::Utils::findDependencyPackDataOrThrow")
<< "Fatal unknown error while looking for Lua dependency"
<< std::endl;

throw;
}
})
.arg("packDisambiguator")
.arg("packName")
.arg("packAuthor")
.arg("fontId")
.doc(
"Sets the font messages will use to `$3` which is the filename of "
"a file inside the `Fonts` folder in the specified pack.");

addLuaFn(lua, "u_setMessageFontSize", //
[this](float mSize) { status.messageTextCharacterSize = mSize; })
.arg("size")
.doc("Sets the font size messages will use to `$0`.");
}

void HexagonGame::initLua_AudioControl()
Expand Down
3 changes: 3 additions & 0 deletions src/SSVOpenHexagon/Core/HexagonGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,9 @@ void HexagonGame::newGame(const std::string& mPackId, const std::string& mId,

debugPause = false;

// Font cleanup
messageText.setFont(font);

// Events cleanup
messageText.setString("");
pbText.setString("");
Expand Down
42 changes: 42 additions & 0 deletions src/SSVOpenHexagon/Global/Assets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,11 @@ HGAssets::~HGAssets()
{
loadPackAssets_loadCustomSounds(packId, packPath);
}

if(!levelsOnly && ssvufs::Path{packPath + "Fonts/"}.isFolder())
{
loadPackAssets_loadCustomFonts(packId, packPath);
}
}

if(ssvufs::Path{packPath + "Music/"}.isFolder() && !levelsOnly)
Expand Down Expand Up @@ -734,6 +739,22 @@ void HGAssets::loadPackAssets_loadCustomSounds(
}
}

void HGAssets::loadPackAssets_loadCustomFonts(
const std::string& mPackId, const ssvufs::Path& mPath)
{
for(const auto& p : scanSingleByExt(mPath + "Fonts/", ".ttf"))
{
if(!assetStorage->loadFont(
concatIntoBuf(mPackId, '_', p.getFileName()), p))
{
ssvu::lo("hg::loadPackAssets_loadCustomFonts")
<< "Failed to load font '" << p << "'\n";
}

++loadInfo.assets;
}
}

void HGAssets::loadPackAssets_loadMusic(
const std::string& mPackId, const ssvufs::Path& mPath)
{
Expand Down Expand Up @@ -1074,6 +1095,27 @@ void HGAssets::reloadAllShaders()
output += "Custom sound files successfully reloaded\n";
}

// Custom fonts
temp = mPath + "Fonts/";
if(!ssvufs::Path{temp}.isFolder())
{
output += "invalid custom font path\n";
}
else
{
for(const auto& p : scanSingleByExt(mPath + "Fonts/", ".ttf"))
{
temp = mPackId + "_" + p.getFileName();
if(!assetStorage->loadFont(temp, p))
{
output += "Failed to load font '";
output += p;
output += "'\n";
}
}
output += "Custom font files successfully reloaded\n";
}

return output;
}

Expand Down