The Battle for Wesnoth  1.15.0-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"
26 #include "gettext.hpp"
27 
28 #include "editor/action/action.hpp"
30 #include "preferences/editor.hpp"
31 
37 #include "gui/dialogs/message.hpp"
39 #include "gui/widgets/retval.hpp"
40 
43 
44 #include "terrain/translation.hpp"
45 
46 #include <memory>
47 
48 namespace editor {
49 
50 static std::vector<std::string> saved_windows_;
51 
52 static const std::string get_menu_marker(const bool changed)
53 {
54  std::ostringstream ss;
55  ss << "[<span ";
56 
57  if(changed) {
58  ss << "color='#f00' ";
59  }
60 
61  ss << "size='large'>" << font::unicode_bullet << "</span>]";
62  return ss.str();
63 }
64 
66  : locs_(nullptr)
67  , gui_(gui)
68  , game_config_(game_config)
69  , default_dir_(preferences::editor::default_dir())
70  , map_generators_()
71  , last_map_generator_(nullptr)
72  , current_context_index_(0)
73  , auto_update_transitions_(preferences::editor::auto_update_transitions())
74  , map_contexts_()
75  , clipboard_()
76 {
77  resources::filter_con = this;
78 
79  if(default_dir_.empty()) {
81  }
82 
84  init_map_generators(game_config);
85 }
86 
88 {
89  // Restore default window title
91 
92  resources::filter_con = nullptr;
93 }
94 
96 {
98 
99  // TODO register the tod_manager with the gui?
102 
103  gui().init_flags();
104 
105  reload_map();
106 
107  // Enable the labels of the current context;
109 
111 }
112 
114 {
115  gui_.rebuild_all();
118  if(locs_) {
119  for(const auto& loc : get_map_context().map().special_locations().left) {
120  locs_->add_item(loc.first);
121  }
122  if(!get_map_context().is_pure_map()) {
123  // If the scenario has more than 9 teams, add locations for them
124  // (First 9 teams are always in the list)
125  size_t n_teams = get_map_context().teams().size();
126  for(size_t i = 10; i <= n_teams; i++) {
127  locs_->add_item(std::to_string(i));
128  }
129  }
130  }
131 }
132 
134 {
135  gui_.reload_map();
138  refresh_all();
139 }
140 
142 {
143  switch (auto_update_transitions_) {
145  return (item == "editor-auto-update-transitions");
147  return (item == "editor-partial-update-transitions");
149  return (item == "editor-no-update-transitions");
150  }
151 
152  return true; //should not be reached
153 }
154 
156 {
159 
161  return true;
162  }
163 
164  return false;
165 }
166 
167 std::size_t context_manager::modified_maps(std::string& message)
168 {
169  std::vector<std::string> modified;
170  for(auto& mc : map_contexts_) {
171  if(mc->modified()) {
172  if(!mc->get_name().empty()) {
173  modified.push_back(mc->get_name());
174  } else if(!mc->get_filename().empty()) {
175  modified.push_back(mc->get_filename());
176  } else {
177  modified.push_back(mc->get_default_context_name());
178  }
179  }
180  }
181 
182  for(std::string& str : modified) {
183  message += "\n" + font::unicode_bullet + " " + str;
184  }
185 
186  return modified.size();
187 }
188 
189 void context_manager::load_map_dialog(bool force_same_context /* = false */)
190 {
192  if(fn.empty()) {
193  fn = default_dir_;
194  }
195 
197 
198  dlg.set_title(_("Load Map"))
199  .set_path(fn);
200 
201  if(dlg.show()) {
202  load_map(dlg.path(), !force_same_context);
203  }
204 }
205 
206 void context_manager::load_mru_item(unsigned int index, bool force_same_context /* = false */)
207 {
208  const std::vector<std::string>& mru = preferences::editor::recent_files();
209  if(mru.empty() || index >= mru.size()) {
210  return;
211  }
212 
213  load_map(mru[index], !force_same_context);
214 }
215 
217 {
218  team& t = get_map_context().teams()[side_index];
219 
220  editor_team_info team_info(t);
221 
222  if(gui2::dialogs::editor_edit_side::execute(team_info)) {
223  get_map_context().set_side_setup(team_info);
224  }
225 }
226 
228 {
229  map_context& context = get_map_context();
230 
231  // TODO
232  //std::string fn = filesystem::directory_name(context.get_filename());
233 
234  std::string id = context.get_id();
235  std::string name = context.get_name();
236  std::string description = context.get_description();
237 
238  int turns = context.get_time_manager()->number_of_turns();
239  int xp_mod = context.get_xp_mod();
240 
241  bool victory = context.victory_defeated();
242  bool random = context.random_start_time();
243 
244  const bool ok = gui2::dialogs::editor_edit_scenario::execute(
245  id, name, description, turns, xp_mod, victory, random
246  );
247 
248  if(!ok) {
249  return;
250  }
251 
252  context.set_scenario_setup(id, name, description, turns, xp_mod, victory, random);
253 
254  if(!name.empty()) {
256  }
257 }
258 
260 {
261  const editor_map& map = get_map_context().map();
262 
263  int w = map.w();
264  int h = map.h();
265 
266  if(gui2::dialogs::editor_new_map::execute(_("New Map"), w, h)) {
268  new_map(w, h, fill, true);
269  }
270 }
271 
273 {
274  const editor_map& map = get_map_context().map();
275 
276  int w = map.w();
277  int h = map.h();
278 
279  if(gui2::dialogs::editor_new_map::execute(_("New Scenario"), w, h)) {
281  new_scenario(w, h, fill, true);
282  }
283 }
284 
285 void context_manager::expand_open_maps_menu(std::vector<config>& items, int i)
286 {
287  auto pos = items.erase(items.begin() + i);
288  std::vector<config> contexts;
289 
290  for(std::size_t mci = 0; mci < map_contexts_.size(); ++mci) {
291  map_context& mc = *map_contexts_[mci];
292 
293  std::string filename;
294  if(mc.is_pure_map()) {
295  filename = filesystem::base_name(mc.get_filename());
296  } else {
297  filename = mc.get_name();
298  }
299 
300  if(filename.empty()) {
301  filename = mc.get_default_context_name();
302  }
303 
304  std::ostringstream ss;
305  ss << "[" << mci + 1 << "] ";
306 
307  const bool changed = mc.modified();
308 
309  if(changed) {
310  ss << "<i>" << filename << "</i>";
311  } else {
312  ss << filename;
313  }
314 
315  if(mc.is_embedded()) {
316  ss << " (E)";
317  }
318 
319  const std::string label = ss.str();
320  const std::string details = get_menu_marker(changed);
321 
322  contexts.emplace_back("label", label, "details", details);
323  }
324 
325  items.insert(pos, contexts.begin(), contexts.end());
326 }
327 
328 void context_manager::expand_load_mru_menu(std::vector<config>& items, int i)
329 {
330  std::vector<std::string> mru = preferences::editor::recent_files();
331 
332  auto pos = items.erase(items.begin() + i);
333 
334  if(mru.empty()) {
335  items.insert(pos, config {"label", _("No Recent Files")});
336  return;
337  }
338 
339  for(std::string& path : mru) {
340  // TODO: add proper leading ellipsization instead, since otherwise
341  // it'll be impossible to tell apart files with identical names and
342  // different parent paths.
344  }
345 
346  std::vector<config> temp;
347  std::transform(mru.begin(), mru.end(), std::back_inserter(temp), [](const std::string& str) {
348  return config {"label", str};
349  });
350 
351  items.insert(pos, temp.begin(), temp.end());
352 }
353 
354 void context_manager::expand_areas_menu(std::vector<config>& items, int i)
355 {
357  if(!tod) {
358  return;
359  }
360 
361  auto pos = items.erase(items.begin() + i);
362  std::vector<config> area_entries;
363 
364  std::vector<std::string> area_ids = tod->get_area_ids();
365 
366  for(std::size_t mci = 0; mci < area_ids.size(); ++mci) {
367  const std::string& area = area_ids[mci];
368 
369  std::stringstream ss;
370  ss << "[" << mci + 1 << "] ";\
371 
372  if(area.empty()) {
373  ss << "<i>" << _("Unnamed Area") << "</i>";
374  } else {
375  ss << area;
376  }
377 
378  const bool changed =
379  mci == static_cast<std::size_t>(get_map_context().get_active_area())
380  && tod->get_area_by_index(mci) != get_map_context().map().selection();
381 
382  const std::string label = ss.str();
383  const std::string details = get_menu_marker(changed);
384 
385  area_entries.emplace_back("label", label, "details", details);
386  }
387 
388  items.insert(pos, area_entries.begin(), area_entries.end());
389 }
390 
391 void context_manager::expand_sides_menu(std::vector<config>& items, int i)
392 {
393  auto pos = items.erase(items.begin() + i);
394  std::vector<config> contexts;
395 
396  for(std::size_t mci = 0; mci < get_map_context().teams().size(); ++mci) {
397 
398  const team& t = get_map_context().teams()[mci];
399  const std::string& teamname = t.user_team_name();
400  std::stringstream label;
401  label << "[" << mci+1 << "] ";
402 
403  if(teamname.empty()) {
404  label << "<i>" << _("New Side") << "</i>";
405  } else {
406  label << teamname;
407  }
408 
409  contexts.emplace_back("label", label.str());
410  }
411 
412  items.insert(pos, contexts.begin(), contexts.end());
413 }
414 
415 void context_manager::expand_time_menu(std::vector<config>& items, int i)
416 {
417  auto pos = items.erase(items.begin() + i);
418  std::vector<config> times;
419 
421 
422  assert(tod_m != nullptr);
423 
424  for(const time_of_day& time : tod_m->times()) {
425  times.emplace_back(
426  "details", time.name, // Use 'details' field here since the image will take the first column
427  "image", time.image
428  );
429  }
430 
431  items.insert(pos, times.begin(), times.end());
432 }
433 
434 void context_manager::expand_local_time_menu(std::vector<config>& items, int i)
435 {
436  auto pos = items.erase(items.begin() + i);
437  std::vector<config> times;
438 
440 
441  for(const time_of_day& time : tod_m->times(get_map_context().get_active_area())) {
442  times.emplace_back(
443  "details", time.name, // Use 'details' field here since the image will take the first column
444  "image", time.image
445  );
446  }
447 
448  items.insert(pos, times.begin(), times.end());
449 }
450 
452 {
453  std::string fn = get_map_context().get_filename();
454  if(fn.empty()) {
455  fn = default_dir_;
456  }
457 
459 
460  dlg.set_title(_("Apply Mask"))
461  .set_path(fn);
462 
463  if(dlg.show()) {
464  try {
465  map_context mask(game_config_, dlg.path());
466  editor_action_apply_mask a(mask.map());
468  } catch (const editor_map_load_exception& e) {
469  gui2::show_transient_message(_("Error loading mask"), e.what());
470  return;
471  } catch (const editor_action_exception& e) {
473  return;
474  }
475  }
476 }
477 
478 void context_manager::perform_refresh(const editor_action& action, bool drag_part /* =false */)
479 {
481  refresh_after_action(drag_part);
482 }
483 
485 {
486  int active_area = get_map_context().get_active_area();
487  std::string name = get_map_context().get_time_manager()->get_area_ids()[active_area];
488 
489  if(gui2::dialogs::edit_text::execute(N_("Rename Area"), N_("Identifier:"), name)) {
490  get_map_context().get_time_manager()->set_area_id(active_area, name);
491  }
492 }
493 
495 {
496  std::string fn = get_map_context().get_filename();
497  if(fn.empty()) {
498  fn = default_dir_;
499  }
500 
502 
503  dlg.set_title(_("Choose Target Map"))
504  .set_path(fn);
505 
506  if(dlg.show()) {
507  try {
508  map_context map(game_config_, dlg.path());
509  editor_action_create_mask a(map.map());
511  } catch (const editor_map_load_exception& e) {
512  gui2::show_transient_message(_("Error loading map"), e.what());
513  return;
514  } catch (const editor_action_exception& e) {
516  return;
517  }
518  }
519 }
520 
522 {
523  if(get_map_context().needs_reload()) {
524  reload_map();
525  return;
526  }
527 
528  const std::set<map_location>& changed_locs = get_map_context().changed_locations();
529 
530  if(get_map_context().needs_terrain_rebuild()) {
533  && (!drag_part || get_map_context().everything_changed())))
534  {
535  gui_.rebuild_all();
537  } else {
538  for(const map_location& loc : changed_locs) {
539  gui_.rebuild_terrain(loc);
540  }
541  }
542  }
543 
544  if(get_map_context().needs_labels_reset()) {
546  }
547 
549 }
550 
552 {
553  const editor_map& map = get_map_context().map();
554 
555  int w = map.w();
556  int h = map.h();
557 
559  bool copy = false;
560 
561  if(!gui2::dialogs::editor_resize_map::execute(w, h, dir, copy)) {
562  return;
563  }
564 
565  if(w != map.w() || h != map.h()) {
567  if(copy) {
569  }
570 
571  int x_offset = map.w() - w;
572  int y_offset = map.h() - h;
573 
574  switch (dir) {
578  y_offset = 0;
579  break;
583  y_offset /= 2;
584  break;
588  break;
589  default:
590  y_offset = 0;
591  WRN_ED << "Unknown resize expand direction" << std::endl;
592  break;
593  }
594 
595  switch (dir) {
599  x_offset = 0;
600  break;
604  x_offset /= 2;
605  break;
609  break;
610  default:
611  x_offset = 0;
612  break;
613  }
614 
615  editor_action_resize_map a(w, h, x_offset, y_offset, fill);
616  perform_refresh(a);
617  }
618 }
619 
621 {
622  std::string input_name = get_map_context().get_filename();
623  if(input_name.empty()) {
624  input_name = filesystem::get_dir(default_dir_ + "/maps");
625  }
626 
628 
629  dlg.set_title(_("Save Map As"))
630  .set_save_mode(true)
631  .set_path(input_name)
632  .set_extension(".map");
633 
634  if(!dlg.show()) {
635  return;
636  }
637 
638  save_map_as(dlg.path());
639 }
640 
642 {
643  std::string input_name = get_map_context().get_filename();
644  if(input_name.empty()) {
645  input_name = filesystem::get_dir(default_dir_ + "/scenarios");
646  }
647 
649 
650  dlg.set_title(_("Save Scenario As"))
651  .set_save_mode(true)
652  .set_path(input_name)
653  .set_extension(".cfg");
654 
655  if(!dlg.show()) {
656  return;
657  }
658 
659  save_scenario_as(dlg.path());
660 }
661 
663 {
664  for(const config& i : game_config.child_range("multiplayer")) {
665  if(i["map_generation"].empty() && i["scenario_generation"].empty()) {
666  continue;
667  }
668 
669  const config& generator_cfg = i.child("generator");
670  if(!generator_cfg) {
671  ERR_ED << "Scenario \"" << i["name"] << "\" with id " << i["id"]
672  << " has map_generation= but no [generator] tag" << std::endl;
673  } else {
674  map_generators_.emplace_back(create_map_generator(i["map_generation"], generator_cfg));
675  }
676  }
677 }
678 
680 {
681  if(map_generators_.empty()) {
682  gui2::show_error_message(_("No random map generators found."));
683  return;
684  }
685 
688 
689  if(dialog.show()) {
690  std::string map_string;
692  try {
693  map_string = map_generator->create_map(dialog.get_seed());
694  } catch (const mapgen_exception& e) {
695  gui2::show_transient_message(_("Map creation failed."), e.what());
696  return;
697  }
698 
699  if(map_string.empty()) {
700  gui2::show_transient_message("", _("Map creation failed."));
701  } else {
702  editor_map new_map(game_config_, map_string);
703  editor_action_whole_map a(new_map);
704  get_map_context().set_needs_labels_reset(); // Ensure Player Start labels are updated together with newly generated map
705  perform_refresh(a);
706  }
707 
708  last_map_generator_ = map_generator;
709  }
710 }
711 
713 {
714  if(get_map_context().modified()) {
715  const int res = gui2::show_message(_("Unsaved Changes"),
716  _("Do you want to discard all changes made to the map since the last save?"), gui2::dialogs::message::yes_no_buttons);
717  return gui2::retval::CANCEL != res;
718  }
719 
720  return true;
721 }
722 
724 {
726 }
727 
728 void context_manager::save_all_maps(bool auto_save_windows)
729 {
730  int current = current_context_index_;
731  saved_windows_.clear();
732  for(std::size_t i = 0; i < map_contexts_.size(); ++i) {
733  switch_context(i);
734  std::string name = get_map_context().get_filename();
735  if(auto_save_windows) {
736  if(name.empty() || filesystem::is_directory(name)) {
737  std::ostringstream s;
738  s << default_dir_ << "/" << "window_" << i + 1;
739  name = s.str();
741  }
742  }
743  saved_windows_.push_back(name);
744  save_map();
745  }
746 
747  switch_context(current);
748 }
749 
751 {
752  const std::string& name = get_map_context().get_filename();
753  if(name.empty() || filesystem::is_directory(name)) {
754  if(get_map_context().is_pure_map()) {
756  } else {
758  }
759  } else {
760  if(get_map_context().is_pure_map()) {
761  write_map();
762  } else {
763  write_scenario();
764  }
765  }
766 }
767 
768 bool context_manager::save_scenario_as(const std::string& filename)
769 {
770  std::size_t is_open = check_open_map(filename);
771  if(is_open < map_contexts_.size() && is_open != static_cast<unsigned>(current_context_index_)) {
772  gui2::show_transient_message(_("This scenario is already open."), filename);
773  return false;
774  }
775 
776  std::string old_filename = get_map_context().get_filename();
777  bool embedded = get_map_context().is_embedded();
778 
779  get_map_context().set_filename(filename);
780  get_map_context().set_embedded(false);
781 
782  if(!write_scenario(true)) {
783  get_map_context().set_filename(old_filename);
784  get_map_context().set_embedded(embedded);
785  return false;
786  }
787 
788  return true;
789 }
790 
791 bool context_manager::save_map_as(const std::string& filename)
792 {
793  std::size_t is_open = check_open_map(filename);
794  if(is_open < map_contexts_.size() && is_open != static_cast<unsigned>(current_context_index_)) {
795  gui2::show_transient_message(_("This map is already open."), filename);
796  return false;
797  }
798 
799  std::string old_filename = get_map_context().get_filename();
800  bool embedded = get_map_context().is_embedded();
801 
802  get_map_context().set_filename(filename);
803  get_map_context().set_embedded(false);
804 
805  if(!write_map(true)) {
806  get_map_context().set_filename(old_filename);
807  get_map_context().set_embedded(embedded);
808  return false;
809  }
810 
811  return true;
812 }
813 
814 bool context_manager::write_scenario(bool display_confirmation)
815 {
816  try {
818  if(display_confirmation) {
819  gui2::show_transient_message("", _("Scenario saved."));
820  }
821  } catch (const editor_map_save_exception& e) {
823  return false;
824  }
825 
826  return true;
827 }
828 
829 bool context_manager::write_map(bool display_confirmation)
830 {
831  try {
833  if(display_confirmation) {
834  gui2::show_transient_message("", _("Map saved."));
835  }
836  } catch (const editor_map_save_exception& e) {
838  return false;
839  }
840 
841  return true;
842 }
843 
844 std::size_t context_manager::check_open_map(const std::string& fn) const
845 {
846  std::size_t i = 0;
847  while(i < map_contexts_.size() && map_contexts_[i]->get_filename() != fn) {
848  ++i;
849  }
850 
851  return i;
852 }
853 
854 bool context_manager::check_switch_open_map(const std::string& fn)
855 {
856  std::size_t i = check_open_map(fn);
857  if(i < map_contexts_.size()) {
858  gui2::show_transient_message(_("This map is already open."), fn);
859  switch_context(i);
860  return true;
861  }
862 
863  return false;
864 }
865 
866 void context_manager::load_map(const std::string& filename, bool new_context)
867 {
868  if(new_context && check_switch_open_map(filename)) {
869  return;
870  }
871 
872  LOG_ED << "Load map: " << filename << (new_context ? " (new)" : " (same)") << "\n";
873  try {
874  {
875  context_ptr mc(new map_context(game_config_, filename));
876  if(mc->get_filename() != filename) {
877  if(new_context && check_switch_open_map(mc->get_filename())) {
878  return;
879  }
880  }
881 
882  if(new_context) {
883  int new_id = add_map_context_of(std::move(mc));
884  switch_context(new_id);
885  } else {
886  replace_map_context_with(std::move(mc));
887  }
888  }
889 
890  if(get_map_context().is_embedded()) {
891  const std::string& msg = _("Loaded embedded map data");
892  gui2::show_transient_message(_("Map loaded from scenario"), msg);
893  } else {
894  if(get_map_context().get_filename() != filename) {
895  if(get_map_context().get_map_data_key().empty()) {
896  ERR_ED << "Internal error, map context filename changed: "
897  << filename << " -> " << get_map_context().get_filename()
898  << " with no apparent scenario load\n";
899  } else {
900  utils::string_map symbols;
901  symbols["old"] = filename;
902  const std::string& msg = _("Loaded referenced map file:\n$new");
903  symbols["new"] = get_map_context().get_filename();
904  symbols["map_data"] = get_map_context().get_map_data_key();
905  gui2::show_transient_message(_("Map loaded from scenario"),
906  //TODO: msg is already translated does vgettext make sense?
907  VGETTEXT(msg.c_str(), symbols));
908  }
909  }
910  }
911  } catch(const editor_map_load_exception& e) {
912  gui2::show_transient_message(_("Error loading map"), e.what());
913  return;
914  }
915 }
916 
918 {
919  if(!confirm_discard()) {
920  return;
921  }
922 
923  std::string filename = get_map_context().get_filename();
924  if(filename.empty()) {
925  ERR_ED << "Empty filename in map revert" << std::endl;
926  return;
927  }
928 
929  load_map(filename, false);
930 }
931 
932 void context_manager::new_map(int width, int height, const t_translation::terrain_code& fill, bool new_context)
933 {
934  const config& default_schedule = game_config_.find_child("editor_times", "id", "default");
935  editor_map m(game_config_, width, height, fill);
936 
937  if(new_context) {
938  int new_id = add_map_context(m, true, default_schedule);
939  switch_context(new_id);
940  } else {
941  replace_map_context(m, true, default_schedule);
942  }
943 }
944 
945 void context_manager::new_scenario(int width, int height, const t_translation::terrain_code& fill, bool new_context)
946 {
947  const config& default_schedule = game_config_.find_child("editor_times", "id", "default");
948  editor_map m(game_config_, width, height, fill);
949 
950  if(new_context) {
951  int new_id = add_map_context(m, false, default_schedule);
952  switch_context(new_id);
953  } else {
954  replace_map_context(m, false, default_schedule);
955  }
956 
957  // Give the new scenario an initial side.
959  gui_.init_flags();
960 }
961 
962 //
963 // Context manipulation
964 //
965 
966 template<typename... T>
967 int context_manager::add_map_context(const T&... args)
968 {
969  map_contexts_.emplace_back(new map_context(args...));
970  return map_contexts_.size() - 1;
971 }
972 
974 {
975  map_contexts_.emplace_back(std::move(mc));
976  return map_contexts_.size() - 1;
977 }
978 
979 template<typename... T>
981 {
982  context_ptr new_mc(new map_context(args...));
983  replace_map_context_with(std::move(new_mc));
984 }
985 
987 {
990 }
991 
993 {
994  if(saved_windows_.empty()) {
997 
998  const config& default_schedule = game_config_.find_child("editor_times", "id", "default");
999  add_map_context(editor_map(game_config_, 44, 33, default_terrain), true, default_schedule);
1000  } else {
1001  for(const std::string& filename : saved_windows_) {
1002  add_map_context(game_config_, filename);
1003  }
1004 
1005  saved_windows_.clear();
1006  }
1007 }
1008 
1010 {
1011  if(!confirm_discard()) return;
1012 
1013  if(map_contexts_.size() == 1) {
1015  map_contexts_.erase(map_contexts_.begin());
1016  } else if(current_context_index_ == static_cast<int>(map_contexts_.size()) - 1) {
1017  map_contexts_.pop_back();
1019  } else {
1021  }
1022 
1024 }
1025 
1026 void context_manager::switch_context(const int index, const bool force)
1027 {
1028  if(index < 0 || static_cast<std::size_t>(index) >= map_contexts_.size()) {
1029  WRN_ED << "Invalid index in switch map context: " << index << std::endl;
1030  return;
1031  }
1032 
1033  if(index == current_context_index_ && !force) {
1034  return;
1035  }
1036 
1037  // Disable the labels of the current context before switching.
1038  // The refresher handles enabling the new ones.
1039  get_map_context().get_labels().enable(false);
1040 
1042 
1044 }
1045 
1047 {
1048  std::string name = get_map_context().get_name();
1049 
1050  if(name.empty()) {
1052  }
1053 
1054  if(name.empty()){
1056  }
1057 
1058  const std::string& wm_title_string = name + " - " + game_config::get_default_title_string();
1059  CVideo::get_singleton().set_window_title(wm_title_string);
1060 }
1061 
1062 } //Namespace editor
int auto_update_transitions()
Definition: editor.cpp:24
bool write_scenario(bool display_confirmation=false)
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:149
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.
Definition: map.hpp:116
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
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:403
void replace_map_context_with(context_ptr &&mc)
std::vector< context_ptr > map_contexts_
The currently opened map context object.
config & find_child(config_key_type key, const std::string &name, const std::string &value)
Returns the first child of tag key with a name attribute containing value.
Definition: config.cpp:789
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.
#define a
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()
Initialize the flag list for all sides.
Definition: display.cpp:176
const std::string & get_filename() const
child_itors child_range(config_key_type key)
Definition: config.cpp:366
void set_area_id(int area_index, const std::string &id)
context_manager(editor_display &gui, const config &game_config)
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:108
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
int get_xp_mod() const
-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.
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:300
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:89
const t_string name
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:749
void reset_starting_position_labels(display &disp)
void select_map_generator(map_generator *mg)
filter_context * filter_con
Definition: resources.cpp:23
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)
Shows a transient message to the user.
void set_embedded(bool v)
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.
Definition: filesystem.cpp:984
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
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
Manage the empty-palette in the editor.
Definition: action.cpp:29
std::string default_terrain
Definition: game_config.cpp:69
void perform_action(const editor_action &action)
Performs an action (thus modifying the map).
const char * what() const noexcept
Definition: exceptions.hpp:37
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:284
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
map_generator * create_map_generator(const std::string &name, const config &cfg)
Definition: map_create.cpp:28
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.
Definition: map.hpp:113
This class adds extra editor-specific functionality to a normal gamemap.
Definition: editor_map.hpp:69
static fs::path get_dir(const fs::path &dirpath)
Definition: filesystem.cpp:311
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:46
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
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:55
#define N_(String)
Definition: gettext.hpp:97
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:42
#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.
std::string base_name(const std::string &file, const bool remove_extension)
Returns the base filename of a file, with directory name stripped.
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:556
void edit_side_dialog(int side_index)
Display a side edit dialog and process user input.
double t
Definition: astarsearch.cpp:63
void reload_map()
Updates internals that cache map size.
Definition: display.cpp:398
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 show_error_message(const std::string &msg, bool message_use_markup)
Shows an error message to the user.
Definition: message.cpp:202
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 init_map_generators(const config &game_config)
init available random map generators
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)
std::string path
File path.
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:297
terrain_code read_terrain_code(const std::string &str, const ter_layer filler)
Reads a single terrain from a string.
const t_string get_default_context_name() const
void rebuild_all()
Rebuild all dynamic terrain.
Definition: display.cpp:393
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