File: soldiercapacitycontrol.cc

package info (click to toggle)
widelands 1%3A19%2Brepack-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 370,608 kB
  • ctags: 20,609
  • sloc: cpp: 108,404; ansic: 18,695; python: 5,155; sh: 487; xml: 460; makefile: 233
file content (126 lines) | stat: -rw-r--r-- 4,182 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * Copyright (C) 2002-2004, 2006-2010 by the Widelands Development Team
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

#include "wui/soldiercapacitycontrol.h"

#include "graphic/graphic.h"
#include "logic/map_objects/tribes/soldiercontrol.h"
#include "logic/player.h"
#include "ui_basic/button.h"
#include "ui_basic/radiobutton.h"
#include "wui/interactive_gamebase.h"

using Widelands::SoldierControl;

/**
 * Widget to control the capacity of \ref MilitaryBuilding and \ref TrainingSite
 * via \ref SoldierControl
 */
struct SoldierCapacityControl : UI::Box {
	SoldierCapacityControl(UI::Panel* parent,
	                       InteractiveGameBase& igb,
	                       Widelands::Building& building);

protected:
	void think() override;

private:
	void change_soldier_capacity(int delta);
	void click_decrease();
	void click_increase();

	InteractiveGameBase& igbase_;
	Widelands::Building& building_;

	UI::Button decrease_;
	UI::Button increase_;
	UI::Textarea value_;
};

SoldierCapacityControl::SoldierCapacityControl(UI::Panel* parent,
                                               InteractiveGameBase& igb,
                                               Widelands::Building& building)
   : Box(parent, 0, 0, Horizontal),
     igbase_(igb),
     building_(building),
     decrease_(this,
               "decrease",
               0,
               0,
               32,
               32,
               g_gr->images().get("images/ui_basic/but4.png"),
               g_gr->images().get("images/wui/buildings/menu_down_train.png"),
               _("Decrease capacity")),
     increase_(this,
               "increase",
               0,
               0,
               32,
               32,
               g_gr->images().get("images/ui_basic/but4.png"),
               g_gr->images().get("images/wui/buildings/menu_up_train.png"),
               _("Increase capacity")),
     value_(this, "199", UI::Align::kCenter) {
	decrease_.sigclicked.connect(
	   boost::bind(&SoldierCapacityControl::click_decrease, boost::ref(*this)));
	increase_.sigclicked.connect(
	   boost::bind(&SoldierCapacityControl::click_increase, boost::ref(*this)));

	add(new UI::Textarea(this, _("Capacity")), UI::Align::kHCenter);
	add(&decrease_, UI::Align::kHCenter);
	add(&value_, UI::Align::kHCenter);
	add(&increase_, UI::Align::kHCenter);

	decrease_.set_repeating(true);
	increase_.set_repeating(true);

	set_thinks(true);
}

void SoldierCapacityControl::think() {
	SoldierControl* soldiers = dynamic_cast<SoldierControl*>(&building_);
	uint32_t const capacity = soldiers->soldier_capacity();
	char buffer[sizeof("4294967295")];

	sprintf(buffer, "%2u", capacity);
	value_.set_text(buffer);

	bool const can_act = igbase_.can_act(building_.owner().player_number());
	decrease_.set_enabled(can_act && soldiers->min_soldier_capacity() < capacity);
	increase_.set_enabled(can_act && soldiers->max_soldier_capacity() > capacity);
}

void SoldierCapacityControl::change_soldier_capacity(int delta) {
	igbase_.game().send_player_change_soldier_capacity(building_, delta);
}

void SoldierCapacityControl::click_decrease() {
	change_soldier_capacity(-1);
}

void SoldierCapacityControl::click_increase() {
	change_soldier_capacity(1);
}

UI::Panel* create_soldier_capacity_control(UI::Panel& parent,
                                           InteractiveGameBase& igb,
                                           Widelands::Building& building) {
	return new SoldierCapacityControl(&parent, igb, building);
}