mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-04 18:41:51 +00:00
cleanup Image element & add fallback
This commit is contained in:
parent
3c900460e6
commit
18e7fb55a6
2 changed files with 40 additions and 33 deletions
|
|
@ -3,6 +3,7 @@
|
|||
#include <utility>
|
||||
|
||||
#include "assets/Assets.hpp"
|
||||
#include "assets/assets_util.hpp"
|
||||
#include "graphics/core/Atlas.hpp"
|
||||
#include "graphics/core/Batch2D.hpp"
|
||||
#include "graphics/core/DrawContext.hpp"
|
||||
|
|
@ -16,37 +17,25 @@ Image::Image(GUI& gui, std::string texture, glm::vec2 size)
|
|||
setInteractive(false);
|
||||
}
|
||||
|
||||
util::TextureRegion Image::refreshTexture(const Assets& assets) {
|
||||
auto region = util::get_texture_region(assets, texture, fallback);
|
||||
if (region.texture && autoresize) {
|
||||
setSize(glm::vec2(
|
||||
region.texture->getWidth() * region.region.getWidth(),
|
||||
region.texture->getHeight() * region.region.getHeight()
|
||||
));
|
||||
}
|
||||
return region;
|
||||
}
|
||||
|
||||
void Image::draw(const DrawContext& pctx, const Assets& assets) {
|
||||
glm::vec2 pos = calcPos();
|
||||
auto batch = pctx.getBatch2D();
|
||||
|
||||
Texture* texture = nullptr;
|
||||
auto separator = this->texture.find(':');
|
||||
if (separator == std::string::npos) {
|
||||
texture = assets.get<Texture>(this->texture);
|
||||
batch->texture(texture);
|
||||
if (texture && autoresize) {
|
||||
setSize(glm::vec2(texture->getWidth(), texture->getHeight()));
|
||||
}
|
||||
} else {
|
||||
auto atlasName = this->texture.substr(0, separator);
|
||||
if (auto atlas = assets.get<Atlas>(atlasName)) {
|
||||
if (auto region =
|
||||
atlas->getIf(this->texture.substr(separator + 1))) {
|
||||
texture = atlas->getTexture();
|
||||
batch->texture(atlas->getTexture());
|
||||
batch->setRegion(*region);
|
||||
if (autoresize) {
|
||||
setSize(glm::vec2(
|
||||
texture->getWidth() * region->getWidth(),
|
||||
texture->getHeight() * region->getHeight()
|
||||
));
|
||||
}
|
||||
} else {
|
||||
batch->texture(nullptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
auto textureRegion = refreshTexture(assets);
|
||||
|
||||
batch->setRegion(textureRegion.region);
|
||||
batch->texture(textureRegion.texture);
|
||||
batch->rect(
|
||||
pos.x,
|
||||
pos.y,
|
||||
|
|
@ -73,10 +62,18 @@ const std::string& Image::getTexture() const {
|
|||
return texture;
|
||||
}
|
||||
|
||||
const std::string& Image::getFallback() const {
|
||||
return fallback;
|
||||
}
|
||||
|
||||
void Image::setTexture(const std::string& name) {
|
||||
texture = name;
|
||||
}
|
||||
|
||||
void Image::setFallback(const std::string& name) {
|
||||
fallback = name;
|
||||
}
|
||||
|
||||
void Image::setRegion(const UVRegion& region) {
|
||||
this->region = region;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,21 +3,31 @@
|
|||
#include "UINode.hpp"
|
||||
#include "maths/UVRegion.hpp"
|
||||
|
||||
namespace util {
|
||||
struct TextureRegion;
|
||||
}
|
||||
|
||||
namespace gui {
|
||||
class Image : public UINode {
|
||||
class Image final : public UINode {
|
||||
protected:
|
||||
std::string texture;
|
||||
std::string fallback;
|
||||
UVRegion region {};
|
||||
bool autoresize = false;
|
||||
bool isFallback = false;
|
||||
|
||||
util::TextureRegion refreshTexture(const Assets& assets);
|
||||
public:
|
||||
Image(GUI& gui, std::string texture, glm::vec2 size=glm::vec2(32,32));
|
||||
|
||||
virtual void draw(const DrawContext& pctx, const Assets& assets) override;
|
||||
void draw(const DrawContext& pctx, const Assets& assets) override;
|
||||
|
||||
virtual void setAutoResize(bool flag);
|
||||
virtual bool isAutoResize() const;
|
||||
virtual const std::string& getTexture() const;
|
||||
virtual void setTexture(const std::string& name);
|
||||
void setAutoResize(bool flag);
|
||||
bool isAutoResize() const;
|
||||
const std::string& getTexture() const;
|
||||
const std::string& getFallback() const;
|
||||
void setTexture(const std::string& name);
|
||||
void setFallback(const std::string& name);
|
||||
void setRegion(const UVRegion& region);
|
||||
const UVRegion& getRegion() const;
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue