Peoplemon  0.1.0
Peoplemon 3 game source documentation
EventEditor.cpp
Go to the documentation of this file.
2 
3 namespace editor
4 {
5 namespace component
6 {
7 using namespace bl::gui;
8 
10 : onEdit(oe)
11 , scriptSelector(std::bind(&EventEditor::onScriptChosen, this, std::placeholders::_1),
12  [this]() { window->setForceFocus(true); }) {
13  window = Window::create(LinePacker::create(LinePacker::Vertical, 10), "Event Editor");
14  window->getSignal(Event::Closed).willAlwaysCall([this](const Event&, Element*) {
15  window->remove();
16  });
17 
18  Box::Ptr row = Box::create(LinePacker::create(LinePacker::Horizontal, 8));
19  scriptLabel = Label::create("");
20  scriptLabel->setColor(sf::Color::Blue, sf::Color::Transparent);
21  row->pack(scriptLabel, true, true);
22  Button::Ptr pickButton = Button::create("Pick/Edit Script");
23  pickButton->getSignal(Event::LeftClicked).willAlwaysCall([this](const Event&, Element*) {
24  window->setForceFocus(false);
25  scriptSelector.open(parent, scriptLabel->getText());
26  });
27  row->pack(pickButton, false, true);
28  window->pack(row, true, false);
29 
30  row = Box::create(LinePacker::create(LinePacker::Horizontal, 4));
31  Box::Ptr input = Box::create(LinePacker::create(LinePacker::Horizontal, 4));
32  input->pack(Label::create("X:"), false, true);
33  xInput = TextEntry::create();
34  input->pack(xInput, true, true);
35  row->pack(input, true, true);
36  input = Box::create(LinePacker::create(LinePacker::Horizontal, 4));
37  input->pack(Label::create("Y:"), false, true);
38  yInput = TextEntry::create();
39  input->pack(yInput, true, true);
40  row->pack(input, true, true);
41  window->pack(row, true, false);
42 
43  row = Box::create(LinePacker::create(LinePacker::Horizontal, 4));
44  input = Box::create(LinePacker::create(LinePacker::Horizontal, 4));
45  input->pack(Label::create("W:"), false, true);
46  wInput = TextEntry::create();
47  input->pack(wInput, true, true);
48  row->pack(input, true, true);
49  input = Box::create(LinePacker::create(LinePacker::Horizontal, 4));
50  input->pack(Label::create("H:"), false, true);
51  hInput = TextEntry::create();
52  input->pack(hInput, true, true);
53  row->pack(input, true, true);
54  window->pack(row, true, false);
55 
56  row = Box::create(LinePacker::create(LinePacker::Horizontal, 4));
57  triggerSelect = ComboBox::create();
58  triggerSelect->addOption("OnEnter");
59  triggerSelect->addOption("OnExit");
60  triggerSelect->addOption("OnEnterOrExit");
61  triggerSelect->addOption("WhileIn");
62  triggerSelect->addOption("OnInteract");
63  row->pack(Label::create("Trigger:"), false, true);
64  row->pack(triggerSelect, false, true);
65  window->pack(row, true, false);
66 
67  row = Box::create(LinePacker::create(LinePacker::Horizontal, 4));
68  Button::Ptr editBut = Button::create("Confirm");
69  editBut->getSignal(Event::LeftClicked).willAlwaysCall([this](const Event&, Element*) {
70  if (valid()) {
71  onEdit(orig, makeEvent());
72  window->remove();
73  }
74  });
75  row->pack(editBut, false, true);
76  Button::Ptr cancelBut = Button::create("Cancel");
77  cancelBut->getSignal(Event::LeftClicked).willAlwaysCall([this](const Event&, Element*) {
78  window->remove();
79  });
80  row->pack(cancelBut, false, true);
81  window->pack(row, true, false);
82 }
83 
84 void EventEditor::open(GUI* p, const core::map::Event* source,
85  const sf::Vector2i& tiles) {
86  parent = p;
87  p->pack(window);
88  window->setForceFocus(true);
89  orig = source;
90 
91  if (source) {
92  scriptLabel->setText(source->script);
93  xInput->setInput(std::to_string(source->position.x));
94  yInput->setInput(std::to_string(source->position.y));
95  wInput->setInput(std::to_string(source->areaSize.x));
96  hInput->setInput(std::to_string(source->areaSize.y));
97  triggerSelect->setSelectedOption(static_cast<int>(source->trigger) - 1);
98  }
99  else {
100  scriptLabel->setText("");
101  xInput->setInput(std::to_string(tiles.x));
102  yInput->setInput(std::to_string(tiles.y));
103  wInput->setInput("1");
104  hInput->setInput("1");
105  triggerSelect->setSelectedOption(0);
106  }
107 }
108 
109 bool EventEditor::valid() const {
110  const auto isNum = [](const std::string& s) -> bool {
111  for (const char c : s) {
112  if (c < '0' || c > '9') return false;
113  }
114  return true;
115  };
116 
117  const auto err = [](const std::string& e) {
118  bl::dialog::tinyfd_messageBox("Error", e.c_str(), "ok", "error", 1);
119  };
120 
121  if (!isNum(xInput->getInput())) {
122  err("X tile position is invalid");
123  return false;
124  }
125  if (!isNum(yInput->getInput())) {
126  err("Y tile position is invalid");
127  return false;
128  }
129  if (!isNum(wInput->getInput())) {
130  err("Region tile width is invalid");
131  return false;
132  }
133  if (!isNum(hInput->getInput())) {
134  err("Region tile height is invalid");
135  return false;
136  }
137  return true;
138 }
139 
140 core::map::Event EventEditor::makeEvent() const {
142  e.script = scriptLabel->getText();
143  e.position.x = std::atoi(xInput->getInput().c_str());
144  e.position.y = std::atoi(yInput->getInput().c_str());
145  e.areaSize.x = std::atoi(wInput->getInput().c_str());
146  e.areaSize.y = std::atoi(hInput->getInput().c_str());
147  e.trigger = static_cast<core::map::Event::Trigger>(triggerSelect->getSelectedOption() + 1);
148  return e;
149 }
150 
151 void EventEditor::onScriptChosen(const std::string& s) {
152  scriptLabel->setText(s);
153  window->setForceFocus(true);
154 }
155 
156 } // namespace component
157 } // namespace editor
All classes and functionality used for implementing the game editor.
Definition: Tile.hpp:11
Represents an event in a Map. A script that is run on a trigger within a given region.
Definition: Event.hpp:19
sf::Vector2i position
Definition: Event.hpp:39
std::string script
Definition: Event.hpp:41
Trigger
What action triggers the event.
Definition: Event.hpp:21
Trigger trigger
Definition: Event.hpp:38
sf::Vector2i areaSize
Definition: Event.hpp:40
Map event editor dialog.
Definition: EventEditor.hpp:18
std::function< void(const core::map::Event *, const core::map::Event &)> OnEdit
Callback called when an event is created or modified.
Definition: EventEditor.hpp:21
void open(bl::gui::GUI *parent, const core::map::Event *source, const sf::Vector2i &pos)
Opens the dialog window and optionally populates with event info.
Definition: EventEditor.cpp:84
EventEditor(const OnEdit &onEdit)
Construct a new Event Editor dialog.
Definition: EventEditor.cpp:9