1/*
2 * Copyright (c) 2010-2017 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"
27#include <framework/graphics/paintershaderprogram.h>
28#include <framework/graphics/declarations.h>
29#include <framework/luaengine/luaobject.h>
30#include <framework/core/declarations.h>
31#include "lightview.h"
32
33// @bindclass
34class MapView : public LuaObject
35{
36public:
37 MapView();
38 ~MapView();
39 void drawMapBackground(const Rect& rect, const TilePtr& crosshairTile = nullptr);
40 void drawMapForeground(const Rect& rect);
41
42private:
43 void drawFloor(short floor, const Position& cameraPosition, const TilePtr& crosshairTile = nullptr);
44 void drawTileTexts(const Rect& rect, const Rect& srcRect);
45 void drawTileWidget(const Rect& rect, const Rect& srcRect);
46 void updateGeometry(const Size& visibleDimension, const Size& optimizedSize);
47 void updateVisibleTilesCache();
48 void requestVisibleTilesCacheUpdate() { m_mustUpdateVisibleTilesCache = true; }
49
50protected:
51 void onTileUpdate(const Position& pos);
52 void onMapCenterChange(const Position& pos);
53
54 friend class Map;
55
56public:
57 // floor visibility related
58 void lockFirstVisibleFloor(int firstVisibleFloor);
59 void unlockFirstVisibleFloor();
60 int getLockedFirstVisibleFloor() { return m_lockedFirstVisibleFloor; }
61
62 void setMultifloor(bool enable) { m_multifloor = enable; requestVisibleTilesCacheUpdate(); }
63 bool isMultifloor() { return m_multifloor; }
64
65 // map dimension related
66 void setVisibleDimension(const Size& visibleDimension);
67 void optimizeForSize(const Size & visibleSize);
68 Size getVisibleDimension() { return m_visibleDimension; }
69 Point getVisibleCenterOffset() { return m_visibleCenterOffset; }
70 int getCachedFirstVisibleFloor() { return m_cachedFirstVisibleFloor; }
71 int getCachedLastVisibleFloor() { return m_cachedLastVisibleFloor; }
72
73 // camera related
74 void followCreature(const CreaturePtr& creature);
75 CreaturePtr getFollowingCreature() { return m_followingCreature; }
76 bool isFollowingCreature() { return m_followingCreature && m_follow; }
77
78 void setCameraPosition(const Position& pos);
79 Position getCameraPosition();
80
81 void setMinimumAmbientLight(float intensity) { m_minimumAmbientLight = intensity; }
82 float getMinimumAmbientLight() { return m_minimumAmbientLight; }
83
84 // drawing related
85 void setDrawFlags(Otc::DrawFlags drawFlags) { m_drawFlags = drawFlags; requestVisibleTilesCacheUpdate(); }
86 Otc::DrawFlags getDrawFlags() { return m_drawFlags; }
87
88 void setDrawTexts(bool enable) { m_drawTexts = enable; }
89 bool isDrawingTexts() { return m_drawTexts; }
90
91 void setDrawNames(bool enable) { m_drawNames = enable; }
92 bool isDrawingNames() { return m_drawNames; }
93
94 void setDrawHealthBars(bool enable) { m_drawHealthBars = enable; }
95 bool isDrawingHealthBars() { return m_drawHealthBars; }
96
97 void setDrawHealthBarsOnTop(bool enable) { m_drawHealthBarsOnTop = enable; }
98 bool isDrawingHealthBarsOnTop() { return m_drawHealthBarsOnTop; }
99
100 void setDrawLights(bool enable);
101 bool isDrawingLights() { return m_drawLight; }
102
103 void setDrawManaBar(bool enable) { m_drawManaBar = enable; }
104 bool isDrawingManaBar() { return m_drawManaBar; }
105
106 void setDrawPlayerBars(bool enable) { m_drawPlayerBars = enable; }
107
108 void move(int x, int y);
109
110 void setAnimated(bool animated) { m_animated = animated; requestVisibleTilesCacheUpdate(); }
111 bool isAnimating() { return m_animated; }
112
113 void setFloorFading(int value) { m_floorFading = value; }
114 void setCrosshair(const std::string& file);
115
116 //void setShader(const PainterShaderProgramPtr& shader, float fadein, float fadeout);
117 //PainterShaderProgramPtr getShader() { return m_shader; }
118
119 Position getPosition(const Point& point, const Size& mapSize);
120
121 Point getPositionOffset(const Point& point, const Size& mapSize);
122
123 MapViewPtr asMapView() { return static_self_cast<MapView>(); }
124
125private:
126 Rect calcFramebufferSource(const Size& destSize, bool inNextFrame = false);
127 int calcFirstVisibleFloor(bool forFading = false);
128 int calcLastVisibleFloor();
129 Point transformPositionTo2D(const Position& position, const Position& relativePosition);
130
131 stdext::timer m_mapRenderTimer;
132
133 int m_lockedFirstVisibleFloor;
134 int m_cachedFirstVisibleFloor;
135 int m_cachedFirstFadingFloor;
136 int m_cachedLastVisibleFloor;
137 int m_updateTilesPos;
138 int m_floorFading = 500;
139 TexturePtr m_crosshair = nullptr;
140 Size m_drawDimension;
141 Size m_visibleDimension;
142 Size m_optimizedSize;
143 Point m_virtualCenterOffset;
144 Point m_visibleCenterOffset;
145 Point m_moveOffset;
146 Position m_customCameraPosition;
147 Position m_lastCameraPosition;
148 stdext::boolean<true> m_mustUpdateVisibleTilesCache;
149 stdext::boolean<true> m_multifloor;
150 stdext::boolean<true> m_animated;
151 stdext::boolean<true> m_drawTexts;
152 stdext::boolean<true> m_drawNames;
153 stdext::boolean<true> m_drawHealthBars;
154 stdext::boolean<false> m_drawHealthBarsOnTop;
155 stdext::boolean<true> m_drawManaBar;
156 bool m_drawPlayerBars = true;
157 stdext::boolean<true> m_smooth;
158
159 stdext::timer m_fadingFloorTimers[Otc::MAX_Z + 1];
160
161 stdext::boolean<true> m_follow;
162 std::vector<TilePtr> m_cachedVisibleTiles[Otc::MAX_Z + 1];
163 CreaturePtr m_followingCreature;
164 Otc::DrawFlags m_drawFlags;
165 bool m_drawLight = false;
166 float m_minimumAmbientLight;
167 std::unique_ptr<LightView> m_lightView;
168 TexturePtr m_lightTexture;
169};
170
171#endif
172