imgui drag&drop

This commit is contained in:
Mikulas Florek 2017-11-08 10:50:45 +01:00
parent 59608dcfd0
commit 2e3e44b233
6 changed files with 24 additions and 92 deletions

View file

@ -468,12 +468,11 @@ void AssetBrowser::fileColumn()
auto callbacks = [this](FileInfo& tile) {
if (ImGui::IsItemHovered()) ImGui::SetTooltip("%s", tile.filepath.data);
if (ImGui::IsMouseDragging() && ImGui::IsItemHovered(ImGuiHoveredFlags_RectOnly))
if (ImGui::BeginDragDropSource(ImGuiDragDropFlags_SourceAllowNullID))
{
if (m_app.getDragData().type == StudioApp::DragData::NONE)
{
m_app.startDrag(StudioApp::DragData::PATH, tile.filepath, stringLength(tile.filepath) + 1);
}
ImGui::Text(tile.filepath);
ImGui::SetDragDropPayload("path", tile.filepath, stringLength(tile.filepath) + 1, ImGuiCond_Once);
ImGui::EndDragDropSource();
}
else if (ImGui::IsItemHovered() && ImGui::IsMouseReleased(0))
{
@ -708,20 +707,22 @@ bool AssetBrowser::resourceInput(const char* label, const char* str_id, char* bu
ImGui::OpenPopup("popup");
}
ImGui::EndGroup();
if (ImGui::IsItemHovered(ImGuiHoveredFlags_RectOnly))
if (ImGui::BeginDragDropTarget())
{
if (ImGui::IsMouseReleased(0) && m_app.getDragData().type == StudioApp::DragData::PATH)
if (auto* payload = ImGui::AcceptDragDropPayload("path"))
{
char ext[10];
const char* path = (const char*)m_app.getDragData().data;
const char* path = (const char*)payload->Data;
PathUtils::getExtension(ext, lengthOf(ext), path);
if (acceptExtension(ext, type))
{
copyString(buf, max_size, path);
ImGui::EndDragDropTarget();
ImGui::PopID();
return true;
}
}
ImGui::EndDragDropTarget();
}
ImGui::SameLine();
ImGui::Text("%s", label);

View file

@ -592,13 +592,15 @@ bool PropertyGrid::entityInput(const char* label, const char* str_id, Entity& en
ImGui::OpenPopup(popup_name);
}
if (ImGui::IsItemHovered(ImGuiHoveredFlags_RectOnly))
if (ImGui::BeginDragDropTarget())
{
if (ImGui::IsMouseReleased(0) && m_app.getDragData().type == StudioApp::DragData::ENTITY)
if (auto* payload = ImGui::AcceptDragDropPayload("entity"))
{
entity = *(Entity*)m_app.getDragData().data;
entity = *(Entity*)payload->Data;
ImGui::EndDragDropTarget();
return true;
}
ImGui::EndDragDropTarget();
}
ImGui::SameLine();

View file

@ -164,7 +164,6 @@ public:
, m_events(m_allocator)
{
m_add_cmp_root.label[0] = '\0';
m_drag_data = {DragData::NONE, nullptr, 0};
m_template_name[0] = '\0';
m_open_filter[0] = '\0';
SDL_SetMainReady();
@ -224,7 +223,6 @@ public:
~StudioAppImpl()
{
m_allocator.deallocate(m_drag_data.data);
saveSettings();
unloadIcons();
@ -511,24 +509,6 @@ public:
ImGui::NewFrame();
ImGui::PushFont(m_font);
if (m_drag_data.type == DragData::PATH)
{
ImGui::BeginTooltip();
char tmp[MAX_PATH_LENGTH];
PathUtils::getFilename(tmp, lengthOf(tmp), (const char*)m_drag_data.data);
ImGui::Text("%s", tmp);
ImGui::EndTooltip();
}
else if (m_drag_data.type == DragData::ENTITY)
{
ImGui::BeginTooltip();
char buf[1024];
getEntityListDisplayName(*m_editor, buf, lengthOf(buf), *(Entity*)m_drag_data.data);
ImGui::Text("%s", buf);
ImGui::EndTooltip();
}
}
@ -587,14 +567,6 @@ public:
}
ImGui::PopFont();
ImGui::Render();
if (ImGui::GetIO().MouseReleased[0])
{
m_allocator.deallocate(m_drag_data.data);
m_drag_data.data = nullptr;
m_drag_data.size = 0;
m_drag_data.type = DragData::NONE;
}
}
void update()
@ -1349,7 +1321,8 @@ public:
ImGui::PopItemWidth();
}
ImGui::EndChild();
if (ImGui::BeginDragDropTarget())
// TODO uncomment once it's fixed in imgui
/*if (ImGui::BeginDragDropTarget())
{
if (auto* payload = ImGui::AcceptDragDropPayload("entity"))
{
@ -1357,7 +1330,7 @@ public:
m_editor->makeParent(INVALID_ENTITY, dropped_entity);
}
ImGui::EndDragDropTarget();
}
}*/
}
ImGui::EndDock();
}
@ -1366,37 +1339,12 @@ public:
void dummy() {}
void startDrag(DragData::Type type, const void* data, int size) override
{
m_allocator.deallocate(m_drag_data.data);
m_drag_data.type = type;
if (size > 0)
{
m_drag_data.data = m_allocator.allocate(size);
copyMemory(m_drag_data.data, data, size);
m_drag_data.size = size;
}
else
{
m_drag_data.data = nullptr;
m_drag_data.size = 0;
}
}
void setFullscreen(bool fullscreen) override
{
SDL_SetWindowFullscreen(m_window, fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
}
DragData getDragData() override
{
return m_drag_data;
}
void saveSettings()
{
m_settings.m_is_asset_browser_open = m_asset_browser->m_is_open;
@ -2429,7 +2377,6 @@ public:
bool m_is_pack_data_dialog_open;
bool m_is_entity_list_open;
bool m_is_save_as_dialog_open;
DragData m_drag_data;
ImFont* m_font;
ImFont* m_bold_font;
};

View file

@ -69,19 +69,6 @@ public:
char label[50];
};
struct DragData
{
enum Type
{
NONE,
PATH,
ENTITY
};
Type type;
void* data;
int size;
};
struct LUMIX_EDITOR_API StaticPluginRegister
{
typedef void (*Creator)(StudioApp& app);
@ -122,8 +109,6 @@ public:
virtual void addWindowAction(Action* action) = 0;
virtual Action* getAction(const char* name) = 0;
virtual SDL_Window* getWindow() = 0;
virtual void startDrag(DragData::Type type, const void* data, int size) = 0;
virtual DragData getDragData() = 0;
virtual void setFullscreen(bool fullscreen) = 0;
virtual bool makeFile(const char* path, const char* content) = 0;
virtual Vec2 getMouseMove() const = 0;

View file

@ -223,9 +223,8 @@ void SceneView::removeDropHandler(DropHandler handler)
}
void SceneView::handleDrop(float x, float y)
void SceneView::handleDrop(const char* path, float x, float y)
{
const char* path = (const char*)m_app.getDragData().data;
auto hit = castRay(x, y);
for (DropHandler handler : m_drop_handlers)
@ -238,7 +237,6 @@ void SceneView::handleDrop(float x, float y)
if (PathUtils::hasExtension(path, "fab"))
{
}
else if (PathUtils::hasExtension(path, "msh"))
{
@ -254,7 +252,6 @@ void SceneView::handleDrop(float x, float y)
}
else if (PathUtils::hasExtension(path, "mat") && hit.m_mesh)
{
auto drag_data = m_app.getDragData();
m_editor.selectEntities(&hit.m_entity, 1);
auto* model = m_pipeline->getScene()->getModelInstanceModel(hit.m_component);
int mesh_index = 0;
@ -267,7 +264,7 @@ void SceneView::handleDrop(float x, float y)
}
}
auto* prop= Properties::getProperty(MODEL_INSTANCE_TYPE, "Materials", "Source");
m_editor.setProperty(MODEL_INSTANCE_TYPE, mesh_index, *prop, &hit.m_entity, 1, drag_data.data, drag_data.size);
m_editor.setProperty(MODEL_INSTANCE_TYPE, mesh_index, *prop, &hit.m_entity, 1, path, stringLength(path) + 1);
}
}
}
@ -389,18 +386,18 @@ void SceneView::onWindowGUI()
{
ImGui::Image(&m_texture_handle, size);
}
if (ImGui::IsItemHovered())
if (ImGui::BeginDragDropTarget())
{
if (ImGui::IsMouseReleased(0) && m_app.getDragData().type == StudioApp::DragData::PATH)
if (auto* payload = ImGui::AcceptDragDropPayload("path"))
{
float x = (ImGui::GetMousePos().x - content_min.x) / size.x;
float y = (ImGui::GetMousePos().y - content_min.y) / size.y;
handleDrop(x, y);
handleDrop((const char*)payload->Data, x, y);
}
ImGui::EndDragDropTarget();
}
view_pos = content_min;
bool handle_input = ImGui::IsItemHovered();
if(handle_input)
{

View file

@ -42,7 +42,7 @@ class SceneView : public StudioApp::IPlugin
void onUniverseDestroyed();
void captureMouse(bool capture);
RayCastModelHit castRay(float x, float y);
void handleDrop(float x, float y);
void handleDrop(const char* path, float x, float y);
void onToolbar();
void resetCameraSpeed();
void onResourceChanged(const Path& path, const char* /*ext*/);