Freeciv-3.3
Loading...
Searching...
No Matches
mapctrl.cpp
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// Qt
19#include <QApplication>
20#include <QKeyEvent>
21#include <QMouseEvent>
22#include <QPushButton>
23
24// common
25#include "control.h"
26#include "goto.h"
27
28// client
29#include "client_main.h"
30#include "climap.h"
31#include "control.h"
32#include "mapctrl.h"
33#include "themes_common.h"
34#include "tile.h"
35#include "unit.h"
36#include "zoom.h"
37
38// gui-qt
39#include "citydlg.h"
40#include "fc_client.h"
41#include "qtg_cxxside.h"
42#include "shortcuts.h"
43
44extern void side_disable_endturn(bool do_restore);
45extern void update_turn_done_tooltip(void);
46extern void qload_lua_script();
47extern void qreload_lua_script();
48extern "C" int city_buy_production(struct city *pcity);
49
50/**********************************************************************/
54void popup_newcity_dialog(struct unit *punit, const char *suggestname)
55{
56 hud_input_box *ask = new hud_input_box(gui()->central_wdg);
57 int index = tile_index(unit_tile(punit));
58
59 ask->set_text_title_definput(_("What should we call our new city?"),
60 _("Build New City"), suggestname);
61 ask->setAttribute(Qt::WA_DeleteOnClose);
62 QObject::connect(ask, &hud_input_box::finished, [=](int result) {
63 if (result == QDialog::Accepted) {
65
66 ask_bytes = ask->input_edit.text().toUtf8();
68 ask_bytes.data());
69 } else {
70 cancel_city(index_to_tile(&(wld.map), index));
71 }
72 });
73 ask->show();
74
75 return;
76}
77
78/**********************************************************************/
88
89/**********************************************************************/
93{
95 int x, y;
96
97 global_pos = QCursor::pos();
98 local_pos = gui()->mapview_wdg->mapFromGlobal(global_pos);
99 x = local_pos.x();
100 y = local_pos.y();
101
102 if (x >= 0 && y >= 0 && x < mapview.width && y < mapview.width) {
103 update_line(x, y);
104 }
105}
106
107/**********************************************************************/
112{
113 // PLS DONT PORT IT
114}
115
116/**********************************************************************/
120{
121 Qt::KeyboardModifiers key_mod = QApplication::keyboardModifiers();
122 bool is_shift = key_mod.testFlag(Qt::ShiftModifier);
123
124 if (C_S_RUNNING == client_state()) {
125 if (is_shift) {
126 switch (event->key()) {
127 case Qt::Key_Return:
128 case Qt::Key_Enter:
129 key_end_turn();
130 return;
131 default:
132 break;
133 }
134 }
135
136 switch (event->key()) {
137 case Qt::Key_Up:
138 case Qt::Key_8:
139 if (is_shift) {
140 recenter_button_pressed(gui()->mapview_wdg->width() / 2, 0);
141 } else {
143 }
144 return;
145 case Qt::Key_Left:
146 case Qt::Key_4:
147 if (is_shift) {
148 recenter_button_pressed(0, gui()->mapview_wdg->height() / 2);
149 } else {
151 }
152 return;
153 case Qt::Key_Right:
154 case Qt::Key_6:
155 if (is_shift) {
156 recenter_button_pressed(gui()->mapview_wdg->width(),
157 gui()->mapview_wdg->height() / 2);
158 } else {
160 }
161 return;
162 case Qt::Key_Down:
163 case Qt::Key_2:
164 if (is_shift) {
165 recenter_button_pressed(gui()->mapview_wdg->width() / 2,
166 gui()->mapview_wdg->height());
167 } else {
169 }
170 return;
171 case Qt::Key_PageUp:
172 case Qt::Key_9:
174 return;
175 case Qt::Key_PageDown:
176 case Qt::Key_3:
178 return;
179 case Qt::Key_Home:
180 case Qt::Key_7:
182 return;
183 case Qt::Key_End:
184 case Qt::Key_1:
186 return;
187 case Qt::Key_5:
188 case Qt::Key_Clear:
190 return;
191 case Qt::Key_Escape:
193 if (gui()->infotab->chat_maximized) {
194 gui()->infotab->restore_chat();
195 }
196 return;
197 case Qt::Key_Enter:
198 case Qt::Key_Return:
199 if (!gui()->infotab->chat_maximized) {
200 gui()->infotab->maximize_chat();
201 gui()->infotab->chtwdg->chat_line->setFocus();
202 }
203 return;
204 default:
205 break;
206 }
207 shortcut_pressed(event->key());
208 }
209}
210
211/**********************************************************************/
215{
216 Qt::MouseButtons bt;
217 QPoint pos;
218 fc_shortcut *sc;
220 Qt::KeyboardModifiers md;
222 struct tile *ctile = nullptr;
223 struct city *pcity = nullptr;
224
225 md = QApplication::keyboardModifiers();
226 bt = QApplication::mouseButtons();
227 pos = mapFromGlobal(QCursor::pos());
228
230 pcity = ctile ? tile_city(ctile) : nullptr;
231
232 if (pcity && pcity->owner != client_player()) {
233 pcity = nullptr;
234 }
235
236 // Trade Generator - skip
238 if (bt == sc->mouse && md == sc->mod
239 && gui()->trade_gen.hover_city) {
241 gui()->trade_gen.add_tile(ctile);
242 gui()->mapview_wdg->repaint();
243
244 return;
245 }
246
247 // Rally point - select city - skip
248 if (bt == sc->mouse && md == sc->mod
249 && gui()->rallies.hover_city) {
250 char text[1024];
251 struct city *rcity;
252
255 if (rcity) {
256 gui()->rallies.hover_tile = true;
257 gui()->rallies.rally_city = rcity;
258
259 fc_snprintf(text, sizeof(text),
260 _("Selected city %s. Now choose rally point."),
263 } else {
264 output_window_append(ftc_client, _("No city selected. Aborted"));
265 }
266
267 return;
268 }
269
270 // Rally point - select tile - skip
271 if (bt == Qt::LeftButton && gui()->rallies.hover_tile && ctile != nullptr) {
272 char text[1024];
273 struct city *rcity = gui()->rallies.rally_city;
274
275 fc_assert_ret(rcity != nullptr);
276
278 fc_snprintf(text, sizeof(text),
279 _("Tile %s set as rally point from city %s."),
282 } else {
283 fc_snprintf(text, sizeof(text),
284 _("Could not set rally point for city %s."),
287 }
288
289 gui()->rallies.rally_city = nullptr;
290 gui()->rallies.hover_tile = false;
291
292 return;
293 }
294
295 if (bt == Qt::LeftButton && gui()->menu_bar->delayed_order) {
297 gui()->menu_bar->set_tile_for_order(ctile);
300 gui()->menu_bar->delayed_order = false;
301
302 return;
303 }
304
305 if (bt == Qt::LeftButton && gui()->infotab->chat_maximized) {
306 gui()->infotab->restore_chat();
307 }
308 if (bt == Qt::LeftButton && gui()->menu_bar->quick_airlifting) {
309 struct city *acity;
310
313 if (acity) {
314 multiairlift(acity, gui()->menu_bar->airlift_type_id);
315 } else {
316 output_window_append(ftc_client, "No city selected for airlift");
317 }
318 gui()->menu_bar->quick_airlifting = false;
319
320 return;
321 }
322
323 // Check configured shortcuts
324 if (!gui()->menu_bar->delayed_order) {
326 if (((key && key == sc->key) || bt == sc->mouse) && md == sc->mod
327 && pcity != nullptr) {
328 pw = new production_widget(this, pcity, false, 0, 0, true);
329 pw->show();
330
331 return;
332 }
333
335 if (((key && key == sc->key) || bt == sc->mouse) && md == sc->mod
336 && ctile != nullptr && unit_list_size(ctile->units) > 0) {
337 gui()->toggle_unit_sel_widget(ctile);
338
339 return;
340 }
341
343 if (((key && key == sc->key) || bt == sc->mouse) && md == sc->mod
344 && ctile != nullptr) {
346
347 return;
348 }
349
351 if (((key && key == sc->key) || bt == sc->mouse) && md == sc->mod
352 && gui()->battlelog_wdg != nullptr) {
353 gui()->battlelog_wdg->show();
354
355 return;
356 }
357
359 if (((key && key == sc->key) || bt == sc->mouse) && md == sc->mod
360 && pcity != nullptr) {
362
363 return;
364 }
365
367 if (((key && key == sc->key) || bt == sc->mouse) && md == sc->mod) {
369
370 return;
371 }
372
374 if (((key && key == sc->key) || bt == sc->mouse) && md == sc->mod) {
375 QPixmapCache::clear();
376 tilespec_reread(tileset_basename(tileset), true, gui()->map_scale);
377
378 return;
379 }
380
382 if (((key && key == sc->key) || bt == sc->mouse) && md == sc->mod) {
384
385 return;
386 }
387
389 if (((key && key == sc->key) || bt == sc->mouse) && md == sc->mod) {
391
392 return;
393 }
394
396 if (((key && key == sc->key) || bt == sc->mouse) && md == sc->mod) {
397 key_city_overlay(pos.x(), pos.y());
398
399 return;
400 }
401
403 if (((key && key == sc->key) || bt == sc->mouse) && md == sc->mod
404 && ctile != nullptr) {
405 gui()->infotab->chtwdg->make_link(ctile);
406
407 return;
408 }
409
411 if (((key && key == sc->key) || bt == sc->mouse) && md == sc->mod
412 && pcity != nullptr) {
414
415 return;
416 }
417
419 if (((key && key == sc->key) || bt == sc->mouse) && md == sc->mod
420 && pcity != nullptr) {
421 pw = new production_widget(this, pcity, false, 0, 0, true, true);
422 pw->show();
423
424 return;
425 }
426
428 if (((key && key == sc->key) || bt == sc->mouse) && md == sc->mod) {
430
431 return;
432 }
433
435 if (((key && key == sc->key) || bt == sc->mouse) && md == sc->mod) {
437
438 return;
439 }
440 }
441
444 if ((((key && key == sc->key) || (bt == sc->mouse
445 // Check if keyboardless goto active and its shortcut if pressed
446 || (goto_is_active() && (bt == (sc->mouse | sc_sec->mouse)))))
447 && md == sc->mod)) {
449
450 return;
451 }
452
454 if (((key && key == sc->key) || bt == sc->mouse) && md == sc->mod) {
455 if (!goto_is_active()) {
459 }
460
461 return;
462 }
463
465 if (((key && key == sc->key) || bt == sc->mouse)
466 && md == sc->mod && ctile != nullptr) {
467 gui()->popup_tile_info(ctile);
468
469 return;
470 }
471
473 if (((key && key == sc->key) || bt == sc->mouse) && md == sc->mod) {
474 wakeup_button_pressed(pos.x(), pos.y());
475
476 return;
477 }
478}
479
480/**********************************************************************/
483void map_view::shortcut_released(Qt::MouseButton bt)
484{
485 QPoint pos;
486 fc_shortcut *sc;
487 Qt::KeyboardModifiers md;
488 md = QApplication::keyboardModifiers();
489 pos = mapFromGlobal(QCursor::pos());
490
492 if (bt == sc->mouse && md == sc->mod) {
493 gui()->popdown_tile_info();
494 return;
495 }
496
498 if (bt == sc->mouse && md == sc->mod) {
499 if (gui()->trade_gen.hover_city
500 || gui()->rallies.hover_city) {
501 gui()->trade_gen.hover_city = false;
502 gui()->rallies.hover_city = false;
503 return;
504 }
505 if (menu_click) {
506 menu_click = false;
507 return;
508 }
512 }
513 release_goto_button(pos.x(), pos.y());
514 return;
515 }
516}
517
518/**********************************************************************/
525
526/**********************************************************************/
533
534/**********************************************************************/
538{
539 update_line(event->pos().x(), event->pos().y());
540
542 maybe_activate_keyboardless_goto(event->pos().x(), event->pos().y());
543 }
545 event->pos().y(),
546 map_zoom));
547}
548
549/**********************************************************************/
553{
554 struct unit *punit = nullptr;
555
556 if (info_tile_wdg != nullptr) {
558 }
559
560 if (TILE_UNKNOWN != client_tile_get_known(ptile)) {
561 mapdeco_set_crosshair(ptile, true);
562 punit = find_visible_unit(ptile);
563 if (punit) {
567 }
568 }
569 info_tile_wdg = new info_tile(ptile, mapview_wdg);
570 info_tile_wdg->show();
571 }
572}
573
574/**********************************************************************/
578{
581 if (info_tile_wdg != nullptr) {
582 info_tile_wdg->close();
583 delete info_tile_wdg;
584 info_tile_wdg = nullptr;
585 }
586}
void output_window_append(const struct ft_color color, const char *featured_text)
int city_buy_production(struct city *pcity)
info_tile * info_tile_wdg
Definition fc_client.h:218
void popdown_tile_info()
Definition mapctrl.cpp:577
map_view * mapview_wdg
Definition fc_client.h:230
void popup_tile_info(struct tile *ptile)
Definition mapctrl.cpp:552
static fc_shortcuts * sc()
fc_shortcut * get_shortcut(shortcut_id id)
bool menu_click
Definition mapview.h:92
bool stored_autocenter
Definition mapview.h:107
void keyPressEvent(QKeyEvent *event)
Definition mapctrl.cpp:119
void mouseMoveEvent(QMouseEvent *event)
Definition mapctrl.cpp:537
void mousePressEvent(QMouseEvent *event)
Definition mapctrl.cpp:521
void mouseReleaseEvent(QMouseEvent *event)
Definition mapctrl.cpp:529
void shortcut_released(Qt::MouseButton mb)
Definition mapctrl.cpp:483
void shortcut_pressed(int key)
Definition mapctrl.cpp:214
enum client_states client_state(void)
#define client_player()
@ C_S_RUNNING
Definition client_main.h:47
enum known_type client_tile_get_known(const struct tile *ptile)
Definition climap.c:36
char * incite_cost
Definition comments.c:76
void key_recall_previous_focus_unit(void)
Definition control.c:3320
void key_cancel_action(void)
Definition control.c:3258
void clear_hover_state(void)
Definition control.c:328
void key_end_turn(void)
Definition control.c:3312
void key_unit_move(enum direction8 gui_dir)
Definition control.c:3341
enum cursor_hover_state hover_state
Definition control.c:89
void finish_city(struct tile *ptile, const char *name)
Definition control.c:4091
struct unit * find_visible_unit(struct tile *ptile)
Definition control.c:822
void control_mouse_cursor(struct tile *ptile)
Definition control.c:1215
void cancel_city(struct tile *ptile)
Definition control.c:4110
@ SELECT_APPEND
Definition control.h:38
@ SELECT_POPUP
Definition control.h:38
@ SELECT_FOCUS
Definition control.h:38
@ HOVER_NONE
Definition control.h:26
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit * punit
Definition dialogs_g.h:74
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit int const struct action *paction struct unit struct city * pcity
Definition dialogs_g.h:78
enum event_type event
Definition events.c:81
#define _(String)
Definition fcintl.h:67
const char * tile_link(const struct tile *ptile)
const struct ft_color ftc_client
const char * city_link(const struct city *pcity)
struct world wld
Definition game.c:62
void exit_goto_state(void)
Definition goto.c:1033
bool goto_is_active(void)
Definition goto.c:1070
bool send_rally_tile(struct city *pcity, struct tile *ptile, bool persistent)
Definition goto.c:1593
static struct tile * pos
Definition finddlg.c:53
static bool load_theme
Definition theme_dlg.c:30
#define fc_assert_ret(condition)
Definition log.h:192
void qload_lua_script()
void qreload_lua_script()
struct tile * index_to_tile(const struct civ_map *imap, int mindex)
Definition map.c:471
void popup_newcity_dialog(struct unit *punit, const char *suggestname)
Definition mapctrl.cpp:54
int city_buy_production(struct city *pcity)
void qload_lua_script()
void create_line_at_mouse_pos(void)
Definition mapctrl.cpp:92
void set_turn_done_button_state(bool state)
Definition mapctrl.cpp:82
void side_disable_endturn(bool do_restore)
Definition sidebar.cpp:676
void update_rect_at_mouse_pos(void)
Definition mapctrl.cpp:111
void update_turn_done_tooltip(void)
Definition gui_main.c:1109
void qreload_lua_script()
void update_line(int canvas_x, int canvas_y)
void maybe_activate_keyboardless_goto(int canvas_x, int canvas_y)
void adjust_workers_button_pressed(int canvas_x, int canvas_y)
void release_goto_button(int canvas_x, int canvas_y)
void recenter_button_pressed(int canvas_x, int canvas_y)
void action_button_pressed(int canvas_x, int canvas_y, enum quickselect_type qtype)
void wakeup_button_pressed(int canvas_x, int canvas_y)
bool keyboardless_goto_button_down
void key_city_overlay(int canvas_x, int canvas_y)
bool keyboardless_goto_active
void clipboard_paste_production(struct city *pcity)
bool clipboard_copy_production(struct tile *ptile)
void mapdeco_set_crosshair(const struct tile *ptile, bool crosshair)
struct view mapview
struct tile * canvas_pos_to_tile(float canvas_x, float canvas_y, float zoom)
void mapdeco_set_gotoroute(const struct unit *punit)
void mapdeco_clear_gotoroutes(void)
void mapdeco_clear_crosshairs(void)
void multiairlift(struct city *acity, Unit_type_id ut)
Definition menu.cpp:3919
static mpgui * gui
Definition mpgui_qt.cpp:52
struct client_options gui_options
Definition options.c:71
@ SC_POPUP_COMB_INF
Definition shortcuts.h:83
@ SC_HIDE_WORKERS
Definition shortcuts.h:52
@ SC_POPUP_INFO
Definition shortcuts.h:47
@ SC_RELOAD_LUA
Definition shortcuts.h:90
@ SC_SHOW_UNITS
Definition shortcuts.h:53
@ SC_PASTE_PROD
Definition shortcuts.h:50
@ SC_WAKEUP_SENTRIES
Definition shortcuts.h:48
@ SC_MAKE_LINK
Definition shortcuts.h:49
@ SC_APPEND_FOCUS
Definition shortcuts.h:46
@ SC_QUICK_BUY
Definition shortcuts.h:42
@ SC_RELOAD_THEME
Definition shortcuts.h:84
@ SC_RELOAD_TILESET
Definition shortcuts.h:85
@ SC_BUY_MAP
Definition shortcuts.h:79
@ SC_SCROLL_MAP
Definition shortcuts.h:35
@ SC_QUICK_SELECT
Definition shortcuts.h:43
@ SC_ADJUST_WORKERS
Definition shortcuts.h:45
@ SC_SELECT_BUTTON
Definition shortcuts.h:44
@ SC_COPY_PROD
Definition shortcuts.h:51
@ SC_LOAD_LUA
Definition shortcuts.h:89
struct sprite int int y
Definition sprite_g.h:31
struct sprite int x
Definition sprite_g.h:31
Definition city.h:317
char gui_qt_default_theme_name[512]
Definition options.h:446
bool auto_center_on_unit
Definition options.h:156
Qt::KeyboardModifiers mod
Definition shortcuts.h:109
Qt::MouseButton mouse
Definition shortcuts.h:108
Definition tile.h:50
Definition unit.h:140
struct tile * goto_tile
Definition unit.h:157
struct player * owner
Definition unit.h:145
int width
struct civ_map map
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:960
#define FALSE
Definition support.h:47
struct city * tile_city(const struct tile *ptile)
Definition tile.c:83
#define tile_index(_pt_)
Definition tile.h:89
@ TILE_UNKNOWN
Definition tile.h:36
const char * tileset_basename(const struct tileset *t)
Definition tilespec.c:730
bool tilespec_reread(const char *new_tileset_name, bool game_fully_initialized, float scale)
Definition tilespec.c:1401
bool unit_has_orders(const struct unit *punit)
Definition unit.c:202
#define unit_tile(_pu)
Definition unit.h:404
float map_zoom
Definition zoom.c:25