TDME2  1.9.200
ContextMenuScreenController.cpp
Go to the documentation of this file.
2 
3 #include <string>
4 #include <unordered_map>
5 
6 #include <tdme/tdme.h>
7 #include <tdme/engine/Engine.h>
14 #include <tdme/gui/nodes/GUINode.h>
20 #include <tdme/gui/GUI.h>
21 #include <tdme/gui/GUIParser.h>
22 #include <tdme/math/Math.h>
25 #include <tdme/utilities/Console.h>
30 
32 
33 using std::string;
34 using std::unordered_map;
35 
51 using tdme::math::Math;
59 
60 ContextMenuScreenController::ContextMenuScreenController(PopUps* popUps): popUps(popUps)
61 {
62 }
63 
64 ContextMenuScreenController::~ContextMenuScreenController()
65 {
66  for (const auto& [actionId, action]: actions) delete action;
67  actions.clear();
68  screenNode = nullptr;
69 }
70 
72 {
73  return screenNode;
74 }
75 
77 {
78  try {
79  screenNode = GUIParser::parse("resources/engine/gui", "popup_contextmenu.xml");
80  screenNode->setEnabled(false);
86  contextMenuNode = required_dynamic_cast<GUIElementNode*>(screenNode->getNodeById("contextmenu"));
87  tscriptMethods.load("resources/engine/code-completion/", "tscript-methods.properties");
88  } catch (Exception& exception) {
89  Console::println("ContextMenuScreenController::initialize(): An error occurred: " + string(exception.what()));
90  }
91 }
92 
94 {
95 }
96 
97 void ContextMenuScreenController::show(int mouseX, int mouseY)
98 {
99  auto x = static_cast<int>((float)mouseX * (float)screenNode->getScreenWidth() / (float)Engine::getInstance()->getGUI()->getWidth());
100  auto y = static_cast<int>((float)mouseY * (float)screenNode->getScreenHeight() / (float)Engine::getInstance()->getGUI()->getHeight());
103  contextMenuNode->getRequestsConstraints().leftType = GUINode_RequestedConstraints_RequestedConstraintsType::PIXEL;
105  contextMenuNode->getRequestsConstraints().topType = GUINode_RequestedConstraints_RequestedConstraintsType::PIXEL;
107  screenNode->setEnabled(true);
108  screenNode->layout();
109  Engine::getInstance()->getGUI()->setFoccussedNode(contextMenuNode);
110 }
111 
113 {
114  screenNode->setEnabled(false);
115 }
116 
118 {
119  if (type == GUIActionListenerType::PERFORMED) {
120  auto actionIt = actions.find(node->getId());
121  if (actionIt != actions.end()) {
122  close();
123  if (actionIt->second != nullptr) actionIt->second->performAction();
124  } else
125  if (StringTools::startsWith(node->getValue(), "miniscript.method.") == true) {
126  close();
127  if (miniScriptMethodSelectionListener != nullptr) {
128  miniScriptMethodSelectionListener->onMethodSelection(StringTools::substring(node->getValue(), string("miniscript.method.").size()));
129  }
130  }
131  }
132 }
133 
135  if (node->getId() == "context_menu_addnode_search") {
136  //
137  required_dynamic_cast<GUIParentNode*>(screenNode->getInnerNodeById("context_menu_addnode_list"))->clearSubNodes();
138  //
139  auto searchValue = StringTools::toLowerCase(node->getController()->getValue().getString());
140  const auto& properties = tscriptMethods.getProperties();
141  for (const auto& [methodNameCandidate, methodDescription]: properties) {
142  auto methodName = string("unknown");
143  if (StringTools::startsWith(methodNameCandidate, "miniscript.basemethod.") == true) {
144  methodName = StringTools::substring(methodNameCandidate, string("miniscript.basemethod.").size());
145  } else
146  if (StringTools::startsWith(methodNameCandidate, "miniscript.logicmethod.") == true) {
147  methodName = StringTools::substring(methodNameCandidate, string("miniscript.logicmethod.").size());
148  } else
149  if (StringTools::startsWith(methodNameCandidate, "miniscript.") == true) {
150  methodName = StringTools::substring(methodNameCandidate, string("miniscript.").size());
151  }
152  if (StringTools::toLowerCase(methodName).find(searchValue) != string::npos) {
153  required_dynamic_cast<GUIParentNode*>(screenNode->getInnerNodeById("context_menu_addnode_list"))->addSubNodes(
154  "<context-menu-item value=\"miniscript.method." + GUIParser::escape(methodName) + "\" template=\"context-menu-item_template_addnode.xml\" category=\"" + GUIParser::escape(methodName) + "\" name=\"" + GUIParser::escape(methodDescription) + "\" />",
155  true
156  );
157  }
158  }
159  }
160 }
161 
163  GUI::setDisableTabFocusControl(node->getScreenNode() != screenNode);
164 }
165 
167 }
168 
171  required_dynamic_cast<GUIParentNode*>(screenNode->getInnerNodeById(contextMenuNode->getId()))->clearSubNodes();
172  for (const auto& [actionId, action]: actions) delete action;
173  actions.clear();
174 }
175 
177  required_dynamic_cast<GUIParentNode*>(screenNode->getInnerNodeById(contextMenuNode->getId()))->addSubNodes(
178  "<template src=\"resources/engine/gui/template_visualcode_addnodemenu.xml\" />",
179  true
180  );
181  //
182  required_dynamic_cast<GUIParentNode*>(screenNode->getInnerNodeById("context_menu_addnode_list"))->clearSubNodes();
183  //
184  const auto& properties = tscriptMethods.getProperties();
185  for (const auto& [methodNameCandidate, methodDescription]: properties) {
186  auto methodName = string("unknown");
187  if (StringTools::startsWith(methodNameCandidate, "miniscript.basemethod.") == true) {
188  methodName = StringTools::substring(methodNameCandidate, string("miniscript.basemethod.").size());
189  } else
190  if (StringTools::startsWith(methodNameCandidate, "miniscript.logicmethod.") == true) {
191  methodName = StringTools::substring(methodNameCandidate, string("miniscript.logicmethod.").size());
192  } else
193  if (StringTools::startsWith(methodNameCandidate, "miniscript.") == true) {
194  methodName = StringTools::substring(methodNameCandidate, string("miniscript.").size());
195  }
196  required_dynamic_cast<GUIParentNode*>(screenNode->getInnerNodeById("context_menu_addnode_list"))->addSubNodes(
197  "<context-menu-item value=\"miniscript.method." + GUIParser::escape(methodName) + "\" template=\"context-menu-item_template_addnode.xml\" category=\"" + GUIParser::escape(methodName) + "\" name=\"" + GUIParser::escape(methodDescription) + "\" />",
198  true
199  );
200  }
201 }
202 
203 void ContextMenuScreenController::addMenuItem(const string& text, const string& id, Action* action) {
204  required_dynamic_cast<GUIParentNode*>(screenNode->getInnerNodeById(contextMenuNode->getId()))->addSubNodes(
205  "<context-menu-item text=\"" + GUIParser::escape(text) + "\" id=\"" + GUIParser::escape(id) + "\" />",
206  true
207  );
208  auto actionIt = actions.find(id);
209  if (actionIt != actions.end() && actionIt->second != nullptr) delete actionIt->second;
210  if (action != nullptr) actions[id] = action;
211 }
212 
214  required_dynamic_cast<GUIParentNode*>(screenNode->getInnerNodeById(contextMenuNode->getId()))->addSubNodes(
215  "<menu-separator />",
216  true
217  );
218 }
219 
220 void ContextMenuScreenController::onTooltipShowRequest(GUINode* node, int mouseX, int mouseY) {
221  popUps->getTooltipScreenController()->show(mouseX, mouseY, node->getToolTip());
222 }
223 
226 }
227 
229  const auto& mouseEvents = Engine::getInstance()->getGUI()->getMouseEvents();
230  const auto& keyboardEvents = Engine::getInstance()->getGUI()->getKeyboardEvents();
231  for (const auto& event: mouseEvents) {
232  if (event.isProcessed() == true) continue;
233  if (event.getType() == GUIMouseEvent::MOUSEEVENT_RELEASED &&
234  (event.getButton() == MOUSE_BUTTON_LEFT ||
235  event.getButton() == MOUSE_BUTTON_MIDDLE ||
236  event.getButton() == MOUSE_BUTTON_RIGHT)) {
237  close();
238  return;
239  }
240  }
241  for (const auto& event: keyboardEvents) {
242  if (event.isProcessed() == true) continue;
243  if (event.getKeyCode() == GUIKeyboardEvent::KEYCODE_ESCAPE) {
244  close();
245  return;
246  }
247  }
248 }
#define MOUSE_BUTTON_LEFT
#define MOUSE_BUTTON_MIDDLE
#define MOUSE_BUTTON_RIGHT
Engine main class.
Definition: Engine.h:131
GUI parser.
Definition: GUIParser.h:40
GUI node controller base class.
virtual const MutableString & getValue()=0
GUI node base class.
Definition: GUINode.h:64
GUINode_RequestedConstraints & getRequestsConstraints()
Definition: GUINode.h:405
const string & getToolTip()
Definition: GUINode.h:346
GUINodeController * getController()
Definition: GUINode.h:661
GUIScreenNode * getScreenNode()
Definition: GUINode.h:325
const string & getId()
Definition: GUINode.h:339
GUINode_ComputedConstraints & getComputedConstraints()
Definition: GUINode.h:412
GUI parent node base class thats supporting child nodes.
Definition: GUIParentNode.h:42
GUI screen node that represents a screen that can be rendered via GUI system.
Definition: GUIScreenNode.h:72
void setInputEventHandler(GUIInputEventHandler *inputEventHandler)
Set input event handler.
void setEnabled(bool enabled)
Set enabled.
void addChangeListener(GUIChangeListener *listener)
Add change listener.
void addTooltipRequestListener(GUITooltipRequestListener *listener)
Add tooltip request listener.
void layout() override
Layout.
void addActionListener(GUIActionListener *listener)
Add action listener.
void addFocusListener(GUIFocusListener *listener)
Add focus listener.
GUINode * getInnerNodeById(const string &nodeId)
Get inner GUI node by id.
GUINode * getNodeById(const string &nodeId)
Get GUI node by id.
Standard math functions.
Definition: Math.h:19
void show(int mouseX, int mouseY)
Shows the pop up.
void setupVisualCodeAddNodeContextMenu()
Clear and setup visual code add node context menu.
void onChange(GUIElementNode *node) override
On change.
void handleInputEvents() override
Handle input events that have not yet been processed.
void onUnfocus(GUIElementNode *node) override
On unfocus.
void onTooltipCloseRequest() override
On tooltip close request.
void onFocus(GUIElementNode *node) override
On focus.
void onAction(GUIActionListenerType type, GUIElementNode *node) override
void addMenuItem(const string &text, const string &id, Action *action=nullptr)
Add menu item.
void onTooltipShowRequest(GUINode *node, int mouseX, int mouseY) override
On tooltip show request.
void show(int mouseX, int mouseY, const string &tooltip)
Show tooltip.
Pop ups controller accessor class.
Definition: PopUps.h:29
TooltipScreenController * getTooltipScreenController()
Definition: PopUps.h:131
Console class.
Definition: Console.h:29
Mutable utf8 aware string class.
Definition: MutableString.h:23
const string & getString() const
Properties class, which helps out with storeing or loading key value pairs from/to property files.
Definition: Properties.h:23
const unordered_map< string, string > & getProperties()
Definition: Properties.h:83
void load(const string &pathName, const string &fileName, FileSystemInterface *fileSystem=nullptr)
Load property file.
Definition: Properties.cpp:24
String tools class.
Definition: StringTools.h:22
std::exception Exception
Exception base class.
Definition: Exception.h:18
GUI action listener interface.
GUI change listener interface.
GUI focus listener interface.
GUI input event handler interface.
GUINode_RequestedConstraints_RequestedConstraintsType * topType
GUINode_RequestedConstraints_RequestedConstraintsType * leftType
Action Interface.
Definition: Action.h:11