The Battle for Wesnoth  1.19.5+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
28 {
29 
30 namespace dialogs
31 {
32 
33 /**
34  * Dialog that allows user to create custom unit types.
35  */
37 {
38 public:
39  editor_edit_unit(const game_config_view& game_config, const std::string& addon_id);
40 
41  /** The execute function. See @ref modal_dialog for more information. */
43 
44  /** Write the cfg file */
45  void write();
46 
47 private:
49  const std::string& addon_id_;
50 
54 
55  /**
56  * Used to control checkboxes for various resistances, defences, etc.
57  * so that only specific values are overridden.
58  */
59  boost::dynamic_bitset<> res_toggles_, def_toggles_, move_toggles_;
60 
62  std::vector<config> specials_list_, abilities_list_;
63 
64  std::vector<std::pair<boost::dynamic_bitset<>, config>> attacks_;
65 
66  /** Need this because can't store macros in config */
67  std::vector<std::string> sel_abilities_, sel_specials_;
68 
69  /** Generated WML */
70  std::string generated_wml;
71 
72  /** 0 means there are no attacks. 1 is the first attack, and so on.*/
73  unsigned int selected_attack_ = 0;
74 
75  virtual void pre_show() override;
76 
77  virtual const std::string& window_id() const override;
78 
79  /** Load Unit Type data from cfg */
80  void load_unit_type();
81 
82  /** Save Unit Type data to cfg */
83  void save_unit_type();
84 
85  /** Write macro to a stream at specified tab level */
86  void write_macro(std::ostream& out, unsigned level, const std::string macro_name);
87 
88  /** Update wml preview */
89  void update_wml_view();
90 
91  /** Callback for loading movetype data in UI */
92  void load_movetype();
93 
94  /** Callback for resistance list */
95  void update_resistances();
96  void store_resistances();
98 
99  /** Callbacks for defense list */
100  void update_defenses();
101  void store_defenses();
102  void enable_defense_slider();
103 
104  /** Callbacks for movement list */
105  void update_movement_costs();
106  void store_movement_costs();
107  void enable_movement_slider();
108 
109  /** Callbacks for attack page */
110  void store_attack();
111  void update_attacks();
112  void add_attack();
113  void delete_attack();
114  void update_index();
115  void next_attack();
116  void prev_attack();
117  void select_attack();
118 
119  /** Callback when an tab item in the "page" listbox is selected */
120  void on_page_select();
121 
122  /** Callback for file select button */
123  void select_file(const std::string& default_dir, const std::string& id_stem);
124 
125  /** Callback for image update */
126  void update_image(const std::string& id_stem);
127 
128  /** Callback to enable/disable OK button if ID/Name is invalid */
129  void button_state_change();
130 
131  /** Quit confirmation */
132  void quit_confirmation();
133 
134  /** Utility method to check if ID contains any invalid characters */
135  bool check_id(std::string id);
136 
137  void set_selected_from_string(menu_button& list, std::vector<config> values, std::string item) {
138  for (unsigned i = 0; i < values.size(); ++i) {
139  if(values.at(i)["label"] == item) {
140  list.set_selected(i);
141  break;
142  }
143  }
144  }
145 
146  void set_selected_from_string(combobox& list, std::vector<config> values, std::string item) {
147  for (unsigned i = 0; i < values.size(); ++i) {
148  if(values.at(i)["label"] == item) {
149  list.set_selected(i);
150  break;
151  }
152  }
153  list.set_value(item);
154  }
155 
156  /* signal handler for Ctrl+O shorcut */
158  const event::ui_event /*event*/,
159  bool& handled,
160  const SDL_Keycode key,
161  SDL_Keymod modifier);
162 };
163 
164 
165 } // namespace dialogs
166 } // namespace gui2
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:287
Dialog that allows user to create custom unit types.
Definition: edit_unit.hpp:37
unsigned int selected_attack_
0 means there are no attacks.
Definition: edit_unit.hpp:73
void write()
Write the cfg file.
Definition: edit_unit.cpp:1038
void quit_confirmation()
Quit confirmation.
Definition: edit_unit.cpp:1030
bool check_id(std::string id)
Utility method to check if ID contains any invalid characters.
Definition: edit_unit.cpp:1009
std::vector< config > defense_list_
Definition: edit_unit.hpp:61
boost::dynamic_bitset res_toggles_
Used to control checkboxes for various resistances, defences, etc.
Definition: edit_unit.hpp:59
const game_config_view & game_config_
Definition: edit_unit.hpp:48
std::vector< std::string > sel_abilities_
Need this because can't store macros in config.
Definition: edit_unit.hpp:67
std::vector< std::pair< boost::dynamic_bitset<>, config > > attacks_
Definition: edit_unit.hpp:64
std::vector< config > abilities_list_
Definition: edit_unit.hpp:62
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:137
std::vector< config > movetype_list_
Definition: edit_unit.hpp:61
std::vector< config > race_list_
Definition: edit_unit.hpp:61
void update_defenses()
Callbacks for defense list.
Definition: edit_unit.cpp:606
std::string generated_wml
Generated WML.
Definition: edit_unit.hpp:70
void save_unit_type()
Save Unit Type data to cfg.
Definition: edit_unit.cpp:482
std::vector< config > align_list_
Definition: edit_unit.hpp:61
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:59
void update_movement_costs()
Callbacks for movement list.
Definition: edit_unit.cpp:629
std::vector< config > resistances_list_
Definition: edit_unit.hpp:61
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
const std::string & addon_id_
Definition: edit_unit.hpp:49
std::vector< config > specials_list_
Definition: edit_unit.hpp:62
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:146
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< std::string > sel_specials_
Definition: edit_unit.hpp:67
boost::dynamic_bitset def_toggles_
Definition: edit_unit.hpp:59
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
std::vector< config > usage_type_list_
Definition: edit_unit.hpp:61
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:1028
Various uncategorised dialogs.
Game configuration data as global variables.
Definition: build_info.cpp:61
ui_event
The event sent to the dispatcher.
Definition: handler.hpp:115
Generic file dialog.
std::map< std::string, struct preproc_define > preproc_map