Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add adding components #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@

## Fixme:

- [*] The scene isn't render;
- [ ] Move all 'Serialize' methods from the entities to the serializers;
- [ ] Disable scroll like mouse for scene;
- [ ] Fix lighting;
- [*] The scene isn't render;
- [*] Fix ImGui scene. Fit the render image to the scene;

## ImGui:

- [ ] Make nice scene tree;
- [ ] Adding & removing nodes dialog;
- [ ] Rename nodes;
- [ ] Dragging nodes to change their position in the tree;
- [*] Adding & removing nodes dialog;
- [ ] Make nice components editor;
- [ ] Adding & removing components;
- [ ] Removing components;
- [ ] Enable/Disable components;
- [*] Adding components;
- [ ] Change style;
- [ ] Add Assets viewer;

Expand All @@ -31,4 +34,4 @@

- [ ] Make build system faster;
- [ ] Add CMakePresets & CI;
- [ ] Add building for any platform;
- [ ] Add building for any platform;
6 changes: 3 additions & 3 deletions src/app_info/app_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

#define LOG_LEVEL "Debug"

#define GIT_COMMIT_HASH "0a2e0afbda0fbdf6d4d61a346aea3685539b0282-dirty"
#define GIT_COMMIT_DATE "Tue Oct 25 08:48:22 2022"
#define GIT_COMMIT_MESSAGE "Update TODO"
#define GIT_COMMIT_HASH "2839200b4c78e1f0821554f940f78811b118071a-dirty"
#define GIT_COMMIT_DATE "Tue Oct 25 14:32:22 2022"
#define GIT_COMMIT_MESSAGE "Merge pull request #17 from seigtm/Golovinsky_Refactoring_And_improvements"

namespace meov {

Expand Down
6 changes: 3 additions & 3 deletions src/app_info/definitions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ constexpr std::string_view Program_name{ "MEOV | Minimalistic Easy Object Viewer

constexpr std::string_view LogLevel{ "Debug" };

constexpr std::string_view GitCommitHash{ "0a2e0afbda0fbdf6d4d61a346aea3685539b0282-dirty" };
constexpr std::string_view GitCommitDate{ "Tue Oct 25 08:48:22 2022" };
constexpr std::string_view GitCommitMessage{ "Update TODO" };
constexpr std::string_view GitCommitHash{ "2839200b4c78e1f0821554f940f78811b118071a-dirty" };
constexpr std::string_view GitCommitDate{ "Tue Oct 25 14:32:22 2022" };
constexpr std::string_view GitCommitMessage{ "Merge pull request #17 from seigtm/Golovinsky_Refactoring_And_improvements" };

} // namespace meov::definitions
64 changes: 64 additions & 0 deletions src/core/object/components/factory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include "factory.hpp"

#include "object.hpp"
#include "transform_component.hpp"
#include "model_component.hpp"
#include "move_component.hpp"
#include "camera_component.hpp"
#include "lighting_component.hpp"
#include "skybox_component.hpp"
#include "shader_component.hpp"


namespace meov::core::components {

template<class Component, class ...Argv>
bool defaultGenerator(std::shared_ptr<Object> object, Argv &&...argv) {
return object->AddComponent<Component>(std::forward<Argv>(argv)...) != nullptr;
}

std::unordered_map<std::string, Factory::AddComponentMethod> Factory::mRegistry{
{ "transform", defaultGenerator<TransformComponent> },
{ "model", defaultGenerator<ModelComponent> },
{ "move", defaultGenerator<MoveComponent> },
// { "camera", defaultGenerator<CameraComponent> },
{ "directional_lighting", defaultGenerator<DirectionalLightingComponent> },
{ "point_lighting", defaultGenerator<PointLightingComponent> },
{ "spot_lighting", defaultGenerator<SpotLightingComponent> },
{ "skybox", defaultGenerator<SkyboxComponent> },
// { "shader", defaultGenerator<ShaderComponent> },
};

void Factory::Register(std::string &&name, AddComponentMethod &&method) {
if (!name.empty() && method);
mRegistry.emplace(std::move(name), std::move(method));
}

void Factory::Build(std::shared_ptr<Object> object, const std::vector<std::string> &components) {
if (object == nullptr || components.empty())
return;

for (const auto &component : components) {
Build(object, component);
}
}

void Factory::Build(std::shared_ptr<Object> object, const std::string &component) {
if (object == nullptr || component.empty())
return;
if (auto found{ mRegistry.find(component) }; found != mRegistry.end()) {
found->second(object);
}
}

std::vector<std::string> Factory::GetComponents() {
std::vector<std::string> components;
components.reserve(mRegistry.size());
std::transform(mRegistry.begin(), mRegistry.end(), std::back_inserter(components),
[] (const auto &pair) { return pair.first; });
return components;
}

} // namespace meov::core::components


30 changes: 30 additions & 0 deletions src/core/object/components/factory.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include <unordered_map>
#include <functional>
#include <string>
#include <memory>
#include <vector>

namespace meov::core {
class Object;
} // namespace meov::core


namespace meov::core::components {

class Factory {
public:
using AddComponentMethod = std::function<bool(std::shared_ptr<Object>)>;

static void Register(std::string &&name, AddComponentMethod &&method);

static void Build(std::shared_ptr<Object> object, const std::vector<std::string> &components);
static void Build(std::shared_ptr<Object> object, const std::string &component);

static std::vector<std::string> GetComponents();
private:
static std::unordered_map<std::string, AddComponentMethod> mRegistry;
};

} // namespace meov::core::components
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class LightingComponent : public Component {

class DirectionalLightingComponent final : public LightingComponent {
public:
explicit DirectionalLightingComponent(glm::vec3 &&dir);
explicit DirectionalLightingComponent(glm::vec3 &&dir = {});
~DirectionalLightingComponent() override;

void PreDraw(Graphics &) override;
Expand Down Expand Up @@ -74,7 +74,7 @@ class PointLightingComponent final : public LightingComponent {

class SpotLightingComponent final : public LightingComponent {
public:
explicit SpotLightingComponent(glm::vec3 &&dir);
explicit SpotLightingComponent(glm::vec3 &&dir = {});
~SpotLightingComponent() override;

void PreDraw(Graphics &) override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

namespace meov::core::components {

ModelComponent::ModelComponent()
: Component{ "Model component" } {}

ModelComponent::ModelComponent(const fs::path &model)
: Component{ "Model component" }
, mModel{ RESOURCES->LoadModel(model) } {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace meov::core::components {

class ModelComponent : public Component {
public:
ModelComponent();
explicit ModelComponent(const fs::path &model);
explicit ModelComponent(const std::shared_ptr<core::Model> &model);
~ModelComponent() override = default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace meov::core::components {

class SkyboxComponent : public Component {
public:
explicit SkyboxComponent(const fs::path &path);
SkyboxComponent(const fs::path &path = "models/skybox");
~SkyboxComponent() override = default;

void PreDraw(Graphics &g) override;
Expand Down
3 changes: 3 additions & 0 deletions src/utils/scope_wrapper/scope_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ class ScopeWrapper {
: mDestructorCallback{ std::move(dCallback) } {
cCallback();
}

explicit ScopeWrapper(DestructorCallback &&dCallback) : mDestructorCallback{ std::move(dCallback) } {}

~ScopeWrapper() {
mDestructorCallback();
}
Expand Down
29 changes: 24 additions & 5 deletions src/windows/properties/properties_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@

#include "object.hpp"
#include "component.hpp"
#include "factory.hpp"

namespace meov::Window {

Properties::Properties(ImVec2 const &size, bool isClosable)
: Base{ "Properties", size, isClosable } {}
: Base{ "Properties", size, isClosable }
, mComponents{ core::components::Factory::GetComponents() }
, mSelectedComponent{ mComponents.begin() } {
}

void Properties::Select(std::vector<std::weak_ptr<core::Object>> &&object) {
mObjects = std::move(object);
Expand All @@ -32,11 +36,10 @@ void Properties::DrawImpl() {
ImGui::Separator();
});

if (ImGui::BeginCombo("Components", "transform")) {
ImGui::EndCombo();
}
DrawComponentsDropBox();
ImGui::SameLine();
if (ImGui::Button("Add component")) {
// core::components::Factory::Build(obj, "component");
core::components::Factory::Build(obj, *mSelectedComponent);
}
ImGui::Unindent();
ImGui::PopID();
Expand All @@ -45,4 +48,20 @@ void Properties::DrawImpl() {
}
}

void Properties::DrawComponentsDropBox() {
if (!ImGui::BeginCombo("##Components", mSelectedComponent->c_str()))
return;
for (auto current{ mComponents.begin() }; current != mComponents.end(); ++current) {
const bool isSelected{ mSelectedComponent == current };
if (ImGui::Selectable(current->c_str(), isSelected))
mSelectedComponent = current;

// Set the initial focus when opening the combo (scrolling + keyboard navigation focus)
if (isSelected)
ImGui::SetItemDefaultFocus();
}

ImGui::EndCombo();
}

} // namespace meov::Window
3 changes: 3 additions & 0 deletions src/windows/properties/properties_window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ class Properties final : public Base {

private:
std::vector<std::weak_ptr<core::Object>> mObjects;
const std::vector<std::string> mComponents;
std::vector<std::string>::const_iterator mSelectedComponent;

void DrawImpl() override;
void DrawComponentsDropBox();
};

} // namespace meov::Window