-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuffer.hpp
More file actions
196 lines (192 loc) · 4.82 KB
/
Buffer.hpp
File metadata and controls
196 lines (192 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#pragma once
#define PC_TUI_BUFFER
#include<vector>
#include"../Container/String.hpp"
#include"Color.hpp"
#include"Ansi.hpp"
#include"OS.hpp"
#include"Util.hpp"
using std::vector;
class Pixel
{
private:
char16_t high,low;
bool surrogate=false;
public:
Color fore,back;
inline void Reset(){Set(u' ',-1,-1);}
void Set(char16_t c,Color f=-1,Color b=-1)
{high=c;fore=f,back=b;surrogate=false;}
void SetSurrogate(char16_t h,char16_t l,Color f=-1,Color b=-1)
{high=h,low=l;fore=f,back=b;surrogate=true;}
Pixel(char16_t c,Color f=-1,Color b=-1)
{Set(c,f,b);}
Pixel(char16_t h,char16_t l,Color f=-1,Color b=-1)
{SetSurrogate(h,l,f,b);}
Pixel(Color back)
{Set(u' ',-1,back);}
Pixel(){Set(u' ',-1,-1);}
inline bool isSurrogate(){return surrogate;}
/// @brief Check if the pixel is occupied by a wide character in front of this
inline bool isEmpty(){return high==u'\0';}
void putchar(bool color=true)
{
if(color)
SetForegroundColor(fore),
SetBackgroundColor(back);
String s;s.Append(high);
if(surrogate) s.Append(low);
OutputUnicode(s);
}
inline int width()
{return pcuni::charWidthInConsole(high);}
};
#define pcTUI_BUF_DEFAULT_WIDTH 20
#define pcTUI_BUF_DEFAULT_HEIGHT 5
#define pcTUI_BUF_EXTEND_MULTIPLE 1.5
class Buffer
{
private:
Coord size;
Color BackgroundColor;
vector<vector<Pixel>> canvas;
void resize(short w,short h)
{
for(auto i:canvas)
i.resize(w);
canvas.resize(h,vector<Pixel>(w));
}
Pixel& get(Coord pos)
{
if(pos.x<0||pos.y<0)
throw pc::Exception("Invalid buffer position access");
if(pos.y>=size.y||pos.x>=size.x)
resize(std::max(pos.x,size.x)*pcTUI_BUF_EXTEND_MULTIPLE,
std::max(pos.y,size.y)*pcTUI_BUF_EXTEND_MULTIPLE);
return canvas[pos.y][pos.x];
}
/**
* @brief Increase cursor position by one, with line break
* @param cur Current cursor position
* @param p View start position
* @param s View size
*/
inline void cursorIncrease(Coord& cur,Coord p,Coord s)
{
cur.x++;
if(cur.x>p.x+s.x) cur.x=p.x,cur.y++;
if(cur.y>p.y+s.y) cur=p;
}
inline void cursorIncrease(Coord& cur)
{cursorIncrease(cur,{0,0},size);}
/**
* @brief Move cursor to the beginning of next line
* @param cur Current cursor position
* @param p View start position
* @param s View size
*/
inline void cursorBreakLine(Coord& cur,Coord p,Coord s)
{
cur.x=p.x,cur.y++;
if(cur.y>p.y+s.y) cur=p;
}
inline void cursorBreakLine(Coord& cur)
{cursorBreakLine(cur,{0,0},size);}
public:
void SetBackgroundColor(Color col)
{BackgroundColor=col;}
void Clear()
{
for(int i=0;i<size.y;i++)
for(int j=0;j<size.x;j++)
canvas[i][j].Set(u' ',-1,BackgroundColor);
}
/**
* @brief Print text at position with colors
* @param pos Start position
* @param vpos View start position
* @param vsize View size
*/
void Print(Coord pos,Color fore,Color back,String text,Coord vpos,Coord vsize)
{
int len=text.Size(),w;
for(int i=0;i<len;)
{
w=pcuni::charWidthInConsole(text[i]);
if(pos.x+w>vpos.x+vsize.x)
cursorBreakLine(pos,vpos,vsize);
if(pcuni::isHighSurrogate(text[i]))
get(pos).SetSurrogate(text[i],text[i+1],fore,back),
i+=2;
else
get(pos).Set(text[i],fore,back),
i++;
w--,cursorIncrease(pos,vpos,vsize);
while(w--) // 被宽字符占用的空格为 u'\0'
get(pos).Set(u'\0',-1,BackgroundColor),
cursorIncrease(pos,vpos,vsize);
}
}
inline void Print(Coord pos,Color fore,Color back,String text)
{Print(pos,fore,back,text,{0,0},size);}
inline void Print(Coord pos,String text)
{Print(pos,-1,-1,text,{0,0},size);}
void PrintTo(Buffer& Target,Coord pos)
{
Coord npos=pos;
for(int i=0;i<size.y;i++)
for(int j=0;j<size.x;j++)
{
npos.Set(pos.x+j,pos.y+i);
Target.get(npos)=canvas[i][j];
}
}
void Render(Coord pos)
{
// CursorGoto(pos);
// ResetAnsiStyle();
// SetBackgroundColor(BackgroundColor);
// for(int i=0;i<size.y;i++)
// OutputUnicode(String(u' ',size.x)),
// CursorGoto(pos+Coord(0,i+1));
// CursorGoto(pos);
ResetAnsiStyle();
Color nowFore=-1,nowBack=-1;
Pixel p;
Coord spos=pos,npos=pos;
int n;
for(int i=0;i<size.y;i++)
{
for(int j=0;j<size.x;j++)
{
p=canvas[i][j];
if(p.isEmpty())
continue;
spos.Set(pos.x+j,pos.y+i);
if(npos!=spos)
CursorGoto(spos),
npos=spos;
if(p.fore!=nowFore)
nowFore=p.fore,
SetForegroundColor(p.fore);
if(p.back!=nowBack)
nowBack=p.back,
SetBackgroundColor(p.back);
p.putchar(false);
n=p.width();
while(n--)
cursorIncrease(npos);
}
ResetAnsiStyle();
}
}
Buffer(short w,short h,Color back=-1):
size({w,h}),BackgroundColor(back),
canvas(h,vector<Pixel>(w,Pixel(back)))
{resize(w,h);Clear();}
Buffer():
size({pcTUI_BUF_DEFAULT_WIDTH,pcTUI_BUF_DEFAULT_HEIGHT}),
BackgroundColor(-1),
canvas(size.y,vector<Pixel>(size.x,Pixel(BackgroundColor)))
{resize(size.x,size.y);Clear();}
};