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