Merge lp:~widelands-dev/widelands/notifications_shipwindow into lp:widelands

Proposed by GunChleoc
Status: Merged
Merged at revision: 8146
Proposed branch: lp:~widelands-dev/widelands/notifications_shipwindow
Merge into: lp:widelands
Diff against target: 627 lines (+170/-162)
10 files modified
src/economy/portdock.cc (+1/-2)
src/logic/editor_game_base.h (+1/-0)
src/logic/map_objects/tribes/ship.cc (+7/-10)
src/logic/map_objects/tribes/ship.h (+24/-23)
src/notifications/note_ids.h (+1/-0)
src/wui/CMakeLists.txt (+1/-0)
src/wui/interactive_gamebase.cc (+2/-1)
src/wui/interactive_gamebase.h (+1/-2)
src/wui/shipwindow.cc (+51/-124)
src/wui/shipwindow.h (+81/-0)
To merge this branch: bzr merge lp:~widelands-dev/widelands/notifications_shipwindow
Reviewer Review Type Date Requested Status
SirVer Approve
GunChleoc Needs Resubmitting
Review via email: mp+307048@code.launchpad.net

Commit message

Ships no longer know about their windows.
- Ships send a NoteShipWindow when window contents need to be changed / the window destroyed.
- Ship windows are now controlled by InteractiveGameBase.
- Made a bunch of functions in Ship const.

Description of the change

Another one for Build 20.

I want to remove the dependency from logic to wui, and I think the Notification system is a good way of doing that. I'd like some feedback on the design before I do more of these.

I also made a bunch of functions in Ship const while I was at it.

To post a comment you must log in.
Revision history for this message
bunnybot (widelandsofficial) wrote :

Continuous integration builds have changed state:

Travis build 1373. State: passed. Details: https://travis-ci.org/widelands/widelands/builds/163454001.
Appveyor build 1215. State: success. Details: https://ci.appveyor.com/project/widelands-dev/widelands/build/_widelands_dev_widelands_notifications_shipwindow-1215.

Revision history for this message
SirVer (sirver) wrote :

> I want to remove the dependency from logic to wui,

that is a noble and excellent goal!

> and I think the Notification system is a good way of doing that.

I designed it with this idea in mind - to decouple user interface from logic.

I think the big problem in this change is the tracking of the open ship windows - I'd try getting rid of that by subscribing again for each ship window.

review: Needs Fixing
Revision history for this message
bunnybot (widelandsofficial) wrote :

Continuous integration builds have changed state:

Travis build 1376. State: failed. Details: https://travis-ci.org/widelands/widelands/builds/163973788.
Appveyor build 1218. State: success. Details: https://ci.appveyor.com/project/widelands-dev/widelands/build/_widelands_dev_widelands_notifications_shipwindow-1218.

Revision history for this message
GunChleoc (gunchleoc) wrote :

Thanks for the review, I like this much better now. Time for the next round :)

review: Needs Resubmitting
Revision history for this message
bunnybot (widelandsofficial) wrote :

Continuous integration builds have changed state:

Travis build 1379. State: passed. Details: https://travis-ci.org/widelands/widelands/builds/164116530.
Appveyor build 1221. State: success. Details: https://ci.appveyor.com/project/widelands-dev/widelands/build/_widelands_dev_widelands_notifications_shipwindow-1221.

Revision history for this message
SirVer (sirver) wrote :

lgtm. nice separation of concerns.

review: Approve
Revision history for this message
GunChleoc (gunchleoc) wrote :

Set to WiP to give Bunnybot a break.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'src/economy/portdock.cc'
--- src/economy/portdock.cc 2016-08-04 15:49:05 +0000
+++ src/economy/portdock.cc 2016-10-18 16:46:53 +0000
@@ -337,8 +337,7 @@
337 // The expedition goods are now on the ship, so from now on it is independent from the port337 // The expedition goods are now on the ship, so from now on it is independent from the port
338 // and thus we switch the port to normal, so we could even start a new expedition,338 // and thus we switch the port to normal, so we could even start a new expedition,
339 cancel_expedition(game);339 cancel_expedition(game);
340 if (upcast(InteractiveGameBase, igb, game.get_ibase()))340 Notifications::publish(NoteShipWindow(ship.serial(), NoteShipWindow::Action::kRefresh));
341 ship.refresh_window(*igb);
342 return fleet_->update(game);341 return fleet_->update(game);
343 }342 }
344 }343 }
345344
=== modified file 'src/logic/editor_game_base.h'
--- src/logic/editor_game_base.h 2016-08-04 15:49:05 +0000
+++ src/logic/editor_game_base.h 2016-10-18 16:46:53 +0000
@@ -154,6 +154,7 @@
154 uint32_t get_gametime() const {154 uint32_t get_gametime() const {
155 return gametime_;155 return gametime_;
156 }156 }
157 // TODO(GunChleoc): Get rid.
157 InteractiveBase* get_ibase() const {158 InteractiveBase* get_ibase() const {
158 return ibase_.get();159 return ibase_.get();
159 }160 }
160161
=== modified file 'src/logic/map_objects/tribes/ship.cc'
--- src/logic/map_objects/tribes/ship.cc 2016-09-07 09:30:49 +0000
+++ src/logic/map_objects/tribes/ship.cc 2016-10-18 16:46:53 +0000
@@ -126,14 +126,13 @@
126126
127Ship::Ship(const ShipDescr& gdescr)127Ship::Ship(const ShipDescr& gdescr)
128 : Bob(gdescr),128 : Bob(gdescr),
129 window_(nullptr),
130 fleet_(nullptr),129 fleet_(nullptr),
131 economy_(nullptr),130 economy_(nullptr),
132 ship_state_(ShipStates::kTransport) {131 ship_state_(ShipStates::kTransport) {
133}132}
134133
135Ship::~Ship() {134Ship::~Ship() {
136 close_window();135 Notifications::publish(NoteShipWindow(serial(), NoteShipWindow::Action::kClose));
137}136}
138137
139PortDock* Ship::get_destination(EditorGameBase& egbase) const {138PortDock* Ship::get_destination(EditorGameBase& egbase) const {
@@ -680,8 +679,7 @@
680679
681 expedition_.reset(nullptr);680 expedition_.reset(nullptr);
682681
683 if (upcast(InteractiveGameBase, igb, game.get_ibase()))682 Notifications::publish(NoteShipWindow(serial(), NoteShipWindow::Action::kRefresh));
684 refresh_window(*igb);
685 return start_task_idle(game, descr().main_animation(), 1500);683 return start_task_idle(game, descr().main_animation(), 1500);
686 }684 }
687 }685 }
@@ -743,7 +741,7 @@
743/**741/**
744 * Find a path to the dock @p pd, returns its length, and the path optionally.742 * Find a path to the dock @p pd, returns its length, and the path optionally.
745 */743 */
746uint32_t Ship::calculate_sea_route(Game& game, PortDock& pd, Path* finalpath) {744uint32_t Ship::calculate_sea_route(Game& game, PortDock& pd, Path* finalpath) const {
747 Map& map = game.map();745 Map& map = game.map();
748 StepEvalAStar se(pd.get_warehouse()->get_position());746 StepEvalAStar se(pd.get_warehouse()->get_position());
749 se.swim_ = true;747 se.swim_ = true;
@@ -847,7 +845,7 @@
847 expedition_->island_exploration = false;845 expedition_->island_exploration = false;
848}846}
849847
850WalkingDir Ship::get_scouting_direction() {848WalkingDir Ship::get_scouting_direction() const {
851 if (expedition_ && ship_state_ == ShipStates::kExpeditionScouting &&849 if (expedition_ && ship_state_ == ShipStates::kExpeditionScouting &&
852 !expedition_->island_exploration) {850 !expedition_->island_exploration) {
853 return expedition_->scouting_direction;851 return expedition_->scouting_direction;
@@ -882,7 +880,7 @@
882 expedition_->island_exploration = true;880 expedition_->island_exploration = true;
883}881}
884882
885IslandExploreDirection Ship::get_island_explore_direction() {883IslandExploreDirection Ship::get_island_explore_direction() const {
886 if (expedition_ && ship_state_ == ShipStates::kExpeditionScouting &&884 if (expedition_ && ship_state_ == ShipStates::kExpeditionScouting &&
887 expedition_->island_exploration) {885 expedition_->island_exploration) {
888 return expedition_->island_explore_direction;886 return expedition_->island_explore_direction;
@@ -926,8 +924,7 @@
926 expedition_.reset(nullptr);924 expedition_.reset(nullptr);
927925
928 // And finally update our ship window926 // And finally update our ship window
929 if (upcast(InteractiveGameBase, igb, game.get_ibase()))927 Notifications::publish(NoteShipWindow(serial(), NoteShipWindow::Action::kRefresh));
930 refresh_window(*igb);
931}928}
932929
933/// Sinks the ship930/// Sinks the ship
@@ -936,10 +933,10 @@
936 // Running colonization has the highest priority + a sink request is only valid once933 // Running colonization has the highest priority + a sink request is only valid once
937 if (!state_is_sinkable())934 if (!state_is_sinkable())
938 return;935 return;
936 Notifications::publish(NoteShipWindow(serial(), NoteShipWindow::Action::kClose));
939 ship_state_ = ShipStates::kSinkRequest;937 ship_state_ = ShipStates::kSinkRequest;
940 // Make sure the ship is active and close possible open windows938 // Make sure the ship is active and close possible open windows
941 ship_wakeup(game);939 ship_wakeup(game);
942 close_window();
943}940}
944941
945void Ship::draw(const EditorGameBase& game, RenderTarget& dst, const Point& pos) const {942void Ship::draw(const EditorGameBase& game, RenderTarget& dst, const Point& pos) const {
946943
=== modified file 'src/logic/map_objects/tribes/ship.h'
--- src/logic/map_objects/tribes/ship.h 2016-09-07 09:30:49 +0000
+++ src/logic/map_objects/tribes/ship.h 2016-10-18 16:46:53 +0000
@@ -28,11 +28,6 @@
28#include "graphic/diranimations.h"28#include "graphic/diranimations.h"
29#include "logic/map_objects/bob.h"29#include "logic/map_objects/bob.h"
3030
31namespace UI {
32class Window;
33}
34class InteractiveGameBase;
35
36namespace Widelands {31namespace Widelands {
3732
38class Economy;33class Economy;
@@ -59,6 +54,19 @@
59 }54 }
60};55};
6156
57struct NoteShipWindow {
58 CAN_BE_SENT_AS_NOTE(NoteId::ShipWindow)
59
60 Serial serial;
61
62 enum class Action { kRefresh, kClose };
63 const Action action;
64
65 NoteShipWindow(Serial init_serial, const Action& init_action)
66 : serial(init_serial), action(init_action) {
67 }
68};
69
62class ShipDescr : public BobDescr {70class ShipDescr : public BobDescr {
63public:71public:
64 ShipDescr(const std::string& init_descname, const LuaTable& t);72 ShipDescr(const std::string& init_descname, const LuaTable& t);
@@ -119,7 +127,7 @@
119 void start_task_movetodock(Game&, PortDock&);127 void start_task_movetodock(Game&, PortDock&);
120 void start_task_expedition(Game&);128 void start_task_expedition(Game&);
121129
122 uint32_t calculate_sea_route(Game& game, PortDock& pd, Path* finalpath = nullptr);130 uint32_t calculate_sea_route(Game& game, PortDock& pd, Path* finalpath = nullptr) const;
123131
124 void log_general_info(const EditorGameBase&) override;132 void log_general_info(const EditorGameBase&) override;
125133
@@ -133,10 +141,6 @@
133 void withdraw_items(Game& game, PortDock& pd, std::vector<ShippingItem>& items);141 void withdraw_items(Game& game, PortDock& pd, std::vector<ShippingItem>& items);
134 void add_item(Game&, const ShippingItem& item);142 void add_item(Game&, const ShippingItem& item);
135143
136 void show_window(InteractiveGameBase&, bool avoid_fastclick = false);
137 void close_window();
138 void refresh_window(InteractiveGameBase&);
139
140 // A ship with task expedition can be in four states: kExpeditionWaiting, kExpeditionScouting,144 // A ship with task expedition can be in four states: kExpeditionWaiting, kExpeditionScouting,
141 // kExpeditionPortspaceFound or kExpeditionColonizing in the first states, the owning player of145 // kExpeditionPortspaceFound or kExpeditionColonizing in the first states, the owning player of
142 // this ship146 // this ship
@@ -169,42 +173,42 @@
169 };173 };
170174
171 /// \returns the current state the ship is in175 /// \returns the current state the ship is in
172 ShipStates get_ship_state() {176 ShipStates get_ship_state() const {
173 return ship_state_;177 return ship_state_;
174 }178 }
175179
176 /// \returns the current name of ship180 /// \returns the current name of ship
177 const std::string& get_shipname() {181 const std::string& get_shipname() const {
178 return shipname_;182 return shipname_;
179 }183 }
180184
181 /// \returns whether the ship is currently on an expedition185 /// \returns whether the ship is currently on an expedition
182 bool state_is_expedition() {186 bool state_is_expedition() const {
183 return (ship_state_ == ShipStates::kExpeditionScouting ||187 return (ship_state_ == ShipStates::kExpeditionScouting ||
184 ship_state_ == ShipStates::kExpeditionWaiting ||188 ship_state_ == ShipStates::kExpeditionWaiting ||
185 ship_state_ == ShipStates::kExpeditionPortspaceFound ||189 ship_state_ == ShipStates::kExpeditionPortspaceFound ||
186 ship_state_ == ShipStates::kExpeditionColonizing);190 ship_state_ == ShipStates::kExpeditionColonizing);
187 }191 }
188 /// \returns whether the ship is in transport mode192 /// \returns whether the ship is in transport mode
189 bool state_is_transport() {193 bool state_is_transport() const {
190 return (ship_state_ == ShipStates::kTransport);194 return (ship_state_ == ShipStates::kTransport);
191 }195 }
192 /// \returns whether a sink request for the ship is currently valid196 /// \returns whether a sink request for the ship is currently valid
193 bool state_is_sinkable() {197 bool state_is_sinkable() const {
194 return (ship_state_ != ShipStates::kSinkRequest &&198 return (ship_state_ != ShipStates::kSinkRequest &&
195 ship_state_ != ShipStates::kSinkAnimation &&199 ship_state_ != ShipStates::kSinkAnimation &&
196 ship_state_ != ShipStates::kExpeditionColonizing);200 ship_state_ != ShipStates::kExpeditionColonizing);
197 }201 }
198202
199 /// \returns (in expedition mode only!) whether the next field in direction \arg dir is swimmable203 /// \returns (in expedition mode only!) whether the next field in direction \arg dir is swimmable
200 bool exp_dir_swimmable(Direction dir) {204 bool exp_dir_swimmable(Direction dir) const {
201 if (!expedition_)205 if (!expedition_)
202 return false;206 return false;
203 return expedition_->swimmable[dir - 1];207 return expedition_->swimmable[dir - 1];
204 }208 }
205209
206 /// \returns whether the expedition ship is close to the coast210 /// \returns whether the expedition ship is close to the coast
207 bool exp_close_to_coast() {211 bool exp_close_to_coast() const {
208 if (!expedition_)212 if (!expedition_)
209 return false;213 return false;
210 for (uint8_t dir = FIRST_DIRECTION; dir <= LAST_DIRECTION; ++dir)214 for (uint8_t dir = FIRST_DIRECTION; dir <= LAST_DIRECTION; ++dir)
@@ -214,7 +218,7 @@
214 }218 }
215219
216 /// \returns (in expedition mode only!) the list of currently seen port build spaces220 /// \returns (in expedition mode only!) the list of currently seen port build spaces
217 const std::vector<Coords>& exp_port_spaces() {221 const std::vector<Coords>& exp_port_spaces() const {
218 return expedition_->seen_port_buildspaces;222 return expedition_->seen_port_buildspaces;
219 }223 }
220224
@@ -224,12 +228,12 @@
224228
225 // Returns integer of direction, or WalkingDir::IDLE if query invalid229 // Returns integer of direction, or WalkingDir::IDLE if query invalid
226 // Intended for LUA scripting230 // Intended for LUA scripting
227 WalkingDir get_scouting_direction();231 WalkingDir get_scouting_direction() const;
228232
229 // Returns integer of direction, or IslandExploreDirection::kNotSet233 // Returns integer of direction, or IslandExploreDirection::kNotSet
230 // if query invalid234 // if query invalid
231 // Intended for LUA scripting235 // Intended for LUA scripting
232 IslandExploreDirection get_island_explore_direction();236 IslandExploreDirection get_island_explore_direction() const;
233237
234 void exp_cancel(Game&);238 void exp_cancel(Game&);
235 void sink_ship(Game&);239 void sink_ship(Game&);
@@ -239,7 +243,6 @@
239243
240private:244private:
241 friend struct Fleet;245 friend struct Fleet;
242 friend struct ShipWindow;
243246
244 void wakeup_neighbours(Game&);247 void wakeup_neighbours(Game&);
245248
@@ -261,8 +264,6 @@
261 const std::string& description,264 const std::string& description,
262 const std::string& picture);265 const std::string& picture);
263266
264 UI::Window* window_;
265
266 Fleet* fleet_;267 Fleet* fleet_;
267 Economy* economy_;268 Economy* economy_;
268 OPtr<PortDock> lastdock_;269 OPtr<PortDock> lastdock_;
269270
=== modified file 'src/notifications/note_ids.h'
--- src/notifications/note_ids.h 2016-03-03 21:46:11 +0000
+++ src/notifications/note_ids.h 2016-10-18 16:46:53 +0000
@@ -35,6 +35,7 @@
35 ProductionSiteOutOfResources,35 ProductionSiteOutOfResources,
36 TrainingSiteSoldierTrained,36 TrainingSiteSoldierTrained,
37 ShipMessage,37 ShipMessage,
38 ShipWindow,
38 GraphicResolutionChanged,39 GraphicResolutionChanged,
39 NoteExpeditionCanceled40 NoteExpeditionCanceled
40};41};
4142
=== modified file 'src/wui/CMakeLists.txt'
--- src/wui/CMakeLists.txt 2016-04-15 17:23:00 +0000
+++ src/wui/CMakeLists.txt 2016-10-18 16:46:53 +0000
@@ -153,6 +153,7 @@
153 quicknavigation.cc153 quicknavigation.cc
154 quicknavigation.h154 quicknavigation.h
155 shipwindow.cc155 shipwindow.cc
156 shipwindow.h
156 soldiercapacitycontrol.cc157 soldiercapacitycontrol.cc
157 soldiercapacitycontrol.h158 soldiercapacitycontrol.h
158 soldierlist.cc159 soldierlist.cc
159160
=== modified file 'src/wui/interactive_gamebase.cc'
--- src/wui/interactive_gamebase.cc 2016-08-04 15:49:05 +0000
+++ src/wui/interactive_gamebase.cc 2016-10-18 16:46:53 +0000
@@ -34,6 +34,7 @@
34#include "logic/player.h"34#include "logic/player.h"
35#include "profile/profile.h"35#include "profile/profile.h"
36#include "wui/game_summary.h"36#include "wui/game_summary.h"
37#include "wui/shipwindow.h"
3738
38namespace {39namespace {
3940
@@ -158,7 +159,7 @@
158 for (Widelands::Bob* temp_ship : ships) {159 for (Widelands::Bob* temp_ship : ships) {
159 if (upcast(Widelands::Ship, ship, temp_ship)) {160 if (upcast(Widelands::Ship, ship, temp_ship)) {
160 if (can_see(ship->get_owner()->player_number())) {161 if (can_see(ship->get_owner()->player_number())) {
161 ship->show_window(*this);162 new ShipWindow(*this, *ship);
162 return true;163 return true;
163 }164 }
164 }165 }
165166
=== modified file 'src/wui/interactive_gamebase.h'
--- src/wui/interactive_gamebase.h 2016-08-04 15:49:05 +0000
+++ src/wui/interactive_gamebase.h 2016-10-18 16:46:53 +0000
@@ -31,8 +31,7 @@
3131
32class InteractiveGameBase : public InteractiveBase {32class InteractiveGameBase : public InteractiveBase {
33public:33public:
34 class GameMainMenuWindows {34 struct GameMainMenuWindows {
35 public:
36 UI::UniqueWindow::Registry loadgame;35 UI::UniqueWindow::Registry loadgame;
37 UI::UniqueWindow::Registry savegame;36 UI::UniqueWindow::Registry savegame;
38 UI::UniqueWindow::Registry readme;37 UI::UniqueWindow::Registry readme;
3938
=== modified file 'src/wui/shipwindow.cc'
--- src/wui/shipwindow.cc 2016-09-07 09:30:49 +0000
+++ src/wui/shipwindow.cc 2016-10-18 16:46:53 +0000
@@ -1,5 +1,5 @@
1/*1/*
2 * Copyright (C) 2011, 2013 by the Widelands Development Team2 * Copyright (C) 2011 - 2016 by the Widelands Development Team
3 *3 *
4 * This program is free software; you can redistribute it and/or4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License5 * modify it under the terms of the GNU General Public License
@@ -17,7 +17,7 @@
17 *17 *
18 */18 */
1919
20#include "logic/map_objects/tribes/ship.h"20#include "wui/shipwindow.h"
2121
22#include "base/macros.h"22#include "base/macros.h"
23#include "economy/portdock.h"23#include "economy/portdock.h"
@@ -29,10 +29,9 @@
29#include "ui_basic/box.h"29#include "ui_basic/box.h"
30#include "wui/actionconfirm.h"30#include "wui/actionconfirm.h"
31#include "wui/game_debug_ui.h"31#include "wui/game_debug_ui.h"
32#include "wui/interactive_gamebase.h"
33#include "wui/interactive_player.h"32#include "wui/interactive_player.h"
34#include "wui/itemwaresdisplay.h"
3533
34namespace {
36static const char pic_goto[] = "images/wui/ship/menu_ship_goto.png";35static const char pic_goto[] = "images/wui/ship/menu_ship_goto.png";
37static const char pic_destination[] = "images/wui/ship/menu_ship_destination.png";36static const char pic_destination[] = "images/wui/ship/menu_ship_destination.png";
38static const char pic_sink[] = "images/wui/ship/menu_ship_sink.png";37static const char pic_sink[] = "images/wui/ship/menu_ship_sink.png";
@@ -47,70 +46,51 @@
47static const char pic_scout_sw[] = "images/wui/ship/ship_scout_sw.png";46static const char pic_scout_sw[] = "images/wui/ship/ship_scout_sw.png";
48static const char pic_scout_se[] = "images/wui/ship/ship_scout_se.png";47static const char pic_scout_se[] = "images/wui/ship/ship_scout_se.png";
49static const char pic_construct_port[] = "images/wui/editor/fsel_editor_set_port_space.png";48static const char pic_construct_port[] = "images/wui/editor/fsel_editor_set_port_space.png";
5049} // namespace
51namespace Widelands {50
5251using namespace Widelands;
53/**52
54 * Display information about a ship.53ShipWindow::ShipWindow(InteractiveGameBase& igb, Ship& ship)
55 */54 : Window(&igb, "shipwindow", 0, 0, 0, 0, ship.get_shipname()), igbase_(igb), ship_(ship) {
56struct ShipWindow : UI::Window {55 init(false);
57 ShipWindow(InteractiveGameBase& igb, Ship& ship, const std::string& title);56 shipnotes_subscriber_ = Notifications::subscribe<Widelands::NoteShipWindow>(
58 virtual ~ShipWindow();57 [this](const Widelands::NoteShipWindow& note) {
5958 if (note.serial == ship_.serial()) {
60 void think() override;59 switch (note.action) {
6160 // The ship state has changed, e.g. expedition canceled
62 UI::Button* make_button(UI::Panel* parent,61 case Widelands::NoteShipWindow::Action::kRefresh:
63 const std::string& name,62 init(true);
64 const std::string& title,63 break;
65 const std::string& picname,64 // The ship is no more
66 boost::function<void()> callback);65 case Widelands::NoteShipWindow::Action::kClose:
6766 // Stop this from thinking to avoid segfaults
68 void act_goto();67 set_thinks(false);
69 void act_destination();68 die();
70 void act_sink();69 break;
71 void act_debug();70 default:
72 void act_cancel_expedition();71 break;
73 void act_scout_towards(WalkingDir);72 }
74 void act_construct_port();73 }
75 void act_explore_island(IslandExploreDirection);74 });
7675}
77private:76
78 InteractiveGameBase& igbase_;77void ShipWindow::init(bool avoid_fastclick) {
79 Ship& ship_;
80
81 UI::Button* btn_goto_;
82 UI::Button* btn_destination_;
83 UI::Button* btn_sink_;
84 UI::Button* btn_debug_;
85 UI::Button* btn_cancel_expedition_;
86 UI::Button* btn_explore_island_cw_;
87 UI::Button* btn_explore_island_ccw_;
88 UI::Button*
89 btn_scout_[LAST_DIRECTION]; // format: DIRECTION - 1, as 0 is normally the current location.
90 UI::Button* btn_construct_port_;
91 ItemWaresDisplay* display_;
92};
93
94ShipWindow::ShipWindow(InteractiveGameBase& igb, Ship& ship, const std::string& title)
95 : Window(&igb, "shipwindow", 0, 0, 0, 0, title), igbase_(igb), ship_(ship) {
96 assert(!ship_.window_);
97 assert(ship_.get_owner());78 assert(ship_.get_owner());
98 ship_.window_ = this;79
9980 vbox_.reset(new UI::Box(this, 0, 0, UI::Box::Vertical));
100 UI::Box* vbox = new UI::Box(this, 0, 0, UI::Box::Vertical);81
10182 display_ = new ItemWaresDisplay(vbox_.get(), *ship_.get_owner());
102 display_ = new ItemWaresDisplay(vbox, *ship.get_owner());83 display_->set_capacity(ship_.descr().get_capacity());
103 display_->set_capacity(ship.descr().get_capacity());84 vbox_->add(display_, UI::Align::kHCenter, false);
104 vbox->add(display_, UI::Align::kHCenter, false);
10585
106 // Expedition buttons86 // Expedition buttons
107 if (ship_.state_is_expedition()) {87 if (ship_.state_is_expedition()) {
108 UI::Box* exp_top = new UI::Box(vbox, 0, 0, UI::Box::Horizontal);88 UI::Box* exp_top = new UI::Box(vbox_.get(), 0, 0, UI::Box::Horizontal);
109 vbox->add(exp_top, UI::Align::kHCenter, false);89 vbox_->add(exp_top, UI::Align::kHCenter, false);
110 UI::Box* exp_mid = new UI::Box(vbox, 0, 0, UI::Box::Horizontal);90 UI::Box* exp_mid = new UI::Box(vbox_.get(), 0, 0, UI::Box::Horizontal);
111 vbox->add(exp_mid, UI::Align::kHCenter, false);91 vbox_->add(exp_mid, UI::Align::kHCenter, false);
112 UI::Box* exp_bot = new UI::Box(vbox, 0, 0, UI::Box::Horizontal);92 UI::Box* exp_bot = new UI::Box(vbox_.get(), 0, 0, UI::Box::Horizontal);
113 vbox->add(exp_bot, UI::Align::kHCenter, false);93 vbox_->add(exp_bot, UI::Align::kHCenter, false);
11494
115 btn_scout_[WALK_NW - 1] =95 btn_scout_[WALK_NW - 1] =
116 make_button(exp_top, "scnw", _("Scout towards the north west"), pic_scout_nw,96 make_button(exp_top, "scnw", _("Scout towards the north west"), pic_scout_nw,
@@ -160,8 +140,8 @@
160 }140 }
161141
162 // Bottom buttons142 // Bottom buttons
163 UI::Box* buttons = new UI::Box(vbox, 0, 0, UI::Box::Horizontal);143 UI::Box* buttons = new UI::Box(vbox_.get(), 0, 0, UI::Box::Horizontal);
164 vbox->add(buttons, UI::Align::kLeft, false);144 vbox_->add(buttons, UI::Align::kLeft, false);
165145
166 btn_goto_ = make_button(146 btn_goto_ = make_button(
167 buttons, "goto", _("Go to ship"), pic_goto, boost::bind(&ShipWindow::act_goto, this));147 buttons, "goto", _("Go to ship"), pic_goto, boost::bind(&ShipWindow::act_goto, this));
@@ -188,17 +168,13 @@
188 btn_debug_->set_enabled(true);168 btn_debug_->set_enabled(true);
189 buttons->add(btn_debug_, UI::Align::kLeft, false);169 buttons->add(btn_debug_, UI::Align::kLeft, false);
190 }170 }
191 set_center_panel(vbox);171 set_center_panel(vbox_.get());
192 set_thinks(true);172 set_thinks(true);
193
194 center_to_parent();
195 move_out_of_the_way();
196 set_fastclick_panel(btn_goto_);173 set_fastclick_panel(btn_goto_);
197}174 if (!avoid_fastclick) {
198175 move_out_of_the_way();
199ShipWindow::~ShipWindow() {176 warp_mouse_to_fastclick_panel();
200 assert(ship_.window_ == this);177 }
201 ship_.window_ = nullptr;
202}178}
203179
204void ShipWindow::think() {180void ShipWindow::think() {
@@ -335,52 +311,3 @@
335 return;311 return;
336 igbase_.game().send_player_ship_explore_island(ship_, direction);312 igbase_.game().send_player_ship_explore_island(ship_, direction);
337}313}
338
339/**
340 * Show the window for this ship as long as it is not sinking:
341 * either bring it to the front, or create it.
342 */
343void Ship::show_window(InteractiveGameBase& igb, bool avoid_fastclick) {
344 // No window, if ship is sinking
345 if (ship_state_ == ShipStates::kSinkRequest || ship_state_ == ShipStates::kSinkAnimation)
346 return;
347
348 if (window_) {
349 if (window_->is_minimal())
350 window_->restore();
351 window_->move_to_top();
352 } else {
353 const std::string& title = get_shipname();
354 new ShipWindow(igb, *this, title);
355 if (!avoid_fastclick)
356 window_->warp_mouse_to_fastclick_panel();
357 }
358}
359
360/**
361 * Close the window for this ship.
362 */
363void Ship::close_window() {
364 if (window_) {
365 delete window_;
366 window_ = nullptr;
367 }
368}
369
370/**
371 * refreshes the window of this ship - useful if some ui elements have to be removed or added
372 */
373void Ship::refresh_window(InteractiveGameBase& igb) {
374 // Only do something if there is actually a window
375 if (window_) {
376 Point window_position = window_->get_pos();
377 close_window();
378 show_window(igb, true);
379 // show window could theoretically fail if refresh_window was called at the very same moment
380 // as the ship begins to sink
381 if (window_)
382 window_->set_pos(window_position);
383 }
384}
385
386} // namespace Widelands
387314
=== added file 'src/wui/shipwindow.h'
--- src/wui/shipwindow.h 1970-01-01 00:00:00 +0000
+++ src/wui/shipwindow.h 2016-10-18 16:46:53 +0000
@@ -0,0 +1,81 @@
1/*
2 * Copyright (C) 2011 - 2016 by the Widelands Development Team
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 *
18 */
19
20#ifndef WL_WUI_SHIPWINDOW_H
21#define WL_WUI_SHIPWINDOW_H
22
23#include <memory>
24
25#include "logic/game.h"
26#include "logic/map_objects/tribes/ship.h"
27#include "logic/map_objects/walkingdir.h"
28#include "notifications/notifications.h"
29#include "ui_basic/button.h"
30#include "ui_basic/window.h"
31#include "wui/interactive_gamebase.h"
32#include "wui/itemwaresdisplay.h"
33
34/**
35 * Display information about a ship.
36 */
37class ShipWindow : public UI::Window {
38public:
39 ShipWindow(InteractiveGameBase& igb, Widelands::Ship& ship);
40
41private:
42 // Resets the vbox_ and fills it with the currently needed buttons, then positions the window.
43 void init(bool avoid_fastclick);
44
45 void think() override;
46
47 UI::Button* make_button(UI::Panel* parent,
48 const std::string& name,
49 const std::string& title,
50 const std::string& picname,
51 boost::function<void()> callback);
52
53 void act_goto();
54 void act_destination();
55 void act_sink();
56 void act_debug();
57 void act_cancel_expedition();
58 void act_scout_towards(Widelands::WalkingDir);
59 void act_construct_port();
60 void act_explore_island(Widelands::IslandExploreDirection);
61
62 InteractiveGameBase& igbase_;
63 Widelands::Ship& ship_;
64
65 std::unique_ptr<UI::Box> vbox_;
66 UI::Button* btn_goto_;
67 UI::Button* btn_destination_;
68 UI::Button* btn_sink_;
69 UI::Button* btn_debug_;
70 UI::Button* btn_cancel_expedition_;
71 UI::Button* btn_explore_island_cw_;
72 UI::Button* btn_explore_island_ccw_;
73 // format: DIRECTION - 1, as 0 is normally the current location.
74 UI::Button* btn_scout_[Widelands::LAST_DIRECTION];
75 UI::Button* btn_construct_port_;
76 ItemWaresDisplay* display_;
77 std::unique_ptr<Notifications::Subscriber<Widelands::NoteShipWindow>> shipnotes_subscriber_;
78 DISALLOW_COPY_AND_ASSIGN(ShipWindow);
79};
80
81#endif // end of include guard: WL_WUI_SHIPWINDOW_H

Subscribers

People subscribed via source and target branches

to status/vote changes: