This repository has been archived on 2021-11-25. You can view files and clone it, but cannot push or open issues or pull requests.
ConsoleUI-Editor/main.cpp
2017-04-13 15:09:04 +08:00

267 lines
4.9 KiB
C++

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <algorithm>
#include <conio.h>
using namespace std;
void ClearScreen()
{
system("cls");
}
char GetUserInput()
{
fflush(stdin);
return getch();
}
string GetUserLine()
{
char buff[1024];
memset(buff,0,1024);
fflush(stdin);
gets(buff);
return string(buff);
}
string projectname;
struct SelectionInfo
{
};
struct PageInfo
{
string title;
vector<string> contextline;
};
vector<PageInfo*> pagevec;
#define CASE_Q case 'Q':case 'q'
#define CASE_W case 'W':case 'w'
#define CASE_E case 'E':case 'e'
#define CASE_A case 'A':case 'a'
#define CASE_S case 'S':case 's'
#define CASE_D case 'D':case 'd'
#define CASE_Z case 'Z':case 'z'
#define CASE_X case 'X':case 'x'
#define CASE_C case 'C':case 'c'
void EditorSetPageTitle(PageInfo* p)
{
ClearScreen();
printf("请输入新的标题...\n");
string newtitle=GetUserLine();
if(newtitle.empty())
{
printf("你输入了空内容,这表明:\n"
"Q 清空标题\n"
"W 不改变标题\n");
switch(GetUserInput())
{
CASE_Q:
p->title.clear();
break;
CASE_W:
return;
}
}
else
{
p->title=newtitle;
}
}
void EditorSetPageContext(PageInfo* p)
{
ClearScreen();
printf("请输入页面内容,将#放在单独一行表示输入结束.\n");
vector<string> newcontextline;
string temp;
while(1)
{
temp=GetUserLine();
if(temp.size()==1&&temp.at(0)=='#') break;
newcontextline.push_back(temp);
}
if(newcontextline.empty())
{
printf("你输入了空内容,这表明:\n"
"Q 清空内容\n"
"W 不改变内容\n");
switch(GetUserInput())
{
CASE_Q:
p->contextline.clear();
break;
CASE_W:
return;
}
}
else
{
p->contextline=newcontextline;
}
}
void EditorPagePreview(PageInfo* p)
{
ClearScreen();
printf("%s\n",p->title.c_str());
for(auto& str:p->contextline)
{
printf("%s\n",str.c_str());
}
printf("\n\n=====================\n");
printf("按下任意键返回\n");
GetUserInput();
}
void EditorEditPage(PageInfo* p)
{
while(1)
{
ClearScreen();
printf("项目 - %s - 创建新页面\n",projectname.c_str());
printf("选项\n"
"Q 设置标题\n"
"W 设置内容\n"
"E 添加按钮\n"
"A 查看按钮列表\n"
"S 删除此页面\n"
"D 浏览此页面\n"
"Z 返回\n"
);
switch(GetUserInput())
{
CASE_Q:
EditorSetPageTitle(p);
break;
CASE_W:
EditorSetPageContext(p);
break;
CASE_D:
EditorPagePreview(p);
break;
CASE_Z:
return;
}
}
}
void EditorViewPages()
{
while(1)
{
ClearScreen();
printf("======\n页面序号 标题\n======\n");
size_t sz=pagevec.size();
for(size_t i=0;i<sz;i++)
{
printf("%u ",i);
if(pagevec.at(i)->title.empty())
{
printf("(无标题)\n");
}
else
{
printf("%s\n",pagevec.at(i)->title.c_str());
}
}
printf("-------\n");
printf("Q 删除页面 W 返回\n");
switch(GetUserInput())
{
CASE_Q:
break;
CASE_W:
return;
}
}
}
PageInfo* EditorCreateNewPage()
{
PageInfo* pinfo=new PageInfo;
pagevec.push_back(pinfo);
return pinfo;
}
void EditorNewProject()
{
ClearScreen();
printf("请输入项目标题,留空返回...\n");
string name=GetUserLine();
if(name.empty()) return;
projectname=name;
while(1)
{
ClearScreen();
printf("项目 - %s \n",projectname.c_str());
printf("选项\n"
"Q 创建新的页面\n"
"W 浏览已有页面\n"
"E 关闭项目\n"
);
switch(GetUserInput())
{
case 'Q':
case 'q':
EditorEditPage(EditorCreateNewPage());
break;
case 'W':
case 'w':
EditorViewPages();
break;
case 'E':
case 'e':
return;
}
}
}
void EditorMain()
{
bool running=true;
while(running)
{
ClearScreen();
printf("选项\n"
"Q 新建工程\n"
"W 退出Editor\n"
);
char c=GetUserInput();
switch(c)
{
case 'Q':
case 'q':
EditorNewProject();
break;
case 'W':
case 'w':
running=false;
break;
}
}
}
int main()
{
printf("启动ConsoleUI Editor...\n");
EditorMain();
printf("关闭ConsoleUI Editor...\n");
return 0;
}