-
Notifications
You must be signed in to change notification settings - Fork 1
Add some flair/polish to countdown #29
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
| "packages": [ | ||
| { | ||
| "uri": "karnkaul/le2d", | ||
| "branch": "v0.4.7" | ||
| "branch": "v0.4.8" | ||
| } | ||
| ] | ||
| } | ||
Binary file not shown.
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
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,6 @@ | ||
| #pragma once | ||
| #include <kvf/color.hpp> | ||
|
|
||
| namespace chomper::theme { | ||
| constexpr auto clearColor_v = kvf::Color{glm::vec4{.34f, .54f, .2f, 1.f}}; | ||
| } // namespace chomper::theme |
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,34 @@ | ||
| #pragma once | ||
| #include "le2d/drawable/shape.hpp" | ||
| #include "le2d/drawable/text.hpp" | ||
| #include <kvf/time.hpp> | ||
|
|
||
| namespace chomper::ui { | ||
| class Countdown { | ||
| public: | ||
| static constexpr auto textHeight_v = le::TextHeight{120}; | ||
|
|
||
| explicit Countdown(gsl::not_null<le::IFont*> font, le::TextHeight textHeight = textHeight_v, kvf::Seconds timer = 3s); | ||
|
|
||
| [[nodiscard]] auto getRemain() const -> kvf::Seconds { | ||
| return m_remain; | ||
| } | ||
|
|
||
| void tick(kvf::Seconds dt); | ||
| void draw(le::IRenderer& renderer) const; | ||
|
|
||
| private: | ||
| void setTimerText(std::chrono::seconds value); | ||
| void updateSector(); | ||
|
|
||
| gsl::not_null<le::IFont*> m_font; | ||
| le::TextHeight m_textHeight{}; | ||
|
|
||
| le::drawable::Sector m_sector{}; | ||
| le::drawable::Circle m_background{}; | ||
| le::drawable::Text m_text{}; | ||
|
|
||
| kvf::Seconds m_timer{}; | ||
| kvf::Seconds m_remain{}; | ||
| }; | ||
| } // namespace chomper::ui | ||
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
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
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,66 @@ | ||
| #include "chomper/ui/countdown.hpp" | ||
| #include "chomper/theme.hpp" | ||
| #include <klib/assert.hpp> | ||
|
|
||
| namespace chomper::ui { | ||
| Countdown::Countdown(gsl::not_null<le::IFont*> font, le::TextHeight const textHeight, kvf::Seconds const timer) | ||
| : m_font(font), m_textHeight(textHeight), m_timer(timer), m_remain(timer) { | ||
| auto const diameter = float(m_textHeight) + 30.0f; | ||
| m_background.create(diameter); | ||
| m_background.tint = theme::clearColor_v; | ||
| } | ||
|
|
||
| void Countdown::tick(kvf::Seconds const dt) { | ||
| if (m_remain <= 0s) { | ||
| return; | ||
| } | ||
|
|
||
| auto const prevSeconds = std::chrono::duration_cast<std::chrono::seconds>(m_remain); | ||
| m_remain -= dt; | ||
| auto const currSeconds = std::chrono::duration_cast<std::chrono::seconds>(m_remain); | ||
| if (prevSeconds > currSeconds) { | ||
| setTimerText(currSeconds + 1s); | ||
| } | ||
|
|
||
| updateSector(); | ||
| } | ||
|
|
||
| void Countdown::draw(le::IRenderer& renderer) const { | ||
| if (m_remain <= 0s) { | ||
| return; | ||
| } | ||
|
|
||
| m_sector.draw(renderer); | ||
| m_background.draw(renderer); | ||
| m_text.draw(renderer); | ||
| } | ||
|
|
||
| void Countdown::setTimerText(std::chrono::seconds const value) { | ||
| auto const params = le::drawable::Text::Params{ | ||
| .height = m_textHeight, | ||
| .expand = le::drawable::TextExpand::eBoth, | ||
| }; | ||
| auto const text = std::format("{}", value.count()); | ||
| m_text.set_string(*m_font, text, params); | ||
|
|
||
| // technically this isn't correct y-centering because parts of glyphs can be above/below the baseline, | ||
| // and Text::get_size() is insufficient to adjust for that. | ||
| // here, since each displayed glyph (0-9) is entirely above the baseline (unlike say 'g'), | ||
| // it can be pushed down by half the size and it will "look" consistently y-centered. | ||
| // this is what is known as a "hack". | ||
| m_text.transform.position.y = -0.5f * m_text.get_size().y; | ||
| } | ||
|
|
||
| void Countdown::updateSector() { | ||
| KLIB_ASSERT(m_timer > 0s); | ||
| auto const ratio = m_remain / m_timer; | ||
| static constexpr auto degreesBegin_v{90.0f}; | ||
| auto const degreesEnd = degreesBegin_v + (ratio * 360.0f); | ||
| auto const diameter = m_background.get_diameter() + 30.0f; | ||
| auto const sectorParams = le::shape::Sector::Params{ | ||
| .degrees_begin = degreesBegin_v, | ||
| .degrees_end = degreesEnd, | ||
| }; | ||
| m_sector.create(diameter, sectorParams); | ||
| } | ||
| } // namespace chomper::ui |
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.