Skip to content

Commit

Permalink
#119 Add asset context menu
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamhong committed Mar 9, 2021
1 parent 5972b4b commit 307db43
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 25 deletions.
100 changes: 97 additions & 3 deletions Projects/Editor/Source/Editor/Space/Assets/CSpaceAssets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,19 @@ This file is part of the "Skylicht Engine".

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

namespace Skylicht
{
namespace Editor
{
CSpaceAssets::CSpaceAssets(GUI::CWindow* window, CEditor* editor) :
CSpace(window, editor),
m_renameNode(NULL)
m_renameNode(NULL),
m_contextMenuOwner(NULL),
m_selectedItem(NULL)
{
GUI::CToolbar* toolbar = new GUI::CToolbar(window);

Expand Down Expand Up @@ -95,6 +100,21 @@ namespace Skylicht

spliter->setColWidth(0, 300.0f);
spliter->setWeakCol(1);

m_contextMenu = new GUI::CMenu(window->getCanvas());
m_contextMenu->setHidden(true);
m_contextMenu->OnCommand = BIND_LISTENER(&CSpaceAssets::OnCommand, this);

m_openMenuItem = 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_listFiles->addAccelerator("SHIFT + C", BIND_LISTENER(&CSpaceAssets::OnCopyPath, this));
m_folder->addAccelerator("SHIFT + C", BIND_LISTENER(&CSpaceAssets::OnCopyPath, this));
}

CSpaceAssets::~CSpaceAssets()
Expand Down Expand Up @@ -264,7 +284,11 @@ namespace Skylicht
GUI::CTreeNode* node = rowItem->getNode();
if (node != NULL)
{
os::Printer::log(node->getTagString().c_str());
m_contextMenuOwner = node->getRoot();
m_selectedItem = node;
m_selectedPath = node->getTagString();
m_openMenuItem->setHidden(true);
m_contextMenu->open(GUI::CInput::getInput()->getMousePosition());
}
}
}
Expand All @@ -274,7 +298,11 @@ namespace Skylicht
GUI::CListRowItem* rowItem = dynamic_cast<GUI::CListRowItem*>(row);
if (rowItem != NULL)
{
os::Printer::log(rowItem->getTagString().c_str());
m_contextMenuOwner = rowItem->getListBox();
m_selectedItem = rowItem;
m_selectedPath = rowItem->getTagString();
m_openMenuItem->setHidden(false);
m_contextMenu->open(GUI::CInput::getInput()->getMousePosition());
}
}

Expand Down Expand Up @@ -339,5 +367,71 @@ namespace Skylicht
{
m_listFiles->focus();
}

void CSpaceAssets::OnCommand(GUI::CBase* item)
{
GUI::CMenuItem* menuItem = dynamic_cast<GUI::CMenuItem*>(item);
const std::wstring& label = menuItem->getLabel();
if (label == L"Open")
{
if (m_contextMenuOwner == m_listFiles)
OnFileOpen(m_selectedItem);
}
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 CSpaceAssets::OnCopyPath(GUI::CBase* item)
{
wchar_t* text = NULL;

if (m_listFiles->isFocussed())
{
GUI::CListRowItem* row = m_listFiles->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_folder->isFocussed())
{
GUI::CTreeNode* node = m_folder->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;
}
}
}
}
14 changes: 14 additions & 0 deletions Projects/Editor/Source/Editor/Space/Assets/CSpaceAssets.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ namespace Skylicht

GUI::CListRowItem* m_renameItem;

GUI::CMenuItem* m_openMenuItem;

GUI::CMenu* m_contextMenu;

GUI::CBase* m_contextMenuOwner;

GUI::CBase* m_selectedItem;

std::string m_selectedPath;

CAssetManager* m_assetManager;

public:
Expand Down Expand Up @@ -78,6 +88,10 @@ namespace Skylicht

void OnListCancelRename(GUI::CBase* control);

void OnCommand(GUI::CBase* item);

void OnCopyPath(GUI::CBase* item);

protected:

void expandTreeFolder(const std::string& folder);
Expand Down
3 changes: 3 additions & 0 deletions Projects/Editor/Source/GUI/Controls/CButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ namespace Skylicht
m_pressed = true;
m_icon->setColor(CThemeConfig::IconPressColor);
m_label->setColor(CThemeConfig::TextPressColor);

if (OnDown != nullptr)
OnDown(this);
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion Projects/Editor/Source/GUI/Controls/CListRowItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ namespace Skylicht

CButton::onMouseClickRight(x, y, down);

if (down != NULL)
if (down == false)
{
CListBox* listBox = dynamic_cast<CListBox*>(m_owner);
if (listBox->OnItemContextMenu != nullptr)
Expand Down
39 changes: 21 additions & 18 deletions Projects/Editor/Source/GUI/Controls/CMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace Skylicht
{
namespace GUI
{
CMenu::CMenu(CBase *parent) :
CMenu::CMenu(CBase* parent) :
CBase(parent),
m_isOpenSubMenu(false),
m_isOpenPopup(false)
Expand All @@ -53,8 +53,11 @@ namespace Skylicht
float menuHeight = 5.0f;
bool firstMenu = true;

for (CBase *menuItem : Children)
for (CBase* menuItem : Children)
{
if (menuItem->isHidden())
continue;

if (firstMenu == true)
{
SMargin margin = menuItem->getMargin();
Expand Down Expand Up @@ -100,8 +103,8 @@ namespace Skylicht
std::vector<std::wstring> result;
CStringImp::splitString(path.c_str(), L"/", result);

CMenu *menu = this;
CMenuItem *item = NULL;
CMenu* menu = this;
CMenuItem* item = NULL;

while (result.size() > 0)
{
Expand Down Expand Up @@ -133,7 +136,7 @@ namespace Skylicht
{
for (CBase* child : Children)
{
CMenuItem *item = dynamic_cast<CMenuItem*>(child);
CMenuItem* item = dynamic_cast<CMenuItem*>(child);
if (item != NULL && item->getLabel() == label)
{
return item;
Expand Down Expand Up @@ -162,8 +165,8 @@ namespace Skylicht
std::vector<std::wstring> result;
CStringImp::splitString(path.c_str(), L"/", result);

CMenu *menu = this;
CMenuItem *item = NULL;
CMenu* menu = this;
CMenuItem* item = NULL;

while (result.size() > 0)
{
Expand Down Expand Up @@ -197,7 +200,7 @@ namespace Skylicht

CMenuItem* CMenu::addItem(ESystemIcon icon)
{
CMenuItem *iconItem = addItem(std::wstring(L""), icon, std::wstring(L""));
CMenuItem* iconItem = addItem(std::wstring(L""), icon, std::wstring(L""));
iconItem->setPadding(GUI::SPadding(6.0f, 0.0f, 0.0f, 0.0f));
iconItem->setIconMargin(GUI::SMargin(-2.0f, 0.0f, 0.0f, 0.0f));
iconItem->sizeToContents();
Expand All @@ -221,7 +224,7 @@ namespace Skylicht

CMenuItem* CMenu::addItem(const std::wstring& label, ESystemIcon icon, const std::wstring& accelerator)
{
CMenuItem *item = new CMenuItem(this);
CMenuItem* item = new CMenuItem(this);
item->setLabel(label);
item->setIcon(icon);
item->showIcon(true);
Expand All @@ -236,12 +239,12 @@ namespace Skylicht

CMenuSeparator* CMenu::addSeparator()
{
CMenuSeparator *item = new CMenuSeparator(this);
CMenuSeparator* item = new CMenuSeparator(this);
item->dock(EPosition::Top);
return item;
}

void CMenu::onAddItem(CMenuItem *item)
void CMenu::onAddItem(CMenuItem* item)
{
item->dock(EPosition::Top);
item->sizeToContents();
Expand All @@ -250,18 +253,18 @@ namespace Skylicht
setSize(w, height());
}

void CMenu::onMenuItemHover(CBase *item)
void CMenu::onMenuItemHover(CBase* item)
{
CMenuItem *menuItem = dynamic_cast<CMenuItem*>(item);
CMenuItem* menuItem = dynamic_cast<CMenuItem*>(item);
if (menuItem != NULL)
{
openMenu(menuItem);
}
}

void CMenu::onMenuItemPress(CBase *item)
void CMenu::onMenuItemPress(CBase* item)
{
CMenuItem *menuItem = dynamic_cast<CMenuItem*>(item);
CMenuItem* menuItem = dynamic_cast<CMenuItem*>(item);
if (menuItem != NULL)
{
if (menuItem->haveSubMenu() == false)
Expand Down Expand Up @@ -296,7 +299,7 @@ namespace Skylicht
}
}

void CMenu::openMenu(CMenuItem *menuItem)
void CMenu::openMenu(CMenuItem* menuItem)
{
if (menuItem->isOpenMenu())
return;
Expand All @@ -315,9 +318,9 @@ namespace Skylicht

void CMenu::closeChildMenu()
{
for (CBase *child : Children)
for (CBase* child : Children)
{
CMenuItem *menuItem = dynamic_cast<CMenuItem*>(child);
CMenuItem* menuItem = dynamic_cast<CMenuItem*>(child);
if (menuItem != NULL)
menuItem->closeMenu();
}
Expand Down
2 changes: 2 additions & 0 deletions Projects/Editor/Source/GUI/Controls/CTreeControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ namespace Skylicht
node->deselectAll();
}
}

invalidate();
}

bool CTreeControl::onKeyUp(bool down)
Expand Down
5 changes: 5 additions & 0 deletions Projects/Editor/Source/GUI/Controls/CTreeNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ namespace Skylicht
return m_textEditHelper;
}

inline CTreeNode* getRoot()
{
return m_root;
}

CTreeNode* addNode(const std::wstring& text);

CTreeNode* addNode(const std::wstring& text, ESystemIcon icon);
Expand Down
5 changes: 3 additions & 2 deletions Projects/Editor/Source/GUI/Input/CInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,10 +402,11 @@ namespace Skylicht
// Build the accelerator search string
std::string accelString;

if (isAltDown())
accelString += "ALT + ";

if (IsControlDown())
{
accelString += "CTRL + ";
}

if (IsShiftDown())
accelString += "SHIFT + ";
Expand Down
5 changes: 5 additions & 0 deletions Projects/Editor/Source/GUI/Input/CInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ namespace Skylicht
return isKeyDown(EKey::KEY_CONTROL);
}

inline bool isAltDown()
{
return isKeyDown(EKey::KEY_MENU);
}

virtual void update();

bool inputMouseMoved(float x, float y, float deltaX, float deltaY);
Expand Down
2 changes: 1 addition & 1 deletion Projects/Editor/Source/GUI/Input/CSkylichtInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ namespace Skylicht
inputKeyEvent(EKey::KEY_MENU, event.KeyInput.PressedDown);
break;
default:
if (event.KeyInput.Char != 0 && !isKeyDown(EKey::KEY_MENU) && event.KeyInput.PressedDown)
if (event.KeyInput.Char != 0 && /*!isKeyDown(EKey::KEY_MENU) &&*/ event.KeyInput.PressedDown)
inputCharacter(event.KeyInput.Char);
else
inputKeyEvent((EKey)event.KeyInput.Key, event.KeyInput.PressedDown);
Expand Down

0 comments on commit 307db43

Please sign in to comment.