Tiled sprite improvements!
This commit is contained in:
		@@ -10,6 +10,7 @@
 | 
			
		||||
#include "scene/debug/SceneDebugLine.hpp"
 | 
			
		||||
#include "physics/ScenePhysicsManager.hpp"
 | 
			
		||||
#include "state/StateEvent.hpp"
 | 
			
		||||
#include "state/StateOwner.hpp"
 | 
			
		||||
 | 
			
		||||
namespace Dawn {
 | 
			
		||||
  class DawnGame;
 | 
			
		||||
@@ -25,7 +26,7 @@ namespace Dawn {
 | 
			
		||||
  template<class T>
 | 
			
		||||
  std::vector<T*> _sceneForwardGetComponents(SceneItem *item);
 | 
			
		||||
 | 
			
		||||
  class Scene {
 | 
			
		||||
  class Scene : public StateOwner {
 | 
			
		||||
    private:
 | 
			
		||||
      sceneitemid_t nextId;
 | 
			
		||||
      std::map<sceneitemid_t, SceneItem*> items;
 | 
			
		||||
 
 | 
			
		||||
@@ -12,7 +12,10 @@ TiledSprite::TiledSprite(SceneItem *item) :
 | 
			
		||||
  SceneItemComponent(item),
 | 
			
		||||
  tile(-1),
 | 
			
		||||
  tileset(nullptr),
 | 
			
		||||
  meshHost(nullptr)
 | 
			
		||||
  meshHost(nullptr),
 | 
			
		||||
  flip(TILED_SPRITE_FLIP_Y),
 | 
			
		||||
  sizeType(TILED_SPRITE_SIZE_TYPE_SCALE),
 | 
			
		||||
  size(1.0f)
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -33,11 +36,60 @@ void TiledSprite::onStart() {
 | 
			
		||||
  useEffect([&]{
 | 
			
		||||
    if(this->meshHost == nullptr || this->tileset == nullptr) return;
 | 
			
		||||
    auto tile = this->tileset->getTile(this->tile);
 | 
			
		||||
    this->meshHost->uv0 = tile.uv0;
 | 
			
		||||
    this->meshHost->uv1 = tile.uv1;
 | 
			
		||||
    this->meshHost->uv0 = glm::vec2(
 | 
			
		||||
      (((flag_t)this->flip) & TILED_SPRITE_FLIP_X) == 0 ? tile.uv0.x : tile.uv1.x,
 | 
			
		||||
      (((flag_t)this->flip) & TILED_SPRITE_FLIP_Y) == 0 ? tile.uv0.y : tile.uv1.y
 | 
			
		||||
    );
 | 
			
		||||
    this->meshHost->uv1 = glm::vec2(
 | 
			
		||||
      (((flag_t)this->flip) & TILED_SPRITE_FLIP_X) == 0 ? tile.uv1.x : tile.uv0.x,
 | 
			
		||||
      (((flag_t)this->flip) & TILED_SPRITE_FLIP_Y) == 0 ? tile.uv1.y : tile.uv0.y
 | 
			
		||||
    );
 | 
			
		||||
  }, {
 | 
			
		||||
    &this->tile,
 | 
			
		||||
    &this->meshHost,
 | 
			
		||||
    &this->tileset
 | 
			
		||||
    &this->tileset,
 | 
			
		||||
    &this->flip
 | 
			
		||||
  })();
 | 
			
		||||
 | 
			
		||||
  useEffect([&]{
 | 
			
		||||
    if(this->meshHost == nullptr || this->tileset == nullptr) return;
 | 
			
		||||
    auto tile = this->tileset->getTile(this->tile);
 | 
			
		||||
 | 
			
		||||
    glm::vec2 size;
 | 
			
		||||
 | 
			
		||||
    switch(this->sizeType) {
 | 
			
		||||
      case TILED_SPRITE_SIZE_TYPE_SCALE: {
 | 
			
		||||
        size = glm::vec2(
 | 
			
		||||
          this->tileset->getTileWidth(this->tile),
 | 
			
		||||
          this->tileset->getTileHeight(this->tile)
 | 
			
		||||
        ) * (float_t)this->size;
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      case TILED_SPRITE_SIZE_TYPE_WIDTH_RATIO: {
 | 
			
		||||
        float_t rw = this->tileset->getTileHeight(this->tile) / this->tileset->getTileWidth(this->tile);
 | 
			
		||||
        size.x = (float_t)this->size;
 | 
			
		||||
        size.y = size.x * rw;
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      case TILED_SPRITE_SIZE_TYPE_HEIGHT_RATIO: {
 | 
			
		||||
        float_t rh = this->tileset->getTileWidth(this->tile) / this->tileset->getTileHeight(this->tile);
 | 
			
		||||
        size.y = (float_t)this->size;
 | 
			
		||||
        size.x = size.y * rh;
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      default:
 | 
			
		||||
        assertUnreachable();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    this->meshHost->xy0 = -size;
 | 
			
		||||
    this->meshHost->xy1 = size;
 | 
			
		||||
  }, {
 | 
			
		||||
    &this->tile,
 | 
			
		||||
    &this->meshHost,
 | 
			
		||||
    &this->tileset,
 | 
			
		||||
    &this->size
 | 
			
		||||
  })();
 | 
			
		||||
}
 | 
			
		||||
@@ -7,7 +7,16 @@
 | 
			
		||||
#include "display/Tileset.hpp"
 | 
			
		||||
#include "scene/components/display/mesh/QuadMeshHost.hpp"
 | 
			
		||||
 | 
			
		||||
#define TILED_SPRITE_FLIP_Y FLAG_DEFINE(1)
 | 
			
		||||
#define TILED_SPRITE_FLIP_X FLAG_DEFINE(2)
 | 
			
		||||
 | 
			
		||||
namespace Dawn {
 | 
			
		||||
  enum TiledSpriteSizeType {
 | 
			
		||||
    TILED_SPRITE_SIZE_TYPE_SCALE,
 | 
			
		||||
    TILED_SPRITE_SIZE_TYPE_WIDTH_RATIO,
 | 
			
		||||
    TILED_SPRITE_SIZE_TYPE_HEIGHT_RATIO
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  class TiledSprite : public SceneItemComponent {
 | 
			
		||||
    public:
 | 
			
		||||
      // @optional
 | 
			
		||||
@@ -16,6 +25,12 @@ namespace Dawn {
 | 
			
		||||
      StateProperty<QuadMeshHost*> meshHost;
 | 
			
		||||
      // @optional
 | 
			
		||||
      StateProperty<int32_t> tile;
 | 
			
		||||
      // @optional
 | 
			
		||||
      StateProperty<flag_t> flip;
 | 
			
		||||
      // @optional
 | 
			
		||||
      StateProperty<enum TiledSpriteSizeType> sizeType;
 | 
			
		||||
      // @optional
 | 
			
		||||
      StateProperty<float_t> size;
 | 
			
		||||
 | 
			
		||||
      TiledSprite(SceneItem *item);
 | 
			
		||||
      std::vector<SceneItemComponent*> getDependencies();
 | 
			
		||||
 
 | 
			
		||||
@@ -42,8 +42,6 @@ namespace Dawn {
 | 
			
		||||
    protected:
 | 
			
		||||
      float_t width = 1;
 | 
			
		||||
      float_t height = 1;
 | 
			
		||||
 | 
			
		||||
      StateEvent<> eventAlignmentUpdated;
 | 
			
		||||
      
 | 
			
		||||
      /**
 | 
			
		||||
       * Simply returns this UI Components' dimensional parent, used for the
 | 
			
		||||
@@ -60,6 +58,7 @@ namespace Dawn {
 | 
			
		||||
      
 | 
			
		||||
    public:
 | 
			
		||||
      StateProperty<bool_t> alignmentNeedsUpdating;
 | 
			
		||||
      StateEvent<> eventAlignmentUpdated;
 | 
			
		||||
 | 
			
		||||
      /**
 | 
			
		||||
       * Method used to calculate alignment values.
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user