#pragma once #include "copium/example/ComponentHandler.h" #include "copium/example/Components.h" #include namespace Copium { class NameComponentHandler : public ComponentHandler { public: NameComponentHandler() : ComponentHandler{"Name", false} {} void Deserialize(Entity entity, const MetaFileClass& metaClass) const override { NameC name; name.name = metaClass.GetValue("name"); entity.AddComponent(name); } protected: void Gui(NameC& name) const override { ImGui::InputText("Name##Name", &name.name); } NameC Create(Entity entity) const override { return NameC{String::Format("Entity %d", entity.GetId())}; } }; class TransformComponentHandler : public ComponentHandler { public: TransformComponentHandler() : ComponentHandler{"Transform", false} {} void Deserialize(Entity entity, const MetaFileClass& metaClass) const override { TransformC transform; transform.position = ReadVec2Opt(metaClass, "position", glm::vec2{0.0f, 0.0f}); transform.size = ReadVec2Opt(metaClass, "size", glm::vec2{1.0f, 1.0f}); entity.AddComponent(transform); } protected: void Gui(TransformC& transform) const override { ImGui::DragFloat2("Position", (float*)&transform.position); ImGui::DragFloat2("Size", (float*)&transform.size); } TransformC Create(Entity entity) const override { return TransformC{glm::vec2{0, 0}, glm::vec2{1, 1}}; } }; class TextureComponentHandler : public ComponentHandler { public: TextureComponentHandler() : ComponentHandler{"Texture", false} {} void Deserialize(Entity entity, const MetaFileClass& metaClass) const override { TextureC texture; texture.asset = AssetRef{AssetManager::LoadAsset(Uuid{metaClass.GetValue("texture-uuid")})}; texture.texCoord1 = ReadVec2Opt(metaClass, "tex-coord", glm::vec2{0.0f, 0.0f}); texture.texCoord2 = texture.texCoord1 + ReadVec2Opt(metaClass, "tex-size", glm::vec2{1.0f, 1.0f} - texture.texCoord1); entity.AddComponent(texture); } protected: void Gui(TextureC& texture) const override { Asset& asset = AssetManager::GetAsset(texture.asset); ImGui::Text("Asset: %s", asset.GetName().c_str()); ImGui::DragFloat2("Tex Coord 1", (float*)&texture.texCoord1, 0.01, 0.0f, 1.0f); ImGui::DragFloat2("Tex Coord 2", (float*)&texture.texCoord2, 0.01, 0.0f, 1.0f); } TextureC Create(Entity entity) const override { return TextureC{AssetRef{AssetManager::DuplicateAsset(Vulkan::GetEmptyTexture2D())}, glm::vec2{0, 0}, glm::vec2{1, 1}}; } }; class TextComponentHandler : public ComponentHandler { public: TextComponentHandler() : ComponentHandler{"Text", false} {} void Deserialize(Entity entity, const MetaFileClass& metaClass) const override { char* endPtr; TextC text; text.font = AssetRef{AssetManager::LoadAsset(Uuid{metaClass.GetValue("font")})}; text.text = metaClass.GetValue("text"); text.fontSize = std::strtof(metaClass.GetValue("font-size").c_str(), &endPtr); entity.AddComponent(text); } protected: void Gui(TextC& text) const override { Asset& asset = AssetManager::GetAsset(text.font); ImGui::Text(asset.GetName().c_str()); ImGui::InputTextMultiline("Text##Text", &text.text); ImGui::DragFloat("Font Size", &text.fontSize); } TextC Create(Entity entity) const override { return TextC{AssetRef{AssetManager::LoadAsset("font.meta")}, "", 20.0f}; } }; class StaticColliderComponentHandler : public ComponentHandler { public: StaticColliderComponentHandler() : ComponentHandler{"Static Collider", false} {} void Deserialize(Entity entity, const MetaFileClass& metaClass) const override { StaticColliderC staticCollider; staticCollider.resolveCollision = ReadBoolOpt(metaClass, "resolve-collision", true); entity.AddComponent(staticCollider); } protected: void Gui(StaticColliderC& staticCollider) const override { ImGui::Checkbox("Resolve Collision##StaticCollider", &staticCollider.resolveCollision); } StaticColliderC Create(Entity entity) const override { return StaticColliderC{false}; } }; class DynamicColliderComponentHandler : public ComponentHandler { public: DynamicColliderComponentHandler() : ComponentHandler{"Dynamic Collider", false} {} void Deserialize(Entity entity, const MetaFileClass& metaClass) const override { DynamicColliderC dynamicCollider; dynamicCollider.resolveCollision = ReadBoolOpt(metaClass, "resolve-collision", true); dynamicCollider.colliderOffset = ReadVec2Opt(metaClass, "collider-offset", glm::vec2{0.0f, 0.0f}); dynamicCollider.colliderSize = ReadVec2Opt(metaClass, "collider-size", glm::vec2{1.0f, 1.0f}); entity.AddComponent(dynamicCollider); } protected: void Gui(DynamicColliderC& dynamicCollider) const override { ImGui::Checkbox("Resolve Collision##DynamicCollider", &dynamicCollider.resolveCollision); ImGui::DragFloat2("Collider Offset", (float*)&dynamicCollider.colliderOffset, 0.01); ImGui::DragFloat2("Collider Size", (float*)&dynamicCollider.colliderSize, 0.01); } DynamicColliderC Create(Entity entity) const override { return DynamicColliderC{true, glm::vec2{0, 0}, glm::vec2{1, 1}}; } }; class PlayerComponentHandler : public ComponentHandler { public: PlayerComponentHandler() : ComponentHandler{"Player", false} {} void Deserialize(Entity entity, const MetaFileClass& metaClass) const override { PlayerC player; player.camera = GetEntity(entity.GetManager(), Uuid{metaClass.GetValue("camera-uuid")}); entity.AddComponent(player); } protected: void Gui(PlayerC& player) const override { EntityGui("Camera", &player.camera); } PlayerC Create(Entity entity) const override { return PlayerC{Entity{entity.GetManager()}}; } }; class CameraComponentHandler : public ComponentHandler { private: BoundingBox* viewport; public: CameraComponentHandler(BoundingBox* viewport) : ComponentHandler{"Camera", false}, viewport{viewport} {} void Deserialize(Entity entity, const MetaFileClass& metaClass) const override { float aspect = viewport->GetSize().x / viewport->GetSize().y; CameraC camera; camera.staticBoundingBox = ReadBoolOpt(metaClass, "static-bounding-box", false); camera.uiCamera = ReadBoolOpt(metaClass, "ui-camera", false); if (camera.uiCamera) camera.projection = BoundingBox(0, 0, Vulkan::GetSwapChain().GetExtent().width, Vulkan::GetSwapChain().GetExtent().height); else camera.projection = BoundingBox(-aspect, -1.0f, aspect, 1.0f); entity.AddComponent(camera); } protected: void Gui(CameraC& camera) const override { ImGui::Checkbox("Static", &camera.staticBoundingBox); ImGui::Checkbox("Ui camera", &camera.uiCamera); // TODO: If this changes, the bounding box should be modified if it is not static } CameraC Create(Entity entity) const override { return CameraC{BoundingBox{-1.0f, -1.0f, 1.0f, 1.0f}, false, false}; } }; class UuidComponentHandler : public ComponentHandler { public: UuidComponentHandler() : ComponentHandler{"Uuid", false} {} void Deserialize(Entity entity, const MetaFileClass& metaClass) const override { UuidC uuid; uuid.uuid = Uuid{metaClass.GetValue("uuid")}; entity.AddComponent(uuid); } protected: void ComponentGui(Entity entity) const override {} }; class HealthComponentHandler : public ComponentHandler { public: HealthComponentHandler() : ComponentHandler{"Health", false} {} void Deserialize(Entity entity, const MetaFileClass& metaClass) const override { char* endPtr; HealthC health; health.max = std::strtol(metaClass.GetValue("health").c_str(), &endPtr, 10); health.current = health.max; entity.AddComponent(health); } protected: void Gui(HealthC& health) const override { ImGui::DragInt("Max Health", &health.max); } HealthC Create(Entity entity) const override { return HealthC{10, 10}; } }; class PhysicsComponentHandler : public ComponentHandler { public: PhysicsComponentHandler() : ComponentHandler{"Physics", false} {} void Deserialize(Entity entity, const MetaFileClass& metaClass) const override { char* endPtr; PhysicsC physics; physics.mass = std::strtof(metaClass.GetValue("mass").c_str(), &endPtr); entity.AddComponent(physics); } protected: void Gui(PhysicsC& physics) const override { ImGui::DragFloat("Mass", &physics.mass); } PhysicsC Create(Entity entity) const override { return PhysicsC{10.0f}; } }; class AnimationComponentHandler : public ComponentHandler { public: AnimationComponentHandler() : ComponentHandler{"Animation", false} {} void Deserialize(Entity entity, const MetaFileClass& metaClass) const override { char* endPtr; AnimationC animation; animation.sheetCoord = ReadVec2Opt(metaClass, "sheet-coord", glm::ivec2{0, 0}); animation.sheetSize = ReadVec2Opt(metaClass, "sheet-size", glm::ivec2{1, 1}); animation.images = std::strtol(metaClass.GetValue("images").c_str(), &endPtr, 10); animation.horizontal = ReadBoolOpt(metaClass, "horizontal", true); animation.time = std::strtof(metaClass.GetValue("time").c_str(), &endPtr); entity.AddComponent(animation); } protected: void Gui(AnimationC& animation) const override { ImGui::DragInt2("Sheet Size", (int*)&animation.sheetSize); ImGui::DragInt2("Sheet Coord", (int*)&animation.sheetCoord, 1, 0, std::max(animation.sheetSize.x, animation.sheetSize.y)); ImGui::DragInt("Images", &animation.images); ImGui::DragFloat("Frame time", &animation.time); ImGui::Checkbox("Horizontal", &animation.horizontal); } AnimationC Create(Entity entity) const override { return AnimationC{glm::ivec2{0, 0}, glm::ivec2{1, 1}, 1, true, 1.0f}; } }; class DebugComponentHandler : public ComponentHandler { public: DebugComponentHandler() : ComponentHandler{"Debug", false} {} void Deserialize(Entity entity, const MetaFileClass& metaClass) const override { DebugC debug; debug.playerEntity = GetEntity(entity.GetManager(), Uuid{metaClass.GetValue("player-uuid")}); entity.AddComponent(debug); } protected: void Gui(DebugC& debug) const override { EntityGui("Player", &debug.playerEntity); } DebugC Create(Entity entity) const override { return DebugC{Entity{entity.GetManager()}}; } }; }