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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
use strum::VariantNames;
use std::str::FromStr;
use crate::core::button::{Button, parse_button_to_component};
use crate::core::CoreHandle;
use crate::modules::components::{map_ui_values, map_ui_values_ref, UIField, UIFieldType, UIFieldValue, UIValue};
use crate::thread::rendering::{ButtonBackground, ButtonText, ButtonTextShadow, RendererComponent};
use crate::thread::util::TextAlignment;
use crate::images::SDImage;
use crate::util::hash_str;
pub fn get_renderer_component_values(core: &CoreHandle, button: &Button) -> Vec<UIValue> {
if let Ok(component) = parse_button_to_component::<RendererComponent>(button) {
let mut fields = vec![];
fields.push(
UIValue {
name: "renderer".to_string(),
display_name: "Renderer to use".to_string(),
description: "Renderers can be added by plugins".to_string(),
ty: UIFieldType::Choice({
let mut names = vec!["default".to_string()];
names.extend(core.core.render_manager.read_renderers().values()
.map(|x| x.name()));
names
}),
value: UIFieldValue::Choice(if component.renderer.is_empty() { "default".to_string() } else { component.renderer.clone() })
}
);
if !component.renderer.is_empty() {
if let Some(renderer) = core.core.render_manager.read_renderers().get(&component.renderer).cloned() {
fields.extend(renderer.component_values(button, &component, core));
}
} else {
fields.push(
UIValue {
name: "background_params".to_string(),
display_name: "Background Parameters".to_string(),
description: "Parameters related to background of the button".to_string(),
ty: UIFieldType::Collapsable,
value: UIFieldValue::Collapsable({
let mut fields = vec![];
fields.push(
UIValue {
name: "background".to_string(),
display_name: "Background Type".to_string(),
description: "Type of the background to use".to_string(),
ty: UIFieldType::Choice(vec!["Solid Color".to_string(), "Horizontal Gradient".to_string(), "Vertical Gradient".to_string(), "Existing Image".to_string(), "New Image".to_string()]),
value: UIFieldValue::Choice(
match &component.background {
ButtonBackground::Solid(_) => "Solid Color",
ButtonBackground::HorizontalGradient(_, _) => "Horizontal Gradient",
ButtonBackground::VerticalGradient(_, _) => "Vertical Gradient",
ButtonBackground::ExistingImage(_) => "Existing Image",
ButtonBackground::NewImage(_) => "New Image",
}.to_string()
)
}
);
match &component.background {
ButtonBackground::Solid(color) => {
fields.push(
UIValue {
name: "color".to_string(),
display_name: "Background Color".to_string(),
description: "Color that will be the background of the button".to_string(),
ty: UIFieldType::Color,
value: color.into()
}
);
}
ButtonBackground::HorizontalGradient(start_color, end_color) => {
fields.push(
UIValue {
name: "start_color".to_string(),
display_name: "Gradient Start Color".to_string(),
description: "Color that will be on left side of the gradient".to_string(),
ty: UIFieldType::Color,
value: start_color.into()
}
);
fields.push(
UIValue {
name: "end_color".to_string(),
display_name: "Gradient End Color".to_string(),
description: "Color that will be on right side of the gradient".to_string(),
ty: UIFieldType::Color,
value: end_color.into()
}
);
}
ButtonBackground::VerticalGradient(start_color, end_color) => {
fields.push(
UIValue {
name: "start_color".to_string(),
display_name: "Gradient Start Color".to_string(),
description: "Color that will be on top side of the gradient".to_string(),
ty: UIFieldType::Color,
value: start_color.into()
}
);
fields.push(
UIValue {
name: "end_color".to_string(),
display_name: "Gradient End Color".to_string(),
description: "Color that will be on bottom side of the gradient".to_string(),
ty: UIFieldType::Color,
value: end_color.into()
}
);
}
ButtonBackground::ExistingImage(identifier) => {
fields.push(
UIValue {
name: "image".to_string(),
display_name: "Image".to_string(),
description: "Image to use as background of the button".to_string(),
ty: UIFieldType::ExistingImage,
value: UIFieldValue::ExistingImage(identifier.to_string())
}
);
}
ButtonBackground::NewImage(blob) => {
fields.push(
UIValue {
name: "image".to_string(),
display_name: "Image".to_string(),
description: "Image to use as background of the button".to_string(),
ty: UIFieldType::ImageData,
value: UIFieldValue::ImageData(blob.to_string())
}
);
}
}
fields
})
}
);
fields.push(
UIValue {
name: "text_params".to_string(),
display_name: "Text Parameters".to_string(),
description: "Parameters related to text on the button".to_string(),
ty: UIFieldType::Collapsable,
value: UIFieldValue::Collapsable({
let mut fields = vec![];
fields.push(
UIValue {
name: "text".to_string(),
display_name: "Text Objects".to_string(),
description: "Array of text objects".to_string(),
ty: UIFieldType::Array(
vec![
UIField {
name: "text".to_string(),
display_name: "Text".to_string(),
description: "Text that will be displayed".to_string(),
ty: UIFieldType::InputFieldString,
default_value: UIFieldValue::InputFieldString("".to_string())
},
UIField {
name: "font".to_string(),
display_name: "Font".to_string(),
description: "Font that will be used for text rendering".to_string(),
ty: UIFieldType::Font,
default_value: UIFieldValue::Font("default".to_string())
},
UIField {
name: "scale".to_string(),
display_name: "Text Scale".to_string(),
description: "Scale of the text".to_string(),
ty: UIFieldType::InputFieldFloat2,
default_value: UIFieldValue::InputFieldFloat2(15.0, 15.0)
},
UIField {
name: "alignment".to_string(),
display_name: "Alignment".to_string(),
description: "To which point of the button the text will be anchored to".to_string(),
ty: UIFieldType::Choice(
TextAlignment::VARIANTS.iter().map(|x| x.to_string()).collect()
),
default_value: UIFieldValue::Choice("Center".to_string())
},
UIField {
name: "padding".to_string(),
display_name: "Padding".to_string(),
description: "Gap to have from alignment/anchor point".to_string(),
ty: UIFieldType::InputFieldUnsignedInteger,
default_value: UIFieldValue::InputFieldUnsignedInteger(0)
},
UIField {
name: "offset".to_string(),
display_name: "Text Offset".to_string(),
description: "2D offset of the text from its alignment/anchor point".to_string(),
ty: UIFieldType::InputFieldFloat2,
default_value: UIFieldValue::InputFieldFloat2(0.0, 0.0)
},
UIField {
name: "color".to_string(),
display_name: "Text Color".to_string(),
description: "Color that text will be displayed in".to_string(),
ty: UIFieldType::Color,
default_value: UIFieldValue::Color(0, 0, 0, 255)
},
UIField {
name: "shadow_enabled".to_string(),
display_name: "Text Shadow".to_string(),
description: "If text shadow should be rendered or not".to_string(),
ty: UIFieldType::Checkbox {
disabled: false
},
default_value: UIFieldValue::Checkbox(false)
}
]
),
value: UIFieldValue::Array({
let mut text_objects = vec![];
for text in &component.text {
let mut values = vec![];
values.push(UIValue {
name: "text".to_string(),
display_name: "Text".to_string(),
description: "Text that will be displayed".to_string(),
ty: UIFieldType::InputFieldString,
value: UIFieldValue::InputFieldString(text.text.clone())
});
values.push(UIValue {
name: "font".to_string(),
display_name: "Font".to_string(),
description: "Font that will be used for text rendering".to_string(),
ty: UIFieldType::Font,
value: UIFieldValue::Font(text.font.clone())
});
values.push(UIValue {
name: "scale".to_string(),
display_name: "Text Scale".to_string(),
description: "Scale of the text".to_string(),
ty: UIFieldType::InputFieldFloat2,
value: UIFieldValue::InputFieldFloat2(text.scale.0, text.scale.1)
});
values.push(UIValue {
name: "alignment".to_string(),
display_name: "Alignment".to_string(),
description: "To which point of the button the text will be anchored to".to_string(),
ty: UIFieldType::Choice(
TextAlignment::VARIANTS.iter().map(|x| x.to_string()).collect()
),
value: UIFieldValue::Choice(text.alignment.to_string())
});
values.push(UIValue {
name: "padding".to_string(),
display_name: "Padding".to_string(),
description: "Gap to have from alignment/anchor point".to_string(),
ty: UIFieldType::InputFieldUnsignedInteger,
value: UIFieldValue::InputFieldUnsignedInteger(text.padding)
});
values.push(UIValue {
name: "offset".to_string(),
display_name: "Text Offset".to_string(),
description: "2D offset of the text from its alignment/anchor point".to_string(),
ty: UIFieldType::InputFieldFloat2,
value: UIFieldValue::InputFieldFloat2(text.offset.0, text.offset.1)
});
values.push(UIValue {
name: "color".to_string(),
display_name: "Text Color".to_string(),
description: "Color that text will be displayed in".to_string(),
ty: UIFieldType::Color,
value: text.color.into()
});
if let Some(shadow) = &text.shadow {
values.push(
UIValue {
name: "shadow_enabled".to_string(),
display_name: "Text Shadow".to_string(),
description: "If text shadow should be rendered or not".to_string(),
ty: UIFieldType::Checkbox {
disabled: false
},
value: UIFieldValue::Checkbox(true)
}
);
values.push(UIValue {
name: "shadow_color".to_string(),
display_name: "Text Shadow Color".to_string(),
description: "Color of the shadow".to_string(),
ty: UIFieldType::Color,
value: shadow.color.into()
});
values.push(UIValue {
name: "shadow_offset".to_string(),
display_name: "Text Shadow Offset".to_string(),
description: "Offset of the shadow from text".to_string(),
ty: UIFieldType::InputFieldInteger2,
value: UIFieldValue::InputFieldInteger2(shadow.offset.0, shadow.offset.1)
});
} else {
values.push(
UIValue {
name: "shadow_enabled".to_string(),
display_name: "Text Shadow".to_string(),
description: "If text shadow should be rendered or not".to_string(),
ty: UIFieldType::Checkbox {
disabled: false
},
value: UIFieldValue::Checkbox(false)
}
);
}
text_objects.push(values);
}
text_objects
})
}
);
fields
})
}
);
fields.push(
UIValue {
name: "plugin_blacklist".to_string(),
display_name: "Allowed plugins to render".to_string(),
description: "Disabled plugins will not appear on button".to_string(),
ty: UIFieldType::Collapsable,
value: UIFieldValue::Collapsable({
let names = core.module_manager().get_modules_for_rendering(&button.component_names());
names.into_values()
.map(|x| {
let name = x.name();
UIValue {
name: name.clone(),
display_name: name.clone(),
description: "".to_string(),
ty: UIFieldType::Checkbox { disabled: false },
value: UIFieldValue::Checkbox(!component.plugin_blacklist.contains(&name))
}
}).collect()
})
}
);
fields.push(
UIValue {
name: "to_cache".to_string(),
display_name: "Caching".to_string(),
description: "If renderer should cache render result or not. Caching might use a lot of RAM, no caching will use a lot more CPU".to_string(),
ty: UIFieldType::Checkbox {
disabled: false
},
value: UIFieldValue::Checkbox(component.to_cache)
}
);
}
fields
} else {
vec![]
}
}
pub fn set_renderer_component_values(core: &CoreHandle, button: &mut Button, value: Vec<UIValue>) {
if let Ok(mut component) = parse_button_to_component::<RendererComponent>(button) {
let change_map = map_ui_values(value);
if let Some(value) = change_map.get("renderer") {
if let Ok(value) = value.value.try_into_string() {
if value == "default" {
component.renderer = "".to_string();
} else {
if let Some(_) = core.core.render_manager.read_renderers().get(&value) {
component.renderer = value;
}
}
}
}
if !component.renderer.is_empty() {
if let Some(renderer) = core.core.render_manager.read_renderers().get(&component.renderer).cloned() {
renderer.set_component_value(button, &mut component, core, change_map.values().cloned().collect());
}
} else {
if let Some(value) = change_map.get("background_params") {
if let UIFieldValue::Collapsable(value) = &value.value {
let change_map = map_ui_values(value.clone());
if let Some(value) = change_map.get("background") {
if let Ok(choice) = value.value.try_into_string() {
match choice.as_str() {
"Solid Color" => component.background = ButtonBackground::Solid((0, 0, 0, 255)),
"Horizontal Gradient" => component.background = ButtonBackground::HorizontalGradient((0, 0, 0, 255), (0, 0, 0, 255)),
"Vertical Gradient" => component.background = ButtonBackground::VerticalGradient((0, 0, 0, 255), (0, 0, 0, 255)),
"Existing Image" => component.background = ButtonBackground::ExistingImage("".to_string()),
"New Image" => component.background = ButtonBackground::NewImage("".to_string()),
_ => {}
}
}
}
if let Some(value) = change_map.get("color") {
if let ButtonBackground::Solid(_) = component.background {
if let Ok(color) = (&value.value).try_into() {
component.background = ButtonBackground::Solid(color);
}
}
}
if let Some(value) = change_map.get("start_color") {
if let ButtonBackground::HorizontalGradient(_, end) = component.background {
if let Ok(color) = (&value.value).try_into() {
component.background = ButtonBackground::HorizontalGradient(color, end);
}
}
if let ButtonBackground::VerticalGradient(_, end) = component.background {
if let Ok(color) = (&value.value).try_into() {
component.background = ButtonBackground::VerticalGradient(color, end);
}
}
}
if let Some(value) = change_map.get("end_color") {
if let ButtonBackground::HorizontalGradient(start, _) = component.background {
if let Ok(color) = (&value.value).try_into() {
component.background = ButtonBackground::HorizontalGradient(start, color);
}
}
if let ButtonBackground::VerticalGradient(start, _) = component.background {
if let Ok(color) = (&value.value).try_into() {
component.background = ButtonBackground::VerticalGradient(start, color);
}
}
}
if let Some(value) = change_map.get("image") {
match &component.background {
ButtonBackground::ExistingImage(_) => {
if let Ok(identifier) = (&value.value).try_into() {
component.background = ButtonBackground::ExistingImage(identifier);
}
}
ButtonBackground::NewImage(_) => {
if let Ok(blob) = (&value.value).try_into_string() {
let identifier = hash_str(&blob);
if let Ok(image) = SDImage::from_base64(&blob, core.core.image_size) {
component.background = ButtonBackground::ExistingImage(identifier.clone());
let mut handle = core.core.image_collection.write().unwrap();
handle.insert(identifier, image);
} else {
component.background = ButtonBackground::NewImage("".to_string());
}
}
}
_ => {}
}
}
}
}
if let Some(value) = change_map.get("text_params") {
if let UIFieldValue::Collapsable(value) = &value.value {
let change_map = map_ui_values(value.clone());
if let Some(value) = change_map.get("text") {
if let UIFieldValue::Array(items) = &value.value {
component.text = vec![];
fn get_text_object(item: &Vec<UIValue>) -> Option<ButtonText> {
let map = map_ui_values_ref(item);
Some(ButtonText {
text: (&map.get("text")?.value).try_into().ok()?,
font: (&map.get("font")?.value).try_into().ok()?,
scale: (&map.get("scale")?.value).try_into().ok()?,
alignment: TextAlignment::from_str(&map.get("alignment")?.value.try_into_string().ok()?).ok()?,
padding: (&map.get("padding")?.value).try_into().ok()?,
offset: (&map.get("offset")?.value).try_into_f32_f32().ok()?,
color: (&map.get("color")?.value).try_into().ok()?,
shadow: if let Some(bool) = map.get("shadow_enabled")?.value.try_into_bool().ok() {
let get_shadow = || {
Some(ButtonTextShadow {
offset: (&map.get("shadow_offset")?.value).try_into().ok()?,
color: (&map.get("shadow_color")?.value).try_into().ok()?
})
};
if bool {
get_shadow().or(Some(ButtonTextShadow {
offset: (0, 0),
color: (0, 0, 0, 0)
}))
} else {
None
}
} else {
None
}
})
}
for item in items {
if let Some(object) = get_text_object(item) {
component.text.push(object)
}
}
}
}
}
}
if let Some(value) = change_map.get("plugin_blacklist") {
if let UIFieldValue::Collapsable(value) = &value.value {
let change_map = map_ui_values(value.clone());
for (name, value) in change_map {
if let UIFieldValue::Checkbox(state) = value.value {
if state {
component.plugin_blacklist.retain(|x| *x != name);
} else {
component.plugin_blacklist.push(name);
}
}
}
}
}
if let Some(value) = change_map.get("to_cache") {
if let Ok(value) = value.value.try_into_bool() {
component.to_cache = value;
}
}
}
button.insert_component(component).ok();
core.core.mark_for_redraw();
}
}