Freeciv-3.1
Loading...
Searching...
No Matches
happiness.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
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#include <gtk/gtk.h>
19
20/* utility */
21#include "fcintl.h"
22#include "log.h"
23#include "mem.h"
24#include "support.h"
25
26/* common */
27#include "city.h"
28#include "game.h"
29#include "government.h"
30
31/* client */
32#include "text.h"
33#include "tilespec.h"
34
35/* client/gui-gtk-4.0 */
36#include "graphics.h"
37#include "gui_main.h"
38#include "gui_stuff.h"
39#include "happiness.h"
40#include "mapview.h"
41
42/* semi-arbitrary number that controls the width of the happiness widget */
43#define HAPPINESS_PIX_WIDTH 30
44
45#define FEELING_WIDTH (HAPPINESS_PIX_WIDTH * tileset_small_sprite_width(tileset))
46#define FEELING_HEIGHT (tileset_small_sprite_height(tileset))
47
48#define NUM_HAPPINESS_MODIFIERS 6
49
51
52struct happiness_dialog {
53 struct city *pcity;
54 GtkWidget *win;
55 GtkWidget *shell;
56 GtkWidget *cityname_label;
60 GtkWidget *close;
61};
62
63#define SPECLIST_TAG dialog
64#define SPECLIST_TYPE struct happiness_dialog
65#include "speclist.h"
66
67#define dialog_list_iterate(dialoglist, pdialog) \
68 TYPED_LIST_ITERATE(struct happiness_dialog, dialoglist, pdialog)
69#define dialog_list_iterate_end LIST_ITERATE_END
70
71static struct dialog_list *dialog_list;
72static struct happiness_dialog *get_happiness_dialog(struct city *pcity);
74 bool low_dlg,
75 GtkWidget *win);
76static gboolean show_happiness_popup(GtkGestureClick *gesture,
77 int n_press,
78 double x, double y, gpointer data);
79
80/**********************************************************************/
84{
85 dialog_list = dialog_list_new();
86}
87
88/**********************************************************************/
92{
93 dialog_list_destroy(dialog_list);
94}
95
96/**********************************************************************/
100{
102 if (pdialog->pcity == pcity) {
103 return pdialog;
104 }
106
107 return NULL;
108}
109
110/**********************************************************************/
113static gboolean show_happiness_popup(GtkGestureClick *gesture,
114 int n_press,
115 double x, double y, gpointer data)
116{
117 GtkWidget *w = gtk_event_controller_get_widget(GTK_EVENT_CONTROLLER(gesture));
118 struct happiness_dialog *pdialog = g_object_get_data(G_OBJECT(w),
119 "pdialog");
120 GtkWidget *p, *label;
121 char buf[1024];
122
123 switch (GPOINTER_TO_UINT(data)) {
124 case CITIES:
125 sz_strlcpy(buf, text_happiness_cities(pdialog->pcity));
126 break;
127 case LUXURIES:
129 break;
130 case BUILDINGS:
132 break;
133 case NATIONALITY:
135 break;
136 case UNITS:
137 sz_strlcpy(buf, text_happiness_units(pdialog->pcity));
138 break;
139 case WONDERS:
141 break;
142 default:
143 return TRUE;
144 }
145
146 p = gtk_popover_new();
147
148 gtk_widget_set_parent(p, w);
149
150 label = gtk_label_new(buf);
151 /* FIXME: there is no font option corresponding to this style name.
152 * Remove?: */
153 gtk_widget_set_name(label, "city_happiness_label");
154 gtk_widget_set_margin_start(label, 4);
155 gtk_widget_set_margin_end(label, 4);
156 gtk_widget_set_margin_top(label, 4);
157 gtk_widget_set_margin_bottom(label, 4);
158 gtk_popover_set_child(GTK_POPOVER(p), label);
159
160 gtk_popover_popup(GTK_POPOVER(p));
161
162 return TRUE;
163}
164
165/**********************************************************************/
169 bool low_dlg,
170 GtkWidget *win)
171{
172 int i;
173 struct happiness_dialog *pdialog;
174 GtkWidget *label, *table;
175 char buf[700];
176
177 static const char *happiness_label_str[NUM_HAPPINESS_MODIFIERS] = {
178 N_("Cities:"),
179 N_("Luxuries:"),
180 N_("Buildings:"),
181 N_("Nationality:"),
182 N_("Units:"),
183 N_("Wonders:"),
184 };
185 static bool happiness_label_str_done;
186
187 pdialog = fc_malloc(sizeof(struct happiness_dialog));
188 pdialog->pcity = pcity;
189
190 pdialog->shell = gtk_grid_new();
191 gtk_orientable_set_orientation(GTK_ORIENTABLE(pdialog->shell),
192 GTK_ORIENTATION_VERTICAL);
193
194 pdialog->cityname_label = gtk_frame_new(_("Happiness"));
195 gtk_grid_attach(GTK_GRID(pdialog->shell), pdialog->cityname_label, 0, 0, 1, 1);
196
197 table = gtk_grid_new();
198 gtk_widget_set_margin_bottom(table, 4);
199 gtk_widget_set_margin_end(table, 4);
200 gtk_widget_set_margin_start(table, 4);
201 gtk_widget_set_margin_top(table, 4);
202 gtk_grid_set_row_spacing(GTK_GRID(table), 10);
203
204 intl_slist(ARRAY_SIZE(happiness_label_str), happiness_label_str,
205 &happiness_label_str_done);
206
207 gtk_frame_set_child(GTK_FRAME(pdialog->cityname_label), table);
208
209 for (i = 0; i < NUM_HAPPINESS_MODIFIERS; i++) {
210 GtkWidget *pic;
211 GtkEventController *controller;
212
213 /* Set spacing between lines of citizens*/
214
215 /* Happiness labels */
216 label = gtk_label_new(happiness_label_str[i]);
217 pdialog->happiness_label[i] = label;
218 gtk_widget_set_name(label, "city_label");
219 gtk_widget_set_halign(label, GTK_ALIGN_START);
220 gtk_widget_set_valign(label, GTK_ALIGN_CENTER);
221
222 gtk_grid_attach(GTK_GRID(table), label, 0, i, 1, 1);
223
224 /* List of citizens */
225 pdialog->feeling_surfaces[i] = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
228 pic = gtk_picture_new();
229 pdialog->feeling_pics[i] = pic;
230 gtk_widget_set_margin_start(pic, 5);
231 g_object_set_data(G_OBJECT(pic), "pdialog", pdialog);
232
233 controller = GTK_EVENT_CONTROLLER(gtk_gesture_click_new());
234 g_signal_connect(controller, "pressed",
235 G_CALLBACK(show_happiness_popup), GUINT_TO_POINTER(i));
236 gtk_widget_add_controller(pic, controller);
237
238 gtk_widget_set_halign(pic, GTK_ALIGN_START);
239 gtk_widget_set_valign(pic, GTK_ALIGN_START);
240
241 gtk_grid_attach(GTK_GRID(table), pic, 1, i, 1, 1);
242 }
243
244 /* TRANS: the width of this text defines the width of the city dialog.
245 * '%s' is either space or newline depending on screen real estate. */
246 fc_snprintf(buf, sizeof(buf),
247 _("Additional information is available%svia left "
248 "click on the citizens."), low_dlg ? "\n" : " ");
249 label = gtk_label_new(buf);
250 gtk_widget_set_name(label, "city_label");
251 gtk_widget_set_halign(label, GTK_ALIGN_START);
252 gtk_widget_set_valign(label, GTK_ALIGN_CENTER);
253 gtk_grid_attach(GTK_GRID(table), label, 0, NUM_HAPPINESS_MODIFIERS, 2, 1);
254
255 gtk_widget_show(pdialog->shell);
256 dialog_list_prepend(dialog_list, pdialog);
258
259 pdialog->win = win;
260
261 return pdialog;
262}
263
264/**********************************************************************/
267static void refresh_feeling_surface(GtkWidget *pic,
268 cairo_surface_t *dst, struct city *pcity,
269 enum citizen_feeling index)
270{
271 enum citizen_category categories[MAX_CITY_SIZE];
272 int i;
273 int num_citizens = get_city_citizen_types(pcity, index, categories);
274 int offset = MIN(tileset_small_sprite_width(tileset), FEELING_WIDTH / num_citizens);
275 cairo_t *cr;
276
277 cr = cairo_create(dst);
278
279 for (i = 0; i < num_citizens; i++) {
280 cairo_set_source_surface(cr,
281 get_citizen_sprite(tileset, categories[i], i, pcity)->surface,
282 i * offset, 0);
283 cairo_rectangle(cr, i * offset, 0, offset, FEELING_HEIGHT);
284 cairo_fill(cr);
285 }
286
287 picture_set_from_surface(GTK_PICTURE(pic), dst);
288
289 cairo_destroy(cr);
290}
291
292/**********************************************************************/
296{
297 int i;
299
300 for (i = 0; i < FEELING_LAST; i++) {
302 pdialog->feeling_surfaces[i], pdialog->pcity, i);
303 }
304}
305
306/**********************************************************************/
310{
312 int i;
313
314 if (pdialog == NULL) {
315 /* City which is being investigated doesn't contain happiness tab */
316 return;
317 }
318
319 gtk_widget_hide(pdialog->shell);
320
321 dialog_list_remove(dialog_list, pdialog);
322
323 gtk_box_remove(GTK_BOX(gtk_widget_get_parent(pdialog->shell)),
324 pdialog->shell);
325
326 for (i = 0; i < NUM_HAPPINESS_MODIFIERS; i++) {
327 cairo_surface_destroy(pdialog->feeling_surfaces[i]);
328 }
329
330 free(pdialog);
331}
332
333/**********************************************************************/
336GtkWidget *get_top_happiness_display(struct city *pcity, bool low_dlg,
337 GtkWidget *win)
338{
339 return create_happiness_dialog(pcity, low_dlg, win)->shell;
340}
citizen_category
Definition city.h:259
#define MAX_CITY_SIZE
Definition city.h:98
citizen_feeling
Definition city.h:270
@ FEELING_LAST
Definition city.h:277
int get_city_citizen_types(struct city *pcity, enum citizen_feeling idx, enum citizen_category *categories)
#define _(String)
Definition fcintl.h:67
#define N_(String)
Definition fcintl.h:69
void intl_slist(int n, const char **s, bool *done)
Definition gui_stuff.c:105
void happiness_dialog_init(void)
Definition happiness.c:86
#define FEELING_WIDTH
Definition happiness.c:45
void close_happiness_dialog(struct city *pcity)
Definition happiness.c:330
static struct happiness_dialog * get_happiness_dialog(struct city *pcity)
Definition happiness.c:102
void happiness_dialog_done(void)
Definition happiness.c:94
#define FEELING_HEIGHT
Definition happiness.c:46
static struct happiness_dialog * create_happiness_dialog(struct city *pcity, bool low_dlg, GtkWidget *win)
Definition happiness.c:198
#define dialog_list_iterate_end
Definition happiness.c:69
static gboolean show_happiness_popup(GtkWidget *w, GdkEventButton *ev, gpointer data)
Definition happiness.c:116
void refresh_happiness_dialog(struct city *pcity)
Definition happiness.c:317
#define dialog_list_iterate(dialoglist, pdialog)
Definition happiness.c:67
static struct dialog_list * dialog_list
Definition happiness.c:71
@ NATIONALITY
Definition happiness.c:50
@ UNITS
Definition happiness.c:50
@ BUILDINGS
Definition happiness.c:50
@ WONDERS
Definition happiness.c:50
@ CITIES
Definition happiness.c:50
@ LUXURIES
Definition happiness.c:50
GtkWidget * get_top_happiness_display(struct city *pcity, bool low_dlg, GtkWidget *win)
Definition happiness.c:356
static void refresh_feeling_surface(cairo_surface_t *dst, struct city *pcity, enum citizen_feeling index)
Definition happiness.c:292
#define NUM_HAPPINESS_MODIFIERS
Definition happiness.c:48
#define NUM_HAPPINESS_MODIFIERS
Definition happiness.c:48
void picture_set_from_surface(GtkPicture *pic, cairo_surface_t *surf)
Definition sprite.c:544
#define fc_malloc(sz)
Definition mem.h:34
const char * text_happiness_cities(const struct city *pcity)
Definition text.c:1931
#define ARRAY_SIZE(x)
Definition shared.h:85
#define MIN(x, y)
Definition shared.h:55
Definition city.h:309
GtkWidget * win
Definition happiness.c:54
GtkWidget * cityname_label
Definition happiness.c:56
struct city * pcity
Definition happiness.c:53
GtkWidget * happiness_label[NUM_HAPPINESS_MODIFIERS]
Definition happiness.c:59
GtkWidget * shell
Definition happiness.c:55
GtkWidget * feeling_pics[NUM_HAPPINESS_MODIFIERS]
Definition happiness.c:57
cairo_surface_t * feeling_surfaces[NUM_HAPPINESS_MODIFIERS]
Definition happiness.c:57
GtkWidget * close
Definition happiness.c:60
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:969
#define sz_strlcpy(dest, src)
Definition support.h:167
#define TRUE
Definition support.h:46
const char * text_happiness_nationality(const struct city *pcity)
Definition text.c:1859
const char * text_happiness_wonders(const struct city *pcity)
Definition text.c:1901
const char * text_happiness_units(const struct city *pcity)
Definition text.c:2065
const char * text_happiness_luxuries(const struct city *pcity)
Definition text.c:2103
const char * text_happiness_buildings(const struct city *pcity)
Definition text.c:1831
int tileset_small_sprite_width(const struct tileset *t)
Definition tilespec.c:864
struct sprite * get_citizen_sprite(const struct tileset *t, enum citizen_category type, int citizen_index, const struct city *pcity)
Definition tilespec.c:6437