Peoplemon  0.1.0
Peoplemon 3 game source documentation
AnimationWindow.cpp
Go to the documentation of this file.
2 
4 #include <Core/Properties.hpp>
5 #include <Core/Resources.hpp>
6 #include <cstdlib>
7 #include <sstream>
8 
9 namespace editor
10 {
11 namespace component
12 {
13 using namespace bl::gui;
14 
15 namespace
16 {
17 bool validFile(const std::string& f) { return !f.empty() && bl::util::FileUtil::exists(f); }
18 } // namespace
19 
20 AnimationWindow::AnimationWindow(bool cm, const ChooseCb& cb, const CloseCb& ccb)
21 : characterMode(cm)
22 , chooseCb(cb)
23 , closeCb(ccb)
24 , parent(nullptr) {
25  constexpr const char* DefaultAnim = "1/down.anim";
26 
27  window = Window::create(LinePacker::create(LinePacker::Vertical, 4), "Animation Picker");
28  window->getSignal(Event::Closed).willAlwaysCall([this](const Event&, Element*) { hide(); });
29 
30  Box::Ptr row = Box::create(LinePacker::create(LinePacker::Horizontal, 4));
31  Button::Ptr pickBut = Button::create("Choose Animation");
32  pickBut->getSignal(Event::LeftClicked).willAlwaysCall([this](const Event&, Element*) {
33  filePicker.emplace(path,
34  std::vector<std::string>{"anim"},
35  std::bind(&AnimationWindow::packAnim, this, std::placeholders::_1),
36  [this]() {
37  filePicker.value().close();
38  filePicker.reset();
39  window->setForceFocus(true);
40  });
41  window->setForceFocus(false);
42  filePicker.value().open(FilePicker::PickExisting, "Select Animation", parent);
43  });
44  fileLabel = Label::create(DefaultAnim);
45  fileLabel->setColor(sf::Color::Cyan, sf::Color::Transparent);
46  row->pack(pickBut, false, true);
47  row->pack(fileLabel, true, true);
48  window->pack(row);
49 
50  Button::Ptr editBut = Button::create("Open Editor");
51  editBut->getSignal(Event::LeftClicked).willAlwaysCall([](const Event&, Element*) {
53  });
54  window->pack(editBut, false, false);
55 
56  row = Box::create(LinePacker::create(LinePacker::Horizontal, 4));
57  auto src = AnimationManager::load(
58  bl::util::FileUtil::joinPath(core::Properties::CharacterAnimationPath(), DefaultAnim));
59  animation = Animation::create(src);
60  row->pack(animation, true, true);
61  window->pack(row, true, true);
62 
63  row = Box::create(LinePacker::create(LinePacker::Horizontal, 4));
64  Button::Ptr chooseBut = Button::create("Use Animation");
65  chooseBut->getSignal(Event::LeftClicked).willAlwaysCall([this](const Event&, Element*) {
66  if (validFile(fileLabel->getText())) {
67  bl::dialog::tinyfd_messageBox(
68  "No File", "Please select an animation", "ok", "error", 1);
69  return;
70  }
71 
72  chooseCb(fileLabel->getText());
73  hide();
74  });
75  chooseBut->setColor(sf::Color::Green, sf::Color::Black);
76  Button::Ptr cancelBut = Button::create("Cancel");
77  cancelBut->getSignal(Event::LeftClicked).willAlwaysCall([this](const Event&, Element*) {
78  hide();
79  });
80  cancelBut->setColor(sf::Color::Red, sf::Color::Black);
81  row->pack(chooseBut, false, true);
82  row->pack(cancelBut, false, true);
83  window->pack(row);
84 }
85 
86 void AnimationWindow::open(GUI* p, const std::string& pt, const std::string& file) {
87  path = pt;
88  parent = p;
89  p->pack(window);
90  if (file != "<no anim selected>") { packAnim(file); }
91  window->setForceFocus(true);
92 }
93 
94 void AnimationWindow::packAnim(const std::string& f) {
95  const std::string af = characterMode ? bl::util::FileUtil::getPath(f) : f;
96  const std::string vf = characterMode ? bl::util::FileUtil::joinPath(af, "down.anim") : af;
97  fileLabel->setText(af);
98  animSrc = AnimationManager::load(bl::util::FileUtil::joinPath(path, vf));
99 
100  if (animSrc) {
101  sf::Vector2f size = animSrc->getMaxSize();
102  if (size.x > 400.f) {
103  size.y *= 400.f / size.x;
104  size.x = 400.f;
105  animation->scaleToSize(size);
106  }
107  if (size.y > 400.f) {
108  size.x *= 400.f / size.y;
109  size.y = 400.f;
110  animation->scaleToSize(size);
111  }
112  animation->setAnimation(animSrc);
113  }
114  if (filePicker.has_value()) {
115  filePicker.value().close();
116  filePicker.reset();
117  window->setForceFocus(true);
118  }
119 }
120 
122  window->remove();
123  window->setForceFocus(false);
124  closeCb();
125  if (filePicker.has_value()) {
126  filePicker.value().close();
127  filePicker.reset();
128  }
129 }
130 
131 } // namespace component
132 } // namespace editor
All classes and functionality used for implementing the game editor.
Definition: Tile.hpp:11
static const std::string & CharacterAnimationPath()
Definition: Properties.cpp:557
std::function< void()> CloseCb
Called when the window is closed.
std::function< void(const std::string &)> ChooseCb
Called when an animation is chosen.
AnimationWindow(bool characterMode, const ChooseCb &chooseCb, const CloseCb &closeCb)
Construct a new Animation Window.
void open(bl::gui::GUI *parent, const std::string &path, const std::string &file)
Opens the animation picker window.
void hide()
Removes the window from view.