The Battle for Wesnoth  1.19.0-dev
edit_unit.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2023 - 2024
3  by babaissarkar(Subhraman Sarkar) <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 
19 #include "gui/widgets/group.hpp"
22 #include "game_config_view.hpp"
23 
24 #include <boost/dynamic_bitset.hpp>
25 #include <SDL2/SDL.h>
26 #include <SDL2/SDL_image.h>
27 
28 namespace gui2
29 {
30 
31 namespace dialogs
32 {
33 
34 /**
35  * @ingroup GUIWindowDefinitionWML
36  *
37  * Dialog that allows user to create custom unit types.
38  */
40 {
41 public:
42  editor_edit_unit(const game_config_view& game_config, const std::string& addon_id);
43 
44  /** The execute function. See @ref modal_dialog for more information. */
46 
47  /** Write the cfg file */
48  void write();
49 
50 private:
52  const std::string& addon_id_;
53 
57  /** Used to control checkboxes, so that only specific values are overridden */
58  boost::dynamic_bitset<> res_toggles_, def_toggles_, move_toggles_;
59 
61  std::vector<config> specials_list_, abilities_list_;
62 
63  std::vector<std::pair<boost::dynamic_bitset<>, config>> attacks_;
64 
65  /** Need this because can't store macros in config */
66  std::vector<std::string> sel_abilities_, sel_specials_;
67 
68  /** Generated WML */
69  std::string generated_wml;
70 
71  /** 0 means there are no attacks. 1 is the first attack, and so on.*/
72  unsigned int selected_attack_ = 0;
73 
74  virtual void pre_show(window& window) override;
75 
76  virtual const std::string& window_id() const override;
77 
78  /** Load Unit Type data from cfg */
79  void load_unit_type();
80 
81  /** Save Unit Type data to cfg */
82  void save_unit_type();
83 
84  /** Check if width/height bigger
85  * than a specified size */
86  bool check_big(std::string img_abs_path, const int scale_size)
87  {
88  SDL_Surface * img_surf = IMG_Load(img_abs_path.c_str());
89  return (img_surf->w > scale_size) || (img_surf->h > scale_size);
90  }
91 
92  /** Write macro to a stream at specified tab level */
93  void write_macro(std::ostream& out, int level, std::string macro_name);
94 
95  /** Update wml preview */
96  void update_wml_view();
97 
98  /** Callback for loading movetype data in UI */
99  void load_movetype();
100 
101  /** Callback for resistance list */
102  void update_resistances();
103  void store_resistances();
105 
106  /** Callbacks for defense list */
107  void update_defenses();
108  void store_defenses();
109  void enable_defense_slider();
110 
111  /** Callbacks for movement list */
112  void update_movement_costs();
113  void store_movement_costs();
114  void enable_movement_slider();
115 
116  /** Callbacks for attack page */
117  void store_attack();
118  void update_attacks();
119  void add_attack();
120  void delete_attack();
121  void update_index();
122  void next_attack();
123  void prev_attack();
124  void select_attack();
125 
126  /** Callback when an tab item in the "page" listbox is selected */
127  void on_page_select();
128 
129  /** Callback for file select button */
130  void select_file(const std::string& default_dir, const std::string& id_stem);
131 
132  /** Callback for image update */
133  void update_image(const std::string& id_stem);
134 
135  /** Callback to enable/disable OK button if ID/Name is invalid */
136  void button_state_change();
137  void button_state_change_id();
138 
139  /** Utility method to check if ID contains any invalid characters */
140  bool check_id(std::string id);
141 
142  /** Utility method to set state of menu_button from a string */
143  void set_selected_from_string(menu_button& list, std::vector<config> values, std::string item) {
144  for (unsigned i = 0; i < values.size(); ++i) {
145  if(values.at(i)["label"] == item) {
146  list.set_selected(i);
147  break;
148  }
149  }
150  }
151 
152  /* signal handler for Ctrl+O shorcut */
153  void signal_handler_sdl_key_down(const event::ui_event /*event*/,
154  bool& handled,
155  const SDL_Keycode key,
156  SDL_Keymod modifier);
157 };
158 
159 
160 } // namespace dialogs
161 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
A class grating read only view to a vector of config objects, viewed as one config with all children ...
Dialog that allows user to create custom unit types.
Definition: edit_unit.hpp:40
unsigned int selected_attack_
0 means there are no attacks.
Definition: edit_unit.hpp:72
void write()
Write the cfg file.
Definition: edit_unit.cpp:1044
bool check_id(std::string id)
Utility method to check if ID contains any invalid characters.
Definition: edit_unit.cpp:1011
std::vector< config > defense_list_
Definition: edit_unit.hpp:60
boost::dynamic_bitset res_toggles_
Used to control checkboxes, so that only specific values are overridden.
Definition: edit_unit.hpp:58
const game_config_view & game_config_
Definition: edit_unit.hpp:51
std::vector< std::string > sel_abilities_
Need this because can't store macros in config.
Definition: edit_unit.hpp:66
std::vector< std::pair< boost::dynamic_bitset<>, config > > attacks_
Definition: edit_unit.hpp:63
std::vector< config > abilities_list_
Definition: edit_unit.hpp:61
void store_attack()
Callbacks for attack page.
Definition: edit_unit.cpp:649
void set_selected_from_string(menu_button &list, std::vector< config > values, std::string item)
Utility method to set state of menu_button from a string.
Definition: edit_unit.hpp:143
std::vector< config > movetype_list_
Definition: edit_unit.hpp:60
std::vector< config > race_list_
Definition: edit_unit.hpp:60
void update_defenses()
Callbacks for defense list.
Definition: edit_unit.cpp:603
std::string generated_wml
Generated WML.
Definition: edit_unit.hpp:69
void save_unit_type()
Save Unit Type data to cfg.
Definition: edit_unit.cpp:479
std::vector< config > align_list_
Definition: edit_unit.hpp:60
void button_state_change()
Callback to enable/disable OK button if ID/Name is invalid.
Definition: edit_unit.cpp:1021
bool check_big(std::string img_abs_path, const int scale_size)
Check if width/height bigger than a specified size.
Definition: edit_unit.hpp:86
void signal_handler_sdl_key_down(const event::ui_event, bool &handled, const SDL_Keycode key, SDL_Keymod modifier)
Definition: edit_unit.cpp:1062
editor_edit_unit(const game_config_view &game_config, const std::string &addon_id)
Definition: edit_unit.cpp:55
void select_file(const std::string &default_dir, const std::string &id_stem)
Callback for file select button.
Definition: edit_unit.cpp:302
void write_macro(std::ostream &out, int level, std::string macro_name)
Write macro to a stream at specified tab level.
Definition: edit_unit.cpp:852
boost::dynamic_bitset move_toggles_
Definition: edit_unit.hpp:58
void update_movement_costs()
Callbacks for movement list.
Definition: edit_unit.cpp:626
std::vector< config > resistances_list_
Definition: edit_unit.hpp:60
void on_page_select()
Callback when an tab item in the "page" listbox is selected.
Definition: edit_unit.cpp:287
void update_resistances()
Callback for resistance list.
Definition: edit_unit.cpp:580
const std::string & addon_id_
Definition: edit_unit.hpp:52
std::vector< config > specials_list_
Definition: edit_unit.hpp:61
virtual const std::string & window_id() const override
The ID of the window to build.
std::vector< std::string > sel_specials_
Definition: edit_unit.hpp:66
boost::dynamic_bitset def_toggles_
Definition: edit_unit.hpp:58
void load_unit_type()
Load Unit Type data from cfg.
Definition: edit_unit.cpp:356
void update_wml_view()
Update wml preview.
Definition: edit_unit.cpp:861
std::vector< config > usage_type_list_
Definition: edit_unit.hpp:60
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
Definition: edit_unit.cpp:79
DEFINE_SIMPLE_EXECUTE_WRAPPER(editor_edit_unit)
The execute function.
void load_movetype()
Callback for loading movetype data in UI.
Definition: edit_unit.cpp:834
void update_image(const std::string &id_stem)
Callback for image update.
Definition: edit_unit.cpp:983
Abstract base class for all modal dialogs.
A menu_button is a styled_widget to choose an element from a list of elements.
Definition: menu_button.hpp:59
void set_selected(unsigned selected, bool fire_event=true)
base class of top level items, the only item which needs to store the final canvases to draw on.
Definition: window.hpp:63
std::size_t i
Definition: function.cpp:968
Various uncategorised dialogs.
Game configuration data as global variables.
Definition: build_info.cpp:60
ui_event
The event sent to the dispatcher.
Definition: handler.hpp:115
Generic file dialog.
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:412
std::string default_dir()
Definition: editor.cpp:33
std::map< std::string, struct preproc_define > preproc_map