TANXL_CONSOLE_LIST.H VERSION_1_4
在完成了游戏存档模块的开发之后,我试图用之前制作的Console_List头文件为基础,制作一个命令行上的数据管理器,但是在实际使用中我发现了Console_List的各种不足。比如提供了接口之后还需要用户输入大量的代码来完成表格的输出,明显不能做到让接口容易被正确使用,不易被误用。于是在这个背景下,我对Console_List进行了完全重写,在比前几代代码减少2/3的情况下,功能大幅加强,同时只需要一次物品初始化,一次调用显示函数,明显更加容易使用。
#ifndef TANXL_CONSOLE_LIST
#define TANXL_CONSOLE_LIST
#ifndef TANXL_LIST
#define TANXL_LIST
#define LIST 1
#endif
#endif
#if LIST
#ifndef IOSTREAM
#define IOSTREAM
#include <iostream>
#endif
#ifndef VECTOR
#define VECTOR
#include <vector>
#endif
#ifndef CONIO_H
#define CONIO_H
#include <conio.h>
#endif
#ifndef IOMANIP
#define IOMANIP
#include <iomanip>
#endif
void Col(unsigned ColN = NULL, bool Under_Line = false)
{
if (ColN == NULL)std::cout << "\033[0m";
else
{
if (Under_Line == true)
std::cout << "\033[7m";
std::cout << "\033[4;1;m";
if (((ColN & 0xf0) >> 4) > 0 && ((ColN & 0xf0) >> 4) <= 7)
std::cout << "\033[3" << ((ColN & 0xf0) >> 4) << "m";
else if ((ColN & 0xf0) >> 4 == 0);
else
std::cout << "\033[3" << rand() % 7 + 1 << "m";
if ((ColN & 0x0f) > 0 && ((ColN & 0x0f) <= 7))
std::cout << "\033[4" << (ColN & 0x0f) << "m";
else if ((ColN & 0x0f) == 0);
else
std::cout << "\033[4" << rand() % 7 + 1 << "m";
}
}
struct Function
{
Function() :Name("NULL"), Func(NULL) {};
Function(std::string N, void(*F)()) :Name(N), Func(F) {};
std::string Name;
void(*Func)();
unsigned SSpace{ 0x171109 };
};
struct Main_Function
{
void Page_Info() { Col(); std::cout << "This: " << Selector << " - Total: " << SonList.size() << " - Max: " << (SSpace & 0x0000ff) << std::endl; }
Main_Function(std::string N = "NULL") :Name(N), SonList(NULL) {};
Main_Function(std::string N, void(*F)()) :Name(N), SonList(NULL), Func(F) {};
int Selector{ 0 };
bool Is_Selected{ false };
std::string Name;
void(*Func)() { NULL };
unsigned SSpace{ 0x171109 };
std::vector<Function>SonList;
};
class CONSOLE_LIST
{
public:
void Display();
void Append_Item(std::string New_Item) { SonList.push_back(Main_Function(New_Item)); };
void Append_Svec(int At, std::string Name, void(*Func)()) { SonList.at(At).SonList.push_back(Function(Name, Func)); };
private:
void Page_Info() { Col(); std::cout << "This: " << Selector << " - Total: " << SonList.size() << " - Max: " << (SSpace & 0x0000ff) << std::endl; }
bool Insert_Action(int* Action_Num, bool* Action_Bol, size_t List_Size);
int Selector{ 0 };
int Pages{ 0 };
bool Is_Selected{ false };
std::vector<Main_Function>SonList;
unsigned SSpace{ 0x171109 };
};
void CONSOLE_LIST::Display()
{
Col();
bool flag{ true };
bool brea{ false };
while (true)
{
if (!flag)
{
Is_Selected = false;
flag = true;
continue;
}
system("cls");
int* Num{ &Selector };
bool* Bol{ &Is_Selected };
size_t Siz{ SonList.size() };
for (int i = 0; i < SonList.size(); i++)
{
if (i == Selector)
Col(0x71);
else
Col(0x17);
std::cout << std::setw((SSpace & 0xff0000) >> 16) << SonList.at(i).Name << std::setw((SSpace & 0x00ff00) >> 8) << " " << std::endl;
if (i == Selector && Is_Selected == true)
{
Pages = SonList.at(i).Selector / (SSpace & 0x0000ff);
for (int j = Pages * (SSpace & 0x0000ff), count = 0; j < SonList.at(i).SonList.size() && count < (SSpace & 0x0000ff); j++&& count++)
{
if (j == SonList.at(i).Selector)
Col(0x71);
else
Col(0x17);
std::cout << std::setw((SSpace & 0xff0000) >> 16) << SonList.at(i).SonList.at(j).Name << std::setw((SSpace & 0x00ff00) >> 8) << " " << std::endl;
if (SonList.at(i).Is_Selected == true && j == SonList.at(i).Selector)
{
SonList.at(i).SonList.at(j).Func();
SonList.at(i).Is_Selected = false;
brea = true;
break;
}
}
SonList.at(i).Page_Info();
if (brea)
break;
Num = &SonList.at(i).Selector;
Bol = &SonList.at(i).Is_Selected;
Siz = SonList.at(i).SonList.size();
}
std::cout << std::endl;
}
Page_Info();
if (brea)
{
brea = false;
continue;
}
Col();
flag = Insert_Action(Num, Bol, Siz);
}
}
bool CONSOLE_LIST::Insert_Action(int* Action_Num, bool* Action_Bol, size_t List_Size)
{
char key = _getch();
if (key == 'c' || key == 'C')
{
*Action_Bol = false;
return false;
}
if (static_cast<int>(key - 48) >= 0 && static_cast<int>(key - 48) <= 9)
{
if (static_cast<int>(key - 48) <= List_Size)
*Action_Num = static_cast<int>(key - 48) - 1;
else
*Action_Num = static_cast<int>(List_Size) - 1;
*Action_Bol = true;
}
else if (key == 'w' || key == 'W' || key == 72)
*Action_Num = *Action_Num == 0 ? static_cast<int>(List_Size) - 1 : -- * Action_Num;
else if (key == 's' || key == 'S' || key == 80)
*Action_Num = *Action_Num == static_cast<int>(List_Size) - 1 ? 0 : ++ * Action_Num;
else if (key == 'a' || key == 'A' || key == 75)
*Action_Num = *Action_Num - static_cast<int>(SSpace & 0x0000ff) < 0 ? 0 : *Action_Num - (SSpace & 0x0000ff);
else if (key == 'd' || key == 'D' || key == 77)
*Action_Num = *Action_Num + (SSpace & 0x0000ff) > static_cast<int>(List_Size) - 1 ? static_cast<int>(List_Size) - 1 : *Action_Num + (SSpace & 0x0000ff);
else if (key == '\r')
*Action_Bol = true;
return true;
}
#endif
以下为使用例:
Main.cpp
#include "TANXL_CONSOLE_LIST.h"
void say()
{
Col();
system("cls");
std::cout << "wdnmd";
system("pause");
}
int main()
{
CONSOLE_LIST TSM;
TSM.Append_Item("数据管理");
TSM.Append_Svec(0, "数据管理1", &say);
TSM.Append_Svec(0, "数据管理2", &say);
TSM.Append_Svec(0, "数据管理3", &say);
TSM.Append_Svec(0, "数据管理4", &say);
TSM.Append_Svec(0, "数据管理5", &say);
TSM.Append_Svec(0, "数据管理6", &say);
TSM.Append_Svec(0, "数据管理7", &say);
TSM.Append_Svec(0, "数据管理8", &say);
TSM.Append_Svec(0, "数据管理9", &say);
TSM.Append_Svec(0, "数据管理10", &say);
TSM.Append_Svec(0, "数据管理11", &say);
TSM.Append_Svec(0, "数据管理12", &say);
TSM.Append_Svec(0, "数据管理13", &say);
TSM.Append_Svec(0, "数据管理14", &say);
TSM.Append_Svec(0, "数据管理15", &say);
TSM.Append_Item("添加数据");
TSM.Append_Svec(1, "添加数据1", &say);
TSM.Append_Svec(1, "添加数据2", &say);
TSM.Append_Svec(1, "添加数据3", &say);
TSM.Append_Item("查看数据");
TSM.Append_Svec(2, "查看数据1", &say);
TSM.Append_Svec(2, "查看数据2", &say);
TSM.Append_Svec(2, "查看数据3", &say);
TSM.Append_Item("修改数据");
TSM.Append_Svec(3, "修改数据1", &say);
TSM.Append_Svec(3, "修改数据2", &say);
TSM.Append_Svec(3, "修改数据3", &say);
TSM.Append_Item("退出程序");
TSM.Append_Svec(4, "_-确认-_", &say);
TSM.Append_Svec(4, "_-取消-_", &say);
TSM.Display();
}
|