From f82bccb73d6c8ee9dee19405cd743aece263a103 Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Tue, 7 Nov 2017 23:32:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0EMenu=E8=8F=9C=E5=8D=95?= =?UTF-8?q?=E7=B1=BB=EF=BC=9B=E4=BF=AE=E5=A4=8D=E4=BA=86EFileUtils?= =?UTF-8?q?=E4=B8=AD=E5=8F=82=E6=95=B0=E4=B8=8EEString=E4=B8=8D=E5=85=BC?= =?UTF-8?q?=E5=AE=B9=E7=9A=84=E9=97=AE=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Easy2D/Easy2D.vcxproj | 1 + Easy2D/Easy2D.vcxproj.filters | 3 ++ Easy2D/Node/EMenu.cpp | 69 +++++++++++++++++++++++++++++++++++ Easy2D/Tool/EFileUtils.cpp | 30 +++++++-------- Easy2D/enodes.h | 41 +++++++++++++++++++++ Easy2D/etools.h | 20 +++++----- 6 files changed, 139 insertions(+), 25 deletions(-) create mode 100644 Easy2D/Node/EMenu.cpp diff --git a/Easy2D/Easy2D.vcxproj b/Easy2D/Easy2D.vcxproj index 1482d9df..6923e458 100644 --- a/Easy2D/Easy2D.vcxproj +++ b/Easy2D/Easy2D.vcxproj @@ -236,6 +236,7 @@ + diff --git a/Easy2D/Easy2D.vcxproj.filters b/Easy2D/Easy2D.vcxproj.filters index b613d558..12ee4a2e 100644 --- a/Easy2D/Easy2D.vcxproj.filters +++ b/Easy2D/Easy2D.vcxproj.filters @@ -210,6 +210,9 @@ Node + + Node + diff --git a/Easy2D/Node/EMenu.cpp b/Easy2D/Node/EMenu.cpp new file mode 100644 index 00000000..bcdab0d8 --- /dev/null +++ b/Easy2D/Node/EMenu.cpp @@ -0,0 +1,69 @@ +#include "..\enodes.h" + +e2d::EMenu::EMenu() + : m_bEnable(true) +{ +} + +e2d::EMenu::EMenu(int number, EButton * button1, ...) + : EMenu() +{ + EButton ** ppButton = &button1; + + while (number > 0) + { + this->addButton(*ppButton); + ppButton++; + number--; + } +} + +bool e2d::EMenu::isEnable() const +{ + return m_bEnable; +} + +size_t e2d::EMenu::getButtonCount() const +{ + return m_vButtons.size(); +} + +void e2d::EMenu::setEnable(bool enable) +{ + m_bEnable = enable; + + for (auto &buttons : m_vButtons) + { + buttons->setEnable(enable); + } +} + +void e2d::EMenu::addButton(EButton * button) +{ + this->addChild(button); + m_vButtons.push_back(button); +} + +bool e2d::EMenu::removeButton(EButton * button) +{ + if (m_vButtons.empty()) + { + return false; + } + + this->removeChild(button); + + if (button) + { + size_t size = m_vButtons.size(); + for (size_t i = 0; i < size; i++) + { + if (m_vButtons[i] == button) + { + m_vButtons.erase(m_vButtons.begin() + i); + return true; + } + } + } + return false; +} diff --git a/Easy2D/Tool/EFileUtils.cpp b/Easy2D/Tool/EFileUtils.cpp index 660eb761..94a573bd 100644 --- a/Easy2D/Tool/EFileUtils.cpp +++ b/Easy2D/Tool/EFileUtils.cpp @@ -66,37 +66,37 @@ e2d::EString e2d::EFileUtils::getDefaultSavePath() return path; } -void e2d::EFileUtils::saveInt(LPCTSTR key, int value) +void e2d::EFileUtils::saveInt(const EString & key, int value) { - ::WritePrivateProfileString(L"Default", key, std::to_wstring(value).c_str(), getDefaultSavePath().c_str()); + ::WritePrivateProfileString(L"Default", key.c_str(), std::to_wstring(value).c_str(), getDefaultSavePath().c_str()); } -void e2d::EFileUtils::saveFloat(LPCTSTR key, float value) +void e2d::EFileUtils::saveFloat(const EString & key, float value) { - ::WritePrivateProfileString(L"Default", key, std::to_wstring(value).c_str(), getDefaultSavePath().c_str()); + ::WritePrivateProfileString(L"Default", key.c_str(), std::to_wstring(value).c_str(), getDefaultSavePath().c_str()); } -void e2d::EFileUtils::saveString(LPCTSTR key, LPCTSTR value) +void e2d::EFileUtils::saveString(const EString & key, const EString & value) { - ::WritePrivateProfileString(L"Default", key, value, getDefaultSavePath().c_str()); + ::WritePrivateProfileString(L"Default", key.c_str(), value.c_str(), getDefaultSavePath().c_str()); } -int e2d::EFileUtils::getInt(LPCTSTR key, int default) +int e2d::EFileUtils::getInt(const EString & key, int default) { - return ::GetPrivateProfileInt(L"Default", key, default, getDefaultSavePath().c_str()); + return ::GetPrivateProfileInt(L"Default", key.c_str(), default, getDefaultSavePath().c_str()); } -float e2d::EFileUtils::getFloat(LPCTSTR key, float default) +float e2d::EFileUtils::getFloat(const EString & key, float default) { TCHAR temp[32] = { 0 }; - ::GetPrivateProfileString(L"Default", key, std::to_wstring(default).c_str(), temp, 31, getDefaultSavePath().c_str()); + ::GetPrivateProfileString(L"Default", key.c_str(), std::to_wstring(default).c_str(), temp, 31, getDefaultSavePath().c_str()); return std::stof(temp); } -e2d::EString e2d::EFileUtils::geTString(LPCTSTR key, LPCTSTR default) +e2d::EString e2d::EFileUtils::geTString(const EString & key, const EString & default) { TCHAR temp[256] = { 0 }; - ::GetPrivateProfileString(L"Default", key, default, temp, 255, getDefaultSavePath().c_str()); + ::GetPrivateProfileString(L"Default", key.c_str(), default.c_str(), temp, 255, getDefaultSavePath().c_str()); return EString(temp); } @@ -117,7 +117,7 @@ e2d::EString e2d::EFileUtils::getFileExtension(const EString & filePath) return fileExtension; } -e2d::EString e2d::EFileUtils::getSaveFilePath(LPCTSTR title, LPCTSTR defExt) +e2d::EString e2d::EFileUtils::getSaveFilePath(const EString & title, const EString & defExt) { // 弹出保存对话框 OPENFILENAME ofn = { 0 }; @@ -130,8 +130,8 @@ e2d::EString e2d::EFileUtils::getSaveFilePath(LPCTSTR title, LPCTSTR defExt) ofn.nMaxFile = sizeof(strFilename); // 缓冲区长度 ofn.lpstrInitialDir = NULL; // 初始目录为默认 ofn.Flags = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;// 目录必须存在,覆盖文件前发出警告 - ofn.lpstrTitle = title; // 使用系统默认标题留空即可 - ofn.lpstrDefExt = defExt; // 默认追加的扩展名 + ofn.lpstrTitle = title.c_str(); // 使用系统默认标题留空即可 + ofn.lpstrDefExt = defExt.c_str(); // 默认追加的扩展名 if (GetSaveFileName(&ofn)) { diff --git a/Easy2D/enodes.h b/Easy2D/enodes.h index d1f0d657..23418aa3 100644 --- a/Easy2D/enodes.h +++ b/Easy2D/enodes.h @@ -782,4 +782,45 @@ protected: bool m_bToggle; }; + +class EMenu : + public ENode +{ +public: + // 创建空菜单 + EMenu(); + + // 创建菜单 + EMenu( + int number, /* 菜单中按钮的数量 */ + EButton * button1, /* 第一个按钮 */ + ... + ); + + // 获取菜单是否禁用 + bool isEnable() const; + + // 获取菜单中的按钮数量 + size_t getButtonCount() const; + + // 设置菜单启用或禁用 + void setEnable( + bool enable + ); + + // 添加按钮 + void addButton( + EButton * button + ); + + // 移除按钮 + bool removeButton( + EButton * button + ); + +protected: + bool m_bEnable; + EVector m_vButtons; +}; + } \ No newline at end of file diff --git a/Easy2D/etools.h b/Easy2D/etools.h index 64b5e082..51e904e1 100644 --- a/Easy2D/etools.h +++ b/Easy2D/etools.h @@ -113,38 +113,38 @@ public: // 保存 int 型的值 static void saveInt( - LPCTSTR key, + const EString & key, int value ); // 保存 float 型的值 static void saveFloat( - LPCTSTR key, + const EString & key, float value ); // 保存 字符串 型的值(不要在 Unicode 字符集下保存中文字符) static void saveString( - LPCTSTR key, - LPCTSTR value + const EString & key, + const EString & value ); // 获取 int 型的值(若不存在则返回 default 参数的值) static int getInt( - LPCTSTR key, + const EString & key, int default ); // 获取 float 型的值(若不存在则返回 default 参数的值) static float getFloat( - LPCTSTR key, + const EString & key, float default ); // 获取 字符串 型的值(若不存在则返回 default 参数的值) static EString geTString( - LPCTSTR key, - LPCTSTR default + const EString & key, + const EString & default ); // 得到文件扩展名(小写) @@ -157,8 +157,8 @@ public: * 参数:返回文件路径的字符串,窗口标题,设置扩展名过滤,设置默认扩展名 */ static EString getSaveFilePath( - LPCTSTR title = L"保存到", - LPCTSTR defExt = NULL + const EString & title = L"保存到", + const EString & defExt = NULL ); };