20 #define GETTEXT_DOMAIN "wesnoth-lib"
104 *
reinterpret_cast<Uint32*
>(start + (y* w* 4) + x* 4) =
color;
123 static void draw_line(
surface& canvas,
130 color = SDL_MapRGBA(canvas->format,
131 ((color & 0xFF000000) >> 24),
132 ((color & 0x00FF0000) >> 16),
133 ((color & 0x0000FF00) >> 8),
134 ((color & 0x000000FF)));
136 ptrdiff_t start =
reinterpret_cast<ptrdiff_t
>(canvas->pixels);
137 unsigned w = canvas->w;
139 DBG_GUI_D <<
"Shape: draw line from " << x1 <<
',' << y1 <<
" to " << x2
140 <<
',' << y2 <<
" canvas width " << w <<
" canvas height "
141 << canvas->h <<
".\n";
143 assert(static_cast<int>(x1) < canvas->w);
144 assert(static_cast<int>(x2) < canvas->w);
145 assert(static_cast<int>(y1) < canvas->h);
146 assert(static_cast<int>(y2) < canvas->h);
154 for(
unsigned y = y1; y <= y2; ++
y) {
162 for(
unsigned x = x1; x <= x2; ++
x) {
171 const int dx = x2 - x1;
172 const int dy = abs(static_cast<int>(y2 - y1));
173 const int step_x = 1;
174 const int step_y = y1 < y2 ? 1 : -1;
175 int err = (dx > dy ? dx : -dy) / 2;
180 if(x1 == x2 && y1 == y2) {
208 static void draw_circle(
surface& canvas,
210 const unsigned x_center,
211 const unsigned y_center,
212 const unsigned radius)
214 color = SDL_MapRGBA(canvas->format,
215 ((color & 0xFF000000) >> 24),
216 ((color & 0x00FF0000) >> 16),
217 ((color & 0x0000FF00) >> 8),
218 ((color & 0x000000FF)));
220 ptrdiff_t start =
reinterpret_cast<ptrdiff_t
>(canvas->pixels);
221 unsigned w = canvas->w;
223 DBG_GUI_D <<
"Shape: draw circle at " << x_center <<
',' << y_center
224 <<
" with radius " << radius <<
" canvas width " << w
225 <<
" canvas height " << canvas->h <<
".\n";
227 assert(static_cast<int>(x_center + radius) < canvas->w);
228 assert(static_cast<int>(x_center - radius) >= 0);
229 assert(static_cast<int>(y_center + radius) < canvas->h);
230 assert(static_cast<int>(y_center - radius) >= 0);
235 int d = -
static_cast<int>(radius);
239 put_pixel(start, color, w, x_center + x, y_center + y);
240 put_pixel(start, color, w, x_center + x, y_center - y);
241 put_pixel(start, color, w, x_center - x, y_center + y);
242 put_pixel(start, color, w, x_center - x, y_center - y);
244 put_pixel(start, color, w, x_center + y, y_center + x);
245 put_pixel(start, color, w, x_center + y, y_center - x);
246 put_pixel(start, color, w, x_center - y, y_center + x);
247 put_pixel(start, color, w, x_center - y, y_center - x);
261 class tline :
public tcanvas::tshape
271 explicit tline(
const config& cfg);
609 tline::tline(
const config& cfg)
619 DBG_GUI_P <<
"Line: found debug message '" << debug <<
"'.\n";
632 const unsigned x1 =
x1_(variables);
633 const unsigned y1 =
y1_(variables);
634 const unsigned x2 =
x2_(variables);
635 const unsigned y2 =
y2_(variables);
637 DBG_GUI_D <<
"Line: draw from " << x1 <<
',' << y1 <<
" to " << x2 <<
','
638 << y2 <<
" canvas size " << canvas->w <<
',' << canvas->h
641 VALIDATE(static_cast<int>(x1) < canvas->w
642 && static_cast<int>(x2) < canvas->w
643 && static_cast<int>(y1) < canvas->h
644 && static_cast<int>(y2) < canvas->h,
645 _(
"Line doesn't fit on canvas."));
656 draw_line(canvas,
color_, x2, y2, x1, y1);
658 draw_line(canvas,
color_, x1, y1, x2, y2);
665 class trectangle :
public tcanvas::tshape
675 explicit trectangle(
const config& cfg);
682 tformula<unsigned>
x_,
742 trectangle::trectangle(
const config& cfg)
757 DBG_GUI_P <<
"Rectangle: found debug message '" << debug <<
"'.\n";
769 const unsigned x =
x_(variables);
770 const unsigned y =
y_(variables);
771 const unsigned w =
w_(variables);
772 const unsigned h =
h_(variables);
774 DBG_GUI_D <<
"Rectangle: draw from " << x <<
',' << y <<
" width " << w
775 <<
" height " << h <<
" canvas size " << canvas->w <<
','
776 << canvas->h <<
".\n";
778 VALIDATE(static_cast<int>(x) < canvas->w
779 && static_cast<int>(x + w) <= canvas->w
780 && static_cast<int>(y) < canvas->h
781 && static_cast<int>(y + h) <= canvas->h,
782 _(
"Rectangle doesn't fit on canvas."));
790 const unsigned left = x +
i;
791 const unsigned right = left + w - (i * 2) - 1;
792 const unsigned top = y +
i;
793 const unsigned bottom = top + h - (i * 2) - 1;
802 draw_line(canvas,
border_color_, left, bottom, right, bottom);
811 (x + border_thickness_),
812 (y + border_thickness_),
813 (w - (border_thickness_ * 2)),
814 (h - (border_thickness_ * 2)),
825 class tcircle :
public tcanvas::tshape
835 explicit tcircle(
const config& cfg);
842 tformula<unsigned>
x_,
877 tcircle::tcircle(
const config& cfg)
885 DBG_GUI_P <<
"Circle: found debug message '" << debug <<
"'.\n";
898 const unsigned x =
x_(variables);
899 const unsigned y =
y_(variables);
900 const unsigned radius =
radius_(variables);
902 DBG_GUI_D <<
"Circle: drawn at " << x <<
',' << y <<
" radius " << radius
903 <<
" canvas size " << canvas->w <<
',' << canvas->h <<
".\n";
906 static_cast<int>(x - radius) >= 0,
907 _(
"Circle doesn't fit on canvas."),
908 (
formatter() <<
"x = " << x <<
", radius = " << radius).str());
911 static_cast<int>(y - radius) >= 0,
912 _(
"Circle doesn't fit on canvas."),
913 (
formatter() <<
"y = " << y <<
", radius = " << radius).str());
916 static_cast<int>(x + radius) < canvas->w,
917 _(
"Circle doesn't fit on canvas."),
918 (
formatter() <<
"x = " << x <<
", radius = " << radius
919 <<
"', canvas width = " << canvas->w <<
".").str());
922 static_cast<int>(y + radius) < canvas->h,
923 _(
"Circle doesn't fit on canvas."),
924 (
formatter() <<
"y = " << y <<
", radius = " << radius
925 <<
"', canvas height = " << canvas->h <<
".").str());
929 draw_circle(canvas,
color_, x, y, radius);
935 class timage :
public tcanvas::tshape
945 explicit timage(
const config& cfg);
952 tformula<unsigned>
x_,
987 tresize_mode get_resize_mode(
const std::string& resize_mode);
1052 timage::timage(
const config& cfg)
1064 if(!debug.empty()) {
1065 DBG_GUI_P <<
"Image: found debug message '" << debug <<
"'.\n";
1082 DBG_GUI_D <<
"Image: formula returned no value, will not be drawn.\n";
1093 ERR_GUI_D <<
"Image: '" << name <<
"' not found and won't be drawn."
1103 local_variables.add(
"image_original_width",
variant(
image_->w));
1104 local_variables.add(
"image_original_height",
variant(
image_->h));
1106 unsigned w =
w_(local_variables);
1108 _(
"Image doesn't fit on canvas."),
1110 <<
"', w = " << static_cast<int>(w)
1113 unsigned h =
h_(local_variables);
1115 _(
"Image doesn't fit on canvas."),
1117 <<
"', h = " << static_cast<int>(h)
1120 local_variables.add(
"image_width",
variant(w ? w :
image_->w));
1121 local_variables.add(
"image_height",
variant(h ? h :
image_->h));
1123 const unsigned x =
x_(local_variables);
1125 _(
"Image doesn't fit on canvas."),
1127 <<
"', x = " << static_cast<int>(x)
1130 const unsigned y =
y_(local_variables);
1132 _(
"Image doesn't fit on canvas."),
1134 <<
"', y = " << static_cast<int>(y)
1145 bool stretch_image = (
resize_mode_ == stretch) && (!!w ^ !!h);
1149 <<
image_->h <<
" to a height of " << h <<
".\n";
1160 <<
',' <<
image_->h <<
" to a width of " << w
1173 <<
image_->h <<
" to " << w <<
',' << h <<
".\n";
1179 for(
int x = 0; x < columns; ++
x) {
1180 for(
int y = 0; y < rows; ++
y) {
1189 ERR_GUI_D <<
"Image: failed to stretch image, "
1190 "fall back to scaling.\n";
1194 <<
image_->h <<
" to " << w <<
',' << h <<
".\n";
1212 timage::tresize_mode timage::get_resize_mode(
const std::string& resize_mode)
1214 if(resize_mode ==
"tile") {
1215 return timage::tile;
1216 }
else if(resize_mode ==
"stretch") {
1217 return timage::stretch;
1219 if(!resize_mode.empty() && resize_mode !=
"scale") {
1220 ERR_GUI_E <<
"Invalid resize mode '" << resize_mode
1221 <<
"' falling back to 'scale'.\n";
1230 class ttext :
public tcanvas::tshape
1240 explicit ttext(
const config& cfg);
1247 tformula<unsigned>
x_,
1341 ttext::ttext(
const config& cfg)
1351 ,
text_(cfg[
"text"])
1362 if(!debug.empty()) {
1363 DBG_GUI_P <<
"Text: found debug message '" << debug <<
"'.\n";
1370 assert(variables.
has_key(
"text"));
1378 DBG_GUI_D <<
"Text: no text to render, leave.\n";
1397 variables.
has_key(
"text_wrap_mode")
1398 ?
static_cast<PangoEllipsizeMode
>(
1401 : PANGO_ELLIPSIZE_END)
1406 DBG_GUI_D <<
"Text: Rendering '" << text
1407 <<
"' resulted in an empty canvas, leave.\n";
1412 local_variables.add(
"text_width",
variant(surf->w));
1413 local_variables.add(
"text_height",
variant(surf->h));
1425 const unsigned x =
x_(local_variables);
1426 const unsigned y =
y_(local_variables);
1427 const unsigned w =
w_(local_variables);
1428 const unsigned h =
h_(local_variables);
1430 DBG_GUI_D <<
"Text: drawing text '" << text <<
"' drawn from " << x <<
','
1431 << y <<
" width " << w <<
" height " << h <<
" canvas size "
1432 << canvas->w <<
',' << canvas->h <<
".\n";
1434 VALIDATE(static_cast<int>(x) < canvas->w && static_cast<int>(y) < canvas->h,
1435 _(
"Text doesn't start on canvas."));
1438 if(surf->w > static_cast<int>(w)) {
1439 WRN_GUI_D <<
"Text: text is too wide for the "
1440 "canvas and will be clipped.\n";
1443 if(surf->h > static_cast<int>(h)) {
1444 WRN_GUI_D <<
"Text: text is too high for the "
1445 "canvas and will be clipped.\n";
1471 DBG_GUI_D <<
"Canvas: nothing to draw.\n";
1482 DBG_GUI_D <<
"Canvas: create new empty canvas.\n";
1531 DBG_GUI_P <<
"Canvas: found shape of the type " << type <<
".\n";
1533 if(type ==
"line") {
1534 shapes_.push_back(
new tline(data));
1535 }
else if(type ==
"rectangle") {
1536 shapes_.push_back(
new trectangle(data));
1537 }
else if(type ==
"circle") {
1538 shapes_.push_back(
new tcircle(data));
1539 }
else if(type ==
"image") {
1540 shapes_.push_back(
new timage(data));
1541 }
else if(type ==
"text") {
1542 shapes_.push_back(
new ttext(data));
1543 }
else if(type ==
"pre_commit") {
1549 if(
function.key ==
"blur") {
1552 ERR_GUI_P <<
"Canvas: found a pre commit function"
1553 <<
" of an invalid type " << type <<
".\n";
1558 ERR_GUI_P <<
"Canvas: found a shape of an invalid type " << type
Define the common log macros for the gui toolkit.
unsigned decode_font_style(const std::string &style)
Converts a font style string to a font style.
surface get_image(const image::locator &i_locator, TYPE type)
function to get the surface corresponding to an image.
unsigned blur_depth_
The depth of the blur to use in the pre committing.
surface flip_surface(const surface &surf, bool optimize)
tformula< unsigned > x_
The x coordinate of the rectangle.
surface create_neutral_surface(int w, int h)
ttext & set_alignment(const PangoAlignment alignment)
tformula< int > maximum_height_
The maximum height for the text.
tformula< PangoAlignment > text_alignment_
The alignment of the text.
family_class
Font classes for get_font_families().
ttext & set_link_aware(bool b)
GLenum GLenum GLenum GLenum GLenum scale
unsigned w_
Width of the canvas.
ttext & set_font_style(const unsigned font_style)
Add a special kind of assert to validate whether the input from WML doesn't contain any problems that...
GLuint GLuint GLsizei GLenum type
void put_pixel(const surface &surf, surface_lock &surf_lock, int x, int y, Uint32 pixel)
Helper methods for setting/getting a single pixel in an image.
surface stretch_surface_vertical(const surface &surf, const unsigned h, const bool optimize)
Stretches a surface in the vertical direction.
surface image_
The image is cached in this surface.
tformula< unsigned > x1_
The start x coordinate of the line.
tformula< std::string > image_name_
Name of the image.
ttext & set_font_size(const unsigned font_size)
tformula< unsigned > h_
The height of the rectangle.
GLint GLint GLint GLint GLint GLint y
surface scale_surface(const surface &surf, int w, int h)
ttext & set_maximum_width(int width)
surface get_surface_portion(const surface &src, SDL_Rect &area)
Get a portion of the screen.
ttext & set_link_color(const std::string &color)
#define VALIDATE_WITH_DEV_MESSAGE(cond, message, dev_message)
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
tformula< t_string > text_
The text to draw.
Definitions for the interface to Wesnoth Markup Language (WML).
unsigned thickness_
The thickness of the line.
A class inherited from ttext_box that displays its input as stars.
void blit_surface(const surface &surf, const SDL_Rect *srcrect, surface &dst, const SDL_Rect *dstrect)
Replacement for sdl_blit.
unsigned h_
Height of the canvas.
static UNUSEDNOWARN std::string _(const char *str)
tformula< unsigned > w_
The width of the rectangle.
lg::log_domain log_gui_parse("gui/parse")
surface canvas_
The surface we draw all items on.
#define VALIDATE(cond, message)
The macro to use for the validation of WML.
void draw_solid_tinted_rectangle(int x, int y, int w, int h, int r, int g, int b, double alpha, surface target)
Fills a specified rectangle area of a surface with a given color and opacity.
unsigned font_style_
The style of the text.
ttext & set_maximum_height(int height, bool multiline)
Uint32 border_color_
The border color of the rectangle.
GLubyte GLubyte GLubyte GLubyte w
all_children_itors all_children_range() const
In-order iteration over all children.
unsigned font_size_
The font size of the text.
This file contains the canvas object which is the part where the widgets draw (temporally) images on...
bool is_dirty_
The dirty state of the canvas.
void draw(const bool force=false)
Draws the canvas.
void draw(surface screen)
#define log_scope2(domain, description)
ttext & set_characters_per_line(const unsigned characters_per_line)
Helper class for pinning SDL surfaces into memory.
void parse_cfg(const config &cfg)
Parses a config object.
tformula< unsigned > y2_
The end y coordinate of the line.
surface blur_surface(const surface &surf, int depth, bool optimize)
Cross-fades a surface.
unsigned border_thickness_
Border thickness.
void blit(surface &surf, SDL_Rect rect)
Blits the canvas unto another surface.
Uint32 fill_color_
The border color of the rectangle.
std::map< std::string, tfilter >::iterator itor
surface render() const
Returns the rendered text.
font::family_class font_family_
The text font family.
std::vector< tshape_ptr > shapes_
Vector with the shapes to draw.
SDL_Rect src_clip_
Contains the size of the image.
Uint32 color_
The color of the line.
GLfloat GLfloat GLfloat GLfloat h
tformula< bool > link_aware_
The link aware switch of the text.
GLint GLint GLint GLint GLint x
tformula< unsigned > y_
The y coordinate of the rectangle.
GLdouble GLdouble GLdouble r
lg::log_domain log_gui_draw("gui/draw")
bool is_neutral(const surface &surf)
Check that the surface is neutral bpp 32.
boost::uint32_t decode_color(const std::string &color)
Converts a color string to a color.
GLuint const GLchar * name
surface & get_video_surface()
unsigned characters_per_line_
The number of characters per line.
SDL_Rect create_rect(const int x, const int y, const int w, const int h)
Creates an empty SDL_Rect.
tformula< int > maximum_width_
The maximum width for the text.
tformula< unsigned > radius_
The radius of the circle.
tformula< unsigned > y1_
The start y coordinate of the line.
tformula< bool > text_markup_
The text markup switch of the text.
Contains the SDL_Rect helper code.
tresize_mode resize_mode_
The resize mode for an image.
tformula< std::string > link_color_
The link color of the text.
void swap(game_board &one, game_board &other)
surface make_neutral_surface(const surface &surf)
ttext & set_family_class(font::family_class fclass)
tformula< unsigned > x2_
The end x coordinate of the line.
void assign(const surface &o)
tformula< bool > vertical_mirror_
Mirror the image over the vertical axis.
bool set_text(const std::string &text, const bool markedup)
Sets the text to render.
family_class str_to_family_class(const std::string &str)
void get_screen_size_variables(game_logic::map_formula_callable &variable)
Gets a formula object with the screen size.
void sdl_blit(const surface &src, SDL_Rect *src_rect, surface &dst, SDL_Rect *dst_rect)
A config object defines a single node in a WML file, with access to child nodes.
surface stretch_surface_horizontal(const surface &surf, const unsigned w, const bool optimize)
Stretches a surface in the horizontal direction.
GLsizei const GLcharARB ** string
ttext & set_ellipse_mode(const PangoEllipsizeMode ellipse_mode)
game_logic::map_formula_callable variables_
The variables of the canvas.
ttext & set_foreground_color(const Uint32 color)