TDME2  1.9.200
DraggingScreenController.cpp
Go to the documentation of this file.
2 
3 #include <memory>
4 #include <string>
5 
6 #include <tdme/tdme.h>
7 #include <tdme/engine/Engine.h>
14 #include <tdme/gui/GUI.h>
15 #include <tdme/gui/GUIParser.h>
16 #include <tdme/math/Math.h>
17 #include <tdme/utilities/Action.h>
18 #include <tdme/utilities/Console.h>
22 
24 
25 using std::string;
26 using std::to_string;
27 using std::unordered_map;
28 using std::unique_ptr;
29 
40 using tdme::math::Math;
46 
47 DraggingScreenController::DraggingScreenController()
48 {
49 }
50 
52 {
53  screenNode = nullptr;
54 }
55 
57 {
58  return screenNode;
59 }
60 
62 {
63  try {
64  screenNode = GUIParser::parse("resources/engine/gui", "popup_dragging.xml");
66  screenNode->setEnabled(false);
67  draggableNode = required_dynamic_cast<GUIParentNode*>(screenNode->getNodeById("draggable"));
68  } catch (Exception& exception) {
69  Console::println("DraggingScreenController::initialize(): An error occurred: " + string(exception.what()));
70  }
71 }
72 
74 {
75 }
76 
78  return true;
79 }
80 
82 }
83 
84 void DraggingScreenController::onRelease(GUINode* node, int mouseX, int mouseY) {
85  dragReleaseMouseX = mouseX;
86  dragReleaseMouseY = mouseY;
87  close();
88  if (onReleaseAction != nullptr) {
89  onReleaseAction->performAction();
90  onReleaseAction = nullptr;
91  }
92 }
93 
94 void DraggingScreenController::start(int mouseX, int mouseY, const string& xml, const string& payload, Action* onReleaseAction)
95 {
96  this->payload = payload;
97  this->onReleaseAction = unique_ptr<Action>(onReleaseAction);
98  dragReleaseMouseX = -1;
99  dragReleaseMouseY = -1;
100  //
101  try {
102  draggableNode->replaceSubNodes(xml, true);
103  } catch (Exception& exception) {
104  Console::println("DraggingScreenController::start(): An error occurred: " + string(exception.what()));
105  }
106  auto scaledMouseX = Engine::getInstance()->getGUI()->getScaledX(screenNode, mouseX);
107  auto scaledMouseY = Engine::getInstance()->getGUI()->getScaledY(screenNode, mouseY);
108  auto scaledX = Engine::getInstance()->getGUI()->getScaledX(screenNode, mouseX - draggableNode->getContentWidth() / 2);
109  auto scaledY = Engine::getInstance()->getGUI()->getScaledY(screenNode, mouseY - draggableNode->getContentHeight() / 2);
110  scaledX = Math::min(scaledX, screenNode->getScreenWidth() - draggableNode->getContentWidth());
111  scaledY = Math::min(scaledY, screenNode->getScreenHeight() - draggableNode->getContentHeight());
112  draggableNode->getRequestsConstraints().leftType = GUINode_RequestedConstraints_RequestedConstraintsType::PIXEL;
114  draggableNode->getRequestsConstraints().topType = GUINode_RequestedConstraints_RequestedConstraintsType::PIXEL;
116  screenNode->setEnabled(true);
117  screenNode->layout();
118  //
119  Engine::getInstance()->getGUI()->startMouseDragging(draggableNode);
120  //
121  try {
122  ((GUIMoveableController*)(draggableNode->getController()))->startMoving(scaledMouseX, scaledMouseY);
123  } catch (Exception& exception) {
124  Console::println("DraggingScreenController::start(): An error occurred: " + string(exception.what()));
125  }
126 }
127 
129 {
130  screenNode->setEnabled(false);
131 }
Engine main class.
Definition: Engine.h:131
GUI parser.
Definition: GUIParser.h:40
GUI node base class.
Definition: GUINode.h:64
virtual int getContentHeight()=0
GUINode_RequestedConstraints & getRequestsConstraints()
Definition: GUINode.h:405
virtual int getContentWidth()=0
GUINodeController * getController()
Definition: GUINode.h:661
GUI parent node base class thats supporting child nodes.
Definition: GUIParentNode.h:42
void replaceSubNodes(const string &xml, bool resetScrollOffsets)
Replace sub nodes with given XML.
GUI screen node that represents a screen that can be rendered via GUI system.
Definition: GUIScreenNode.h:72
void setEnabled(bool enabled)
Set enabled.
void addMoveListener(GUIMoveListener *listener)
Add move listener.
void layout() override
Layout.
GUINode * getNodeById(const string &nodeId)
Get GUI node by id.
Standard math functions.
Definition: Math.h:19
void start(int mouseX, int mouseY, const string &xml, const string &payload, Action *onReleaseAction)
Show dragging screen and a dragging image from given source.
void onRelease(GUINode *node, int mouseX, int mouseY) override
On release.
Console class.
Definition: Console.h:29
Mutable utf8 aware string class.
Definition: MutableString.h:23
String tools class.
Definition: StringTools.h:22
std::exception Exception
Exception base class.
Definition: Exception.h:18
GUINode_RequestedConstraints_RequestedConstraintsType * topType
GUINode_RequestedConstraints_RequestedConstraintsType * leftType
Action Interface.
Definition: Action.h:11