Chesto 0.9
A declarative and element-based library for creating GUIs on homebrew'd consoles
NetImageElement.cpp
1#include "NetImageElement.hpp"
2
3namespace Chesto {
4
5NetImageElement::NetImageElement(const char *url, std::function<Texture *(void)> getImageFallback, bool immediateLoad)
6{
7 std::string key = std::string(url);
8 // printf("Key: %s\n", key.c_str());
9 if (loadFromCache(key)) {
10 loaded = true;
11
12 // if we're using the cache, we can update the size now
13 // printf("The size of the image is %d x %d\n", texW, texH);
14 width = texW;
15 height = texH;
16 }
17 else {
18 // setup a temporary image fallback
19 if (getImageFallback)
20 imgFallback = getImageFallback();
21
22 // start downloading the correct image
23 imgDownload = new DownloadOperation();
24 imgDownload->url = std::string(url);
25 imgDownload->cb = std::bind(&NetImageElement::imgDownloadComplete, this, std::placeholders::_1);
26
27 // load immediately
28 if (immediateLoad)
29 fetch();
30 }
31}
32
33NetImageElement::~NetImageElement()
34{
35 if (imgFallback)
36 delete imgFallback;
37
38 if (imgDownload)
39 {
40 DownloadQueue::downloadQueue->downloadCancel(imgDownload);
41 delete imgDownload;
42 }
43}
44
46{
47 if (!downloadStarted && imgDownload) {
48 downloadStarted = true; // guard against re-entry
49 DownloadQueue::downloadQueue->downloadAdd(imgDownload);
50 }
51}
52
54{
55 bool ret = Texture::process(event); // updates xAbs/yAbs
56
57 // fetch the image if we're visible onscreen
58 if (!downloadStarted && !loaded && imgDownload) {
59 CST_Rect rect = { this->xAbs, this->yAbs, this->width, this->height };
60 if (!CST_isRectOffscreen(&rect)) {
61 fetch();
62 }
63 }
64
65 return ret;
66}
67
68void NetImageElement::imgDownloadComplete(DownloadOperation *download)
69{
70 bool success = false;
71
72 if (download->status == DownloadStatus::COMPLETE)
73 {
74 CST_Surface *surface = IMG_Load_RW(SDL_RWFromMem((void*)download->buffer.c_str(), download->buffer.size()), 1);
75 success = loadFromSurfaceSaveToCache(download->url, surface);
76 CST_FreeSurface(surface);
77 }
78
79 if (success)
80 {
81 this->needsRedraw = true;
82 loaded = true;
83
84 delete imgFallback;
85 imgFallback = nullptr;
86
87 if (updateSizeAfterLoad) {
88 width = texW;
89 height = texH;
90 }
91 }
92
93 delete imgDownload;
94 imgDownload = nullptr;
95}
96
97
99{
100 // if we're hidden, don't render
101 if (hidden) return;
102
103 if (mTexture)
104 {
106 }
107 else if (imgFallback)
108 {
109 // we need to apply our constraints
110 Element::render(parent); // not texture's
111
112 imgFallback->x = x;
113 imgFallback->y = y;
114 imgFallback->width = width;
115 imgFallback->height = height;
116 imgFallback->render(parent);
117 }
118}
119
120} // namespace Chesto
int width
width and height of this element (must be manually set, isn't usually calculated (but is in some case...
Definition: Element.hpp:132
virtual void render(Element *parent)
display the current state of the display
Definition: Element.cpp:89
bool hidden
whether this element should skip rendering or not
Definition: Element.hpp:119
virtual bool process(InputEvents *event)
process any input that is received for this element
Definition: Element.cpp:29
bool needsRedraw
whether or not this element needs the screen redrawn next time it's processed
Definition: Element.hpp:95
Element * parent
the parent element (reference only, not owned)
Definition: Element.hpp:116
NetImageElement(const char *url, std::function< Texture *(void)> getImageFallback=NULL, bool immediateLoad=true)
Creates a new image element, downloading the image from url If the url is not cached,...
void render(Element *parent) override
Render the image.
bool process(InputEvents *event) override
Checks if we're onscreen and start download if needed.
void fetch()
Start downloading the image (called in the constructor unless immediateLoad is false)
bool loadFromSurfaceSaveToCache(std::string &key, CST_Surface *surface)
Loads the texture from a surface and saves the results in caches Returns true if successful.
Definition: Texture.cpp:96
int texW
The size of the texture.
Definition: Texture.hpp:93
bool loadFromCache(std::string &key)
Loads the texture from caches Returns true if successful.
Definition: Texture.cpp:81
void render(Element *parent)
Renders the texture.
Definition: Texture.cpp:131
CST_Texture * mTexture
The actual texture.
Definition: Texture.hpp:90