Skip to content

Commit

Permalink
Merge pull request #184 from OpenVicProject/font-colour-codes
Browse files Browse the repository at this point in the history
Load font colour codes
  • Loading branch information
Hop311 authored Aug 6, 2024
2 parents 3c07aa7 + 2314a9b commit 9f9c584
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/openvic-simulation/dataloader/NodeTools.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ namespace OpenVic {

template<typename Key, typename Value, typename... MapArgs>
Callback<Value> auto map_callback(
tsl::ordered_map<Key const*, Value, MapArgs...>& map, Key const* key, bool warn = false
tsl::ordered_map<Key, Value, MapArgs...>& map, Key key, bool warn = false
) {
return [&map, key, warn](Value value) -> bool {
if (map.emplace(key, std::move(value)).second) {
Expand Down
4 changes: 2 additions & 2 deletions src/openvic-simulation/interface/GFXSprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ using namespace OpenVic::NodeTools;

Font::Font(
std::string_view new_identifier, colour_argb_t new_colour, std::string_view new_fontname, std::string_view new_charset,
uint32_t new_height
uint32_t new_height, colour_codes_t&& new_colour_codes
) : HasIdentifierAndAlphaColour { new_identifier, new_colour, false }, fontname { new_fontname }, charset { new_charset },
height { new_height } {}
height { new_height }, colour_codes { std::move(new_colour_codes) } {}

node_callback_t Sprite::expect_sprites(length_callback_t length_callback, callback_t<std::unique_ptr<Sprite>&&> callback) {
return expect_dictionary_keys_and_length(
Expand Down
7 changes: 5 additions & 2 deletions src/openvic-simulation/interface/GFXSprite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@ namespace OpenVic::GFX {
struct Font : HasIdentifierAndAlphaColour {
friend class OpenVic::UIManager;

using colour_codes_t = ordered_map<char, colour_t>;

private:
std::string PROPERTY(fontname);
std::string PROPERTY(charset);
uint32_t PROPERTY(height);
colour_codes_t PROPERTY(colour_codes);

// TODO - colorcodes, effect
// TODO - effect

Font(
std::string_view new_identifier, colour_argb_t new_colour, std::string_view new_fontname,
std::string_view new_charset, uint32_t new_height
std::string_view new_charset, uint32_t new_height, colour_codes_t&& new_colour_codes
);

public:
Expand Down
24 changes: 20 additions & 4 deletions src/openvic-simulation/interface/UI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ using namespace OpenVic::GFX;
using namespace OpenVic::GUI;

bool UIManager::add_font(
std::string_view identifier, colour_argb_t colour, std::string_view fontname, std::string_view charset, uint32_t height
std::string_view identifier, colour_argb_t colour, std::string_view fontname, std::string_view charset, uint32_t height,
Font::colour_codes_t&& colour_codes
) {
if (identifier.empty()) {
Logger::error("Invalid font identifier - empty!");
Expand All @@ -22,23 +23,38 @@ bool UIManager::add_font(
Logger::error("Invalid fontname for font ", identifier, " - empty!");
return false;
}
return fonts.add_item({ identifier, colour, fontname, charset, height }, duplicate_warning_callback);
return fonts.add_item(
{ identifier, colour, fontname, charset, height, std::move(colour_codes) },
duplicate_warning_callback
);
}

bool UIManager::_load_font(ast::NodeCPtr node) {
std::string_view identifier, fontname, charset;
colour_argb_t colour = colour_argb_t::null();
uint32_t height = 0;
Font::colour_codes_t colour_codes;

bool ret = expect_dictionary_keys(
"name", ONE_EXACTLY, expect_string(assign_variable_callback(identifier)),
"fontname", ONE_EXACTLY, expect_string(assign_variable_callback(fontname)),
"color", ONE_EXACTLY, expect_colour_hex(assign_variable_callback(colour)),
"charset", ZERO_OR_ONE, expect_string(assign_variable_callback(charset)),
"height", ZERO_OR_ONE, expect_uint(assign_variable_callback(height)),
"colorcodes", ZERO_OR_ONE, success_callback,
"colorcodes", ZERO_OR_ONE, expect_dictionary(
[&colour_codes](std::string_view key, ast::NodeCPtr value) -> bool {
if (key.size() != 1) {
Logger::error("Invalid colour code key: \"", key, "\" (expected single character)");
return false;
}
return expect_colour(map_callback(colour_codes, key.front()))(value);
}
),
"effect", ZERO_OR_ONE, success_callback
)(node);
ret &= add_font(identifier, colour, fontname, charset, height);

ret &= add_font(identifier, colour, fontname, charset, height, std::move(colour_codes));

return ret;
}

Expand Down
2 changes: 1 addition & 1 deletion src/openvic-simulation/interface/UI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace OpenVic {
public:
bool add_font(
std::string_view identifier, colour_argb_t colour, std::string_view fontname, std::string_view charset,
uint32_t height
uint32_t height, GFX::Font::colour_codes_t&& colour_codes
);

void lock_gfx_registries();
Expand Down

0 comments on commit 9f9c584

Please sign in to comment.