2013-06-02 15:08:32 +00:00
|
|
|
#ifndef _IMAGECOMPONENT_H_
|
|
|
|
#define _IMAGECOMPONENT_H_
|
2012-08-02 01:43:55 +00:00
|
|
|
|
2013-05-13 19:53:28 +00:00
|
|
|
#include "../platform.h"
|
|
|
|
#include GLHEADER
|
|
|
|
|
2013-06-02 15:08:32 +00:00
|
|
|
#include "../GuiComponent.h"
|
2012-08-02 01:43:55 +00:00
|
|
|
#include <string>
|
2013-06-21 16:49:29 +00:00
|
|
|
#include <memory>
|
|
|
|
#include "../resources/TextureResource.h"
|
2012-08-02 01:43:55 +00:00
|
|
|
|
2013-06-02 15:08:32 +00:00
|
|
|
class ImageComponent : public GuiComponent
|
2012-08-02 01:43:55 +00:00
|
|
|
{
|
|
|
|
public:
|
2014-01-24 22:21:10 +00:00
|
|
|
ImageComponent(Window* window);
|
2013-06-02 15:08:32 +00:00
|
|
|
virtual ~ImageComponent();
|
2012-08-02 01:43:55 +00:00
|
|
|
|
2014-01-24 22:21:10 +00:00
|
|
|
//Loads the image at the given filepath. Will tile if tile is true (retrieves texture as tiling, creates vertices accordingly).
|
|
|
|
void setImage(std::string path, bool tile = false);
|
|
|
|
//Loads an image from memory.
|
|
|
|
void setImage(const char* image, size_t length, bool tile = false);
|
|
|
|
//Use an already existing texture.
|
|
|
|
void setImage(const std::shared_ptr<TextureResource>& texture);
|
|
|
|
|
2014-04-12 01:48:13 +00:00
|
|
|
void onSizeChanged() override;
|
|
|
|
void setOpacity(unsigned char opacity) override;
|
|
|
|
|
2014-01-24 22:21:10 +00:00
|
|
|
//Sets the origin as a percentage of this image (e.g. (0, 0) is top left, (0.5, 0.5) is the center)
|
|
|
|
void setOrigin(float originX, float originY);
|
2013-12-30 23:23:34 +00:00
|
|
|
inline void setOrigin(Eigen::Vector2f origin) { setOrigin(origin.x(), origin.y()); }
|
2014-01-24 22:21:10 +00:00
|
|
|
|
|
|
|
// Resize the image to fit this size. If one axis is zero, scale that axis to maintain aspect ratio.
|
|
|
|
// If both are non-zero, potentially break the aspect ratio. If both are zero, no resizing.
|
|
|
|
// Can be set before or after an image is loaded.
|
|
|
|
// setMaxSize() and setResize() are mutually exclusive.
|
2014-01-10 23:45:47 +00:00
|
|
|
void setResize(float width, float height);
|
|
|
|
inline void setResize(const Eigen::Vector2f& size) { setResize(size.x(), size.y()); }
|
2014-01-24 22:21:10 +00:00
|
|
|
|
|
|
|
// Resize the image to be as large as possible but fit within a box of this size.
|
|
|
|
// Can be set before or after an image is loaded.
|
|
|
|
// Never breaks the aspect ratio. setMaxSize() and setResize() are mutually exclusive.
|
2014-01-10 23:45:47 +00:00
|
|
|
void setMaxSize(float width, float height);
|
|
|
|
inline void setMaxSize(const Eigen::Vector2f& size) { setMaxSize(size.x(), size.y()); }
|
2014-01-24 22:21:10 +00:00
|
|
|
|
|
|
|
// Multiply all pixels in the image by this color when rendering.
|
2013-08-07 22:40:27 +00:00
|
|
|
void setColorShift(unsigned int color);
|
2012-08-02 01:43:55 +00:00
|
|
|
|
2014-01-24 22:21:10 +00:00
|
|
|
void setFlipX(bool flip); // Mirror on the X axis.
|
|
|
|
void setFlipY(bool flip); // Mirror on the Y axis.
|
2012-10-07 22:25:51 +00:00
|
|
|
|
2014-01-24 22:21:10 +00:00
|
|
|
// Returns the size of the current texture, or (0, 0) if none is loaded. May be different than drawn size (use getSize() for that).
|
2013-07-10 11:29:43 +00:00
|
|
|
Eigen::Vector2i getTextureSize() const;
|
2013-06-02 22:33:49 +00:00
|
|
|
|
2014-01-24 22:21:10 +00:00
|
|
|
// Returns the center point of the image (takes origin into account).
|
2013-08-06 13:15:20 +00:00
|
|
|
Eigen::Vector2f getCenter() const;
|
|
|
|
|
2012-10-10 13:51:48 +00:00
|
|
|
bool hasImage();
|
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
void render(const Eigen::Affine3f& parentTrans) override;
|
2013-06-02 19:34:50 +00:00
|
|
|
|
2014-01-01 05:39:22 +00:00
|
|
|
virtual void applyTheme(const std::shared_ptr<ThemeData>& theme, const std::string& view, const std::string& element, unsigned int properties) override;
|
|
|
|
|
2012-08-02 01:43:55 +00:00
|
|
|
private:
|
2013-07-10 11:29:43 +00:00
|
|
|
Eigen::Vector2f mTargetSize;
|
|
|
|
Eigen::Vector2f mOrigin;
|
2013-06-02 21:05:29 +00:00
|
|
|
|
2014-01-19 18:23:01 +00:00
|
|
|
bool mFlipX, mFlipY, mTargetIsMax;
|
2012-08-09 21:19:07 +00:00
|
|
|
|
2014-01-24 22:21:10 +00:00
|
|
|
// Calculates the correct mSize from our resizing information (set by setResize/setMaxSize).
|
|
|
|
// Used internally whenever the resizing parameters or texture change.
|
2012-10-05 20:04:12 +00:00
|
|
|
void resize();
|
2014-01-24 22:21:10 +00:00
|
|
|
|
2014-04-12 01:48:13 +00:00
|
|
|
struct Vertex
|
|
|
|
{
|
|
|
|
Eigen::Vector2f pos;
|
|
|
|
Eigen::Vector2f tex;
|
|
|
|
} mVertices[6];
|
|
|
|
|
|
|
|
GLubyte mColors[6*4];
|
|
|
|
|
|
|
|
void updateVertices();
|
|
|
|
void updateColors();
|
2012-08-10 02:17:48 +00:00
|
|
|
|
2013-08-07 22:40:27 +00:00
|
|
|
unsigned int mColorShift;
|
|
|
|
|
2013-06-21 16:49:29 +00:00
|
|
|
std::shared_ptr<TextureResource> mTexture;
|
2012-08-02 01:43:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|