Skip to content

Commit 7660611

Browse files
authored
Update GYScreenshotPopup.hpp
1 parent 0f148df commit 7660611

File tree

1 file changed

+5
-91
lines changed

1 file changed

+5
-91
lines changed

src/layers/GYScreenshotPopup.hpp

Lines changed: 5 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -15,97 +15,11 @@ class GYScreenshotPopup : public geode::Popup<int const&> {
1515
Ref<CCImage> m_image;
1616
CCSprite* m_sprite;
1717

18-
bool setup(int const& layer) override {
19-
Tags tags;
20-
auto layerName = tags.getStringFromTag(layer);
21-
22-
if (layerName.empty()) {
23-
log::error("Layer name is empty for tag: {}", layer);
24-
return false;
25-
}
26-
27-
std::string result = extractLastSegment(layerName);
28-
29-
auto winSize = CCDirector::sharedDirector()->getWinSize();
30-
if (!m_mainLayer) {
31-
log::error("m_mainLayer is not initialized");
32-
return false;
33-
}
34-
35-
this->setTitle(result);
36-
m_mainLayer->setContentSize({ winSize.width * 0.6f, winSize.height * 0.7f });
37-
m_mainLayer->updateLayout();
38-
39-
std::string URL = fmt::format("https://raw.githubusercontent.com/OmgRod/Geodify/master/previews/{}Preview.png", layerName);
40-
41-
auto req = web::WebRequest();
42-
m_downloadListener.bind([this](web::WebTask::Event* e) {
43-
if (auto res = e->getValue()) {
44-
if (!res->ok()) {
45-
log::error("Failed to download image from URL.");
46-
onDownloadFail();
47-
} else {
48-
auto data = res->data();
49-
std::thread imageThread = std::thread([data, this]() {
50-
m_image = new CCImage();
51-
m_image->autorelease();
52-
m_image->initWithImageData(const_cast<uint8_t*>(data.data()), data.size());
53-
geode::Loader::get()->queueInMainThread([this]() {
54-
imageCreationFinished(m_image);
55-
});
56-
});
57-
imageThread.detach();
58-
}
59-
}
60-
});
61-
m_downloadListener.setFilter(req.get(URL));
62-
63-
return true;
64-
}
65-
66-
void imageCreationFinished(CCImage* image) {
67-
CCTexture2D* texture = new CCTexture2D();
68-
texture->autorelease();
69-
texture->initWithImage(image);
70-
71-
m_sprite = CCSprite::createWithTexture(texture);
72-
if (m_sprite) {
73-
auto maxWidth = m_mainLayer->getContentSize().width * 0.75f;
74-
auto maxHeight = m_mainLayer->getContentSize().height * 0.75f;
75-
76-
auto spriteWidth = m_sprite->getContentSize().width;
77-
auto spriteHeight = m_sprite->getContentSize().height;
78-
79-
float scale = std::min(maxWidth / spriteWidth, maxHeight / spriteHeight);
80-
m_sprite->setScale(scale);
81-
82-
m_mainLayer->addChildAtPosition(m_sprite, Anchor::Center);
83-
}
84-
}
85-
86-
void onDownloadFail() {
87-
CCSprite* fallbackSprite = CCSprite::create("noPreview.png"_spr);
88-
m_mainLayer->addChildAtPosition(fallbackSprite, Anchor::Center);
89-
}
18+
bool setup(int const& layer) override;
19+
void imageCreationFinished(CCImage* image);
20+
void onDownloadFail();
9021

9122
public:
92-
std::string extractLastSegment(const std::string& input) {
93-
size_t lastDash = input.rfind('-');
94-
if (lastDash != std::string::npos) {
95-
return input.substr(lastDash + 1);
96-
}
97-
return input;
98-
}
99-
100-
static GYScreenshotPopup* create(int const& text) {
101-
auto ret = new GYScreenshotPopup();
102-
auto winSize = CCDirector::sharedDirector()->getWinSize();
103-
if (ret->initAnchored(winSize.width * .6f, winSize.height * .7f, text)) {
104-
ret->autorelease();
105-
return ret;
106-
}
107-
108-
delete ret;
109-
return nullptr;
110-
}
23+
static GYScreenshotPopup* create(int const& text);
24+
std::string extractLastSegment(const std::string& input);
11125
};

0 commit comments

Comments
 (0)