The Battle for Wesnoth  1.19.7+dev
edit_unit.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2023 - 2024
3  by Subhraman Sarkar (babaissarkar) <suvrax@gmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
17 
18 #include "game_config_view.hpp"
20 #include "gui/widgets/group.hpp"
21 #include "gui/widgets/combobox.hpp"
24 
25 #include <boost/dynamic_bitset.hpp>
26 
27 namespace gui2::dialogs
28 {
29 /**
30  * Dialog that allows user to create custom unit types.
31  */
33 {
34 public:
35  editor_edit_unit(const game_config_view& game_config, const std::string& addon_id);
36 
37  /** The execute function. See @ref modal_dialog for more information. */
39 
40  /** Write the cfg file */
41  void write();
42 
43 private:
45  const std::string& addon_id_;
46 
50 
51  /**
52  * Used to control checkboxes for various resistances, defences, etc.
53  * so that only specific values are overridden.
54  */
55  boost::dynamic_bitset<> res_toggles_, def_toggles_, move_toggles_;
56 
58  std::vector<config> specials_list_, abilities_list_;
59 
60  std::vector<std::pair<boost::dynamic_bitset<>, config>> attacks_;
61 
62  /** Need this because can't store macros in config */
63  std::vector<std::string> sel_abilities_, sel_specials_;
64 
65  /** Generated WML */
66  std::string generated_wml;
67 
68  /** 0 means there are no attacks. 1 is the first attack, and so on.*/
69  unsigned int selected_attack_ = 0;
70 
71  virtual void pre_show() override;
72 
73  virtual const std::string& window_id() const override;
74 
75  /** Load Unit Type data from cfg */
76  void load_unit_type();
77 
78  /** Save Unit Type data to cfg */
79  void save_unit_type();
80 
81  /** Write macro to a stream at specified tab level */
82  void write_macro(std::ostream& out, unsigned level, const std::string& macro_name);
83 
84  /** Update wml preview */
85  void update_wml_view();
86 
87  /** Callback for loading movetype data in UI */
88  void load_movetype();
89 
90  /** Callback for resistance list */
91  void update_resistances();
92  void store_resistances();
94 
95  /** Callbacks for defense list */
96  void update_defenses();
97  void store_defenses();
98  void enable_defense_slider();
99 
100  /** Callbacks for movement list */
101  void update_movement_costs();
102  void store_movement_costs();
103  void enable_movement_slider();
104 
105  /** Callbacks for attack page */
106  void store_attack();
107  void update_attacks();
108  void add_attack();
109  void delete_attack();
110  void update_index();
111  void next_attack();
112  void prev_attack();
113  void select_attack();
114 
115  /** Callback when an tab item in the "page" listbox is selected */
116  void on_page_select();
117 
118  /** Callback for file select button */
119  void select_file(const std::string& default_dir, const std::string& id_stem);
120 
121  /** Callback for image update */
122  void update_image(const std::string& id_stem);
123 
124  /** Callback to enable/disable OK button if ID/Name is invalid */
125  void button_state_change();
126 
127  /** Quit confirmation */
128  void quit_confirmation();
129 
130  /** Utility method to check if ID contains any invalid characters */
131  bool check_id(const std::string& id);
132 
133  void set_selected_from_string(menu_button& list, std::vector<config> values, std::string item) {
134  for (unsigned i = 0; i < values.size(); ++i) {
135  if(values.at(i)["label"] == item) {
136  list.set_selected(i);
137  break;
138  }
139  }
140  }
141 
142  void set_selected_from_string(combobox& list, std::vector<config> values, std::string item) {
143  for (unsigned i = 0; i < values.size(); ++i) {
144  if(values.at(i)["label"] == item) {
145  list.set_selected(i);
146  break;
147  }
148  }
149  list.set_value(item);
150  }
151 
152  /* signal handler for Ctrl+O shorcut */
154  const event::ui_event /*event*/,
155  bool& handled,
156  const SDL_Keycode key,
157  SDL_Keymod modifier);
158 };
159 
160 } // namespace gui2::dialogs
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
A class grating read only view to a vector of config objects, viewed as one config with all children ...
Class for a combobox.
Definition: combobox.hpp:37
void set_selected(unsigned selected, bool fire_event=true)
Definition: combobox.cpp:289
Dialog that allows user to create custom unit types.
Definition: edit_unit.hpp:33
unsigned int selected_attack_
0 means there are no attacks.
Definition: edit_unit.hpp:69
void write()
Write the cfg file.
Definition: edit_unit.cpp:1038
void quit_confirmation()
Quit confirmation.
Definition: edit_unit.cpp:1030
std::vector< config > defense_list_
Definition: edit_unit.hpp:57
boost::dynamic_bitset res_toggles_
Used to control checkboxes for various resistances, defences, etc.
Definition: edit_unit.hpp:55
const game_config_view & game_config_
Definition: edit_unit.hpp:44
std::vector< std::string > sel_abilities_
Need this because can't store macros in config.
Definition: edit_unit.hpp:63
std::vector< std::pair< boost::dynamic_bitset<>, config > > attacks_
Definition: edit_unit.hpp:60
std::vector< config > abilities_list_
Definition: edit_unit.hpp:58
void store_attack()
Callbacks for attack page.
Definition: edit_unit.cpp:652
void set_selected_from_string(menu_button &list, std::vector< config > values, std::string item)
Definition: edit_unit.hpp:133
std::vector< config > movetype_list_
Definition: edit_unit.hpp:57
std::vector< config > race_list_
Definition: edit_unit.hpp:57
void update_defenses()
Callbacks for defense list.
Definition: edit_unit.cpp:606
std::string generated_wml
Generated WML.
Definition: edit_unit.hpp:66
void save_unit_type()
Save Unit Type data to cfg.
Definition: edit_unit.cpp:482
std::vector< config > align_list_
Definition: edit_unit.hpp:57
void button_state_change()
Callback to enable/disable OK button if ID/Name is invalid.
Definition: edit_unit.cpp:1019
void signal_handler_sdl_key_down(const event::ui_event, bool &handled, const SDL_Keycode key, SDL_Keymod modifier)
Definition: edit_unit.cpp:1057
editor_edit_unit(const game_config_view &game_config, const std::string &addon_id)
Definition: edit_unit.cpp:59
void select_file(const std::string &default_dir, const std::string &id_stem)
Callback for file select button.
Definition: edit_unit.cpp:308
boost::dynamic_bitset move_toggles_
Definition: edit_unit.hpp:55
void update_movement_costs()
Callbacks for movement list.
Definition: edit_unit.cpp:629
std::vector< config > resistances_list_
Definition: edit_unit.hpp:57
void on_page_select()
Callback when an tab item in the "page" listbox is selected.
Definition: edit_unit.cpp:298
void update_resistances()
Callback for resistance list.
Definition: edit_unit.cpp:583
bool check_id(const std::string &id)
Utility method to check if ID contains any invalid characters.
Definition: edit_unit.cpp:1009
const std::string & addon_id_
Definition: edit_unit.hpp:45
std::vector< config > specials_list_
Definition: edit_unit.hpp:58
virtual const std::string & window_id() const override
The ID of the window to build.
void set_selected_from_string(combobox &list, std::vector< config > values, std::string item)
Definition: edit_unit.hpp:142
std::vector< std::string > sel_specials_
Definition: edit_unit.hpp:63
boost::dynamic_bitset def_toggles_
Definition: edit_unit.hpp:55
void load_unit_type()
Load Unit Type data from cfg.
Definition: edit_unit.cpp:353
virtual void pre_show() override
Actions to be taken before showing the window.
Definition: edit_unit.cpp:84
void update_wml_view()
Update wml preview.
Definition: edit_unit.cpp:856
void write_macro(std::ostream &out, unsigned level, const std::string &macro_name)
Write macro to a stream at specified tab level.
Definition: edit_unit.cpp:847
std::vector< config > usage_type_list_
Definition: edit_unit.hpp:57
DEFINE_SIMPLE_EXECUTE_WRAPPER(editor_edit_unit)
The execute function.
void load_movetype()
Callback for loading movetype data in UI.
Definition: edit_unit.cpp:823
void update_image(const std::string &id_stem)
Callback for image update.
Definition: edit_unit.cpp:979
Abstract base class for all modal dialogs.
void set_selected(unsigned selected, bool fire_event=true)
virtual void set_value(const std::string &text)
The set_value is virtual for the password_box class.
std::size_t i
Definition: function.cpp:1029
Game configuration data as global variables.
Definition: build_info.cpp:61
ui_event
The event sent to the dispatcher.
Definition: handler.hpp:115
std::map< std::string, struct preproc_define > preproc_map