Makefile: remove spurious tab
[xcsoar.git] / src / MapWindow / TargetMapWindow.cpp
blob7e04d13dfb7c7cc1fb14840ef342e02096dc632c
1 /*
2 Copyright_License {
4 XCSoar Glide Computer - http://www.xcsoar.org/
5 Copyright (C) 2000-2013 The XCSoar Project
6 A detailed list of copyright holders can be found in the file "AUTHORS".
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include "TargetMapWindow.hpp"
25 #include "Screen/Layout.hpp"
26 #include "Topography/TopographyStore.hpp"
27 #include "Topography/TopographyRenderer.hpp"
28 #include "Terrain/RasterTerrain.hpp"
29 #include "Terrain/RasterWeather.hpp"
30 #include "Look/TaskLook.hpp"
31 #include "Renderer/TaskRenderer.hpp"
32 #include "Renderer/TaskPointRenderer.hpp"
33 #include "Renderer/OZRenderer.hpp"
34 #include "Renderer/AircraftRenderer.hpp"
35 #include "Renderer/TrailRenderer.hpp"
36 #include "Task/ProtectedTaskManager.hpp"
37 #include "Units/Units.hpp"
38 #include "Interface.hpp"
39 #include "Computer/GlideComputer.hpp"
40 #include "Asset.hpp"
41 #include "Engine/Task/Ordered/OrderedTask.hpp"
42 #include "Engine/Task/Ordered/Points/OrderedTaskPoint.hpp"
43 #include "Engine/Task/ObservationZones/ObservationZonePoint.hpp"
44 #include "Engine/Task/ObservationZones/CylinderZone.hpp"
46 #ifdef ENABLE_OPENGL
47 #include "Screen/OpenGL/Scissor.hpp"
48 #else
49 #include "Screen/WindowCanvas.hpp"
50 #endif
52 #include <tchar.h>
54 static const ComputerSettings &
55 GetComputerSettings()
57 return CommonInterface::GetComputerSettings();
60 static const MapSettings &
61 GetMapSettings()
63 return CommonInterface::GetMapSettings();
66 static const MoreData &
67 Basic()
69 return CommonInterface::Basic();
72 static const DerivedInfo &
73 Calculated()
75 return CommonInterface::Calculated();
78 /**
79 * Constructor of the MapWindow class
81 TargetMapWindow::TargetMapWindow(const WaypointLook &waypoint_look,
82 const AirspaceLook &_airspace_look,
83 const TrailLook &_trail_look,
84 const TaskLook &_task_look,
85 const AircraftLook &_aircraft_look)
86 :task_look(_task_look),
87 aircraft_look(_aircraft_look),
88 topography_renderer(NULL),
89 airspace_renderer(_airspace_look),
90 way_point_renderer(NULL, waypoint_look),
91 trail_renderer(_trail_look),
92 task(NULL)
96 TargetMapWindow::~TargetMapWindow()
98 delete topography_renderer;
101 void
102 TargetMapWindow::Create(ContainerWindow &parent, PixelRect rc,
103 WindowStyle style)
105 projection.SetScale(fixed(0.01));
107 BufferWindow::Create(parent, rc, style);
110 void
111 TargetMapWindow::RenderTerrain(Canvas &canvas)
113 background.SetShadingAngle(projection, GetMapSettings().terrain,
114 Calculated());
115 background.Draw(canvas, projection, GetMapSettings().terrain);
118 void
119 TargetMapWindow::RenderTopography(Canvas &canvas)
121 if (topography_renderer != NULL && GetMapSettings().topography_enabled)
122 topography_renderer->Draw(canvas, projection);
125 void
126 TargetMapWindow::RenderTopographyLabels(Canvas &canvas)
128 if (topography_renderer != NULL && GetMapSettings().topography_enabled)
129 topography_renderer->DrawLabels(canvas, projection, label_block);
132 void
133 TargetMapWindow::RenderAirspace(Canvas &canvas)
135 if (GetMapSettings().airspace.enable)
136 airspace_renderer.Draw(canvas,
137 #ifndef ENABLE_OPENGL
138 buffer_canvas, stencil_canvas,
139 #endif
140 projection,
141 Basic(), Calculated(),
142 GetComputerSettings().airspace,
143 GetMapSettings().airspace);
146 void
147 TargetMapWindow::DrawTask(Canvas &canvas)
149 if (task == NULL)
150 return;
152 ProtectedTaskManager::Lease task_manager(*task);
153 const AbstractTask *task = task_manager->GetActiveTask();
154 if (task && task->CheckTask()) {
155 OZRenderer ozv(task_look, airspace_renderer.GetLook(),
156 GetMapSettings().airspace);
157 TaskPointRenderer tpv(canvas, projection, task_look,
158 /* we're accessing the OrderedTask here,
159 which may be invalid at this point, but it
160 will be used only if active, so it's ok */
161 task_manager->GetOrderedTask().GetTaskProjection(),
162 ozv, false, TaskPointRenderer::ALL,
163 Basic().location_available, Basic().location);
164 TaskRenderer dv(tpv, projection.GetScreenBounds());
165 dv.Draw(*task);
169 void
170 TargetMapWindow::DrawWaypoints(Canvas &canvas)
172 const MapSettings &settings_map = GetMapSettings();
173 WaypointRendererSettings settings = settings_map.waypoint;
174 settings.display_text_type = WaypointRendererSettings::DisplayTextType::NAME;
176 way_point_renderer.render(canvas, label_block,
177 projection, settings,
178 GetComputerSettings().polar,
179 GetComputerSettings().task,
180 Basic(), Calculated(),
181 task, NULL);
184 void
185 TargetMapWindow::RenderTrail(Canvas &canvas)
187 if (glide_computer == NULL)
188 return;
190 unsigned min_time = std::max(0, (int)Basic().time - 600);
191 trail_renderer.Draw(canvas, glide_computer->GetTraceComputer(),
192 projection, min_time);
195 void
196 TargetMapWindow::OnPaintBuffer(Canvas &canvas)
198 #ifdef ENABLE_OPENGL
199 /* enable clipping */
200 GLCanvasScissor scissor(canvas);
201 #endif
203 // Calculate screen position of the aircraft
204 const RasterPoint aircraft_pos = projection.GeoToScreen(Basic().location);
206 // reset label over-write preventer
207 label_block.reset();
209 // Render terrain, groundline and topography
210 RenderTerrain(canvas);
211 RenderTopography(canvas);
213 // Render airspace
214 RenderAirspace(canvas);
216 #ifdef ENABLE_OPENGL
217 /* desaturate the map background, to focus on the task */
218 canvas.FadeToWhite(0x80);
219 #endif
221 // Render task, waypoints
222 DrawTask(canvas);
223 DrawWaypoints(canvas);
225 // Render the snail trail
226 RenderTrail(canvas);
228 // Render topography on top of airspace, to keep the text readable
229 RenderTopographyLabels(canvas);
231 // Finally, draw you!
232 if (Basic().alive)
233 AircraftRenderer::Draw(canvas, GetMapSettings(), aircraft_look,
234 Basic().attitude.heading - projection.GetScreenAngle(),
235 aircraft_pos);
238 void
239 TargetMapWindow::OnPaint(Canvas &canvas)
241 BufferWindow::OnPaint(canvas);
243 if (drag_mode == DRAG_TARGET || drag_mode == DRAG_OZ)
244 TargetPaintDrag(canvas, drag_last);
247 void
248 TargetMapWindow::SetTerrain(RasterTerrain *terrain)
250 background.SetTerrain(terrain);
253 void
254 TargetMapWindow::SetTopograpgy(TopographyStore *topography)
256 delete topography_renderer;
257 topography_renderer = topography != NULL
258 ? new TopographyRenderer(*topography)
259 : NULL;
262 static fixed
263 GetRadius(const ObservationZonePoint &oz)
265 switch (oz.GetShape()) {
266 case ObservationZone::Shape::LINE:
267 case ObservationZone::Shape::MAT_CYLINDER:
268 case ObservationZone::Shape::CYLINDER:
269 case ObservationZone::Shape::SECTOR:
270 case ObservationZone::Shape::FAI_SECTOR:
271 case ObservationZone::Shape::CUSTOM_KEYHOLE:
272 case ObservationZone::Shape::DAEC_KEYHOLE:
273 case ObservationZone::Shape::BGAFIXEDCOURSE:
274 case ObservationZone::Shape::BGAENHANCEDOPTION:
275 case ObservationZone::Shape::BGA_START:
276 case ObservationZone::Shape::ANNULAR_SECTOR:
277 case ObservationZone::Shape::SYMMETRIC_QUADRANT:
278 const CylinderZone &cz = (const CylinderZone &)oz;
279 return cz.GetRadius();
282 return fixed(1);
285 static fixed
286 GetRadius(const OrderedTaskPoint &tp)
288 return GetRadius(tp.GetObservationZone());
291 void
292 TargetMapWindow::SetTarget(unsigned index)
294 GeoPoint location;
295 fixed radius;
298 ProtectedTaskManager::Lease lease(*task);
299 const OrderedTask &o_task = lease->GetOrderedTask();
300 if (!o_task.IsValidIndex(index))
301 return;
303 const OrderedTaskPoint &tp = o_task.GetTaskPoint(index);
304 location = tp.GetLocation();
305 radius = std::max(GetRadius(tp) * fixed(1.3), fixed(2000));
308 projection.SetGeoLocation(location);
309 projection.SetScaleFromRadius(radius);
310 projection.SetScreenAngle(Angle::Zero());
311 projection.UpdateScreenBounds();
313 target_index = index;
315 Invalidate();
318 void
319 TargetMapWindow::OnResize(PixelSize new_size)
321 BufferWindow::OnResize(new_size);
323 #ifndef ENABLE_OPENGL
324 buffer_canvas.Grow(new_size);
326 if (!IsAncientHardware())
327 stencil_canvas.Grow(new_size);
328 #endif
330 projection.SetScreenSize(new_size);
331 projection.SetScreenOrigin(new_size.cx / 2, new_size.cy / 2);
332 projection.UpdateScreenBounds();
335 void
336 TargetMapWindow::OnCreate()
338 BufferWindow::OnCreate();
340 drag_mode = DRAG_NONE;
342 #ifndef ENABLE_OPENGL
343 WindowCanvas canvas(*this);
344 buffer_canvas.Create(canvas);
346 if (!IsAncientHardware())
347 stencil_canvas.Create(canvas);
348 #endif
351 void
352 TargetMapWindow::OnDestroy()
354 SetTerrain(NULL);
355 SetTopograpgy(NULL);
356 SetAirspaces(NULL);
357 SetWaypoints(NULL);
359 #ifndef ENABLE_OPENGL
360 buffer_canvas.Destroy();
362 if (!IsAncientHardware())
363 stencil_canvas.Destroy();
364 #endif
366 BufferWindow::OnDestroy();