forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget.cpp
1307 lines (1234 loc) · 44.8 KB
/
widget.cpp
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "widget.h"
#include "character_martial_arts.h"
#include "color.h"
#include "display.h"
#include "generic_factory.h"
#include "json.h"
#include "output.h"
#include "overmapbuffer.h"
const static flag_id json_flag_W_DISABLED( "W_DISABLED" );
const static flag_id json_flag_W_DYNAMIC_HEIGHT( "W_DYNAMIC_HEIGHT" );
const static flag_id json_flag_W_LABEL_NONE( "W_LABEL_NONE" );
// Use generic factory wrappers for widgets to use standardized JSON loading methods
namespace
{
generic_factory<widget> widget_factory( "widgets" );
} // namespace
template<>
const widget &string_id<widget>::obj() const
{
return widget_factory.obj( *this );
}
template<>
bool string_id<widget>::is_valid() const
{
return widget_factory.is_valid( *this );
}
void widget::load_widget( const JsonObject &jo, const std::string &src )
{
widget_factory.load( jo, src );
}
void widget::reset()
{
widget_factory.reset();
}
const std::vector<widget> &widget::get_all()
{
return widget_factory.get_all();
}
const widget_id &widget::getId() const
{
return id;
}
// Convert widget "var" enums to string equivalents
namespace io
{
template<>
std::string enum_to_string<widget_var>( widget_var data )
{
switch( data ) {
case widget_var::focus:
return "focus";
case widget_var::hunger:
return "hunger";
case widget_var::move:
return "move";
case widget_var::move_cost:
return "move_cost";
case widget_var::mood:
return "mood";
case widget_var::pain:
return "pain";
case widget_var::sound:
return "sound";
case widget_var::speed:
return "speed";
case widget_var::stamina:
return "stamina";
case widget_var::thirst:
return "thirst";
case widget_var::fatigue:
return "fatigue";
case widget_var::health:
return "health";
case widget_var::weariness_level:
return "weariness_level";
case widget_var::mana:
return "mana";
case widget_var::max_mana:
return "max_mana";
case widget_var::morale_level:
return "morale_level";
// Compass
case widget_var::compass_text:
return "compass_text";
case widget_var::compass_legend_text:
return "compass_legend_text";
// Base stats
case widget_var::stat_str:
return "stat_str";
case widget_var::stat_dex:
return "stat_dex";
case widget_var::stat_int:
return "stat_int";
case widget_var::stat_per:
return "stat_per";
// Bodypart attributes
case widget_var::bp_hp:
return "bp_hp";
case widget_var::bp_encumb:
return "bp_encumb";
case widget_var::bp_warmth:
return "bp_warmth";
case widget_var::bp_wetness:
return "bp_wetness";
case widget_var::cardio_fit:
return "cardio_fit";
case widget_var::cardio_acc:
return "cardio_acc";
// Description functions
case widget_var::activity_text:
return "activity_text";
case widget_var::body_temp_text:
return "body_temp_text";
case widget_var::bp_armor_outer_text:
return "bp_armor_outer_text";
case widget_var::bp_status_text:
return "bp_status_text";
case widget_var::bp_status_sym_text:
return "bp_status_sym_text";
case widget_var::bp_status_legend_text:
return "bp_status_legend_text";
case widget_var::date_text:
return "date_text";
case widget_var::env_temp_text:
return "env_temp_text";
case widget_var::fatigue_text:
return "fatigue_text";
case widget_var::health_text:
return "health_text";
case widget_var::hunger_text:
return "hunger_text";
case widget_var::lighting_text:
return "lighting_text";
case widget_var::mood_text:
return "mood_text";
case widget_var::moon_phase_text:
return "moon_phase_text";
case widget_var::move_count_mode_text:
return "move_count_mode_text";
case widget_var::move_mode_letter:
return "move_mode_letter";
case widget_var::move_mode_text:
return "move_mode_text";
case widget_var::pain_text:
return "pain_text";
case widget_var::overmap_loc_text:
return "overmap_loc_text";
case widget_var::overmap_text:
return "overmap_text";
case widget_var::place_text:
return "place_text";
case widget_var::power_text:
return "power_text";
case widget_var::rad_badge_text:
return "rad_badge_text";
case widget_var::safe_mode_text:
return "safe_mode_text";
case widget_var::style_text:
return "style_text";
case widget_var::thirst_text:
return "thirst_text";
case widget_var::time_text:
return "time_text";
case widget_var::veh_azimuth_text:
return "veh_azimuth_text";
case widget_var::veh_cruise_text:
return "veh_cruise_text";
case widget_var::veh_fuel_text:
return "veh_fuel_text";
case widget_var::weariness_text:
return "weariness_text";
case widget_var::weary_malus_text:
return "weary_malus_text";
case widget_var::weather_text:
return "weather_text";
case widget_var::weight_text:
return "weight_text";
case widget_var::wielding_text:
return "wielding_text";
case widget_var::wind_text:
return "wind_text";
// Fall-through - invalid
case widget_var::last:
break;
}
cata_fatal( "Invalid widget_var" );
}
template<>
std::string enum_to_string<cardinal_direction>( cardinal_direction dir )
{
switch( dir ) {
case cardinal_direction::NORTH:
return "N";
case cardinal_direction::SOUTH:
return "S";
case cardinal_direction::EAST:
return "E";
case cardinal_direction::WEST:
return "W";
case cardinal_direction::NORTHEAST:
return "NE";
case cardinal_direction::NORTHWEST:
return "NW";
case cardinal_direction::SOUTHEAST:
return "SE";
case cardinal_direction::SOUTHWEST:
return "SW";
case cardinal_direction::LOCAL:
return "L";
case cardinal_direction::num_cardinal_directions:
default:
break;
}
cata_fatal( "Invalid cardinal_direction" );
}
template<>
std::string enum_to_string<bodypart_status>( bodypart_status stat )
{
switch( stat ) {
case bodypart_status::BITTEN:
return "bitten";
case bodypart_status::INFECTED:
return "infected";
case bodypart_status::BROKEN:
return "broken";
case bodypart_status::SPLINTED:
return "splinted";
case bodypart_status::BANDAGED:
return "bandaged";
case bodypart_status::DISINFECTED:
return "disinfected";
case bodypart_status::BLEEDING:
return "bleeding";
case bodypart_status::num_bodypart_status:
default:
break;
}
cata_fatal( "Invalid bodypart_status" );
}
template<>
std::string enum_to_string<widget_alignment>( widget_alignment align )
{
switch( align ) {
case widget_alignment::LEFT:
return "left";
case widget_alignment::CENTER:
return "center";
case widget_alignment::RIGHT:
return "right";
case widget_alignment::num_widget_alignments:
default:
break;
}
cata_fatal( "Invalid widget_alignment" );
}
} // namespace io
void widget_phrase::load( const JsonObject &jo )
{
optional( jo, false, "id", id );
optional( jo, false, "text", text );
optional( jo, false, "sym", sym );
optional( jo, false, "value", value, INT_MIN );
std::string clr;
optional( jo, false, "color", clr, "white" );
color = color_from_string( clr );
}
int widget_phrase::get_val_for_id( const std::string &phrase_id, const widget_id &wgt )
{
auto iter = std::find_if( wgt->_phrases.begin(), wgt->_phrases.end(),
[&phrase_id]( const widget_phrase & wp ) {
return wp.id == phrase_id;
} );
return iter == wgt->_phrases.end() ? INT_MIN : iter->value;
}
const translation &widget_phrase::get_text_for_id( const std::string &phrase_id,
const widget_id &wgt )
{
static const translation none;
auto iter = std::find_if( wgt->_phrases.begin(), wgt->_phrases.end(),
[&phrase_id]( const widget_phrase & wp ) {
return wp.id == phrase_id;
} );
return iter == wgt->_phrases.end() ? none : iter->text;
}
const std::string &widget_phrase::get_sym_for_id( const std::string &phrase_id,
const widget_id &wgt )
{
static const std::string none;
auto iter = std::find_if( wgt->_phrases.begin(), wgt->_phrases.end(),
[&phrase_id]( const widget_phrase & wp ) {
return wp.id == phrase_id;
} );
return iter == wgt->_phrases.end() ? none : iter->sym;
}
nc_color widget_phrase::get_color_for_id( const std::string &phrase_id, const widget_id &wgt,
int val )
{
std::map<int, nc_color> vals;
for( const widget_phrase &wp : wgt->_phrases ) {
if( phrase_id != wp.id ) {
continue;
}
if( val == INT_MIN ) {
return wp.color;
}
vals.emplace( wp.value, wp.color );
}
if( vals.empty() ) {
return c_white;
}
int key = INT_MIN;
for( const auto &v : vals ) {
if( v.first == val ) {
return v.second;
} else if( v.first > val ) {
break;
}
key = v.first;
}
return key == INT_MIN ? c_white : vals[key];
}
void widget::load( const JsonObject &jo, const std::string & )
{
optional( jo, was_loaded, "strings", _strings );
optional( jo, was_loaded, "width", _width, 1 );
optional( jo, was_loaded, "height", _height_max, 1 );
optional( jo, was_loaded, "symbols", _symbols, "-" );
optional( jo, was_loaded, "fill", _fill, "bucket" );
optional( jo, was_loaded, "label", _label, translation() );
optional( jo, was_loaded, "style", _style, "number" );
optional( jo, was_loaded, "arrange", _arrange, "columns" );
optional( jo, was_loaded, "direction", _direction, cardinal_direction::num_cardinal_directions );
optional( jo, was_loaded, "text_align", _text_align, widget_alignment::RIGHT );
optional( jo, was_loaded, "label_align", _label_align, widget_alignment::LEFT );
optional( jo, was_loaded, "flags", _flags );
_height = _height_max;
_label_width = _label.empty() ? 0 : utf8_width( _label.translated() );
if( jo.has_string( "var" ) ) {
_var = io::string_to_enum<widget_var>( jo.get_string( "var" ) );
}
if( jo.has_string( "bodypart" ) ) {
_bp_id = bodypart_id( jo.get_string( "bodypart" ) );
}
if( jo.has_array( "colors" ) ) {
_colors.clear();
for( const std::string color_name : jo.get_array( "colors" ) ) {
_colors.emplace_back( get_all_colors().name_to_color( color_name ) );
}
}
if( jo.has_array( "phrases" ) ) {
_phrases.clear();
for( JsonObject jobj : jo.get_array( "phrases" ) ) {
widget_phrase phs;
phs.load( jobj );
_phrases.emplace_back( phs );
}
}
optional( jo, was_loaded, "widgets", _widgets, string_id_reader<::widget> {} );
}
// Returns the derived label width for this widget/layout
int widget::finalize_label_width_recursive( const widget_id &id )
{
widget *w = nullptr;
// Get the original widget from the widget factory.
for( const widget &wgt : widget::get_all() ) {
if( wgt.getId() == id ) {
w = const_cast<widget *>( &wgt );
break;
}
}
// None found, return 0 as the label width.
if( w == nullptr ) {
return 0;
} else if( w->_widgets.empty() ) {
// No more nested layouts, we've found an individual widget.
// Return the widget's label width, or 0 if the label is disabled.
return w->has_flag( json_flag_W_LABEL_NONE ) ? 0 : w->_label_width;
}
// If we get here, we have a layout that contains nested widgets.
// Find the longest label width within this layout.
int width = 0;
for( const widget_id &wid : w->_widgets ) {
// Skip nested elements that are "rows" layouts,
// these don't count towards the parent's label width.
if( wid->_style == "layout" && wid->_arrange == "rows" ) {
continue;
}
// Dive deeper to retrieve the nested element's label width.
int tmpw = widget::finalize_label_width_recursive( wid );
if( tmpw > width ) {
width = tmpw;
}
}
// Update this layout's label width to reflect the longest label within.
w->_label_width = width;
return w->_label_width;
}
void widget::finalize()
{
for( const widget &wgt : widget::get_all() ) {
widget::finalize_label_width_recursive( wgt.getId() );
}
}
void widget::set_default_var_range( const avatar &ava )
{
_var_min = INT_MIN;
_var_max = INT_MAX;
_var_norm = std::make_pair( INT_MIN, INT_MAX );
switch( _var ) {
case widget_var::cardio_acc:
break; // TODO
case widget_var::cardio_fit:
_var_min = 0;
// Same maximum used by get_cardiofit - 3 x BMR, adjusted for mutations
_var_max = 3 * ava.base_bmr() * ava.mutation_value( "cardio_multiplier" );
break;
case widget_var::fatigue:
_var_min = 0;
_var_max = 1000;
break;
case widget_var::focus:
_var_min = 0;
_var_max = 200;
// Small range of normal focus that won't be color-coded
_var_norm = std::make_pair( 90, 110 );
break;
case widget_var::health:
_var_min = -200;
_var_max = 200;
// Small range of normal health that won't be color-coded
_var_norm = std::make_pair( -10, 10 );
break;
case widget_var::hunger:
break; // TODO
case widget_var::mana:
_var_min = 0;
_var_max = ava.magic->max_mana( ava );
break;
case widget_var::max_mana:
_var_min = 0;
// What could "max max mana" mean? Use 2x current max because why not
_var_max = 2 * ava.magic->max_mana( ava );
break;
case widget_var::mood:
break; // TODO
case widget_var::morale_level:
_var_min = -100;
_var_max = 100;
// Small range of morale that isn't worth crying about
_var_norm = std::make_pair( -10, 10 );
break;
case widget_var::move:
_var_min = 0;
_var_max = 1000; // TODO: Determine better max
// This is a counter of remaining moves, with no normal value
break;
case widget_var::move_cost:
_var_min = 0;
_var_max = 300; // Can go up to 500-600 while prone
_var_norm = std::make_pair( 100, 100 );
break;
case widget_var::pain:
_var_min = 0;
_var_max = 80;
// Zero pain isn't really normal but it's the state we prefer
_var_norm = std::make_pair( 0, 0 );
break;
case widget_var::sound:
_var_min = 0;
_var_max = 200;
// Quiet sounds are normal
_var_norm = std::make_pair( 0, 5 );
break;
case widget_var::speed:
_var_min = 0;
_var_max = 200;
_var_norm.first = ava.get_speed_base();
_var_norm.second = ava.get_speed_base();
break;
case widget_var::stamina:
_var_min = 0;
_var_max = ava.get_stamina_max();
// No normal defined, unless we want max stamina to be colored white? (maybe)
break;
case widget_var::thirst:
break; // TODO
case widget_var::weariness_level:
_var_min = 0;
_var_max = 10;
break;
// Base stats
// Normal is the base stat value only; min and max are -3 and +3 from base
case widget_var::stat_str:
_var_norm.first = ava.get_str_base();
_var_norm.second = ava.get_str_base();
_var_min = _var_norm.first - 3;
_var_max = _var_norm.first + 3;
break;
case widget_var::stat_dex:
_var_norm.first = ava.get_dex_base();
_var_norm.second = ava.get_dex_base();
_var_min = _var_norm.first - 3;
_var_max = _var_norm.first + 3;
break;
case widget_var::stat_int:
_var_norm.first = ava.get_int_base();
_var_norm.second = ava.get_int_base();
_var_min = _var_norm.first - 3;
_var_max = _var_norm.first + 3;
break;
case widget_var::stat_per:
_var_norm.first = ava.get_per_base();
_var_norm.second = ava.get_per_base();
_var_min = _var_norm.first - 3;
_var_max = _var_norm.first + 3;
break;
// Bodypart attributes
case widget_var::bp_hp:
// HP for body part
_var_min = 0;
_var_max = ava.get_part_hp_max( _bp_id );
break;
case widget_var::bp_encumb:
_var_min = 0;
_var_max = 100; // ???
break;
case widget_var::bp_warmth:
// From weather.h: Body temperature is measured on a scale of 0u to 10000u,
// where 10u = 0.02C and 5000u is 37C
_var_min = 0;
_var_max = 10000;
break;
case widget_var::bp_wetness:
_var_min = 0;
_var_max = 100; // ???
break;
default:
break;
}
}
int widget::get_var_value( const avatar &ava ) const
{
// Numeric value to be rendered in the widget
int value = 0;
// Each "var" value refers to some attribute, typically of the avatar, that yields a numeric
// value, and can be displayed as a numeric field, a graph, or a series of text phrases.
switch( _var ) {
// Vars with a known max val
case widget_var::stamina:
value = ava.get_stamina();
break;
case widget_var::mana:
value = ava.magic->available_mana();
break;
case widget_var::max_mana:
value = ava.magic->max_mana( ava );
break;
case widget_var::morale_level:
value = ava.get_morale_level();
break;
case widget_var::bp_hp:
// HP for body part
value = ava.get_part_hp_cur( _bp_id );
break;
case widget_var::bp_warmth:
// Body part warmth/temperature
value = ava.get_part_temp_cur( _bp_id );
break;
case widget_var::bp_wetness:
// Body part wetness
value = ava.get_part_wetness( _bp_id );
break;
case widget_var::focus:
value = ava.get_focus();
break;
case widget_var::speed:
value = ava.get_speed();
break;
case widget_var::move:
value = ava.movecounter;
break;
case widget_var::move_cost:
value = ava.run_cost( 100 );
break;
case widget_var::pain:
value = ava.get_perceived_pain();
break;
case widget_var::fatigue:
value = ava.get_fatigue();
break;
case widget_var::health:
value = ava.get_healthy();
break;
case widget_var::weariness_level:
value = ava.weariness_level();
break;
case widget_var::stat_str:
value = ava.get_str();
break;
case widget_var::stat_dex:
value = ava.get_dex();
break;
case widget_var::stat_int:
value = ava.get_int();
break;
case widget_var::stat_per:
value = ava.get_per();
break;
case widget_var::sound:
value = ava.volume;
break;
case widget_var::bp_encumb:
// Encumbrance for body part
value = ava.get_part_encumbrance_data( _bp_id ).encumbrance;
break;
case widget_var::cardio_fit:
value = ava.get_cardiofit();
break;
case widget_var::cardio_acc:
value = ava.get_cardio_acc();
break;
// TODO
case widget_var::mood:
// see morale_emotion
case widget_var::hunger:
// see display::hunger_text_color()
case widget_var::thirst:
// see display::thirst_text_color()
default:
value = 0;
}
return value;
}
bool widget::has_flag( const flag_id &flag ) const
{
return _flags.count( flag ) != 0;
}
bool widget::has_flag( const std::string &flag ) const
{
return has_flag( flag_id( flag ) );
}
std::string widget::show( const avatar &ava, const unsigned int max_width )
{
set_default_var_range( ava );
if( uses_text_function() ) {
// Text functions are a carry-over from before widgets, with existing functions generating
// descriptive colorized text for avatar attributes. The "value" for these is immaterial;
// only the final color string is shown. Bypass value calculation and call the
// text-rendering function directly.
return color_text_function_string( ava, max_width );
} else {
// For normal widgets, get current numeric value and potential maximum,
// and return a color string rendering of that value in the appropriate style.
int value = get_var_value( ava );
return color_value_string( value );
}
}
// Returns the new row index after drawing
int widget::custom_draw_multiline( const std::string &widget_string, const catacurses::window &w,
const int margin, const int width, int row_num )
{
std::string wgt_str = widget_string;
size_t strpos = 0;
// Split the widget string into lines (for height > 1)
while( ( strpos = wgt_str.find( '\n' ) ) != std::string::npos ) {
trim_and_print( w, point( margin, row_num ), width, c_light_gray, wgt_str.substr( 0, strpos ) );
wgt_str.erase( 0, strpos + 1 );
row_num++;
}
// Last line (or first line for single-line widgets)
trim_and_print( w, point( margin, row_num ), width, c_light_gray, wgt_str );
row_num++;
return row_num;
}
// Drawing function, provided as a callback to the window_panel constructor.
// Handles rendering a widget's content into a window panel.
static void custom_draw_func( const draw_args &args )
{
const avatar &u = args._ava;
const catacurses::window &w = args._win;
widget *wgt = args.get_widget();
// Get full window width
const int width = catacurses::getmaxx( w );
// Leave 1 character space for margin on left and right
const int margin = 1;
const int widt = width - 2 * margin;
// Quit if there is nothing to draw or no space to draw it
if( wgt == nullptr || width <= 0 ) {
return;
}
werase( w );
if( wgt->_style == "sidebar" ) {
} else if( wgt->_style == "layout" ) {
if( wgt->_arrange == "rows" ) {
// Layout widgets in rows
// FIXME: Be able to handle rows that are themselves more than one line!
// Could this be done in the layout() function somehow (by returning newlines?)
int row_num = 0;
for( const widget_id &row_wid : wgt->_widgets ) {
widget row_widget = row_wid.obj();
const std::string txt = row_widget.layout( u, widt, wgt->_label_width );
row_num = widget::custom_draw_multiline( txt, w, margin, widt, row_num );
}
} else {
// Layout widgets in columns
// For now, this is the default when calling layout()
// So, just layout self on a single line
widget::custom_draw_multiline( wgt->layout( u, widt ), w, margin, widt, 0 );
}
} else {
// No layout, just a widget
widget::custom_draw_multiline( wgt->layout( u, widt ), w, margin, widt, 0 );
}
wnoutrefresh( w );
}
window_panel widget::get_window_panel( const int width, const int req_height )
{
// Width is fixed, but height may vary depending on child widgets
int height = req_height;
// For layout with rows, height will be the combined
// height of all child widgets.
if( _style == "layout" && _arrange == "rows" ) {
height = 0;
for( const widget_id &wid : _widgets ) {
height += wid->_height > 0 ? wid->_height : 1;
}
} else if( _style == "widget" || _style == "text" ) {
height = _height > 1 ? _height : req_height;
}
// Minimap and log do not have a predetermined height
// (or they should allow caller to customize height)
window_panel win( custom_draw_func, _label.translated(), _label, height, width,
!has_flag( json_flag_W_DISABLED ) );
win.set_widget( this->id );
return win;
}
bool widget::uses_text_function()
{
switch( _var ) {
case widget_var::activity_text:
case widget_var::body_temp_text:
case widget_var::bp_armor_outer_text:
case widget_var::bp_status_text:
case widget_var::bp_status_sym_text:
case widget_var::bp_status_legend_text:
case widget_var::compass_text:
case widget_var::compass_legend_text:
case widget_var::date_text:
case widget_var::env_temp_text:
case widget_var::fatigue_text:
case widget_var::health_text:
case widget_var::hunger_text:
case widget_var::lighting_text:
case widget_var::mood_text:
case widget_var::moon_phase_text:
case widget_var::move_count_mode_text:
case widget_var::move_mode_letter:
case widget_var::move_mode_text:
case widget_var::pain_text:
case widget_var::overmap_loc_text:
case widget_var::overmap_text:
case widget_var::place_text:
case widget_var::power_text:
case widget_var::rad_badge_text:
case widget_var::safe_mode_text:
case widget_var::style_text:
case widget_var::thirst_text:
case widget_var::time_text:
case widget_var::veh_azimuth_text:
case widget_var::veh_cruise_text:
case widget_var::veh_fuel_text:
case widget_var::weariness_text:
case widget_var::weary_malus_text:
case widget_var::weather_text:
case widget_var::weight_text:
case widget_var::wielding_text:
case widget_var::wind_text:
return true;
default:
return false;
}
}
// Simple workaround from the copied widget from the panel to set the widget's height globally
static void set_height_for_widget( const widget_id &id, int height )
{
const std::vector<widget> &wlist = widget::get_all();
auto iter = std::find_if( wlist.begin(), wlist.end(), [&id]( const widget & w ) {
return w.getId() == id;
} );
if( iter != wlist.end() ) {
const_cast<widget *>( &*iter )->_height = height;
}
}
static std::set<bodypart_id> get_bodyparts_from_status_widgets()
{
std::set<bodypart_id> ret;
for( const widget &w : widget::get_all() ) {
if( w._var == widget_var::bp_status_sym_text ) {
ret.emplace( w._bp_id );
}
}
return ret;
}
// NOTE: Use max_width to split multi-line widgets across lines
std::string widget::color_text_function_string( const avatar &ava, unsigned int max_width )
{
std::string ret;
// Most text variables have both a string and a color.
// The string and color in `desc` will be converted to colorized text with markup.
std::pair<std::string, nc_color> desc;
// Set a default color
desc.second = c_light_gray;
// By default, colorize the string in desc.first with the color in desc.second.
bool apply_color = true;
// Don't bother updating the widget's height by default
bool update_height = false;
// Some helper display:: functions do their own internal colorization of the string.
// For those, desc.first is the already-colorized string, and apply_color is set to false.
switch( _var ) {
case widget_var::activity_text:
desc = display::activity_text_color( ava );
break;
case widget_var::body_temp_text:
desc = display::temp_text_color( ava );
break;
case widget_var::bp_armor_outer_text:
desc.first = display::colorized_bodypart_outer_armor( ava, _bp_id );
apply_color = false; // Item name already colorized by tname
break;
case widget_var::bp_status_text:
desc.first = display::colorized_bodypart_status_text( ava, _bp_id, id.str() );
apply_color = false; // Has embedded color already
break;
case widget_var::bp_status_sym_text:
desc.first = display::colorized_bodypart_status_sym_text( ava, _bp_id, id.str() );
apply_color = false; // Already colorized
break;
case widget_var::bp_status_legend_text:
desc.first = display::colorized_bodypart_status_legend_text( ava,
get_bodyparts_from_status_widgets(), id.str(),
_width == 0 ? max_width : _width, _height_max, _height );
update_height = true; // Dynamically adjusted height
apply_color = false; // Already colorized
break;
case widget_var::date_text:
desc.first = display::date_string();
break;
case widget_var::env_temp_text:
desc.first = display::get_temp( ava );
break;
case widget_var::fatigue_text:
desc = display::fatigue_text_color( ava );
break;
case widget_var::health_text:
desc = display::health_text_color( ava );
break;
case widget_var::hunger_text:
desc = display::hunger_text_color( ava );
break;
case widget_var::lighting_text:
desc = get_light_level( ava.fine_detail_vision_mod() );
break;
case widget_var::mood_text:
desc = display::morale_face_color( ava );
break;
case widget_var::moon_phase_text:
desc.first = display::get_moon();
break;
case widget_var::move_count_mode_text:
desc = display::move_count_and_mode_text_color( ava );
break;
case widget_var::move_mode_letter:
desc = display::move_mode_letter_color( ava );
break;
case widget_var::move_mode_text:
desc = display::move_mode_text_color( ava );
break;
case widget_var::pain_text:
desc = display::pain_text_color( ava );
break;
case widget_var::overmap_loc_text:
desc.first = display::overmap_position_text( ava.global_omt_location() );
break;
case widget_var::overmap_text:
desc.first = display::colorized_overmap_text( ava, _width == 0 ? max_width : _width, _height );
apply_color = false;
break;
case widget_var::place_text:
desc.first = overmap_buffer.ter( ava.global_omt_location() )->get_name();
break;
case widget_var::power_text:
desc = display::power_text_color( ava );
break;
case widget_var::rad_badge_text:
desc = display::rad_badge_text_color( ava );
break;
case widget_var::safe_mode_text:
desc = display::safe_mode_text_color( false );
break;
case widget_var::style_text:
desc.first = ava.martial_arts_data->selected_style_name( ava );
break;
case widget_var::thirst_text:
desc = display::thirst_text_color( ava );
break;
case widget_var::time_text:
desc.first = display::time_string( ava );
break;
case widget_var::veh_azimuth_text:
desc.first = display::vehicle_azimuth_text( ava );
break;
case widget_var::veh_cruise_text:
desc = display::vehicle_cruise_text_color( ava );
break;
case widget_var::veh_fuel_text:
desc = display::vehicle_fuel_percent_text_color( ava );
break;
case widget_var::weariness_text:
desc = display::weariness_text_color( ava );
break;
case widget_var::weary_malus_text:
desc = display::weary_malus_text_color( ava );
break;
case widget_var::weather_text:
desc = display::weather_text_color( ava );
break;
case widget_var::weight_text:
desc = display::weight_text_color( ava );
break;
case widget_var::wielding_text:
desc.first = ava.weapname();
break;
case widget_var::wind_text:
desc = display::wind_text_color( ava );
break;
case widget_var::compass_text:
desc.first = display::colorized_compass_text( _direction, _width );
apply_color = false; // Already colorized
break;
case widget_var::compass_legend_text:
desc.first = display::colorized_compass_legend_text( _width == 0 ? max_width : _width, _height_max,
_height );
update_height = true; // Dynamically adjusted height
apply_color = false; // Already colorized
break;
default:
debugmsg( "Unexpected widget_var %s - no text_color function defined",
io::enum_to_string<widget_var>( _var ) );