Peoplemon  0.1.0
Peoplemon 3 game source documentation
NewMapDialog.cpp
Go to the documentation of this file.
2 
3 #include <BLIB/Interfaces/GUI/Dialogs/tinyfiledialogs.hpp>
4 #include <Core/Properties.hpp>
5 
6 namespace editor
7 {
8 namespace component
9 {
10 namespace
11 {
12 bool isnum(const std::string& s) {
13  for (char c : s) {
14  if (c < '0' || c > '9') return false;
15  }
16  return true;
17 }
18 } // namespace
19 using namespace bl::gui;
20 
22 : createCb(cb)
23 , tilesetPicker(
24  core::Properties::TilesetPath(), {"tlst"},
25  [this](const std::string& tileset) {
26  tilesetLabel->setText(tileset);
27  tilesetPicker.close();
28  window->setForceFocus(true);
29  },
30  [this]() {
31  tilesetPicker.close();
32  window->setForceFocus(true);
33  }) {
34  window = Window::create(LinePacker::create(LinePacker::Vertical, 4), "New map");
35 
36  Box::Ptr row = Box::create(LinePacker::create(LinePacker::Horizontal, 6));
37  row->pack(Label::create("Map file:"), false, true);
38  mapFileLabel = Label::create("map file");
39  mapFileLabel->setColor(sf::Color(15, 100, 245), sf::Color::Transparent);
40  row->pack(mapFileLabel, false, true);
41  window->pack(row, true, false);
42 
43  row = Box::create(LinePacker::create(LinePacker::Horizontal, 6));
44  row->pack(Label::create("Name:"), false, true);
45  nameEntry = TextEntry::create();
46  nameEntry->setRequisition({250, 10});
47  row->pack(nameEntry, true, true);
48  window->pack(row, true, false);
49 
50  row = Box::create(LinePacker::create(LinePacker::Horizontal, 4));
51  row->pack(Label::create("Width:"), false, true);
52  widthEntry = TextEntry::create();
53  widthEntry->setRequisition({100, 10});
54  row->pack(widthEntry, false, true);
55  row->pack(Label::create("Height:"), false, true);
56  heightEntry = TextEntry::create();
57  heightEntry->setRequisition({100, 10});
58  row->pack(heightEntry, false, true);
59  window->pack(row);
60 
61  row = Box::create(LinePacker::create(LinePacker::Horizontal, 4));
62  pickBut = Button::create("Pick Tileset");
63  row->pack(pickBut, false, true);
64  tilesetLabel = Label::create("tileset file");
65  tilesetLabel->setColor(sf::Color(30, 245, 85), sf::Color::Transparent);
66  row->pack(tilesetLabel, false, true);
67  window->pack(row, true, false);
68 
69  Button::Ptr createBut = Button::create("Create Map");
70  createBut->getSignal(Event::LeftClicked).willAlwaysCall([this](const Event&, Element*) {
71  if (nameEntry->getInput().empty()) {
72  bl::dialog::tinyfd_messageBox("Error", "Please enter a map name", "ok", "error", 1);
73  return;
74  }
75  if (widthEntry->getInput().empty()) {
76  bl::dialog::tinyfd_messageBox("Error", "Please enter a width", "ok", "error", 1);
77  return;
78  }
79  if (heightEntry->getInput().empty()) {
80  bl::dialog::tinyfd_messageBox("Error", "Please enter a height", "ok", "error", 1);
81  return;
82  }
83  if (!isnum(widthEntry->getInput())) {
84  bl::dialog::tinyfd_messageBox(
85  "Error", "Width must be a positive integer", "ok", "error", 1);
86  return;
87  }
88  if (!isnum(heightEntry->getInput())) {
89  bl::dialog::tinyfd_messageBox(
90  "Error", "Height must be a positive integer", "ok", "error", 1);
91  return;
92  }
93  if (tilesetLabel->getText().empty()) {
94  bl::dialog::tinyfd_messageBox("Error", "Please pick a tileset", "ok", "error", 1);
95  return;
96  }
97 
98  window->remove();
99  createCb(mapFileLabel->getText(),
100  nameEntry->getInput(),
101  tilesetLabel->getText(),
102  std::atoi(widthEntry->getInput().c_str()),
103  std::atoi(heightEntry->getInput().c_str()));
104  });
105  window->pack(createBut);
106 
107  window->getSignal(Event::Closed).willAlwaysCall([this](const Event&, Element*) {
108  window->remove();
109  });
110 }
111 
112 void NewMapDialog::show(GUI* parent, const std::string& file) {
113  mapFileLabel->setText(file);
114  tilesetLabel->setText("");
115  nameEntry->setInput("");
116  widthEntry->setInput("");
117  heightEntry->setInput("");
118 
119  if (!pickInit) {
120  pickInit = true;
121  pickBut->getSignal(Event::LeftClicked)
122  .willAlwaysCall([this, parent](const Event&, Element*) {
123  window->setForceFocus(false);
124  tilesetPicker.open(FilePicker::CreateOrPick, "Pick tileset", parent, true);
125  });
126  }
127 
128  parent->pack(window);
129  window->setForceFocus(true);
130 }
131 
132 } // namespace component
133 } // namespace editor
Core classes and functionality for both the editor and game.
All classes and functionality used for implementing the game editor.
Definition: Tile.hpp:11
void show(bl::gui::GUI *parent, const std::string &file)
Opens the new map dialog.
NewMapDialog(const CreateCb &createCb)
Creates a new new-map dialoig.
std::function< void(const std::string &file, const std::string &name, const std::string &tileset, unsigned int w, unsigned int h)> CreateCb
Callback for when maps are created.