Freeciv-3.3: /fast/freeciv/nightly/main/client/gui-sdl2/widget_icon.c Source File

Freeciv-3.3

Loading...
Searching...
No Matches
widget_icon.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 2006 - The Freeciv Project
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18/* SDL2 */
19#ifdef SDL2_PLAIN_INCLUDE
20#include <SDL.h>
21#else /* SDL2_PLAIN_INCLUDE */
22#include <SDL2/SDL.h>
23#endif /* SDL2_PLAIN_INCLUDE */
24
25/* utility */
26#include "log.h"
27
28/* gui-sdl2 */
29#include "colors.h"
30#include "graphics.h"
31#include "themespec.h"
32
33#include "widget.h"
34#include "widget_p.h"
35
36 static int (*baseclass_redraw)(struct widget *pwidget);
37
38/* =================================================== */
39/* ===================== ICON ======================== */
40/* =================================================== */
41
42/**********************************************************************/
45 static int redraw_icon(struct widget *icon)
46{
47 int ret;
48 SDL_Rect src, area = icon->size;
49
50 ret = (*baseclass_redraw)(icon);
51 if (ret != 0) {
52 return ret;
53 }
54
55 if (!icon->theme) {
56 return -3;
57 }
58
59 src.x = (icon->theme->w / 4) * (Uint8) (get_wstate(icon));
60 src.y = 0;
61 src.w = (icon->theme->w / 4);
62 src.h = icon->theme->h;
63
64 if (icon->size.w != src.w) {
65 area.x += (icon->size.w - src.w) / 2;
66 }
67
68 if (icon->size.h != src.h) {
69 area.y += (icon->size.h - src.h) / 2;
70 }
71
72 return alphablit(icon->theme, &src, icon->dst->surface, &area, 255);
73}
74
75/**********************************************************************/
78 static int redraw_icon2(struct widget *icon)
79{
80 int ret;
81 SDL_Rect dest;
82
83 ret = (*baseclass_redraw)(icon);
84 if (ret != 0) {
85 return ret;
86 }
87
88 if (!icon) {
89 return -3;
90 }
91
92 if (!icon->theme) {
93 return -4;
94 }
95
96 dest.x = icon->size.x;
97 dest.y = icon->size.y;
98 dest.w = icon->theme->w;
99 dest.h = icon->theme->h;
100
101 switch (get_wstate(icon)) {
102 case FC_WS_SELECTED:
103 create_frame(icon->dst->surface,
104 dest.x + 1, dest.y + 1,
105 dest.w + adj_size(2), dest.h + adj_size(2),
106 get_theme_color(COLOR_THEME_CUSTOM_WIDGET_SELECTED_FRAME));
107 break;
108
109 case FC_WS_PRESSED:
110 create_frame(icon->dst->surface,
111 dest.x + 1, dest.y + 1,
112 dest.w + adj_size(2), dest.h + adj_size(2),
113 get_theme_color(COLOR_THEME_CUSTOM_WIDGET_SELECTED_FRAME));
114
115 create_frame(icon->dst->surface,
116 dest.x, dest.y,
117 dest.w + adj_size(3), dest.h + adj_size(3),
118 get_theme_color(COLOR_THEME_CUSTOM_WIDGET_PRESSED_FRAME));
119 break;
120
121 case FC_WS_DISABLED:
122 create_frame(icon->dst->surface,
123 dest.x + 1, dest.y + 1,
124 dest.w + adj_size(2), dest.h + adj_size(2),
125 get_theme_color(COLOR_THEME_WIDGET_DISABLED_TEXT));
126 break;
127 case FC_WS_NORMAL:
128 /* No frame by default */
129 break;
130 }
131
132 dest.x += adj_size(2);
133 dest.y += adj_size(2);
134 ret = alphablit(icon->theme, NULL, icon->dst->surface, &dest, 255);
135 if (ret) {
136 return ret;
137 }
138
139 return 0;
140}
141
142/**********************************************************************/
145 void set_new_icon_theme(struct widget *icon_widget, SDL_Surface *new_theme)
146{
147 if ((new_theme) && (icon_widget)) {
148 FREESURFACE(icon_widget->theme);
149 icon_widget->theme = new_theme;
150 icon_widget->size.w = new_theme->w / 4;
151 icon_widget->size.h = new_theme->h;
152 }
153}
154
155/**********************************************************************/
158 SDL_Surface *create_icon_theme_surf(SDL_Surface *icon)
159{
160 SDL_Color bg_color = { 255, 255, 255, 128 };
161 SDL_Rect dest;
162 SDL_Rect src;
163 SDL_Surface *ptheme;
164
165 get_smaller_surface_rect(icon, &src);
166 ptheme = create_surf((src.w + adj_size(4)) * 4, src.h + adj_size(4),
167 SDL_SWSURFACE);
168
169 dest.x = adj_size(2);
170 dest.y = (ptheme->h - src.h) / 2;
171
172 /* normal */
173 alphablit(icon, &src, ptheme, &dest, 255);
174
175 /* selected */
176 dest.x += (src.w + adj_size(4));
177 alphablit(icon, &src, ptheme, &dest, 255);
178
179 /* draw selected frame */
180 create_frame(ptheme,
181 dest.x - 1, dest.y - 1, src.w + 1, src.h + 1,
182 get_theme_color(COLOR_THEME_CUSTOM_WIDGET_SELECTED_FRAME));
183
184 /* pressed */
185 dest.x += (src.w + adj_size(4));
186 alphablit(icon, &src, ptheme, &dest, 255);
187
188 /* draw selected frame */
189 create_frame(ptheme,
190 dest.x - 1, dest.y - 1, src.w + 1, src.h + 1,
191 get_theme_color(COLOR_THEME_CUSTOM_WIDGET_SELECTED_FRAME));
192 /* draw press frame */
193 create_frame(ptheme,
194 dest.x - adj_size(2), dest.y - adj_size(2),
195 src.w + adj_size(3), src.h + adj_size(3),
196 get_theme_color(COLOR_THEME_CUSTOM_WIDGET_PRESSED_FRAME));
197
198 /* disabled */
199 dest.x += (src.w + adj_size(4));
200 alphablit(icon, &src, ptheme, &dest, 255);
201 dest.w = src.w;
202 dest.h = src.h;
203
204 fill_rect_alpha(ptheme, &dest, &bg_color);
205
206 return ptheme;
207}
208
209/**********************************************************************/
212 struct widget *create_themeicon(SDL_Surface *icon_theme,
213 struct gui_layer *pdest,
214 Uint32 flags)
215{
216 struct widget *icon_widget = widget_new();
217
218 icon_widget->theme = icon_theme;
219
220 set_wflag(icon_widget, (WF_FREE_STRING | WF_FREE_GFX | flags));
221 set_wstate(icon_widget, FC_WS_DISABLED);
222 set_wtype(icon_widget, WT_ICON);
223 icon_widget->mod = KMOD_NONE;
224 icon_widget->dst = pdest;
225
226 baseclass_redraw = icon_widget->redraw;
227 icon_widget->redraw = redraw_icon;
228
229 if (icon_theme) {
230 icon_widget->size.w = icon_theme->w / 4;
231 icon_widget->size.h = icon_theme->h;
232 }
233
234 return icon_widget;
235}
236
237/**********************************************************************/
240 int draw_icon(struct widget *icon, Sint16 start_x, Sint16 start_y)
241{
242 icon->size.x = start_x;
243 icon->size.y = start_y;
244
245 if (get_wflags(icon) & WF_RESTORE_BACKGROUND) {
246 refresh_widget_background(icon);
247 }
248
249 return draw_icon_from_theme(icon->theme, get_wstate(icon), icon->dst,
250 icon->size.x, icon->size.y);
251}
252
253/**********************************************************************/
268 int draw_icon_from_theme(SDL_Surface *icon_theme, Uint8 state,
269 struct gui_layer *pdest, Sint16 start_x, Sint16 start_y)
270{
271 SDL_Rect src, des = {.x = start_x, .y = start_y, .w = 0, .h = 0};
272
273 if (icon_theme == NULL) {
274 return -3;
275 }
276
277 src.x = 0 + (icon_theme->w / 4) * state;
278 src.y = 0;
279 src.w = icon_theme->w / 4;
280 src.h = icon_theme->h;
281
282 return alphablit(icon_theme, &src, pdest->surface, &des, 255);
283}
284
285/**********************************************************************/
298 SDL_Surface *create_icon_from_theme(SDL_Surface *icon_theme, Uint8 state)
299{
300 SDL_Rect src;
301
302 if (icon_theme == NULL) {
303 return NULL;
304 }
305
306 src.w = icon_theme->w / 4;
307 src.x = src.w * state;
308 src.y = 0;
309 src.h = icon_theme->h;
310
311 return crop_rect_from_surface(icon_theme, &src);
312}
313
314/* =================================================== */
315/* ===================== ICON2 ======================= */
316/* =================================================== */
317
318/**********************************************************************/
321 void set_new_icon2_theme(struct widget *icon_widget, SDL_Surface *new_theme,
322 bool free_old_theme)
323{
324 if ((new_theme) && (icon_widget)) {
325 if (free_old_theme) {
326 FREESURFACE(icon_widget->theme);
327 }
328 icon_widget->theme = new_theme;
329 icon_widget->size.w = new_theme->w + adj_size(4);
330 icon_widget->size.h = new_theme->h + adj_size(4);
331 }
332}
333
334/**********************************************************************/
337 struct widget *create_icon2(SDL_Surface *icon, struct gui_layer *pdest,
338 Uint32 flags)
339{
340 struct widget *icon_widget = widget_new();
341
342 icon_widget->theme = icon;
343
344 set_wflag(icon_widget, (WF_FREE_STRING | WF_FREE_GFX | flags));
345 set_wstate(icon_widget, FC_WS_DISABLED);
346 set_wtype(icon_widget, WT_ICON2);
347 icon_widget->mod = KMOD_NONE;
348 icon_widget->dst = pdest;
349
350 baseclass_redraw = icon_widget->redraw;
351 icon_widget->redraw = redraw_icon2;
352
353 if (icon) {
354 icon_widget->size.w = icon->w + adj_size(4);
355 icon_widget->size.h = icon->h + adj_size(4);
356 }
357
358 return icon_widget;
359}
incite_cost
char * incite_cost
Definition comments.c:74
get_theme_color
SDL_Color * get_theme_color(enum theme_color themecolor)
Definition colors.c:47
fill_rect_alpha
int fill_rect_alpha(SDL_Surface *surf, SDL_Rect *prect, SDL_Color *pcolor)
Definition graphics.c:865
crop_rect_from_surface
SDL_Surface * crop_rect_from_surface(SDL_Surface *psource, SDL_Rect *prect)
Definition graphics.c:236
alphablit
int alphablit(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect, unsigned char alpha_mod)
Definition graphics.c:199
create_surf
SDL_Surface * create_surf(int width, int height, Uint32 flags)
Definition graphics.c:351
get_smaller_surface_rect
void get_smaller_surface_rect(SDL_Surface *surf, SDL_Rect *rect)
Definition graphics.c:936
create_frame
void create_frame(SDL_Surface *dest, Sint16 left, Sint16 top, Sint16 width, Sint16 height, SDL_Color *pcolor)
Definition graphics.c:1350
FREESURFACE
#define FREESURFACE(ptr)
Definition graphics.h:322
adj_size
#define adj_size(size)
Definition gui_main.h:141
COLOR_THEME_CUSTOM_WIDGET_PRESSED_FRAME
@ COLOR_THEME_CUSTOM_WIDGET_PRESSED_FRAME
Definition themecolors.h:29
COLOR_THEME_WIDGET_DISABLED_TEXT
@ COLOR_THEME_WIDGET_DISABLED_TEXT
Definition themecolors.h:43
COLOR_THEME_CUSTOM_WIDGET_SELECTED_FRAME
@ COLOR_THEME_CUSTOM_WIDGET_SELECTED_FRAME
Definition themecolors.h:27
refresh_widget_background
void refresh_widget_background(struct widget *pwidget)
Definition widget.c:1151
FC_WS_DISABLED
@ FC_WS_DISABLED
Definition widget.h:99
FC_WS_NORMAL
@ FC_WS_NORMAL
Definition widget.h:96
FC_WS_PRESSED
@ FC_WS_PRESSED
Definition widget.h:98
FC_WS_SELECTED
@ FC_WS_SELECTED
Definition widget.h:97
get_wflags
enum widget_flag get_wflags(const struct widget *pwidget)
Definition widget_core.c:86
set_wstate
void set_wstate(struct widget *pwidget, enum widget_state state)
Definition widget_core.c:36
get_wstate
enum widget_state get_wstate(const struct widget *pwidget)
Definition widget_core.c:70
WF_FREE_GFX
@ WF_FREE_GFX
Definition widget.h:70
WF_FREE_STRING
@ WF_FREE_STRING
Definition widget.h:76
WF_RESTORE_BACKGROUND
@ WF_RESTORE_BACKGROUND
Definition widget.h:85
set_wtype
void set_wtype(struct widget *pwidget, enum widget_type type)
Definition widget_core.c:45
set_wflag
void set_wflag(struct widget *pwidget, enum widget_flag flag)
Definition widget_core.c:54
WT_ICON2
@ WT_ICON2
Definition widget.h:60
WT_ICON
@ WT_ICON
Definition widget.h:49
widget_new
struct widget * widget_new(void)
Definition widget_core.c:252
create_themeicon
struct widget * create_themeicon(SDL_Surface *icon_theme, struct gui_layer *pdest, Uint32 flags)
Definition widget_icon.c:212
create_icon2
struct widget * create_icon2(SDL_Surface *icon, struct gui_layer *pdest, Uint32 flags)
Definition widget_icon.c:337
set_new_icon2_theme
void set_new_icon2_theme(struct widget *icon_widget, SDL_Surface *new_theme, bool free_old_theme)
Definition widget_icon.c:321
create_icon_theme_surf
SDL_Surface * create_icon_theme_surf(SDL_Surface *icon)
Definition widget_icon.c:158
baseclass_redraw
static int(* baseclass_redraw)(struct widget *pwidget)
Definition widget_icon.c:36
create_icon_from_theme
SDL_Surface * create_icon_from_theme(SDL_Surface *icon_theme, Uint8 state)
Definition widget_icon.c:298
draw_icon_from_theme
int draw_icon_from_theme(SDL_Surface *icon_theme, Uint8 state, struct gui_layer *pdest, Sint16 start_x, Sint16 start_y)
Definition widget_icon.c:268
redraw_icon
static int redraw_icon(struct widget *icon)
Definition widget_icon.c:45
set_new_icon_theme
void set_new_icon_theme(struct widget *icon_widget, SDL_Surface *new_theme)
Definition widget_icon.c:145
redraw_icon2
static int redraw_icon2(struct widget *icon)
Definition widget_icon.c:78
draw_icon
int draw_icon(struct widget *icon, Sint16 start_x, Sint16 start_y)
Definition widget_icon.c:240
themespec.h
widget.h
widget_p.h
colors.h
graphics.h
log.h
gui_layer
Definition graphics.h:227
gui_layer::surface
SDL_Surface * surface
Definition graphics.h:229
widget
Definition widget.h:112
widget::theme
SDL_Surface * theme
Definition widget.h:118
widget::dst
struct gui_layer * dst
Definition widget.h:116
widget::area
SDL_Rect area
Definition widget.h:149
widget::size
SDL_Rect size
Definition widget.h:145