-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f936e01
commit 92a336e
Showing
6 changed files
with
292 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#pragma once | ||
#include<vector> | ||
#include "component.h" | ||
|
||
class basicImage : public component | ||
{ | ||
public: | ||
basicImage(const std::vector<cgui::string>& image); | ||
int getWidth() const override; | ||
int getHeight() const override; | ||
std::vector<cgui::string> getData() const override; | ||
|
||
void setImage(const std::vector<cgui::string>& image); | ||
|
||
private: | ||
std::vector<cgui::string> image; | ||
int width; | ||
void calculateWidth(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#pragma once | ||
#include "component.h" | ||
|
||
class basicText : public component | ||
{ | ||
public: | ||
basicText(std::string_view str); | ||
int getWidth() const override; | ||
int getHeight() const override; | ||
std::vector<cgui::string> getData() const override; | ||
|
||
void setText(std::string_view str); | ||
|
||
private: | ||
cgui::string text; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#pragma once | ||
#include<iostream> | ||
#include<string> | ||
using namespace std; | ||
|
||
class basicthing | ||
{ | ||
public: | ||
basicthing(string data); | ||
~basicthing(); | ||
string selfDraw(); | ||
private: | ||
string data; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#pragma once | ||
#include"basicImage.h" | ||
#include"basicProgressBar.h" | ||
#include"basicText.h" | ||
#include"multiLineText.h" | ||
#include <map> | ||
#include <memory> | ||
#include <string> | ||
#include <vector> | ||
|
||
struct logicPos { | ||
int row, col; | ||
}; | ||
static bool operator<(logicPos lhs, logicPos rhs) { | ||
return (lhs.row < rhs.row) || (lhs.row == rhs.row && lhs.col < rhs.col); | ||
} | ||
static bool operator==(logicPos lhs, logicPos rhs) { | ||
return (lhs.row == rhs.row) && (lhs.col == rhs.col); | ||
} | ||
|
||
class page // : public component | ||
{ | ||
public: | ||
page(bool enableSelect); | ||
int getWeight() const; | ||
int getHeight() const; | ||
std::vector<cgui::string> getData() const; | ||
|
||
// 把page中的内容格式化为字符串 | ||
std::string toString(); | ||
|
||
// 打印page | ||
void update(); | ||
|
||
// 把某个控件放到... | ||
void setTo(logicPos pos, std::shared_ptr<component> src); | ||
|
||
// 删除某个控件 | ||
void erase(logicPos pos); | ||
|
||
// 清空控件 | ||
void clear(); | ||
|
||
// 启用控件选择 | ||
void setEnableSelect(bool v); | ||
|
||
// 选择控件 | ||
void select(logicPos pos); | ||
|
||
private: | ||
std::map<logicPos, std::shared_ptr<component>> components; | ||
std::map<int, int> lineWidthList; | ||
std::map<int, int> lineHeightList; | ||
|
||
// select要怎么设计比较好? | ||
logicPos selectedPos = { 0, 0 }; | ||
bool enableSelect; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
#include "string.h" | ||
#include <regex> | ||
|
||
using namespace cgui; | ||
|
||
static const std::string defaultColor = "\033[0m"; | ||
|
||
string::string() {} | ||
string::string(const char* in) : str(in) { calculateVisibleLength(); } | ||
string::string(std::string_view in) : str(in) { calculateVisibleLength(); } | ||
string::string(int count, char c) : str(count, c) { calculateVisibleLength(); } | ||
|
||
size_t string::size() const { return str.size(); } | ||
int string::length() const { return visibleLength; } | ||
|
||
void string::insert(int pos, const string& other) { | ||
str.insert(pos, other.str); | ||
visibleLength += other.visibleLength; | ||
} | ||
void string::insert(int pos, int count, char c) { | ||
str.insert(pos, count, c); | ||
} | ||
|
||
void string::pushBackDefaultRGB() { | ||
str.insert(pushBackPos(), defaultColor); | ||
} | ||
void cgui::string::insertDefaultRGB(int pos) | ||
{ | ||
str.insert(pos, defaultColor); | ||
} | ||
void string::pushBackRGB(int r, int g, int b) { | ||
str.insert(pushBackPos(), "\x1b[38;2;" + std::to_string(r) + ";" + std::to_string(g) + ";" + std::to_string(b) + "m"); | ||
++colorCount; | ||
if (colorCount == 1) { str += defaultColor; } | ||
} | ||
void string::insertRGB(int pos, int r, int g, int b) { | ||
str.insert(pos, "\x1b[38;2;" + std::to_string(r) + ";" + std::to_string(g) + ";" + std::to_string(b) + "m"); | ||
++colorCount; | ||
if (colorCount == 1) { str += defaultColor; } | ||
} | ||
void cgui::string::pushBackBackgroundRGB(int r, int g, int b) | ||
{ | ||
str.insert(pushBackPos(), "\x1b[48;2;" + std::to_string(r) + ";" + std::to_string(g) + ";" + std::to_string(b) + "m"); | ||
++colorCount; | ||
if (colorCount == 1) { str += defaultColor; } | ||
} | ||
void cgui::string::insertBackgroundRGB(int pos, int r, int g, int b) | ||
{ | ||
str.insert(pos, "\x1b[48;2;" + std::to_string(r) + ";" + std::to_string(g) + ";" + std::to_string(b) + "m"); | ||
++colorCount; | ||
if (colorCount == 1) { str += defaultColor; } | ||
} | ||
|
||
string& string::operator+(const string& other) { | ||
str.insert(pushBackPos(), other.str); | ||
visibleLength += other.visibleLength; | ||
return *this; | ||
} | ||
string& cgui::string::operator+(char other) | ||
{ | ||
str.insert(pushBackPos(), 1, other); | ||
visibleLength += 1; | ||
return *this; | ||
} | ||
void string::operator+=(const string& other) { | ||
str.insert(pushBackPos(), other.str); | ||
visibleLength += other.visibleLength; | ||
} | ||
void cgui::string::operator+=(char other) | ||
{ | ||
str.insert(pushBackPos(), 1, other); | ||
visibleLength += 1; | ||
} | ||
|
||
void cgui::string::operator=(const string& other) | ||
{ | ||
str = other.str; | ||
visibleLength = other.visibleLength; | ||
} | ||
|
||
void cgui::string::operator=(std::string_view other) | ||
{ | ||
str = other; | ||
calculateVisibleLength(); | ||
} | ||
|
||
const char* cgui::string::data() const | ||
{ | ||
return str.data(); | ||
} | ||
|
||
int cgui::string::pushBackPos() const | ||
{ | ||
if (colorCount == 0) { | ||
return str.end() - str.begin(); | ||
} | ||
else { | ||
return str.end() - str.begin() - defaultColor.size(); | ||
} | ||
} | ||
|
||
void string::calculateVisibleLength() { | ||
visibleLength = 0; | ||
// 不能有换行符 | ||
for (auto& c : str) { | ||
if (c == '\n') { c = ' '; } | ||
} | ||
// 移除ANSI转义序列 | ||
std::regex ansiEscape(R"(\x1B\[[0-9;]*[A-Za-z])"); | ||
std::string cleanLine = std::regex_replace(str, ansiEscape, ""); | ||
// 处理unicode字符 | ||
// todo | ||
visibleLength = cleanLine.size(); | ||
} | ||
|
||
string cgui::operator+(std::string_view lhs, string& rhs) | ||
{ | ||
return string(lhs) + rhs; | ||
} | ||
|
||
string cgui::operator+(std::string_view lhs, string&& rhs) | ||
{ | ||
return string(lhs) + rhs; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#pragma once | ||
#include <string> | ||
|
||
namespace cgui { | ||
|
||
// std::string造成的bug太多了,为了便于使用只好再封装一层 | ||
// 这个string是为了控制台ui所专门设计的 | ||
// | ||
// 功能如下: | ||
// | ||
// length()返回可见字符数量 | ||
// size()返回全部字符数量 | ||
// | ||
// 解决Unicode字符占用长度和显示长度不一致问题 | ||
// | ||
// 可直接插入rgb,会自动转成ascii序列 | ||
// 末尾会自动恢复默认颜色 | ||
// | ||
// 不会包含换行符,\n会变成空格 | ||
// | ||
class string { | ||
public: | ||
string(); | ||
string(const char* in); | ||
string(std::string_view in); | ||
string(int count, char c); | ||
|
||
size_t size() const; | ||
int length() const; | ||
|
||
void insert(int pos, const string& other); | ||
void insert(int pos, int count, char c); | ||
|
||
void pushBackDefaultRGB(); | ||
void insertDefaultRGB(int pos); | ||
void pushBackRGB(int r, int g, int b); | ||
void insertRGB(int pos, int r, int g, int b); | ||
void pushBackBackgroundRGB(int r, int g, int b); | ||
void insertBackgroundRGB(int pos, int r, int g, int b); | ||
|
||
string& operator+(const string& other); | ||
string& operator+(char other); | ||
void operator+=(const string& other); | ||
void operator+=(char other); | ||
void operator=(const string& other); | ||
void operator=(std::string_view other); | ||
|
||
const char* data() const; | ||
|
||
private: | ||
std::string str; | ||
int visibleLength = 0; | ||
int colorCount = 0; // 用于颜色的ascii转义字符数量 | ||
|
||
int pushBackPos() const; // 如果有颜色,pos会在最后一个恢复默认颜色之前 | ||
void calculateVisibleLength(); | ||
}; | ||
string operator+(std::string_view lhs, string& rhs); | ||
string operator+(std::string_view lhs, string&& rhs); | ||
|
||
} |