Initial Commit

This commit is contained in:
Kirigaya Kazuto 2017-04-12 17:17:38 +08:00
commit 2abe6733bd
3 changed files with 369 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
bin/
obj/

279
CUI.cpp Normal file
View File

@ -0,0 +1,279 @@
#include "CUI.h"
#include <cstdio>
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<Selection*>::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<Selection*>::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;
}
}

87
CUI.h Normal file
View File

@ -0,0 +1,87 @@
#pragma once
#include <list>
#include <string>
#include <stack>
/// 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<Selection*> _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<Page*> _stk;
};