The Battle for Wesnoth  1.19.9+dev
unit_attack.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2025
3  by Mark de Wever <koraq@xs4all.nl>
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 
18 
19 #include "color.hpp"
21 #include "gui/widgets/button.hpp"
22 #include "gui/widgets/listbox.hpp"
24 #include "gui/widgets/window.hpp"
25 #include "game_config.hpp"
26 #include "gettext.hpp"
27 #include "language.hpp"
28 #include "serialization/markup.hpp"
29 #include "units/unit.hpp"
30 
31 #include <functional>
32 
33 namespace gui2::dialogs
34 {
35 
37 
38 unit_attack::unit_attack(const unit_map::iterator& attacker_itor,
39  const unit_map::iterator& defender_itor,
40  std::vector<battle_context>&& weapons,
41  const int best_weapon,
42  std::vector<gui2::widget_data>& bc_widget_data_vector,
43  const int leadership_bonus)
44  : modal_dialog(window_id())
45  , selected_weapon_(-1)
46  , attacker_itor_(attacker_itor)
47  , defender_itor_(defender_itor)
48  , weapons_(std::move(weapons))
49  , best_weapon_(best_weapon)
50  , bc_widget_data_vector_(bc_widget_data_vector)
51  , leadership_bonus_(leadership_bonus)
52 {
53 }
54 
56 {
57  const std::size_t index = find_widget<listbox>("weapon_list").get_selected_row();
59 }
60 
62 {
64  find_widget<button>("damage_calculation"),
65  std::bind(&unit_attack::damage_calc_callback, this));
66 
67  find_widget<unit_preview_pane>("attacker_pane")
68  .set_display_data(*attacker_itor_);
69 
70  find_widget<unit_preview_pane>("defender_pane")
71  .set_display_data(*defender_itor_);
72 
73  selected_weapon_ = -1;
74 
75  listbox& weapon_list = find_widget<listbox>("weapon_list");
76  keyboard_capture(&weapon_list);
77 
79  weapon_list.add_row(data);
80  }
81 
82  // If these two aren't the same size, we can't use list selection incides
83  // to access to weapons list!
84  assert(weapons_.size() == weapon_list.get_item_count());
85 
86  weapon_list.select_row(best_weapon_);
87 }
88 
90 {
91  if(get_retval() == retval::OK) {
92  selected_weapon_ = find_widget<listbox>("weapon_list").get_selected_row();
93  }
94 }
95 
96 
97 } // namespace dialogs
Computes the statistics of a battle between an attacker and a defender unit.
Definition: attack.hpp:167
Abstract base class for all modal dialogs.
virtual void post_show() override
Actions to be taken after the window has been shown.
Definition: unit_attack.cpp:89
int selected_weapon_
The index of the selected weapon.
Definition: unit_attack.hpp:52
unit_map::iterator defender_itor_
Iterator pointing to the defender.
Definition: unit_attack.hpp:58
int best_weapon_
The best weapon, aka the one high-lighted.
Definition: unit_attack.hpp:64
std::vector< gui2::widget_data > bc_widget_data_vector_
Definition: unit_attack.hpp:66
virtual void pre_show() override
Actions to be taken before showing the window.
Definition: unit_attack.cpp:61
unit_map::iterator attacker_itor_
Iterator pointing to the attacker.
Definition: unit_attack.hpp:55
std::vector< battle_context > weapons_
List of all battle contexts used for getting the weapons.
Definition: unit_attack.hpp:61
The listbox class.
Definition: listbox.hpp:41
grid & add_row(const widget_item &item, const int index=-1)
When an item in the list is selected by the user we need to update the state.
Definition: listbox.cpp:92
bool select_row(const unsigned row, const bool select=true)
Selects a row.
Definition: listbox.cpp:280
unsigned get_item_count() const
Returns the number of items in the listbox.
Definition: listbox.cpp:159
void keyboard_capture(widget *widget)
Definition: window.cpp:1193
int get_retval()
Definition: window.hpp:402
Container associating units to locations.
Definition: map.hpp:98
This file contains the window object, this object is a top level container which has the event manage...
REGISTER_DIALOG(editor_edit_unit)
void connect_signal_mouse_left_click(dispatcher &dispatcher, const signal &signal)
Connects a signal handler for a left mouse button click.
Definition: dispatcher.cpp:177
Generic file dialog.
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:36
@ OK
Dialog was closed with the OK button.
Definition: retval.hpp:35
void unit_attack(display *disp, game_board &board, const map_location &a, const map_location &b, int damage, const attack_type &attack, const const_attack_ptr &secondary_attack, int swing, const std::string &hit_text, int drain_amount, const std::string &att_text, const std::vector< std::string > *extra_hit_sounds, bool attacking)
Make the unit on tile 'a' attack the unit on tile 'b'.
Definition: udisplay.cpp:595
std::size_t index(std::string_view str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:70
std::string::const_iterator iterator
Definition: tokenizer.hpp:25
std::string_view data
Definition: picture.cpp:178
pointer get_shared_ptr() const
This is exactly the same as operator-> but it's slightly more readable, and can replace &*iter syntax...
Definition: map.hpp:217