Symphony Of Empires
lobby.cpp
Go to the documentation of this file.
1 // Symphony of Empires
2 // Copyright (C) 2021, Symphony of Empires contributors
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (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, see <https://www.gnu.org/licenses/>.
16 //
17 // ----------------------------------------------------------------------------
18 // Name:
19 // client/interface/lobby.cpp
20 //
21 // Abstract:
22 // Does some important stuff.
23 // ----------------------------------------------------------------------------
24 
25 #include <filesystem>
26 #include "eng3d/ui/ui.hpp"
27 #include "eng3d/ui/button.hpp"
28 #include "eng3d/ui/label.hpp"
29 #include "eng3d/ui/group.hpp"
30 #include "eng3d/ui/image.hpp"
31 #include "eng3d/ui/window.hpp"
32 #include "eng3d/camera.hpp"
33 
38 #include "client/game_state.hpp"
39 #include "world.hpp"
40 #include "client/map.hpp"
41 #include "client/lua_save_util.hpp"
42 
43 using namespace Interface;
44 
45 // Start screen
47  : UI::Window(-450, 0, 450, 128),
48  gs{ _gs },
49  curr_selected_nation{ 0 }
50 {
52 
53  // Country button for selection
54  this->curr_country_btn = new UI::Button(0, 0, 250, 24, this);
55  this->curr_country_btn->set_text(translate("No country selected"));
56  this->curr_country_btn->set_tooltip(translate("Select a country by clicking on the map or pressing the buttons below"));
57  this->curr_country_btn->set_on_click([this](UI::Widget&) {
58  if(this->gs.curr_nation != nullptr) {
59  this->gs.ui_ctx.clear();
60  if(!this->gs.singleplayer) {
61  if(this->gs.host_mode) {
62  this->gs.server = std::make_unique<Server>(gs, 1836);
63  this->gs.client = std::make_unique<Client>(gs, "127.0.0.1", 1836);
64  this->gs.client->username = "Host";
65  }
66  }
67  this->gs.in_game = true;
68  this->gs.play_nation();
69  }
70  });
71 
72  // Flag with shadow
73  this->curr_country_flag_img = new UI::Image(250, 0, 128, 72, gs.tex_man.get_white(), this);
74 #ifndef E3D_HANDHELD
75  this->make_widget<UI::Image>(250, 0, this->curr_country_flag_img->width, this->curr_country_flag_img->height, "gfx/drop_shadow.png");
76 #endif
77 
78  auto& back_btn = this->make_widget<UI::Button>(0, 0, 128, 24);
79  back_btn.set_text("Back");
80  back_btn.below_of(*curr_country_btn);
81  back_btn.set_on_click([this](UI::Widget&) {
82  this->kill();
83  new Interface::MainMenu(gs);
84  });
85 
86  auto& prev_country_btn = this->make_widget<UI::Image>(0, 0, 24, 24, "gfx/arrow_left.png");
87  prev_country_btn.below_of(*curr_country_btn);
88  prev_country_btn.right_side_of(back_btn);
89  prev_country_btn.set_on_click([this](UI::Widget&) {
90  this->change_nation(this->curr_selected_nation - 1);
91  });
92 
93  auto& next_country_ibtn = this->make_widget<UI::Image>(0, 0, 24, 24, "gfx/arrow_right.png");
94  next_country_ibtn.below_of(*curr_country_btn);
95  next_country_ibtn.right_side_of(prev_country_btn);
96  next_country_ibtn.set_on_click([this](UI::Widget&) {
97  this->change_nation(this->curr_selected_nation + 1);
98  });
99 
100  auto& random_country_ibtn = this->make_widget<UI::Image>(0, 0, 24, 24, "gfx/noicon.png");
101  random_country_ibtn.set_tooltip(translate("Select a random country"));
102  random_country_ibtn.below_of(*curr_country_btn);
103  random_country_ibtn.right_side_of(next_country_ibtn);
104  random_country_ibtn.set_on_click([this](UI::Widget&) {
105  this->change_nation(rand() % this->gs.world->nations.size());
106  });
107 
108  const auto path = std::filesystem::current_path().string();
109  auto& savefiles_grp = this->make_widget<UI::Group>(0, 0, 128, gs.height);
110  savefiles_grp.below_of(random_country_ibtn);
111  savefiles_grp.flex = UI::Flex::COLUMN;
112  savefiles_grp.is_scroll = true;
113  for(const auto& entry : std::filesystem::directory_iterator(path)) {
114  if(!entry.is_directory() && entry.path().extension() == ".sc4") {
115  auto savefile_path = entry.path().lexically_relative(path).string();
116  auto& savefile_btn = savefiles_grp.make_widget<UI::Button>(0, 0, 128, 24);
117  savefile_btn.set_text(savefile_path);
118  savefile_btn.set_on_click([this, &savefile_path](UI::Widget&) {
119  LUA_util::load(this->gs, savefile_path);
120  });
121  }
122  }
123 }
124 
125 // Change nation in start screen
126 void LobbySelectView::change_nation(size_t id) {
127  size_t old_id = curr_selected_nation;
128  if(gs.world->nations.empty()) {
129  gs.ui_ctx.prompt("Error", "No nations to select");
130  return;
131  }
132 
133  if(id >= gs.world->nations.size())
134  id = 0;
135 
136  gs.curr_nation = &gs.world->nations[id];
137  if(id > old_id) {
138  while(gs.curr_nation->exists() == false) {
139  id++;
140  if(id >= gs.world->nations.size())
141  id = 0;
142  gs.curr_nation = &gs.world->nations[id];
143  }
144  } else {
145  while(gs.curr_nation->exists() == false) {
146  id--;
147  if(id >= gs.world->nations.size())
148  id = gs.world->nations.size() - 1;
149  gs.curr_nation = &gs.world->nations[id];
150  }
151  }
152 
154  this->curr_country_flag_img->current_texture = this->gs.get_nation_flag(*this->gs.curr_nation);
155  this->curr_country_btn->set_text(gs.curr_nation->name.c_str());
156  const auto& capital = gs.world->provinces[gs.curr_nation->capital_id];
157  gs.map->camera->set_pos(capital.box_area.right, capital.box_area.bottom);
158 }
UI::Context ui_ctx
Definition: state.hpp:128
Nation * curr_nation
Definition: game_state.hpp:158
std::unique_ptr< Map > map
Definition: game_state.hpp:159
std::shared_ptr< Eng3D::Texture > get_nation_flag(const Nation &nation)
Definition: game_state.cpp:84
World * world
Definition: game_state.hpp:156
LobbySelectView(GameState &gs)
Definition: lobby.cpp:46
ProvinceId capital_id
Definition: nation.hpp:136
Eng3D::StringRef name
Definition: nation.hpp:125
bool exists() const
Whetever the nation exists at all - we cannot add nations in-game so we just check if the nation "exi...
Definition: nation.hpp:107
Button widget.
Definition: button.hpp:32
void prompt(const std::string &title, const std::string &text)
Definition: ui.cpp:183
Image widget, can display pictures or effects on the screen.
Definition: image.hpp:43
The master widget all the other widgets inherit from, do not use directly instead use one of the many...
Definition: widget.hpp:176
virtual void set_on_click(std::function< void(UI::Widget &)> _on_click)
Sets the on_click function of this widget.
Definition: widget.hpp:259
UI::Origin origin
Definition: widget.hpp:318
virtual void set_tooltip(UI::Tooltip *tooltip)
Set the tooltip to be shown when this widget is hovered, overrides the previous tooltip.
Definition: widget.cpp:458
std::shared_ptr< Eng3D::Texture > current_texture
Definition: widget.hpp:327
virtual void set_text(const std::string &text)
Generates text for the widget and overrides the current text texture.
Definition: widget.cpp:445
@ UPPER_RIGHT_SCREEN
std::string translate(const std::string_view str)
Definition: string.cpp:76
void load(GameState &gs, const std::string &savefile_path)
const char * c_str() const
Definition: string.hpp:55