Skip to content

Commit

Permalink
Implement new gui (toolbar) on editor
Browse files Browse the repository at this point in the history
Fix bug wrong selection when drag item on Hierarchy
  • Loading branch information
ducphamhong committed Aug 19, 2024
1 parent 661fc29 commit e370fc5
Show file tree
Hide file tree
Showing 9 changed files with 316 additions and 245 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,9 @@ namespace Skylicht

void CGUITransformGizmos::onRemove()
{

CSelection::getInstance()->clear();
CGUIHandles::getInstance()->end();
m_selectID = "";
}

void CGUITransformGizmos::refresh()
Expand Down
100 changes: 73 additions & 27 deletions Projects/Editor/Source/Editor/Space/GUIDesign/CSpaceGUIDesign.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,12 @@ namespace Skylicht
{
GUI::CButton* btn = NULL;
m_toolBar = new GUI::CToolbar(window);
m_toolBar->addButton(L"New", GUI::ESystemIcon::NewFile);

btn = m_toolBar->addButton(L"New", GUI::ESystemIcon::NewFile);
btn->OnPress = BIND_LISTENER(&CSpaceGUIDesign::onNew, this);

btn = m_toolBar->addButton(L"Save", GUI::ESystemIcon::Save);
btn->OnPress = BIND_LISTENER(&CSpaceGUIDesign::onSave, this);

m_toolBar->addSpace();

btn = m_toolBar->addButton(L"Zoom in", GUI::ESystemIcon::ZoomIn);
Expand All @@ -76,16 +77,16 @@ namespace Skylicht
m_toolBar->addSpace();
btn = m_toolBar->addButton(L"Copy", GUI::ESystemIcon::Copy);
btn->OnPress = BIND_LISTENER(&CSpaceGUIDesign::onCopy, this);

btn = m_toolBar->addButton(L"Paste", GUI::ESystemIcon::Paste);
btn->OnPress = BIND_LISTENER(&CSpaceGUIDesign::onPaste, this);

m_toolBar->addSpace();
btn = m_toolBar->addButton(L"Setting", GUI::ESystemIcon::Setting, true);
m_canvasSettingMenu = new GUI::CMenu(window->getCanvas());
m_canvasSettingController = new CCanvasSettingController(editor, m_canvasSettingMenu);
btn->OnPress = BIND_LISTENER(&CSpaceGUIDesign::onSetting, this);

m_textMousePos = new GUI::CLabel(m_toolBar);
m_toolBar->addControl(m_textMousePos, true);
m_textMousePos->setPadding(GUI::SPadding(0.0f, 3.0f, 0.0f, 0.0f));
Expand Down Expand Up @@ -189,26 +190,30 @@ namespace Skylicht

CGUIDesignController::getInstance()->rebuildGUIHierachy();

// reset view
m_guiScale = 1.0f;
m_viewX = 0.0f;
m_viewY = 0.0f;

float dx = 0.0f;
float dy = 0.0f;
m_topRuler->setPosition(-(m_viewX * m_guiScale + dx));
m_leftRuler->setPosition(-(m_viewY * m_guiScale + dy));

canvas->getRootElement()->setPosition(
core::vector3df(
m_viewX + dx / m_guiScale,
m_viewY + dy / m_guiScale,
0.0f)
);
resetView(canvas);
}
}
}

void CSpaceGUIDesign::resetView(CCanvas* canvas)
{
m_guiScale = 1.0f;
m_viewX = 0.0f;
m_viewY = 0.0f;

float dx = 0.0f;
float dy = 0.0f;
m_topRuler->setPosition(-(m_viewX * m_guiScale + dx));
m_leftRuler->setPosition(-(m_viewY * m_guiScale + dy));

canvas->getRootElement()->setPosition(
core::vector3df(
m_viewX + dx / m_guiScale,
m_viewY + dy / m_guiScale,
0.0f)
);
}

void CSpaceGUIDesign::onKeyPressed(GUI::CBase* base, int key, bool down)
{
GUI::CInput* input = GUI::CInput::getInput();
Expand Down Expand Up @@ -477,17 +482,58 @@ namespace Skylicht
m_canvasSettingMenu->open(base);
m_canvasSettingController->onShow();
}

void CSpaceGUIDesign::onNew(GUI::CBase* base)
{

CGUIDesignController* guiController = CGUIDesignController::getInstance();
if (guiController->needSave())
{
const std::string& path = guiController->getSaveGUIPath();
std::string name = CPath::getFileName(path);
if (name.empty())
name = "UntitledGUI.gui";

GUI::CMessageBox* msgBox = new GUI::CMessageBox(m_window->getCanvas(), GUI::CMessageBox::YesNoCancel);
msgBox->setMessage("Save changes (GUI) before new GUI?", name);
msgBox->OnYes = [&, &p = path, controller = guiController](GUI::CBase* button) {
if (p.empty())
{
std::string assetFolder = CAssetManager::getInstance()->getAssetFolder();
GUI::COpenSaveDialog* dialog = new GUI::COpenSaveDialog(m_window->getCanvas(), GUI::COpenSaveDialog::Save, assetFolder.c_str(), assetFolder.c_str(), "scene;*");
dialog->OnSave = [&, controller = controller](std::string path)
{
controller->save(path.c_str());
newGUI();
};
}
else
{
controller->save(path.c_str());
newGUI();
}
};
msgBox->OnNo = [&](GUI::CBase* button) {
newGUI();
};
}
}

void CSpaceGUIDesign::newGUI()
{
CGameObject* canvasObj = m_scene->searchObjectInChild(L"GUICanvas");
CCanvas* canvas = canvasObj->getComponent<CCanvas>();
canvas->removeAllElement();
m_gizmos->onRemove();

CGUIDesignController::getInstance()->rebuildGUIHierachy();
resetView(canvas);
}

void CSpaceGUIDesign::onSave(GUI::CBase* base)
{
CEditor::getInstance()->onSaveGUICanvas();
}

void CSpaceGUIDesign::onCopy(GUI::CBase* base)
{
CGUIDesignController::getInstance()->onCopy();
Expand All @@ -497,7 +543,7 @@ namespace Skylicht
{
CGUIDesignController::getInstance()->onPaste();
}

void CSpaceGUIDesign::onRender(GUI::CBase* base)
{
// flush 2d gui
Expand Down
16 changes: 10 additions & 6 deletions Projects/Editor/Source/Editor/Space/GUIDesign/CSpaceGUIDesign.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ namespace Skylicht

GUI::CMenu* m_canvasSettingMenu;
CCanvasSettingController* m_canvasSettingController;

public:
CSpaceGUIDesign(GUI::CWindow* window, CEditor* editor);

Expand Down Expand Up @@ -108,15 +108,19 @@ namespace Skylicht
void doZoomOut(float dx, float dy);

void onSetting(GUI::CBase* base);

void onNew(GUI::CBase* base);


void newGUI();

void resetView(CCanvas* canvas);

void onSave(GUI::CBase* base);

void onCopy(GUI::CBase* base);

void onPaste(GUI::CBase* base);

void onRender(GUI::CBase* base);

void postMouseEventToHandles(EMOUSE_INPUT_EVENT eventType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ namespace Skylicht
{
if (m_guiNode != NULL)
{
m_guiNode->setSelected(false);
m_guiNode->remove();
m_guiNode = NULL;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ This file is part of the "Skylicht Engine".
#include "Scene/CScene.h"

#include "Editor/SpaceController/CGUIDesignController.h"
#include "Selection/CSelection.h"

namespace Skylicht
{
Expand Down Expand Up @@ -258,7 +259,9 @@ namespace Skylicht
{
CGUIHierachyNode* node = (CGUIHierachyNode*)treeNode->getTagData();
if (node->OnSelected != nullptr)
{
node->OnSelected(node, treeNode->isSelected());
}
}
}

Expand All @@ -278,93 +281,95 @@ namespace Skylicht
}

rowItem->OnAcceptDragDrop = [node](GUI::SDragDropPackage* data)
{
if (data->Name == "GUIHierarchyNode")
{
return true;
}
else if (data->Name == "ListFSItem")
{
GUI::CListRowItem* rowItem = (GUI::CListRowItem*)data->UserData;
bool isFolder = rowItem->getTagBool();
if (isFolder)
return false;

std::string path = rowItem->getTagString();
std::string fileExt = CPath::getFileNameExt(path);
fileExt = CStringImp::toLower(fileExt);
if (fileExt == "png" || fileExt == "tga")
if (data->Name == "GUIHierarchyNode")
{
return true;
}
}
return false;
};

rowItem->OnDragDropHover = [rowItem, node](GUI::SDragDropPackage* data, float mouseX, float mouseY)
{
GUI::SPoint local = rowItem->canvasPosToLocal(GUI::SPoint(mouseX, mouseY));

if (node->getParent() == NULL)
{
// this is canvas node
rowItem->enableDrawLine(false, false);
}
else
{
if (local.Y < rowItem->height() * 0.25f)
{
rowItem->enableDrawLine(true, false);
}
else if (local.Y > rowItem->height() * 0.75f)
{
rowItem->enableDrawLine(false, true);
}
else
else if (data->Name == "ListFSItem")
{
rowItem->enableDrawLine(false, false);
GUI::CListRowItem* rowItem = (GUI::CListRowItem*)data->UserData;
bool isFolder = rowItem->getTagBool();
if (isFolder)
return false;

std::string path = rowItem->getTagString();
std::string fileExt = CPath::getFileNameExt(path);
fileExt = CStringImp::toLower(fileExt);
if (fileExt == "png" || fileExt == "tga")
{
return true;
}
}
}
};

rowItem->OnDragDropOut = [rowItem, node](GUI::SDragDropPackage* data, float mouseX, float mouseY)
{
rowItem->enableDrawLine(false, false);
};
return false;
};

rowItem->OnDrop = [&, editor = m_editor, rowItem, node](GUI::SDragDropPackage* data, float mouseX, float mouseY)
{
if (data->Name == "GUIHierarchyNode")
rowItem->OnDragDropHover = [rowItem, node](GUI::SDragDropPackage* data, float mouseX, float mouseY)
{
CGUIHierachyNode* dragNode = (CGUIHierachyNode*)data->UserData;
GUI::SPoint local = rowItem->canvasPosToLocal(GUI::SPoint(mouseX, mouseY));

if (node->getParent() == NULL)
{
// this is canvas node
moveToChild(dragNode, node);
rowItem->enableDrawLine(false, false);
}
else
{
// this is child node
GUI::SPoint local = rowItem->canvasPosToLocal(GUI::SPoint(mouseX, mouseY));
if (local.Y < rowItem->height() * 0.25f)
{
move(dragNode, node, false);
rowItem->enableDrawLine(true, false);
}
else if (local.Y > rowItem->height() * 0.75f)
{
move(dragNode, node, true);
rowItem->enableDrawLine(false, true);
}
else
{
rowItem->enableDrawLine(false, false);
}
}
};

rowItem->OnDragDropOut = [rowItem, node](GUI::SDragDropPackage* data, float mouseX, float mouseY)
{
rowItem->enableDrawLine(false, false);
};

rowItem->OnDrop = [&, editor = m_editor, rowItem, node](GUI::SDragDropPackage* data, float mouseX, float mouseY)
{
if (data->Name == "GUIHierarchyNode")
{
CGUIHierachyNode* dragNode = (CGUIHierachyNode*)data->UserData;

if (node->getParent() == NULL)
{
// this is canvas node
moveToChild(dragNode, node);
}
else
{
// this is child node
GUI::SPoint local = rowItem->canvasPosToLocal(GUI::SPoint(mouseX, mouseY));
if (local.Y < rowItem->height() * 0.25f)
{
move(dragNode, node, false);
}
else if (local.Y > rowItem->height() * 0.75f)
{
move(dragNode, node, true);
}
else
{
moveToChild(dragNode, node);
}
}

dragNode->getGUINode()->setSelected(true);
}
}

rowItem->enableDrawLine(false, false);
editor->refresh();
};
rowItem->enableDrawLine(false, false);
editor->refresh();
};
}

void CGUIHierarchyController::move(CGUIHierachyNode* from, CGUIHierachyNode* target, bool behind)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ namespace Skylicht
{
if (m_guiNode != NULL)
{
m_guiNode->setSelected(false);
m_guiNode->remove();
m_guiNode = NULL;
}
Expand Down
Loading

0 comments on commit e370fc5

Please sign in to comment.