Try fix linux. Formatted code

This commit is contained in:
Kirigaya Kazuto 2018-05-11 16:19:20 +08:00
parent ba3b18d726
commit eb028441da

View File

@ -1,28 +1,34 @@
#include "ColorUI.h"
#include <Windows.h>
#include <algorithm>
#include <conio.h> // getch
#ifdef _WIN32
#include <Windows.h>
#else
#include <cstdio>
#endif
namespace _cns
{
class _auto_init_console_info_class
{
public:
#ifdef _WIN32 /// Windows Platform
class _auto_init_console_info_class
{
public:
_auto_init_console_info_class()
{
hout=GetStdHandle(STD_OUTPUT_HANDLE);
hout = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO tInfo;
GetConsoleScreenBufferInfo(hout,&tInfo);
color=tInfo.wAttributes;
GetConsoleScreenBufferInfo(hout, &tInfo);
color = tInfo.wAttributes;
}
HANDLE hout;
WORD color;
}_auto_init_console_info_obj;
}_auto_init_console_info_obj;
inline ConsoleColor _winColor2ConsoleColor(int winColor)
{
switch(winColor)
inline ConsoleColor _winColor2ConsoleColor(int winColor)
{
switch (winColor)
{
case 0:
return ConsoleColor::black;
@ -48,11 +54,11 @@ inline ConsoleColor _winColor2ConsoleColor(int winColor)
case BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED:
return ConsoleColor::white;
}
}
}
inline int _ConsoleColor2winForeColor(ConsoleColor conColor)
{
switch(conColor)
inline int _ConsoleColor2winForeColor(ConsoleColor conColor)
{
switch (conColor)
{
case ConsoleColor::black:
return 0;
@ -65,7 +71,7 @@ inline int _ConsoleColor2winForeColor(ConsoleColor conColor)
case ConsoleColor::lightblue:
return FOREGROUND_GREEN | FOREGROUND_BLUE;
case ConsoleColor::purple:
return FOREGROUND_RED |FOREGROUND_BLUE;
return FOREGROUND_RED | FOREGROUND_BLUE;
case ConsoleColor::yellow:
return FOREGROUND_RED | FOREGROUND_GREEN;
case ConsoleColor::white:
@ -73,11 +79,11 @@ inline int _ConsoleColor2winForeColor(ConsoleColor conColor)
default:
return 0;
}
}
}
inline int _ConsoleColor2winBackColor(ConsoleColor conColor)
{
switch(conColor)
inline int _ConsoleColor2winBackColor(ConsoleColor conColor)
{
switch (conColor)
{
case ConsoleColor::black:
return 0;
@ -98,42 +104,53 @@ inline int _ConsoleColor2winBackColor(ConsoleColor conColor)
default:
return 0;
}
}
inline void cprint(ConsoleColor FrontColor,ConsoleColor BackColor,bool withIntensity=true)
{
int iFront=_ConsoleColor2winForeColor(FrontColor);
int iBack=_ConsoleColor2winBackColor(BackColor);
if(withIntensity)
{
if(iFront!=0) iFront=iFront | FOREGROUND_INTENSITY;
if(iBack!=0) iBack=iBack| BACKGROUND_INTENSITY;
}
SetConsoleTextAttribute(_auto_init_console_info_obj.hout,iFront | iBack );
}
inline void cprint()
{
SetConsoleTextAttribute(_auto_init_console_info_obj.hout,_auto_init_console_info_obj.color);
}
int _last_keyval;
int _GetUserInputKey_Real()
{
return _last_keyval=getch();///defined in conio.h
}
enum class KEY { UNDEF=0, UP, DOWN, LEFT, RIGHT, CONFIRM, ESC, HOME, END };
int _last_isSpecial;
KEY GetUserInputKey()
{
int a=_GetUserInputKey_Real();
if(a==224||a==0)
inline void cprint(ConsoleColor FrontColor, ConsoleColor BackColor, bool withIntensity = true)
{
_last_isSpecial=1;
int b=_GetUserInputKey_Real();
switch(b)
int iFront = _ConsoleColor2winForeColor(FrontColor);
int iBack = _ConsoleColor2winBackColor(BackColor);
if (withIntensity)
{
if (iFront != 0) iFront = iFront | FOREGROUND_INTENSITY;
if (iBack != 0) iBack = iBack | BACKGROUND_INTENSITY;
}
SetConsoleTextAttribute(_auto_init_console_info_obj.hout, iFront | iBack);
}
inline void cprint()
{
SetConsoleTextAttribute(_auto_init_console_info_obj.hout, _auto_init_console_info_obj.color);
}
#else /// Linix-like
inline void cprint(ConsoleColor FrontColor, ConsoleColor BackColor, bool withIntensity = true)
{
/// Ignore intensity option?
printf("\033[%d;%dm", (int)FrontColor + 30, (int)BackColor + 40);
}
inline void cprint()
{
printf("\033[0m");
}
#endif /// End of platform
int _last_keyval;
int _GetUserInputKey_Real()
{
return _last_keyval = getch();///defined in conio.h
}
enum class KEY { UNDEF = 0, UP, DOWN, LEFT, RIGHT, CONFIRM, ESC, HOME, END };
int _last_isSpecial;
KEY GetUserInputKey()
{
int a = _GetUserInputKey_Real();
if (a == 224 || a == 0)
{
_last_isSpecial = 1;
int b = _GetUserInputKey_Real();
switch (b)
{
case 72:return KEY::UP;
case 80:return KEY::DOWN;
@ -146,8 +163,8 @@ KEY GetUserInputKey()
}
else
{
_last_isSpecial=0;
switch(a)
_last_isSpecial = 0;
switch (a)
{
case ' ':case '\r':case '\n':case '5':
return KEY::CONFIRM;
@ -166,60 +183,60 @@ KEY GetUserInputKey()
return KEY::UNDEF;
}
}
}
}
int GetAction(int& cid,int MinVal,int MaxVal,int EscapeVal)
{
KEY t=GetUserInputKey();
switch(t)
int GetAction(int& cid, int MinVal, int MaxVal, int EscapeVal)
{
KEY t = GetUserInputKey();
switch (t)
{
case KEY::UP:
{
cid=max(min(cid-1,MaxVal),MinVal);
cid = max(min(cid - 1, MaxVal), MinVal);
return 0;
}
case KEY::DOWN:
{
cid=max(min(cid+1,MaxVal),MinVal);
cid = max(min(cid + 1, MaxVal), MinVal);
return 0;
}
case KEY::ESC:
{
cid=EscapeVal;
cid = EscapeVal;
return 0;
}
case KEY::CONFIRM:
return 1;
case KEY::HOME:
{
cid=MinVal;
cid = MinVal;
return 0;
}
case KEY::END:
{
cid=MaxVal;
cid = MaxVal;
return 0;
}
default:
return 0;
}
}
}
static void ClearInputBuffer()
{
static void ClearInputBuffer()
{
scanf("%*[^\n]%*c");
}
}
}/// End of namespace _cns
ColorSelection::ColorSelection()
{
frontColorNormal=ConsoleColor::white;
backColorNormal=ConsoleColor::black;
frontColorActivate=ConsoleColor::black;
backColorActivate=ConsoleColor::yellow;
_is_active=false;
_pframe=nullptr;
frontColorNormal = ConsoleColor::white;
backColorNormal = ConsoleColor::black;
frontColorActivate = ConsoleColor::black;
backColorActivate = ConsoleColor::yellow;
_is_active = false;
_pframe = nullptr;
}
//virtual
@ -231,22 +248,22 @@ ColorSelection::~ColorSelection()
//virtual
void ColorSelection::drawText()
{
if(_is_active)
if (_is_active)
{
_cns::cprint(frontColorActivate,backColorActivate);
_cns::cprint(frontColorActivate, backColorActivate);
}
else
{
_cns::cprint(frontColorNormal,backColorNormal);
_cns::cprint(frontColorNormal, backColorNormal);
}
printf("%s",text.c_str());
printf("%s", text.c_str());
}
//virtual
void ColorSelection::drawInfo()
{
printf("%s",info.c_str());
printf("%s", info.c_str());
}
bool ColorSelection::hasInfo()
@ -257,13 +274,13 @@ bool ColorSelection::hasInfo()
//virtual
void ColorSelection::onActivate()
{
_is_active=true;
_is_active = true;
}
//virtual
void ColorSelection::onDeActivate()
{
_is_active=false;
_is_active = false;
}
ColorFrame* ColorSelection::getFrame()
@ -273,7 +290,7 @@ ColorFrame* ColorSelection::getFrame()
void ColorSelection::setFrame(ColorFrame* pFrame)
{
_pframe=pFrame;
_pframe = pFrame;
}
//virtual
@ -290,12 +307,12 @@ int ColorSelection::onDelete()
ColorPage::ColorPage()
{
titleFrontColor=ConsoleColor::black;
titleBackColor=ConsoleColor::lightblue;
textFrontColor=ConsoleColor::white;
textBackColor=ConsoleColor::black;
_curActive=0;
_pframe=nullptr;
titleFrontColor = ConsoleColor::black;
titleBackColor = ConsoleColor::lightblue;
textFrontColor = ConsoleColor::white;
textBackColor = ConsoleColor::black;
_curActive = 0;
_pframe = nullptr;
}
//virtual
@ -312,20 +329,20 @@ void ColorPage::add(ColorSelection* p)
int ColorPage::del(ColorSelection* p)
{
auto iter=std::find(_vec.begin(),_vec.end(),p);
if(iter==_vec.end())
auto iter = std::find(_vec.begin(), _vec.end(), p);
if (iter == _vec.end())
{
/// Object to delete is Not Found
return 1;
}
if(_curActive==iter-_vec.begin()+1)
if (_curActive == iter - _vec.begin() + 1)
{
/// Current Activated Selection is to be deleted.
_curActive=0;
_curActive = 0;
/// Delete Selection
if((*iter)->onDelete()==0)
if ((*iter)->onDelete() == 0)
{
(*iter)->setFrame(nullptr);
delete (*iter);
@ -341,10 +358,10 @@ int ColorPage::del(ColorSelection* p)
else
{
/// Get current activated selection.
ColorSelection* _curActive_ptr=_vec.at(_curActive-1);
ColorSelection* _curActive_ptr = _vec.at(_curActive - 1);
/// Delete Target Selection
if((*iter)->onDelete()==0)
if ((*iter)->onDelete() == 0)
{
(*iter)->setFrame(nullptr);
delete (*iter);
@ -358,7 +375,7 @@ int ColorPage::del(ColorSelection* p)
_vec.erase(iter);
/// Re-Calculate active id.
_curActive=std::find(_vec.begin(),_vec.end(),_curActive_ptr)-_vec.begin()+1;
_curActive = std::find(_vec.begin(), _vec.end(), _curActive_ptr) - _vec.begin() + 1;
}
return 0;
@ -382,8 +399,8 @@ ColorFrame* ColorPage::getFrame()
void ColorPage::setFrame(ColorFrame* pFrame)
{
_pframe=pFrame;
for(auto& ptr:_vec)
_pframe = pFrame;
for (auto& ptr : _vec)
{
ptr->setFrame(getFrame());
}
@ -392,27 +409,27 @@ void ColorPage::setFrame(ColorFrame* pFrame)
//virtual
void ColorPage::draw()
{
if(!title.empty())
if (!title.empty())
{
_cns::cprint(titleFrontColor,titleBackColor);
printf("%s\n",title.c_str());
_cns::cprint(titleFrontColor, titleBackColor);
printf("%s\n", title.c_str());
}
if(!text.empty())
if (!text.empty())
{
_cns::cprint(textFrontColor,textBackColor,false);
printf("%s\n",text.c_str());
_cns::cprint(textFrontColor, textBackColor, false);
printf("%s\n", text.c_str());
}
if(!_curActive) // _curActive==0
if (!_curActive) // _curActive==0
{
if(!_vec.empty())
if (!_vec.empty())
{
_curActive=1;
_curActive = 1;
onSelectionOver(_curActive);
}
}
for(auto iter=_vec.begin();iter!=_vec.end();++iter)
for (auto iter = _vec.begin(); iter != _vec.end(); ++iter)
{
if(!(*iter)->text.empty())
if (!(*iter)->text.empty())
{
(*iter)->drawText();
}
@ -425,9 +442,9 @@ void ColorPage::draw()
}
_cns::cprint();
printf("---------------\n");
if(_vec.at(_curActive-1)->hasInfo())
if (_vec.at(_curActive - 1)->hasInfo())
{
_vec.at(_curActive-1)->drawInfo();
_vec.at(_curActive - 1)->drawInfo();
printf("\n");
}
_cns::cprint();
@ -454,30 +471,30 @@ void ColorPage::onForeground()
//virtual
int ColorPage::onUnload()
{
for(auto& ptr:_vec)
for (auto& ptr : _vec)
{
if(!ptr->onDelete())
if (!ptr->onDelete())
{
delete ptr;
}
}
_vec.clear();
_curActive=0;
_curActive = 0;
return 0;
}
//virtual
void ColorPage::onSelectionOver(int id)
{
_vec.at(_curActive-1)->onDeActivate();
_vec.at(id-1)->onActivate();
_curActive=id;
_vec.at(_curActive - 1)->onDeActivate();
_vec.at(id - 1)->onActivate();
_curActive = id;
}
//virtual
int ColorPage::onActive(int id)
{
return _vec.at(id-1)->onClick();
return _vec.at(id - 1)->onClick();
}
//protected
@ -488,11 +505,11 @@ std::vector<ColorSelection*>& ColorPage::_getvec()
ColorFrame::ColorFrame()
{
_home_page=nullptr;
_cur_page=nullptr;
_next_page=nullptr;
_delete_home_page_on_dtor=true;
_has_started=false;
_home_page = nullptr;
_cur_page = nullptr;
_next_page = nullptr;
_delete_home_page_on_dtor = true;
_has_started = false;
}
ColorFrame::~ColorFrame()
@ -500,11 +517,11 @@ ColorFrame::~ColorFrame()
}
void ColorFrame::setHomePage(ColorPage* pPage,bool deleteOnRemove)
void ColorFrame::setHomePage(ColorPage* pPage, bool deleteOnRemove)
{
if(_has_started) return;
_home_page=pPage;
_delete_home_page_on_dtor=deleteOnRemove;
if (_has_started) return;
_home_page = pPage;
_delete_home_page_on_dtor = deleteOnRemove;
pPage->setFrame(this);
}
@ -515,7 +532,7 @@ ColorPage* ColorFrame::getHomePage()
void ColorFrame::jumpTo(ColorPage* nextPage)
{
_next_page=nextPage;
_next_page = nextPage;
}
void ColorFrame::clearScreen()
@ -542,34 +559,34 @@ void ColorFrame::exitInputMode()
void ColorFrame::run()
{
/// A Frame can only be started once.
if(_has_started) return;
if (_has_started) return;
_has_started=true;
_has_started = true;
/// No Home Page
if(_home_page==nullptr) return;
_cur_page=_home_page;
if (_home_page == nullptr) return;
_cur_page = _home_page;
_cur_page->onLoad();
bool stop=false;
int cid=0;
while(!stop)
bool stop = false;
int cid = 0;
while (!stop)
{
clearScreen();
_cur_page->draw();
if(_cur_page->getSelectionSize()>0)
if (_cur_page->getSelectionSize() > 0)
{
cid=_cur_page->getCurrentActive();
int oldcid=cid;
if(_cns::GetAction(cid,1,_cur_page->getSelectionSize(),_cur_page->getSelectionSize()))
cid = _cur_page->getCurrentActive();
int oldcid = cid;
if (_cns::GetAction(cid, 1, _cur_page->getSelectionSize(), _cur_page->getSelectionSize()))
{
/// Confirmed Key Pressed.
switch(_cur_page->onActive(cid))
switch (_cur_page->onActive(cid))
{
case 0:
{
if(_next_page!=nullptr)
if (_next_page != nullptr)
{
/// New Page!
/// Bring Current Page to Background
@ -577,8 +594,8 @@ void ColorFrame::run()
_stk.push(_cur_page);
/// Load New Page
_cur_page=_next_page;
_next_page=nullptr;
_cur_page = _next_page;
_next_page = nullptr;
_cur_page->setFrame(this);
_cur_page->onLoad();
}
@ -591,7 +608,7 @@ void ColorFrame::run()
case 1:
{
/// Unload Current Page
if(_cur_page->onUnload()==0)
if (_cur_page->onUnload() == 0)
{
_cur_page->setFrame(nullptr);
delete _cur_page;
@ -600,16 +617,16 @@ void ColorFrame::run()
{
_cur_page->setFrame(nullptr);
}
_cur_page=nullptr;
_cur_page = nullptr;
/// Bring Previous Page to Foreground.
if(_stk.empty())
if (_stk.empty())
{
stop=true;
stop = true;
}
else
{
_cur_page=_stk.top();
_cur_page = _stk.top();
_stk.pop();
_cur_page->onForeground();
}
@ -617,14 +634,14 @@ void ColorFrame::run()
break;
case 2:
default:
stop=true;
stop = true;
break;
}
}
else
{
/// If not confirmed , just go ahead.
if(oldcid!=cid)
if (oldcid != cid)
{
_cur_page->onSelectionOver(cid);
}
@ -633,7 +650,7 @@ void ColorFrame::run()
else
{
/// Unload Current Page
if(_cur_page->onUnload()==0)
if (_cur_page->onUnload() == 0)
{
_cur_page->setFrame(nullptr);
delete _cur_page;
@ -642,25 +659,25 @@ void ColorFrame::run()
{
_cur_page->setFrame(nullptr);
}
_cur_page=nullptr;
_cur_page = nullptr;
/// Bring Previous Page to Foreground.
if(_stk.empty())
if (_stk.empty())
{
stop=true;
stop = true;
}
else
{
_cur_page=_stk.top();
_cur_page = _stk.top();
_stk.pop();
_cur_page->onForeground();
}
}
}
if(_cur_page)
if (_cur_page)
{
if(_cur_page->onUnload()==0)
if (_cur_page->onUnload() == 0)
{
_cur_page->setFrame(nullptr);
delete _cur_page;
@ -669,13 +686,13 @@ void ColorFrame::run()
{
_cur_page->setFrame(nullptr);
}
_cur_page=nullptr;
_cur_page = nullptr;
}
while(!_stk.empty())
while (!_stk.empty())
{
_cur_page=_stk.top();
_cur_page = _stk.top();
_cur_page->onForeground();
if(_cur_page->onUnload()==0)
if (_cur_page->onUnload() == 0)
{
_cur_page->setFrame(nullptr);
delete _cur_page;
@ -685,13 +702,13 @@ void ColorFrame::run()
_cur_page->setFrame(nullptr);
}
}
_cur_page=nullptr;
_cur_page = nullptr;
}
ColorInputModeGuard::ColorInputModeGuard(ColorFrame* f)
{
_pframe=f;
if(_pframe)
_pframe = f;
if (_pframe)
{
_pframe->enterInputMode();
}
@ -699,7 +716,7 @@ ColorInputModeGuard::ColorInputModeGuard(ColorFrame* f)
ColorInputModeGuard::~ColorInputModeGuard()
{
if(_pframe)
if (_pframe)
{
_pframe->exitInputMode();
}