Peoplemon  0.1.0
Peoplemon 3 game source documentation
CharacterSpawnWindow.cpp
Go to the documentation of this file.
2 
3 #include <Core/Properties.hpp>
4 
5 namespace editor
6 {
7 namespace component
8 {
9 namespace
10 {
11 const std::string NoFile = "<no file selected>";
12 
13 bool isNum(const std::string& s) {
14  for (char c : s) {
15  if (c < '0' || c > '9') return false;
16  }
17  return true;
18 }
19 
20 bool validFile(const std::string& f) {
21  if (f.empty() || f == NoFile) { return false; }
22  if (!bl::util::FileUtil::exists(bl::util::FileUtil::joinPath(core::Properties::NpcPath(), f))) {
23  if (!bl::util::FileUtil::exists(
24  bl::util::FileUtil::joinPath(core::Properties::TrainerPath(), f))) {
25  return false;
26  }
27  }
28  return true;
29 }
30 
31 } // namespace
32 
33 using namespace bl::gui;
34 
36 : onEdit(cb)
37 , npcEditor(std::bind(&CharacterSpawnWindow::onFilechoose, this, std::placeholders::_1),
38  [this]() { window->setForceFocus(true); })
39 , trainerEditor(std::bind(&CharacterSpawnWindow::onFilechoose, this, std::placeholders::_1),
40  [this]() { window->setForceFocus(true); }) {
41  window = Window::create(LinePacker::create(LinePacker::Vertical, 4), "Character Spawn");
42  window->getSignal(Event::Closed).willAlwaysCall([this](const Event&, Element*) { closeAll(); });
43 
44  Box::Ptr row = Box::create(LinePacker::create(LinePacker::Horizontal, 4));
45  fileLabel = Label::create("file here");
46  fileLabel->setColor(sf::Color::Cyan, sf::Color::Cyan);
47  row->pack(fileLabel, true, true);
48  Button::Ptr npcBut = Button::create("NPC");
49  npcBut->getSignal(Event::LeftClicked).willAlwaysCall([this](const Event&, Element*) {
50  std::string f = fileLabel->getText();
51  if (bl::util::FileUtil::getExtension(f) != core::Properties::NpcFileExtension() ||
52  !validFile(f)) {
53  f.clear();
54  }
55  window->setForceFocus(false);
56  npcEditor.show(parent, f);
57  });
58  row->pack(npcBut, false, true);
59  Button::Ptr tnrBut = Button::create("Trainer");
60  tnrBut->getSignal(Event::LeftClicked).willAlwaysCall([this](const Event&, Element*) {
61  std::string f = fileLabel->getText();
62  if (bl::util::FileUtil::getExtension(f) != core::Properties::TrainerFileExtension() ||
63  !validFile(f)) {
64  f.clear();
65  }
66  window->setForceFocus(false);
67  trainerEditor.show(parent, f);
68  });
69  row->pack(tnrBut, false, true);
70  window->pack(row, true, false);
71 
72  row = Box::create(LinePacker::create(LinePacker::Horizontal, 4));
73  row->pack(Label::create("Level:"), false, true);
74  levelInput = TextEntry::create();
75  levelInput->setMode(TextEntry::Mode::Integer);
76  row->pack(levelInput, true, false);
77  window->pack(row, true, false);
78 
79  row = Box::create(LinePacker::create(LinePacker::Horizontal, 4));
80  Box::Ptr input = Box::create(LinePacker::create(LinePacker::Horizontal, 4));
81  input->pack(Label::create("X:"), false, true);
82  xInput = TextEntry::create();
83  xInput->setMode(TextEntry::Mode::Integer);
84  input->pack(xInput, true, true);
85  row->pack(input, true, false);
86  input = Box::create(LinePacker::create(LinePacker::Horizontal, 4));
87  input->pack(Label::create("Y:"), false, true);
88  yInput = TextEntry::create();
89  yInput->setMode(TextEntry::Mode::Integer);
90  input->pack(yInput, true, true);
91  row->pack(input, true, false);
92  window->pack(row, true, false);
93 
94  dirEntry = ComboBox::create();
95  dirEntry->addOption("Up");
96  dirEntry->addOption("Right");
97  dirEntry->addOption("Down");
98  dirEntry->addOption("Left");
99  row = Box::create(LinePacker::create(LinePacker::Horizontal, 4));
100  row->pack(Label::create("Dir:"), false, true);
101  row->pack(dirEntry, false, true);
102  window->pack(row, true, false);
103 
104  row = Box::create(LinePacker::create(LinePacker::Horizontal, 4));
105  Button::Ptr editBut = Button::create("Confirm");
106  editBut->getSignal(Event::LeftClicked).willAlwaysCall([this](const Event&, Element*) {
107  if (!validFile(fileLabel->getText())) {
108  bl::dialog::tinyfd_messageBox(
109  "Selelct Character", "Please select an NPC or trainer to spawn", "ok", "error", 1);
110  return;
111  }
112  if (levelInput->getInput().empty() || !isNum(levelInput->getInput())) {
113  bl::dialog::tinyfd_messageBox(
114  "Bad Level", "Please enter a valid level", "ok", "error", 1);
115  return;
116  }
117  if (xInput->getInput().empty() || !isNum(xInput->getInput())) {
118  bl::dialog::tinyfd_messageBox(
119  "Bad Position", "Please enter a valid x tile", "ok", "error", 1);
120  return;
121  }
122  if (yInput->getInput().empty() || !isNum(yInput->getInput())) {
123  bl::dialog::tinyfd_messageBox(
124  "Bad Position", "Please enter a valid y tile", "ok", "error", 1);
125  return;
126  }
127  closeAll();
128  onEdit(
129  orig,
131  bl::tmap::Position(std::atoi(levelInput->getInput().c_str()),
132  glm::i32vec2(std::atoi(xInput->getInput().c_str()),
133  std::atoi(yInput->getInput().c_str())),
134  static_cast<bl::tmap::Direction>(dirEntry->getSelectedOption())),
135  fileLabel->getText()));
136  });
137  row->pack(editBut, false, true);
138  Button::Ptr cancelBut = Button::create("Cancel");
139  cancelBut->getSignal(Event::LeftClicked).willAlwaysCall([this](const Event&, Element*) {
140  closeAll();
141  });
142  row->pack(cancelBut, false, true);
143  window->pack(row, true, false);
144 }
145 
146 void CharacterSpawnWindow::open(bl::gui::GUI* p, unsigned int level, const sf::Vector2i& pos,
147  const core::map::CharacterSpawn* orig) {
148  parent = p;
149  this->orig = orig;
150  parent->pack(window);
151  window->setForceFocus(true);
152 
153  if (orig) {
154  fileLabel->setText(orig->file);
155  xInput->setInput(std::to_string(orig->position.position.x));
156  yInput->setInput(std::to_string(orig->position.position.y));
157  levelInput->setInput(std::to_string(orig->position.level));
158  dirEntry->setSelectedOption(static_cast<int>(orig->position.direction));
159  }
160  else {
161  fileLabel->setText("<no file selected>");
162  xInput->setInput(std::to_string(pos.x));
163  yInput->setInput(std::to_string(pos.y));
164  levelInput->setInput(std::to_string(level));
165  dirEntry->setSelectedOption(0);
166  }
167 }
168 
169 void CharacterSpawnWindow::onFilechoose(const std::string& file) { fileLabel->setText(file); }
170 
171 void CharacterSpawnWindow::closeAll() {
172  window->remove();
173  window->setForceFocus(false);
174  npcEditor.hide();
175 }
176 
177 } // namespace component
178 } // namespace editor
All classes and functionality used for implementing the game editor.
Definition: Tile.hpp:11
Represents a character to be spawned into a map on load.
bl::tmap::Position position
static const std::string & NpcFileExtension()
Definition: Properties.cpp:515
static const std::string & TrainerPath()
Definition: Properties.cpp:533
static const std::string & NpcPath()
Definition: Properties.cpp:521
static const std::string & TrainerFileExtension()
Definition: Properties.cpp:527
Editing window for character spawns. Manages NPCs and trainers.
CharacterSpawnWindow(const OnEdit &onEdit)
Creates a new CharacterSpawnWindow.
void open(bl::gui::GUI *parent, unsigned int level, const sf::Vector2i &pos, const core::map::CharacterSpawn *orig)
Opens the spawn editing window.
std::function< void(const core::map::CharacterSpawn *, const core::map::CharacterSpawn &)> OnEdit
Callback for when spawns are created or edited.
void hide()
Hides the window and all created child windows.