Otclient  14/8/2020
mouse.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #include "mouse.h"
24 #include <framework/ui/uiwidget.h>
27 
29 
31 {
32 }
33 
35 {
36  m_cursors.clear();
37 }
38 
39 void Mouse::loadCursors(std::string filename)
40 {
41  filename = g_resources.guessFilePath(filename, "otml");
42  try {
43  OTMLDocumentPtr doc = OTMLDocument::parse(filename);
44  OTMLNodePtr cursorsNode = doc->at("Cursors");
45 
46  for(const OTMLNodePtr& cursorNode : cursorsNode->children())
47  addCursor(cursorNode->tag(),
48  stdext::resolve_path(cursorNode->valueAt("image"), cursorNode->source()),
49  cursorNode->valueAt<Point>("hot-spot"));
50  } catch(stdext::exception& e) {
51  g_logger.error(stdext::format("unable to load cursors file: %s", e.what()));
52  }
53 }
54 
55 void Mouse::addCursor(const std::string& name, const std::string& file, const Point& hotSpot)
56 {
57  int cursorId = g_window.loadMouseCursor(file, hotSpot);
58  if(cursorId >= 0) {
59  m_cursors[name] = cursorId;
60  } else
61  g_logger.error(stdext::format("unable to load cursor %s", name));
62 }
63 
64 bool Mouse::pushCursor(const std::string& name)
65 {
66  auto it = m_cursors.find(name);
67  if(it == m_cursors.end())
68  return false;
69 
70  int cursorId = it->second;
71  g_window.setMouseCursor(cursorId);
72  m_cursorStack.push_back(cursorId);
73  return true;
74 }
75 
76 void Mouse::popCursor(const std::string& name)
77 {
78  if(m_cursorStack.empty())
79  return;
80 
81  if(name.empty() || m_cursors.find(name) == m_cursors.end())
82  m_cursorStack.pop_back();
83  else {
84  int cursorId = m_cursors[name];
85  int index = -1;
86  for(uint i=0;i<m_cursorStack.size();++i) {
87  if(m_cursorStack[i] == cursorId)
88  index = i;
89  }
90  if(index >= 0)
91  m_cursorStack.erase(m_cursorStack.begin() + index);
92  else
93  return;
94  }
95 
96  if(!m_cursorStack.empty())
97  g_window.setMouseCursor(m_cursorStack.back());
98  else
100 }
101 
103 {
104  return !m_cursorStack.empty();
105 }
106 
108 {
109  return g_window.isMouseButtonPressed(mouseButton);
110 }
111 
112 void Mouse::checkStackSize()
113 {
114  if(m_cursorStack.size() > 5) {
115  g_logger.error("mouse cursor stack is too long");
116  m_cursorStack.pop_front();
117  }
118 }
Mouse::popCursor
void popCursor(const std::string &name)
Definition: mouse.cpp:76
Mouse::pushCursor
bool pushCursor(const std::string &name)
Definition: mouse.cpp:64
PlatformWindow::restoreMouseCursor
virtual void restoreMouseCursor()=0
Mouse
Definition: mouse.h:25
platformwindow.h
OTMLDocument::parse
static OTMLDocumentPtr parse(const std::string &fileName)
Parse OTML from a file.
Definition: otmldocument.cpp:36
resourcemanager.h
Logger::error
void error(const std::string &what)
Definition: logger.h:54
OTMLNode::children
OTMLNodeList children()
Definition: otmlnode.cpp:170
uiwidget.h
stdext::format
std::string format()
Definition: format.h:82
Mouse::addCursor
void addCursor(const std::string &name, const std::string &file, const Point &hotSpot)
Definition: mouse.cpp:55
g_resources
ResourceManager g_resources
Definition: resourcemanager.cpp:32
stdext::resolve_path
std::string resolve_path(const std::string &filePath, std::string sourcePath)
Resolve a file path by combining sourcePath with filePath.
Definition: string.cpp:35
g_window
PlatformWindow & g_window
Definition: platformwindow.cpp:37
g_mouse
Mouse g_mouse
Definition: mouse.cpp:28
uint
unsigned int uint
Definition: types.h:31
g_logger
Logger g_logger
Definition: logger.cpp:35
mouse.h
Mouse::isCursorChanged
bool isCursorChanged()
Definition: mouse.cpp:102
PlatformWindow::setMouseCursor
virtual void setMouseCursor(int cursorId)=0
stdext::exception::what
virtual const char * what() const
Definition: exception.h:37
Mouse::terminate
void terminate()
Definition: mouse.cpp:34
stdext::shared_object_ptr< OTMLDocument >
Mouse::init
void init()
Definition: mouse.cpp:30
PlatformWindow::loadMouseCursor
int loadMouseCursor(const std::string &file, const Point &hotSpot)
Definition: platformwindow.cpp:39
Mouse::isPressed
bool isPressed(Fw::MouseButton mouseButton)
Definition: mouse.cpp:107
TPoint< int >
Mouse::loadCursors
void loadCursors(std::string filename)
Definition: mouse.cpp:39
PlatformWindow::isMouseButtonPressed
bool isMouseButtonPressed(Fw::MouseButton mouseButton)
Definition: platformwindow.h:87
Fw::MouseButton
MouseButton
Definition: const.h:246
OTMLNode::at
OTMLNodePtr at(const std::string &childTag)
Definition: otmlnode.cpp:70
ResourceManager::guessFilePath
std::string guessFilePath(const std::string &filename, const std::string &type)
Definition: resourcemanager.cpp:352
stdext::exception
Definition: exception.h:31