PeriDyno 1.2.1
Loading...
Searching...
No Matches
WtNodeStyle.cpp
Go to the documentation of this file.
1#include "WtNodeStyle.h"
2
3using json = nlohmann::json;
4
5#define STYLE_READ_COLOR(values, variable) { \
6 auto valueRef = values[#variable]; \
7 if(!valueRef.is_boolean()) { \
8 if (valueRef.is_array()) { \
9 std::vector<int> rgb; rgb.reserve(3); \
10 for (auto it = valueRef.begin(); it != valueRef.end(); ++it) { \
11 rgb.push_back(it->get<int>()); \
12 } \
13 variable.setRgb(rgb[0], rgb[1], rgb[2]); \
14 } else { \
15 std::string str = valueRef;\
16 if(str.compare("white") == 0) {variable = Wt::WColor(Wt::StandardColor::White) ;}\
17 if(str.compare("black") == 0) {variable = Wt::WColor(Wt::StandardColor::Black) ;}\
18 if(str.compare("red") == 0) {variable = Wt::WColor(Wt::StandardColor::Red) ;}\
19 if(str.compare("darkred") == 0) {variable = Wt::WColor(Wt::StandardColor::DarkRed) ;}\
20 if(str.compare("green") == 0) {variable = Wt::WColor(Wt::StandardColor::Green) ;}\
21 if(str.compare("darkgreen") == 0) {variable = Wt::WColor(Wt::StandardColor::DarkGreen) ;}\
22 if(str.compare("blue") == 0) {variable = Wt::WColor(Wt::StandardColor::Blue) ;}\
23 if(str.compare("darlblue") == 0) {variable = Wt::WColor(Wt::StandardColor::DarkBlue) ;}\
24 if(str.compare("cyan") == 0) {variable = Wt::WColor(Wt::StandardColor::Cyan) ;}\
25 if(str.compare("darkcyan") == 0) {variable = Wt::WColor(Wt::StandardColor::DarkCyan) ;}\
26 if(str.compare("megenta") == 0) {variable = Wt::WColor(Wt::StandardColor::Magenta) ;}\
27 if(str.compare("darkmegenta") == 0) {variable = Wt::WColor(Wt::StandardColor::DarkMagenta) ;}\
28 if(str.compare("yellow") == 0) {variable = Wt::WColor(Wt::StandardColor::Yellow) ;}\
29 if(str.compare("darkyellow") == 0) {variable = Wt::WColor(Wt::StandardColor::DarkYellow) ;}\
30 if(str.compare("gray") == 0) {variable = Wt::WColor(Wt::StandardColor::Gray) ;}\
31 if(str.compare("darkgray") == 0) {variable = Wt::WColor(Wt::StandardColor::DarkGray) ;}\
32 if(str.compare("lightgray") == 0) {variable = Wt::WColor(Wt::StandardColor::LightGray) ;}\
33 if(str.compare("transparent") == 0) {variable = Wt::WColor(Wt::StandardColor::Transparent) ;}\
34 if(str.compare("lightcyan") == 0) {variable = Wt::WColor(224, 255, 255) ;}\
35 } \
36} \
37}
38
39#define STYLE_READ_FLOAT(values, variable) { \
40 auto valueRef = values[#variable]; \
41 if(!valueRef.is_boolean()) { \
42 if(valueRef.is_number_float()) { \
43 variable = valueRef; \
44 }\
45 else { \
46 Wt::log("error") << "Error with Json"; \
47 }\
48} \
49}
50
51#define STYLE_READ_BOOL(values, variable) { \
52 auto valueRef = values[#variable]; \
53 if(valueRef.is_boolean()){ \
54 variable = valueRef; \
55 }\
56 else { \
57 Wt::log("error") << "Error with Json"; \
58 }\
59}
60
62{
63 std::string filePath = getAssetPath() + "../src/Rendering/GUI/WtGUI/resources/WtDefaultStyle.json";
64 loadJsonFile(filePath);
65}
66
67WtNodeStyle::WtNodeStyle(std::string jsonText)
68{
69 loadJsonText(jsonText);
70}
71
72void WtNodeStyle::setNodeStyle(std::string jsonText)
73{
74 WtNodeStyle style(jsonText);
75 //WtStyleCollection::setNodeStyle(style);
76}
77
78void WtNodeStyle::loadJsonFile(std::string styleFile)
79{
80 std::ifstream file(styleFile, std::ios::in);
81
82 if (!file.is_open())
83 {
84 Wt::log("error") << "Couldn't open style file" << styleFile;
85 return;
86 }
87 std::stringstream buffer;
88 buffer << file.rdbuf();
89 loadJsonFromByteArray(buffer.str());
90 file.close();
91}
92
93void WtNodeStyle::loadJsonText(std::string jsonText)
94{
95 loadJsonFromByteArray(jsonText);
96}
97
128
130{
131 std::string filePath = getAssetPath() + "../external/nodeeditor/resources/DefaultStyle.json";
132 loadJsonFile(filePath);
133}
134
136{
137 loadJsonText(jsonText);
138}
139
141{
142 WtConnectionStyle style(jsonText);
143 //WtStyleCollection::setConnectionStyle(style);
144}
145
146void WtConnectionStyle::loadJsonFile(std::string styleFile)
147{
148 std::ifstream file(styleFile, std::ios::in);
149
150 if (!file.is_open())
151 {
152 Wt::log("error") << "Couldn't open style file" << styleFile;
153 return;
154 }
155 std::stringstream buffer;
156 buffer << file.rdbuf();
157 loadJsonFromByteArray(buffer.str());
158 file.close();
159}
160
161void WtConnectionStyle::loadJsonText(std::string jsonText)
162{
163 loadJsonFromByteArray(jsonText);
164}
165
166void WtConnectionStyle::loadJsonFromByteArray(std::string const& jsonData)
167{
168 json j = json::parse(jsonData);
169 json obj = j.at("ConnectionStyle");
175
179
181}
182
184{
185 return ConstructionColor;
186}
187
189{
190 return NormalColor;
191}
192
193Wt::WColor WtConnectionStyle::normalColor(std::string typeID) const
194{
195 //std::hash<std::string> hasher;
196 //std::size_t hash_value = hasher(typeID);
197 //std::mt19937 gen(static_cast<unsigned int>(hash_value));
198 //std::uniform_int_distribution<> hueDist(0, 255);
199 //int hue = hueDist(gen);
200 //int sat = 120 + hash_value % 129;
201 //return fromHSL(hash_value, sat, 160);
202
203 if (typeID == "port")
204 {
205 return Wt::WColor(198, 241, 79);
206 }
207 else if (typeID == "FInstance")
208 {
209 return Wt::WColor(207, 174, 113);
210 }
211 else
212 {
213 return Wt::WColor(Wt::StandardColor::Black);
214 }
215
216}
217
218Wt::WColor WtConnectionStyle::fromHSL(int h, int s, int l) const
219{
220 int r, g, b;
221 if (s == 0)
222 {
223 // 饱和度为0,表示灰色,RGB值相等
224 r = g = b = static_cast<int>(l * 255);
225 }
226 else
227 {
228 float q = l < 0.5f ? l * (1 + s) : l + s - l * s;
229 float p = 2 * l - q;
230 float hk = h / 360.0f;
231
232 // 计算RGB的临时值
233 float tr = hk + 1.0f / 3.0f;
234 float tg = hk;
235 float tb = hk - 1.0f / 3.0f;
236
237 // 将临时值转换到0-1范围内
238 tr = tr - static_cast<int>(tr);
239 tg = tg - static_cast<int>(tg);
240 tb = tb - static_cast<int>(tb);
241
242 // 调整负值
243 if (tr < 0) tr += 1;
244 if (tg < 0) tg += 1;
245 if (tb < 0) tb += 1;
246
247 // 计算RGB值
248 float rf, gf, bf;
249
250 // 红色通道
251 if (tr < 1.0f / 6.0f) rf = p + (q - p) * 6.0f * tr;
252 else if (tr < 0.5f) rf = q;
253 else if (tr < 2.0f / 3.0f) rf = p + (q - p) * (2.0f / 3.0f - tr) * 6.0f;
254 else rf = p;
255
256 // 绿色通道
257 if (tg < 1.0f / 6.0f) gf = p + (q - p) * 6.0f * tg;
258 else if (tg < 0.5f) gf = q;
259 else if (tg < 2.0f / 3.0f) gf = p + (q - p) * (2.0f / 3.0f - tg) * 6.0f;
260 else gf = p;
261
262 // 蓝色通道
263 if (tb < 1.0f / 6.0f) bf = p + (q - p) * 6.0f * tb;
264 else if (tb < 0.5f) bf = q;
265 else if (tb < 2.0f / 3.0f) bf = p + (q - p) * (2.0f / 3.0f - tb) * 6.0f;
266 else bf = p;
267
268 // 转换为整数并限制在0-255范围内
269 r = static_cast<int>(rf * 255);
270 g = static_cast<int>(gf * 255);
271 b = static_cast<int>(bf * 255);
272 }
273
274 return Wt::WColor(r, g, b);
275}
276
278{
279 return SelectedColor;
280}
281
283{
284 return SelectedHaloColor;
285}
286
288{
289 return HoveredColor;
290}
291
293{
294 return LineWidth;
295}
296
301
303{
304 return PointDiameter;
305}
306
311
313{
314 std::string filePath = getAssetPath() + "../external/nodeeditor/resources/DefaultStyle.json";
315 loadJsonFile(filePath);
316}
317
319{
320 loadJsonText(jsonText);
321}
322
323void WtFlowViewStyle::setStyle(std::string jsonText)
324{
325 WtFlowViewStyle style(jsonText);
326 //WtStyleCollection::setNodeStyle(style);
327}
328
329void WtFlowViewStyle::loadJsonFile(std::string styleFile)
330{
331 std::ifstream file(styleFile, std::ios::in);
332
333 if (!file.is_open())
334 {
335 Wt::log("error") << "Couldn't open style file" << styleFile;
336 return;
337 }
338 std::stringstream buffer;
339 buffer << file.rdbuf();
340 loadJsonFromByteArray(buffer.str());
341 file.close();
342}
343
344void WtFlowViewStyle::loadJsonText(std::string jsonText)
345{
346 loadJsonFromByteArray(jsonText);
347}
348
349void WtFlowViewStyle::loadJsonFromByteArray(std::string const& jsonData)
350{
351 json j = json::parse(jsonData);
352 json obj = j.at("FlowViewStyle");
356}
357
362
367
372
377
382
387
389{
390 static WtStyleCollection collection;
391
392 return collection;
393}
#define STYLE_READ_COLOR(values, variable)
#define STYLE_READ_FLOAT(values, variable)
#define STYLE_READ_BOOL(values, variable)
float ConstructionLineWidth
Wt::WColor NormalColor
float constructionLineWidth() const
Wt::WColor normalColor() const
bool useDataDefinedColors() const
void loadJsonFromByteArray(std::string const &jsonData) override
void loadJsonFile(std::string fileName) override
Wt::WColor SelectedColor
Wt::WColor HoveredColor
float lineWidth() const
Wt::WColor hoveredColor() const
static void setConnectionStyle(std::string jsonText)
float pointDiameter() const
Wt::WColor ConstructionColor
void loadJsonText(std::string jsonText) override
Wt::WColor constructionColor() const
Wt::WColor selectedHaloColor() const
Wt::WColor fromHSL(int h, int s, int l) const
Wt::WColor SelectedHaloColor
Wt::WColor selectedColor() const
void loadJsonFile(std::string fileName) override
Wt::WColor CoarseGridColor
Wt::WColor FineGridColor
void loadJsonFromByteArray(std::string const &jsonData) override
Wt::WColor BackgroundColor
void loadJsonText(std::string jsonText) override
static void setStyle(std::string jsonText)
void loadJsonFromByteArray(std::string const &jsonData) override
Wt::WColor HotKeyColor1
Definition WtNodeStyle.h:58
Wt::WColor HotKeyColor0
Definition WtNodeStyle.h:57
Wt::WColor GradientColor2
Definition WtNodeStyle.h:51
Wt::WColor WarningColor
Definition WtNodeStyle.h:64
Wt::WColor ErrorColor
Definition WtNodeStyle.h:65
Wt::WColor GradientColor0
Definition WtNodeStyle.h:49
Wt::WColor HotKeyColor2
Definition WtNodeStyle.h:59
Wt::WColor SelectedBoundaryColor
Definition WtNodeStyle.h:47
Wt::WColor ShadowColor
Definition WtNodeStyle.h:53
float HoveredPenWidth
Definition WtNodeStyle.h:68
void loadJsonText(std::string jsonText) override
Wt::WColor FontColor
Definition WtNodeStyle.h:54
float ConnectionPointDiameter
Definition WtNodeStyle.h:70
Wt::WColor ConnectionPointColor
Definition WtNodeStyle.h:61
static void setNodeStyle(std::string jsonText)
Wt::WColor FilledConnectionPointColor
Definition WtNodeStyle.h:62
Wt::WColor GradientColor1
Definition WtNodeStyle.h:50
float PenWidth
Definition WtNodeStyle.h:67
Wt::WColor NormalBoundaryColor
Definition WtNodeStyle.h:46
float Opacity
Definition WtNodeStyle.h:72
Wt::WColor SelectedDragColor
Definition WtNodeStyle.h:48
Wt::WColor FontColorFaded
Definition WtNodeStyle.h:55
void loadJsonFile(std::string fileName) override
Wt::WColor GradientColor3
Definition WtNodeStyle.h:52
WtConnectionStyle _connectionStyle
static WtFlowViewStyle const & flowViewStyle()
static WtStyleCollection & instance()
WtFlowViewStyle _flowViewStyle
static void setConnectionStyle(WtConnectionStyle)
static WtNodeStyle const & nodeStyle()
static void setNodeStyle(WtNodeStyle)
WtNodeStyle _nodeStyle
static WtConnectionStyle const & connectionStyle()
static void setFlowViewStyle(WtFlowViewStyle)
WtStyleCollection()=default
reference at(size_type idx)
access specified array element with bounds checking
Definition json.hpp:21240
basic_json<> json
default specialization
Definition json.hpp:3423