Otclient  14/8/2020
mapview.h
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 #ifndef MAPVIEW_H
24 #define MAPVIEW_H
25 
26 #include "declarations.h"
31 #include "lightview.h"
32 
33 // @bindclass
34 class MapView : public LuaObject
35 {
36 public:
37  enum ViewMode {
42  };
43 
44  MapView();
45  ~MapView();
46  void draw(const Rect& rect);
47 
48 private:
49  void updateGeometry(const Size& visibleDimension, const Size& optimizedSize);
50  void updateVisibleTilesCache(int start = 0);
51  void requestVisibleTilesCacheUpdate() { m_mustUpdateVisibleTilesCache = true; }
52 
53 protected:
54  void onTileUpdate(const Position& pos);
55  void onMapCenterChange(const Position& pos);
56 
57  friend class Map;
58 
59 public:
60  // floor visibility related
61  void lockFirstVisibleFloor(int firstVisibleFloor);
63  int getLockedFirstVisibleFloor() { return m_lockedFirstVisibleFloor; }
64 
65  void setMultifloor(bool enable) { m_multifloor = enable; requestVisibleTilesCacheUpdate(); }
66  bool isMultifloor() { return m_multifloor; }
67 
68  // map dimension related
69  void setVisibleDimension(const Size& visibleDimension);
70  Size getVisibleDimension() { return m_visibleDimension; }
71  int getTileSize() { return m_tileSize; }
72  Point getVisibleCenterOffset() { return m_visibleCenterOffset; }
73  int getCachedFirstVisibleFloor() { return m_cachedFirstVisibleFloor; }
74  int getCachedLastVisibleFloor() { return m_cachedLastVisibleFloor; }
75 
76  // view mode related
77  void setViewMode(ViewMode viewMode);
78  ViewMode getViewMode() { return m_viewMode; }
79  void optimizeForSize(const Size& visibleSize);
80 
81  void setAutoViewMode(bool enable);
82  bool isAutoViewModeEnabled() { return m_autoViewMode; }
83 
84  // camera related
85  void followCreature(const CreaturePtr& creature);
86  CreaturePtr getFollowingCreature() { return m_followingCreature; }
87  bool isFollowingCreature() { return m_followingCreature && m_follow; }
88 
89  void setCameraPosition(const Position& pos);
91 
92  void setMinimumAmbientLight(float intensity) { m_minimumAmbientLight = intensity; }
93  float getMinimumAmbientLight() { return m_minimumAmbientLight; }
94 
95  // drawing related
96  void setDrawFlags(Otc::DrawFlags drawFlags) { m_drawFlags = drawFlags; requestVisibleTilesCacheUpdate(); }
97  Otc::DrawFlags getDrawFlags() { return m_drawFlags; }
98 
99  void setDrawTexts(bool enable) { m_drawTexts = enable; }
100  bool isDrawingTexts() { return m_drawTexts; }
101 
102  void setDrawNames(bool enable) { m_drawNames = enable; }
103  bool isDrawingNames() { return m_drawNames; }
104 
105  void setDrawHealthBars(bool enable) { m_drawHealthBars = enable; }
106  bool isDrawingHealthBars() { return m_drawHealthBars; }
107 
108  void setDrawLights(bool enable);
109  bool isDrawingLights() { return m_drawLights; }
110 
111  void setDrawManaBar(bool enable) { m_drawManaBar = enable; }
112  bool isDrawingManaBar() { return m_drawManaBar; }
113 
114  void move(int x, int y);
115 
116  void setAnimated(bool animated) { m_animated = animated; requestVisibleTilesCacheUpdate(); }
117  bool isAnimating() { return m_animated; }
118 
120 
121  void setShader(const PainterShaderProgramPtr& shader, float fadein, float fadeout);
122  PainterShaderProgramPtr getShader() { return m_shader; }
123 
124  Position getPosition(const Point& point, const Size& mapSize);
125 
126  MapViewPtr asMapView() { return static_self_cast<MapView>(); }
127 
128 private:
129  Rect calcFramebufferSource(const Size& destSize);
130  int calcFirstVisibleFloor();
131  int calcLastVisibleFloor();
132  Point transformPositionTo2D(const Position& position, const Position& relativePosition) {
133  return Point((m_virtualCenterOffset.x + (position.x - relativePosition.x) - (relativePosition.z - position.z)) * m_tileSize,
134  (m_virtualCenterOffset.y + (position.y - relativePosition.y) - (relativePosition.z - position.z)) * m_tileSize);
135  }
136 
137  int m_lockedFirstVisibleFloor;
138  int m_cachedFirstVisibleFloor;
139  int m_cachedLastVisibleFloor;
140  int m_tileSize;
141  int m_updateTilesPos;
142  Size m_drawDimension;
143  Size m_visibleDimension;
144  Size m_optimizedSize;
145  Point m_virtualCenterOffset;
146  Point m_visibleCenterOffset;
147  Point m_moveOffset;
148  Position m_customCameraPosition;
149  stdext::boolean<true> m_mustUpdateVisibleTilesCache;
150  stdext::boolean<true> m_mustDrawVisibleTilesCache;
151  stdext::boolean<true> m_mustCleanFramebuffer;
152  stdext::boolean<true> m_multifloor;
153  stdext::boolean<true> m_animated;
154  stdext::boolean<true> m_autoViewMode;
155  stdext::boolean<true> m_drawTexts;
156  stdext::boolean<true> m_drawNames;
157  stdext::boolean<true> m_drawHealthBars;
158  stdext::boolean<false> m_drawLights;
159  stdext::boolean<true> m_drawManaBar;
160  stdext::boolean<true> m_smooth;
161 
162  stdext::boolean<true> m_follow;
163  std::vector<TilePtr> m_cachedVisibleTiles;
164  std::vector<CreaturePtr> m_cachedFloorVisibleCreatures;
165  CreaturePtr m_followingCreature;
166  FrameBufferPtr m_framebuffer;
167  PainterShaderProgramPtr m_shader;
168  ViewMode m_viewMode;
169  Otc::DrawFlags m_drawFlags;
170  std::vector<Point> m_spiral;
171  LightViewPtr m_lightView;
172  float m_minimumAmbientLight;
173  Timer m_fadeTimer;
174  PainterShaderProgramPtr m_nextShader;
175  float m_fadeInTime;
176  float m_fadeOutTime;
177  stdext::boolean<true> m_shaderSwitchDone;
178 };
179 
180 #endif
MapView::setAnimated
void setAnimated(bool animated)
Definition: mapview.h:116
MapView::NEAR_VIEW
@ NEAR_VIEW
Definition: mapview.h:38
lightview.h
MapView::setDrawHealthBars
void setDrawHealthBars(bool enable)
Definition: mapview.h:105
Painter::BlendEquation_Add
@ BlendEquation_Add
Definition: painter.h:35
MapView::lockFirstVisibleFloor
void lockFirstVisibleFloor(int firstVisibleFloor)
Definition: mapview.cpp:492
MapView::getLockedFirstVisibleFloor
int getLockedFirstVisibleFloor()
Definition: mapview.h:63
TPoint::y
T y
Definition: point.h:83
MapView::setDrawManaBar
void setDrawManaBar(bool enable)
Definition: mapview.h:111
MapView::setMinimumAmbientLight
void setMinimumAmbientLight(float intensity)
Definition: mapview.h:92
paintershaderprogram.h
MapView::getCachedFirstVisibleFloor
int getCachedFirstVisibleFloor()
Definition: mapview.h:73
MapView::getShader
PainterShaderProgramPtr getShader()
Definition: mapview.h:122
TRect< int >
Position::x
int x
Definition: position.h:243
MapView::setMultifloor
void setMultifloor(bool enable)
Definition: mapview.h:65
MapView::isDrawingTexts
bool isDrawingTexts()
Definition: mapview.h:100
MapView::onTileUpdate
void onTileUpdate(const Position &pos)
Definition: mapview.cpp:482
MapView::setDrawFlags
void setDrawFlags(Otc::DrawFlags drawFlags)
Definition: mapview.h:96
MapView::isAutoViewModeEnabled
bool isAutoViewModeEnabled()
Definition: mapview.h:82
MapView::isMultifloor
bool isMultifloor()
Definition: mapview.h:66
MapView::getCachedLastVisibleFloor
int getCachedLastVisibleFloor()
Definition: mapview.h:74
MapView::getPosition
Position getPosition(const Point &point, const Size &mapSize)
Definition: mapview.cpp:554
MapView::FAR_VIEW
@ FAR_VIEW
Definition: mapview.h:40
Point
TPoint< int > Point
Definition: point.h:86
Position::y
int y
Definition: position.h:244
MapView::isFollowingCreature
bool isFollowingCreature()
Definition: mapview.h:87
MapView::setDrawLights
void setDrawLights(bool enable)
Definition: mapview.cpp:735
stdext::boolean< true >
luaobject.h
Position::z
short z
Definition: position.h:245
MapView::setShader
void setShader(const PainterShaderProgramPtr &shader, float fadein, float fadeout)
Definition: mapview.cpp:716
declarations.h
declarations.h
MapView::setAddLightMethod
void setAddLightMethod(bool add)
Definition: mapview.h:119
MapView::getViewMode
ViewMode getViewMode()
Definition: mapview.h:78
MapView::setVisibleDimension
void setVisibleDimension(const Size &visibleDimension)
Definition: mapview.cpp:504
MapView::setAutoViewMode
void setAutoViewMode(bool enable)
Definition: mapview.cpp:528
MapView::unlockFirstVisibleFloor
void unlockFirstVisibleFloor()
Definition: mapview.cpp:498
Position
Definition: position.h:33
MapView::MID_VIEW
@ MID_VIEW
Definition: mapview.h:39
MapView::asMapView
MapViewPtr asMapView()
Definition: mapview.h:126
LightView::setBlendEquation
void setBlendEquation(Painter::BlendEquation blendEquation)
Definition: lightview.h:48
TPoint::x
T x
Definition: point.h:83
MapView::setDrawTexts
void setDrawTexts(bool enable)
Definition: mapview.h:99
MapView::getCameraPosition
Position getCameraPosition()
Definition: mapview.cpp:708
MapView::getMinimumAmbientLight
float getMinimumAmbientLight()
Definition: mapview.h:93
MapView::setDrawNames
void setDrawNames(bool enable)
Definition: mapview.h:102
MapView::setCameraPosition
void setCameraPosition(const Position &pos)
Definition: mapview.cpp:547
MapView::getFollowingCreature
CreaturePtr getFollowingCreature()
Definition: mapview.h:86
MapView::isDrawingLights
bool isDrawingLights()
Definition: mapview.h:109
MapView::isDrawingHealthBars
bool isDrawingHealthBars()
Definition: mapview.h:106
MapView::isDrawingManaBar
bool isDrawingManaBar()
Definition: mapview.h:112
declarations.h
MapView::getDrawFlags
Otc::DrawFlags getDrawFlags()
Definition: mapview.h:97
stdext::shared_object_ptr< Creature >
MapView
Definition: mapview.h:34
Otc::DrawFlags
DrawFlags
Definition: const.h:47
MapView::MapView
MapView()
Definition: mapview.cpp:53
Timer
Definition: timer.h:28
MapView::getVisibleCenterOffset
Point getVisibleCenterOffset()
Definition: mapview.h:72
MapView::draw
void draw(const Rect &rect)
Definition: mapview.cpp:78
MapView::ViewMode
ViewMode
Definition: mapview.h:37
MapView::getTileSize
int getTileSize()
Definition: mapview.h:71
Painter::BlendEquation_Max
@ BlendEquation_Max
Definition: painter.h:36
MapView::followCreature
void followCreature(const CreaturePtr &creature)
Definition: mapview.cpp:540
MapView::optimizeForSize
void optimizeForSize(const Size &visibleSize)
Definition: mapview.cpp:535
TPoint< int >
LuaObject
LuaObject, all script-able classes have it as base.
Definition: luaobject.h:30
MapView::move
void move(int x, int y)
Definition: mapview.cpp:581
MapView::~MapView
~MapView()
Definition: mapview.cpp:71
MapView::setViewMode
void setViewMode(ViewMode viewMode)
Definition: mapview.cpp:522
TSize< int >
MapView::isDrawingNames
bool isDrawingNames()
Definition: mapview.h:103
MapView::getVisibleDimension
Size getVisibleDimension()
Definition: mapview.h:70
MapView::isAnimating
bool isAnimating()
Definition: mapview.h:117
Map
Definition: map.h:139
MapView::onMapCenterChange
void onMapCenterChange(const Position &pos)
Definition: mapview.cpp:487
MapView::HUGE_VIEW
@ HUGE_VIEW
Definition: mapview.h:41