The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
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
17
18#include "color.hpp"
24#include "game_config.hpp"
25#include "gettext.hpp"
26#include "language.hpp"
28#include "units/unit.hpp"
29
30#include <functional>
31
32namespace gui2::dialogs
33{
34
35REGISTER_DIALOG(unit_attack)
36
37unit_attack::unit_attack(const unit_map::iterator& attacker_itor,
38 const unit_map::iterator& defender_itor,
39 std::vector<battle_context>&& weapons,
40 const int best_weapon,
41 std::vector<gui2::widget_data>& bc_widget_data_vector)
42 : modal_dialog(window_id())
43 , selected_weapon_(-1)
44 , attacker_itor_(attacker_itor)
45 , defender_itor_(defender_itor)
46 , weapons_(std::move(weapons))
47 , best_weapon_(best_weapon)
48 , bc_widget_data_vector_(bc_widget_data_vector)
49{
50}
51
53{
54 const std::size_t index = find_widget<listbox>("weapon_list").get_selected_row();
55 attack_predictions::display(weapons_[index], attacker_itor_.get_shared_ptr(), defender_itor_.get_shared_ptr());
56}
57
59{
60 connect_signal_mouse_left_click(
61 find_widget<button>("damage_calculation"),
62 std::bind(&unit_attack::damage_calc_callback, this));
63
64 find_widget<unit_preview_pane>("attacker_pane")
65 .set_display_data(*attacker_itor_);
66
67 find_widget<unit_preview_pane>("defender_pane")
68 .set_display_data(*defender_itor_);
69
71
72 listbox& weapon_list = find_widget<listbox>("weapon_list");
73 keyboard_capture(&weapon_list);
74
76 weapon_list.add_row(data);
77 }
78
79 // If these two aren't the same size, we can't use list selection incides
80 // to access to weapons list!
81 assert(weapons_.size() == weapon_list.get_item_count());
82
83 weapon_list.select_row(best_weapon_);
84}
85
87{
88 if(get_retval() == retval::OK) {
89 selected_weapon_ = find_widget<listbox>("weapon_list").get_selected_row();
90 }
91}
92
93
94} // namespace dialogs
Computes the statistics of a battle between an attacker and a defender unit.
Definition attack.hpp:183
Abstract base class for all modal dialogs.
virtual void post_show() override
Actions to be taken after the window has been shown.
int selected_weapon_
The index of the selected weapon.
unit_map::iterator defender_itor_
Iterator pointing to the defender.
int best_weapon_
The best weapon, aka the one high-lighted.
std::vector< gui2::widget_data > bc_widget_data_vector_
virtual void pre_show() override
Actions to be taken before showing the window.
unit_map::iterator attacker_itor_
Iterator pointing to the attacker.
std::vector< battle_context > weapons_
List of all battle contexts used for getting the weapons.
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:267
unsigned get_item_count() const
Returns the number of items in the listbox.
Definition listbox.cpp:153
void keyboard_capture(widget *widget)
Definition window.cpp:1197
int get_retval()
Definition window.hpp:394
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...
#define REGISTER_DIALOG(window_id)
Wrapper for REGISTER_DIALOG2.
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
STL namespace.
std::string_view data
Definition picture.cpp:188
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