11 const std::string NoFile =
"<no file selected>";
13 bool isNum(
const std::string& s) {
15 if (c < '0' || c >
'9')
return false;
20 bool validFile(
const std::string& f) {
21 if (f.empty() || f == NoFile) {
return false; }
23 if (!bl::util::FileUtil::exists(
33 using namespace bl::gui;
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(); });
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();
55 window->setForceFocus(
false);
56 npcEditor.show(parent, f);
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();
66 window->setForceFocus(
false);
67 trainerEditor.show(parent, f);
69 row->pack(tnrBut,
false,
true);
70 window->pack(row,
true,
false);
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);
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);
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);
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);
112 if (levelInput->getInput().empty() || !isNum(levelInput->getInput())) {
113 bl::dialog::tinyfd_messageBox(
114 "Bad Level",
"Please enter a valid level",
"ok",
"error", 1);
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);
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);
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()));
137 row->pack(editBut,
false,
true);
138 Button::Ptr cancelBut = Button::create(
"Cancel");
139 cancelBut->getSignal(Event::LeftClicked).willAlwaysCall([
this](
const Event&, Element*) {
142 row->pack(cancelBut,
false,
true);
143 window->pack(row,
true,
false);
150 parent->pack(window);
151 window->setForceFocus(
true);
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));
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);
169 void CharacterSpawnWindow::onFilechoose(
const std::string& file) { fileLabel->setText(file); }
171 void CharacterSpawnWindow::closeAll() {
173 window->setForceFocus(
false);
All classes and functionality used for implementing the game editor.
Represents a character to be spawned into a map on load.
bl::tmap::Position position
static const std::string & NpcFileExtension()
static const std::string & TrainerPath()
static const std::string & NpcPath()
static const std::string & TrainerFileExtension()
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.