first version
This commit is contained in:
commit
e1f71c3d46
6
.gitignore
vendored
Normal file
6
.gitignore
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
/bin
|
||||
/obj
|
||||
*.cbp
|
||||
*.depend
|
||||
*.layout
|
||||
|
95
ConsoleMenu.cpp
Normal file
95
ConsoleMenu.cpp
Normal file
|
@ -0,0 +1,95 @@
|
|||
#include "ConsoleMenu.h"
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <Windows.h>
|
||||
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;i<menus.size();i++){
|
||||
if(menuID == menus[i]->menuID){
|
||||
defaultMenu = menuID;
|
||||
return;
|
||||
}
|
||||
}
|
||||
//Case not found...
|
||||
std::cout<<"invaild menuID: "<<menuID<<std::endl;
|
||||
system("pause");
|
||||
if(activeMenu!=nullptr){ //If possible,refresh active UI...
|
||||
activeMenu->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;i<menus.size();i++){
|
||||
if(menuID == menus[i]->menuID){
|
||||
activeMenu = menus[i];
|
||||
menus[i]->DrawMenu();
|
||||
return;
|
||||
}
|
||||
}
|
||||
//Case not Found...
|
||||
cout<<"invaild menuID: "<<menuID<<endl;
|
||||
system("pause");
|
||||
if(activeMenu!=nullptr){ //If possible,redraw active UI...
|
||||
activeMenu->DrawMenu();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void menuBase::DrawMenu(){
|
||||
|
||||
system("cls");
|
||||
|
||||
//Draw title text...
|
||||
SetConsoleTextAttribute(handle, titleTextTheme);
|
||||
cout<<titleText<<endl;
|
||||
SetConsoleTextAttribute(handle, normalTextTheme);
|
||||
cout<<cutline<<endl;
|
||||
|
||||
//Output before menu..
|
||||
beginOutPut();
|
||||
|
||||
//Draw options...
|
||||
for(int i = 0; i < numOfStatus; i++){
|
||||
if(i == highLightedIndex){
|
||||
SetConsoleTextAttribute(handle,highLightedTextTheme);
|
||||
cout<<i+1<<". "<<options[i]<<endl;
|
||||
SetConsoleTextAttribute(handle,normalTextTheme);
|
||||
}else{
|
||||
cout<<i+1<<". "<<options[i]<<endl;
|
||||
}
|
||||
}
|
||||
|
||||
//Draw helping info...
|
||||
if(numOfStatus!=0){
|
||||
std::cout<<cutline<<std::endl;
|
||||
std::cout<<infos[highLightedIndex]<<std::endl;
|
||||
}
|
||||
|
||||
//Out put after Draw...
|
||||
endOutPut();
|
||||
}
|
54
ConsoleMenu.h
Normal file
54
ConsoleMenu.h
Normal file
|
@ -0,0 +1,54 @@
|
|||
#ifndef __CONSOLEMENU_H
|
||||
#define __CONSOLEMENU_H
|
||||
|
||||
#include <Windows.h>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
|
||||
class menuBase{
|
||||
public:
|
||||
static std::string cutline;
|
||||
std::string titleText; //title
|
||||
std::vector<std::string> options; //options
|
||||
std::vector<std::string> 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<menuBase*> 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
|
46
main.cpp
Normal file
46
main.cpp
Normal file
|
@ -0,0 +1,46 @@
|
|||
#include "ConsoleMenu.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <cstdlib>
|
||||
|
||||
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");
|
||||
}
|
Loading…
Reference in New Issue
Block a user