From 00cda1ba30187b43f7c95fecf1d2caa2f3eed705 Mon Sep 17 00:00:00 2001 From: ducphamhong Date: Mon, 8 Mar 2021 13:10:10 +0700 Subject: [PATCH] #119 Update rename assets (GUI) --- .../Editor/Space/Assets/CSpaceAssets.cpp | 80 ++++++++++++++++++- .../Source/Editor/Space/Assets/CSpaceAssets.h | 16 ++++ .../Editor/Source/GUI/Controls/CListBox.cpp | 14 ++++ .../Editor/Source/GUI/Controls/CListBox.h | 6 +- .../Source/GUI/Controls/CListRowItem.cpp | 4 +- .../Editor/Source/GUI/Controls/CListRowItem.h | 8 ++ .../Source/GUI/Controls/CTextEditHelper.cpp | 12 +++ .../Source/GUI/Controls/CTextEditHelper.h | 2 + 8 files changed, 138 insertions(+), 4 deletions(-) diff --git a/Projects/Editor/Source/Editor/Space/Assets/CSpaceAssets.cpp b/Projects/Editor/Source/Editor/Space/Assets/CSpaceAssets.cpp index 5192988e2..25d647a4d 100644 --- a/Projects/Editor/Source/Editor/Space/Assets/CSpaceAssets.cpp +++ b/Projects/Editor/Source/Editor/Space/Assets/CSpaceAssets.cpp @@ -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); @@ -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(); @@ -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); @@ -235,5 +251,67 @@ namespace Skylicht } } } + + void CSpaceAssets::OnTreeNodeKeyPress(GUI::CBase* control, int key, bool press) + { + GUI::CTreeControl* tree = dynamic_cast(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(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(); + } } } \ No newline at end of file diff --git a/Projects/Editor/Source/Editor/Space/Assets/CSpaceAssets.h b/Projects/Editor/Source/Editor/Space/Assets/CSpaceAssets.h index 896a7533f..e0ffb3c05 100644 --- a/Projects/Editor/Source/Editor/Space/Assets/CSpaceAssets.h +++ b/Projects/Editor/Source/Editor/Space/Assets/CSpaceAssets.h @@ -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: @@ -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); diff --git a/Projects/Editor/Source/GUI/Controls/CListBox.cpp b/Projects/Editor/Source/GUI/Controls/CListBox.cpp index 106ab7e22..388d7fc11 100644 --- a/Projects/Editor/Source/GUI/Controls/CListBox.cpp +++ b/Projects/Editor/Source/GUI/Controls/CListBox.cpp @@ -135,6 +135,20 @@ namespace Skylicht } } + CListRowItem* CListBox::getSelected() + { + for (CBase* child : m_innerPanel->Children) + { + CListRowItem* item = dynamic_cast(child); + if (item != NULL && item->getToggle()) + { + return item; + } + } + + return NULL; + } + void CListBox::unSelectAll() { for (CBase* child : m_innerPanel->Children) diff --git a/Projects/Editor/Source/GUI/Controls/CListBox.h b/Projects/Editor/Source/GUI/Controls/CListBox.h index 10df70495..f51b83a7c 100644 --- a/Projects/Editor/Source/GUI/Controls/CListBox.h +++ b/Projects/Editor/Source/GUI/Controls/CListBox.h @@ -38,7 +38,7 @@ namespace Skylicht class CListBox : public CScrollControl { public: - CListBox(CBase *parent); + CListBox(CBase* parent); virtual ~CListBox(); @@ -52,6 +52,8 @@ namespace Skylicht CListRowItem* getItemByTagValue(int value); + CListRowItem* getSelected(); + void unSelectAll(); void removeAllItem(); @@ -73,7 +75,7 @@ namespace Skylicht protected: - virtual void onItemDown(CBase *item); + virtual void onItemDown(CBase* item); }; } diff --git a/Projects/Editor/Source/GUI/Controls/CListRowItem.cpp b/Projects/Editor/Source/GUI/Controls/CListRowItem.cpp index ce406a1b4..bbe3ae17b 100644 --- a/Projects/Editor/Source/GUI/Controls/CListRowItem.cpp +++ b/Projects/Editor/Source/GUI/Controls/CListRowItem.cpp @@ -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() diff --git a/Projects/Editor/Source/GUI/Controls/CListRowItem.h b/Projects/Editor/Source/GUI/Controls/CListRowItem.h index f39f0c442..12bbdaee3 100644 --- a/Projects/Editor/Source/GUI/Controls/CListRowItem.h +++ b/Projects/Editor/Source/GUI/Controls/CListRowItem.h @@ -26,6 +26,7 @@ This file is part of the "Skylicht Engine". #include "CBase.h" #include "CButton.h" +#include "CTextEditHelper.h" namespace Skylicht { @@ -38,6 +39,8 @@ namespace Skylicht protected: CBase* m_owner; + CTextEditHelper* m_textEditHelper; + public: CListRowItem(CBase* base); @@ -52,6 +55,11 @@ namespace Skylicht virtual void renderBackground(); virtual void onMouseClickRight(float x, float y, bool down); + + inline CTextEditHelper* getTextEditHelper() + { + return m_textEditHelper; + } }; } } diff --git a/Projects/Editor/Source/GUI/Controls/CTextEditHelper.cpp b/Projects/Editor/Source/GUI/Controls/CTextEditHelper.cpp index fee8ca313..cb66949c9 100644 --- a/Projects/Editor/Source/GUI/Controls/CTextEditHelper.cpp +++ b/Projects/Editor/Source/GUI/Controls/CTextEditHelper.cpp @@ -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); } @@ -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(); diff --git a/Projects/Editor/Source/GUI/Controls/CTextEditHelper.h b/Projects/Editor/Source/GUI/Controls/CTextEditHelper.h index f2196efda..15f5b8f0a 100644 --- a/Projects/Editor/Source/GUI/Controls/CTextEditHelper.h +++ b/Projects/Editor/Source/GUI/Controls/CTextEditHelper.h @@ -45,6 +45,8 @@ namespace Skylicht std::wstring m_oldValue; + bool m_end; + public: CTextEditHelper(CBase* base, CTextBox* textBox, CTextContainer* textContainer);