Peoplemon  0.1.0
Peoplemon 3 game source documentation
MapArea.cpp
Go to the documentation of this file.
2 
3 namespace editor
4 {
5 namespace page
6 {
7 using namespace bl::gui;
8 
11 : onClick(cb)
12 , map(component::EditMap::create(
13  cb, std::bind(&MapArea::onMouseOver, this, std::placeholders::_1, std::placeholders::_2),
14  std::bind(&MapArea::refreshButtons, this), syncCb, s))
15 , lastDragTile(-1, -1) {
16  content = Box::create(LinePacker::create(LinePacker::Vertical, 0));
17  content->setOutlineThickness(0.f);
18 
19  Box::Ptr controlRow = Box::create(LinePacker::create(LinePacker::Horizontal, 0));
20  controlRow->setOutlineThickness(0.f);
21 
22  Box::Ptr leftSide = Box::create(LinePacker::create(LinePacker::Horizontal, 4));
23  undoBut = Button::create("Undo");
24  undoBut->getSignal(Event::LeftClicked).willAlwaysCall([this](const Event&, Element*) {
25  map->undo();
26  });
27  undoBut->setActive(false);
28  redoBut = Button::create("Redo");
29  redoBut->getSignal(Event::LeftClicked).willAlwaysCall([this](const Event&, Element*) {
30  map->redo();
31  });
32  redoBut->setActive(false);
33  CheckButton::Ptr gbut = CheckButton::create("Grid");
34  gbut->getSignal(Event::ValueChanged).willAlwaysCall([this](const Event& e, Element*) {
35  map->showGrid(e.toggleValue());
36  });
37  positionLabel = Label::create("Tile: ()");
38  positionLabel->setHorizontalAlignment(RenderSettings::Right);
39  positionLabel->setVerticalAlignment(RenderSettings::Bottom);
40  leftSide->pack(undoBut, false, true);
41  leftSide->pack(redoBut, false, true);
42  leftSide->pack(gbut, false, true);
43  leftSide->pack(positionLabel, true, true);
44 
45  Box::Ptr rightSide = Box::create(LinePacker::create(
46  LinePacker::Horizontal, 6.f, LinePacker::Compact, LinePacker::RightAlign));
47  enableBut = CheckButton::create("Enable Map Controls");
48  enableBut->setTooltip("Enable or disable map editing. Useful for not making accidental edits");
49  enableBut->getSignal(Event::ValueChanged).willAlwaysCall([this](const Event& a, Element*) {
50  map->setControlsEnabled(a.toggleValue());
51  });
52  dragBut = CheckButton::create("Enable drag");
53 
54  rightSide->pack(enableBut, false, true);
55  rightSide->pack(dragBut, false, true);
56 
57  controlRow->pack(leftSide, true, false);
58  controlRow->pack(rightSide, true, false);
59  content->pack(controlRow, true, false);
60  content->pack(map, true, true);
61 
62  bl::event::Dispatcher::subscribe(this);
63 }
64 
66 
67 bl::gui::Element::Ptr MapArea::getContent() { return content; }
68 
69 void MapArea::refreshButtons() {
70  const char* undoDesc = map->undoDescription();
71  if (undoDesc) {
72  undoBut->setTooltip(std::string("Undo ") + undoDesc);
73  undoBut->setActive(true);
74  }
75  else {
76  undoBut->setTooltip("");
77  undoBut->setActive(false);
78  }
79 
80  const char* redoDesc = map->redoDescription();
81  if (redoDesc) {
82  redoBut->setTooltip(std::string("Redo ") + redoDesc);
83  redoBut->setActive(true);
84  }
85  else {
86  redoBut->setTooltip("");
87  redoBut->setActive(false);
88  }
89 }
90 
91 void MapArea::onMouseOver(const sf::Vector2f& pixels, const sf::Vector2i& tiles) {
92  positionLabel->setText("Tile: (" + std::to_string(tiles.x) + ", " + std::to_string(tiles.y) +
93  ")");
94  if (sf::Mouse::isButtonPressed(sf::Mouse::Left) && dragBut->getValue()) {
95  if (lastDragTile != tiles && enableBut->getValue()) {
96  lastDragTile = tiles;
97  onClick(pixels, tiles);
98  }
99  }
100 }
101 
103  editMap().setControlsEnabled(true);
104  enableBut->setValue(true);
105 }
106 
108  editMap().setControlsEnabled(false);
109  enableBut->setValue(false);
110 }
111 
112 void MapArea::observe(const event::MapRenderStarted&) {
113  enableBut->setActive(false);
114  enableBut->setForceFocus(true);
115 }
116 
117 void MapArea::observe(const event::MapRenderCompleted&) {
118  enableBut->setActive(true);
119  enableBut->setForceFocus(false);
120 }
121 
122 } // namespace page
123 } // namespace editor
All classes and functionality used for implementing the game editor.
Definition: Tile.hpp:11
Owns all primary systems and a reference to the engine.
Definition: Systems.hpp:47
Wrapper over the core::Map class that is directly usable in a bl::gui::GUI.
Definition: EditMap.hpp:28
std::function< void(const sf::Vector2f &pixels, const sf::Vector2i &tiles)> PositionCb
Called when the map is clicked.
Definition: EditMap.hpp:34
std::function< void()> ActionCb
Called on various event types.
Definition: EditMap.hpp:37
void setControlsEnabled(bool enabled)
Enables or disables the map camera and click controls.
Definition: EditMap.cpp:292
Fired when a map rendering is started.
Definition: MapRender.hpp:19
Fired when a map rendering is complete.
Definition: MapRender.hpp:26
Section of the map area with the map itself and related controls.
Definition: MapArea.hpp:16
bl::gui::Element::Ptr getContent()
Returns the GUI element to pack.
Definition: MapArea.cpp:67
void enableControls()
Enables the map controls.
Definition: MapArea.cpp:102
MapArea(const component::EditMap::PositionCb &clickCb, const component::EditMap::ActionCb &syncCb, core::system::Systems &systems)
Construct a new Map Area.
Definition: MapArea.cpp:9
component::EditMap & editMap()
Returns the contained map.
Definition: MapArea.cpp:65
void disableControls()
Disables the map controls when a dialog is opened or the map tab is inactive.
Definition: MapArea.cpp:107