Minor - Enhances ECS component functionality - V13.5.0
Adds serialization/deserialization to components, allowing saving and loading of entity states. Provides ImGui widgets for editing component properties, improving editor usability. Passes the D3D device and context to entities and components enabling resource creation within components.
This commit is contained in:
@@ -14,17 +14,7 @@
|
||||
extern std::map<std::string, std::shared_ptr<model_class>> g_model_cache;
|
||||
|
||||
namespace ecs {
|
||||
/**
|
||||
* Enum for different types of textures used in rendering.
|
||||
*/
|
||||
enum class TextureType
|
||||
{
|
||||
Diffuse,
|
||||
Normal,
|
||||
Specular,
|
||||
Alpha,
|
||||
Reflection
|
||||
};
|
||||
|
||||
|
||||
class RenderComponent : public Component {
|
||||
public:
|
||||
@@ -34,7 +24,18 @@ public:
|
||||
RenderComponent() : m_model(nullptr), m_isVisible(true) {}
|
||||
~RenderComponent() = default;
|
||||
|
||||
void Initialize() override {}
|
||||
|
||||
/**
|
||||
* Initialize the component by setting up the Direct3D device and context from the parent entity.
|
||||
* This method is called when the component is added to an entity.
|
||||
*/
|
||||
void Initialize() override
|
||||
{
|
||||
if (auto parent = GetParent()) {
|
||||
SetDevice(parent->GetDevice());
|
||||
SetContext(parent->GetContext());
|
||||
}
|
||||
}
|
||||
void Update(float deltaTime) override {}
|
||||
|
||||
/**
|
||||
@@ -186,6 +187,11 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialize the RenderComponent's state to a string.
|
||||
* This method is useful for saving the component's state or debugging.
|
||||
* @return A string representation of the RenderComponent's state.
|
||||
*/
|
||||
std::string Serialize() const override {
|
||||
if (!m_model) return "RenderComponent:NoModel";
|
||||
|
||||
@@ -194,6 +200,13 @@ public:
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserialize the RenderComponent's state from a string.
|
||||
* This method parses the string and sets the component's properties accordingly.
|
||||
* Note: The model will be loaded separately based on the model file path.
|
||||
* @param data The string representation of the RenderComponent's state.
|
||||
* @return True if deserialization was successful, otherwise false.
|
||||
*/
|
||||
bool Deserialize(const std::string& data) override {
|
||||
std::stringstream ss(data);
|
||||
std::string type;
|
||||
@@ -208,6 +221,11 @@ public:
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the ImGui interface for the RenderComponent.
|
||||
* This method provides a user interface for inspecting and modifying the component's properties.
|
||||
* It includes options to toggle visibility, display model information, and manage textures.
|
||||
*/
|
||||
void OnImGuiRender() override {
|
||||
ImGui::Checkbox("Visible", &m_isVisible);
|
||||
ImGui::Text("Model File Path: %s", m_modelFilePath.c_str());
|
||||
@@ -216,12 +234,128 @@ public:
|
||||
} else {
|
||||
ImGui::Text("No model loaded.");
|
||||
}
|
||||
|
||||
ImGui::Separator();
|
||||
|
||||
// D<>finir les types de textures_
|
||||
std::vector<std::string> textureCategories = {
|
||||
"Diffuse", "Normal", "Specular", "Alpha"
|
||||
};
|
||||
|
||||
std::vector<TextureType> textureTypes = {
|
||||
TextureType::Diffuse, TextureType::Normal,
|
||||
TextureType::Specular, TextureType::Alpha
|
||||
};
|
||||
|
||||
// Cr<43>er un espace pour afficher les textures_ avec d<>filement
|
||||
ImGui::BeginChild("TextureChild", ImVec2(0, 200), true, ImGuiWindowFlags_HorizontalScrollbar);
|
||||
|
||||
// Pour chaque type de texture
|
||||
for (int typeIndex = 0; typeIndex < textureCategories.size(); typeIndex++)
|
||||
{
|
||||
TextureType type = textureTypes[typeIndex];
|
||||
std::string typeName = textureCategories[typeIndex];
|
||||
|
||||
// Afficher le titre de la cat<61>gorie
|
||||
ImGui::Text("%s:", typeName.c_str());
|
||||
ImGui::SameLine();
|
||||
|
||||
// Compter combien de textures_ de ce type existent
|
||||
int textureCount = 0;
|
||||
while (GetTexture(type, textureCount) != nullptr)
|
||||
{
|
||||
textureCount++;
|
||||
}
|
||||
|
||||
// Afficher toutes les textures_ existantes
|
||||
ImGui::BeginGroup();
|
||||
for (int texIndex = 0; texIndex < textureCount; texIndex++)
|
||||
{
|
||||
ID3D11ShaderResourceView* texture = GetTexture(type, texIndex);
|
||||
if (texture)
|
||||
{
|
||||
// ID unique pour chaque bouton de texture
|
||||
std::string buttonId = "tex##" +
|
||||
std::to_string(typeIndex) + "_" +
|
||||
std::to_string(texIndex);
|
||||
|
||||
if (ImGui::ImageButton(buttonId.c_str(), (ImTextureID)texture, ImVec2(48, 48)))
|
||||
{
|
||||
// Ouvrir une bo<62>te de dialogue pour changer la texture
|
||||
OPENFILENAME ofn;
|
||||
WCHAR szFile[260];
|
||||
ZeroMemory(&ofn, sizeof(ofn));
|
||||
ofn.lStructSize = sizeof(ofn);
|
||||
ofn.hwndOwner = NULL;
|
||||
ofn.lpstrFile = szFile;
|
||||
szFile[0] = '\0';
|
||||
ofn.nMaxFile = sizeof(szFile);
|
||||
ofn.lpstrFilter = L"Texture\0*.png;*.jpg;*.dds\0";
|
||||
ofn.nFilterIndex = 1;
|
||||
ofn.lpstrInitialDir = NULL;
|
||||
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
|
||||
|
||||
if (GetOpenFileName(&ofn))
|
||||
{
|
||||
// Changer la texture existante
|
||||
m_model->ChangeTexture(device, context, ofn.lpstrFile, type, texIndex);
|
||||
}
|
||||
}
|
||||
|
||||
// Afficher l'indice de texture et pr<70>visualisation au survol
|
||||
if (ImGui::IsItemHovered())
|
||||
{
|
||||
ImGui::BeginTooltip();
|
||||
ImGui::Text("%s %d", typeName.c_str(), texIndex);
|
||||
ImGui::Image((ImTextureID)texture, ImVec2(192, 192));
|
||||
ImGui::EndTooltip();
|
||||
}
|
||||
|
||||
ImGui::SameLine();
|
||||
}
|
||||
}
|
||||
|
||||
// Bouton pour ajouter une nouvelle texture
|
||||
std::string addButtonLabel = "+##" + std::to_string(typeIndex);
|
||||
if (ImGui::Button(addButtonLabel.c_str(), ImVec2(48, 48)))
|
||||
{
|
||||
// Ouvrir une bo<62>te de dialogue pour ajouter une texture
|
||||
OPENFILENAME ofn;
|
||||
WCHAR szFile[260];
|
||||
ZeroMemory(&ofn, sizeof(ofn));
|
||||
ofn.lStructSize = sizeof(ofn);
|
||||
ofn.hwndOwner = NULL;
|
||||
ofn.lpstrFile = szFile;
|
||||
szFile[0] = '\0';
|
||||
ofn.nMaxFile = sizeof(szFile);
|
||||
ofn.lpstrFilter = L"Texture\0*.png;*.jpg;*.dds\0";
|
||||
ofn.nFilterIndex = 1;
|
||||
ofn.lpstrInitialDir = NULL;
|
||||
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
|
||||
|
||||
if (GetOpenFileName(&ofn))
|
||||
{
|
||||
// Ajouter une nouvelle texture
|
||||
m_model->AddTexture(device, context, ofn.lpstrFile, type);
|
||||
}
|
||||
}
|
||||
|
||||
ImGui::EndGroup();
|
||||
ImGui::Separator();
|
||||
}
|
||||
|
||||
ImGui::EndChild();
|
||||
}
|
||||
|
||||
void SetDevice (ID3D11Device* dev) { device = dev; }
|
||||
void SetContext(ID3D11DeviceContext* ctx) { context = ctx; }
|
||||
|
||||
private:
|
||||
std::shared_ptr<model_class> m_model;
|
||||
std::string m_modelFilePath;
|
||||
bool m_isVisible;
|
||||
ID3D11Device* device;
|
||||
ID3D11DeviceContext* context;
|
||||
};
|
||||
|
||||
} // namespace ecs
|
||||
|
||||
Reference in New Issue
Block a user