commit 2abe6733bd05abd92a07b24e07089afb2f3bc9e1 Author: kiritow <1362050620@qq.com> Date: Wed Apr 12 17:17:38 2017 +0800 Initial Commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f6f63c7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +bin/ +obj/ + diff --git a/CUI.cpp b/CUI.cpp new file mode 100644 index 0000000..b5d37fa --- /dev/null +++ b/CUI.cpp @@ -0,0 +1,279 @@ +#include "CUI.h" +#include + +Selection::Selection() +{ + id=-1; +} + +//virtual +void Selection::drawText() +{ + printf("%s",text.c_str()); +} + +//virtual +void Selection::drawInfo() +{ + printf("%s",info.c_str()); +} + +bool Selection::hasInfo() +{ + return !info.empty(); +} + +//virtual +int Selection::onClick() +{ + return 0; +} + +Page::Page() +{ + +} + +Page::~Page() +{ + +} + +//virtual +void Page::draw() +{ + if(!title.empty()) + { + printf("%s\n",title.c_str()); + } + if(!text.empty()) + { + printf("%s\n",text.c_str()); + } + + int id=1; + for(std::list::iterator iter=_vec.begin();iter!=_vec.end();iter++) + { + printf("%d ",id); + (*iter)->drawText(); + if((*iter)->hasInfo()) + { + printf(" ("); + (*iter)->drawInfo(); + printf(")"); + } + printf("\n"); + ++id; + } + printf("\n"); +} + +//virtual +void Page::onLoad() +{ + +} + +//virtual +int Page::onUnload() +{ + return 0; +} + +//virtual +void Page::onForeground() +{ + +} + +//virtual +void Page::onBackground() +{ + +} + +int Page::getSelectionSize() +{ + return _vec.size(); +} + +int Page::onActive(int id) +{ + int left=id; + std::list::iterator iter=_vec.begin(); + while(left>0) + { + ++iter; + --left; + } + return (*iter)->onClick(); +} + +void Page::setFrame(Frame* f) +{ + _pframe=f; +} + +Frame* Page::getFrame() +{ + return _pframe; +} + +void Page::add(Selection* p) +{ + _vec.push_back(p); +} + +Frame::Frame() +{ + _page=nullptr; + _cpage=nullptr; + _npage=nullptr; +} + +Frame::~Frame() +{ + +} + +//protected +void Frame::clearScreen() +{ + system("cls"); +} + +//protected +void Frame::clearInput() +{ + fflush(stdin); +} + +//protected +int Frame::onInvalidInput() +{ + clearInput(); + printf("Invalid Input.\n"); + return 0; +} + +void Frame::setPage(Page* p) +{ + _page=p; + p->setFrame(this); +} + +Page* Frame::getPage() +{ + return _page; +} + +void Frame::jumpTo(Page* p) +{ + _npage=p; +} + +void Frame::run() +{ + /// No Page + if(_page==nullptr) return; + + bool stop=false; + _cpage=_page; + + _cpage->onLoad(); + + while(!stop) + { + clearScreen(); + _cpage->draw(); + if(_cpage->getSelectionSize()>0) + { + int uinput; + while(scanf("%d%*c",&uinput)!=1) + { + if(onInvalidInput()!=0) + { + stop=true; + break; + } + } + if(stop) continue; + + if(uinput<1||uinput>_cpage->getSelectionSize()) + { + if(onInvalidInput()!=0) + { + stop=true; + continue; + } + } + + int ret=_cpage->onActive(uinput-1); + switch(ret) + { + case 2: + stop=true; + break; + case 1: + { + if(_stk.empty()) + { + stop=true; + } + else + { + /// Unload Current Page + if(_cpage->onUnload()==0) + { + delete _cpage; + } + _cpage=_stk.top(); + _stk.pop(); + _cpage->onForeground(); + } + } + break; + case 0: + { + if(_npage!=nullptr) + { + /// New Page. + _cpage->onBackground(); + _stk.push(_cpage); + _cpage=_npage; + _npage=nullptr; + _cpage->onLoad(); + } + else + { + /// continue running. + } + } + break; + } + } + else + { + /// No Selection on page... + stop=true; + } + } + + if(_cpage->onUnload()==0) + { + delete _cpage; + } + _cpage=nullptr; + + /// Unload all inactive pages. + while(!_stk.empty()) + { + _cpage=_stk.top(); + _stk.pop(); + if(_cpage->onUnload()==0) + { + delete _cpage; + } + _cpage=nullptr; + } +} diff --git a/CUI.h b/CUI.h new file mode 100644 index 0000000..feee006 --- /dev/null +++ b/CUI.h @@ -0,0 +1,87 @@ +#pragma once +#include +#include +#include + +/// Fwd DECL +class Frame; + +class Selection +{ +public: + Selection(); + int id; + std::string text; + std::string info; + + virtual void drawText(); + virtual void drawInfo(); + bool hasInfo(); + Frame* getFrame(); + void setFrame(); + + /// return 2 to exit. + /// return 1 to return last page.(If no page to return, exit the program) + /// return 0 to continue running.(If you want to change page, you should return 0) + virtual int onClick(); +private: + Frame* _pframe; +}; + +class Page +{ +public: + Page(); + virtual ~Page(); + std::string title; + std::string text; + + void add(Selection*); + int del(Selection*); + int getSelectionSize(); + + Frame* getFrame(); + void setFrame(Frame*); + + virtual void draw(); + + virtual void onLoad(); + virtual void onForeground(); + virtual void onBackground(); + /// return 0 tell frame to delete page, return 1 tell frame not to delete + virtual int onUnload(); + + virtual int onActive(int id); +private: + std::list _vec; + Frame* _pframe; +}; + +class Frame +{ +public: + Frame(); + ~Frame(); + void run(); + Page* getPage(); + void setPage(Page*); + + void jumpTo(Page*); + +protected: + void clearScreen(); + void clearInput(); + + /// return 1 if error cannot be solve. return 0 on solved problem. + int onInvalidInput(); +private: + /// First Page. + Page* _page; + /// Current Processing Page (Internal Variable) + Page* _cpage; + /// Next Page to be processed (Internal Variable) + Page* _npage; + /// Inactive Page Stack (Internal Variable) + std::stack _stk; +}; +