The Battle for Wesnoth  1.15.7+dev
context_manager.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2018 by David White <dave@whitevine.net>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 #define GETTEXT_DOMAIN "wesnoth-editor"
15 
16 #include "resources.hpp"
17 #include "team.hpp"
18 
19 #include "display.hpp"
22 #include "filesystem.hpp"
23 #include "formula/string_utils.hpp"
24 #include "game_board.hpp"
27 #include "gettext.hpp"
28 
29 #include "editor/action/action.hpp"
31 #include "preferences/editor.hpp"
32 
38 #include "gui/dialogs/message.hpp"
40 #include "gui/widgets/retval.hpp"
41 
44 #include "game_config_view.hpp"
45 
46 #include "terrain/translation.hpp"
47 
48 #include <memory>
49 
50 namespace editor {
51 
52 static std::vector<std::string> saved_windows_;
53 
54 static const std::string get_menu_marker(const bool changed)
55 {
56  std::ostringstream ss;
57  ss << "[<span ";
58 
59  if(changed) {
60  ss << "color='#f00' ";
61  }
62 
63  ss << "size='large'>" << font::unicode_bullet << "</span>]";
64  return ss.str();
65 }
66 
68  : locs_(nullptr)
69  , gui_(gui)
70  , game_config_(game_config)
71  , default_dir_(preferences::editor::default_dir())
72  , map_generators_()
73  , last_map_generator_(nullptr)
74  , current_context_index_(0)
75  , auto_update_transitions_(preferences::editor::auto_update_transitions())
76  , map_contexts_()
77  , clipboard_()
78 {
79  resources::filter_con = this;
80 
81  if(default_dir_.empty()) {
83  }
84 
86  init_map_generators(game_config);
87 }
88 
90 {
91  // Restore default window title
93 
94  resources::filter_con = nullptr;
95 }
96 
98 {
100 
101  // TODO register the tod_manager with the gui?
104 
105  // Reset side when switching to an existing scenario
106  if (gui().get_teams().size() > 0) {
107  gui().set_team(0, true);
108  gui().set_playing_team(0);
109  }
110  gui().init_flags();
111 
112  reload_map();
113 
114  // Enable the labels of the current context;
116 
118 }
119 
121 {
122  gui_.rebuild_all();
128  if(locs_) {
129  for(const auto& loc : get_map_context().map().special_locations().left) {
130  locs_->add_item(loc.first);
131  }
132  if(!get_map_context().is_pure_map()) {
133  // If the scenario has more than 9 teams, add locations for them
134  // (First 9 teams are always in the list)
135  size_t n_teams = get_map_context().teams().size();
136  for(size_t i = 10; i <= n_teams; i++) {
137  locs_->add_item(std::to_string(i));
138  }
139  }
140  }
141 }
142 
144 {
145  gui_.reload_map();
148  refresh_all();
149 }
150 
152 {
153  switch (auto_update_transitions_) {
155  return (item == "editor-auto-update-transitions");
157  return (item == "editor-partial-update-transitions");
159  return (item == "editor-no-update-transitions");
160  }
161 
162  return true; //should not be reached
163 }
164 
166 {
169 
171  return true;
172  }
173 
174  return false;
175 }
176 
178 {
179  std::vector<std::string> modified;
180  for(auto& mc : map_contexts_) {
181  if(mc->modified()) {
182  if(!mc->get_name().empty()) {
183  modified.push_back(mc->get_name());
184  } else if(!mc->get_filename().empty()) {
185  modified.push_back(mc->get_filename());
186  } else {
187  modified.push_back(mc->get_default_context_name());
188  }
189  }
190  }
191 
192  for(std::string& str : modified) {
193  message += "\n" + font::unicode_bullet + " " + str;
194  }
195 
196  return modified.size();
197 }
198 
199 void context_manager::load_map_dialog(bool force_same_context /* = false */)
200 {
202  if(fn.empty()) {
203  fn = default_dir_;
204  }
205 
207 
208  dlg.set_title(_("Load Map"))
209  .set_path(fn);
210 
211  if(dlg.show()) {
212  load_map(dlg.path(), !force_same_context);
213  }
214 }
215 
216 void context_manager::load_mru_item(unsigned int index, bool force_same_context /* = false */)
217 {
218  const std::vector<std::string>& mru = preferences::editor::recent_files();
219  if(mru.empty() || index >= mru.size()) {
220  return;
221  }
222 
223  load_map(mru[index], !force_same_context);
224 }
225 
227 {
228  team& t = get_map_context().teams()[side_index];
229 
230  editor_team_info team_info(t);
231 
233  get_map_context().set_side_setup(team_info);
234  }
235 }
236 
238 {
239  map_context& context = get_map_context();
240 
241  // TODO
242  //std::string fn = filesystem::directory_name(context.get_filename());
243 
244  std::string id = context.get_id();
245  std::string name = context.get_name();
246  std::string description = context.get_description();
247 
248  int turns = context.get_time_manager()->number_of_turns();
249  int xp_mod = context.get_xp_mod() ? *context.get_xp_mod() : 70;
250 
251  bool victory = context.victory_defeated();
252  bool random = context.random_start_time();
253 
255  id, name, description, turns, xp_mod, victory, random
256  );
257 
258  if(!ok) {
259  return;
260  }
261 
262  context.set_scenario_setup(id, name, description, turns, xp_mod, victory, random);
263 
264  if(!name.empty()) {
266  }
267 }
268 
270 {
271  const editor_map& map = get_map_context().map();
272 
273  int w = map.w();
274  int h = map.h();
275 
276  if(gui2::dialogs::editor_new_map::execute(_("New Map"), w, h)) {
278  new_map(w, h, fill, true);
279  }
280 }
281 
283 {
284  const editor_map& map = get_map_context().map();
285 
286  int w = map.w();
287  int h = map.h();
288 
289  if(gui2::dialogs::editor_new_map::execute(_("New Scenario"), w, h)) {
291  new_scenario(w, h, fill, true);
292  }
293 }
294 
295 void context_manager::expand_open_maps_menu(std::vector<config>& items, int i)
296 {
297  auto pos = items.erase(items.begin() + i);
298  std::vector<config> contexts;
299 
300  for(std::size_t mci = 0; mci < map_contexts_.size(); ++mci) {
301  map_context& mc = *map_contexts_[mci];
302 
303  std::string filename;
304  if(mc.is_pure_map()) {
305  filename = filesystem::base_name(mc.get_filename());
306  } else {
307  filename = mc.get_name();
308  }
309 
310  if(filename.empty()) {
311  filename = mc.get_default_context_name();
312  }
313 
314  std::ostringstream ss;
315  ss << "[" << mci + 1 << "] ";
316 
317  const bool changed = mc.modified();
318 
319  if(changed) {
320  ss << "<i>" << filename << "</i>";
321  } else {
322  ss << filename;
323  }
324 
325  if(mc.is_embedded()) {
326  ss << " (E)";
327  }
328 
329  const std::string label = ss.str();
330  const std::string details = get_menu_marker(changed);
331 
332  contexts.emplace_back("label", label, "details", details);
333  }
334 
335  items.insert(pos, contexts.begin(), contexts.end());
336 }
337 
338 void context_manager::expand_load_mru_menu(std::vector<config>& items, int i)
339 {
340  std::vector<std::string> mru = preferences::editor::recent_files();
341 
342  auto pos = items.erase(items.begin() + i);
343 
344  if(mru.empty()) {
345  items.insert(pos, config {"label", _("No Recent Files")});
346  return;
347  }
348 
349  for(std::string& path : mru) {
350  // TODO: add proper leading ellipsization instead, since otherwise
351  // it'll be impossible to tell apart files with identical names and
352  // different parent paths.
354  }
355 
356  std::vector<config> temp;
357  std::transform(mru.begin(), mru.end(), std::back_inserter(temp), [](const std::string& str) {
358  return config {"label", str};
359  });
360 
361  items.insert(pos, temp.begin(), temp.end());
362 }
363 
364 void context_manager::expand_areas_menu(std::vector<config>& items, int i)
365 {
367  if(!tod) {
368  return;
369  }
370 
371  auto pos = items.erase(items.begin() + i);
372  std::vector<config> area_entries;
373 
374  std::vector<std::string> area_ids = tod->get_area_ids();
375 
376  for(std::size_t mci = 0; mci < area_ids.size(); ++mci) {
377  const std::string& area = area_ids[mci];
378 
379  std::stringstream ss;
380  ss << "[" << mci + 1 << "] ";\
381 
382  if(area.empty()) {
383  ss << "<i>" << _("Unnamed Area") << "</i>";
384  } else {
385  ss << area;
386  }
387 
388  const bool changed =
389  mci == static_cast<std::size_t>(get_map_context().get_active_area())
390  && tod->get_area_by_index(mci) != get_map_context().map().selection();
391 
392  const std::string label = ss.str();
393  const std::string details = get_menu_marker(changed);
394 
395  area_entries.emplace_back("label", label, "details", details);
396  }
397 
398  items.insert(pos, area_entries.begin(), area_entries.end());
399 }
400 
401 void context_manager::expand_sides_menu(std::vector<config>& items, int i)
402 {
403  auto pos = items.erase(items.begin() + i);
404  std::vector<config> contexts;
405 
406  for(std::size_t mci = 0; mci < get_map_context().teams().size(); ++mci) {
407 
408  const team& t = get_map_context().teams()[mci];
409  const std::string& teamname = t.user_team_name();
410  std::stringstream label;
411  label << "[" << mci+1 << "] ";
412 
413  if(teamname.empty()) {
414  label << "<i>" << _("New Side") << "</i>";
415  } else {
416  label << teamname;
417  }
418 
419  contexts.emplace_back("label", label.str());
420  }
421 
422  items.insert(pos, contexts.begin(), contexts.end());
423 }
424 
425 void context_manager::expand_time_menu(std::vector<config>& items, int i)
426 {
427  auto pos = items.erase(items.begin() + i);
428  std::vector<config> times;
429 
431 
432  assert(tod_m != nullptr);
433 
434  for(const time_of_day& time : tod_m->times()) {
435  times.emplace_back(
436  "details", time.name, // Use 'details' field here since the image will take the first column
437  "image", time.image
438  );
439  }
440 
441  items.insert(pos, times.begin(), times.end());
442 }
443 
444 void context_manager::expand_local_time_menu(std::vector<config>& items, int i)
445 {
446  auto pos = items.erase(items.begin() + i);
447  std::vector<config> times;
448 
450 
451  for(const time_of_day& time : tod_m->times(get_map_context().get_active_area())) {
452  times.emplace_back(
453  "details", time.name, // Use 'details' field here since the image will take the first column
454  "image", time.image
455  );
456  }
457 
458  items.insert(pos, times.begin(), times.end());
459 }
460 
462 {
464  if(fn.empty()) {
465  fn = default_dir_;
466  }
467 
469 
470  dlg.set_title(_("Apply Mask"))
471  .set_path(fn);
472 
473  if(dlg.show()) {
474  try {
475  map_context mask(game_config_, dlg.path());
476  editor_action_apply_mask a(mask.map());
478  } catch (const editor_map_load_exception& e) {
479  gui2::show_transient_message(_("Error loading mask"), e.what());
480  return;
481  } catch (const editor_action_exception& e) {
483  return;
484  }
485  }
486 }
487 
488 void context_manager::perform_refresh(const editor_action& action, bool drag_part /* =false */)
489 {
491  refresh_after_action(drag_part);
492 }
493 
495 {
496  int active_area = get_map_context().get_active_area();
498 
499  if(gui2::dialogs::edit_text::execute(N_("Rename Area"), N_("Identifier:"), name)) {
500  get_map_context().get_time_manager()->set_area_id(active_area, name);
501  }
502 }
503 
505 {
507  if(fn.empty()) {
508  fn = default_dir_;
509  }
510 
512 
513  dlg.set_title(_("Choose Target Map"))
514  .set_path(fn);
515 
516  if(dlg.show()) {
517  try {
518  map_context map(game_config_, dlg.path());
519  editor_action_create_mask a(map.map());
521  } catch (const editor_map_load_exception& e) {
522  gui2::show_transient_message(_("Error loading map"), e.what());
523  return;
524  } catch (const editor_action_exception& e) {
526  return;
527  }
528  }
529 }
530 
532 {
533  if(get_map_context().needs_reload()) {
534  reload_map();
535  return;
536  }
537 
538  const std::set<map_location>& changed_locs = get_map_context().changed_locations();
539 
540  if(get_map_context().needs_terrain_rebuild()) {
543  && (!drag_part || get_map_context().everything_changed())))
544  {
545  gui_.rebuild_all();
548  } else {
549  for(const map_location& loc : changed_locs) {
550  gui_.rebuild_terrain(loc);
551  }
552  gui_.invalidate(changed_locs);
553  }
554  } else {
555  if(get_map_context().everything_changed()) {
557  } else {
558  gui_.invalidate(changed_locs);
559  }
560  }
561 
562  if(get_map_context().needs_labels_reset()) {
564  }
565 
568 }
569 
571 {
572  const editor_map& map = get_map_context().map();
573 
574  int w = map.w();
575  int h = map.h();
576 
578  bool copy = false;
579 
580  if(!gui2::dialogs::editor_resize_map::execute(w, h, dir, copy)) {
581  return;
582  }
583 
584  if(w != map.w() || h != map.h()) {
586  if(copy) {
588  }
589 
590  int x_offset = map.w() - w;
591  int y_offset = map.h() - h;
592 
593  switch (dir) {
597  y_offset = 0;
598  break;
602  y_offset /= 2;
603  break;
607  break;
608  default:
609  y_offset = 0;
610  WRN_ED << "Unknown resize expand direction" << std::endl;
611  break;
612  }
613 
614  switch (dir) {
618  x_offset = 0;
619  break;
623  x_offset /= 2;
624  break;
628  break;
629  default:
630  x_offset = 0;
631  break;
632  }
633 
634  editor_action_resize_map a(w, h, x_offset, y_offset, fill);
635  perform_refresh(a);
636  }
637 }
638 
640 {
641  std::string input_name = get_map_context().get_filename();
642  if(input_name.empty()) {
643  input_name = filesystem::get_dir(default_dir_ + "/maps");
644  }
645 
647 
648  dlg.set_title(_("Save Map As"))
649  .set_save_mode(true)
650  .set_path(input_name)
651  .set_extension(".map");
652 
653  if(!dlg.show()) {
654  return;
655  }
656 
657  save_map_as(dlg.path());
658 }
659 
661 {
662  std::string input_name = get_map_context().get_filename();
663  if(input_name.empty()) {
664  input_name = filesystem::get_dir(default_dir_ + "/scenarios");
665  }
666 
668 
669  dlg.set_title(_("Save Scenario As"))
670  .set_save_mode(true)
671  .set_path(input_name)
672  .set_extension(".cfg");
673 
674  if(!dlg.show()) {
675  return;
676  }
677 
678  save_scenario_as(dlg.path());
679 }
680 
682 {
683  for(const config& i : game_config.child_range("multiplayer")) {
684  if(i["map_generation"].empty() && i["scenario_generation"].empty()) {
685  continue;
686  }
687 
688  const config& generator_cfg = i.child("generator");
689  if(!generator_cfg) {
690  ERR_ED << "Scenario \"" << i["name"] << "\" with id " << i["id"]
691  << " has map_generation= but no [generator] tag" << std::endl;
692  } else {
693  map_generators_.emplace_back(create_map_generator(i["map_generation"].empty() ? i["scenario_generation"] : i["map_generation"], generator_cfg));
694  }
695  }
696 }
697 
699 {
700  if(map_generators_.empty()) {
701  gui2::show_error_message(_("No random map generators found."));
702  return;
703  }
704 
707 
708  if(dialog.show()) {
709  std::string map_string;
711  try {
712  map_string = map_generator->create_map(dialog.get_seed());
713  } catch (const mapgen_exception& e) {
714  gui2::show_transient_message(_("Map creation failed."), e.what());
715  return;
716  }
717 
718  if(map_string.empty()) {
719  gui2::show_transient_message("", _("Map creation failed."));
720  } else {
721  editor_map new_map(game_config_, map_string);
722  editor_action_whole_map a(new_map);
723  get_map_context().set_needs_labels_reset(); // Ensure Player Start labels are updated together with newly generated map
724  perform_refresh(a);
725  }
726 
727  last_map_generator_ = map_generator;
728  }
729 }
730 
732 {
733  if(get_map_context().modified()) {
734  const int res = gui2::show_message(_("Unsaved Changes"),
735  _("Do you want to discard all changes made to the map since the last save?"), gui2::dialogs::message::yes_no_buttons);
736  return gui2::retval::CANCEL != res;
737  }
738 
739  return true;
740 }
741 
743 {
745 }
746 
747 void context_manager::save_all_maps(bool auto_save_windows)
748 {
749  int current = current_context_index_;
750  saved_windows_.clear();
751  for(std::size_t i = 0; i < map_contexts_.size(); ++i) {
752  switch_context(i);
754  if(auto_save_windows) {
755  if(name.empty() || filesystem::is_directory(name)) {
756  std::ostringstream s;
757  s << default_dir_ << "/" << "window_" << i + 1;
758  name = s.str();
760  }
761  }
762  saved_windows_.push_back(name);
763  save_map();
764  }
765 
766  switch_context(current);
767 }
768 
770 {
772  if(name.empty() || filesystem::is_directory(name)) {
773  if(get_map_context().is_pure_map()) {
775  } else {
777  }
778  } else {
779  if(get_map_context().is_pure_map()) {
780  write_map();
781  } else {
782  write_scenario();
783  }
784  }
785 }
786 
788 {
789  std::size_t is_open = check_open_map(filename);
790  if(is_open < map_contexts_.size() && is_open != static_cast<unsigned>(current_context_index_)) {
791  gui2::show_transient_message(_("This scenario is already open."), filename);
792  return false;
793  }
794 
795  std::string old_filename = get_map_context().get_filename();
796  bool embedded = get_map_context().is_embedded();
797 
798  get_map_context().set_filename(filename);
799  get_map_context().set_embedded(false);
800 
801  if(!write_scenario(true)) {
802  get_map_context().set_filename(old_filename);
803  get_map_context().set_embedded(embedded);
804  return false;
805  }
806 
807  return true;
808 }
809 
811 {
812  std::size_t is_open = check_open_map(filename);
813  if(is_open < map_contexts_.size() && is_open != static_cast<unsigned>(current_context_index_)) {
814  gui2::show_transient_message(_("This map is already open."), filename);
815  return false;
816  }
817 
818  std::string old_filename = get_map_context().get_filename();
819  bool embedded = get_map_context().is_embedded();
820 
821  get_map_context().set_filename(filename);
822  get_map_context().set_embedded(false);
823 
824  if(!write_map(true)) {
825  get_map_context().set_filename(old_filename);
826  get_map_context().set_embedded(embedded);
827  return false;
828  }
829 
830  return true;
831 }
832 
833 bool context_manager::write_scenario(bool display_confirmation)
834 {
835  try {
837  if(display_confirmation) {
838  gui2::show_transient_message("", _("Scenario saved."));
839  }
840  } catch (const editor_map_save_exception& e) {
842  return false;
843  }
844 
845  return true;
846 }
847 
848 bool context_manager::write_map(bool display_confirmation)
849 {
850  try {
852  if(display_confirmation) {
853  gui2::show_transient_message("", _("Map saved."));
854  }
855  } catch (const editor_map_save_exception& e) {
857  return false;
858  }
859 
860  return true;
861 }
862 
863 std::size_t context_manager::check_open_map(const std::string& fn) const
864 {
865  std::size_t i = 0;
866  while(i < map_contexts_.size() && map_contexts_[i]->get_filename() != fn) {
867  ++i;
868  }
869 
870  return i;
871 }
872 
874 {
875  std::size_t i = check_open_map(fn);
876  if(i < map_contexts_.size()) {
877  gui2::show_transient_message(_("This map is already open."), fn);
878  switch_context(i);
879  return true;
880  }
881 
882  return false;
883 }
884 
885 void context_manager::load_map(const std::string& filename, bool new_context)
886 {
887  if(new_context && check_switch_open_map(filename)) {
888  return;
889  }
890 
891  LOG_ED << "Load map: " << filename << (new_context ? " (new)" : " (same)") << "\n";
892  try {
893  {
894  context_ptr mc(new map_context(game_config_, filename));
895  if(mc->get_filename() != filename) {
896  if(new_context && check_switch_open_map(mc->get_filename())) {
897  return;
898  }
899  }
900 
901  if(new_context) {
902  int new_id = add_map_context_of(std::move(mc));
903  switch_context(new_id);
904  } else {
905  replace_map_context_with(std::move(mc));
906  }
907  }
908 
909  if(get_map_context().is_embedded()) {
910  const std::string& msg = _("Loaded embedded map data");
911  gui2::show_transient_message(_("Map loaded from scenario"), msg);
912  } else {
913  if(get_map_context().get_filename() != filename) {
914  if(get_map_context().get_map_data_key().empty()) {
915  ERR_ED << "Internal error, map context filename changed: "
916  << filename << " -> " << get_map_context().get_filename()
917  << " with no apparent scenario load\n";
918  } else {
919  utils::string_map symbols;
920  symbols["old"] = filename;
921  const std::string& msg = _("Loaded referenced map file:\n$new");
922  symbols["new"] = get_map_context().get_filename();
923  symbols["map_data"] = get_map_context().get_map_data_key();
924  gui2::show_transient_message(_("Map loaded from scenario"),
925  //TODO: msg is already translated does vgettext make sense?
926  VGETTEXT(msg.c_str(), symbols));
927  }
928  }
929  }
930  } catch(const editor_map_load_exception& e) {
931  gui2::show_transient_message(_("Error loading map"), e.what());
932  return;
933  }
934 }
935 
937 {
938  if(!confirm_discard()) {
939  return;
940  }
941 
943  if(filename.empty()) {
944  ERR_ED << "Empty filename in map revert" << std::endl;
945  return;
946  }
947 
948  load_map(filename, false);
949 }
950 
951 void context_manager::new_map(int width, int height, const t_translation::terrain_code& fill, bool new_context)
952 {
953  const config& default_schedule = game_config_.find_child("editor_times", "id", "empty");
954  editor_map m(game_config_, width, height, fill);
955 
956  if(new_context) {
957  int new_id = add_map_context(m, true, default_schedule);
958  switch_context(new_id);
959  } else {
960  replace_map_context(m, true, default_schedule);
961  }
962 }
963 
964 void context_manager::new_scenario(int width, int height, const t_translation::terrain_code& fill, bool new_context)
965 {
966  const config& default_schedule = game_config_.find_child("editor_times", "id", "empty");
967  editor_map m(game_config_, width, height, fill);
968 
969  if(new_context) {
970  int new_id = add_map_context(m, false, default_schedule);
971  switch_context(new_id);
972  } else {
973  replace_map_context(m, false, default_schedule);
974  }
975 
976  // Give the new scenario an initial side.
978  gui().set_team(0, true);
979  gui().set_playing_team(0);
980  gui_.init_flags();
981 }
982 
983 //
984 // Context manipulation
985 //
986 
987 template<typename... T>
988 int context_manager::add_map_context(const T&... args)
989 {
990  map_contexts_.emplace_back(new map_context(args...));
991  return map_contexts_.size() - 1;
992 }
993 
995 {
996  map_contexts_.emplace_back(std::move(mc));
997  return map_contexts_.size() - 1;
998 }
999 
1000 template<typename... T>
1002 {
1003  context_ptr new_mc(new map_context(args...));
1004  replace_map_context_with(std::move(new_mc));
1005 }
1006 
1008 {
1011 }
1012 
1014 {
1015  if(saved_windows_.empty()) {
1018 
1019  const config& default_schedule = game_config_.find_child("editor_times", "id", "empty");
1020  add_map_context(editor_map(game_config_, 44, 33, default_terrain), true, default_schedule);
1021  } else {
1022  for(const std::string& filename : saved_windows_) {
1023  add_map_context(game_config_, filename);
1024  }
1025 
1026  saved_windows_.clear();
1027  }
1028 }
1029 
1031 {
1032  if(!confirm_discard()) return;
1033 
1034  if(map_contexts_.size() == 1) {
1036  map_contexts_.erase(map_contexts_.begin());
1037  } else if(current_context_index_ == static_cast<int>(map_contexts_.size()) - 1) {
1038  map_contexts_.pop_back();
1040  } else {
1042  }
1043 
1045 }
1046 
1047 void context_manager::switch_context(const int index, const bool force)
1048 {
1049  if(index < 0 || static_cast<std::size_t>(index) >= map_contexts_.size()) {
1050  WRN_ED << "Invalid index in switch map context: " << index << std::endl;
1051  return;
1052  }
1053 
1054  if(index == current_context_index_ && !force) {
1055  return;
1056  }
1057 
1058  // Disable the labels of the current context before switching.
1059  // The refresher handles enabling the new ones.
1060  get_map_context().get_labels().enable(false);
1061 
1063 
1065 }
1066 
1068 {
1070 
1071  if(name.empty()) {
1073  }
1074 
1075  if(name.empty()){
1077  }
1078 
1079  const std::string& wm_title_string = name + " - " + game_config::get_default_title_string();
1080  CVideo::get_singleton().set_window_title(wm_title_string);
1081 }
1082 
1083 } //Namespace editor
int auto_update_transitions()
Definition: editor.cpp:24
bool write_scenario(bool display_confirmation=false)
boost::optional< int > get_xp_mod() const
Dialog was closed with the CANCEL button.
Definition: retval.hpp:37
void set_side_setup(editor_team_info &info)
TODO.
void show_message(const std::string &title, const std::string &msg, const std::string &button_caption, const bool auto_close, const bool message_use_markup, const bool title_use_markup)
Shows a message to the user.
Definition: message.cpp:152
const std::string & get_name() const
boost::optional< uint32_t > get_seed()
::tod_manager * tod_manager
Definition: resources.cpp:29
int h() const
Effective map height, in hexes.
Definition: map.hpp:128
void apply_mask_dialog()
Display an apply mask dialog and process user input.
std::size_t check_open_map(const std::string &fn) const
Check if a map is already open.
bool check_switch_open_map(const std::string &fn)
Check if a map is already open.
std::unique_ptr< map_context > context_ptr
std::size_t modified_maps(std::string &modified)
int auto_update_transitions_
Flag to rebuild terrain on every terrain change.
const t_translation::terrain_code & get_selected_bg_terrain()
std::vector< std::string > recent_files()
Retrieves the list of recently opened files.
Definition: editor.cpp:118
const terrain_code NONE_TERRAIN
Definition: translation.hpp:59
void edit_scenario_dialog()
Display a scenario edit dialog and process user input.
std::map< std::string, t_string > string_map
bool invalidate(const map_location &loc)
Function to invalidate a specific tile for redrawing.
Definition: display.cpp:3018
file_dialog & set_extension(const std::string &value)
Sets the default file extension for file names in save mode.
game_classification * classification
Definition: resources.cpp:34
file_dialog & set_path(const std::string &value)
Sets the initial file selection.
void change_display_context(const display_context *dc)
Definition: display.cpp:510
void replace_map_context_with(context_ptr &&mc)
std::vector< context_ptr > map_contexts_
The currently opened map context object.
void new_side()
Adds a new side to the map.
void reload_map()
Reload the map after it has significantly changed (when e.g.
bool save_scenario()
Saves the scenario under the current filename.
void resize_map_dialog()
Display a load map dialog and process user input.
void set_playing_team(std::size_t team)
set_playing_team sets the team whose turn it currently is
Definition: display.cpp:416
#define a
config_array_view child_range(config_key_type key) const
void save_scenario_as_dialog()
Display a save map as dialog and process user input.
void rename_area_dialog()
Display an dialog to querry a new id for an [time_area].
void refresh_after_action(bool drag_part=false)
Refresh the display after an action has been performed.
void init_flags()
Init the flag list and the team colors used by ~TC.
Definition: display.cpp:302
const std::string & get_filename() const
void show_transient_message(const std::string &title, const std::string &message, const std::string &image, const bool message_use_markup, const bool title_use_markup, const bool restore_background)
Shows a transient message to the user.
void set_area_id(int area_index, const std::string &id)
map_labels & get_labels()
void save_map()
Save the map, open dialog if not named yet.
#define LOG_ED
General purpose widgets.
std::string default_dir_
Default directory for map load/save as dialogs.
A terrain string which is converted to a terrain is a string with 1 or 2 layers the layers are separa...
Definition: translation.hpp:50
std::string get_default_title_string()
void perform_refresh(const editor_action &action, bool drag_part=false)
Perform an action on the current map_context, then refresh the display.
file_dialog & set_save_mode(bool value)
Sets the dialog&#39;s behavior on non-existent file name inputs.
static CVideo & get_singleton()
Definition: video.hpp:48
#define h
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:109
const std::vector< std::string > items
int add_map_context(const T &... args)
Add a map context.
Replace contents of the entire map, Useful as a fallback undo method when something else would be imp...
Definition: action.hpp:37
std::string str
Definition: statement.cpp:110
-file sdl_utils.hpp
bool show(const unsigned auto_close_time=0)
Shows the window.
bool confirm_discard()
Shows an are-you-sure dialog if the map was modified.
void set_needs_reload(bool value=true)
Setter for the reload flag.
map_context & get_map_context()
Get the current map context object.
void refresh_all()
Refresh everything, i.e.
int add_map_context_of(context_ptr &&mc)
map_generator * get_selected_map_generator()
void set_scenario_setup(const std::string &id, const std::string &name, const std::string &description, int turns, int xp_mod, bool victory_defeated, bool random_time)
TODO.
static bfs::path get_dir(const bfs::path &dirpath)
Definition: filesystem.cpp:278
void revert_map()
Revert the map by reloading it from disk.
const std::string & get_id() const
bool modified() const
void expand_areas_menu(std::vector< config > &items, int i)
Menu expanding for the map&#39;s defined areas.
std::string path() const
Gets the current file selection.
std::string default_dir()
Definition: editor.cpp:32
#define WRN_ED
game_classification & get_classification()
void set_window_title(const std::string &title)
Sets the title of the main window.
Definition: video.cpp:354
Object which defines a time of day with associated bonuses, image, sounds etc.
Definition: time_of_day.hpp:57
This class stores all the data for a single &#39;side&#39; (in game nomenclature).
Definition: team.hpp:44
void close_current_context()
Closes the active map context.
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:100
const std::set< map_location > & selection() const
Return the selection set.
Definition: editor_map.hpp:149
void load_map_dialog(bool force_same_context=false)
Display a load map dialog and process user input.
std::string get_user_data_dir()
Definition: filesystem.cpp:795
void reset_starting_position_labels(display &disp)
void recalculate_minimap()
Schedule the minimap for recalculation.
Definition: display.hpp:616
void select_map_generator(map_generator *mg)
filter_context * filter_con
Definition: resources.cpp:23
void set_embedded(bool v)
const game_config_view & game_config_
const std::set< map_location > changed_locations() const
void set_auto_update_transitions(int value)
Definition: editor.cpp:28
void new_scenario_dialog()
Display a new map dialog and process user input.
bool is_directory(const std::string &fname)
Returns true if the given file is a directory.
Editor action classes.
bool random_start_time() const
bool save_map()
Saves the map under the current filename.
void enable(bool is_enabled)
Definition: label.cpp:253
std::string path
Definition: game_config.cpp:39
Modify, read and display user preferences.
Shows a yes and no button.
Definition: message.hpp:79
map_display and display: classes which take care of displaying the map and game-data on the screen...
void refresh_on_context_change()
Performs the necessary housekeeping necessary when switching contexts.
bool is_pure_map() const
void create_buttons()
Definition: display.cpp:874
Manage the empty-palette in the editor.
Definition: action.cpp:29
std::string default_terrain
Definition: game_config.cpp:70
void perform_action(const editor_action &action)
Performs an action (thus modifying the map).
const char * what() const noexcept
Definition: exceptions.hpp:37
void invalidate_all()
Function to invalidate all tiles.
Definition: display.cpp:3011
bool is_active_transitions_hotkey(const std::string &item)
void switch_context(const int index, const bool force=false)
Switches the context to the one under the specified index.
void new_scenario(int width, int height, const t_translation::terrain_code &fill, bool new_context)
Create a new scenario.
Paint the same terrain on a number of locations on the map.
Definition: action.hpp:264
void new_map(int width, int height, const t_translation::terrain_code &fill, bool new_context)
Create a new map.
bool is_embedded() const
int number_of_turns() const
void load_map(const std::string &filename, bool new_context)
Load a map given the filename.
virtual std::string create_map(boost::optional< uint32_t > randomseed=boost::none)=0
Creates a new map and returns it.
std::vector< std::string > get_area_ids() const
void set_needs_terrain_rebuild(bool value=true)
Setter for the terrain rebuild flag.
Encapsulates the map of the game.
Definition: location.hpp:42
class location_palette * locs_
const std::string & get_description() const
void clear_changed_locations()
int w() const
Effective map width, in hexes.
Definition: map.hpp:125
This class adds extra editor-specific functionality to a normal gamemap.
Definition: editor_map.hpp:69
map_generator * create_map_generator(const std::string &name, const config &cfg, const config *vars)
Definition: map_create.cpp:28
std::size_t i
Definition: function.cpp:933
void add_item(const std::string &id)
void fill_selection()
Fill the selection with the foreground terrain.
void save_map_as_dialog()
Display a save map as dialog and process user input.
bool write_map(bool display_confirmation=false)
Save the map under a given filename.
Game configuration data as global variables.
Definition: build_info.cpp:55
void expand_local_time_menu(std::vector< config > &items, int i)
Menu expanding for the map&#39;s defined areas.
void rebuild_terrain(const map_location &loc)
bool save_scenario_as(const std::string &filename)
const tod_manager * get_time_manager() const
static map_location::DIRECTION s
const std::vector< time_of_day > & times(const map_location &loc=map_location::null_location()) const
editor_display & gui()
Base class for all editor actions.
Definition: action_base.hpp:40
terrain_code read_terrain_code(utils::string_view str, const ter_layer filler)
Reads a single terrain from a string.
std::string name
Definition: sdl_ttf.cpp:70
void save_all_maps(bool auto_save_windows=false)
Save all maps, open dialog if not named yet, except when using auto_save_windows which will name unna...
const std::string & get_map_data_key() const
void create_mask_to_dialog()
Display an apply mask dialog and process user input.
Declarations for File-IO.
int w
This class wraps around a map to provide a concise interface for the editor to work with...
Definition: map_context.hpp:60
#define N_(String)
Definition: gettext.hpp:108
std::size_t index(const std::string &str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:71
const std::string unicode_bullet
Definition: constants.cpp:43
#define VGETTEXT(msgid,...)
Handy wrappers around interpolate_variables_into_string and gettext.
void replace_map_context(const T &... args)
Replace the current map context and refresh accordingly.
const config & find_child(config_key_type key, const std::string &name, const std::string &value) const
std::string base_name(const std::string &file, const bool remove_extension)
Returns the base filename of a file, with directory name stripped.
context_manager(editor_display &gui, const game_config_view &game_config)
void set_team(std::size_t team, bool observe=false)
Sets the team controlled by the player using the computer.
Definition: display.cpp:397
void expand_sides_menu(std::vector< config > &items, int i)
Menu expanding for the map&#39;s player sides.
bool victory_defeated() const
static std::string get_filename(const std::string &file_code)
map_generator * last_map_generator_
int turns()
Definition: game.cpp:572
void edit_side_dialog(int side_index)
Display a side edit dialog and process user input.
double t
Definition: astarsearch.cpp:64
void reload_map()
Updates internals that cache map size.
Definition: display.cpp:504
virtual const editor_map & map() const override
Const map accessor.
lu_byte left
Definition: lparser.cpp:1026
int get_active_area() const
#define ERR_ED
void set_needs_labels_reset(bool value=true)
Setter for the labels reset flag.
std::vector< std::unique_ptr< map_generator > > map_generators_
Available random map generators.
void set_filename(const std::string &fn)
void expand_load_mru_menu(std::vector< config > &items, int i)
Menu expanding for most recent loaded list.
const std::set< map_location > & get_area_by_index(int index) const
void init_map_generators(const game_config_view &game_config)
init available random map generators
void show_error_message(const std::string &msg, bool message_use_markup)
Shows an error message to the user.
Definition: message.cpp:205
void generate_map_dialog()
Display a generate random map dialog and process user input.
void set_window_title()
Displays the specified map name in the window titlebar.
void create_default_context()
Creates a default map context object, used to ensure there is always at least one.
The dialog for selecting which random generator to use in the editor.
#define e
bool save_map_as(const std::string &filename)
Save the map under a given filename.
void expand_time_menu(std::vector< config > &items, int i)
Menu expanding for the map&#39;s defined areas.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
void load_mru_item(unsigned index, bool force_same_context=false)
Open the specified entry from the recent files list.
virtual const std::vector< team > & teams() const override
Const teams accessor.
static std::vector< std::string > saved_windows_
static const std::string get_menu_marker(const bool changed)
void expand_open_maps_menu(std::vector< config > &items, int i)
Menu expanding for open maps list.
std::string directory_name(const std::string &file)
Returns the directory name of a file, with filename stripped.
const t_string & user_team_name() const
Definition: team.hpp:298
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:384
const t_string get_default_context_name() const
void rebuild_all()
Rebuild all dynamic terrain.
Definition: display.cpp:499
auto * ss
Definition: result_set.cpp:281
void new_map_dialog()
Display a new map dialog and process user input.
file_dialog & set_title(const std::string &value)
Sets the current dialog title text.
Definition: file_dialog.hpp:56