-
Notifications
You must be signed in to change notification settings - Fork 240
Progress bar helper class #1467
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 11 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
d78f3ad
progress bars
BlueWitherer 5833511
Merge branch 'geode-sdk:main' into main
BlueWitherer 9cd6e67
Merge branch 'geode-sdk:main' into main
BlueWitherer 4313cf7
Merge branch 'geode-sdk:main' into main
BlueWitherer 7dd5c09
merge classes and add pimpl
BlueWitherer 2ff3f4d
dont force options
BlueWitherer 969be70
organize
BlueWitherer 3a78b4e
customsetup, shorten func names
BlueWitherer 1a59f41
Merge branch 'geode-sdk:main' into main
BlueWitherer 0ba5b28
Merge branch 'geode-sdk:main' into main
BlueWitherer 2f8003c
Merge branch 'geode-sdk:main' into main
BlueWitherer 746e397
add percent precision, tweak some stuff
BlueWitherer de1d55c
update progressbar when setting precision
BlueWitherer 1b9cf7c
Merge branch 'geode-sdk:main' into main
BlueWitherer f4043ed
Merge branch 'geode-sdk:main' into main
BlueWitherer 32fdb95
fix cocos type defs
BlueWitherer 2f34cb9
fix dll
BlueWitherer 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
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,77 @@ | ||
| #pragma once | ||
|
|
||
| #include <cocos2d.h> | ||
|
|
||
| namespace geode { | ||
| // Enum for progress bar style | ||
| enum class ProgressBarStyle { | ||
| Level = 0, | ||
| Solid = 1, | ||
| }; | ||
|
|
||
| // Custom class for the progress bar | ||
| class ProgressBar : public cocos2d::CCNode { | ||
| protected: | ||
| class Impl; | ||
| std::unique_ptr<Impl> m_impl; | ||
|
|
||
| ProgressBar(); | ||
| ~ProgressBar(); | ||
|
|
||
| void customSetup(); | ||
HJfod marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| bool init() override; | ||
|
|
||
| public: | ||
| // Create a custom progress bar | ||
| static ProgressBar* create(); | ||
|
|
||
| /** | ||
| * Set the style of the progress bar | ||
| * | ||
| * @param style Style to switch to | ||
| */ | ||
| void setStyle(ProgressBarStyle style); | ||
|
|
||
| /** | ||
| * Set the color of the fill of the bar | ||
| * | ||
| * @param color RGB color object | ||
| */ | ||
| void setFillColor(ccColor3B color); | ||
|
|
||
| /** | ||
| * Update the size of the fill of the bar | ||
| * | ||
| * @param value A float from 0 to 100 | ||
| */ | ||
| virtual void updateProgress(float value); | ||
|
|
||
| /** | ||
| * Show the label displaying the current percentage of progress | ||
| * | ||
| * @param bool Whether to toggle visibility | ||
| */ | ||
| void showProgressLabel(bool show); | ||
|
|
||
| /** | ||
| * Get the current progress percentage of the bar | ||
| */ | ||
| float getProgress() const; | ||
|
|
||
| /** | ||
| * Get the progress percentage text label node | ||
| */ | ||
| CCLabelBMFont* getProgressLabel() const; | ||
|
|
||
| /** | ||
| * Get the current style of the progress bar | ||
| */ | ||
| ProgressBarStyle getStyle() const; | ||
|
|
||
| /** | ||
| * Get the current color of the fill of the progress bar | ||
| */ | ||
| ccColor3B getFillColor() const; | ||
| }; | ||
| } | ||
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,178 @@ | ||
| #include <cocos2d.h> | ||
| #include <Geode/ui/ProgressBar.hpp> | ||
|
|
||
| using namespace geode::prelude; | ||
|
|
||
| class ProgressBar::Impl final { | ||
| public: | ||
| // Progress bar outline | ||
| Ref<CCSprite> progressBar = nullptr; | ||
| // Progress bar fill | ||
| CCSprite* progressBarFill = nullptr; | ||
| // The text label displaying the percentage | ||
| Ref<CCLabelBMFont> progressPercentLabel = nullptr; | ||
|
|
||
| // Current progress bar fill percentage ranging from 0 to 100 | ||
| float progress = 0.0f; | ||
|
|
||
| // Style of the progress bar | ||
| ProgressBarStyle style = ProgressBarStyle::Level; | ||
| // Current color of the filled progress bar | ||
| ccColor3B progressBarFillColor = { 255, 255, 255 }; | ||
| // Whether to show the label showing the percentage of the current progress | ||
| bool showProgressPercentLabel = false; | ||
|
|
||
| // Max width for the progress fill bar node | ||
| float progressBarFillMaxWidth = 0.0f; | ||
| // Max height for the progress fill bar node | ||
| float progressBarFillMaxHeight = 0.0f; | ||
| }; | ||
|
|
||
| ProgressBar::ProgressBar() { | ||
| m_impl = std::make_unique<Impl>(); | ||
| }; | ||
|
|
||
| ProgressBar::~ProgressBar() {}; | ||
|
|
||
| void ProgressBar::customSetup() { | ||
| switch (m_impl->style) { | ||
| case ProgressBarStyle::Level: | ||
| m_impl->progressBar = CCSprite::create("slidergroove2.png"); | ||
| m_impl->progressBar->setID("progress-bar"); | ||
| m_impl->progressBar->setAnchorPoint({ 0.5, 0.5 }); | ||
| m_impl->progressBar->setPosition({ m_impl->progressBar->getScaledContentWidth() / 2.0f, m_impl->progressBar->getScaledContentHeight() / 2.0f }); | ||
| m_impl->progressBar->setZOrder(1); | ||
|
|
||
| m_impl->progressBarFill = CCSprite::create("sliderBar2.png"); | ||
| m_impl->progressBarFill->setID("progress-bar-fill"); | ||
| m_impl->progressBarFill->setAnchorPoint({ 0, 0.5 }); | ||
| m_impl->progressBarFill->setPosition({ 2.0f, m_impl->progressBar->getScaledContentHeight() / 2.0f }); | ||
| m_impl->progressBarFill->setColor(m_impl->progressBarFillColor); | ||
| m_impl->progressBarFill->setZOrder(-1); | ||
|
|
||
| m_impl->progressBarFillMaxWidth = m_impl->progressBar->getScaledContentWidth() - 4.0f; | ||
| m_impl->progressBarFillMaxHeight = m_impl->progressBarFill->getScaledContentHeight() - 0.5f; | ||
|
|
||
| m_impl->progressPercentLabel = CCLabelBMFont::create("0%", "bigFont.fnt"); | ||
| m_impl->progressPercentLabel->setID("progress-percent-label"); | ||
| m_impl->progressPercentLabel->setScale(0.5f); | ||
| m_impl->progressPercentLabel->setAnchorPoint({ 0, 0.5 }); | ||
| m_impl->progressPercentLabel->setPosition({ m_impl->progressBar->getScaledContentWidth() + 2.5f, m_impl->progressBar->getScaledContentHeight() / 2.0f }); | ||
| m_impl->progressPercentLabel->setAlignment(CCTextAlignment::kCCTextAlignmentLeft); | ||
| m_impl->progressPercentLabel->setVisible(m_impl->showProgressPercentLabel); | ||
| m_impl->progressPercentLabel->setZOrder(1); | ||
HJfod marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| break; | ||
|
|
||
| case ProgressBarStyle::Solid: | ||
| m_impl->progressBar = CCSprite::create("GJ_progressBar_001.png"); | ||
| m_impl->progressBar->setID("progress-bar"); | ||
| m_impl->progressBar->setAnchorPoint({ 0.5, 0.5 }); | ||
| m_impl->progressBar->setPosition({ m_impl->progressBar->getScaledContentWidth() / 2.0f, m_impl->progressBar->getScaledContentHeight() / 2.0f }); | ||
| m_impl->progressBar->setColor({ 0, 0, 0 }); | ||
| m_impl->progressBar->setOpacity(125); | ||
| m_impl->progressBar->setZOrder(-1); | ||
|
|
||
| m_impl->progressBarFill = CCSprite::create("GJ_progressBar_001.png"); | ||
| m_impl->progressBarFill->setID("progress-bar-fill"); | ||
| m_impl->progressBarFill->setScale(0.992f); | ||
| m_impl->progressBarFill->setScaleY(0.86f); | ||
| m_impl->progressBarFill->setAnchorPoint({ 0, 0.5 }); | ||
| m_impl->progressBarFill->setPosition({ 1.36f, m_impl->progressBar->getScaledContentHeight() / 2.0f }); | ||
| m_impl->progressBarFill->setColor(m_impl->progressBarFillColor); | ||
| m_impl->progressBarFill->setZOrder(0); | ||
|
|
||
| m_impl->progressBarFillMaxWidth = m_impl->progressBar->getScaledContentWidth(); | ||
| m_impl->progressBarFillMaxHeight = 20.0f; | ||
|
|
||
| m_impl->progressPercentLabel = CCLabelBMFont::create("0%", "bigFont.fnt"); | ||
| m_impl->progressPercentLabel->setID("progress-percent-label"); | ||
| m_impl->progressPercentLabel->setScale(0.5f); | ||
| m_impl->progressPercentLabel->setAnchorPoint({ 0.5, 0.5 }); | ||
| m_impl->progressPercentLabel->setPosition({ m_impl->progressBar->getScaledContentWidth() / 2.0f, m_impl->progressBar->getScaledContentHeight() / 2.0f }); | ||
| m_impl->progressPercentLabel->setAlignment(CCTextAlignment::kCCTextAlignmentCenter); | ||
| m_impl->progressPercentLabel->setVisible(m_impl->showProgressPercentLabel); | ||
| m_impl->progressPercentLabel->setZOrder(1); | ||
| break; | ||
| }; | ||
|
|
||
| this->setScaledContentSize(m_impl->progressBar->getScaledContentSize()); | ||
|
|
||
| m_impl->progressBar->addChild(m_impl->progressBarFill); | ||
|
|
||
| this->addChild(m_impl->progressBar); | ||
| this->addChild(m_impl->progressPercentLabel); | ||
|
|
||
| this->updateProgress(m_impl->progress); | ||
| }; | ||
|
|
||
| bool ProgressBar::init() { | ||
| if (!CCNode::init()) return false; | ||
|
|
||
| this->customSetup(); | ||
|
|
||
| return true; | ||
| }; | ||
|
|
||
| void ProgressBar::setStyle(ProgressBarStyle style) { | ||
| if (m_impl->style != style) { | ||
| m_impl->style = style; | ||
|
|
||
| this->removeAllChildren(); | ||
| this->customSetup(); // setup again with new style | ||
| }; | ||
| }; | ||
|
|
||
| void ProgressBar::setFillColor(ccColor3B color) { | ||
| m_impl->progressBarFillColor = color; | ||
| if (m_impl->progressBarFill) m_impl->progressBarFill->setColor(color); | ||
| }; | ||
|
|
||
| void ProgressBar::updateProgress(float value) { | ||
| if (value > 100.0f) value = 100.0f; | ||
| if (value < 0.0f) value = 0.0f; | ||
|
|
||
| m_impl->progress = value; | ||
|
|
||
| if (m_impl->progressBarFill) { | ||
| float width = m_impl->progressBarFillMaxWidth * (m_impl->progress / 100.0f); | ||
| m_impl->progressBarFill->setTextureRect({ 0.0f, 0.0f, width, m_impl->progressBarFillMaxHeight }); | ||
| }; | ||
|
|
||
| if (m_impl->progressPercentLabel) { | ||
| auto percentString = fmt::format("{}%", static_cast<int>(m_impl->progress)); | ||
HJfod marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| m_impl->progressPercentLabel->setCString(percentString.c_str()); | ||
| }; | ||
| }; | ||
|
|
||
| void ProgressBar::showProgressLabel(bool show) { | ||
| m_impl->showProgressPercentLabel = show; | ||
| if (m_impl->progressPercentLabel) m_impl->progressPercentLabel->setVisible(show); | ||
| }; | ||
|
|
||
| float ProgressBar::getProgress() const { | ||
| return m_impl->progress; | ||
| }; | ||
|
|
||
| CCLabelBMFont* ProgressBar::getProgressLabel() const { | ||
| return m_impl->progressPercentLabel; | ||
| }; | ||
|
|
||
| ProgressBarStyle ProgressBar::getStyle() const { | ||
| return m_impl->style; | ||
| }; | ||
|
|
||
| ccColor3B ProgressBar::getFillColor() const { | ||
| return m_impl->progressBarFillColor; | ||
| }; | ||
|
|
||
| ProgressBar* ProgressBar::create() { | ||
| auto ret = new ProgressBar(); | ||
|
|
||
| if (ret && ret->init()) { | ||
| ret->autorelease(); | ||
| return ret; | ||
| }; | ||
|
|
||
| delete ret; | ||
| return nullptr; | ||
| }; | ||
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.