Skip to content

Commit 3a35321

Browse files
committed
add cancellation to LazySprite
1 parent f6a5fb3 commit 3a35321

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

loader/include/Geode/ui/LazySprite.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ namespace geode {
5050
bool isLoaded();
5151
bool isLoading();
5252

53+
/**
54+
* Cancel the sprite loading process. Does nothing if `isLoading == false`.
55+
* Callback will not be called, and sprite loading will be halted as soon as possible.
56+
*/
57+
void cancelLoad();
58+
5359
virtual bool initWithTexture(cocos2d::CCTexture2D* pTexture, const cocos2d::CCRect& rect) override;
5460
virtual bool initWithSpriteFrame(cocos2d::CCSpriteFrame* pSpriteFrame) override;
5561
virtual bool initWithSpriteFrameName(const char* pszSpriteFrameName) override;
@@ -61,7 +67,7 @@ namespace geode {
6167
Format m_expectedFormat;
6268
EventListener<utils::web::WebTask> m_listener;
6369
bool m_isLoading = false;
64-
bool m_hasLoaded = false;
70+
std::atomic_bool m_hasLoaded = false;
6571
bool m_autoresize;
6672
cocos2d::CCSize m_targetSize;
6773

loader/src/ui/nodes/LazySprite.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,9 @@ void LazySprite::loadFromFile(const std::filesystem::path& path, Format format,
213213
res = std::move(res)
214214
]() mutable {
215215
auto self = selfref.lock();
216-
if (!self) return;
216+
217+
// if sprite was destructed or loading has been cancelled, do nothing
218+
if (!self || !self->m_isLoading) return;
217219

218220
if (!res) {
219221
self->onError(fmt::format("failed to load from file {}: {}", path, res.unwrapErr()));
@@ -260,7 +262,7 @@ void LazySprite::doInitFromBytes(std::vector<uint8_t> data, std::string cacheKey
260262

261263
Loader::get()->queueInMainThread([selfref = std::move(selfref)]() mutable {
262264
auto self = selfref.lock();
263-
if (self) {
265+
if (self && self->m_isLoading) {
264266
self->onError("invalid image data or format");
265267
}
266268
});
@@ -269,15 +271,15 @@ void LazySprite::doInitFromBytes(std::vector<uint8_t> data, std::string cacheKey
269271
}
270272

271273
// image initialization succeeded, all we need to do now is to
272-
// create the opengl texture (must be on main thread!) and then set this sprite to use that.
274+
// create the OpenGL texture (must be on main thread!) and then set this sprite to use that.
273275

274276
Loader::get()->queueInMainThread([
275277
selfref = std::move(selfref),
276278
image,
277279
cacheKey = std::move(cacheKey)
278280
] {
279281
auto self = selfref.lock();
280-
if (!self) return;
282+
if (!self || !self->m_isLoading) return;
281283

282284
auto texture = new CCTexture2D();
283285
if (!texture->initWithImage(image)) {
@@ -399,6 +401,15 @@ bool LazySprite::isLoading() {
399401
return m_isLoading;
400402
}
401403

404+
void LazySprite::cancelLoad() {
405+
m_isLoading = false;
406+
407+
if (m_loadingCircle) {
408+
m_loadingCircle->removeFromParent();
409+
m_loadingCircle = nullptr;
410+
}
411+
}
412+
402413
LazySprite* LazySprite::create(CCSize size, bool loadingCircle) {
403414
auto ret = new LazySprite;
404415
if (ret->init(size, loadingCircle)) {

0 commit comments

Comments
 (0)