commit e1f71c3d4615aaecb82add311b26fbc23b920bb2 Author: yamiYori <496071360@qq.com> Date: Sun May 14 23:18:26 2017 +0800 first version diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1bf71c5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +/bin +/obj +*.cbp +*.depend +*.layout + diff --git a/ConsoleMenu.cpp b/ConsoleMenu.cpp new file mode 100644 index 0000000..648ea8b --- /dev/null +++ b/ConsoleMenu.cpp @@ -0,0 +1,95 @@ +#include "ConsoleMenu.h" +#include +#include +#include +#include +#include +using namespace std; + +//color theme +static WORD titleTextTheme = BACKGROUND_BLUE|BACKGROUND_GREEN; //Cyan background with Black text +static WORD highLightedTextTheme = BACKGROUND_RED|BACKGROUND_GREEN; //Yellow background with Black text +static WORD normalTextTheme = FOREGROUND_INTENSITY; //Black background with White text +//console handle +static HANDLE handle; + +string menuBase::cutline = "====================================="; +menuManager* menuManager::instance = nullptr; + +menuManager::menuManager(){ + handle = GetStdHandle(STD_OUTPUT_HANDLE); //Get console handle... + menus.clear(); +} + +void menuManager::SetDefaultUI(string menuID){ + for(unsigned i=0;imenuID){ + defaultMenu = menuID; + return; + } + } + //Case not found... + std::cout<<"invaild menuID: "<DrawMenu(); + } +} + + +void menuManager::UIRegistion(menuBase* pmenu){ + pmenu->numOfStatus = pmenu->options.size(); + pmenu->highLightedIndex = 0; + menus.push_back(pmenu); +} + +void menuManager::UITransform(string menuID){ + for(unsigned i=0;imenuID){ + activeMenu = menus[i]; + menus[i]->DrawMenu(); + return; + } + } + //Case not Found... + cout<<"invaild menuID: "<DrawMenu(); + } +} + + +void menuBase::DrawMenu(){ + + system("cls"); + + //Draw title text... + SetConsoleTextAttribute(handle, titleTextTheme); + cout< +#include +#include + + +class menuBase{ +public: + static std::string cutline; + std::string titleText; //title + std::vector options; //options + std::vector infos; //help info + std::string menuID; //Must be unique. + + int numOfStatus; + int highLightedIndex; //from 0 to numOfStatus + + virtual void DrawMenu(); + virtual void StatusMove(int status) = 0; + virtual void beginOutPut(){}; + virtual void endOutPut(){}; + +}; + +class menuManager{ + +public: + std::vector menus; //menus + menuBase* activeMenu; //current menu + std::string defaultMenu = ""; +private: + menuManager(); + static menuManager *instance; + +//API +public: + void SetDefaultUI(std::string menuID); + void UIRegistion(menuBase* pmenu); + void UITransform(std::string menuID); + + static menuManager* (GetInstance()){ + if(instance == nullptr){ + instance = new menuManager(); + if(instance == nullptr){exit(1);} //alloc failed + } + return instance; + } +}; + + + +#endif diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..e5dfa9f --- /dev/null +++ b/main.cpp @@ -0,0 +1,46 @@ +#include "ConsoleMenu.h" +#include +#include +#include + +class myMenu:public menuBase{ +public: + myMenu(){ + titleText="my test menu"; + options.clear(); + options.push_back("item1"); + options.push_back("item2"); + options.push_back("item3"); + options.push_back("item4"); + + infos.clear(); + infos.push_back("info1"); + infos.push_back("info2"); + infos.push_back("info3"); + infos.push_back("info4"); + + menuID = "myMenu"; + } + + void StatusMove(int status){ + switch(status){ + case 0: + case 1: + case 2: + case 3: + default: + break; + } + } + +}; + +menuManager* MenuCon; + +int main(){ + MenuCon = menuManager::GetInstance(); + MenuCon->UIRegistion(new myMenu()); + + MenuCon->UITransform("myMenu"); + system("pause"); +}