trmnl_sdl/Widgets/WidgetImage.h

60 lines
1.5 KiB
C++

#ifndef WIDGET_IMAGE_H_
#define WIDGET_IMAGE_H_
#include "Widget.h"
#include <string>
#include "../sdl_helpers.h"
// Renders an image with optional scaling
class WidgetImage : public Widget
{
protected:
// Image filepath
std::string m_filename;
// Image surface
SDL_Surface* m_image_surface;
// Whether to resize the image and how
// RESIZE_NONE - Do not resize
// RESIZE_FIT - Scale the image to fit the rectangle
// RESIZE_STRETCH - Scale and stretch the image to fit the box perfectly
ImageResize m_resize_type;
// Default - center
HorizontalAlign m_halign;
// Default - center
VerticalAlign m_valign;
// Background color to be used
// Default - transparent white
SDL_Color m_bg_color;
public:
WidgetImage(int x, int y, int width, int height, std::string filename,
ImageResize resize_type = RESIZE_FIT,
HorizontalAlign halign = HALIGN_CENTER,
VerticalAlign valign = VALIGN_CENTER,
SDL_Color bg_color = SDL_Color{.r = 255, .g = 255, .b = 255, .a = SDL_ALPHA_TRANSPARENT});
void set_filename(const std::string& filename);
void set_resize(ImageResize type);
void set_halign(HorizontalAlign halign);
void set_valign(VerticalAlign valign);
void set_bg_color(SDL_Color bg_color);
virtual void draw() override;
static std::unique_ptr<Widget> builder(const nlohmann::json& j);
protected:
// Create a new surface of the image_surface that is scaled via RESIZE_FIT method
// NOTE: The user MUST free the surface when done with it
SDL_Surface* image_scale_fit();
};
#endif // WIDGET_IMAGE_H_