Repository navigation
TextLabel: Full look, attach and options, in the game's units #330
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fbe6880
Widgets: World text markup, layout and draw
Kheartz b3b544b
TextLabel: Full look, attach and options, in the game's units
Kheartz a0d7835
Widgets: Hex colours need their leading #
Kheartz 745a1e5
Widgets: Look world text glyphs up once per draw
Kheartz 55ae977
TextLabel: Attached labels follow their target
Kheartz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
386 changes: 386 additions & 0 deletions
386
code/framework/src/external/imgui/widgets/world_text_layout.h
Large diffs are not rendered by default.
Oops, something went wrong.
165 changes: 165 additions & 0 deletions
165
code/framework/src/external/imgui/widgets/world_text_markup.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| /* | ||
| * MafiaHub OSS license | ||
| * Copyright (c) 2021-2026, MafiaHub. All rights reserved. | ||
| * | ||
| * This file comes from MafiaHub, hosted at https://github.com/MafiaHub/Framework. | ||
| * See LICENSE file in the source repository for information regarding licensing. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <cstdint> | ||
| #include <optional> | ||
| #include <string> | ||
| #include <string_view> | ||
| #include <vector> | ||
|
|
||
| namespace Framework::External::ImGUI::Widgets { | ||
| // A stretch of text sharing one inline style. color is 0xAARRGGBB; unset follows the label's own. | ||
| struct WorldTextRun { | ||
| std::string text; | ||
| std::optional<uint32_t> color; | ||
| bool bold = false; | ||
| bool italic = false; | ||
| }; | ||
|
|
||
| namespace Detail { | ||
| inline int HexDigit(char c) { | ||
| if (c >= '0' && c <= '9') { | ||
| return c - '0'; | ||
| } | ||
| if (c >= 'a' && c <= 'f') { | ||
| return c - 'a' + 10; | ||
| } | ||
| if (c >= 'A' && c <= 'F') { | ||
| return c - 'A' + 10; | ||
| } | ||
| return -1; | ||
| } | ||
|
|
||
| // "#RRGGBB" or "#RRGGBBAA" (CSS order) to 0xAARRGGBB. | ||
| inline std::optional<uint32_t> ParseHexColor(std::string_view tag) { | ||
| if ((tag.size() != 7 && tag.size() != 9) || tag[0] != '#') { | ||
| return std::nullopt; | ||
| } | ||
| uint32_t value = 0; | ||
| for (size_t i = 1; i < tag.size(); ++i) { | ||
| const int digit = HexDigit(tag[i]); | ||
| if (digit < 0) { | ||
| return std::nullopt; | ||
| } | ||
| value = (value << 4) | static_cast<uint32_t>(digit); | ||
| } | ||
| if (tag.size() == 7) { | ||
| return 0xFF000000u | value; | ||
| } | ||
| return (value >> 8) | ((value & 0xFFu) << 24); | ||
| } | ||
| } // namespace Detail | ||
|
|
||
| // Reads one code point at `i` and advances past it; malformed input yields U+FFFD one byte on. | ||
| inline uint32_t DecodeUtf8(std::string_view text, size_t &i) { | ||
| const auto byte = [&](size_t at) { | ||
| return static_cast<uint8_t>(text[at]); | ||
| }; | ||
| const uint8_t lead = byte(i); | ||
| size_t length = 0; | ||
| uint32_t cp = 0; | ||
| if (lead < 0x80) { | ||
| ++i; | ||
| return lead; | ||
| } | ||
| if ((lead & 0xE0) == 0xC0) { | ||
| length = 2; | ||
| cp = lead & 0x1F; | ||
| } | ||
| else if ((lead & 0xF0) == 0xE0) { | ||
| length = 3; | ||
| cp = lead & 0x0F; | ||
| } | ||
| else if ((lead & 0xF8) == 0xF0) { | ||
| length = 4; | ||
| cp = lead & 0x07; | ||
| } | ||
| if (length == 0 || i + length > text.size()) { | ||
| ++i; | ||
| return 0xFFFD; | ||
| } | ||
| for (size_t k = 1; k < length; ++k) { | ||
| if ((byte(i + k) & 0xC0) != 0x80) { | ||
| ++i; | ||
| return 0xFFFD; | ||
| } | ||
| cp = (cp << 6) | (byte(i + k) & 0x3F); | ||
| } | ||
| static constexpr uint32_t kMinimum[] {0, 0, 0x80, 0x800, 0x10000}; | ||
| i += length; | ||
| if (cp < kMinimum[length] || cp > 0x10FFFF || (cp >= 0xD800 && cp <= 0xDFFF)) { | ||
| return 0xFFFD; | ||
| } | ||
| return cp; | ||
| } | ||
|
|
||
| // Tags: {#RRGGBB} / {#RRGGBBAA} set the colour, {#} restores the label's, {b}{/b} and {i}{/i} | ||
| // toggle bold and italic, {{ is a literal brace. Anything else in braces stays as text. | ||
| inline std::vector<WorldTextRun> ParseWorldTextMarkup(std::string_view text) { | ||
| std::vector<WorldTextRun> runs; | ||
| WorldTextRun current; | ||
| const auto flush = [&]() { | ||
| if (!current.text.empty()) { | ||
| runs.push_back(current); | ||
| current.text.clear(); | ||
| } | ||
| }; | ||
|
|
||
| for (size_t i = 0; i < text.size(); ++i) { | ||
| const char c = text[i]; | ||
| if (c != '{') { | ||
| current.text.push_back(c); | ||
| continue; | ||
| } | ||
| if (i + 1 < text.size() && text[i + 1] == '{') { | ||
| current.text.push_back('{'); | ||
| ++i; | ||
| continue; | ||
| } | ||
| const size_t close = text.find('}', i + 1); | ||
| if (close == std::string_view::npos) { | ||
| current.text.push_back(c); | ||
| continue; | ||
| } | ||
|
|
||
| const std::string_view tag = text.substr(i + 1, close - i - 1); | ||
| std::optional<uint32_t> color; | ||
| bool known = true; | ||
| if (tag == "#") { | ||
| flush(); | ||
| current.color.reset(); | ||
| } | ||
| else if ((color = Detail::ParseHexColor(tag))) { | ||
| flush(); | ||
| current.color = color; | ||
| } | ||
| else if (tag == "b" || tag == "/b") { | ||
| flush(); | ||
| current.bold = tag == "b"; | ||
| } | ||
| else if (tag == "i" || tag == "/i") { | ||
| flush(); | ||
| current.italic = tag == "i"; | ||
| } | ||
| else { | ||
| known = false; | ||
| } | ||
|
|
||
| if (known) { | ||
| i = close; | ||
| } | ||
| else { | ||
| current.text.push_back(c); | ||
| } | ||
| } | ||
| flush(); | ||
| return runs; | ||
| } | ||
| } // namespace Framework::External::ImGUI::Widgets | ||
55 changes: 55 additions & 0 deletions
55
code/framework/src/integrations/client/ui/text_label_imgui.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /* | ||
| * MafiaHub OSS license | ||
| * Copyright (c) 2026, MafiaHub. All rights reserved. | ||
| * | ||
| * This file comes from MafiaHub, hosted at https://github.com/MafiaHub/Framework. | ||
| * See LICENSE file in the source repository for information regarding licensing. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <external/imgui/widgets/world_text_layout.h> | ||
| #include <external/imgui/widgets/world_text_markup.h> | ||
| #include <networking/replication/entities/text_label_entity.h> | ||
|
|
||
| #include <algorithm> | ||
| #include <vector> | ||
|
|
||
| namespace Framework::Integrations::Client::UI::TextLabels { | ||
| // A replicated TextLabelEntity in the terms of the world text widgets. The game resolves where the | ||
| // label is and whether it can be seen; this is only what it looks like. | ||
|
|
||
| static_assert(static_cast<int>(::Framework::External::ImGUI::Widgets::WorldTextAlign::Right) == static_cast<int>(::Framework::Networking::Replication::Entities::TextLabelAlign::Right)); | ||
| static_assert(static_cast<int>(::Framework::External::ImGUI::Widgets::WorldTextAnchor::Top) == static_cast<int>(::Framework::Networking::Replication::Entities::TextLabelAnchor::Top)); | ||
| static_assert(static_cast<int>(::Framework::External::ImGUI::Widgets::WorldTextAnchor::Bottom) == static_cast<int>(::Framework::Networking::Replication::Entities::TextLabelAnchor::Bottom)); | ||
| static_assert(::Framework::Networking::Replication::Entities::kTextLabelMaxOutlineWidth <= ::Framework::External::ImGUI::Widgets::kWorldTextMaxOutline, "the widget would cut a label's outline short"); | ||
| static_assert(::Framework::Networking::Replication::Entities::kTextLabelMaxShadowBlur <= ::Framework::External::ImGUI::Widgets::kWorldTextMaxBlur, "the widget would cut a label's blur short"); | ||
|
|
||
| inline ::Framework::External::ImGUI::Widgets::WorldTextLook Look(const ::Framework::Networking::Replication::Entities::TextLabelEntity &label) { | ||
| ::Framework::External::ImGUI::Widgets::WorldTextLook look; | ||
| look.color = label.color; | ||
| look.outlineWidth = std::clamp(label.outlineWidth, 0.0f, ::Framework::Networking::Replication::Entities::kTextLabelMaxOutlineWidth); | ||
| look.outlineColor = label.outlineColor; | ||
| look.shadowX = label.shadowOffsetX; | ||
| look.shadowY = label.shadowOffsetY; | ||
| look.shadowBlur = std::clamp(label.shadowBlur, 0.0f, ::Framework::Networking::Replication::Entities::kTextLabelMaxShadowBlur); | ||
| look.shadowColor = label.shadowColor; | ||
| look.backgroundColor = label.backgroundColor; | ||
| look.backgroundPadding = label.backgroundPadding; | ||
| look.backgroundRadius = label.backgroundRadius; | ||
| look.anchor = static_cast<::Framework::External::ImGUI::Widgets::WorldTextAnchor>(label.anchor); | ||
| return look; | ||
| } | ||
|
|
||
| inline ::Framework::External::ImGUI::Widgets::WorldTextAlign Align(const ::Framework::Networking::Replication::Entities::TextLabelEntity &label) { | ||
| return static_cast<::Framework::External::ImGUI::Widgets::WorldTextAlign>(label.align); | ||
| } | ||
|
|
||
| // Markup is parsed only when the label asks for it, so plain text never loses a brace. | ||
| inline std::vector<::Framework::External::ImGUI::Widgets::WorldTextRun> Runs(const ::Framework::Networking::Replication::Entities::TextLabelEntity &label) { | ||
| if (label.markup) { | ||
| return ::Framework::External::ImGUI::Widgets::ParseWorldTextMarkup(label.text); | ||
| } | ||
| return {::Framework::External::ImGUI::Widgets::WorldTextRun {label.text}}; | ||
| } | ||
| } // namespace Framework::Integrations::Client::UI::TextLabels |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.