Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

feat: Add DragDropManager for asset drag-and-drop

esengine4fe6c62d53b9
diff --git a/src/esengine/editor/CMakeLists.txt b/src/esengine/editor/CMakeLists.txt
index 84610baa5..78c99156f 100644
--- a/src/esengine/editor/CMakeLists.txt
+++ b/src/esengine/editor/CMakeLists.txt
@@ -5,6 +5,7 @@ set(EDITOR_SOURCES
EditorApplication.cpp
AssetDatabase.cpp
ThumbnailGenerator.cpp
+ DragDropManager.cpp
# Property editors
property/PropertyEditor.cpp
@@ -29,6 +30,8 @@ set(EDITOR_HEADERS
EditorApplication.hpp
AssetDatabase.hpp
ThumbnailGenerator.hpp
+ DragDropTypes.hpp
+ DragDropManager.hpp
# Core subsystem
core/EditorEvents.hpp
diff --git a/src/esengine/editor/DragDropManager.cpp b/src/esengine/editor/DragDropManager.cpp
new file mode 100644
index 000000000..de8c06a73
--- /dev/null
+++ b/src/esengine/editor/DragDropManager.cpp
@@ -0,0 +1,64 @@
+#include "DragDropManager.hpp"
+
+#include <cmath>
+
+namespace esengine::editor {
+
+void DragDropManager::beginDrag(const DragDropPayload& payload, const glm::vec2& startPos) {
+ if (isDragging_) {
+ return;
+ }
+
+ pendingPayload_ = payload;
+ startPos_ = startPos;
+ currentPos_ = startPos;
+}
+
+void DragDropManager::updateDrag(const glm::vec2& currentPos) {
+ currentPos_ = currentPos;
+
+ if (pendingPayload_.has_value() && !isDragging_) {
+ f32 distance = glm::length(currentPos_ - startPos_);
+ if (distance >= DRAG_THRESHOLD) {
+ isDragging_ = true;
+ activePayload_ = *pendingPayload_;
+ pendingPayload_.reset();
+ onDragStart.publish(activePayload_);
+ }
+ }
+
+ if (isDragging_) {
+ onDragMove.publish(activePayload_, currentPos_);
+ }
+}
+
+void DragDropManager::endDrag(const glm::vec2& pos) {
+ currentPos_ = pos;
+
+ if (isDragging_) {
+ onDragEnd.publish(activePayload_, pos);
+ isDragging_ = false;
+ activePayload_ = {};
+ }
+
+ pendingPayload_.reset();
+}
+
+void DragDropManager::cancelDrag() {
+ if (isDragging_) {
+ onDragCancel.publish();
+ }
+
+ isDragging_ = false;
+ activePayload_ = {};
+ pendingPayload_.reset();
+}
+
+const DragDropPayload* DragDropManager::getPayload() const {
+ if (isDragging_) {
+ return &activePayload_;
+ }
+ return nullptr;
+}
+
+} // namespace esengine::editor
diff --git a/src/esengine/editor/DragDropManager.hpp b/src/esengine/editor/DragDropManager.hpp
new file mode 100644
index 000000000..23111a2c7
--- /dev/null
+++ b/src/esengine/editor/DragDropManager.hpp
@@ -0,0 +1,45 @@
+#pragma once
+
+#include "DragDropTypes.hpp"
+#include "../events/Signal.hpp"
+
+#include <glm/glm.hpp>
+
+#include <optional>
+
+namespace esengine::editor {
+
+class DragDropManager {
+public:
+ static constexpr f32 DRAG_THRESHOLD = 5.0f;
+
+ DragDropManager() = default;
+ ~DragDropManager() = default;
+
+ void beginDrag(const DragDropPayload& payload, const glm::vec2& startPos);
+ void updateDrag(const glm::vec2& currentPos);
+ void endDrag(const glm::vec2& pos);
+ void cancelDrag();
+
+ bool isDragging() const { return isDragging_; }
+ bool hasPendingDrag() const { return pendingPayload_.has_value(); }
+
+ const DragDropPayload* getPayload() const;
+ glm::vec2 getDragPosition() const { return currentPos_; }
+ glm::vec2 getDragStartPosition() const { return startPos_; }
+ glm::vec2 getDragDelta() const { return currentPos_ - startPos_; }
+
+ Signal<void(const DragDropPayload&)> onDragStart;
+ Signal<void(const DragDropPayload&, const glm::vec2&)> onDragMove;
+ Signal<void(const DragDropPayload&, const glm::vec2&)> onDragEnd;
+ Signal<void()> onDragCancel;
+
+private:
+ bool isDragging_ = false;
+ std::optional<DragDropPayload> pendingPayload_;
+ DragDropPayload activePayload_;
+ glm::vec2 startPos_{0.0f};
+ glm::vec2 currentPos_{0.0f};
+};
+
+} // namespace esengine::editor
diff --git a/src/esengine/editor/DragDropTypes.hpp b/src/esengine/editor/DragDropTypes.hpp
new file mode 100644
index 000000000..516edef44
--- /dev/null
+++ b/src/esengine/editor/DragDropTypes.hpp
@@ -0,0 +1,44 @@
+#pragma once
+
+#include "../core/Types.hpp"
+#include "../ecs/Entity.hpp"
+
+#include <glm/glm.hpp>
+
+#include <string>
+#include <variant>
+
+namespace esengine::editor {
+
+enum class DragDropType : u8 {
+ None,
+ Asset,
+ Entity,
+ Component
+};
+
+struct AssetDragData {
+ std::string guid;
+ std::string path;
+ std::string name;
+};
+
+struct EntityDragData {
+ Entity entity = INVALID_ENTITY;
+};
+
+struct ComponentDragData {
+ Entity entity = INVALID_ENTITY;
+ std::string componentType;
+};
+
+using DragDropData = std::variant<std::monostate, AssetDragData, EntityDragData, ComponentDragData>;
+
+struct DragDropPayload {
+ DragDropType type = DragDropType::None;
+ DragDropData data;
+
+ bool isValid() const { return type != DragDropType::None; }
+};
+
+} // namespace esengine::editor
diff --git a/src/esengine/editor/EditorApplication.cpp b/src/esengine/editor/EditorApplication.cpp
index 4ce3b2f86..3699ed03b 100644
--- a/src/esengine/editor/EditorApplication.cpp
+++ b/src/esengine/editor/EditorApplication.cpp
@@ -238,6 +238,8 @@ void EditorApplication::onTouch(TouchType type, const TouchPoint& point) {
return;
}
+ glm::vec2 pos(point.x, point.y);
+
switch (type) {
case TouchType::Begin:
uiContext_->processMouseDown(ui::MouseButton::Left, point.x, point.y);
@@ -245,10 +247,14 @@ void EditorApplication::onTouch(TouchType type, const TouchPoint& point) {
case TouchType::Move:
uiContext_->processMouseMove(point.x, point.y);
+ dragDropManager_.updateDrag(pos);
break;
case TouchType::End:
case TouchType::Cancel:
+ if (dragDropManager_.isDragging()) {
+ dragDropManager_.endDrag(pos);
+ }
uiContext_->processMouseUp(ui::MouseButton::Left, point.x, point.y);
break;
}
diff --git a/src/esengine/editor/EditorApplication.hpp b/src/esengine/editor/EditorApplication.hpp
index c74f75652..d66b5749c 100644
--- a/src/esengine/editor/EditorApplication.hpp
+++ b/src/esengine/editor/EditorApplication.hpp
@@ -26,6 +26,7 @@
#include "core/Selection.hpp"
#include "AssetDatabase.hpp"
#include "ThumbnailGenerator.hpp"
+#include "DragDropManager.hpp"
namespace esengine {
namespace editor {
@@ -112,6 +113,12 @@ public:
*/
ThumbnailGenerator& getThumbnailGenerator() { return thumbnailGenerator_; }
+ /**
+ * @brief Get the drag-drop manager
+ * @return Reference to the drag-drop manager
+ */
+ DragDropManager& getDragDropManager() { return dragDropManager_; }
+
protected:
// =========================================================================
// Application Lifecycle Overrides
@@ -207,6 +214,7 @@ private:
EntitySelection selection_; ///< Entity selection manager
AssetDatabase assetDatabase_; ///< Asset database
ThumbnailGenerator thumbnailGenerator_; ///< Thumbnail generator
+ DragDropManager dragDropManager_; ///< Drag-drop manager
Unique<ui::UIContext> uiContext_; ///< UI system context
ui::DockArea* dockArea_ = nullptr; ///< Main docking area (owned by UIContext)