Chesto 0.9
A declarative and element-based library for creating GUIs on homebrew'd consoles
Public Member Functions | Public Attributes | List of all members
Chesto::NetImageElement Class Reference
Inheritance diagram for Chesto::NetImageElement:
Inheritance graph
[legend]
Collaboration diagram for Chesto::NetImageElement:
Collaboration graph
[legend]

Public Member Functions

 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, getImageFallback will be called to get a Texture to be shown while downloading the correct image; the provided Texture is free'd when the download is complete or the destructor is called If immediateLoad is set to false, the loading won't begin until load() is called. More...
 
void fetch ()
 Start downloading the image (called in the constructor unless immediateLoad is false) More...
 
bool process (InputEvents *event) override
 Checks if we're onscreen and start download if needed. More...
 
void render (Element *parent) override
 Render the image. More...
 
- Public Member Functions inherited from Chesto::Texture
void clear (void)
 Reinitialize Texture Resets texture content, size and color. More...
 
bool loadFromSurface (CST_Surface *surface)
 Loads the texture from a surface Returns true if successful. More...
 
bool loadFromCache (std::string &key)
 Loads the texture from caches Returns true if successful. More...
 
bool loadFromSurfaceSaveToCache (std::string &key, CST_Surface *surface)
 Loads the texture from a surface and saves the results in caches Returns true if successful. More...
 
void render (Element *parent)
 Renders the texture. More...
 
void resize (int w, int h)
 Resizes the texture. More...
 
void setScaleMode (TextureScaleMode mode)
 Sets texture scaling mode. More...
 
void getTextureSize (int *w, int *h)
 Return texture's original size. More...
 
TexturesetSize (int w, int h)
 
bool saveTo (std::string &path)
 save this texture to the given file path as a PNG More...
 
void loadPath (std::string &path, bool forceReload=false)
 update and load or reload the texture More...
 
- Public Member Functions inherited from Chesto::Element
virtual bool process (InputEvents *event)
 process any input that is received for this element More...
 
virtual void render (Element *parent)
 display the current state of the display More...
 
bool onTouchDown (InputEvents *event)
 
bool onTouchDrag (InputEvents *event)
 
bool onTouchUp (InputEvents *event)
 
void hide ()
 
void unhide ()
 
void renderBackground (bool fill=true)
 
void addNode (std::unique_ptr< Element > node)
 
void remove (Element *element)
 
void removeAll ()
 
void position (int x, int y)
 position the element More...
 
void recalcPosition (Element *parent)
 
float getEffectiveScale () const
 
CST_Rect getBounds ()
 
CST_Renderer * getRenderer ()
 
ElementsetPosition (int x, int y)
 
ElementsetAction (std::function< void()> func)
 
ElementsetAbsolute (bool isAbs)
 
template<typename T , typename... Args>
T * createNode (Args &&... args)
 
Elementconstrain (int flags, int padding=0)
 
ElementconstrainToTarget (Element *target, int flags, int padding=0)
 
Elementanimate (int durationIn, std::function< void(float)> onStep, std::function< void()> onFinish)
 
ElementmoveToFront ()
 
ElementsetTouchable (bool touchable)
 
void screenshot (std::string path)
 Take a screenshot of this element and its children, and save it to the given path. More...
 

Public Attributes

bool loaded = false
 
bool updateSizeAfterLoad = false
 
- Public Attributes inherited from Chesto::Texture
int cornerRadius = 0
 Rounded corner radius (if >0, will round) More...
 
SDL_BlendMode blendMode = SDL_BLENDMODE_BLEND
 Blend mode to use for this texture. More...
 
- Public Attributes inherited from Chesto::Element
std::function< void()> action = NULL
 the action to call (from binded callback) on touch or button selection https://stackoverflow.com/questions/14189440/c-class-member-callback-simple-examples More...
 
std::function< void(InputEvents *event)> actionWithEvents = NULL
 
std::vector< std::unique_ptr< Element, std::function< void(Element *)> > > elements
 visible GUI child elements of this element More...
 
float scale = 1.0f
 
bool touchable = false
 whether or not this element can be touched (highlights bounds) More...
 
bool dragging = false
 whether or not this element is currently being dragged More...
 
bool needsRedraw = false
 whether or not this element needs the screen redrawn next time it's processed More...
 
int futureRedrawCounter = 0
 whether this element needs a redraw for the next X redraws (decreases each time) (0 is no redraws) More...
 
int lastMouseY = 0
 the last Y, X coordinate of the mouse (from a drag probably) More...
 
int lastMouseX = 0
 
bool hasBackground = false
 
rgb backgroundColor = {0, 0, 0}
 
int backgroundOpacity = 0xff
 
bool isAbsolute = false
 
Elementparent = nullptr
 the parent element (reference only, not owned) More...
 
bool hidden = false
 whether this element should skip rendering or not More...
 
bool isProtected = false
 
int elasticCounter = 0
 how much time is left in an elastic-type flick/scroll set by the last distance traveled in a scroll, and counts down every frame More...
 
int width = 0
 width and height of this element (must be manually set, isn't usually calculated (but is in some cases, like text or images)) More...
 
int height = 0
 
int x = 0
 
int y = 0
 
int xAbs = 0
 
int yAbs = 0
 
double angle = 0
 rotation angle in degrees More...
 
int cornerRadius = 0
 
int tag = 0
 
std::vector< std::unique_ptr< Constraint > > constraints
 
std::vector< std::unique_ptr< Animation > > animations
 
bool useColorMask = false
 whether or not to overlay a color mask on top of this element More...
 
CST_Color maskColor = {0,0,0,0}
 The color to overlay on top. More...
 

Additional Inherited Members

- Public Types inherited from Chesto::Element
typedef Element super
 
- Static Public Member Functions inherited from Chesto::Texture
static void wipeEntireCache ()
 Wipes the entire texture cache (generally, should only be used to reload entire theme / text) More...
 
static void wipeTextCache ()
 Similar to wipeEntireCache, but only wipes cached text textures (made by TextElement) More...
 
- Static Public Attributes inherited from Chesto::Texture
static const std::string textElemPrefix = "(TextElement):"
 
- Protected Member Functions inherited from Chesto::Element
void addStackMember (Element *element)
 
- Protected Attributes inherited from Chesto::Texture
CST_Texture * mTexture = nullptr
 The actual texture. More...
 
int texW = 0
 The size of the texture. More...
 
int texH = 0
 
CST_Color texFirstPixel = {0,0,0,0}
 The color of the first pixel. More...
 
TextureScaleMode texScaleMode = SCALE_STRETCH
 Texture's scaling mode. More...
 
- Static Protected Attributes inherited from Chesto::Texture
static std::unordered_map< std::string, TextureDatatexCache
 Cache previously displayed textures. More...
 

Detailed Description

Definition at line 10 of file NetImageElement.hpp.

Constructor & Destructor Documentation

◆ NetImageElement()

Chesto::NetImageElement::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, getImageFallback will be called to get a Texture to be shown while downloading the correct image; the provided Texture is free'd when the download is complete or the destructor is called If immediateLoad is set to false, the loading won't begin until load() is called.

Definition at line 5 of file NetImageElement.cpp.

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}
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
void fetch()
Start downloading the image (called in the constructor unless immediateLoad is false)
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

References fetch(), Chesto::Texture::loadFromCache(), Chesto::Texture::texW, and Chesto::Element::width.

◆ ~NetImageElement()

Chesto::NetImageElement::~NetImageElement ( )

Definition at line 33 of file NetImageElement.cpp.

34{
35 if (imgFallback)
36 delete imgFallback;
37
38 if (imgDownload)
39 {
40 DownloadQueue::downloadQueue->downloadCancel(imgDownload);
41 delete imgDownload;
42 }
43}

Member Function Documentation

◆ fetch()

void Chesto::NetImageElement::fetch ( )

Start downloading the image (called in the constructor unless immediateLoad is false)

Definition at line 45 of file NetImageElement.cpp.

46{
47 if (!downloadStarted && imgDownload) {
48 downloadStarted = true; // guard against re-entry
49 DownloadQueue::downloadQueue->downloadAdd(imgDownload);
50 }
51}

Referenced by NetImageElement(), and process().

◆ process()

bool Chesto::NetImageElement::process ( InputEvents event)
overridevirtual

Checks if we're onscreen and start download if needed.

Reimplemented from Chesto::Element.

Definition at line 53 of file NetImageElement.cpp.

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}
virtual bool process(InputEvents *event)
process any input that is received for this element
Definition: Element.cpp:29

References fetch(), Chesto::Element::process(), and Chesto::Element::width.

◆ render()

void Chesto::NetImageElement::render ( Element parent)
overridevirtual

Render the image.

Reimplemented from Chesto::Element.

Definition at line 98 of file NetImageElement.cpp.

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}
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
Element * parent
the parent element (reference only, not owned)
Definition: Element.hpp:116
void render(Element *parent)
Renders the texture.
Definition: Texture.cpp:131
CST_Texture * mTexture
The actual texture.
Definition: Texture.hpp:90

References Chesto::Element::hidden, Chesto::Texture::mTexture, Chesto::Element::parent, Chesto::Element::render(), Chesto::Texture::render(), and Chesto::Element::width.

Member Data Documentation

◆ loaded

bool Chesto::NetImageElement::loaded = false

Definition at line 32 of file NetImageElement.hpp.

◆ updateSizeAfterLoad

bool Chesto::NetImageElement::updateSizeAfterLoad = false

Definition at line 33 of file NetImageElement.hpp.


The documentation for this class was generated from the following files: