Skip to content

Commit

Permalink
#119 Refactor File System Context Menu
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamhong committed Mar 10, 2021
1 parent 5975c4a commit 3865083
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 145 deletions.
2 changes: 1 addition & 1 deletion Projects/Editor/Source/Editor/CEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ namespace Skylicht
void CEditor::initSessionLayout(const std::string& data)
{
io::IFileSystem* fs = getIrrlichtDevice()->getFileSystem();
io::IReadFile* file = fs->createMemoryReadFile(data.c_str(), data.length(), "data");
io::IReadFile* file = fs->createMemoryReadFile(data.c_str(), (s32)data.length(), "data");
io::IXMLReader* xmlRead = fs->createXMLReader(file);
if (xmlRead == NULL)
return;
Expand Down
160 changes: 160 additions & 0 deletions Projects/Editor/Source/Editor/Space/Assets/CContextMenuFS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
!@
MIT License
CopyRight (c) 2020 Skylicht Technology CO., LTD
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the Rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRight HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This file is part of the "Skylicht Engine".
https://github.com/skylicht-lab/skylicht-engine
!#
*/

#include "pch.h"
#include "CContextMenuFS.h"
#include "Utils/CStringImp.h"
#include "GUI/Input/CInput.h"
#include "GUI/Clipboard/CClipboard.h"

namespace Skylicht
{
namespace Editor
{
CContextMenuFS::CContextMenuFS(GUI::CCanvas* canvas, GUI::CTreeControl* tree, GUI::CListBox* list, CListFSController* listFSController) :
m_treeFS(tree),
m_listFS(list),
m_listFSController(listFSController)
{
m_contextMenu = new GUI::CMenu(canvas);
m_contextMenu->setHidden(true);
m_contextMenu->OnCommand = BIND_LISTENER(&CContextMenuFS::OnCommand, this);

m_open = m_contextMenu->addItem(L"Open");
m_contextMenu->addItem(L"Show in Explorer");
m_contextMenu->addSeparator();
m_contextMenu->addItem(L"Delete");
m_contextMenu->addItem(L"Rename", L"F2");
m_contextMenu->addItem(L"Copy path", L"SHIFT + C");
m_contextMenu->addItem(L"Duplicate", L"CTRL + D");

m_treeFS->addAccelerator("SHIFT + C", BIND_LISTENER(&CContextMenuFS::OnCopyPath, this));
m_listFS->addAccelerator("SHIFT + C", BIND_LISTENER(&CContextMenuFS::OnCopyPath, this));

m_treeFS->OnItemContextMenu = BIND_LISTENER(&CContextMenuFS::OnTreeContextMenu, this);
m_listFS->OnItemContextMenu = BIND_LISTENER(&CContextMenuFS::OnListContextMenu, this);
}

CContextMenuFS::~CContextMenuFS()
{

}

void CContextMenuFS::OnTreeContextMenu(GUI::CBase* row)
{
GUI::CTreeRowItem* rowItem = dynamic_cast<GUI::CTreeRowItem*>(row);
if (rowItem != NULL)
{
GUI::CTreeNode* node = rowItem->getNode();
if (node != NULL)
{
m_ownerControl = node->getRoot();
m_selected = node;
m_selectedPath = node->getTagString();
m_open->setHidden(true);
m_contextMenu->open(GUI::CInput::getInput()->getMousePosition());
}
}
}

void CContextMenuFS::OnListContextMenu(GUI::CBase* row)
{
GUI::CListRowItem* rowItem = dynamic_cast<GUI::CListRowItem*>(row);
if (rowItem != NULL)
{
m_ownerControl = rowItem->getListBox();
m_selected = rowItem;
m_selectedPath = rowItem->getTagString();
m_open->setHidden(false);
m_contextMenu->open(GUI::CInput::getInput()->getMousePosition());
}
}

void CContextMenuFS::OnCommand(GUI::CBase* item)
{
GUI::CMenuItem* menuItem = dynamic_cast<GUI::CMenuItem*>(item);
const std::wstring& label = menuItem->getLabel();
if (label == L"Open")
{
if (m_ownerControl == m_listFS)
m_listFSController->OnFileOpen(m_selected);
}
else if (label == L"Show in Explorer")
{

}
else if (label == L"Delete")
{

}
else if (label == L"Rename")
{

}
else if (label == L"Copy path")
{
wchar_t* text = new wchar_t[m_selectedPath.size() + 1];
CStringImp::convertUTF8ToUnicode(m_selectedPath.c_str(), text);
GUI::CClipboard::get()->copyTextToClipboard(text);
delete[]text;
}
else if (label == L"Duplicate")
{

}
}

void CContextMenuFS::OnCopyPath(GUI::CBase* item)
{
wchar_t* text = NULL;

if (m_listFS->isFocussed())
{
GUI::CListRowItem* row = m_listFS->getSelected();
std::string path = row->getTagString();
if (path.empty() == false)
{
text = new wchar_t[path.size() + 1];
CStringImp::convertUTF8ToUnicode(path.c_str(), text);
}
}
else if (m_treeFS->isFocussed())
{
GUI::CTreeNode* node = m_treeFS->getChildSelected();
std::string path = node->getTagString();
if (path.empty() == false)
{
text = new wchar_t[path.size() + 1];
CStringImp::convertUTF8ToUnicode(path.c_str(), text);
}
}

if (text != NULL)
{
GUI::CClipboard::get()->copyTextToClipboard(text);
delete[]text;
}
}
}
}
68 changes: 68 additions & 0 deletions Projects/Editor/Source/Editor/Space/Assets/CContextMenuFS.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
!@
MIT License
CopyRight (c) 2020 Skylicht Technology CO., LTD
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the Rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRight HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This file is part of the "Skylicht Engine".
https://github.com/skylicht-lab/skylicht-engine
!#
*/

#pragma once

#include "GUI/GUI.h"
#include "CListFSController.h"
#include "AssetManager/CAssetManager.h"

namespace Skylicht
{
namespace Editor
{
class CContextMenuFS
{
protected:
GUI::CTreeControl* m_treeFS;

GUI::CListBox* m_listFS;

CListFSController* m_listFSController;

GUI::CMenu* m_contextMenu;

GUI::CMenuItem* m_open;

GUI::CBase* m_ownerControl;

GUI::CBase* m_selected;

std::string m_selectedPath;

public:
CContextMenuFS(GUI::CCanvas* canvas, GUI::CTreeControl* tree, GUI::CListBox* list, CListFSController* listFSController);

virtual ~CContextMenuFS();

void OnTreeContextMenu(GUI::CBase* row);

void OnListContextMenu(GUI::CBase* row);

void OnCommand(GUI::CBase* item);

void OnCopyPath(GUI::CBase* item);
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ namespace Skylicht
{
namespace Editor
{
CListFSController::CListFSController(GUI::CListBox* list) :
m_renameItem(NULL)
CListFSController::CListFSController(GUI::CListBox* list, CTreeFSController* treeController) :
m_renameItem(NULL),
m_treeController(treeController)
{
m_assetManager = CAssetManager::getInstance();

Expand Down Expand Up @@ -133,7 +134,7 @@ namespace Skylicht

add(fullPath, files);

// expandTreeFolder(fullPath);
m_treeController->expand(fullPath);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ This file is part of the "Skylicht Engine".

#include "GUI/GUI.h"
#include "AssetManager/CAssetManager.h"
#include "CTreeFSController.h"

namespace Skylicht
{
Expand All @@ -38,10 +39,12 @@ namespace Skylicht

GUI::CListRowItem* m_renameItem;

CTreeFSController* m_treeController;

CAssetManager* m_assetManager;

public:
CListFSController(GUI::CListBox* list);
CListFSController(GUI::CListBox* list, CTreeFSController* treeController);

virtual ~CListFSController();

Expand Down
Loading

0 comments on commit 3865083

Please sign in to comment.