/
canvas.cpp
1563 lines (1391 loc) · 58.3 KB
/
canvas.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
/*
Copyright (C) 2007 - 2017 by Mark de Wever <koraq@xs4all.nl>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
/**
* @file
* Implementation of canvas.hpp.
*/
#define GETTEXT_DOMAIN "wesnoth-lib"
#include "gui/core/canvas.hpp"
#include "gui/core/canvas_private.hpp"
#include "font/text.hpp"
#include "formatter.hpp"
#include "gettext.hpp"
#include "image.hpp"
#include "gui/auxiliary/typed_formula.hpp"
#include "gui/core/log.hpp"
#include "gui/widgets/helper.hpp"
#include "sdl/rect.hpp"
#include "sdl/render_utils.hpp"
#include "video.hpp"
#include "wml_exception.hpp"
namespace gui2
{
namespace
{
/*WIKI
* @page = GUICanvasWML
*
* {{Autogenerated}}
*
* = Canvas =
*
* A canvas is a blank drawing area on which the user can draw several shapes.
* The drawing is done by adding WML structures to the canvas.
*/
/*WIKI
* @page = GUICanvasWML
* @begin{parent}{name="generic/state/draw/"}
*
* == Pre commit ==
* @begin{tag}{name="pre_commit"}{min="0"}{max="1"}
*
* This section contains the pre commit functions. These functions will be
* executed before the drawn canvas is applied on top of the normal
* background. There should only be one pre commit section and its order
* regarding the other shapes doesn't matter. The function has effect on the
* entire canvas, it's not possible to affect only a small part of the canvas.
*
* The section can have one of the following subsections.
*
* === Blur ===
* @begin{tag}{name="blur"}{min="0"}{max="1"}
*
* Blurs the background before applying the canvas. This doesn't make sense
* if the widget isn't semi-transparent.
*
* Keys:
* @begin{table}{config}
* depth & unsigned & 0 & The depth to blur. $
* @end{table}
* @end{tag}{name="blur"}
* @end{tag}{name="pre_commit"}
*/
/***** ***** ***** ***** ***** DRAWING PRIMITIVES ***** ***** ***** ***** *****/
static void set_renderer_color(SDL_Renderer* renderer, color_t color)
{
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
}
/**
* Draws a line on a surface.
*
* @pre The caller needs to make sure the entire line fits on
* the @p surface.
* @pre @p x2 >= @p x1
* @pre The @p surface is locked.
*
* @param canvas The canvas to draw upon, the caller should lock the
* surface before calling.
* @param color The color of the line to draw.
* @param x1 The start x coordinate of the line to draw.
* @param y1 The start y coordinate of the line to draw.
* @param x2 The end x coordinate of the line to draw.
* @param y2 The end y coordinate of the line to draw.
*/
static void draw_line(const int canvas_w,
const int canvas_h,
SDL_Renderer* renderer,
color_t color,
unsigned x1,
unsigned y1,
const unsigned x2,
unsigned y2)
{
DBG_GUI_D << "Shape: draw line from " << x1 << ',' << y1 << " to " << x2
<< ',' << y2 << " canvas width " << canvas_w << " canvas height "
<< canvas_h << ".\n";
assert(static_cast<int>(x1) < canvas_w);
assert(static_cast<int>(x2) < canvas_w);
assert(static_cast<int>(y1) < canvas_h);
assert(static_cast<int>(y2) < canvas_h);
set_renderer_color(renderer, color);
if(x1 == x2 && y1 == y2) {
// Handle single-pixel lines properly
SDL_RenderDrawPoint(renderer, x1, y1);
} else {
SDL_RenderDrawLine(renderer, x1, y1, x2, y2);
}
}
/**
* Draws a circle on a surface.
*
* @pre The circle must fit on the canvas.
* @pre The @p surface is locked.
*
* @param canvas The canvas to draw upon, the caller should lock the
* surface before calling.
* @param color The border color of the circle to draw.
* @param x_center The x coordinate of the center of the circle to draw.
* @param y_center The y coordinate of the center of the circle to draw.
* @param radius The radius of the circle to draw.
* @tparam octants A bitfield indicating which octants to draw, starting at twelve o'clock and moving clockwise.
*/
template<unsigned int octants = 0xff>
static void draw_circle(const int canvas_w,
const int canvas_h,
SDL_Renderer* renderer,
color_t color,
const int x_center,
const int y_center,
const int radius)
{
unsigned w = canvas_w;
DBG_GUI_D << "Shape: draw circle at " << x_center << ',' << y_center
<< " with radius " << radius << " canvas width " << w
<< " canvas height " << canvas_h << ".\n";
if(octants & 0x0f) assert((x_center + radius) < canvas_w);
if(octants & 0xf0) assert((x_center - radius) >= 0);
if(octants & 0x3c) assert((y_center + radius) < canvas_h);
if(octants & 0xc3) assert((y_center - radius) >= 0);
set_renderer_color(renderer, color);
// Algorithm based on
// http://de.wikipedia.org/wiki/Rasterung_von_Kreisen#Methode_von_Horn
// version of 2011.02.07.
int d = -static_cast<int>(radius);
int x = radius;
int y = 0;
std::vector<SDL_Point> points;
while(!(y > x)) {
if(octants & 0x04) points.push_back({x_center + x, y_center + y});
if(octants & 0x02) points.push_back({x_center + x, y_center - y});
if(octants & 0x20) points.push_back({x_center - x, y_center + y});
if(octants & 0x40) points.push_back({x_center - x, y_center - y});
if(octants & 0x08) points.push_back({x_center + y, y_center + x});
if(octants & 0x01) points.push_back({x_center + y, y_center - x});
if(octants & 0x10) points.push_back({x_center - y, y_center + x});
if(octants & 0x80) points.push_back({x_center - y, y_center - x});
d += 2 * y + 1;
++y;
if(d > 0) {
d += -2 * x + 2;
--x;
}
}
SDL_RenderDrawPoints(renderer, points.data(), points.size());
}
/**
* Draws a filled circle on a surface.
*
* @pre The circle must fit on the canvas.
* @pre The @p surface is locked.
*
* @param canvas The canvas to draw upon, the caller should lock the
* surface before calling.
* @param color The fill color of the circle to draw.
* @param x_center The x coordinate of the center of the circle to draw.
* @param y_center The y coordinate of the center of the circle to draw.
* @param radius The radius of the circle to draw.
* @tparam octants A bitfield indicating which octants to draw, starting at twelve o'clock and moving clockwise.
*/
template<unsigned int octants = 0xff>
static void fill_circle(const int canvas_w,
const int canvas_h,
SDL_Renderer* renderer,
color_t color,
const int x_center,
const int y_center,
const int radius)
{
unsigned w = canvas_w;
DBG_GUI_D << "Shape: draw filled circle at " << x_center << ',' << y_center
<< " with radius " << radius << " canvas width " << w
<< " canvas height " << canvas_h << ".\n";
if(octants & 0x0f) assert((x_center + radius) < canvas_w);
if(octants & 0xf0) assert((x_center - radius) >= 0);
if(octants & 0x3c) assert((y_center + radius) < canvas_h);
if(octants & 0xc3) assert((y_center - radius) >= 0);
set_renderer_color(renderer, color);
int d = -static_cast<int>(radius);
int x = radius;
int y = 0;
while(!(y > x)) {
// I use the formula of Bresenham's line algorithm to determine the boundaries of a segment.
// The slope of the line is always 1 or -1 in this case.
if(octants & 0x04) SDL_RenderDrawLine(renderer, x_center + x, y_center + y + 1, x_center + y + 1, y_center + y + 1); // x2 - 1 = y2 - (y_center + 1) + x_center
if(octants & 0x02) SDL_RenderDrawLine(renderer, x_center + x, y_center - y, x_center + y + 1, y_center - y); // x2 - 1 = y_center - y2 + x_center
if(octants & 0x20) SDL_RenderDrawLine(renderer, x_center - x - 1, y_center + y + 1, x_center - y - 2, y_center + y + 1); // x2 + 1 = (y_center + 1) - y2 + (x_center - 1)
if(octants & 0x40) SDL_RenderDrawLine(renderer, x_center - x - 1, y_center - y, x_center - y - 2, y_center - y); // x2 + 1 = y2 - y_center + (x_center - 1)
if(octants & 0x08) SDL_RenderDrawLine(renderer, x_center + y, y_center + x + 1, x_center + y, y_center + y + 1); // y2 = x2 - x_center + (y_center + 1)
if(octants & 0x01) SDL_RenderDrawLine(renderer, x_center + y, y_center - x, x_center + y, y_center - y); // y2 = x_center - x2 + y_center
if(octants & 0x10) SDL_RenderDrawLine(renderer, x_center - y - 1, y_center + x + 1, x_center - y - 1, y_center + y + 1); // y2 = (x_center - 1) - x2 + (y_center + 1)
if(octants & 0x80) SDL_RenderDrawLine(renderer, x_center - y - 1, y_center - x, x_center - y - 1, y_center - y); // y2 = x2 - (x_center - 1) + y_center
d += 2 * y + 1;
++y;
if(d > 0) {
d += -2 * x + 2;
--x;
}
}
}
} // namespace
/*WIKI - unclassified
* This code can be used by a parser to generate the wiki page
* structure
* [tag name]
* param type_info description
*
* param Name of the parameter.
*
* type_info = ( type = default_value) The info about a optional parameter.
* type_info = ( type ) The info about a mandatory parameter
* type_info = [ type_info ] The info about a conditional parameter
* description should explain the reason.
*
* description Description of the parameter.
*
*
*
*
* Formulas are a function between brackets, that way the engine can see whether
* there is standing a plain number or a formula eg:
* 0 A value of zero
* (0) A formula returning zero
*
* When formulas are available the text should state the available variables
* which are available in that function.
*/
/*WIKI
* @page = GUIVariable
*
* {{Autogenerated}}
*
* = Variables =
*
* In various parts of the GUI there are several variables types in use. This
* page describes them.
*
* == Simple types ==
*
* The simple types are types which have one value or a short list of options.
*
* @begin{table}{variable_types}
* unsigned & Unsigned number (positive whole numbers
* and zero). $
* f_unsigned & Unsigned number or formula returning an
* unsigned number. $
* int & Signed number (whole numbers). $
* f_int & Signed number or formula returning an
* signed number. $
* bool & A boolean value accepts the normal
* values as the rest of the game. $
* f_bool & Boolean value or a formula returning a
* boolean value. $
* string & A text. $
* tstring & A translatable string. $
* f_tstring & Formula returning a translatable string.
* $
* function & A string containing a set of function
* definition for the formula language. $
*
* color & A string which contains the color, this
* a group of 4 numbers between 0 and 255
* separated by a comma. The numbers are red
* component, green component, blue
* component and alpha. A color of 0 is not
* available. An alpha of 255 is fully
* transparent. Omitted values are set to
* 0. $
* f_color & A string which contains a color, or a formula
* which evaluates to a list containing the color's
* components. $
*
* font_style & A string which contains the style of the
* font:
* @* normal normal font
* @* bold bold font
* @* italic italic font
* @* underline underlined font
* @-Since SDL has problems combining these
* styles only one can be picked. Once SDL
* will allow multiple options, this type
* will be transformed to a comma separated
* list. If empty we default to the normal
* style. Since the render engine is
* replaced by Pango markup this field will
* change later on. Note widgets that allow
* marked up text can use markup to change
* the font style. $
*
* v_align & Vertical alignment; how an item is
* aligned vertically in the available
* space. Possible values:
* @* top aligned at the top
* @* bottom aligned at the bottom
* @* center centered
* @-When nothing is set or an another
* value as in the list the item is
* centered. $
*
* h_align & Horizontal alignment; how an item is
* aligned horizontal in the available
* space. Possible values:
* @* left aligned at the left side
* @* right aligned at the right side
* @* center centered $
*
* f_h_align & A horizontal alignment or a formula
* returning a horizontal alignment. $
*
* border & Comma separated list of borders to use.
* Possible values:
* @* left border at the left side
* @* right border at the right side
* @* top border at the top
* @* bottom border at the bottom
* @* all alias for "left, right, top,
* bottom" $
*
* scrollbar_mode & How to show the scrollbar of a widget.
* Possible values:
* @* always The scrollbar is always
* shown, regardless whether it's required
* or not.
* @* never The scrollbar is never
* shown, even not when needed. (Note when
* setting this mode dialogs might
* not properly fit anymore).
* @* auto Shows the scrollbar when
* needed. The widget will reserve space for
* the scrollbar, but only show when needed.
* @* initial_auto Like auto, but when the
* scrollbar is not needed the space is not
* reserved.
* @-Use auto when the list can be changed
* dynamically eg the game list in the
* lobby. For optimization you can also
* use auto when you really expect a
* scrollbar, but don't want it to be shown
* when not needed eg the language list
* will need a scrollbar on most screens. $
*
* resize_mode & Determines how an image is resized.
* Possible values:
* @* scale The image is scaled.
* @* stretch The first row or column
* of pixels is copied over the entire
* image. (Can only be used to scale resize
* in one direction, else falls
* back to scale.)
* @* tile The image is placed
* several times until the entire surface
* is filled. The last images are
* truncated.
* @* tile_center Like tile, but the
* image is first centered on the screen. $
*
* grow_direction & Determines how an image is resized.
* Possible values:
* @* scale The image is scaled.
* @* stretch The first row or column
* of pixels is copied over the entire
* image. (Can only be used to scale resize
* in one direction, else falls
* back to scale.)
* @* tile The image is placed
* several times until the entire surface
* is filled. The last images are
* truncated. $
* @end{table}
* @allow{type}{name="unsigned"}{value="^\d+$"}
* @allow{type}{name="f_unsigned"}{value="^.+$"}
* @allow{type}{name="int"}{value="^-?\d+$"}
* @allow{type}{name="f_int"}{value="^.*$"}
* @allow{type}{name="bool"}{value="^true|false|yes|no$"}
* @allow{type}{name="f_bool"}{value="^.*$"}
* @allow{type}{name="string"}{value="^.*$"}
* @allow{type}{name="t_string"}{value="^_?.*$"}
* @allow{type}{name="f_string"}{value="^.*$"}
* @allow{type}{name="f_tstring"}{value="^_?.*$"}
* @allow{type}{name="function"}{value="^_?.*$"}
*
* @allow{type}{name="color"}{value="^(?:2[0-5][0-5]|[01]?\d?\d)[.,]\s*(?:2[0-5][0-5]|[01]?\d?\d)[.,]\s*(?:2[0-5][0-5]|[01]?\d?\d)[.,]\s*(?:2[0-5][0-5]|[01]?\d?\d)$"}
*
* @allow{type}{name="font_style"}{value="^(normal|bold|italic|underline)?$"}
* @allow{type}{name="v_align"}{value="^top|bottom|center$"}
* @allow{type}{name="h_align"}{value="^left|right|center$"}
* @allow{type}{name="f_h_align"}{value="^.*$"}
* @allow{type}{name="border"}{value="^(top|bottom|left|right|all)?(,\s*(top|bottom|left|right|all))*$"}
* @allow{type}{name="scrollbar_mode"}{value="^always|never|auto|initial_auto$"}
* @allow{type}{name="resize_mode"}{value="^scale|stretch|tile$"}
* @allow{type}{name="grow_direction"}{value="^horizontal|vertical$"}
*
* @remove{type}{name="section"}
* @remove{type}{name="config"}
* @remove{type}{name="grid"}
* == Section types ==
*
* For more complex parts, there are sections. Sections contain of several
* lines of WML and can have sub sections. For example a grid has sub sections
* which contain various widgets. Here's the list of sections.
*
* @begin{table}{variable_types}
* section & A generic section. The documentation
* about the section should describe the
* section in further detail. $
*
* grid & A grid contains several widgets. (TODO
* add link to generic grid page.) $
* @end{table}
*/
/***** ***** ***** ***** ***** LINE ***** ***** ***** ***** *****/
/*WIKI
* @page = GUICanvasWML
*
* == Line ==
* @begin{tag}{name="line"}{min="0"}{max="-1"}
* Definition of a line.
*
* Keys:
* @begin{table}{config}
* x1 & f_unsigned & 0 & The x coordinate of the startpoint. $
* y1 & f_unsigned & 0 & The y coordinate of the startpoint. $
* x2 & f_unsigned & 0 & The x coordinate of the endpoint. $
* y2 & f_unsigned & 0 & The y coordinate of the endpoint. $
* color & f_color & "" & The color of the line. $
* thickness & unsigned & 0 & The thickness of the line if 0 nothing
* is drawn. $
* debug & string & "" & Debug message to show upon creation
* this message is not stored. $
* @end{table}
* @end{tag}{name="line"}
*
* <span id="general_variables">Variables:</span>.
* @begin{table}{formula}
* width & unsigned & The width of the canvas. $
* height & unsigned & The height of the canvas. $
* text & tstring & The text to render on the widget. $
* text_maximum_width & unsigned & The maximum width available for the
* text on the widget. $
* text_maximum_height & unsigned & The maximum height available for the
* text on the widget. $
* text_wrap_mode & int & When the text doesn't fit in the
* available width there are several ways
* to fix that. This variable holds the
* best method. (NOTE this is a 'hidden'
* variable meant to copy state from a
* widget to its canvas so there's no
* reason to use this variable and thus
* its values are not listed and might
* change without further notice.) $
* text_alignment & h_align & The way the text is aligned inside the
* canvas. $
*@end{table}
*
* The size variables are copied to the window and will be determined at
* runtime. This is needed since the main window can be resized and the dialog
* needs to resize accordingly. The following variables are available:
* @begin{table}{formula}
* screen_width & unsigned & The usable width of the Wesnoth main
* window. $
* screen_height & unsigned & The usable height of the Wesnoth main
* window. $
* gamemapx_offset & unsigned & The distance between left edge of the
* screen and the game map. $
* gamemap_width & unsigned & The usable width of the Wesnoth gamemap,
* if no gamemap shown it's the same value
* as screen_width. $
* gamemap_height & unsigned & The usable height of the Wesnoth
* gamemap, if no gamemap shown it's the
* same value as screen_height. $
*
* mouse_x & unsigned & The x coordinate of the mouse pointer. $
* mouse_y & unsigned & The y coordinate of the mouse pointer. $
*
* window_width & unsigned & The window width. This value has two
* meanings during the layout phase. This
* only applies if automatic placement is
* not enabled.
* - When set to 0 it should return the
* wanted maximum width. If no maximum
* is wanted it should be set to the
* '"(screen_width)"'.
* - When not equal to 0 its value is the
* best width for the window. When the
* size should remain unchanged it
* should be set to '"(window_width)"'.
* $
*
* window_height & unsigned & The window height. This value has two
* meanings during the layout phase. This
* only applies if automatic placement is
* not enabled.
* - When set to 0 it should return the
* wanted maximum height. If no maximum
* is wanted it should be set to the
* '"(screen_height)"'.
* - When not equal to 0 its value is the
* best height for the window. When the
* size should remain unchanged it
* should be set to '"(window_height)"'.
* $
*
* size_request_mode & string & A field foo:
* - maximum
* - size
*
* @end{table}
*
* Note when drawing the valid coordinates are:<br>
* 0 -> width - 1 <br>
* 0 -> height -1
*
* Drawing outside this area will result in unpredictable results including
* crashing. (That should be fixed, when encountered.)
*/
line_shape::line_shape(const config& cfg)
: shape(cfg)
, x1_(cfg["x1"])
, y1_(cfg["y1"])
, x2_(cfg["x2"])
, y2_(cfg["y2"])
, color_(cfg["color"])
, thickness_(cfg["thickness"])
{
const std::string& debug = (cfg["debug"]);
if(!debug.empty()) {
DBG_GUI_P << "Line: found debug message '" << debug << "'.\n";
}
}
void line_shape::draw(
const int canvas_w,
const int canvas_h,
SDL_Renderer* renderer,
wfl::map_formula_callable& variables)
{
/**
* @todo formulas are now recalculated every draw cycle which is a bit silly
* unless there has been a resize. So to optimize we should use an extra
* flag or do the calculation in a separate routine.
*/
const unsigned x1 = x1_(variables);
const unsigned y1 = y1_(variables);
const unsigned x2 = x2_(variables);
const unsigned y2 = y2_(variables);
DBG_GUI_D << "Line: draw from " << x1 << ',' << y1 << " to " << x2 << ','
<< y2 << " canvas size " << canvas_w << ',' << canvas_h
<< ".\n";
VALIDATE(static_cast<int>(x1) < canvas_w
&& static_cast<int>(x2) < canvas_w
&& static_cast<int>(y1) < canvas_h
&& static_cast<int>(y2) < canvas_h,
_("Line doesn't fit on canvas."));
// @todo FIXME respect the thickness.
draw_line(canvas_w, canvas_h, renderer, color_(variables), x1, y1, x2, y2);
}
/***** ***** ***** ***** ***** Rectangle ***** ***** ***** ***** *****/
/*WIKI
* @page = GUICanvasWML
*
* == Rectangle ==
* @begin{tag}{name="rectangle"}{min="0"}{max="-1"}
*
* Definition of a rectangle. When drawing a rectangle it doesn't get blended on
* the surface but replaces the pixels instead. A blitting flag might be added
* later if needed.
*
* Keys:
* @begin{table}{config}
* x & f_unsigned & 0 & The x coordinate of the top left corner.
* $
* y & f_unsigned & 0 & The y coordinate of the top left corner.
* $
* w & f_unsigned & 0 & The width of the rectangle. $
* h & f_unsigned & 0 & The height of the rectangle. $
* border_thickness & unsigned & 0 &
* The thickness of the border; if the
* thickness is zero it's not drawn. $
* border_color & f_color & "" & The color of the border; if empty it's
* not drawn. $
* fill_color & f_color & "" & The color of the interior; if omitted
* it's not drawn. $
* debug & string & "" & Debug message to show upon creation;
* this message is not stored. $
* @end{table}
* @end{tag}{name="rectangle"}
* Variables:
* See [[#general_variables|Line]].
*
*/
rectangle_shape::rectangle_shape(const config& cfg)
: shape(cfg)
, x_(cfg["x"])
, y_(cfg["y"])
, w_(cfg["w"])
, h_(cfg["h"])
, border_thickness_(cfg["border_thickness"])
, border_color_(cfg["border_color"], color_t::null_color())
, fill_color_(cfg["fill_color"], color_t::null_color())
{
// Check if a raw color string evaluates to a null color.
if(!border_color_.has_formula() && border_color_().null()) {
border_thickness_ = 0;
}
const std::string& debug = (cfg["debug"]);
if(!debug.empty()) {
DBG_GUI_P << "Rectangle: found debug message '" << debug << "'.\n";
}
}
void rectangle_shape::draw(
const int canvas_w,
const int canvas_h,
SDL_Renderer* renderer,
wfl::map_formula_callable& variables)
{
/**
* @todo formulas are now recalculated every draw cycle which is a bit
* silly unless there has been a resize. So to optimize we should use an
* extra flag or do the calculation in a separate routine.
*/
const int x = x_(variables);
const int y = y_(variables);
const int w = w_(variables);
const int h = h_(variables);
DBG_GUI_D << "Rectangle: draw from " << x << ',' << y << " width " << w
<< " height " << h << " canvas size " << canvas_w << ','
<< canvas_h << ".\n";
VALIDATE(x < canvas_w
&& x + w <= canvas_w
&& y < canvas_h
&& y + h <= canvas_h, _("Rectangle doesn't fit on canvas."));
const color_t fill_color = fill_color_(variables);
// Fill the background, if applicable
if(!fill_color.null() && w && h) {
set_renderer_color(renderer, fill_color);
SDL_Rect area {
x + border_thickness_,
y + border_thickness_,
w - (border_thickness_ * 2),
h - (border_thickness_ * 2)
};
SDL_RenderFillRect(renderer, &area);
}
// Draw the border
for(int i = 0; i < border_thickness_; ++i) {
SDL_Rect dimensions {
x + i,
y + i,
w - (i * 2),
h - (i * 2)
};
set_renderer_color(renderer, border_color_(variables));
SDL_RenderDrawRect(renderer, &dimensions);
}
}
/***** ***** ***** ***** ***** Rounded Rectangle ***** ***** ***** ***** *****/
/*WIKI
* @page = GUICanvasWML
*
* == Rounded Rectangle ==
* @begin{tag}{name="round_rectangle"}{min="0"}{max="-1"}
*
* Definition of a rounded rectangle. When drawing a rounded rectangle it doesn't get blended on
* the surface but replaces the pixels instead. A blitting flag might be added
* later if needed.
*
* Keys:
* @begin{table}{config}
* x & f_unsigned & 0 & The x coordinate of the top left corner.
* $
* y & f_unsigned & 0 & The y coordinate of the top left corner.
* $
* w & f_unsigned & 0 & The width of the rounded rectangle. $
* h & f_unsigned & 0 & The height of the rounded rectangle. $
* corner_radius & f_unsigned & 0 &The radius of the rectangle's corners. $
* border_thickness & unsigned & 0 &
* The thickness of the border; if the
* thickness is zero it's not drawn. $
* border_color & f_color & "" & The color of the border; if empty it's
* not drawn. $
* fill_color & f_color & "" & The color of the interior; if omitted
* it's not drawn. $
* debug & string & "" & Debug message to show upon creation;
* this message is not stored. $
* @end{table}
* @end{tag}{name="round_rectangle"}
* Variables:
* See [[#general_variables|Line]].
*
*/
round_rectangle_shape::round_rectangle_shape(const config& cfg)
: shape(cfg)
, x_(cfg["x"])
, y_(cfg["y"])
, w_(cfg["w"])
, h_(cfg["h"])
, r_(cfg["corner_radius"])
, border_thickness_(cfg["border_thickness"])
, border_color_(cfg["border_color"], color_t::null_color())
, fill_color_(cfg["fill_color"], color_t::null_color())
{
// Check if a raw color string evaluates to a null color.
if(!border_color_.has_formula() && border_color_().null()) {
border_thickness_ = 0;
}
const std::string& debug = (cfg["debug"]);
if(!debug.empty()) {
DBG_GUI_P << "Rounded Rectangle: found debug message '" << debug << "'.\n";
}
}
void round_rectangle_shape::draw(
const int canvas_w,
const int canvas_h,
SDL_Renderer* renderer,
wfl::map_formula_callable& variables)
{
/**
* @todo formulas are now recalculated every draw cycle which is a bit
* silly unless there has been a resize. So to optimize we should use an
* extra flag or do the calculation in a separate routine.
*/
const int x = x_(variables);
const int y = y_(variables);
const int w = w_(variables);
const int h = h_(variables);
const int r = r_(variables);
DBG_GUI_D << "Rounded Rectangle: draw from " << x << ',' << y << " width " << w
<< " height " << h << " canvas size " << canvas_w << ','
<< canvas_h << ".\n";
VALIDATE(x < canvas_w
&& x + w <= canvas_w
&& y < canvas_h
&& y + h <= canvas_h, _("Rounded Rectangle doesn't fit on canvas."));
const color_t fill_color = fill_color_(variables);
// Fill the background, if applicable
if(!fill_color.null() && w && h) {
set_renderer_color(renderer, fill_color);
static const int count = 3;
SDL_Rect area[count] {
{x + r, y + border_thickness_, w - r * 2, r - border_thickness_ + 1},
{x + border_thickness_, y + r + 1, w - border_thickness_ * 2, h - r * 2},
{x + r, y - r + h + 1, w - r * 2, r - border_thickness_},
};
SDL_RenderFillRects(renderer, area, count);
fill_circle<0xc0>(canvas_w, canvas_h, renderer, fill_color, x + r, y + r, r);
fill_circle<0x03>(canvas_w, canvas_h, renderer, fill_color, x + w - r, y + r, r);
fill_circle<0x30>(canvas_w, canvas_h, renderer, fill_color, x + r, y + h - r, r);
fill_circle<0x0c>(canvas_w, canvas_h, renderer, fill_color, x + w - r, y + h - r, r);
}
const color_t border_color = border_color_(variables);
// Draw the border
for(int i = 0; i < border_thickness_; ++i) {
set_renderer_color(renderer, border_color);
SDL_RenderDrawLine(renderer, x + r, y + i, x + w - r, y + i);
SDL_RenderDrawLine(renderer, x + r, y + h - i, x + w - r, y + h - i);
SDL_RenderDrawLine(renderer, x + i, y + r, x + i, y + h - r);
SDL_RenderDrawLine(renderer, x + w - i, y + r, x + w - i, y + h - r);
draw_circle<0xc0>(canvas_w, canvas_h, renderer, border_color, x + r, y + r, r - i);
draw_circle<0x03>(canvas_w, canvas_h, renderer, border_color, x + w - r, y + r, r - i);
draw_circle<0x30>(canvas_w, canvas_h, renderer, border_color, x + r, y + h - r, r - i);
draw_circle<0x0c>(canvas_w, canvas_h, renderer, border_color, x + w - r, y + h - r, r - i);
}
}
/***** ***** ***** ***** ***** CIRCLE ***** ***** ***** ***** *****/
/*WIKI
* @page = GUICanvasWML
*
* == Circle ==
* @begin{tag}{name="circle"}{min="0"}{max="-1"}
*
* Definition of a circle. When drawing a circle it doesn't get blended on
* the surface but replaces the pixels instead. A blitting flag might be
* added later if needed.
*
* Keys:
* @begin{table}{config}
* x & f_unsigned & 0 & The x coordinate of the center. $
* y & f_unsigned & 0 & The y coordinate of the center. $
* radius & f_unsigned & 0 & The radius of the circle; if 0 nothing is
* drawn. $
* border_thickness & unsigned & "" &
* The border thickness of the circle. $
* border_color & f_color & "" & The color of the circle. $
* fill_color & f_color & "" & The color of the circle. $
* debug & string & "" & Debug message to show upon creation; this
* message is not stored. $
* @end{table}
* @end{tag}{name="circle"}
* Variables:
* See [[#general_variables|Line]].
*
* Drawing outside the area will result in unpredictable results including
* crashing. (That should be fixed, when encountered.)
*/
circle_shape::circle_shape(const config& cfg)
: shape(cfg)
, x_(cfg["x"])
, y_(cfg["y"])
, radius_(cfg["radius"])
, border_color_(cfg["border_color"])
, fill_color_(cfg["fill_color"])
, border_thickness_(cfg["border_thickness"].to_int(1))
{
const std::string& debug = (cfg["debug"]);
if(!debug.empty()) {
DBG_GUI_P << "Circle: found debug message '" << debug << "'.\n";
}
}
void circle_shape::draw(
const int canvas_w,
const int canvas_h,
SDL_Renderer* renderer,
wfl::map_formula_callable& variables)
{
/**
* @todo formulas are now recalculated every draw cycle which is a bit
* silly unless there has been a resize. So to optimize we should use an
* extra flag or do the calculation in a separate routine.
*/
const unsigned x = x_(variables);
const unsigned y = y_(variables);
const unsigned radius = radius_(variables);
DBG_GUI_D << "Circle: drawn at " << x << ',' << y << " radius " << radius
<< " canvas size " << canvas_w << ',' << canvas_h << ".\n";
VALIDATE_WITH_DEV_MESSAGE(
static_cast<int>(x - radius) >= 0,
_("Circle doesn't fit on canvas."),
formatter() << "x = " << x << ", radius = " << radius);
VALIDATE_WITH_DEV_MESSAGE(
static_cast<int>(y - radius) >= 0,
_("Circle doesn't fit on canvas."),
formatter() << "y = " << y << ", radius = " << radius);
VALIDATE_WITH_DEV_MESSAGE(
static_cast<int>(x + radius) < canvas_w,
_("Circle doesn't fit on canvas."),
formatter() << "x = " << x << ", radius = " << radius
<< "', canvas width = " << canvas_w << ".");
VALIDATE_WITH_DEV_MESSAGE(
static_cast<int>(y + radius) < canvas_h,
_("Circle doesn't fit on canvas."),
formatter() << "y = " << y << ", radius = " << radius
<< "', canvas height = " << canvas_h << ".");
const color_t fill_color = fill_color_(variables);
if(!fill_color.null() && radius) {
fill_circle(canvas_w, canvas_h, renderer, fill_color, x, y, radius);
}
const color_t border_color = border_color_(variables);
for(unsigned int i = 0; i < border_thickness_; i++) {
draw_circle(canvas_w, canvas_h, renderer, border_color, x, y, radius - i);
}
}
/***** ***** ***** ***** ***** IMAGE ***** ***** ***** ***** *****/
/*WIKI
* @page = GUICanvasWML
*
* == Image ==
* @begin{tag}{name="image"}{min="0"}{max="-1"}
* Definition of an image.
*
* Keys:
* @begin{table}{config}
* x & f_unsigned & 0 & The x coordinate of the top left corner.
* $
* y & f_unsigned & 0 & The y coordinate of the top left corner.
* $
* w & f_unsigned & 0 & The width of the image, if not zero the
* image will be scaled to the desired
* width. $
* h & f_unsigned & 0 & The height of the image, if not zero the
* image will be scaled to the desired
* height. $
* resize_mode & resize_mode & scale &
* Determines how an image is scaled to fit
* the wanted size. $
* vertical_mirror & f_bool & false &
* Mirror the image over the vertical axis.
* $
* name & f_string & "" & The name of the image. $
* debug & string & "" & Debug message to show upon creation
* this message is not stored. $
*
* @end{table}
* @end{tag}{name="image"}
* Variables:
* @begin{table}{formula}
* image_width & unsigned & The width of the image, either the