Shkyera Engine
Easy to use, game engine for Python
Loading...
Searching...
No Matches
AssetSelector.hpp
Go to the documentation of this file.
1#pragma once
2
7#include <Common/Types.hpp>
10#include <ECS/Registry.hpp>
11#include <UI/Common/Style.hpp>
12#include <Utils/AssetUtils.hpp>
13
14namespace shkyera {
15
16template <typename AssetType>
18 public:
19 AssetSelector() = default;
20 AssetSelector(const std::string& title) : _title(title) {}
21 AssetSelector(const std::string& title, Registry* registry, std::optional<AssetHandle> asset)
23 virtual ~AssetSelector() = default;
24
25 void setUpdateCallback(std::function<void(AssetHandle file)> callback) { _updateCallback = std::move(callback); }
26
27 void setClearCallback(std::function<void()> callback) { _clearCallback = std::move(callback); }
28
29 void draw() {
30 ImGui::TextUnformatted(_title.c_str());
31
32 ImGui::SameLine();
33 ImGui::SetCursorPosX(128);
34
35 ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(7.0f, 5.0f));
36 ImGui::PushStyleColor(ImGuiCol_ChildBg, style::DARK_ACCENT);
37 ImGui::PushFont(style::SMALL_FONT);
38
39 const std::string childName = _title + "Child";
40 ImGui::BeginChild(childName.c_str(), ImVec2(-60, 24), true,
42
43 if (_asset.has_value() && _registry) {
44 std::string displayName = "Unknown Name and Path";
45
48 } else if constexpr (PathConstructible<AssetType>) {
51 if (const auto* assetLoader =
52 dynamic_cast<utils::assets::PathAssetLoader<AssetType>*>(assetComponent.constructionFunction.get())) {
54 }
55 }
56 }
57
58 ImGui::TextUnformatted(displayName.c_str());
59
60 ImGui::SameLine();
62 } else {
63 ImGui::BeginDisabled();
64
65 std::string label = "Drag " + _title;
66 ImGui::TextUnformatted(label.c_str());
67 ImGui::EndDisabled();
68 }
69
70 if (ImGui::BeginDragDropTarget()) {
71 const auto assetTypeName = std::string(typeid(AssetType).name());
72 if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload(("ASSET_" + assetTypeName).c_str())) {
74 std::memcpy(&handle, payload->Data, sizeof(AssetHandle));
75 _asset = handle;
76
77 if (_updateCallback) {
79 }
80 }
81 ImGui::EndDragDropTarget();
82 }
83
84 ImGui::EndChild();
85
86 ImGui::PopFont();
87 ImGui::PopStyleColor();
88 ImGui::PopStyleVar();
89
90 ImGui::SameLine();
91 if (ImGui::Button(std::string("Clear##" + childName).c_str(), ImVec2(50, 22))) {
92 _asset.reset();
93 clearAsset();
94 if (_clearCallback) {
96 }
97 }
98 }
99
100 protected:
102 virtual void clearAsset() {}
103
104 private:
105 std::string _title{};
107 std::optional<AssetHandle> _asset;
108
110 std::function<void()> _clearCallback;
111};
112
113class TextureAssetSelector : public AssetSelector<Texture> {
114 public:
117
118 private:
120 if (registry->hasComponent<AssetComponent<Texture>>(handle)) {
121 auto& textureAsset = registry->getComponent<AssetComponent<Texture>>(handle);
123 const auto imageSize = _texture->getSize();
124 const auto aspectRatio = imageSize.x / imageSize.y;
125
126 ImGui::Image(_texture->getImguiTextureID(), {aspectRatio * 16, 16});
127 }
128 }
129
130 void clearAsset() override { _texture.reset(); }
131
132 AssetRef<Texture> _texture{}; //< Keeps the Texture Asset alive
133};
134
135} // namespace shkyera
Definition AssetSelector.hpp:17
std::function< void(AssetHandle)> _updateCallback
Definition AssetSelector.hpp:109
void setUpdateCallback(std::function< void(AssetHandle file)> callback)
Definition AssetSelector.hpp:25
AssetSelector(const std::string &title)
Definition AssetSelector.hpp:20
void setClearCallback(std::function< void()> callback)
Definition AssetSelector.hpp:27
std::string _title
Definition AssetSelector.hpp:105
void draw()
Definition AssetSelector.hpp:29
std::function< void()> _clearCallback
Definition AssetSelector.hpp:110
std::optional< AssetHandle > _asset
Definition AssetSelector.hpp:107
virtual ~AssetSelector()=default
AssetSelector(const std::string &title, Registry *registry, std::optional< AssetHandle > asset)
Definition AssetSelector.hpp:21
Registry * _registry
Definition AssetSelector.hpp:106
virtual void clearAsset()
Definition AssetSelector.hpp:102
virtual void drawAsset(Registry *registry, AssetHandle handle)
Definition AssetSelector.hpp:101
Definition NameComponent.hpp:9
std::string & getName()
Definition NameComponent.hpp:16
Definition Registry.hpp:28
bool hasComponent(Entity entity) const
Definition Registry.hpp:117
Component & getComponent(Entity entity)
Definition Registry.hpp:141
Definition AssetSelector.hpp:113
void drawAsset(Registry *registry, AssetHandle handle) override
Definition AssetSelector.hpp:119
~TextureAssetSelector()
Definition AssetSelector.hpp:116
void clearAsset() override
Definition AssetSelector.hpp:130
AssetRef< Texture > _texture
Definition AssetSelector.hpp:132
Definition Texture.hpp:9
Definition AssetUtils.hpp:17
Definition Types.hpp:12
const ImVec4 DARK_ACCENT
Definition Style.cpp:26
ImFont * SMALL_FONT
Definition Style.cpp:13
AssetRef< AssetType > read(AssetComponent< AssetType > &assetComponent)
Definition AssetUtils.hpp:81
Definition Asset.hpp:6
Entity AssetHandle
Definition Asset.hpp:8
Definition Clock.hpp:9
void reset()
Definition Clock.hpp:13