File dropping into resource paths in the editor

This commit is contained in:
2016-01-27 17:45:25 +01:00
parent c519ba2536
commit 65da634c71
2 changed files with 35 additions and 9 deletions
+5 -1
View File
@@ -7,7 +7,7 @@
#include <nativefiledialog/nfd.h> #include <nativefiledialog/nfd.h>
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
#include <boost/any.hpp> #include <boost/any.hpp>
#include <boost/sort/spreadsort/string_sort.hpp> #include <boost/algorithm/string/replace.hpp>
#include "../Common.h" #include "../Common.h"
#include "../GLM.h" #include "../GLM.h"
#include <glm/gtx/common.hpp> #include <glm/gtx/common.hpp>
@@ -19,6 +19,7 @@
#include "../Core/ResourceManager.h" #include "../Core/ResourceManager.h"
#include "../Core/EPause.h" #include "../Core/EPause.h"
#include "../Core/EKeyDown.h" #include "../Core/EKeyDown.h"
#include "../Core/EFileDropped.h"
#include "../Rendering/Texture.h" #include "../Rendering/Texture.h"
class EditorGUI class EditorGUI
@@ -96,6 +97,7 @@ private:
WidgetMode m_CurrentWidgetMode = WidgetMode::Translate; WidgetMode m_CurrentWidgetMode = WidgetMode::Translate;
std::set<std::string> m_ModalsToOpen; std::set<std::string> m_ModalsToOpen;
std::map<std::string, boost::any> m_ModalData; std::map<std::string, boost::any> m_ModalData;
std::string m_DroppedFile = "";
// Callbacks // Callbacks
OnEntitySelectedCallback_t m_OnEntitySelected = nullptr; OnEntitySelectedCallback_t m_OnEntitySelected = nullptr;
@@ -112,6 +114,8 @@ private:
// Events // Events
EventRelay<EditorGUI, Events::KeyDown> m_EKeyDown; EventRelay<EditorGUI, Events::KeyDown> m_EKeyDown;
bool OnKeyDown(const Events::KeyDown& e); bool OnKeyDown(const Events::KeyDown& e);
EventRelay<EditorGUI, Events::FileDropped> m_EFileDropped;
bool OnFileDropped(const Events::FileDropped& e);
// Utility functions // Utility functions
boost::filesystem::path fileOpenDialog(); boost::filesystem::path fileOpenDialog();
+30 -8
View File
@@ -6,6 +6,7 @@ EditorGUI::EditorGUI(World* world, EventBroker* eventBroker)
, m_EventBroker(eventBroker) , m_EventBroker(eventBroker)
{ {
EVENT_SUBSCRIBE_MEMBER(m_EKeyDown, &EditorGUI::OnKeyDown); EVENT_SUBSCRIBE_MEMBER(m_EKeyDown, &EditorGUI::OnKeyDown);
EVENT_SUBSCRIBE_MEMBER(m_EFileDropped, &EditorGUI::OnFileDropped);
} }
void EditorGUI::Draw() void EditorGUI::Draw()
@@ -168,10 +169,7 @@ bool EditorGUI::drawEntityNode(EntityWrapper entity)
ImGui::Text(formatEntityName(entity).c_str()); ImGui::Text(formatEntityName(entity).c_str());
ImGui::End(); ImGui::End();
} }
}/* else if (m_CurrentlyDragging == entity) { }
LOG_DEBUG("Stopped dragging %i", entity.ID);
m_CurrentlyDragging = EntityWrapper::Invalid;
}*/
// Entity context menu // Entity context menu
std::string contextMenuUniqueID = std::string("EntityContextMenu") + std::to_string(entity.ID); std::string contextMenuUniqueID = std::string("EntityContextMenu") + std::to_string(entity.ID);
if (hovered && ImGui::IsMouseClicked(1)) { if (hovered && ImGui::IsMouseClicked(1)) {
@@ -436,18 +434,31 @@ bool EditorGUI::drawComponentField_bool(ComponentWrapper &c, const ComponentInfo
bool EditorGUI::drawComponentField_string(ComponentWrapper &c, const ComponentInfo::Field_t &field) bool EditorGUI::drawComponentField_string(ComponentWrapper &c, const ComponentInfo::Field_t &field)
{ {
bool result = false;
auto& val = c.Field<std::string>(field.Name); auto& val = c.Field<std::string>(field.Name);
char tempString[1024]; // Let's just hope this is an sufficiently large buffer for strings :) char tempString[1024]; // Let's just hope this is an sufficiently large buffer for strings :)
tempString[1023] = '\0'; // Null terminator just in case the string is larger than the buffer tempString[1023] = '\0'; // Null terminator just in case the string is larger than the buffer
// Copy the string into the buffer, taking the null terminator into account // Copy the string into the buffer, taking the null terminator into account
memcpy(tempString, val.c_str(), std::min(val.length() + 1, sizeof(tempString) - 1)); memcpy(tempString, val.c_str(), std::min(val.length() + 1, sizeof(tempString) - 1));
if (ImGui::InputText("", tempString, sizeof(tempString))) { if (ImGui::InputText("", tempString, sizeof(tempString))) {
val = std::string(tempString); val = std::string(tempString);
return true; result = true;
} else {
return false;
} }
// TODO: Handle drag and drop of files
// Handle file drag and drop
if (ImGui::IsItemHovered() && !m_DroppedFile.empty()) {
// Unset potential input focus or our newly set value will be overwritten!
if (ImGui::IsItemActive()) {
ImGui::SetActiveID(0, nullptr);
}
// Set the actual dropped value
val = m_DroppedFile;
m_DroppedFile = "";
}
return result;
} }
void EditorGUI::drawModals() void EditorGUI::drawModals()
@@ -574,6 +585,17 @@ bool EditorGUI::OnKeyDown(const Events::KeyDown& e)
return true; return true;
} }
bool EditorGUI::OnFileDropped(const Events::FileDropped& e)
{
// Make a best effort to make the path relative to the working directory of the executable
m_DroppedFile = boost::filesystem::path(e.Path).lexically_relative(boost::filesystem::current_path()).string();
// Compensate for Windows retardedness
std::replace(m_DroppedFile.begin(), m_DroppedFile.end(), '\\', '/');
// Special case for when people drop from the asset folder instead of from the symlink to the asset folders in bin
boost::algorithm::replace_first(m_DroppedFile, "../assets/", "");
return true;
}
boost::filesystem::path EditorGUI::fileOpenDialog() boost::filesystem::path EditorGUI::fileOpenDialog()
{ {
namespace bfs = boost::filesystem; namespace bfs = boost::filesystem;