Skip to content

Commit

Permalink
#119 Update rename assets (GUI)
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamhong committed Mar 8, 2021
1 parent c496603 commit 00cda1b
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 4 deletions.
80 changes: 79 additions & 1 deletion Projects/Editor/Source/Editor/Space/Assets/CSpaceAssets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ namespace Skylicht
namespace Editor
{
CSpaceAssets::CSpaceAssets(GUI::CWindow* window, CEditor* editor) :
CSpace(window, editor)
CSpace(window, editor),
m_renameNode(NULL)
{
GUI::CToolbar* toolbar = new GUI::CToolbar(window);

Expand All @@ -48,6 +49,13 @@ namespace Skylicht
spliter->setNumberRowCol(1, 2);

m_folder = new GUI::CTreeControl(spliter);
m_folder->OnKeyPress = std::bind(
&CSpaceAssets::OnTreeNodeKeyPress,
this,
std::placeholders::_1,
std::placeholders::_2,
std::placeholders::_3
);

m_assetManager = CAssetManager::getInstance();

Expand All @@ -69,6 +77,14 @@ namespace Skylicht
spliter->setControl(m_folder, 0, 0);

m_listFiles = new GUI::CListBox(spliter);
m_listFiles->OnKeyPress = std::bind(
&CSpaceAssets::OnListKeyPress,
this,
std::placeholders::_1,
std::placeholders::_2,
std::placeholders::_3
);

spliter->setControl(m_listFiles, 0, 1);

addListFolder("", files);
Expand Down Expand Up @@ -235,5 +251,67 @@ namespace Skylicht
}
}
}

void CSpaceAssets::OnTreeNodeKeyPress(GUI::CBase* control, int key, bool press)
{
GUI::CTreeControl* tree = dynamic_cast<GUI::CTreeControl*>(control);
if (tree == NULL)
return;

if (key == GUI::KEY_F2)
{
GUI::CTreeNode* node = tree->getChildSelected();
if (node != NULL)
{
m_renameNode = node;

node->getTextEditHelper()->beginEdit(
BIND_LISTENER(&CSpaceAssets::OnTreeRename, this),
BIND_LISTENER(&CSpaceAssets::OnTreeCancelRename, this)
);
}
}
}

void CSpaceAssets::OnListKeyPress(GUI::CBase* control, int key, bool press)
{
GUI::CListBox* list = dynamic_cast<GUI::CListBox*>(control);
if (list == NULL)
return;

if (key == GUI::KEY_F2)
{
GUI::CListRowItem* node = list->getSelected();
if (node != NULL)
{
m_renameItem = node;

node->getTextEditHelper()->beginEdit(
BIND_LISTENER(&CSpaceAssets::OnListRename, this),
BIND_LISTENER(&CSpaceAssets::OnListCancelRename, this)
);
}
}
}

void CSpaceAssets::OnTreeRename(GUI::CBase* control)
{
m_folder->focus();
}

void CSpaceAssets::OnTreeCancelRename(GUI::CBase* control)
{
m_folder->focus();
}

void CSpaceAssets::OnListRename(GUI::CBase* control)
{
m_listFiles->focus();
}

void CSpaceAssets::OnListCancelRename(GUI::CBase* control)
{
m_listFiles->focus();
}
}
}
16 changes: 16 additions & 0 deletions Projects/Editor/Source/Editor/Space/Assets/CSpaceAssets.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,14 @@ namespace Skylicht

GUI::CTreeNode* m_root;

GUI::CTreeNode* m_renameNode;

GUI::CListBox* m_listFiles;

GUI::CTextBox* m_search;

GUI::CListRowItem* m_renameItem;

CAssetManager* m_assetManager;

public:
Expand All @@ -58,6 +62,18 @@ namespace Skylicht

void OnFileOpen(GUI::CBase* node);

void OnTreeNodeKeyPress(GUI::CBase* control, int key, bool press);

void OnTreeRename(GUI::CBase* control);

void OnTreeCancelRename(GUI::CBase* control);

void OnListKeyPress(GUI::CBase* control, int key, bool press);

void OnListRename(GUI::CBase* control);

void OnListCancelRename(GUI::CBase* control);

protected:

void expandTreeFolder(const std::string& folder);
Expand Down
14 changes: 14 additions & 0 deletions Projects/Editor/Source/GUI/Controls/CListBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,20 @@ namespace Skylicht
}
}

CListRowItem* CListBox::getSelected()
{
for (CBase* child : m_innerPanel->Children)
{
CListRowItem* item = dynamic_cast<CListRowItem*>(child);
if (item != NULL && item->getToggle())
{
return item;
}
}

return NULL;
}

void CListBox::unSelectAll()
{
for (CBase* child : m_innerPanel->Children)
Expand Down
6 changes: 4 additions & 2 deletions Projects/Editor/Source/GUI/Controls/CListBox.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace Skylicht
class CListBox : public CScrollControl
{
public:
CListBox(CBase *parent);
CListBox(CBase* parent);

virtual ~CListBox();

Expand All @@ -52,6 +52,8 @@ namespace Skylicht

CListRowItem* getItemByTagValue(int value);

CListRowItem* getSelected();

void unSelectAll();

void removeAllItem();
Expand All @@ -73,7 +75,7 @@ namespace Skylicht

protected:

virtual void onItemDown(CBase *item);
virtual void onItemDown(CBase* item);

};
}
Expand Down
4 changes: 3 additions & 1 deletion Projects/Editor/Source/GUI/Controls/CListRowItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ namespace Skylicht
setPadding(SPadding(8.0f, 0.0f, 0.0f, 0.0f));

showIcon(true);

m_textEditHelper = new CTextEditHelper(this, new CTextBox(this), m_label);
}

CListRowItem::~CListRowItem()
{

delete m_textEditHelper;
}

void CListRowItem::layout()
Expand Down
8 changes: 8 additions & 0 deletions Projects/Editor/Source/GUI/Controls/CListRowItem.h
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 "CBase.h"
#include "CButton.h"
#include "CTextEditHelper.h"

namespace Skylicht
{
Expand All @@ -38,6 +39,8 @@ namespace Skylicht
protected:
CBase* m_owner;

CTextEditHelper* m_textEditHelper;

public:
CListRowItem(CBase* base);

Expand All @@ -52,6 +55,11 @@ namespace Skylicht
virtual void renderBackground();

virtual void onMouseClickRight(float x, float y, bool down);

inline CTextEditHelper* getTextEditHelper()
{
return m_textEditHelper;
}
};
}
}
Expand Down
12 changes: 12 additions & 0 deletions Projects/Editor/Source/GUI/Controls/CTextEditHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,21 @@ namespace Skylicht

m_onCancel = onCancel;
m_onEndEdit = onEndEdit;

m_end = false;
}

void CTextEditHelper::cancelEdit()
{
if (m_end == true)
return;

m_textBox->setHidden(true);
m_textContainer->setHidden(false);
m_textContainer->setString(m_oldValue);

m_end = true;

if (m_onCancel != nullptr)
m_onCancel(m_textBox);
}
Expand All @@ -83,12 +90,17 @@ namespace Skylicht
m_textBox->setHidden(true);
m_textContainer->setHidden(false);

m_end = true;

if (m_onEndEdit != nullptr)
m_onEndEdit(m_textBox);
}

void CTextEditHelper::onChar(CBase* textBox)
{
if (m_end == true)
return;

// auto calc text size
m_textContainer->setString(m_textBox->getString());
m_textContainer->layout();
Expand Down
2 changes: 2 additions & 0 deletions Projects/Editor/Source/GUI/Controls/CTextEditHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ namespace Skylicht

std::wstring m_oldValue;

bool m_end;

public:
CTextEditHelper(CBase* base, CTextBox* textBox, CTextContainer* textContainer);

Expand Down

0 comments on commit 00cda1b

Please sign in to comment.