00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #define GETTEXT_DOMAIN "wesnoth-lib"
00022
00023 #include "gui/auxiliary/canvas.hpp"
00024
00025 #include "config.hpp"
00026 #include "../../image.hpp"
00027 #include "foreach.hpp"
00028 #include "formatter.hpp"
00029 #include "gettext.hpp"
00030 #include "gui/auxiliary/formula.hpp"
00031 #include "gui/auxiliary/log.hpp"
00032 #include "gui/widgets/helper.hpp"
00033 #include "../../text.hpp"
00034 #include "wml_exception.hpp"
00035
00036 namespace gui2 {
00037
00038 namespace {
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096 static void put_pixel(
00097 const ptrdiff_t start
00098 , const Uint32 color
00099 , const unsigned w
00100 , const unsigned x
00101 , const unsigned y)
00102 {
00103 *reinterpret_cast<Uint32*>(start + (y * w * 4) + x * 4) = color;
00104 }
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122 static void draw_line(
00123 surface& canvas
00124 , Uint32 color
00125 , unsigned x1
00126 , unsigned y1
00127 , const unsigned x2
00128 , unsigned y2)
00129 {
00130 color = SDL_MapRGBA(canvas->format,
00131 ((color & 0xFF000000) >> 24),
00132 ((color & 0x00FF0000) >> 16),
00133 ((color & 0x0000FF00) >> 8),
00134 ((color & 0x000000FF)));
00135
00136 ptrdiff_t start = reinterpret_cast<ptrdiff_t>(canvas->pixels);
00137 unsigned w = canvas->w;
00138
00139 DBG_GUI_D << "Shape: draw line from "
00140 << x1 << ',' << y1 << " to " << x2 << ',' << y2
00141 << " canvas width " << w << " canvas height "
00142 << canvas->h << ".\n";
00143
00144 assert(static_cast<int>(x1) < canvas->w);
00145 assert(static_cast<int>(x2) < canvas->w);
00146 assert(static_cast<int>(y1) < canvas->h);
00147 assert(static_cast<int>(y2) < canvas->h);
00148
00149
00150 if(x1 == x2) {
00151 if(y2 < y1) {
00152 std::swap(y1, y2);
00153 }
00154
00155 for(unsigned y = y1; y <= y2; ++y) {
00156 put_pixel(start, color, w, x1, y);
00157 }
00158 return;
00159 }
00160
00161
00162 if(y1 == y2) {
00163 for(unsigned x = x1; x <= x2; ++x) {
00164 put_pixel(start, color, w, x, y1);
00165 }
00166 return;
00167 }
00168
00169
00170
00171
00172 const int dx = x2 - x1;
00173 const int dy = abs(static_cast<int>(y2 - y1));
00174 const int step_x = 1;
00175 const int step_y = y1 < y2 ? 1 : -1;
00176 int err = (dx > dy ? dx : -dy) / 2;
00177 int e2;
00178
00179 for(;;){
00180 put_pixel(start, color, w, x1, y1);
00181 if(x1 == x2 && y1 == y2) {
00182 break;
00183 }
00184 e2 = err;
00185 if(e2 > -dx) {
00186 err -= dy;
00187 x1 += step_x;
00188 }
00189 if(e2 < dy) {
00190 err += dx;
00191 y1 += step_y;
00192 }
00193 }
00194 }
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209 static void draw_circle(
00210 surface& canvas
00211 , Uint32 color
00212 , const unsigned x_centre
00213 , const unsigned y_centre
00214 , const unsigned radius)
00215 {
00216 color = SDL_MapRGBA(canvas->format,
00217 ((color & 0xFF000000) >> 24),
00218 ((color & 0x00FF0000) >> 16),
00219 ((color & 0x0000FF00) >> 8),
00220 ((color & 0x000000FF)));
00221
00222 ptrdiff_t start = reinterpret_cast<ptrdiff_t>(canvas->pixels);
00223 unsigned w = canvas->w;
00224
00225 DBG_GUI_D << "Shape: draw circle at "
00226 << x_centre << ',' << y_centre
00227 << " with radius " << radius
00228 << " canvas width " << w << " canvas height "
00229 << canvas->h << ".\n";
00230
00231 assert(static_cast<int>(x_centre + radius) < canvas->w);
00232 assert(static_cast<int>(x_centre - radius) >= 0);
00233 assert(static_cast<int>(y_centre + radius) < canvas->h);
00234 assert(static_cast<int>(y_centre - radius) >= 0);
00235
00236
00237
00238
00239 int d = -static_cast<int>(radius);
00240 int x = radius;
00241 int y = 0;
00242 while(!(y > x)) {
00243 put_pixel(start, color, w, x_centre + x, y_centre + y);
00244 put_pixel(start, color, w, x_centre + x, y_centre - y);
00245 put_pixel(start, color, w, x_centre - x, y_centre + y);
00246 put_pixel(start, color, w, x_centre - x, y_centre - y);
00247
00248 put_pixel(start, color, w, x_centre + y, y_centre + x);
00249 put_pixel(start, color, w, x_centre + y, y_centre - x);
00250 put_pixel(start, color, w, x_centre - y, y_centre + x);
00251 put_pixel(start, color, w, x_centre - y, y_centre - x);
00252
00253 d += 2 * y + 1;
00254 ++y;
00255 if(d > 0) {
00256 d += -2 * x + 2;
00257 --x;
00258 }
00259 }
00260 }
00261
00262
00263
00264
00265 class tline
00266 : public tcanvas::tshape
00267 {
00268 public:
00269
00270
00271
00272
00273
00274
00275
00276
00277 explicit tline(const config& cfg);
00278
00279
00280 void draw(surface& canvas
00281 , const game_logic::map_formula_callable& variables);
00282
00283 private:
00284 tformula<unsigned>
00285 x1_,
00286 y1_,
00287 x2_,
00288 y2_;
00289
00290
00291 Uint32 color_;
00292
00293
00294
00295
00296
00297
00298
00299
00300 unsigned thickness_;
00301 };
00302
00303 tline::tline(const config& cfg)
00304 : x1_(cfg["x1"])
00305 , y1_(cfg["y1"])
00306 , x2_(cfg["x2"])
00307 , y2_(cfg["y2"])
00308 , color_(decode_color(cfg["color"]))
00309 , thickness_(cfg["thickness"])
00310 {
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567 const std::string& debug = (cfg["debug"]);
00568 if(!debug.empty()) {
00569 DBG_GUI_P << "Line: found debug message '" << debug << "'.\n";
00570 }
00571 }
00572
00573 void tline::draw(surface& canvas
00574 , const game_logic::map_formula_callable& variables)
00575 {
00576
00577
00578
00579
00580
00581
00582 const unsigned x1 = x1_(variables);
00583 const unsigned y1 = y1_(variables);
00584 const unsigned x2 = x2_(variables);
00585 const unsigned y2 = y2_(variables);
00586
00587 DBG_GUI_D << "Line: draw from "
00588 << x1 << ',' << y1 << " to " << x2 << ',' << y2
00589 << " canvas size " << canvas->w << ',' << canvas->h << ".\n";
00590
00591 VALIDATE(
00592 static_cast<int>(x1) < canvas->w
00593 && static_cast<int>(x2) < canvas->w
00594 && static_cast<int>(y1) < canvas->h
00595 && static_cast<int>(y2) < canvas->h
00596 , _("Line doesn't fit on canvas."));
00597
00598
00599
00600
00601
00602
00603
00604 surface_lock locker(canvas);
00605 if(x1 > x2) {
00606
00607 draw_line(canvas, color_, x2, y2, x1, y1);
00608 } else {
00609 draw_line(canvas, color_, x1, y1, x2, y2);
00610 }
00611 }
00612
00613
00614
00615
00616 class trectangle
00617 : public tcanvas::tshape
00618 {
00619 public:
00620
00621
00622
00623
00624
00625
00626
00627
00628 explicit trectangle(const config& cfg);
00629
00630
00631 void draw(surface& canvas
00632 , const game_logic::map_formula_callable& variables);
00633
00634 private:
00635 tformula<unsigned>
00636 x_,
00637 y_,
00638 w_,
00639 h_;
00640
00641
00642
00643
00644
00645
00646 unsigned border_thickness_;
00647
00648
00649
00650
00651
00652
00653 Uint32 border_color_;
00654
00655
00656
00657
00658
00659
00660 Uint32 fill_color_;
00661 };
00662
00663 trectangle::trectangle(const config& cfg)
00664 : x_(cfg["x"])
00665 , y_(cfg["y"])
00666 , w_(cfg["w"])
00667 , h_(cfg["h"])
00668 , border_thickness_(cfg["border_thickness"])
00669 , border_color_(decode_color(cfg["border_color"]))
00670 , fill_color_(decode_color(cfg["fill_color"]))
00671 {
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703 if(border_color_ == 0) {
00704 border_thickness_ = 0;
00705 }
00706
00707 const std::string& debug = (cfg["debug"]);
00708 if(!debug.empty()) {
00709 DBG_GUI_P << "Rectangle: found debug message '" << debug << "'.\n";
00710 }
00711 }
00712
00713 void trectangle::draw(surface& canvas
00714 , const game_logic::map_formula_callable& variables)
00715 {
00716
00717
00718
00719
00720
00721 const unsigned x = x_(variables);
00722 const unsigned y = y_(variables);
00723 const unsigned w = w_(variables);
00724 const unsigned h = h_(variables);
00725
00726 DBG_GUI_D << "Rectangle: draw from " << x << ',' << y
00727 << " width " << w << " height " << h
00728 << " canvas size " << canvas->w << ',' << canvas->h << ".\n";
00729
00730 VALIDATE(
00731 static_cast<int>(x) < canvas->w
00732 && static_cast<int>(x + w) <= canvas->w
00733 && static_cast<int>(y) < canvas->h
00734 && static_cast<int>(y + h) <= canvas->h
00735 , _("Rectangle doesn't fit on canvas."));
00736
00737
00738 surface_lock locker(canvas);
00739
00740
00741 for(unsigned i = 0; i < border_thickness_; ++i) {
00742
00743 const unsigned left = x + i;
00744 const unsigned right = left + w - (i * 2) - 1;
00745 const unsigned top = y + i;
00746 const unsigned bottom = top + h - (i * 2) - 1;
00747
00748
00749 draw_line(canvas, border_color_, left, top, right, top);
00750
00751
00752 draw_line(canvas, border_color_, right, top, right, bottom);
00753
00754
00755 draw_line(canvas, border_color_, left, bottom, right, bottom);
00756
00757
00758 draw_line(canvas, border_color_, left, top, left, bottom);
00759 }
00760
00761
00762
00763 if(fill_color_) {
00764
00765 const unsigned left = x + border_thickness_;
00766 const unsigned right = left + w - (2 * border_thickness_) - 1;
00767 const unsigned top = y + border_thickness_;
00768 const unsigned bottom = top + h - (2 * border_thickness_);
00769
00770 for(unsigned i = top; i < bottom; ++i) {
00771
00772 draw_line(canvas, fill_color_, left, i, right, i);
00773 }
00774 }
00775 }
00776
00777
00778
00779
00780 class tcircle
00781 : public tcanvas::tshape
00782 {
00783 public:
00784
00785
00786
00787
00788
00789
00790
00791
00792 explicit tcircle(const config& cfg);
00793
00794
00795 void draw(surface& canvas
00796 , const game_logic::map_formula_callable& variables);
00797
00798 private:
00799 tformula<unsigned>
00800 x_,
00801 y_,
00802 radius_;
00803
00804
00805 Uint32 color_;
00806
00807 };
00808
00809 tcircle::tcircle(const config& cfg)
00810 : x_(cfg["x"])
00811 , y_(cfg["y"])
00812 , radius_(cfg["radius"])
00813 , color_(decode_color(cfg["color"]))
00814 {
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843 const std::string& debug = (cfg["debug"]);
00844 if(!debug.empty()) {
00845 DBG_GUI_P << "Circle: found debug message '" << debug << "'.\n";
00846 }
00847 }
00848
00849 void tcircle::draw(surface& canvas
00850 , const game_logic::map_formula_callable& variables)
00851 {
00852
00853
00854
00855
00856
00857
00858 const unsigned x = x_(variables);
00859 const unsigned y = y_(variables);
00860 const unsigned radius = radius_(variables);
00861
00862 DBG_GUI_D << "Circle: drawn at "
00863 << x << ',' << y << " radius " << radius
00864 << " canvas size " << canvas->w << ',' << canvas->h << ".\n";
00865
00866 VALIDATE_WITH_DEV_MESSAGE(
00867 static_cast<int>(x - radius) >= 0
00868 , _("Circle doesn't fit on canvas.")
00869 , (formatter() << "x = " << x << ", radius = " << radius).str());
00870
00871 VALIDATE_WITH_DEV_MESSAGE(
00872 static_cast<int>(y - radius) >= 0
00873 , _("Circle doesn't fit on canvas.")
00874 , (formatter() << "y = " << y << ", radius = " << radius).str());
00875
00876 VALIDATE_WITH_DEV_MESSAGE(
00877 static_cast<int>(x + radius) < canvas->w
00878 , _("Circle doesn't fit on canvas.")
00879 , (formatter() << "x = " << x << ", radius = " << radius
00880 << "', canvas width = " << canvas->w << ".").str());
00881
00882 VALIDATE_WITH_DEV_MESSAGE(
00883 static_cast<int>(y + radius) < canvas->h
00884 , _("Circle doesn't fit on canvas.")
00885 , (formatter() << "y = " << y << ", radius = " << radius
00886 << "', canvas height = " << canvas->h << ".").str());
00887
00888
00889 surface_lock locker(canvas);
00890 draw_circle(canvas, color_, x, y, radius);
00891 }
00892
00893
00894
00895
00896 class timage
00897 : public tcanvas::tshape
00898 {
00899 public:
00900
00901
00902
00903
00904
00905
00906
00907
00908 explicit timage(const config& cfg);
00909
00910
00911 void draw(surface& canvas
00912 , const game_logic::map_formula_callable& variables);
00913
00914 private:
00915 tformula<unsigned>
00916 x_,
00917 y_,
00918 w_,
00919 h_;
00920
00921
00922 SDL_Rect src_clip_;
00923
00924
00925 surface image_;
00926
00927
00928
00929
00930
00931
00932
00933
00934
00935
00936 tformula<std::string> image_name_;
00937
00938
00939
00940
00941
00942
00943
00944 enum tresize_mode {
00945 scale
00946 , stretch
00947 , tile
00948 };
00949
00950
00951 tresize_mode get_resize_mode(const std::string& resize_mode);
00952
00953
00954 tresize_mode resize_mode_;
00955
00956
00957 tformula<bool> vertical_mirror_;
00958 };
00959
00960 timage::timage(const config& cfg)
00961 : x_(cfg["x"])
00962 , y_(cfg["y"])
00963 , w_(cfg["w"])
00964 , h_(cfg["h"])
00965 , src_clip_()
00966 , image_()
00967 , image_name_(cfg["name"])
00968 , resize_mode_(get_resize_mode(cfg["resize_mode"]))
00969 , vertical_mirror_(cfg["vertical_mirror"])
00970 {
00971
00972
00973
00974
00975
00976
00977
00978
00979
00980
00981
00982
00983
00984
00985
00986
00987
00988
00989
00990
00991
00992
00993
00994
00995
00996
00997
00998
00999
01000
01001
01002
01003
01004
01005
01006
01007
01008
01009
01010
01011
01012
01013
01014
01015
01016
01017
01018
01019
01020
01021
01022
01023
01024
01025 const std::string& debug = (cfg["debug"]);
01026 if(!debug.empty()) {
01027 DBG_GUI_P << "Image: found debug message '" << debug << "'.\n";
01028 }
01029 }
01030
01031 void timage::draw(surface& canvas
01032 , const game_logic::map_formula_callable& variables)
01033 {
01034 DBG_GUI_D << "Image: draw.\n";
01035
01036
01037
01038
01039
01040
01041 const std::string& name = image_name_(variables);
01042
01043 if(name.empty()) {
01044 DBG_GUI_D << "Image: formula returned no value, will not be drawn.\n";
01045 return;
01046 }
01047
01048
01049
01050
01051
01052 surface tmp(image::get_image(image::locator(name)));
01053
01054 if(!tmp) {
01055 ERR_GUI_D << "Image: '" << name << "' not found and won't be drawn.\n";
01056 return;
01057 }
01058
01059 image_.assign(make_neutral_surface(tmp));
01060 assert(image_);
01061 src_clip_ = ::create_rect(0, 0, image_->w, image_->h);
01062
01063 game_logic::map_formula_callable local_variables(variables);
01064 local_variables.add("image_original_width", variant(image_->w));
01065 local_variables.add("image_original_height", variant(image_->h));
01066
01067 unsigned w = w_(local_variables);
01068 VALIDATE_WITH_DEV_MESSAGE(
01069 static_cast<int>(w) >= 0
01070 , _("Image doesn't fit on canvas.")
01071 , (formatter() << "Image '" << name
01072 << "', w = " << static_cast<int>(w) << ".").str());
01073
01074 unsigned h = h_(local_variables);
01075 VALIDATE_WITH_DEV_MESSAGE(
01076 static_cast<int>(h) >= 0
01077 , _("Image doesn't fit on canvas.")
01078 , (formatter() << "Image '" << name
01079 << "', h = " << static_cast<int>(h) << ".").str());
01080
01081 local_variables.add("image_width", variant(w ? w : image_->w));
01082 local_variables.add("image_height", variant(h ? h : image_->h));
01083
01084 const unsigned x = x_(local_variables);
01085 VALIDATE_WITH_DEV_MESSAGE(
01086 static_cast<int>(x) >= 0
01087 , _("Image doesn't fit on canvas.")
01088 , (formatter() << "Image '" << name
01089 << "', x = " << static_cast<int>(x) << ".").str());
01090
01091 const unsigned y = y_(local_variables);
01092 VALIDATE_WITH_DEV_MESSAGE(
01093 static_cast<int>(y) >= 0
01094 , _("Image doesn't fit on canvas.")
01095 , (formatter() << "Image '" << name
01096 << "', y = " << static_cast<int>(y) << ".").str());
01097
01098
01099 SDL_Rect src_clip = src_clip_;
01100 SDL_Rect dst_clip = ::create_rect(x, y, 0, 0);
01101 surface surf;
01102
01103
01104 if(w || h) {
01105 bool done = false;
01106 bool stretch_image = (resize_mode_ == stretch) && (!!w ^ !!h);
01107 if(!w) {
01108 if(stretch_image) {
01109 DBG_GUI_D << "Image: vertical stretch from " << image_->w
01110 << ',' << image_->h << " to a height of " << h << ".\n";
01111
01112 surf = stretch_surface_vertical(image_, h, false);
01113 done = true;
01114 }
01115 w = image_->w;
01116 }
01117
01118 if(!h) {
01119 if(stretch_image) {
01120 DBG_GUI_D << "Image: horizontal stretch from " << image_->w
01121 << ',' << image_->h << " to a width of " << w << ".\n";
01122
01123 surf = stretch_surface_horizontal(image_, w, false);
01124 done = true;
01125 }
01126 h = image_->h;
01127 }
01128
01129 if(!done) {
01130
01131 if(resize_mode_ == tile) {
01132 DBG_GUI_D << "Image: tiling from " << image_->w
01133 << ',' << image_->h << " to " << w << ',' << h << ".\n";
01134
01135 const int columns = (w + image_->w - 1) / image_->w;
01136 const int rows = (h + image_->h - 1) / image_->h;
01137 surf = create_neutral_surface(w, h);
01138
01139 for(int x = 0; x < columns; ++x) {
01140 for(int y = 0; y < rows; ++y) {
01141 const SDL_Rect dest = ::create_rect(
01142 x * image_->w
01143 , y * image_->h
01144 , 0
01145 , 0);
01146 blit_surface(image_, NULL, surf, &dest);
01147 }
01148 }
01149
01150 } else {
01151 if(resize_mode_ == stretch) {
01152 ERR_GUI_D << "Image: failed to stretch image, "
01153 "fall back to scaling.\n";
01154 }
01155
01156 DBG_GUI_D << "Image: scaling from " << image_->w
01157 << ',' << image_->h << " to " << w << ',' << h << ".\n";
01158
01159 surf = scale_surface(image_, w, h, false);
01160 }
01161 }
01162 src_clip.w = w;
01163 src_clip.h = h;
01164 } else {
01165 surf = image_;
01166 }
01167
01168 if(vertical_mirror_(local_variables)) {
01169 surf = flip_surface(surf, false);
01170 }
01171
01172 blit_surface(surf, &src_clip, canvas, &dst_clip);
01173 }
01174
01175 timage::tresize_mode timage::get_resize_mode(const std::string& resize_mode)
01176 {
01177 if(resize_mode == "tile") {
01178 return timage::tile;
01179 } else if(resize_mode == "stretch") {
01180 return timage::stretch;
01181 } else {
01182 if(!resize_mode.empty() && resize_mode != "scale") {
01183 ERR_GUI_E << "Invalid resize mode '"
01184 << resize_mode << "' falling back to 'scale'.\n";
01185 }
01186 return timage::scale;
01187 }
01188 }
01189
01190
01191
01192
01193 class ttext
01194 : public tcanvas::tshape
01195 {
01196 public:
01197
01198
01199
01200
01201
01202
01203
01204
01205 explicit ttext(const config& cfg);
01206
01207
01208 void draw(surface& canvas
01209 , const game_logic::map_formula_callable& variables);
01210
01211 private:
01212 tformula<unsigned>
01213 x_,
01214 y_,
01215 w_,
01216 h_;
01217
01218
01219 unsigned font_size_;
01220
01221
01222 unsigned font_style_;
01223
01224
01225 tformula<PangoAlignment> text_alignment_;
01226
01227
01228 Uint32 color_;
01229
01230
01231 tformula<t_string> text_;
01232
01233
01234 tformula<bool> text_markup_;
01235
01236
01237 tformula<int> maximum_width_;
01238
01239
01240 unsigned characters_per_line_;
01241
01242
01243 tformula<int> maximum_height_;
01244 };
01245
01246 ttext::ttext(const config& cfg)
01247 : x_(cfg["x"])
01248 , y_(cfg["y"])
01249 , w_(cfg["w"])
01250 , h_(cfg["h"])
01251 , font_size_(cfg["font_size"])
01252 , font_style_(decode_font_style(cfg["font_style"]))
01253 , text_alignment_(cfg["text_alignment"])
01254 , color_(decode_color(cfg["color"]))
01255 , text_(cfg["text"])
01256 , text_markup_(cfg["text_markup"], false)
01257 , maximum_width_(cfg["maximum_width"], -1)
01258 , characters_per_line_(cfg["text_characters_per_line"])
01259 , maximum_height_(cfg["maximum_height"], -1)
01260 {
01261
01262
01263
01264
01265
01266
01267
01268
01269
01270
01271
01272
01273
01274
01275
01276
01277
01278
01279
01280
01281
01282
01283
01284
01285
01286
01287
01288
01289
01290
01291
01292
01293
01294
01295
01296
01297
01298
01299
01300
01301
01302
01303
01304
01305
01306 VALIDATE(font_size_, _("Text has a font size of 0."));
01307
01308 const std::string& debug = (cfg["debug"]);
01309 if(!debug.empty()) {
01310 DBG_GUI_P << "Text: found debug message '" << debug << "'.\n";
01311 }
01312 }
01313
01314 void ttext::draw(surface& canvas
01315 , const game_logic::map_formula_callable& variables)
01316 {
01317 assert(variables.has_key("text"));
01318
01319
01320
01321
01322 const t_string text = text_(variables);
01323
01324 if(text.empty()) {
01325 DBG_GUI_D << "Text: no text to render, leave.\n";
01326 return;
01327 }
01328
01329 static font::ttext text_renderer;
01330 text_renderer.set_text(text, text_markup_(variables));
01331
01332 text_renderer.set_font_size(font_size_)
01333 .set_font_style(font_style_)
01334 .set_alignment(text_alignment_(variables))
01335 .set_foreground_color(color_)
01336 .set_maximum_width(maximum_width_(variables))
01337 .set_maximum_height(maximum_height_(variables))
01338 .set_ellipse_mode(variables.has_key("text_wrap_mode")
01339 ? static_cast<PangoEllipsizeMode>
01340 (variables.query_value("text_wrap_mode").as_int())
01341 : PANGO_ELLIPSIZE_END)
01342 .set_characters_per_line(characters_per_line_);
01343
01344 surface surf = text_renderer.render();
01345 if(surf->w == 0) {
01346 DBG_GUI_D << "Text: Rendering '"
01347 << text << "' resulted in an empty canvas, leave.\n";
01348 return;
01349 }
01350
01351 game_logic::map_formula_callable local_variables(variables);
01352 local_variables.add("text_width", variant(surf->w));
01353 local_variables.add("text_height", variant(surf->h));
01354
01355
01356
01357
01358
01359
01360
01361
01362
01363
01364
01365 const unsigned x = x_(local_variables);
01366 const unsigned y = y_(local_variables);
01367 const unsigned w = w_(local_variables);
01368 const unsigned h = h_(local_variables);
01369
01370 DBG_GUI_D << "Text: drawing text '" << text
01371 << "' drawn from " << x << ',' << y
01372 << " width " << w << " height " << h
01373 << " canvas size " << canvas->w << ',' << canvas->h << ".\n";
01374
01375 VALIDATE(static_cast<int>(x) < canvas->w && static_cast<int>(y) < canvas->h
01376 , _("Text doesn't start on canvas."));
01377
01378
01379 if(surf->w > static_cast<int>(w)) {
01380 WRN_GUI_D << "Text: text is too wide for the "
01381 "canvas and will be clipped.\n";
01382 }
01383
01384 if(surf->h > static_cast<int>(h)) {
01385 WRN_GUI_D << "Text: text is too high for the "
01386 "canvas and will be clipped.\n";
01387 }
01388
01389 SDL_Rect dst = ::create_rect(x, y, canvas->w, canvas->h);
01390 blit_surface(surf, 0, canvas, &dst);
01391 }
01392
01393 }
01394
01395
01396
01397 tcanvas::tcanvas()
01398 : shapes_()
01399 , blur_depth_(0)
01400 , w_(0)
01401 , h_(0)
01402 , canvas_()
01403 , variables_()
01404 , dirty_(true)
01405 {
01406 }
01407
01408 void tcanvas::draw(const bool force)
01409 {
01410 log_scope2(log_gui_draw, "Canvas: drawing.");
01411 if(!dirty_ && !force) {
01412 DBG_GUI_D << "Canvas: nothing to draw.\n";
01413 return;
01414 }
01415
01416 if(dirty_) {
01417 get_screen_size_variables(variables_);
01418 variables_.add("width",variant(w_));
01419 variables_.add("height",variant(h_));
01420 }
01421
01422
01423 DBG_GUI_D << "Canvas: create new empty canvas.\n";
01424 canvas_.assign(create_neutral_surface(w_, h_));
01425
01426
01427 for(std::vector<tshape_ptr>::iterator itor =
01428 shapes_.begin(); itor != shapes_.end(); ++itor) {
01429 log_scope2(log_gui_draw, "Canvas: draw shape.");
01430
01431 (*itor)->draw(canvas_, variables_);
01432 }
01433
01434 dirty_ = false;
01435 }
01436
01437 void tcanvas::blit(surface& surf, SDL_Rect rect)
01438 {
01439 draw();
01440
01441 if(blur_depth_) {
01442 if(is_neutral(surf)) {
01443 blur_surface(surf, rect, blur_depth_);
01444 } else {
01445
01446 SDL_Rect r = rect;
01447
01448
01449 surface s = get_surface_portion(surf, r, true);
01450 s = blur_surface(s, blur_depth_);
01451 sdl_blit(s, NULL, surf, &rect);
01452 }
01453 }
01454
01455 sdl_blit(canvas_, NULL, surf, &rect);
01456 }
01457
01458 void tcanvas::parse_cfg(const config& cfg)
01459 {
01460 log_scope2(log_gui_parse, "Canvas: parsing config.");
01461 shapes_.clear();
01462
01463 foreach(const config::any_child& shape, cfg.all_children_range()) {
01464 const std::string &type = shape.key;
01465 const config &data = shape.cfg;
01466
01467 DBG_GUI_P << "Canvas: found shape of the type " << type << ".\n";
01468
01469 if(type == "line") {
01470 shapes_.push_back(new tline(data));
01471 } else if(type == "rectangle") {
01472 shapes_.push_back(new trectangle(data));
01473 } else if(type == "circle") {
01474 shapes_.push_back(new tcircle(data));
01475 } else if(type == "image") {
01476 shapes_.push_back(new timage(data));
01477 } else if(type == "text") {
01478 shapes_.push_back(new ttext(data));
01479 } else if(type == "pre_commit") {
01480
01481
01482 foreach(const config::any_child& function,
01483 data.all_children_range()) {
01484
01485 if(function.key == "blur") {
01486 blur_depth_ = function.cfg["depth"];
01487 } else {
01488 ERR_GUI_P << "Canvas: found a pre commit function"
01489 << " of an invalid type " << type << ".\n";
01490 }
01491 }
01492
01493 } else {
01494 ERR_GUI_P << "Canvas: found a shape of an invalid type "
01495 << type << ".\n";
01496
01497 assert(false);
01498 }
01499 }
01500 }
01501
01502
01503
01504 }
01505
01506
01507
01508
01509
01510
01511
01512
01513
01514
01515
01516
01517
01518
01519
01520