From d6ba76a3caa5d6b5bc921973f89e467b9fb18b6e Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sat, 20 Nov 2021 12:54:01 +0200 Subject: [PATCH 40/40] Add unit stack size display for gtk-clients See osdn #43249 Signed-off-by: Marko Lindqvist --- client/gui-gtk-3.22/canvas.c | 46 +++++++++++++++++++++++------------- client/gui-gtk-3.22/canvas.h | 4 ++++ client/gui-gtk-3.22/sprite.c | 25 ++++++++++++++++++-- client/gui-gtk-4.0/canvas.c | 46 +++++++++++++++++++++++------------- client/gui-gtk-4.0/canvas.h | 4 ++++ client/gui-gtk-4.0/sprite.c | 25 ++++++++++++++++++-- 6 files changed, 112 insertions(+), 38 deletions(-) diff --git a/client/gui-gtk-3.22/canvas.c b/client/gui-gtk-3.22/canvas.c index 724d2a5110..a6aee763f6 100644 --- a/client/gui-gtk-3.22/canvas.c +++ b/client/gui-gtk-3.22/canvas.c @@ -362,16 +362,11 @@ void get_text_size(int *width, int *height, Supplied canvas_x/y are prior to any canvas zoom. ****************************************************************************/ void canvas_put_text(struct canvas *pcanvas, int canvas_x, int canvas_y, - enum client_font font, - struct color *pcolor, - const char *text) + enum client_font font, struct color *pcolor, + const char *text) { cairo_t *cr; - if (!layout) { - layout = pango_layout_new(gdk_pango_context_get_for_screen(gdk_screen_get_default())); - } - if (!pcanvas->drawable) { cr = cairo_create(pcanvas->surface); } else { @@ -382,6 +377,29 @@ void canvas_put_text(struct canvas *pcanvas, int canvas_x, int canvas_y, cairo_save(cr); } + surface_put_text(cr, canvas_x, canvas_y, pcanvas->zoom, + font, pcolor, text); + + if (!pcanvas->drawable) { + cairo_destroy(cr); + } else { + cairo_restore(cr); + } +} + +/************************************************************************//** + Draw the text onto the surface in the given color and font. The + position does not account for the ascent of the text; this function must + take care of this manually. The text may not be NULL but may be empty. +****************************************************************************/ +void surface_put_text(cairo_t *cr, int x, int y, float zoom, + enum client_font font, struct color *pcolor, + const char *text) +{ + if (!layout) { + layout = pango_layout_new(gdk_pango_context_get_for_screen(gdk_screen_get_default())); + } + pango_layout_set_font_description(layout, FONT(font)); pango_layout_set_text(layout, text, -1); @@ -391,19 +409,13 @@ void canvas_put_text(struct canvas *pcanvas, int canvas_x, int canvas_y, if (!gdk_rgba_equal(&pcolor->color, &black)) { gdk_cairo_set_source_rgba(cr, &black); - cairo_move_to(cr, canvas_x * pcanvas->zoom + 1, - canvas_y * pcanvas->zoom + 1); - pango_cairo_show_layout (cr, layout); + cairo_move_to(cr, x * zoom + 1, + y * zoom + 1); + pango_cairo_show_layout(cr, layout); } } - cairo_move_to(cr, canvas_x * pcanvas->zoom, canvas_y * pcanvas->zoom); + cairo_move_to(cr, x * zoom, y * zoom); gdk_cairo_set_source_rgba(cr, &pcolor->color); pango_cairo_show_layout(cr, layout); - - if (!pcanvas->drawable) { - cairo_destroy(cr); - } else { - cairo_restore(cr); - } } diff --git a/client/gui-gtk-3.22/canvas.h b/client/gui-gtk-3.22/canvas.h index 1955d4309b..10b431c59b 100644 --- a/client/gui-gtk-3.22/canvas.h +++ b/client/gui-gtk-3.22/canvas.h @@ -27,4 +27,8 @@ struct canvas #define FC_STATIC_CANVAS_INIT { NULL, NULL, 1.0 } +void surface_put_text(cairo_t *cr, int x, int y, float zoom, + enum client_font font, struct color *pcolor, + const char *text); + #endif /* FC__CANVAS_H */ diff --git a/client/gui-gtk-3.22/sprite.c b/client/gui-gtk-3.22/sprite.c index 830fd36b46..83d0677f3a 100644 --- a/client/gui-gtk-3.22/sprite.c +++ b/client/gui-gtk-3.22/sprite.c @@ -516,6 +516,27 @@ GdkPixbuf *create_extra_pixbuf(const struct extra_type *pextra) ****************************************************************************/ struct sprite *load_gfxnumber(int num) { - /* Not supported in gtk3.22-client */ - return NULL; + int width, height; + char buf[10]; + struct sprite *spr; + struct color *sprcolor = color_alloc(0xff, 0xff, 0x00); + struct color *textcolor = color_alloc(0x00, 0x00, 0x00); + cairo_t *cr; + int border = 2; + + fc_snprintf(buf, sizeof(buf), "%d", num); + get_text_size(&width, &height, FONT_CITY_PROD, buf); + + spr = create_sprite(width + border * 2, height + border * 2, sprcolor); + + cr = cairo_create(spr->surface); + + surface_put_text(cr, border, border, 1.0, FONT_CITY_PROD, textcolor, buf); + + cairo_destroy(cr); + + color_free(textcolor); + color_free(sprcolor); + + return spr; } diff --git a/client/gui-gtk-4.0/canvas.c b/client/gui-gtk-4.0/canvas.c index c3e30bff61..55ff70deaf 100644 --- a/client/gui-gtk-4.0/canvas.c +++ b/client/gui-gtk-4.0/canvas.c @@ -362,16 +362,11 @@ void get_text_size(int *width, int *height, Supplied canvas_x/y are prior to any canvas zoom. ****************************************************************************/ void canvas_put_text(struct canvas *pcanvas, int canvas_x, int canvas_y, - enum client_font font, - struct color *pcolor, - const char *text) + enum client_font font, struct color *pcolor, + const char *text) { cairo_t *cr; - if (!layout) { - layout = pango_layout_new(gtk_widget_get_pango_context(toplevel)); - } - if (!pcanvas->drawable) { cr = cairo_create(pcanvas->surface); } else { @@ -382,6 +377,29 @@ void canvas_put_text(struct canvas *pcanvas, int canvas_x, int canvas_y, cairo_save(cr); } + surface_put_text(cr, canvas_x, canvas_y, pcanvas->zoom, + font, pcolor, text); + + if (!pcanvas->drawable) { + cairo_destroy(cr); + } else { + cairo_restore(cr); + } +} + +/************************************************************************//** + Draw the text onto the surface in the given color and font. The + position does not account for the ascent of the text; this function must + take care of this manually. The text may not be NULL but may be empty. +****************************************************************************/ +void surface_put_text(cairo_t *cr, int x, int y, float zoom, + enum client_font font, struct color *pcolor, + const char *text) +{ + if (!layout) { + layout = pango_layout_new(gtk_widget_get_pango_context(toplevel)); + } + pango_layout_set_font_description(layout, FONT(font)); pango_layout_set_text(layout, text, -1); @@ -391,19 +409,13 @@ void canvas_put_text(struct canvas *pcanvas, int canvas_x, int canvas_y, if (!gdk_rgba_equal(&pcolor->color, &black)) { gdk_cairo_set_source_rgba(cr, &black); - cairo_move_to(cr, canvas_x * pcanvas->zoom + 1, - canvas_y * pcanvas->zoom + 1); - pango_cairo_show_layout (cr, layout); + cairo_move_to(cr, x * zoom + 1, + y * zoom + 1); + pango_cairo_show_layout(cr, layout); } } - cairo_move_to(cr, canvas_x * pcanvas->zoom, canvas_y * pcanvas->zoom); + cairo_move_to(cr, x * zoom, y * zoom); gdk_cairo_set_source_rgba(cr, &pcolor->color); pango_cairo_show_layout(cr, layout); - - if (!pcanvas->drawable) { - cairo_destroy(cr); - } else { - cairo_restore(cr); - } } diff --git a/client/gui-gtk-4.0/canvas.h b/client/gui-gtk-4.0/canvas.h index 1955d4309b..10b431c59b 100644 --- a/client/gui-gtk-4.0/canvas.h +++ b/client/gui-gtk-4.0/canvas.h @@ -27,4 +27,8 @@ struct canvas #define FC_STATIC_CANVAS_INIT { NULL, NULL, 1.0 } +void surface_put_text(cairo_t *cr, int x, int y, float zoom, + enum client_font font, struct color *pcolor, + const char *text); + #endif /* FC__CANVAS_H */ diff --git a/client/gui-gtk-4.0/sprite.c b/client/gui-gtk-4.0/sprite.c index 822bf527fa..8c0b8b5dfe 100644 --- a/client/gui-gtk-4.0/sprite.c +++ b/client/gui-gtk-4.0/sprite.c @@ -549,6 +549,27 @@ void image_set_from_surface(GtkImage *image, cairo_surface_t *surf) ****************************************************************************/ struct sprite *load_gfxnumber(int num) { - /* Not supported in gtk3x-client */ - return NULL; + int width, height; + char buf[10]; + struct sprite *spr; + struct color *sprcolor = color_alloc(0xff, 0xff, 0x00); + struct color *textcolor = color_alloc(0x00, 0x00, 0x00); + cairo_t *cr; + int border = 2; + + fc_snprintf(buf, sizeof(buf), "%d", num); + get_text_size(&width, &height, FONT_CITY_PROD, buf); + + spr = create_sprite(width + border * 2, height + border * 2, sprcolor); + + cr = cairo_create(spr->surface); + + surface_put_text(cr, border, border, 1.0, FONT_CITY_PROD, textcolor, buf); + + cairo_destroy(cr); + + color_free(textcolor); + color_free(sprcolor); + + return spr; } -- 2.33.0