Peoplemon  0.1.0
Peoplemon 3 game source documentation
Towns.cpp
Go to the documentation of this file.
2 
3 #include <Core/Properties.hpp>
4 #include <Core/Resources.hpp>
7 
8 namespace editor
9 {
10 namespace page
11 {
12 namespace
13 {
14 constexpr std::uint16_t NoSpawn = std::numeric_limits<std::uint16_t>::max();
15 constexpr float FlymapScale = 0.5f;
16 } // namespace
17 using namespace bl::gui;
18 
19 Towns::Towns(bl::engine::Engine& engine, MapArea& ma)
20 : engine(engine)
21 , mapArea(ma)
22 , map(ma.editMap())
23 , active(0)
24 , playlistWindow(std::bind(&Towns::onPlaylistPick, this, std::placeholders::_1),
25  std::bind(&Towns::takeFocus, this)) {
26  content = Box::create(LinePacker::create(LinePacker::Vertical, 6.f));
27 
28  Box::Ptr row = Box::create(LinePacker::create(LinePacker::Horizontal, 4.f));
29  noTownBut = RadioButton::create("No town", "none");
30  noTownBut->getSignal(Event::LeftClicked).willAlwaysCall([this](const Event&, Element*) {
31  active = 0;
32  });
33  row->pack(noTownBut, false, true);
34 
35  Button::Ptr but = Button::create("New Town");
36  but->getSignal(Event::LeftClicked).willAlwaysCall(std::bind(&Towns::newTown, this));
37  row->pack(but, false, true);
38  content->pack(row, true, false);
39 
40  scrollRegion = ScrollArea::create(LinePacker::create(LinePacker::Vertical, 4.f));
41  scrollRegion->setColor(sf::Color::Transparent, sf::Color::Black);
42  scrollRegion->setOutlineThickness(1.5f);
43  content->pack(scrollRegion, true, true);
44 
45  window = Window::create(LinePacker::create(LinePacker::Vertical, 4.f), "Edit Town");
46  window->getSignal(Event::Closed).willAlwaysCall(std::bind(&Towns::closeWindow, this));
47 
48  row = Box::create(LinePacker::create(LinePacker::Horizontal, 4.f));
49  nameEntry = TextEntry::create();
50  nameEntry->setRequisition({100.f, 15.f});
51  row->pack(Label::create("Name:"));
52  row->pack(nameEntry, false, false);
53  window->pack(row, true, false);
54 
55  row = Box::create(LinePacker::create(LinePacker::Horizontal, 4.f));
56  descEntry = TextEntry::create();
57  descEntry->setRequisition({200.f, 15.f});
58  row->pack(Label::create("Description:"));
59  row->pack(descEntry, true, false);
60  window->pack(row, true, false);
61 
62  playlistLabel = Label::create("");
63  playlistLabel->setColor(sf::Color(20, 230, 255), sf::Color::Transparent);
64  row = Box::create(LinePacker::create(LinePacker::Horizontal, 4.f));
65  but = Button::create("Pick Playlist");
66  but->getSignal(Event::LeftClicked).willAlwaysCall([this](const Event&, Element*) {
67  window->setForceFocus(false);
68  playlistWindow.open(gui, playlistLabel->getText());
69  });
70  row->pack(but, false, true);
71  row->pack(playlistLabel, true, false);
72  window->pack(row, true, false);
73 
74  row = Box::create(LinePacker::create(LinePacker::Horizontal, 4.f));
75  weatherSelect = component::WeatherSelect::create();
76  row->pack(Label::create("Weather:"));
77  row->pack(weatherSelect);
78  window->pack(row, true, false);
79 
80  row = Box::create(LinePacker::create(LinePacker::Horizontal, 4.f));
81  row->pack(Label::create("PC Spawn:"), false, true);
82  spawnSelect = ComboBox::create();
83  spawnSelect->setMaxHeight(150.f);
84  row->pack(spawnSelect, false, true);
85  window->pack(row, true, false);
86 
87  scene = engine.renderer().scenePool().allocateScene<bl::rc::scene::Scene2D>();
88  flymapTxtr = engine.renderer().texturePool().getOrLoadTexture(
89  bl::util::FileUtil::joinPath(core::Properties::MenuImagePath(), "FlyMap/background.png"));
90  flyMap.create(engine, flymapTxtr);
91  flyMap.getTransform().setScale(FlymapScale, FlymapScale);
92  flyMap.getTransform().setDepth(2.f);
93  flyMap.addToScene(scene, bl::rc::UpdateSpeed::Static);
94 
95  townCircle.create(engine, 5.f);
96  townCircle.setFillColor(sf::Color::Black);
97  townCircle.setOutlineColor(sf::Color::Red);
98  townCircle.setOutlineThickness(1.f);
99  townCircle.getTransform().setOrigin(5.f, 5.f);
100  townCircle.getTransform().setDepth(1.f);
101  townCircle.addToScene(scene, bl::rc::UpdateSpeed::Static);
102 
103  const glm::vec2 halfSize = flymapTxtr->size() * 0.5f;
104  window->pack(Label::create("Fly map position:"));
105  mapPosCanvas = Canvas::create(halfSize.x, halfSize.y);
106  mapPosCanvas->setScene(scene);
107  auto* cam = mapPosCanvas->setCamera<bl::cam::Camera2D>(halfSize * 0.5f, halfSize);
108  mapPosCanvas->getSignal(Event::LeftClicked)
109  .willAlwaysCall(std::bind(&Towns::setMapPos, this, std::placeholders::_1));
110  window->pack(mapPosCanvas, false, false);
111 
112  but = Button::create("Save");
113  but->getSignal(Event::LeftClicked).willAlwaysCall(std::bind(&Towns::onTownEdit, this));
114  window->pack(but);
115 }
116 
117 Element::Ptr Towns::getContent() { return content; }
118 
119 std::uint8_t Towns::selected() const { return active; }
120 
121 void Towns::setGUI(GUI* g) { gui = g; }
122 
123 sf::Color Towns::getColor(std::uint8_t i) { return Catchables::getColor(i); }
124 
126  scrollRegion->clearChildren(true);
127  active = 0;
128  noTownBut->setValue(true);
129 
130  for (unsigned int i = 0; i < map.towns.size(); ++i) {
131  scrollRegion->pack(makeRow(i, map.towns[i].name), true, false);
132  }
133 
134  refreshSpawns(NoSpawn);
135 }
136 
137 void Towns::refreshSpawns(std::uint16_t s) {
138  spawnSelect->clearOptions();
139  spawnSelect->addOption("None");
140 
141  std::vector<std::uint16_t> spawns;
142  spawns.reserve(map.spawns.size());
143  for (const auto& spawn : map.spawns) { spawns.emplace_back(spawn.first); }
144  std::sort(spawns.begin(), spawns.end());
145 
146  int opt = 0;
147  for (unsigned int i = 0; i < spawns.size(); ++i) {
148  spawnSelect->addOption(std::to_string(spawns[i]));
149  if (spawns[i] == s) { opt = i + 1; }
150  }
151  spawnSelect->setSelectedOption(opt);
152 }
153 
154 void Towns::onPlaylistPick(const std::string& p) {
155  playlistLabel->setText(p);
156  window->setForceFocus(true);
157 }
158 
159 void Towns::takeFocus() { window->setForceFocus(true); }
160 
161 void Towns::closeWindow() {
162  window->setForceFocus(false);
163  window->remove();
164 }
165 
166 void Towns::newTown() { map.addTown(); }
167 
168 void Towns::editTown(std::uint8_t i) {
169  editing = i;
170  nameEntry->setInput(map.towns[i].name);
171  descEntry->setInput(map.towns[i].description);
172  playlistLabel->setText(map.towns[i].playlist);
173  weatherSelect->setSelectedWeather(map.towns[i].weather);
174  mapPos = map.towns[i].mapPos;
175  refreshFlymapCanvas();
176  refreshSpawns(map.towns[i].pcSpawn);
177  mapArea.disableControls();
178  gui->pack(window);
179  window->setForceFocus(true);
180 }
181 
182 void Towns::removeTown(std::uint8_t i) { map.removeTown(i); }
183 
184 void Towns::onTownEdit() {
185  core::map::Town t;
186  t.name = nameEntry->getInput();
187  t.description = descEntry->getInput();
188  t.playlist = playlistLabel->getText();
189  t.weather = weatherSelect->selectedWeather();
190  t.mapPos = mapPos;
191  if (spawnSelect->getSelectedOptionText() == "None") { t.pcSpawn = NoSpawn; }
192  else { t.pcSpawn = std::atoi(spawnSelect->getSelectedOptionText().c_str()); }
193  map.editTown(editing, t);
194  closeWindow();
195 }
196 
197 Box::Ptr Towns::makeRow(std::uint8_t i, const std::string& name) {
198  Box::Ptr row = Box::create(LinePacker::create(
199  LinePacker::Horizontal, 4.f, LinePacker::Compact, LinePacker::RightAlign));
200  row->setColor(getColor(i + 1), sf::Color::Black);
201 
202  RadioButton::Ptr but = RadioButton::create(name, name, noTownBut->getRadioGroup());
203  but->getSignal(Event::LeftClicked).willAlwaysCall([this, i](const Event&, Element*) {
204  active = i + 1;
205  });
206  but->setHorizontalAlignment(RenderSettings::Left);
207 
208  Button::Ptr edit = Button::create("Edit");
209  edit->getSignal(Event::LeftClicked).willAlwaysCall(std::bind(&Towns::editTown, this, i));
210 
211  Button::Ptr del = Button::create("Remove");
212  del->getSignal(Event::LeftClicked).willAlwaysCall(std::bind(&Towns::removeTown, this, i));
213  del->setColor(sf::Color::Red, sf::Color::Black);
214 
215  row->pack(del, false, true);
216  row->pack(edit, false, true);
217  row->pack(but, true, true);
218 
219  return row;
220 }
221 
222 void Towns::setMapPos(const Event& click) {
223  const sf::Vector2f localPos =
224  (click.mousePosition() - mapPosCanvas->getPosition()) / FlymapScale;
225  mapPos = sf::Vector2i(localPos);
226  refreshFlymapCanvas();
227 }
228 
229 void Towns::refreshFlymapCanvas() {
230  townCircle.getTransform().setPosition(glm::vec2(mapPos.x, mapPos.y) * FlymapScale);
231 }
232 
233 } // namespace page
234 } // namespace editor
All classes and functionality used for implementing the game editor.
Definition: Tile.hpp:11
std::unordered_map< std::uint16_t, Spawn > spawns
Definition: Map.hpp:305
std::vector< Town > towns
Definition: Map.hpp:312
Represents a town, route, or region within a map. Maps may have many towns. Individual tiles are asso...
Definition: Town.hpp:22
std::uint16_t pcSpawn
Definition: Town.hpp:26
sf::Vector2i mapPos
Definition: Town.hpp:28
std::string playlist
Definition: Town.hpp:24
Weather::Type weather
Definition: Town.hpp:25
std::string description
Definition: Town.hpp:27
std::string name
Definition: Town.hpp:23
static const std::string & MenuImagePath()
Definition: Properties.cpp:303
void removeTown(std::uint8_t i)
Removes a town.
Definition: EditMap.cpp:873
void addTown()
Adds a town.
Definition: EditMap.cpp:867
void editTown(std::uint8_t i, const core::map::Town &town)
Modifies an existing town.
Definition: EditMap.cpp:869
void open(bl::gui::GUI *gui, const std::string &plist)
Opens the window with an optional file to load.
static Ptr create()
Creates a new weather selector.
static sf::Color getColor(std::uint8_t index)
Returns the color to use for the given catch region.
Definition: Catchables.cpp:59
Section of the map area with the map itself and related controls.
Definition: MapArea.hpp:16
void disableControls()
Disables the map controls when a dialog is opened or the map tab is inactive.
Definition: MapArea.cpp:107
Subpage for creating, editing, deleting, and placing towns in maps.
Definition: Towns.hpp:21
bl::gui::Element::Ptr getContent()
Returns the GUI element to pack.
Definition: Towns.cpp:117
void refresh()
Refreshes the list of towns in the page.
Definition: Towns.cpp:125
void setGUI(bl::gui::GUI *gui)
Set the primary GUI object.
Definition: Towns.cpp:121
static sf::Color getColor(std::uint8_t index)
Returns the color to use for the given town.
Definition: Towns.cpp:123
std::uint8_t selected() const
Returns the currently selected town index.
Definition: Towns.cpp:119
Towns(bl::engine::Engine &engine, MapArea &map)
Creates the towns subpage and GUI elements.
Definition: Towns.cpp:19