The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
context_manager.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2017 by David White <dave@whitevine.net>
3  Part of the Battle for Wesnoth Project http://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/window.hpp"
41 
44 
45 #include "terrain/translation.hpp"
46 
47 #include <memory>
48 
49 namespace editor {
50 
51 static std::vector<std::string> saved_windows_;
52 
53 static const std::string get_menu_marker(const bool changed)
54 {
55  std::ostringstream ss;
56  ss << "[<span ";
57 
58  if(changed) {
59  ss << "color='#f00' ";
60  }
61 
62  ss << "size='large'>" << font::unicode_bullet << "</span>]";
63  return ss.str();
64 }
65 
66 /**
67  * Utility class to properly refresh the display when the map context object is replaced
68  * without duplicating code.
69  */
71 {
72 public:
74  : context_manager_(ec)
75  , refreshed_(false)
76  {
77  }
78 
80  {
81  if(!refreshed_) refresh();
82  }
83 
84  void refresh()
85  {
87 
88  // TODO register the tod_manager with the gui?
92 
94 
96 
98 
100 
101  // Enable the labels of the current context;
103 
104  refreshed_ = true;
105  }
106 
107 private:
110 };
111 
112 
114  : gui_(gui)
115  , game_config_(game_config)
116  , default_dir_(preferences::editor::default_dir())
117  , map_generators_()
118  , last_map_generator_(nullptr)
119  , current_context_index_(0)
120  , auto_update_transitions_(preferences::editor::auto_update_transitions())
121  , map_contexts_()
122  , clipboard_()
123 {
124  if(default_dir_.empty()) {
126  }
127 
129  init_map_generators(game_config);
130 }
131 
133 {
134  // Restore default window title
136 }
137 
139 {
140  gui_.rebuild_all();
144  gui_.draw(false);
147 }
148 
150 {
151  gui_.reload_map();
154  refresh_all();
155 }
156 
158 {
159  switch (auto_update_transitions_) {
161  return (item == "editor-auto-update-transitions");
163  return (item == "editor-partial-update-transitions");
165  return (item == "editor-no-update-transitions");
166  }
167 
168  return true; //should not be reached
169 }
170 
172 {
175 
177  return true;
178  }
179 
180  return false;
181 }
182 
184 {
185  std::vector<std::string> modified;
186  for(auto& mc : map_contexts_) {
187  if(mc->modified()) {
188  if(!mc->get_name().empty()) {
189  modified.push_back(mc->get_name());
190  } else if(!mc->get_filename().empty()) {
191  modified.push_back(mc->get_filename());
192  } else {
193  modified.push_back(mc->get_default_context_name());
194  }
195  }
196  }
197 
198  for(std::string& str : modified) {
199  message += "\n" + font::unicode_bullet + " " + str;
200  }
201 
202  return modified.size();
203 }
204 
205 void context_manager::load_map_dialog(bool force_same_context /* = false */)
206 {
208  if(fn.empty()) {
209  fn = default_dir_;
210  }
211 
213 
214  dlg.set_title(_("Load Map"))
215  .set_path(fn);
216 
217  if(dlg.show(gui_.video())) {
218  load_map(dlg.path(), !force_same_context);
219  }
220 }
221 
222 void context_manager::load_mru_item(unsigned int index, bool force_same_context /* = false */)
223 {
224  const std::vector<std::string>& mru = preferences::editor::recent_files();
225  if(mru.empty() || index >= mru.size()) {
226  return;
227  }
228 
229  load_map(mru[index], !force_same_context);
230 }
231 
233 {
234  team& t = get_map_context().get_teams()[side_index];
235 
236  editor_team_info team_info(t);
237 
239  get_map_context().set_side_setup(team_info);
240  }
241 }
242 
244 {
245  map_context& context = get_map_context();
246 
247  // TODO
248  //std::string fn = filesystem::directory_name(context.get_filename());
249 
250  std::string id = context.get_id();
251  std::string name = context.get_name();
252  std::string description = context.get_description();
253 
254  int turns = context.get_time_manager()->number_of_turns();
255  int xp_mod = context.get_xp_mod();
256 
257  bool victory = context.victory_defeated();
258  bool random = context.random_start_time();
259 
261  id, name, description, turns, xp_mod, victory, random, gui_.video()
262  );
263 
264  if(!ok) {
265  return;
266  }
267 
268  context.set_scenario_setup(id, name, description, turns, xp_mod, victory, random);
269 
270  if(!name.empty()) {
272  }
273 }
274 
276 {
277  int w = get_map().w();
278  int h = get_map().h();
279 
280  if(gui2::dialogs::editor_new_map::execute(_("New Map"), w, h, gui_.video())) {
282  new_map(w, h, fill, true);
283  }
284 }
285 
287 {
288  int w = get_map().w();
289  int h = get_map().h();
290 
291  if(gui2::dialogs::editor_new_map::execute(_("New Scenario"), w, h, gui_.video())) {
293  new_scenario(w, h, fill, true);
294  }
295 }
296 
297 void context_manager::expand_open_maps_menu(std::vector<config>& items, int i)
298 {
299  auto pos = items.erase(items.begin() + i);
300  std::vector<config> contexts;
301 
302  for(size_t mci = 0; mci < map_contexts_.size(); ++mci) {
303  map_context& mc = *map_contexts_[mci];
304 
305  std::string filename;
306  if(mc.is_pure_map()) {
307  filename = filesystem::base_name(mc.get_filename());
308  } else {
309  filename = mc.get_name();
310  }
311 
312  if(filename.empty()) {
313  filename = mc.get_default_context_name();
314  }
315 
316  std::ostringstream ss;
317  ss << "[" << mci + 1 << "] ";
318 
319  const bool changed = mc.modified();
320 
321  if(changed) {
322  ss << "<i>" << filename << "</i>";
323  } else {
324  ss << filename;
325  }
326 
327  if(mc.is_embedded()) {
328  ss << " (E)";
329  }
330 
331  const std::string label = ss.str();
332  const std::string details = get_menu_marker(changed);
333 
334  contexts.emplace_back(config {"label", label, "details", details});
335  }
336 
337  items.insert(pos, contexts.begin(), contexts.end());
338 }
339 
340 void context_manager::expand_load_mru_menu(std::vector<config>& items, int i)
341 {
342  std::vector<std::string> mru = preferences::editor::recent_files();
343 
344  auto pos = items.erase(items.begin() + i);
345 
346  if(mru.empty()) {
347  items.insert(pos, config {"label", _("No Recent Files")});
348  return;
349  }
350 
351  for(std::string& path : mru) {
352  // TODO: add proper leading ellipsization instead, since otherwise
353  // it'll be impossible to tell apart files with identical names and
354  // different parent paths.
356  }
357 
358  std::vector<config> temp;
359  std::transform(mru.begin(), mru.end(), std::back_inserter(temp), [](const std::string& str) {
360  return config {"label", str};
361  });
362 
363  items.insert(pos, temp.begin(), temp.end());
364 }
365 
366 void context_manager::expand_areas_menu(std::vector<config>& items, int i)
367 {
369  if(!tod) {
370  return;
371  }
372 
373  auto pos = items.erase(items.begin() + i);
374  std::vector<config> area_entries;
375 
376  std::vector<std::string> area_ids = tod->get_area_ids();
377 
378  for(size_t mci = 0; mci < area_ids.size(); ++mci) {
379  const std::string& area = area_ids[mci];
380 
381  std::stringstream ss;
382  ss << "[" << mci + 1 << "] ";\
383 
384  if(area.empty()) {
385  ss << "<i>" << _("Unnamed Area") << "</i>";
386  } else {
387  ss << area;
388  }
389 
390  const bool changed =
391  mci == static_cast<size_t>(get_map_context().get_active_area())
392  && tod->get_area_by_index(mci) != get_map_context().get_map().selection();
393 
394  const std::string label = ss.str();
395  const std::string details = get_menu_marker(changed);
396 
397  area_entries.emplace_back(config {"label", label, "details", details});
398  }
399 
400  items.insert(pos, area_entries.begin(), area_entries.end());
401 }
402 
403 void context_manager::expand_sides_menu(std::vector<config>& items, int i)
404 {
405  auto pos = items.erase(items.begin() + i);
406  std::vector<config> contexts;
407 
408  for(size_t mci = 0; mci < get_map_context().get_teams().size(); ++mci) {
409 
410  const team& t = get_map_context().get_teams()[mci];
411  const std::string& teamname = t.user_team_name();
412  std::stringstream label;
413  label << "[" << mci+1 << "] ";
414 
415  if(teamname.empty()) {
416  label << "<i>" << _("New Side") << "</i>";
417  } else {
418  label << teamname;
419  }
420 
421  contexts.emplace_back(config {"label", label.str()});
422  }
423 
424  items.insert(pos, contexts.begin(), contexts.end());
425 }
426 
427 void context_manager::expand_time_menu(std::vector<config>& items, int i)
428 {
429  auto pos = items.erase(items.begin() + i);
430  std::vector<config> times;
431 
433 
434  assert(tod_m != nullptr);
435 
436  for(const time_of_day& time : tod_m->times()) {
437  times.emplace_back(config {
438  "details", time.name, // Use 'details' field here since the image will take the first column
439  "image", time.image,
440  });
441  }
442 
443  items.insert(pos, times.begin(), times.end());
444 }
445 
446 void context_manager::expand_local_time_menu(std::vector<config>& items, int i)
447 {
448  auto pos = items.erase(items.begin() + i);
449  std::vector<config> times;
450 
452 
453  for(const time_of_day& time : tod_m->times(get_map_context().get_active_area())) {
454  times.emplace_back(config {
455  "details", time.name, // Use 'details' field here since the image will take the first column
456  "image", time.image,
457  });
458  }
459 
460  items.insert(pos, times.begin(), times.end());
461 }
462 
464 {
466  if(fn.empty()) {
467  fn = default_dir_;
468  }
469 
471 
472  dlg.set_title(_("Apply Mask"))
473  .set_path(fn);
474 
475  if(dlg.show(gui_.video())) {
476  try {
477  map_context mask(game_config_, dlg.path());
478  editor_action_apply_mask a(mask.get_map());
480  } catch (editor_map_load_exception& e) {
481  gui2::show_transient_message(gui_.video(), _("Error loading mask"), e.what());
482  return;
483  } catch (editor_action_exception& e) {
485  return;
486  }
487  }
488 }
489 
490 void context_manager::perform_refresh(const editor_action& action, bool drag_part /* =false */)
491 {
493  refresh_after_action(drag_part);
494 }
495 
497 {
498  int active_area = get_map_context().get_active_area();
500 
501  if(gui2::dialogs::edit_text::execute(N_("Rename Area"), N_("Identifier:"), name, gui_.video())) {
502  get_map_context().get_time_manager()->set_area_id(active_area, name);
503  }
504 }
505 
507 {
509  if(fn.empty()) {
510  fn = default_dir_;
511  }
512 
514 
515  dlg.set_title(_("Choose Target Map"))
516  .set_path(fn);
517 
518  if(dlg.show(gui_.video())) {
519  try {
520  map_context map(game_config_, dlg.path());
521  editor_action_create_mask a(map.get_map());
523  } catch (editor_map_load_exception& e) {
524  gui2::show_transient_message(gui_.video(), _("Error loading map"), e.what());
525  return;
526  } catch (editor_action_exception& e) {
528  return;
529  }
530  }
531 }
532 
534 {
535  if(get_map_context().needs_reload()) {
536  reload_map();
537  return;
538  }
539 
540  const std::set<map_location>& changed_locs = get_map_context().changed_locations();
541 
542  if(get_map_context().needs_terrain_rebuild()) {
545  && (!drag_part || get_map_context().everything_changed())))
546  {
547  gui_.rebuild_all();
550  } else {
551  for(const map_location& loc : changed_locs) {
552  gui_.rebuild_terrain(loc);
553  }
554  gui_.invalidate(changed_locs);
555  }
556  } else {
557  if(get_map_context().everything_changed()) {
559  } else {
560  gui_.invalidate(changed_locs);
561  }
562  }
563 
564  if(get_map_context().needs_labels_reset()) {
566  }
567 
570 }
571 
573 {
574  int w = get_map().w();
575  int h = get_map().h();
576 
578  bool copy = false;
579 
580  if(!gui2::dialogs::editor_resize_map::execute(w, h, dir, copy, gui_.video())) {
581  return;
582  }
583 
584  if(w != get_map().w() || h != get_map().h()) {
586  if(copy) {
588  }
589 
590  int x_offset = get_map().w() - w;
591  int y_offset = get_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(gui_.video())) {
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(gui_.video())) {
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"], generator_cfg));
694  }
695  }
696 }
697 
699 {
700  if(map_generators_.empty()) {
701  gui2::show_error_message(gui_.video(), _("No random map generators found."));
702  return;
703  }
704 
707  dialog.show(gui_.video());
708 
709  if(dialog.get_retval() == gui2::window::OK) {
710  std::string map_string;
712  try {
713  map_string = map_generator->create_map(dialog.get_seed());
714  } catch (mapgen_exception& e) {
715  gui2::show_transient_message(gui_.video(), _("Map creation failed."), e.what());
716  return;
717  }
718 
719  if(map_string.empty()) {
720  gui2::show_transient_message(gui_.video(), "", _("Map creation failed."));
721  } else {
722  editor_map new_map(game_config_, map_string);
723  editor_action_whole_map a(new_map);
724  get_map_context().set_needs_labels_reset(); // Ensure Player Start labels are updated together with newly generated map
725  perform_refresh(a);
726  }
727 
728  last_map_generator_ = map_generator;
729  }
730 }
731 
733 {
734  if(get_map_context().modified()) {
735  const int res = gui2::show_message(gui_.video(), _("Unsaved Changes"),
736  _("Do you want to discard all changes made to the map since the last save?"), gui2::dialogs::message::yes_no_buttons);
737  return gui2::window::CANCEL != res;
738  }
739 
740  return true;
741 }
742 
744 {
746 }
747 
748 void context_manager::save_all_maps(bool auto_save_windows)
749 {
750  int current = current_context_index_;
751  saved_windows_.clear();
752  for(size_t i = 0; i < map_contexts_.size(); ++i) {
753  switch_context(i);
755  if(auto_save_windows) {
756  if(name.empty() || filesystem::is_directory(name)) {
757  std::ostringstream s;
758  s << default_dir_ << "/" << "window_" << i + 1;
759  name = s.str();
761  }
762  }
763  saved_windows_.push_back(name);
764  save_map();
765  }
766 
767  switch_context(current);
768 }
769 
771 {
773  if(name.empty() || filesystem::is_directory(name)) {
774  if(get_map_context().is_pure_map()) {
776  } else {
778  }
779  } else {
780  if(get_map_context().is_pure_map()) {
781  write_map();
782  } else {
783  write_scenario();
784  }
785  }
786 }
787 
789 {
790  size_t is_open = check_open_map(filename);
791  if(is_open < map_contexts_.size() && is_open != static_cast<unsigned>(current_context_index_)) {
792  gui2::show_transient_message(gui_.video(), _("This scenario is already open."), filename);
793  return false;
794  }
795 
796  std::string old_filename = get_map_context().get_filename();
797  bool embedded = get_map_context().is_embedded();
798 
799  get_map_context().set_filename(filename);
800  get_map_context().set_embedded(false);
801 
802  if(!write_scenario(true)) {
803  get_map_context().set_filename(old_filename);
804  get_map_context().set_embedded(embedded);
805  return false;
806  }
807 
808  return true;
809 }
810 
812 {
813  size_t is_open = check_open_map(filename);
814  if(is_open < map_contexts_.size() && is_open != static_cast<unsigned>(current_context_index_)) {
815  gui2::show_transient_message(gui_.video(), _("This map is already open."), filename);
816  return false;
817  }
818 
819  std::string old_filename = get_map_context().get_filename();
820  bool embedded = get_map_context().is_embedded();
821 
822  get_map_context().set_filename(filename);
823  get_map_context().set_embedded(false);
824 
825  if(!write_map(true)) {
826  get_map_context().set_filename(old_filename);
827  get_map_context().set_embedded(embedded);
828  return false;
829  }
830 
831  return true;
832 }
833 
834 bool context_manager::write_scenario(bool display_confirmation)
835 {
836  try {
838  if(display_confirmation) {
839  gui2::show_transient_message(gui_.video(), "", _("Scenario saved."));
840  }
841  } catch (editor_map_save_exception& e) {
843  return false;
844  }
845 
846  return true;
847 }
848 
849 bool context_manager::write_map(bool display_confirmation)
850 {
851  try {
853  if(display_confirmation) {
854  gui2::show_transient_message(gui_.video(), "", _("Map saved."));
855  }
856  } catch (editor_map_save_exception& e) {
858  return false;
859  }
860 
861  return true;
862 }
863 
865 {
866  size_t i = 0;
867  while(i < map_contexts_.size() && map_contexts_[i]->get_filename() != fn) {
868  ++i;
869  }
870 
871  return i;
872 }
873 
875 {
876  size_t i = check_open_map(fn);
877  if(i < map_contexts_.size()) {
878  gui2::show_transient_message(gui_.video(), _("This map is already open."), fn);
879  switch_context(i);
880  return true;
881  }
882 
883  return false;
884 }
885 
886 void context_manager::load_map(const std::string& filename, bool new_context)
887 {
888  if(new_context && check_switch_open_map(filename)) {
889  return;
890  }
891 
892  LOG_ED << "Load map: " << filename << (new_context ? " (new)" : " (same)") << "\n";
893  try {
894  {
895  context_ptr mc(new map_context(game_config_, filename));
896  if(mc->get_filename() != filename) {
897  if(new_context && check_switch_open_map(mc->get_filename())) {
898  return;
899  }
900  }
901 
902  if(new_context) {
903  int new_id = add_map_context_of(std::move(mc));
904  switch_context(new_id);
905  } else {
906  replace_map_context_with(std::move(mc));
907  }
908  }
909 
910  if(get_map_context().is_embedded()) {
911  const std::string& msg = _("Loaded embedded map data");
912  gui2::show_transient_message(gui_.video(), _("Map loaded from scenario"), msg);
913  } else {
914  if(get_map_context().get_filename() != filename) {
915  if(get_map_context().get_map_data_key().empty()) {
916  ERR_ED << "Internal error, map context filename changed: "
917  << filename << " -> " << get_map_context().get_filename()
918  << " with no apparent scenario load\n";
919  } else {
920  utils::string_map symbols;
921  symbols["old"] = filename;
922  const std::string& msg = _("Loaded referenced map file:\n$new");
923  symbols["new"] = get_map_context().get_filename();
924  symbols["map_data"] = get_map_context().get_map_data_key();
925  gui2::show_transient_message(gui_.video(), _("Map loaded from scenario"),
926  //TODO: msg is already translated does vgettext make sense?
927  vgettext(msg.c_str(), symbols));
928  }
929  }
930  }
931  } catch (editor_map_load_exception& e) {
932  gui2::show_transient_message(gui_.video(), _("Error loading map"), e.what());
933  return;
934  }
935 }
936 
938 {
939  if(!confirm_discard()) {
940  return;
941  }
942 
944  if(filename.empty()) {
945  ERR_ED << "Empty filename in map revert" << std::endl;
946  return;
947  }
948 
949  load_map(filename, false);
950 }
951 
952 void context_manager::new_map(int width, int height, const t_translation::terrain_code& fill, bool new_context)
953 {
954  const config& default_schedule = game_config_.find_child("editor_times", "id", "default");
955  editor_map m(game_config_, width, height, fill);
956 
957  if(new_context) {
958  int new_id = add_map_context(m, true, default_schedule);
959  switch_context(new_id);
960  } else {
961  replace_map_context(m, true, default_schedule);
962  }
963 }
964 
965 void context_manager::new_scenario(int width, int height, const t_translation::terrain_code& fill, bool new_context)
966 {
967  const config& default_schedule = game_config_.find_child("editor_times", "id", "default");
968  editor_map m(game_config_, width, height, fill);
969 
970  if(new_context) {
971  int new_id = add_map_context(m, false, default_schedule);
972  switch_context(new_id);
973  } else {
974  replace_map_context(m, false, default_schedule);
975  }
976 
977  // Give the new scenario an initial side.
979  gui_.init_flags();
980 }
981 
982 //
983 // Context manipulation
984 //
985 
986 template<typename... T>
987 int context_manager::add_map_context(const T&... args)
988 {
989  map_contexts_.emplace_back(new map_context(args...));
990  return map_contexts_.size() - 1;
991 }
992 
994 {
995  map_contexts_.emplace_back(std::move(mc));
996  return map_contexts_.size() - 1;
997 }
998 
999 template<typename... T>
1001 {
1002  context_ptr new_mc(new map_context(args...));
1003  replace_map_context_with(std::move(new_mc));
1004 }
1005 
1007 {
1008  map_context_refresher mcr(*this);
1010 
1011  set_window_title();
1012 }
1013 
1015 {
1016  if(saved_windows_.empty()) {
1019 
1020  const config& default_schedule = game_config_.find_child("editor_times", "id", "default");
1021  add_map_context(editor_map(game_config_, 44, 33, default_terrain), true, default_schedule);
1022  } else {
1023  for(const std::string& filename : saved_windows_) {
1024  add_map_context(game_config_, filename);
1025  }
1026 
1027  saved_windows_.clear();
1028  }
1029 }
1030 
1032 {
1033  if(!confirm_discard()) return;
1034 
1035  if(map_contexts_.size() == 1) {
1037  map_contexts_.erase(map_contexts_.begin());
1038  } else if(current_context_index_ == static_cast<int>(map_contexts_.size()) - 1) {
1039  map_contexts_.pop_back();
1041  } else {
1043  }
1044 
1045  map_context_refresher(*this);
1046  set_window_title();
1047 }
1048 
1049 void context_manager::switch_context(const int index, const bool force)
1050 {
1051  if(index < 0 || static_cast<size_t>(index) >= map_contexts_.size()) {
1052  WRN_ED << "Invalid index in switch map context: " << index << std::endl;
1053  return;
1054  }
1055 
1056  if(index == current_context_index_ && !force) {
1057  return;
1058  }
1059 
1060  // Disable the labels of the current context before switching.
1061  // The refresher handles enabling the new ones.
1062  get_labels().enable(false);
1063 
1064  map_context_refresher mcr(*this);
1066 
1067  set_window_title();
1068 }
1069 
1071 {
1073 
1074  if(name.empty()) {
1076  }
1077 
1078  if(name.empty()){
1080  }
1081 
1082  const std::string& wm_title_string = name + " - " + game_config::get_default_title_string();
1083  CVideo::get_singleton().set_window_title(wm_title_string);
1084 }
1085 
1086 } //Namespace editor
int auto_update_transitions()
Definition: editor.cpp:25
bool write_scenario(bool display_confirmation=false)
void set_side_setup(editor_team_info &info)
TODO.
bool victory_defeated() const
void show_error_message(CVideo &video, const std::string &msg, bool message_use_markup)
Shows an error message to the user.
Definition: message.cpp:207
Dialog is closed with ok button.
Definition: window.hpp:111
boost::optional< uint32_t > get_seed()
::tod_manager * tod_manager
Definition: resources.cpp:30
const char * what() const NOEXCEPT
Definition: exceptions.hpp:37
void apply_mask_dialog()
Display an apply mask dialog and process user input.
std::vector< char_t > string
bool check_switch_open_map(const std::string &fn)
Check if a map is already open.
std::unique_ptr< map_context > context_ptr
size_t index(const utf8::string &str, const size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:71
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:119
const terrain_code NONE_TERRAIN
Definition: translation.hpp:64
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:2981
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:36
file_dialog & set_path(const std::string &value)
Sets the initial file selection.
void change_display_context(const display_context *dc)
Definition: display.cpp:501
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:715
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.
static bool execute(editor::editor_team_info &info, CVideo &video)
The execute function see modal_dialog for more information.
Definition: edit_side.hpp:36
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()
Init the flag list and the team colors used by ~TC.
Definition: display.cpp:282
This file contains the window object, this object is a top level container which has the event manage...
child_itors child_range(config_key_type key)
Definition: config.cpp:295
void set_area_id(int area_index, const std::string &id)
context_manager(editor_display &gui, const config &game_config)
void save_map()
Save the map, open dialog if not named yet.
bool modified() const
#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:55
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's behavior on non-existent file name inputs.
static CVideo & get_singleton()
Definition: video.hpp:50
void show_transient_message(CVideo &video, 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.
#define h
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:110
size_t check_open_map(const std::string &fn) const
Check if a map is already open.
std::string path() const
Gets the current file selection.
const std::string & get_filename() const
const std::vector< std::string > items
virtual void draw()
Draws invalidated items.
Definition: display.cpp:2432
Replace contents of the entire map, Useful as a fallback undo method when something else would be imp...
Definition: action.hpp:37
-file sdl_utils.hpp
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.
const std::string & get_description() const
int get_xp_mod() const
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::set< map_location > changed_locations() const
const editor_map & get_map() const
Get the map from the current map context object - const version.
void expand_areas_menu(std::vector< config > &items, int i)
Menu expanding for the map's defined areas.
std::string default_dir()
Definition: editor.cpp:33
#define WRN_ED
overlay_map & get_overlays()
const t_string get_default_context_name() const
game_classification & get_classification()
void set_window_title(const std::string &title)
Sets the title of the main window.
Definition: video.cpp:346
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 'side' (in game nomenclature).
Definition: team.hpp:44
int add_map_context(const T &...args)
Add a map context.
void close_current_context()
Closes the active map context.
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:89
const std::string & get_name() const
tod_manager * get_time_manager()
bool is_pure_map() const
void load_map_dialog(bool force_same_context=false)
Display a load map dialog and process user input.
const t_string & user_team_name() const
Definition: team.hpp:295
std::string get_user_data_dir()
void reset_starting_position_labels(display &disp)
void recalculate_minimap()
Schedule the minimap for recalculation.
Definition: display.hpp:594
void select_map_generator(map_generator *mg)
filter_context * filter_con
Definition: resources.cpp:23
void replace_map_context(const T &...args)
Replace the current map context and refresh accordingly.
void set_embedded(bool v)
int w() const
Effective map width.
Definition: map.hpp:90
editor_map & get_map()
Map accessor.
Definition: map_context.hpp:92
void set_auto_update_transitions(int value)
Definition: editor.cpp:29
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 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:56
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...
if(win_locale=="af") win_locale
void create_buttons()
Definition: display.cpp:896
Manage the empty-palette in the editor.
Definition: action.cpp:29
static bool execute(const std::string &title, const std::string &label, std::string &text, CVideo &video)
Executes the dialog.
Definition: edit_text.hpp:52
std::string default_terrain
Definition: game_config.cpp:86
void perform_action(const editor_action &action)
Performs an action (thus modifying the map).
void invalidate_all()
Function to invalidate all tiles.
Definition: display.cpp:2971
static bool execute(std::string &id, std::string &name, std::string &description, int &turns, int &experience_modifier, bool &victory_when_enemies_defeated, bool &random_start_time, CVideo &video)
The execute function see modal_dialog for more information.
bool is_active_transitions_hotkey(const std::string &item)
Utility class to properly refresh the display when the map context object is replaced without duplica...
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.
std::string get_dir(const std::string &dir)
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.
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.
map_generator * create_map_generator(const std::string &name, const config &cfg)
Definition: map_create.cpp:29
void set_needs_terrain_rebuild(bool value=true)
Setter for the terrain rebuild flag.
Encapsulates the map of the game.
Definition: location.hpp:40
void clear_changed_locations()
std::vector< team > & get_teams()
Get the team from the current map context object.
void show_message(CVideo &video, 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
std::vector< std::string > get_area_ids() const
This class adds extra editor-specific functionality to a normal gamemap.
Definition: editor_map.hpp:69
int h() const
Effective map height.
Definition: map.hpp:93
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.
static bool execute(int &width, int &height, EXPAND_DIRECTION &expand_direction, bool &copy_edge_terrain, CVideo &video)
The execute function see modal_dialog for more information.
Definition: resize_map.hpp:76
Game configuration data as global variables.
Definition: build_info.cpp:53
void expand_local_time_menu(std::vector< config > &items, int i)
Menu expanding for the map's defined areas.
void rebuild_terrain(const map_location &loc)
bool save_scenario_as(const std::string &filename)
unit_map & get_units()
Get the unit map from the current map context object.
static map_location::DIRECTION s
editor_display & gui()
Base class for all editor actions.
Definition: action_base.hpp:40
const std::string & get_map_data_key() const
const std::set< map_location > & get_area_by_index(int index) const
#define i
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...
size_t modified_maps(std::string &modified)
void create_mask_to_dialog()
Display an apply mask dialog and process user input.
bool show(CVideo &video, const unsigned auto_close_time=0)
Shows the window.
Declarations for File-IO.
bool random_start_time() const
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
const std::string unicode_bullet
Definition: constants.cpp:42
const std::set< map_location > & selection() const
Return the selection set.
Definition: editor_map.hpp:149
map_context_refresher(context_manager &ec)
std::string vgettext(const char *msgid, const utils::string_map &symbols)
std::string base_name(const std::string &file, const bool remove_extension=false)
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's player sides.
static std::string get_filename(const std::string &file_code)
map_generator * last_map_generator_
int turns()
Definition: game.cpp:560
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:495
#define ERR_ED
const std::vector< time_of_day > & times(const map_location &loc=map_location::null_location()) const
void set_needs_labels_reset(bool value=true)
Setter for the labels reset flag.
CVideo & video()
Gets the underlying screen object.
Definition: display.hpp:195
std::vector< std::unique_ptr< map_generator > > map_generators_
Available random map generators.
void set_filename(const std::string &fn)
int get_active_area() const
void expand_load_mru_menu(std::vector< config > &items, int i)
Menu expanding for most recent loaded list.
static const char * name(const std::vector< SDL_Joystick * > &joysticks, const size_t index)
Definition: joystick.cpp:48
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's defined areas.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:93
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.
static std::vector< std::string > saved_windows_
static const std::string get_menu_marker(const bool changed)
Dialog is closed with the cancel button.
Definition: window.hpp:112
static bool execute(const t_string &title, int &width, int &height, CVideo &video)
The execute function see modal_dialog for more information.
Definition: new_map.hpp:47
bool is_embedded() const
unit_map * units
Definition: resources.cpp:34
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.
terrain_code read_terrain_code(const std::string &str, const ter_layer filler)
Reads a single terrain from a string.
void rebuild_all()
Rebuild all dynamic terrain.
Definition: display.cpp:490
int number_of_turns() const
void replace_overlay_map(overlay_map *overlays)
Definition: display.hpp:1037
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
const std::string & get_id() const