expose LoadingSpinner for mods to use

This commit is contained in:
HJfod 2024-07-29 12:50:22 +03:00
parent f489878952
commit 5c840127fa
3 changed files with 72 additions and 45 deletions

View file

@ -0,0 +1,29 @@
#pragma once
#include <cocos2d.h>
namespace geode {
/**
* An eternally spinning loading circle. Essentially just a more convenient
* alternative to RobTop's `LoadingCircle` class, as this one respects its
* content size and is a lot more stripped down (not requiring a `show`
* method or anything - it just works!)
*/
class LoadingSpinner : public cocos2d::CCNode {
protected:
cocos2d::CCSprite* m_spinner;
bool init(float size);
void spin();
public:
/**
* Create a loading circle
* @param size The diameter of the circle in Cocos units
*/
static LoadingSpinner* create(float size);
void setVisible(bool visible) override;
};
}

View file

@ -3,6 +3,7 @@
#include <Geode/utils/ColorProvider.hpp>
#include <Geode/loader/SettingEvent.hpp>
#include <Geode/binding/ButtonSprite.hpp>
#include <Geode/ui/LoadingSpinner.hpp>
$on_mod(Loaded) {
// todo: these names should probably be shorter so they fit in SSO...
@ -134,51 +135,6 @@ void GeodeSquareSprite::setState(bool state) {
}
}
class LoadingSpinner : public CCNode {
protected:
CCSprite* m_spinner;
bool init(float sideLength) {
if (!CCNode::init())
return false;
this->setID("loading-spinner");
this->setContentSize({ sideLength, sideLength });
this->setAnchorPoint({ .5f, .5f });
m_spinner = CCSprite::create("loadingCircle.png");
m_spinner->setBlendFunc({ GL_ONE, GL_ONE });
limitNodeSize(m_spinner, m_obContentSize, 1.f, .1f);
this->addChildAtPosition(m_spinner, Anchor::Center);
this->spin();
return true;
}
void spin() {
m_spinner->runAction(CCRepeatForever::create(CCRotateBy::create(1.f, 360.f)));
}
public:
static LoadingSpinner* create(float sideLength) {
auto ret = new LoadingSpinner();
if (ret->init(sideLength)) {
ret->autorelease();
return ret;
}
delete ret;
return nullptr;
}
void setVisible(bool visible) override {
CCNode::setVisible(visible);
if (visible) {
this->spin();
}
}
};
CCNode* createLoadingCircle(float sideLength, const char* id) {
auto spinner = LoadingSpinner::create(sideLength);
spinner->setID(id);

View file

@ -0,0 +1,42 @@
#include <Geode/ui/LoadingSpinner.hpp>
using namespace geode::prelude;
bool LoadingSpinner::init(float sideLength) {
if (!CCNode::init())
return false;
this->setID("loading-spinner");
this->setContentSize({ sideLength, sideLength });
this->setAnchorPoint({ .5f, .5f });
m_spinner = CCSprite::create("loadingCircle.png");
m_spinner->setBlendFunc({ GL_ONE, GL_ONE });
limitNodeSize(m_spinner, m_obContentSize, 1.f, .1f);
this->addChildAtPosition(m_spinner, Anchor::Center);
this->spin();
return true;
}
void LoadingSpinner::spin() {
m_spinner->runAction(CCRepeatForever::create(CCRotateBy::create(1.f, 360.f)));
}
LoadingSpinner* LoadingSpinner::create(float sideLength) {
auto ret = new LoadingSpinner();
if (ret->init(sideLength)) {
ret->autorelease();
return ret;
}
delete ret;
return nullptr;
}
void LoadingSpinner::setVisible(bool visible) {
CCNode::setVisible(visible);
if (visible) {
this->spin();
}
}