ConsoleMenu/ConsoleMenu.cpp

96 lines
2.2 KiB
C++
Raw Normal View History

2017-05-14 23:18:26 +08:00
#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();
}