ManaPlus
viewport.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2004-2009 The Mana World Development Team
4  * Copyright (C) 2009-2010 The Mana Developers
5  * Copyright (C) 2011-2019 The ManaPlus Developers
6  *
7  * This file is part of The ManaPlus Client.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
24 
25 #include "configuration.h"
26 #include "sdlshared.h"
27 
28 #include "gui/gui.h"
29 #include "gui/popupmanager.h"
30 
31 #include "render/graphics.h"
32 
33 #include "debug.h"
34 
35 Viewport *viewport = nullptr;
36 
37 extern volatile int tick_time;
38 
41  MouseListener(),
43  mMouseX(0),
44  mMouseY(0),
45  mMap(nullptr),
46  mHoverBeing(nullptr),
47  mHoverItem(nullptr),
48  mHoverSign(nullptr),
49  mScrollRadius(config.getIntValue("ScrollRadius")),
50  mScrollLaziness(config.getIntValue("ScrollLaziness")),
51  mScrollCenterOffsetX(config.getIntValue("ScrollCenterOffsetX")),
52  mScrollCenterOffsetY(config.getIntValue("ScrollCenterOffsetY")),
53  mMousePressX(0),
54  mMousePressY(0),
55  mPixelViewX(0),
56  mPixelViewY(0),
57  mLocalWalkTime(-1),
58  mCameraRelativeX(0),
59  mCameraRelativeY(0),
60  mShowBeingPopup(config.getBoolValue("showBeingPopup")),
61  mSelfMouseHeal(config.getBoolValue("selfMouseHeal")),
62  mEnableLazyScrolling(config.getBoolValue("enableLazyScrolling")),
63  mMouseDirectionMove(config.getBoolValue("mouseDirectionMove")),
64  mLongMouseClick(config.getBoolValue("longmouseclick")),
65  mMouseClicked(false),
66  mPlayerFollowMouse(false)
67 {
69  addMouseListener(this);
70 
71  config.addListener("ScrollLaziness", this);
72  config.addListener("ScrollRadius", this);
73  config.addListener("showBeingPopup", this);
74  config.addListener("selfMouseHeal", this);
75  config.addListener("enableLazyScrolling", this);
76  config.addListener("mouseDirectionMove", this);
77  config.addListener("longmouseclick", this);
78 
79  setFocusable(true);
80 }
81 
83 {
84  config.removeListeners(this);
86 }
87 
88 void Viewport::setMap(Map *const map A_UNUSED)
89 {
90 }
91 
92 void Viewport::draw(Graphics *const graphics)
93 {
94  BLOCK_START("Viewport::draw 1")
95  static int lastTick = tick_time;
96 
97  graphics->setColor(Color(64, 64, 64, 255));
98  graphics->fillRectangle(
99  Rect(0, 0, getWidth(), getHeight()));
100 
101  // Avoid freaking out when tick_time overflows
102  if (tick_time < lastTick)
103  lastTick = tick_time;
104 
105  // Draw contained widgets
106  WindowContainer::draw(graphics);
107  BLOCK_END("Viewport::draw 1")
108 }
109 
110 void Viewport::safeDraw(Graphics *const graphics)
111 {
112  Viewport::draw(graphics);
113 }
114 
116 {
117  BLOCK_START("Viewport::logic")
118  // Make the player follow the mouse position
119  // if the mouse is dragged elsewhere than in a window.
121  BLOCK_END("Viewport::logic")
122 }
123 
125 {
126 }
127 
129 {
130 }
131 
133 {
134  return false;
135 }
136 
138 {
139  if (event.getSource() != this || event.isConsumed())
140  return;
141 
142  mMouseClicked = true;
143 
144  mMousePressX = event.getX();
145  mMousePressY = event.getY();
146  const MouseButtonT eventButton = event.getButton();
147 
148  // Right click might open a popup
149  if (eventButton == MouseButton::RIGHT)
150  {
151  if (openContextMenu(event))
152  return;
153  }
154 
155  // If a popup is active, just remove it
157  {
158  mPlayerFollowMouse = false;
160  return;
161  }
162 
163  // Left click can cause different actions
164  if (!mLongMouseClick && eventButton == MouseButton::LEFT)
165  {
166  if (leftMouseAction())
167  {
168  mPlayerFollowMouse = false;
169  return;
170  }
171  }
172 }
173 
175 {
176 }
177 
179 {
180  if (event.getSource() != this || event.isConsumed())
181  {
182  mPlayerFollowMouse = false;
183  return;
184  }
185  if (mMouseClicked)
186  {
187  if (abs(event.getX() - mMousePressX) > 32
188  || abs(event.getY() - mMousePressY) > 32)
189  {
190  mPlayerFollowMouse = true;
191  }
192  }
193 
194  walkByMouse(event);
195 }
196 
198 {
199  mPlayerFollowMouse = false;
200  mLocalWalkTime = -1;
202  {
203  mMouseClicked = false;
204  if (event.getSource() != this || event.isConsumed())
205  return;
206  const MouseButtonT eventButton = event.getButton();
207  if (eventButton == MouseButton::LEFT)
208  {
209  // long button press
210  if ((gui != nullptr) && gui->isLongPress())
211  {
212  if (openContextMenu(event))
213  {
214  gui->resetClickCount();
215  return;
216  }
217  }
218  else
219  {
220  if (leftMouseAction())
221  return;
222  }
223  walkByMouse(event);
224  }
225  }
226 }
227 
228 void Viewport::optionChanged(const std::string &name)
229 {
230  if (name == "ScrollLaziness")
231  mScrollLaziness = config.getIntValue("ScrollLaziness");
232  else if (name == "ScrollRadius")
233  mScrollRadius = config.getIntValue("ScrollRadius");
234  else if (name == "showBeingPopup")
235  mShowBeingPopup = config.getBoolValue("showBeingPopup");
236  else if (name == "selfMouseHeal")
237  mSelfMouseHeal = config.getBoolValue("selfMouseHeal");
238  else if (name == "enableLazyScrolling")
239  mEnableLazyScrolling = config.getBoolValue("enableLazyScrolling");
240  else if (name == "mouseDirectionMove")
241  mMouseDirectionMove = config.getBoolValue("mouseDirectionMove");
242  else if (name == "longmouseclick")
243  mLongMouseClick = config.getBoolValue("longmouseclick");
244 }
245 
247 {
248 }
249 
251 {
252 }
253 
255 {
256 }
257 
259 {
260  mHoverBeing = nullptr;
261  mHoverItem = nullptr;
262  mHoverSign = nullptr;
263 }
264 
266 {
267  return false;
268 }
269 
271 {
272 }
ConfigListener
Definition: configlistener.h:36
CHECKLISTENERS
#define CHECKLISTENERS
Definition: localconsts.h:276
Viewport::mLongMouseClick
bool mLongMouseClick
Definition: viewport.h:206
tick_time
volatile int tick_time
Definition: timer.cpp:52
MouseButton::RIGHT
@ RIGHT
Definition: mousebutton.h:74
viewport.h
Viewport::mScrollRadius
int mScrollRadius
Definition: viewport.h:188
Viewport::mouseReleased
void mouseReleased(MouseEvent &event)
Definition: viewport.cpp:197
Viewport::~Viewport
~Viewport()
Definition: viewport.cpp:82
Viewport::safeDraw
void safeDraw(Graphics *const graphics)
Definition: viewport.cpp:110
Viewport::mScrollLaziness
int mScrollLaziness
Definition: viewport.h:189
Viewport::mMousePressY
int mMousePressY
Definition: viewport.h:193
Viewport
Definition: viewport.h:43
Viewport::mShowBeingPopup
bool mShowBeingPopup
Definition: viewport.h:202
Viewport::mMouseClicked
bool mMouseClicked
Definition: viewport.h:207
BasicContainer2::draw
void draw(Graphics *const graphics)
Definition: basiccontainer2.cpp:84
A_UNUSED
#define A_UNUSED
Definition: localconsts.h:159
Viewport::mouseDragged
void mouseDragged(MouseEvent &event)
Definition: viewport.cpp:178
Graphics
Definition: graphics.h:107
Viewport::drawDebugPath
void drawDebugPath(Graphics *const graphics)
Definition: viewport.cpp:128
Viewport::mMouseY
int mMouseY
Definition: viewport.h:154
Viewport::toggleCameraMode
void toggleCameraMode()
Definition: viewport.cpp:254
Configuration::addListener
void addListener(const std::string &key, ConfigListener *const listener)
Definition: configuration.cpp:905
Viewport::toggleMapDrawType
void toggleMapDrawType()
Definition: viewport.cpp:250
MouseEvent::getY
int getY() const
Definition: mouseevent.h:137
Gui::isLongPress
bool isLongPress() const
Definition: gui.h:308
Event::getSource
Widget * getSource() const
Definition: event.h:103
Viewport::setMap
void setMap(Map *const map)
Definition: viewport.cpp:88
nullptr
#define nullptr
Definition: localconsts.h:44
Viewport::mEnableLazyScrolling
bool mEnableLazyScrolling
Definition: viewport.h:204
InputGuiEvent::isConsumed
bool isConsumed() const
Definition: inputguievent.h:109
Viewport::mLocalWalkTime
int mLocalWalkTime
Definition: viewport.h:197
Widget::addMouseListener
void addMouseListener(MouseListener *const mouseListener)
Definition: widget.cpp:291
Viewport::mHoverSign
MapItem * mHoverSign
Definition: viewport.h:186
Gui::resetClickCount
void resetClickCount()
Definition: gui.cpp:979
gui
Gui * gui
Definition: gui.cpp:110
Viewport::mMouseX
int mMouseX
Definition: viewport.h:153
MouseEvent::getX
int getX() const
Definition: mouseevent.h:126
Widget::getWidth
int getWidth() const
Definition: widget.h:220
Viewport::mSelfMouseHeal
bool mSelfMouseHeal
Definition: viewport.h:203
WindowContainer
Definition: windowcontainer.h:34
Viewport::mouseMoved
void mouseMoved(MouseEvent &event)
Definition: viewport.cpp:246
Gui::getMouseState
static uint8_t getMouseState(int &x, int &y)
Definition: gui.cpp:1170
Opaque_false
const bool Opaque_false
Definition: opaque.h:29
Configuration::getIntValue
int getIntValue(const std::string &key) const
Definition: configuration.cpp:403
sdlshared.h
Widget::getHeight
int getHeight() const
Definition: widget.h:239
viewport
Viewport * viewport
Definition: viewport.cpp:35
Viewport::mMousePressX
int mMousePressX
Definition: viewport.h:192
Viewport::mPlayerFollowMouse
bool mPlayerFollowMouse
Definition: viewport.h:208
Viewport::leftMouseAction
bool leftMouseAction()
Definition: viewport.cpp:265
config
Configuration config
Definition: configuration.cpp:51
MouseListener
Definition: mouselistener.h:76
Graphics::setColor
virtual void setColor(const Color &color)
Definition: graphics.h:319
Configuration::removeListeners
void removeListeners(ConfigListener *const listener)
Definition: configuration.cpp:938
PopupManager::hidePopupMenu
static void hidePopupMenu()
Definition: popupmanager.cpp:68
Viewport::mousePressed
void mousePressed(MouseEvent &event)
Definition: viewport.cpp:137
gui.h
Rect
Definition: rect.h:72
Viewport::logic
void logic()
Definition: viewport.cpp:115
MouseButtonT
MouseButton ::T MouseButtonT
Definition: mousebutton.h:77
Viewport::mHoverItem
FloorItem * mHoverItem
Definition: viewport.h:185
popupmanager.h
configuration.h
Viewport::optionChanged
void optionChanged(const std::string &name)
Definition: viewport.cpp:228
Viewport::videoResized
void videoResized()
Definition: viewport.cpp:270
Map
Definition: map.h:71
Viewport::cleanHoverItems
void cleanHoverItems()
Definition: viewport.cpp:258
BLOCK_START
#define BLOCK_START(name)
Definition: perfomance.h:78
Widget::setFocusable
void setFocusable(const bool focusable)
Definition: widget.cpp:191
BLOCK_END
#define BLOCK_END(name)
Definition: perfomance.h:79
Ea::GameRecv::mMap
std::string mMap
Definition: gamerecv.cpp:45
Graphics::fillRectangle
virtual void fillRectangle(const Rect &rectangle)=0
Viewport::walkByMouse
void walkByMouse(const MouseEvent &event)
Definition: viewport.cpp:174
Viewport::mMouseDirectionMove
bool mMouseDirectionMove
Definition: viewport.h:205
BasicContainer2::setOpaque
void setOpaque(Opaque opaque)
Definition: basiccontainer2.h:109
Viewport::draw
void draw(Graphics *const graphics)
Definition: viewport.cpp:92
MouseEvent
Definition: mouseevent.h:79
Color
Definition: color.h:74
Viewport::mHoverBeing
Being * mHoverBeing
Definition: viewport.h:184
Viewport::followMouse
void followMouse()
Definition: viewport.cpp:124
Viewport::Viewport
Viewport()
Definition: viewport.cpp:39
debug.h
Viewport::openContextMenu
bool openContextMenu(const MouseEvent &event)
Definition: viewport.cpp:132
PopupManager::isPopupMenuVisible
static bool isPopupMenuVisible()
Definition: popupmanager.cpp:84
Configuration::getBoolValue
bool getBoolValue(const std::string &key) const
Definition: configuration.cpp:596
MouseButton::LEFT
@ LEFT
Definition: mousebutton.h:73