-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscg-graph.h
More file actions
164 lines (136 loc) · 3.91 KB
/
scg-graph.h
File metadata and controls
164 lines (136 loc) · 3.91 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
#pragma once
#include "scg-utility.h"
#include "scg-console.h"
#include "scg-settings.h"
#include <mutex>
using namespace std;
namespace scg {
// Note: These functions generates color information for client_area::pixel.
// This function put 3 color information into 1 integer.
class client_area {
public:
// 'change' must be set after output!
struct pixel {
// 'Color' must from pixel_colors::Generate()
pixel(char Display = '\0', pixel_color Color = 0, bool Transparent = false) : changed(true), data(Display) {
color_info = Color;
transparent = Transparent;
}
pixel(const pixel &other) {
this->changed = true;
this->color_info.fdata = other.color_info.fdata;
this->data = other.data;
this->transparent.fdata = other.transparent.fdata;
}
void Display() {
// Before print all pixels
SetTextDisplay();
pixel_colors::Unpack(color_info);
putchar(data);
}
operator char() {
return data;
}
char operator = (char data) {
this->data = data;
changed = true;
return data;
}
//int color_info = 0;
property<pixel_color> color_info = property<pixel_color>(
[this](int &buf) -> int {
return buf;
}, [this](int set, int &buf) {
if (buf != set) {
changed = true;
buf = set;
}
}
);
property<bool> transparent = property<bool>(
[this](bool &buf) -> bool {
return buf;
}, [this](bool set, bool &buf) {
if (buf != set) {
changed = true;
buf = set;
}
}
);
char data;
//bool transparent = true; // Will not override previous one if do so
bool changed = false;
};
client_area(console_size SizeH, console_size SizeW) : data(array_2d<pixel>(SizeH, SizeW)) {
}
client_area(const client_area &other) : data(other.data) {
this->SizeH.fdata = other.SizeH.fdata;
this->SizeW.fdata = other.SizeW.fdata;
}
// Higher priority must merge later
void MergeWith(client_area Other, console_pos StartX = 0, console_pos StartY = 0) {
console_pos EndX = StartX + Other.SizeH;
console_pos EndY = StartY + Other.SizeW;
if (EndX >= SizeH) return;
if (EndY >= SizeW) return;
for (console_pos i = StartX; i < EndX; i++) {
for (console_pos j = StartY; j < EndY; j++) {
console_pos other_i = i - StartX, other_j = j - StartY;
auto &op = Other.data[other_i][other_j];
if (!op.transparent) {
data[i][j] = op;
}
}
}
}
// Must move to specified place first.
void Draw(bool InvaildateAll = false, bool NoChanging = false, bool IgnoreLock = false) {
if (!IgnoreLock) client_area::output_lock.lock();
SaveCursorPos();
for (console_pos i = 0; i < SizeH; i++) {
for (console_pos j = 0; j < SizeW; j++) {
auto &op = data[i][j];
if (((!op.transparent) && op.changed) || InvaildateAll) {
MoveAbsoluteCursor(coords(i, j));
op.Display();
if (!NoChanging) op.changed = false;
}
}
}
RestoreCursorPos();
if (!IgnoreLock) client_area::output_lock.unlock();
}
void Fillup(const pixel& PixelData) {
data.FillWith(PixelData);
}
void Resize(console_size NewHeight, console_size NewWidth) {
data.Allocate(NewHeight, NewWidth);
}
property<console_pos> SizeH = property<console_pos>(
[this](console_pos &reserved) -> console_pos {
return data.Size1D;
}, [](console_pos set, console_pos &reserved) {
throw scg_exception("Cannot write this property!");
}
);
property<console_pos> SizeW = property<console_pos>(
[this](console_pos &reserved) -> console_pos {
return data.Size2D;
}, [](console_pos set, console_pos &reserved) {
throw scg_exception("Cannot write this property!");
}
);
operator array_2d<pixel>&() {
return data;
}
array_2d<pixel>::__array_helper operator [] (console_pos Line) {
return data[Line];
}
array_2d<pixel> data;
private:
//...
static mutex output_lock;
};
mutex client_area::output_lock;
using spixel = client_area::pixel;
}