OpenTTD Source  20240915-master-g3784a3d3d6
viewport.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
63 #include "stdafx.h"
64 #include "core/backup_type.hpp"
65 #include "landscape.h"
66 #include "viewport_func.h"
67 #include "station_base.h"
68 #include "waypoint_base.h"
69 #include "town.h"
70 #include "signs_base.h"
71 #include "signs_func.h"
72 #include "vehicle_base.h"
73 #include "vehicle_gui.h"
74 #include "blitter/factory.hpp"
75 #include "strings_func.h"
76 #include "zoom_func.h"
77 #include "vehicle_func.h"
78 #include "company_func.h"
79 #include "waypoint_func.h"
80 #include "window_func.h"
81 #include "tilehighlight_func.h"
82 #include "window_gui.h"
84 #include "viewport_kdtree.h"
85 #include "town_kdtree.h"
86 #include "viewport_sprite_sorter.h"
87 #include "bridge_map.h"
88 #include "company_base.h"
89 #include "command_func.h"
90 #include "network/network_func.h"
91 #include "framerate_type.h"
92 #include "viewport_cmd.h"
93 
94 #include <forward_list>
95 #include <stack>
96 
97 #include "table/strings.h"
98 #include "table/string_colours.h"
99 
100 #include "safeguards.h"
101 
102 Point _tile_fract_coords;
103 
104 
105 ViewportSignKdtree _viewport_sign_kdtree(&Kdtree_ViewportSignXYFunc);
106 static int _viewport_sign_maxwidth = 0;
107 
108 
109 static const int MAX_TILE_EXTENT_LEFT = ZOOM_BASE * TILE_PIXELS;
110 static const int MAX_TILE_EXTENT_RIGHT = ZOOM_BASE * TILE_PIXELS;
111 static const int MAX_TILE_EXTENT_TOP = ZOOM_BASE * MAX_BUILDING_PIXELS;
112 static const int MAX_TILE_EXTENT_BOTTOM = ZOOM_BASE * (TILE_PIXELS + 2 * TILE_HEIGHT);
113 
115  std::string string;
116  StringID string_id;
117  Colours colour;
118  int32_t x;
119  int32_t y;
120  uint16_t width;
121 };
122 
124  SpriteID image;
125  PaletteID pal;
126  const SubSprite *sub;
127  int32_t x;
128  int32_t y;
129 };
130 
132  SpriteID image;
133  PaletteID pal;
134  const SubSprite *sub;
135  int32_t x;
136  int32_t y;
137  bool relative;
138  int next;
139 };
140 
146  FOUNDATION_PART_END
147 };
148 
157 };
158 
159 typedef std::vector<TileSpriteToDraw> TileSpriteToDrawVector;
160 typedef std::vector<StringSpriteToDraw> StringSpriteToDrawVector;
161 typedef std::vector<ParentSpriteToDraw> ParentSpriteToDrawVector;
162 typedef std::vector<ChildScreenSpriteToDraw> ChildScreenSpriteToDrawVector;
163 
166  DrawPixelInfo dpi;
167 
168  StringSpriteToDrawVector string_sprites_to_draw;
169  TileSpriteToDrawVector tile_sprites_to_draw;
170  ParentSpriteToDrawVector parent_sprites_to_draw;
171  ParentSpriteToSortVector parent_sprites_to_sort;
172  ChildScreenSpriteToDrawVector child_screen_sprites_to_draw;
173 
174  int *last_child;
175 
177 
178  int foundation[FOUNDATION_PART_END];
180  int *last_foundation_child[FOUNDATION_PART_END];
181  Point foundation_offset[FOUNDATION_PART_END];
182 };
183 
184 static bool MarkViewportDirty(const Viewport *vp, int left, int top, int right, int bottom);
185 
186 static ViewportDrawer _vd;
187 
188 TileHighlightData _thd;
189 static TileInfo _cur_ti;
190 bool _draw_bounding_boxes = false;
191 bool _draw_dirty_blocks = false;
192 uint _dirty_block_colour = 0;
193 static VpSpriteSorter _vp_sprite_sorter = nullptr;
194 
195 static Point MapXYZToViewport(const Viewport *vp, int x, int y, int z)
196 {
197  Point p = RemapCoords(x, y, z);
198  p.x -= vp->virtual_width / 2;
199  p.y -= vp->virtual_height / 2;
200  return p;
201 }
202 
203 void DeleteWindowViewport(Window *w)
204 {
205  delete w->viewport;
206  w->viewport = nullptr;
207 }
208 
219 void InitializeWindowViewport(Window *w, int x, int y,
220  int width, int height, std::variant<TileIndex, VehicleID> focus, ZoomLevel zoom)
221 {
222  assert(w->viewport == nullptr);
223 
224  ViewportData *vp = new ViewportData();
225 
226  vp->left = x + w->left;
227  vp->top = y + w->top;
228  vp->width = width;
229  vp->height = height;
230 
232 
233  vp->virtual_width = ScaleByZoom(width, zoom);
234  vp->virtual_height = ScaleByZoom(height, zoom);
235 
236  Point pt;
237 
238  if (std::holds_alternative<VehicleID>(focus)) {
239  const Vehicle *veh;
240 
241  vp->follow_vehicle = std::get<VehicleID>(focus);
242  veh = Vehicle::Get(vp->follow_vehicle);
243  pt = MapXYZToViewport(vp, veh->x_pos, veh->y_pos, veh->z_pos);
244  } else {
245  TileIndex tile = std::get<TileIndex>(focus);
246  if (tile == INVALID_TILE) {
247  /* No tile? Use center of main viewport. */
248  const Window *mw = GetMainWindow();
249 
250  /* center on same place as main window (zoom is maximum, no adjustment needed) */
251  pt.x = mw->viewport->scrollpos_x + mw->viewport->virtual_width / 2;
252  pt.x -= vp->virtual_width / 2;
253  pt.y = mw->viewport->scrollpos_y + mw->viewport->virtual_height / 2;
254  pt.y -= vp->virtual_height / 2;
255  } else {
256  x = TileX(tile) * TILE_SIZE;
257  y = TileY(tile) * TILE_SIZE;
258  pt = MapXYZToViewport(vp, x, y, GetSlopePixelZ(x, y));
259  }
261  }
262 
263  vp->scrollpos_x = pt.x;
264  vp->scrollpos_y = pt.y;
265  vp->dest_scrollpos_x = pt.x;
266  vp->dest_scrollpos_y = pt.y;
267 
268  vp->overlay = nullptr;
269 
270  w->viewport = vp;
271  vp->virtual_left = 0;
272  vp->virtual_top = 0;
273 }
274 
275 static Point _vp_move_offs;
276 
277 static void DoSetViewportPosition(Window::IteratorToFront it, int left, int top, int width, int height)
278 {
279  for (; !it.IsEnd(); ++it) {
280  const Window *w = *it;
281  if (left + width > w->left &&
282  w->left + w->width > left &&
283  top + height > w->top &&
284  w->top + w->height > top) {
285 
286  if (left < w->left) {
287  DoSetViewportPosition(it, left, top, w->left - left, height);
288  DoSetViewportPosition(it, left + (w->left - left), top, width - (w->left - left), height);
289  return;
290  }
291 
292  if (left + width > w->left + w->width) {
293  DoSetViewportPosition(it, left, top, (w->left + w->width - left), height);
294  DoSetViewportPosition(it, left + (w->left + w->width - left), top, width - (w->left + w->width - left), height);
295  return;
296  }
297 
298  if (top < w->top) {
299  DoSetViewportPosition(it, left, top, width, (w->top - top));
300  DoSetViewportPosition(it, left, top + (w->top - top), width, height - (w->top - top));
301  return;
302  }
303 
304  if (top + height > w->top + w->height) {
305  DoSetViewportPosition(it, left, top, width, (w->top + w->height - top));
306  DoSetViewportPosition(it, left, top + (w->top + w->height - top), width, height - (w->top + w->height - top));
307  return;
308  }
309 
310  return;
311  }
312  }
313 
314  {
315  int xo = _vp_move_offs.x;
316  int yo = _vp_move_offs.y;
317 
318  if (abs(xo) >= width || abs(yo) >= height) {
319  /* fully_outside */
320  RedrawScreenRect(left, top, left + width, top + height);
321  return;
322  }
323 
324  GfxScroll(left, top, width, height, xo, yo);
325 
326  if (xo > 0) {
327  RedrawScreenRect(left, top, xo + left, top + height);
328  left += xo;
329  width -= xo;
330  } else if (xo < 0) {
331  RedrawScreenRect(left + width + xo, top, left + width, top + height);
332  width += xo;
333  }
334 
335  if (yo > 0) {
336  RedrawScreenRect(left, top, width + left, top + yo);
337  } else if (yo < 0) {
338  RedrawScreenRect(left, top + height + yo, width + left, top + height);
339  }
340  }
341 }
342 
343 static void SetViewportPosition(Window *w, int x, int y)
344 {
345  Viewport *vp = w->viewport;
346  int old_left = vp->virtual_left;
347  int old_top = vp->virtual_top;
348  int i;
349  int left, top, width, height;
350 
351  vp->virtual_left = x;
352  vp->virtual_top = y;
353 
354  /* Viewport is bound to its left top corner, so it must be rounded down (UnScaleByZoomLower)
355  * else glitch described in FS#1412 will happen (offset by 1 pixel with zoom level > NORMAL)
356  */
357  old_left = UnScaleByZoomLower(old_left, vp->zoom);
358  old_top = UnScaleByZoomLower(old_top, vp->zoom);
359  x = UnScaleByZoomLower(x, vp->zoom);
360  y = UnScaleByZoomLower(y, vp->zoom);
361 
362  old_left -= x;
363  old_top -= y;
364 
365  if (old_top == 0 && old_left == 0) return;
366 
367  _vp_move_offs.x = old_left;
368  _vp_move_offs.y = old_top;
369 
370  left = vp->left;
371  top = vp->top;
372  width = vp->width;
373  height = vp->height;
374 
375  if (left < 0) {
376  width += left;
377  left = 0;
378  }
379 
380  i = left + width - _screen.width;
381  if (i >= 0) width -= i;
382 
383  if (width > 0) {
384  if (top < 0) {
385  height += top;
386  top = 0;
387  }
388 
389  i = top + height - _screen.height;
390  if (i >= 0) height -= i;
391 
392  if (height > 0) {
394  ++it;
395  DoSetViewportPosition(it, left, top, width, height);
396  }
397  }
398 }
399 
408 Viewport *IsPtInWindowViewport(const Window *w, int x, int y)
409 {
410  Viewport *vp = w->viewport;
411 
412  if (vp != nullptr &&
413  IsInsideMM(x, vp->left, vp->left + vp->width) &&
414  IsInsideMM(y, vp->top, vp->top + vp->height))
415  return vp;
416 
417  return nullptr;
418 }
419 
432 Point TranslateXYToTileCoord(const Viewport *vp, int x, int y, bool clamp_to_map)
433 {
434  if (!IsInsideBS(x, vp->left, vp->width) || !IsInsideBS(y, vp->top, vp->height)) {
435  Point pt = { -1, -1 };
436  return pt;
437  }
438 
439  return InverseRemapCoords2(
440  ScaleByZoom(x - vp->left, vp->zoom) + vp->virtual_left,
441  ScaleByZoom(y - vp->top, vp->zoom) + vp->virtual_top, clamp_to_map);
442 }
443 
444 /* When used for zooming, check area below current coordinates (x,y)
445  * and return the tile of the zoomed out/in position (zoom_x, zoom_y)
446  * when you just want the tile, make x = zoom_x and y = zoom_y */
447 static Point GetTileFromScreenXY(int x, int y, int zoom_x, int zoom_y)
448 {
449  Window *w;
450  Viewport *vp;
451  Point pt;
452 
453  if ( (w = FindWindowFromPt(x, y)) != nullptr &&
454  (vp = IsPtInWindowViewport(w, x, y)) != nullptr)
455  return TranslateXYToTileCoord(vp, zoom_x, zoom_y);
456 
457  pt.y = pt.x = -1;
458  return pt;
459 }
460 
461 Point GetTileBelowCursor()
462 {
463  return GetTileFromScreenXY(_cursor.pos.x, _cursor.pos.y, _cursor.pos.x, _cursor.pos.y);
464 }
465 
466 
467 Point GetTileZoomCenterWindow(bool in, Window * w)
468 {
469  int x, y;
470  Viewport *vp = w->viewport;
471 
472  if (in) {
473  x = ((_cursor.pos.x - vp->left) >> 1) + (vp->width >> 2);
474  y = ((_cursor.pos.y - vp->top) >> 1) + (vp->height >> 2);
475  } else {
476  x = vp->width - (_cursor.pos.x - vp->left);
477  y = vp->height - (_cursor.pos.y - vp->top);
478  }
479  /* Get the tile below the cursor and center on the zoomed-out center */
480  return GetTileFromScreenXY(_cursor.pos.x, _cursor.pos.y, x + vp->left, y + vp->top);
481 }
482 
491 void HandleZoomMessage(Window *w, const Viewport *vp, WidgetID widget_zoom_in, WidgetID widget_zoom_out)
492 {
493  w->SetWidgetDisabledState(widget_zoom_in, vp->zoom <= _settings_client.gui.zoom_min);
494  w->SetWidgetDirty(widget_zoom_in);
495 
496  w->SetWidgetDisabledState(widget_zoom_out, vp->zoom >= _settings_client.gui.zoom_max);
497  w->SetWidgetDirty(widget_zoom_out);
498 }
499 
512 static void AddTileSpriteToDraw(SpriteID image, PaletteID pal, int32_t x, int32_t y, int z, const SubSprite *sub = nullptr, int extra_offs_x = 0, int extra_offs_y = 0)
513 {
514  assert((image & SPRITE_MASK) < MAX_SPRITES);
515 
516  TileSpriteToDraw &ts = _vd.tile_sprites_to_draw.emplace_back();
517  ts.image = image;
518  ts.pal = pal;
519  ts.sub = sub;
520  Point pt = RemapCoords(x, y, z);
521  ts.x = pt.x + extra_offs_x;
522  ts.y = pt.y + extra_offs_y;
523 }
524 
537 static void AddChildSpriteToFoundation(SpriteID image, PaletteID pal, const SubSprite *sub, FoundationPart foundation_part, int extra_offs_x, int extra_offs_y)
538 {
539  assert(IsInsideMM(foundation_part, 0, FOUNDATION_PART_END));
540  assert(_vd.foundation[foundation_part] != -1);
541  Point offs = _vd.foundation_offset[foundation_part];
542 
543  /* Change the active ChildSprite list to the one of the foundation */
544  int *old_child = _vd.last_child;
545  _vd.last_child = _vd.last_foundation_child[foundation_part];
546 
547  AddChildSpriteScreen(image, pal, offs.x + extra_offs_x, offs.y + extra_offs_y, false, sub, false, false);
548 
549  /* Switch back to last ChildSprite list */
550  _vd.last_child = old_child;
551 }
552 
566 void DrawGroundSpriteAt(SpriteID image, PaletteID pal, int32_t x, int32_t y, int z, const SubSprite *sub, int extra_offs_x, int extra_offs_y)
567 {
568  /* Switch to first foundation part, if no foundation was drawn */
570 
571  if (_vd.foundation[_vd.foundation_part] != -1) {
572  Point pt = RemapCoords(x, y, z);
573  AddChildSpriteToFoundation(image, pal, sub, _vd.foundation_part, pt.x + extra_offs_x * ZOOM_BASE, pt.y + extra_offs_y * ZOOM_BASE);
574  } else {
575  AddTileSpriteToDraw(image, pal, _cur_ti.x + x, _cur_ti.y + y, _cur_ti.z + z, sub, extra_offs_x * ZOOM_BASE, extra_offs_y * ZOOM_BASE);
576  }
577 }
578 
589 void DrawGroundSprite(SpriteID image, PaletteID pal, const SubSprite *sub, int extra_offs_x, int extra_offs_y)
590 {
591  DrawGroundSpriteAt(image, pal, 0, 0, 0, sub, extra_offs_x, extra_offs_y);
592 }
593 
601 void OffsetGroundSprite(int x, int y)
602 {
603  /* Switch to next foundation part */
604  switch (_vd.foundation_part) {
607  break;
610  break;
611  default: NOT_REACHED();
612  }
613 
614  /* _vd.last_child == nullptr if foundation sprite was clipped by the viewport bounds */
615  if (_vd.last_child != nullptr) _vd.foundation[_vd.foundation_part] = (uint)_vd.parent_sprites_to_draw.size() - 1;
616 
617  _vd.foundation_offset[_vd.foundation_part].x = x * ZOOM_BASE;
618  _vd.foundation_offset[_vd.foundation_part].y = y * ZOOM_BASE;
619  _vd.last_foundation_child[_vd.foundation_part] = _vd.last_child;
620 }
621 
633 static void AddCombinedSprite(SpriteID image, PaletteID pal, int x, int y, int z, const SubSprite *sub)
634 {
635  Point pt = RemapCoords(x, y, z);
636  const Sprite *spr = GetSprite(image & SPRITE_MASK, SpriteType::Normal);
637 
638  if (pt.x + spr->x_offs >= _vd.dpi.left + _vd.dpi.width ||
639  pt.x + spr->x_offs + spr->width <= _vd.dpi.left ||
640  pt.y + spr->y_offs >= _vd.dpi.top + _vd.dpi.height ||
641  pt.y + spr->y_offs + spr->height <= _vd.dpi.top)
642  return;
643 
644  const ParentSpriteToDraw &pstd = _vd.parent_sprites_to_draw.back();
645  AddChildSpriteScreen(image, pal, pt.x - pstd.left, pt.y - pstd.top, false, sub, false);
646 }
647 
673 void AddSortableSpriteToDraw(SpriteID image, PaletteID pal, int x, int y, int w, int h, int dz, int z, bool transparent, int bb_offset_x, int bb_offset_y, int bb_offset_z, const SubSprite *sub)
674 {
675  int32_t left, right, top, bottom;
676 
677  assert((image & SPRITE_MASK) < MAX_SPRITES);
678 
679  /* make the sprites transparent with the right palette */
680  if (transparent) {
683  }
684 
686  AddCombinedSprite(image, pal, x, y, z, sub);
687  return;
688  }
689 
690  _vd.last_child = nullptr;
691 
692  Point pt = RemapCoords(x, y, z);
693  int tmp_left, tmp_top, tmp_x = pt.x, tmp_y = pt.y;
694 
695  /* Compute screen extents of sprite */
696  if (image == SPR_EMPTY_BOUNDING_BOX) {
697  left = tmp_left = RemapCoords(x + w , y + bb_offset_y, z + bb_offset_z).x;
698  right = RemapCoords(x + bb_offset_x, y + h , z + bb_offset_z).x + 1;
699  top = tmp_top = RemapCoords(x + bb_offset_x, y + bb_offset_y, z + dz ).y;
700  bottom = RemapCoords(x + w , y + h , z + bb_offset_z).y + 1;
701  } else {
702  const Sprite *spr = GetSprite(image & SPRITE_MASK, SpriteType::Normal);
703  left = tmp_left = (pt.x += spr->x_offs);
704  right = (pt.x + spr->width );
705  top = tmp_top = (pt.y += spr->y_offs);
706  bottom = (pt.y + spr->height);
707  }
708 
709  if (_draw_bounding_boxes && (image != SPR_EMPTY_BOUNDING_BOX)) {
710  /* Compute maximal extents of sprite and its bounding box */
711  left = std::min(left , RemapCoords(x + w , y + bb_offset_y, z + bb_offset_z).x);
712  right = std::max(right , RemapCoords(x + bb_offset_x, y + h , z + bb_offset_z).x + 1);
713  top = std::min(top , RemapCoords(x + bb_offset_x, y + bb_offset_y, z + dz ).y);
714  bottom = std::max(bottom, RemapCoords(x + w , y + h , z + bb_offset_z).y + 1);
715  }
716 
717  /* Do not add the sprite to the viewport, if it is outside */
718  if (left >= _vd.dpi.left + _vd.dpi.width ||
719  right <= _vd.dpi.left ||
720  top >= _vd.dpi.top + _vd.dpi.height ||
721  bottom <= _vd.dpi.top) {
722  return;
723  }
724 
725  ParentSpriteToDraw &ps = _vd.parent_sprites_to_draw.emplace_back();
726  ps.x = tmp_x;
727  ps.y = tmp_y;
728 
729  ps.left = tmp_left;
730  ps.top = tmp_top;
731 
732  ps.image = image;
733  ps.pal = pal;
734  ps.sub = sub;
735  ps.xmin = x + bb_offset_x;
736  ps.xmax = x + std::max(bb_offset_x, w) - 1;
737 
738  ps.ymin = y + bb_offset_y;
739  ps.ymax = y + std::max(bb_offset_y, h) - 1;
740 
741  ps.zmin = z + bb_offset_z;
742  ps.zmax = z + std::max(bb_offset_z, dz) - 1;
743 
744  ps.first_child = -1;
745 
746  _vd.last_child = &ps.first_child;
747 
749 }
750 
770 {
771  assert(_vd.combine_sprites == SPRITE_COMBINE_NONE);
773 }
774 
780 {
781  assert(_vd.combine_sprites != SPRITE_COMBINE_NONE);
783 }
784 
794 static bool IsInRangeInclusive(int begin, int end, int check)
795 {
796  if (begin > end) Swap(begin, end);
797  return begin <= check && check <= end;
798 }
799 
806 bool IsInsideRotatedRectangle(int x, int y)
807 {
808  int dist_a = (_thd.size.x + _thd.size.y); // Rotated coordinate system for selected rectangle.
809  int dist_b = (_thd.size.x - _thd.size.y); // We don't have to divide by 2. It's all relative!
810  int a = ((x - _thd.pos.x) + (y - _thd.pos.y)); // Rotated coordinate system for the point under scrutiny.
811  int b = ((x - _thd.pos.x) - (y - _thd.pos.y));
812 
813  /* Check if a and b are between 0 and dist_a or dist_b respectively. */
814  return IsInRangeInclusive(dist_a, 0, a) && IsInRangeInclusive(dist_b, 0, b);
815 }
816 
829 void AddChildSpriteScreen(SpriteID image, PaletteID pal, int x, int y, bool transparent, const SubSprite *sub, bool scale, bool relative)
830 {
831  assert((image & SPRITE_MASK) < MAX_SPRITES);
832 
833  /* If the ParentSprite was clipped by the viewport bounds, do not draw the ChildSprites either */
834  if (_vd.last_child == nullptr) return;
835 
836  /* make the sprites transparent with the right palette */
837  if (transparent) {
840  }
841 
842  *_vd.last_child = (uint)_vd.child_screen_sprites_to_draw.size();
843 
844  ChildScreenSpriteToDraw &cs = _vd.child_screen_sprites_to_draw.emplace_back();
845  cs.image = image;
846  cs.pal = pal;
847  cs.sub = sub;
848  cs.x = scale ? x * ZOOM_BASE : x;
849  cs.y = scale ? y * ZOOM_BASE : y;
850  cs.relative = relative;
851  cs.next = -1;
852 
853  /* Append the sprite to the active ChildSprite list.
854  * If the active ParentSprite is a foundation, update last_foundation_child as well.
855  * Note: ChildSprites of foundations are NOT sequential in the vector, as selection sprites are added at last. */
856  if (_vd.last_foundation_child[0] == _vd.last_child) _vd.last_foundation_child[0] = &cs.next;
857  if (_vd.last_foundation_child[1] == _vd.last_child) _vd.last_foundation_child[1] = &cs.next;
858  _vd.last_child = &cs.next;
859 }
860 
861 static void AddStringToDraw(int x, int y, StringID string, Colours colour, uint16_t width)
862 {
863  assert(width != 0);
864  StringSpriteToDraw &ss = _vd.string_sprites_to_draw.emplace_back();
865  ss.string = GetString(string);
866  ss.string_id = string;
867  ss.x = x;
868  ss.y = y;
869  ss.width = width;
870  ss.colour = colour;
871 }
872 
873 
887 static void DrawSelectionSprite(SpriteID image, PaletteID pal, const TileInfo *ti, int z_offset, FoundationPart foundation_part, int extra_offs_x = 0, int extra_offs_y = 0)
888 {
889  /* FIXME: This is not totally valid for some autorail highlights that extend over the edges of the tile. */
890  if (_vd.foundation[foundation_part] == -1) {
891  /* draw on real ground */
892  AddTileSpriteToDraw(image, pal, ti->x, ti->y, ti->z + z_offset, nullptr, extra_offs_x, extra_offs_y);
893  } else {
894  /* draw on top of foundation */
895  AddChildSpriteToFoundation(image, pal, nullptr, foundation_part, extra_offs_x, extra_offs_y - z_offset * ZOOM_BASE);
896  }
897 }
898 
905 static void DrawTileSelectionRect(const TileInfo *ti, PaletteID pal)
906 {
907  if (!IsValidTile(ti->tile)) return;
908 
909  SpriteID sel;
910  if (IsHalftileSlope(ti->tileh)) {
911  Corner halftile_corner = GetHalftileSlopeCorner(ti->tileh);
912  SpriteID sel2 = SPR_HALFTILE_SELECTION_FLAT + halftile_corner;
914 
915  Corner opposite_corner = OppositeCorner(halftile_corner);
916  if (IsSteepSlope(ti->tileh)) {
917  sel = SPR_HALFTILE_SELECTION_DOWN;
918  } else {
919  sel = ((ti->tileh & SlopeWithOneCornerRaised(opposite_corner)) != 0 ? SPR_HALFTILE_SELECTION_UP : SPR_HALFTILE_SELECTION_FLAT);
920  }
921  sel += opposite_corner;
922  } else {
923  sel = SPR_SELECT_TILE + SlopeToSpriteOffset(ti->tileh);
924  }
926 }
927 
928 static bool IsPartOfAutoLine(int px, int py)
929 {
930  px -= _thd.selstart.x;
931  py -= _thd.selstart.y;
932 
933  if ((_thd.drawstyle & HT_DRAG_MASK) != HT_LINE) return false;
934 
935  switch (_thd.drawstyle & HT_DIR_MASK) {
936  case HT_DIR_X: return py == 0; // x direction
937  case HT_DIR_Y: return px == 0; // y direction
938  case HT_DIR_HU: return px == -py || px == -py - 16; // horizontal upper
939  case HT_DIR_HL: return px == -py || px == -py + 16; // horizontal lower
940  case HT_DIR_VL: return px == py || px == py + 16; // vertical left
941  case HT_DIR_VR: return px == py || px == py - 16; // vertical right
942  default:
943  NOT_REACHED();
944  }
945 }
946 
947 /* [direction][side] */
948 static const HighLightStyle _autorail_type[6][2] = {
949  { HT_DIR_X, HT_DIR_X },
950  { HT_DIR_Y, HT_DIR_Y },
951  { HT_DIR_HU, HT_DIR_HL },
952  { HT_DIR_HL, HT_DIR_HU },
953  { HT_DIR_VL, HT_DIR_VR },
954  { HT_DIR_VR, HT_DIR_VL }
955 };
956 
957 #include "table/autorail.h"
958 
965 static void DrawAutorailSelection(const TileInfo *ti, uint autorail_type)
966 {
967  SpriteID image;
968  PaletteID pal;
969  int offset;
970 
971  FoundationPart foundation_part = FOUNDATION_PART_NORMAL;
972  Slope autorail_tileh = RemoveHalftileSlope(ti->tileh);
973  if (IsHalftileSlope(ti->tileh)) {
974  static const uint _lower_rail[4] = { 5U, 2U, 4U, 3U };
975  Corner halftile_corner = GetHalftileSlopeCorner(ti->tileh);
976  if (autorail_type != _lower_rail[halftile_corner]) {
977  foundation_part = FOUNDATION_PART_HALFTILE;
978  /* Here we draw the highlights of the "three-corners-raised"-slope. That looks ok to me. */
979  autorail_tileh = SlopeWithThreeCornersRaised(OppositeCorner(halftile_corner));
980  }
981  }
982 
983  offset = _AutorailTilehSprite[autorail_tileh][autorail_type];
984  if (offset >= 0) {
985  image = SPR_AUTORAIL_BASE + offset;
986  pal = PAL_NONE;
987  } else {
988  image = SPR_AUTORAIL_BASE - offset;
989  pal = PALETTE_SEL_TILE_RED;
990  }
991 
992  DrawSelectionSprite(image, _thd.make_square_red ? PALETTE_SEL_TILE_RED : pal, ti, 7, foundation_part);
993 }
994 
995 enum TileHighlightType {
996  THT_NONE,
997  THT_WHITE,
998  THT_BLUE,
999  THT_RED,
1000 };
1001 
1005 
1011 static TileHighlightType GetTileHighlightType(TileIndex t)
1012 {
1013  if (_viewport_highlight_station != nullptr) {
1014  if (IsTileType(t, MP_STATION) && GetStationIndex(t) == _viewport_highlight_station->index) return THT_WHITE;
1015  if (_viewport_highlight_station->TileIsInCatchment(t)) return THT_BLUE;
1016  }
1017  if (_viewport_highlight_waypoint != nullptr) {
1018  if (IsTileType(t, MP_STATION) && GetStationIndex(t) == _viewport_highlight_waypoint->index) return THT_BLUE;
1019  }
1020 
1021  if (_viewport_highlight_town != nullptr) {
1022  if (IsTileType(t, MP_HOUSE)) {
1024  TileHighlightType type = THT_RED;
1025  for (const Station *st : _viewport_highlight_town->stations_near) {
1026  if (st->owner != _current_company) continue;
1027  if (st->TileIsInCatchment(t)) return THT_BLUE;
1028  }
1029  return type;
1030  }
1031  } else if (IsTileType(t, MP_STATION)) {
1032  for (const Station *st : _viewport_highlight_town->stations_near) {
1033  if (st->owner != _current_company) continue;
1034  if (GetStationIndex(t) == st->index) return THT_WHITE;
1035  }
1036  }
1037  }
1038 
1039  return THT_NONE;
1040 }
1041 
1047 static void DrawTileHighlightType(const TileInfo *ti, TileHighlightType tht)
1048 {
1049  switch (tht) {
1050  default:
1051  case THT_NONE: break;
1052  case THT_WHITE: DrawTileSelectionRect(ti, PAL_NONE); break;
1053  case THT_BLUE: DrawTileSelectionRect(ti, PALETTE_SEL_TILE_BLUE); break;
1054  case THT_RED: DrawTileSelectionRect(ti, PALETTE_SEL_TILE_RED); break;
1055  }
1056 }
1057 
1063 {
1064  /* Going through cases in order of computational time. */
1065 
1066  if (_town_local_authority_kdtree.Count() == 0) return;
1067 
1068  /* Tile belongs to town regardless of distance from town. */
1069  if (GetTileType(ti->tile) == MP_HOUSE) {
1070  if (!Town::GetByTile(ti->tile)->show_zone) return;
1071 
1073  return;
1074  }
1075 
1076  /* If the closest town in the highlighted list is far, we can stop searching. */
1077  TownID tid = _town_local_authority_kdtree.FindNearest(TileX(ti->tile), TileY(ti->tile));
1078  Town *closest_highlighted_town = Town::Get(tid);
1079 
1080  if (DistanceManhattan(ti->tile, closest_highlighted_town->xy) >= _settings_game.economy.dist_local_authority) return;
1081 
1082  /* Tile is inside of the local autrhority distance of a highlighted town,
1083  but it is possible that a non-highlighted town is even closer. */
1085 
1086  if (closest_town->show_zone) {
1088  }
1089 
1090 }
1091 
1096 static void DrawTileSelection(const TileInfo *ti)
1097 {
1098  /* Highlight tiles insede local authority of selected towns. */
1100 
1101  /* Draw a red error square? */
1102  bool is_redsq = _thd.redsq == ti->tile;
1104 
1105  TileHighlightType tht = GetTileHighlightType(ti->tile);
1106  DrawTileHighlightType(ti, tht);
1107 
1108  /* No tile selection active? */
1109  if ((_thd.drawstyle & HT_DRAG_MASK) == HT_NONE) return;
1110 
1111  if (_thd.diagonal) { // We're drawing a 45 degrees rotated (diagonal) rectangle
1112  if (IsInsideRotatedRectangle((int)ti->x, (int)ti->y)) goto draw_inner;
1113  return;
1114  }
1115 
1116  /* Inside the inner area? */
1117  if (IsInsideBS(ti->x, _thd.pos.x, _thd.size.x) &&
1118  IsInsideBS(ti->y, _thd.pos.y, _thd.size.y)) {
1119 draw_inner:
1120  if (_thd.drawstyle & HT_RECT) {
1121  if (!is_redsq) DrawTileSelectionRect(ti, _thd.make_square_red ? PALETTE_SEL_TILE_RED : PAL_NONE);
1122  } else if (_thd.drawstyle & HT_POINT) {
1123  /* Figure out the Z coordinate for the single dot. */
1124  int z = 0;
1125  FoundationPart foundation_part = FOUNDATION_PART_NORMAL;
1126  if (ti->tileh & SLOPE_N) {
1127  z += TILE_HEIGHT;
1129  }
1130  if (IsHalftileSlope(ti->tileh)) {
1131  Corner halftile_corner = GetHalftileSlopeCorner(ti->tileh);
1132  if ((halftile_corner == CORNER_W) || (halftile_corner == CORNER_E)) z += TILE_HEIGHT;
1133  if (halftile_corner != CORNER_S) {
1134  foundation_part = FOUNDATION_PART_HALFTILE;
1135  if (IsSteepSlope(ti->tileh)) z -= TILE_HEIGHT;
1136  }
1137  }
1138  DrawSelectionSprite(SPR_DOT, PAL_NONE, ti, z, foundation_part);
1139  } else if (_thd.drawstyle & HT_RAIL) {
1140  /* autorail highlight piece under cursor */
1141  HighLightStyle type = _thd.drawstyle & HT_DIR_MASK;
1142  assert(type < HT_DIR_END);
1143  DrawAutorailSelection(ti, _autorail_type[type][0]);
1144  } else if (IsPartOfAutoLine(ti->x, ti->y)) {
1145  /* autorail highlighting long line */
1146  HighLightStyle dir = _thd.drawstyle & HT_DIR_MASK;
1147  uint side;
1148 
1149  if (dir == HT_DIR_X || dir == HT_DIR_Y) {
1150  side = 0;
1151  } else {
1152  TileIndex start = TileVirtXY(_thd.selstart.x, _thd.selstart.y);
1153  side = Delta(Delta(TileX(start), TileX(ti->tile)), Delta(TileY(start), TileY(ti->tile)));
1154  }
1155 
1156  DrawAutorailSelection(ti, _autorail_type[dir][side]);
1157  }
1158  return;
1159  }
1160 
1161  /* Check if it's inside the outer area? */
1162  if (!is_redsq && (tht == THT_NONE || tht == THT_RED) && _thd.outersize.x > 0 &&
1163  IsInsideBS(ti->x, _thd.pos.x + _thd.offs.x, _thd.size.x + _thd.outersize.x) &&
1164  IsInsideBS(ti->y, _thd.pos.y + _thd.offs.y, _thd.size.y + _thd.outersize.y)) {
1165  /* Draw a blue rect. */
1167  return;
1168  }
1169 }
1170 
1177 static int GetViewportY(Point tile)
1178 {
1179  /* Each increment in X or Y direction moves down by half a tile, i.e. TILE_PIXELS / 2. */
1180  return (tile.y * (int)(TILE_PIXELS / 2) + tile.x * (int)(TILE_PIXELS / 2) - TilePixelHeightOutsideMap(tile.x, tile.y)) << ZOOM_BASE_SHIFT;
1181 }
1182 
1187 {
1188  assert(_vd.dpi.top <= _vd.dpi.top + _vd.dpi.height);
1189  assert(_vd.dpi.left <= _vd.dpi.left + _vd.dpi.width);
1190 
1191  Point upper_left = InverseRemapCoords(_vd.dpi.left, _vd.dpi.top);
1192  Point upper_right = InverseRemapCoords(_vd.dpi.left + _vd.dpi.width, _vd.dpi.top);
1193 
1194  /* Transformations between tile coordinates and viewport rows/columns: See vp_column_row
1195  * column = y - x
1196  * row = x + y
1197  * x = (row - column) / 2
1198  * y = (row + column) / 2
1199  * Note: (row, columns) pairs are only valid, if they are both even or both odd.
1200  */
1201 
1202  /* Columns overlap with neighbouring columns by a half tile.
1203  * - Left column is column of upper_left (rounded down) and one column to the left.
1204  * - Right column is column of upper_right (rounded up) and one column to the right.
1205  * Note: Integer-division does not round down for negative numbers, so ensure rounding with another increment/decrement.
1206  */
1207  int left_column = (upper_left.y - upper_left.x) / (int)TILE_SIZE - 2;
1208  int right_column = (upper_right.y - upper_right.x) / (int)TILE_SIZE + 2;
1209 
1210  int potential_bridge_height = ZOOM_BASE * TILE_HEIGHT * _settings_game.construction.max_bridge_height;
1211 
1212  /* Rows overlap with neighbouring rows by a half tile.
1213  * The first row that could possibly be visible is the row above upper_left (if it is at height 0).
1214  * Due to integer-division not rounding down for negative numbers, we need another decrement.
1215  */
1216  int row = (upper_left.x + upper_left.y) / (int)TILE_SIZE - 2;
1217  bool last_row = false;
1218  for (; !last_row; row++) {
1219  last_row = true;
1220  for (int column = left_column; column <= right_column; column++) {
1221  /* Valid row/column? */
1222  if ((row + column) % 2 != 0) continue;
1223 
1224  Point tilecoord;
1225  tilecoord.x = (row - column) / 2;
1226  tilecoord.y = (row + column) / 2;
1227  assert(column == tilecoord.y - tilecoord.x);
1228  assert(row == tilecoord.y + tilecoord.x);
1229 
1230  TileType tile_type;
1231  _cur_ti.x = tilecoord.x * TILE_SIZE;
1232  _cur_ti.y = tilecoord.y * TILE_SIZE;
1233 
1234  if (IsInsideBS(tilecoord.x, 0, Map::SizeX()) && IsInsideBS(tilecoord.y, 0, Map::SizeY())) {
1235  /* This includes the south border at Map::MaxX / Map::MaxY. When terraforming we still draw tile selections there. */
1236  _cur_ti.tile = TileXY(tilecoord.x, tilecoord.y);
1237  tile_type = GetTileType(_cur_ti.tile);
1238  } else {
1239  _cur_ti.tile = INVALID_TILE;
1240  tile_type = MP_VOID;
1241  }
1242 
1243  if (tile_type != MP_VOID) {
1244  /* We are inside the map => paint landscape. */
1245  std::tie(_cur_ti.tileh, _cur_ti.z) = GetTilePixelSlope(_cur_ti.tile);
1246  } else {
1247  /* We are outside the map => paint black. */
1248  std::tie(_cur_ti.tileh, _cur_ti.z) = GetTilePixelSlopeOutsideMap(tilecoord.x, tilecoord.y);
1249  }
1250 
1251  int viewport_y = GetViewportY(tilecoord);
1252 
1253  if (viewport_y + MAX_TILE_EXTENT_BOTTOM < _vd.dpi.top) {
1254  /* The tile in this column is not visible yet.
1255  * Tiles in other columns may be visible, but we need more rows in any case. */
1256  last_row = false;
1257  continue;
1258  }
1259 
1260  int min_visible_height = viewport_y - (_vd.dpi.top + _vd.dpi.height);
1261  bool tile_visible = min_visible_height <= 0;
1262 
1263  if (tile_type != MP_VOID) {
1264  /* Is tile with buildings visible? */
1265  if (min_visible_height < MAX_TILE_EXTENT_TOP) tile_visible = true;
1266 
1267  if (IsBridgeAbove(_cur_ti.tile)) {
1268  /* Is the bridge visible? */
1269  TileIndex bridge_tile = GetNorthernBridgeEnd(_cur_ti.tile);
1270  int bridge_height = ZOOM_BASE * (GetBridgePixelHeight(bridge_tile) - TilePixelHeight(_cur_ti.tile));
1271  if (min_visible_height < bridge_height + MAX_TILE_EXTENT_TOP) tile_visible = true;
1272  }
1273 
1274  /* Would a higher bridge on a more southern tile be visible?
1275  * If yes, we need to loop over more rows to possibly find one. */
1276  if (min_visible_height < potential_bridge_height + MAX_TILE_EXTENT_TOP) last_row = false;
1277  } else {
1278  /* Outside of map. If we are on the north border of the map, there may still be a bridge visible,
1279  * so we need to loop over more rows to possibly find one. */
1280  if ((tilecoord.x <= 0 || tilecoord.y <= 0) && min_visible_height < potential_bridge_height + MAX_TILE_EXTENT_TOP) last_row = false;
1281  }
1282 
1283  if (tile_visible) {
1284  last_row = false;
1286  _vd.foundation[0] = -1;
1287  _vd.foundation[1] = -1;
1288  _vd.last_foundation_child[0] = nullptr;
1289  _vd.last_foundation_child[1] = nullptr;
1290 
1291  _tile_type_procs[tile_type]->draw_tile_proc(&_cur_ti);
1292  if (_cur_ti.tile != INVALID_TILE) DrawTileSelection(&_cur_ti);
1293  }
1294  }
1295  }
1296 }
1297 
1308 void ViewportAddString(const DrawPixelInfo *dpi, ZoomLevel small_from, const ViewportSign *sign, StringID string_normal, StringID string_small, StringID string_small_shadow, Colours colour)
1309 {
1310  bool small = dpi->zoom >= small_from;
1311 
1312  int left = dpi->left;
1313  int top = dpi->top;
1314  int right = left + dpi->width;
1315  int bottom = top + dpi->height;
1316 
1317  int sign_height = ScaleByZoom(WidgetDimensions::scaled.fullbevel.top + GetCharacterHeight(small ? FS_SMALL : FS_NORMAL) + WidgetDimensions::scaled.fullbevel.bottom, dpi->zoom);
1318  int sign_half_width = ScaleByZoom((small ? sign->width_small : sign->width_normal) / 2, dpi->zoom);
1319 
1320  if (bottom < sign->top ||
1321  top > sign->top + sign_height ||
1322  right < sign->center - sign_half_width ||
1323  left > sign->center + sign_half_width) {
1324  return;
1325  }
1326 
1327  if (!small) {
1328  AddStringToDraw(sign->center - sign_half_width, sign->top, string_normal, colour, sign->width_normal);
1329  } else {
1330  int shadow_offset = 0;
1331  if (string_small_shadow != STR_NULL) {
1332  shadow_offset = 4;
1333  AddStringToDraw(sign->center - sign_half_width + shadow_offset, sign->top, string_small_shadow, INVALID_COLOUR, sign->width_small | 0x8000);
1334  }
1335  AddStringToDraw(sign->center - sign_half_width, sign->top - shadow_offset, string_small, colour, sign->width_small | 0x8000);
1336  }
1337 }
1338 
1339 static Rect ExpandRectWithViewportSignMargins(Rect r, ZoomLevel zoom)
1340 {
1341  const int fh = std::max(GetCharacterHeight(FS_NORMAL), GetCharacterHeight(FS_SMALL));
1342  const int max_tw = _viewport_sign_maxwidth / 2 + 1;
1343  const int expand_y = ScaleByZoom(WidgetDimensions::scaled.fullbevel.top + fh + WidgetDimensions::scaled.fullbevel.bottom, zoom);
1344  const int expand_x = ScaleByZoom(WidgetDimensions::scaled.fullbevel.left + max_tw + WidgetDimensions::scaled.fullbevel.right, zoom);
1345 
1346  r.left -= expand_x;
1347  r.right += expand_x;
1348  r.top -= expand_y;
1349  r.bottom += expand_y;
1350 
1351  return r;
1352 }
1353 
1354 static void ViewportAddKdtreeSigns(DrawPixelInfo *dpi)
1355 {
1356  Rect search_rect{ dpi->left, dpi->top, dpi->left + dpi->width, dpi->top + dpi->height };
1357  search_rect = ExpandRectWithViewportSignMargins(search_rect, dpi->zoom);
1358 
1359  bool show_stations = HasBit(_display_opt, DO_SHOW_STATION_NAMES) && _game_mode != GM_MENU;
1360  bool show_waypoints = HasBit(_display_opt, DO_SHOW_WAYPOINT_NAMES) && _game_mode != GM_MENU;
1361  bool show_towns = HasBit(_display_opt, DO_SHOW_TOWN_NAMES) && _game_mode != GM_MENU;
1362  bool show_signs = HasBit(_display_opt, DO_SHOW_SIGNS) && !IsInvisibilitySet(TO_SIGNS);
1363  bool show_competitors = HasBit(_display_opt, DO_SHOW_COMPETITOR_SIGNS);
1364 
1365  /* Collect all the items first and draw afterwards, to ensure layering */
1366  std::vector<const BaseStation *> stations;
1367  std::vector<const Town *> towns;
1368  std::vector<const Sign *> signs;
1369 
1370  _viewport_sign_kdtree.FindContained(search_rect.left, search_rect.top, search_rect.right, search_rect.bottom, [&](const ViewportSignKdtreeItem & item) {
1371  switch (item.type) {
1372  case ViewportSignKdtreeItem::VKI_STATION: {
1373  if (!show_stations) break;
1374  const BaseStation *st = BaseStation::Get(item.id.station);
1375 
1376  /* Don't draw if station is owned by another company and competitor station names are hidden. Stations owned by none are never ignored. */
1377  if (!show_competitors && _local_company != st->owner && st->owner != OWNER_NONE) break;
1378 
1379  stations.push_back(st);
1380  break;
1381  }
1382 
1383  case ViewportSignKdtreeItem::VKI_WAYPOINT: {
1384  if (!show_waypoints) break;
1385  const BaseStation *st = BaseStation::Get(item.id.station);
1386 
1387  /* Don't draw if station is owned by another company and competitor station names are hidden. Stations owned by none are never ignored. */
1388  if (!show_competitors && _local_company != st->owner && st->owner != OWNER_NONE) break;
1389 
1390  stations.push_back(st);
1391  break;
1392  }
1393 
1394  case ViewportSignKdtreeItem::VKI_TOWN:
1395  if (!show_towns) break;
1396  towns.push_back(Town::Get(item.id.town));
1397  break;
1398 
1399  case ViewportSignKdtreeItem::VKI_SIGN: {
1400  if (!show_signs) break;
1401  const Sign *si = Sign::Get(item.id.sign);
1402 
1403  /* Don't draw if sign is owned by another company and competitor signs should be hidden.
1404  * Note: It is intentional that also signs owned by OWNER_NONE are hidden. Bankrupt
1405  * companies can leave OWNER_NONE signs after them. */
1406  if (!show_competitors && _local_company != si->owner && si->owner != OWNER_DEITY) break;
1407 
1408  signs.push_back(si);
1409  break;
1410  }
1411 
1412  default:
1413  NOT_REACHED();
1414  }
1415  });
1416 
1417  /* Layering order (bottom to top): Town names, signs, stations */
1418 
1419  for (const auto *t : towns) {
1420  SetDParam(0, t->index);
1421  SetDParam(1, t->cache.population);
1422  ViewportAddString(dpi, ZOOM_LVL_OUT_4X, &t->cache.sign,
1423  _settings_client.gui.population_in_label ? STR_VIEWPORT_TOWN_POP : STR_VIEWPORT_TOWN,
1424  STR_VIEWPORT_TOWN_TINY_WHITE, STR_VIEWPORT_TOWN_TINY_BLACK);
1425  }
1426 
1427  /* Do not draw signs nor station names if they are set invisible */
1428  if (IsInvisibilitySet(TO_SIGNS)) return;
1429 
1430  for (const auto *si : signs) {
1431  SetDParam(0, si->index);
1432  ViewportAddString(dpi, ZOOM_LVL_OUT_4X, &si->sign,
1433  STR_WHITE_SIGN,
1434  (IsTransparencySet(TO_SIGNS) || si->owner == OWNER_DEITY) ? STR_VIEWPORT_SIGN_SMALL_WHITE : STR_VIEWPORT_SIGN_SMALL_BLACK, STR_NULL,
1435  (si->owner == OWNER_NONE) ? COLOUR_GREY : (si->owner == OWNER_DEITY ? INVALID_COLOUR : _company_colours[si->owner]));
1436  }
1437 
1438  for (const auto *st : stations) {
1439  SetDParam(0, st->index);
1440  SetDParam(1, st->facilities);
1441  if (Station::IsExpected(st)) {
1442  /* Station */
1443  ViewportAddString(dpi, ZOOM_LVL_OUT_4X, &st->sign,
1444  STR_VIEWPORT_STATION, STR_VIEWPORT_STATION_TINY, STR_NULL,
1445  (st->owner == OWNER_NONE || !st->IsInUse()) ? COLOUR_GREY : _company_colours[st->owner]);
1446  } else {
1447  /* Waypoint */
1448  ViewportAddString(dpi, ZOOM_LVL_OUT_4X, &st->sign,
1449  STR_VIEWPORT_WAYPOINT, STR_VIEWPORT_WAYPOINT_TINY, STR_NULL,
1450  (st->owner == OWNER_NONE || !st->IsInUse()) ? COLOUR_GREY : _company_colours[st->owner]);
1451  }
1452  }
1453 }
1454 
1455 
1463 void ViewportSign::UpdatePosition(int center, int top, StringID str, StringID str_small)
1464 {
1465  if (this->width_normal != 0) this->MarkDirty();
1466 
1467  this->top = top;
1468 
1469  std::string name = GetString(str);
1471  this->center = center;
1472 
1473  /* zoomed out version */
1474  if (str_small != STR_NULL) {
1475  name = GetString(str_small);
1476  }
1478 
1479  this->MarkDirty();
1480 }
1481 
1489 {
1490  Rect zoomlevels[ZOOM_LVL_END];
1491 
1492  /* We don't know which size will be drawn, so mark the largest area dirty. */
1493  const uint half_width = std::max(this->width_normal, this->width_small) / 2 + 1;
1495 
1496  for (ZoomLevel zoom = ZOOM_LVL_BEGIN; zoom != ZOOM_LVL_END; zoom++) {
1497  /* FIXME: This doesn't switch to width_small when appropriate. */
1498  zoomlevels[zoom].left = this->center - ScaleByZoom(half_width, zoom);
1499  zoomlevels[zoom].top = this->top - ScaleByZoom(1, zoom);
1500  zoomlevels[zoom].right = this->center + ScaleByZoom(half_width, zoom);
1501  zoomlevels[zoom].bottom = this->top + ScaleByZoom(height, zoom);
1502  }
1503 
1504  for (const Window *w : Window::Iterate()) {
1505  Viewport *vp = w->viewport;
1506  if (vp != nullptr && vp->zoom <= maxzoom) {
1507  assert(vp->width != 0);
1508  Rect &zl = zoomlevels[vp->zoom];
1509  MarkViewportDirty(vp, zl.left, zl.top, zl.right, zl.bottom);
1510  }
1511  }
1512 }
1513 
1514 static void ViewportDrawTileSprites(const TileSpriteToDrawVector *tstdv)
1515 {
1516  for (const TileSpriteToDraw &ts : *tstdv) {
1517  DrawSpriteViewport(ts.image, ts.pal, ts.x, ts.y, ts.sub);
1518  }
1519 }
1520 
1523 {
1524  return true;
1525 }
1526 
1528 static void ViewportSortParentSprites(ParentSpriteToSortVector *psdv)
1529 {
1530  if (psdv->size() < 2) return;
1531 
1532  /* We rely on sprites being, for the most part, already ordered.
1533  * So we don't need to move many of them and can keep track of their
1534  * order efficiently by using stack. We always move sprites to the front
1535  * of the current position, i.e. to the top of the stack.
1536  * Also use special constants to indicate sorting state without
1537  * adding extra fields to ParentSpriteToDraw structure.
1538  */
1539  const uint32_t ORDER_COMPARED = UINT32_MAX; // Sprite was compared but we still need to compare the ones preceding it
1540  const uint32_t ORDER_RETURNED = UINT32_MAX - 1; // Makr sorted sprite in case there are other occurrences of it in the stack
1541  std::stack<ParentSpriteToDraw *> sprite_order;
1542  uint32_t next_order = 0;
1543 
1544  std::forward_list<std::pair<int64_t, ParentSpriteToDraw *>> sprite_list; // We store sprites in a list sorted by xmin+ymin
1545 
1546  /* Initialize sprite list and order. */
1547  for (auto p = psdv->rbegin(); p != psdv->rend(); p++) {
1548  sprite_list.emplace_front((*p)->xmin + (*p)->ymin, *p);
1549  sprite_order.push(*p);
1550  (*p)->order = next_order++;
1551  }
1552 
1553  sprite_list.sort();
1554 
1555  std::vector<ParentSpriteToDraw *> preceding; // Temporarily stores sprites that precede current and their position in the list
1556  auto preceding_prev = sprite_list.begin(); // Store iterator in case we need to delete a single preciding sprite
1557  auto out = psdv->begin(); // Iterator to output sorted sprites
1558 
1559  while (!sprite_order.empty()) {
1560 
1561  auto s = sprite_order.top();
1562  sprite_order.pop();
1563 
1564  /* Sprite is already sorted, ignore it. */
1565  if (s->order == ORDER_RETURNED) continue;
1566 
1567  /* Sprite was already compared, just need to output it. */
1568  if (s->order == ORDER_COMPARED) {
1569  *(out++) = s;
1570  s->order = ORDER_RETURNED;
1571  continue;
1572  }
1573 
1574  preceding.clear();
1575 
1576  /* We only need sprites with xmin <= s->xmax && ymin <= s->ymax && zmin <= s->zmax
1577  * So by iterating sprites with xmin + ymin <= s->xmax + s->ymax
1578  * we get all we need and some more that we filter out later.
1579  * We don't include zmin into the sum as there are usually more neighbors on x and y than z
1580  * so including it will actually increase the amount of false positives.
1581  * Also min coordinates can be > max so using max(xmin, xmax) + max(ymin, ymax)
1582  * to ensure that we iterate the current sprite as we need to remove it from the list.
1583  */
1584  auto ssum = std::max(s->xmax, s->xmin) + std::max(s->ymax, s->ymin);
1585  auto prev = sprite_list.before_begin();
1586  auto x = sprite_list.begin();
1587  while (x != sprite_list.end() && ((*x).first <= ssum)) {
1588  auto p = (*x).second;
1589  if (p == s) {
1590  /* We found the current sprite, remove it and move on. */
1591  x = sprite_list.erase_after(prev);
1592  continue;
1593  }
1594 
1595  auto p_prev = prev;
1596  prev = x++;
1597 
1598  if (s->xmax < p->xmin || s->ymax < p->ymin || s->zmax < p->zmin) continue;
1599  if (s->xmin <= p->xmax && // overlap in X?
1600  s->ymin <= p->ymax && // overlap in Y?
1601  s->zmin <= p->zmax) { // overlap in Z?
1602  if (s->xmin + s->xmax + s->ymin + s->ymax + s->zmin + s->zmax <=
1603  p->xmin + p->xmax + p->ymin + p->ymax + p->zmin + p->zmax) {
1604  continue;
1605  }
1606  }
1607  preceding.push_back(p);
1608  preceding_prev = p_prev;
1609  }
1610 
1611  if (preceding.empty()) {
1612  /* No preceding sprites, add current one to the output */
1613  *(out++) = s;
1614  s->order = ORDER_RETURNED;
1615  continue;
1616  }
1617 
1618  /* Optimization for the case when we only have 1 sprite to move. */
1619  if (preceding.size() == 1) {
1620  auto p = preceding[0];
1621  /* We can only output the preceding sprite if there can't be any other sprites preceding it. */
1622  if (p->xmax <= s->xmax && p->ymax <= s->ymax && p->zmax <= s->zmax) {
1623  p->order = ORDER_RETURNED;
1624  s->order = ORDER_RETURNED;
1625  sprite_list.erase_after(preceding_prev);
1626  *(out++) = p;
1627  *(out++) = s;
1628  continue;
1629  }
1630  }
1631 
1632  /* Sort all preceding sprites by order and assign new orders in reverse (as original sorter did). */
1633  std::sort(preceding.begin(), preceding.end(), [](const ParentSpriteToDraw *a, const ParentSpriteToDraw *b) {
1634  return a->order > b->order;
1635  });
1636 
1637  s->order = ORDER_COMPARED;
1638  sprite_order.push(s); // Still need to output so push it back for now
1639 
1640  for (auto p: preceding) {
1641  p->order = next_order++;
1642  sprite_order.push(p);
1643  }
1644  }
1645 }
1646 
1647 
1648 static void ViewportDrawParentSprites(const ParentSpriteToSortVector *psd, const ChildScreenSpriteToDrawVector *csstdv)
1649 {
1650  for (const ParentSpriteToDraw *ps : *psd) {
1651  if (ps->image != SPR_EMPTY_BOUNDING_BOX) DrawSpriteViewport(ps->image, ps->pal, ps->x, ps->y, ps->sub);
1652 
1653  int child_idx = ps->first_child;
1654  while (child_idx >= 0) {
1655  const ChildScreenSpriteToDraw *cs = csstdv->data() + child_idx;
1656  child_idx = cs->next;
1657  if (cs->relative) {
1658  DrawSpriteViewport(cs->image, cs->pal, ps->left + cs->x, ps->top + cs->y, cs->sub);
1659  } else {
1660  DrawSpriteViewport(cs->image, cs->pal, ps->x + cs->x, ps->y + cs->y, cs->sub);
1661  }
1662  }
1663  }
1664 }
1665 
1670 static void ViewportDrawBoundingBoxes(const ParentSpriteToSortVector *psd)
1671 {
1672  for (const ParentSpriteToDraw *ps : *psd) {
1673  Point pt1 = RemapCoords(ps->xmax + 1, ps->ymax + 1, ps->zmax + 1); // top front corner
1674  Point pt2 = RemapCoords(ps->xmin , ps->ymax + 1, ps->zmax + 1); // top left corner
1675  Point pt3 = RemapCoords(ps->xmax + 1, ps->ymin , ps->zmax + 1); // top right corner
1676  Point pt4 = RemapCoords(ps->xmax + 1, ps->ymax + 1, ps->zmin ); // bottom front corner
1677 
1678  DrawBox( pt1.x, pt1.y,
1679  pt2.x - pt1.x, pt2.y - pt1.y,
1680  pt3.x - pt1.x, pt3.y - pt1.y,
1681  pt4.x - pt1.x, pt4.y - pt1.y);
1682  }
1683 }
1684 
1689 {
1691  const DrawPixelInfo *dpi = _cur_dpi;
1692  void *dst;
1693  int right = UnScaleByZoom(dpi->width, dpi->zoom);
1694  int bottom = UnScaleByZoom(dpi->height, dpi->zoom);
1695 
1696  int colour = _string_colourmap[_dirty_block_colour & 0xF];
1697 
1698  dst = dpi->dst_ptr;
1699 
1700  uint8_t bo = UnScaleByZoom(dpi->left + dpi->top, dpi->zoom) & 1;
1701  do {
1702  for (int i = (bo ^= 1); i < right; i += 2) blitter->SetPixel(dst, i, 0, (uint8_t)colour);
1703  dst = blitter->MoveTo(dst, 0, 1);
1704  } while (--bottom > 0);
1705 }
1706 
1707 static void ViewportDrawStrings(ZoomLevel zoom, const StringSpriteToDrawVector *sstdv)
1708 {
1709  for (const StringSpriteToDraw &ss : *sstdv) {
1710  TextColour colour = TC_BLACK;
1711  bool small = HasBit(ss.width, 15);
1712  int w = GB(ss.width, 0, 15);
1713  int x = UnScaleByZoom(ss.x, zoom);
1714  int y = UnScaleByZoom(ss.y, zoom);
1716 
1717  if (ss.colour != INVALID_COLOUR) {
1718  if (IsTransparencySet(TO_SIGNS) && ss.string_id != STR_WHITE_SIGN) {
1719  /* Don't draw the rectangle.
1720  * Real colours need the TC_IS_PALETTE_COLOUR flag.
1721  * Otherwise colours from _string_colourmap are assumed. */
1722  colour = (TextColour)GetColourGradient(ss.colour, SHADE_LIGHTER) | TC_IS_PALETTE_COLOUR;
1723  } else {
1724  /* Draw the rectangle if 'transparent station signs' is off,
1725  * or if we are drawing a general text sign (STR_WHITE_SIGN). */
1726  DrawFrameRect(
1727  x, y, x + w - 1, y + h - 1, ss.colour,
1729  );
1730  }
1731  }
1732 
1733  DrawString(x + WidgetDimensions::scaled.fullbevel.left, x + w - 1 - WidgetDimensions::scaled.fullbevel.right, y + WidgetDimensions::scaled.fullbevel.top, ss.string, colour, SA_HOR_CENTER, false, small ? FS_SMALL : FS_NORMAL);
1734  }
1735 }
1736 
1737 void ViewportDoDraw(const Viewport *vp, int left, int top, int right, int bottom)
1738 {
1739  _vd.dpi.zoom = vp->zoom;
1740  int mask = ScaleByZoom(-1, vp->zoom);
1741 
1743 
1744  _vd.dpi.width = (right - left) & mask;
1745  _vd.dpi.height = (bottom - top) & mask;
1746  _vd.dpi.left = left & mask;
1747  _vd.dpi.top = top & mask;
1748  _vd.dpi.pitch = _cur_dpi->pitch;
1749  _vd.last_child = nullptr;
1750 
1751  int x = UnScaleByZoom(_vd.dpi.left - (vp->virtual_left & mask), vp->zoom) + vp->left;
1752  int y = UnScaleByZoom(_vd.dpi.top - (vp->virtual_top & mask), vp->zoom) + vp->top;
1753 
1754  _vd.dpi.dst_ptr = BlitterFactory::GetCurrentBlitter()->MoveTo(_cur_dpi->dst_ptr, x - _cur_dpi->left, y - _cur_dpi->top);
1755  AutoRestoreBackup dpi_backup(_cur_dpi, &_vd.dpi);
1756 
1758  ViewportAddVehicles(&_vd.dpi);
1759 
1760  ViewportAddKdtreeSigns(&_vd.dpi);
1761 
1762  DrawTextEffects(&_vd.dpi);
1763 
1764  if (!_vd.tile_sprites_to_draw.empty()) ViewportDrawTileSprites(&_vd.tile_sprites_to_draw);
1765 
1766  for (auto &psd : _vd.parent_sprites_to_draw) {
1767  _vd.parent_sprites_to_sort.push_back(&psd);
1768  }
1769 
1770  _vp_sprite_sorter(&_vd.parent_sprites_to_sort);
1771  ViewportDrawParentSprites(&_vd.parent_sprites_to_sort, &_vd.child_screen_sprites_to_draw);
1772 
1773  if (_draw_bounding_boxes) ViewportDrawBoundingBoxes(&_vd.parent_sprites_to_sort);
1774  if (_draw_dirty_blocks) ViewportDrawDirtyBlocks();
1775 
1776  DrawPixelInfo dp = _vd.dpi;
1777  ZoomLevel zoom = _vd.dpi.zoom;
1778  dp.zoom = ZOOM_LVL_MIN;
1779  dp.width = UnScaleByZoom(dp.width, zoom);
1780  dp.height = UnScaleByZoom(dp.height, zoom);
1781  _cur_dpi = &dp;
1782 
1783  if (vp->overlay != nullptr && vp->overlay->GetCargoMask() != 0 && vp->overlay->GetCompanyMask() != 0) {
1784  /* translate to window coordinates */
1785  dp.left = x;
1786  dp.top = y;
1787  vp->overlay->Draw(&dp);
1788  }
1789 
1790  if (!_vd.string_sprites_to_draw.empty()) {
1791  /* translate to world coordinates */
1792  dp.left = UnScaleByZoom(_vd.dpi.left, zoom);
1793  dp.top = UnScaleByZoom(_vd.dpi.top, zoom);
1794  ViewportDrawStrings(zoom, &_vd.string_sprites_to_draw);
1795  }
1796 
1797  _vd.string_sprites_to_draw.clear();
1798  _vd.tile_sprites_to_draw.clear();
1799  _vd.parent_sprites_to_draw.clear();
1800  _vd.parent_sprites_to_sort.clear();
1801  _vd.child_screen_sprites_to_draw.clear();
1802 }
1803 
1804 static inline void ViewportDraw(const Viewport *vp, int left, int top, int right, int bottom)
1805 {
1806  if (right <= vp->left || bottom <= vp->top) return;
1807 
1808  if (left >= vp->left + vp->width) return;
1809 
1810  if (left < vp->left) left = vp->left;
1811  if (right > vp->left + vp->width) right = vp->left + vp->width;
1812 
1813  if (top >= vp->top + vp->height) return;
1814 
1815  if (top < vp->top) top = vp->top;
1816  if (bottom > vp->top + vp->height) bottom = vp->top + vp->height;
1817 
1818  ViewportDoDraw(vp,
1819  ScaleByZoom(left - vp->left, vp->zoom) + vp->virtual_left,
1820  ScaleByZoom(top - vp->top, vp->zoom) + vp->virtual_top,
1821  ScaleByZoom(right - vp->left, vp->zoom) + vp->virtual_left,
1822  ScaleByZoom(bottom - vp->top, vp->zoom) + vp->virtual_top
1823  );
1824 }
1825 
1830 {
1832 
1833  DrawPixelInfo *dpi = _cur_dpi;
1834 
1835  dpi->left += this->left;
1836  dpi->top += this->top;
1837 
1838  ViewportDraw(this->viewport, dpi->left, dpi->top, dpi->left + dpi->width, dpi->top + dpi->height);
1839 
1840  dpi->left -= this->left;
1841  dpi->top -= this->top;
1842 }
1843 
1854 static inline void ClampViewportToMap(const Viewport *vp, int *scroll_x, int *scroll_y)
1855 {
1856  /* Centre of the viewport is hot spot. */
1857  Point pt = {
1858  *scroll_x + vp->virtual_width / 2,
1859  *scroll_y + vp->virtual_height / 2
1860  };
1861 
1862  /* Find nearest tile that is within borders of the map. */
1863  bool clamped;
1864  pt = InverseRemapCoords2(pt.x, pt.y, true, &clamped);
1865 
1866  if (clamped) {
1867  /* Convert back to viewport coordinates and remove centering. */
1868  pt = RemapCoords2(pt.x, pt.y);
1869  *scroll_x = pt.x - vp->virtual_width / 2;
1870  *scroll_y = pt.y - vp->virtual_height / 2;
1871  }
1872 }
1873 
1886 static void ClampSmoothScroll(uint32_t delta_ms, int64_t delta_hi, int64_t delta_lo, int &delta_hi_clamped, int &delta_lo_clamped)
1887 {
1889  constexpr int PIXELS_PER_TILE = TILE_PIXELS * 2 * ZOOM_BASE;
1890 
1891  assert(delta_hi != 0);
1892 
1893  /* Move at most 75% of the distance every 30ms, for a smooth experience */
1894  int64_t delta_left = delta_hi * std::pow(0.75, delta_ms / 30.0);
1895  /* Move never more than 16 tiles per 30ms. */
1896  int max_scroll = Map::ScaleBySize1D(16 * PIXELS_PER_TILE * delta_ms / 30);
1897 
1898  /* We never go over the max_scroll speed. */
1899  delta_hi_clamped = Clamp(delta_hi - delta_left, -max_scroll, max_scroll);
1900  /* The lower delta is in ratio of the higher delta, so we keep going straight at the destination. */
1901  delta_lo_clamped = delta_lo * delta_hi_clamped / delta_hi;
1902 
1903  /* Ensure we always move (delta_hi can't be zero). */
1904  if (delta_hi_clamped == 0) {
1905  delta_hi_clamped = delta_hi > 0 ? 1 : -1;
1906  }
1907 }
1908 
1913 void UpdateViewportPosition(Window *w, uint32_t delta_ms)
1914 {
1915  const Viewport *vp = w->viewport;
1916 
1918  const Vehicle *veh = Vehicle::Get(w->viewport->follow_vehicle);
1919  Point pt = MapXYZToViewport(vp, veh->x_pos, veh->y_pos, veh->z_pos);
1920 
1921  w->viewport->scrollpos_x = pt.x;
1922  w->viewport->scrollpos_y = pt.y;
1923  SetViewportPosition(w, pt.x, pt.y);
1924  } else {
1925  /* Ensure the destination location is within the map */
1927 
1928  int delta_x = w->viewport->dest_scrollpos_x - w->viewport->scrollpos_x;
1929  int delta_y = w->viewport->dest_scrollpos_y - w->viewport->scrollpos_y;
1930 
1931  int current_x = w->viewport->scrollpos_x;
1932  int current_y = w->viewport->scrollpos_y;
1933 
1934  bool update_overlay = false;
1935  if (delta_x != 0 || delta_y != 0) {
1937  int delta_x_clamped;
1938  int delta_y_clamped;
1939 
1940  if (abs(delta_x) > abs(delta_y)) {
1941  ClampSmoothScroll(delta_ms, delta_x, delta_y, delta_x_clamped, delta_y_clamped);
1942  } else {
1943  ClampSmoothScroll(delta_ms, delta_y, delta_x, delta_y_clamped, delta_x_clamped);
1944  }
1945 
1946  w->viewport->scrollpos_x += delta_x_clamped;
1947  w->viewport->scrollpos_y += delta_y_clamped;
1948  } else {
1951  }
1952  update_overlay = (w->viewport->scrollpos_x == w->viewport->dest_scrollpos_x &&
1954  }
1955 
1957 
1958  /* When moving small amounts around the border we can get stuck, and
1959  * not actually move. In those cases, teleport to the destination. */
1960  if ((delta_x != 0 || delta_y != 0) && current_x == w->viewport->scrollpos_x && current_y == w->viewport->scrollpos_y) {
1963  }
1964 
1965  SetViewportPosition(w, w->viewport->scrollpos_x, w->viewport->scrollpos_y);
1966  if (update_overlay) RebuildViewportOverlay(w);
1967  }
1968 }
1969 
1980 static bool MarkViewportDirty(const Viewport *vp, int left, int top, int right, int bottom)
1981 {
1982  /* Rounding wrt. zoom-out level */
1983  right += (1 << vp->zoom) - 1;
1984  bottom += (1 << vp->zoom) - 1;
1985 
1986  right -= vp->virtual_left;
1987  if (right <= 0) return false;
1988 
1989  bottom -= vp->virtual_top;
1990  if (bottom <= 0) return false;
1991 
1992  left = std::max(0, left - vp->virtual_left);
1993 
1994  if (left >= vp->virtual_width) return false;
1995 
1996  top = std::max(0, top - vp->virtual_top);
1997 
1998  if (top >= vp->virtual_height) return false;
1999 
2000  AddDirtyBlock(
2001  UnScaleByZoomLower(left, vp->zoom) + vp->left,
2002  UnScaleByZoomLower(top, vp->zoom) + vp->top,
2003  UnScaleByZoom(right, vp->zoom) + vp->left + 1,
2004  UnScaleByZoom(bottom, vp->zoom) + vp->top + 1
2005  );
2006 
2007  return true;
2008 }
2009 
2019 bool MarkAllViewportsDirty(int left, int top, int right, int bottom)
2020 {
2021  bool dirty = false;
2022 
2023  for (const Window *w : Window::Iterate()) {
2024  Viewport *vp = w->viewport;
2025  if (vp != nullptr) {
2026  assert(vp->width != 0);
2027  if (MarkViewportDirty(vp, left, top, right, bottom)) dirty = true;
2028  }
2029  }
2030 
2031  return dirty;
2032 }
2033 
2034 void ConstrainAllViewportsZoom()
2035 {
2036  for (Window *w : Window::Iterate()) {
2037  if (w->viewport == nullptr) continue;
2038 
2039  ZoomLevel zoom = static_cast<ZoomLevel>(Clamp(w->viewport->zoom, _settings_client.gui.zoom_min, _settings_client.gui.zoom_max));
2040  if (zoom != w->viewport->zoom) {
2041  while (w->viewport->zoom < zoom) DoZoomInOutWindow(ZOOM_OUT, w);
2042  while (w->viewport->zoom > zoom) DoZoomInOutWindow(ZOOM_IN, w);
2043  }
2044  }
2045 }
2046 
2054 void MarkTileDirtyByTile(TileIndex tile, int bridge_level_offset, int tile_height_override)
2055 {
2056  Point pt = RemapCoords(TileX(tile) * TILE_SIZE, TileY(tile) * TILE_SIZE, tile_height_override * TILE_HEIGHT);
2058  pt.x - MAX_TILE_EXTENT_LEFT,
2059  pt.y - MAX_TILE_EXTENT_TOP - ZOOM_BASE * TILE_HEIGHT * bridge_level_offset,
2060  pt.x + MAX_TILE_EXTENT_RIGHT,
2061  pt.y + MAX_TILE_EXTENT_BOTTOM);
2062 }
2063 
2072 {
2073  int x_size = _thd.size.x;
2074  int y_size = _thd.size.y;
2075 
2076  if (!_thd.diagonal) { // Selecting in a straight rectangle (or a single square)
2077  int x_start = _thd.pos.x;
2078  int y_start = _thd.pos.y;
2079 
2080  if (_thd.outersize.x != 0) {
2081  x_size += _thd.outersize.x;
2082  x_start += _thd.offs.x;
2083  y_size += _thd.outersize.y;
2084  y_start += _thd.offs.y;
2085  }
2086 
2087  x_size -= TILE_SIZE;
2088  y_size -= TILE_SIZE;
2089 
2090  assert(x_size >= 0);
2091  assert(y_size >= 0);
2092 
2093  int x_end = Clamp(x_start + x_size, 0, Map::SizeX() * TILE_SIZE - TILE_SIZE);
2094  int y_end = Clamp(y_start + y_size, 0, Map::SizeY() * TILE_SIZE - TILE_SIZE);
2095 
2096  x_start = Clamp(x_start, 0, Map::SizeX() * TILE_SIZE - TILE_SIZE);
2097  y_start = Clamp(y_start, 0, Map::SizeY() * TILE_SIZE - TILE_SIZE);
2098 
2099  /* make sure everything is multiple of TILE_SIZE */
2100  assert((x_end | y_end | x_start | y_start) % TILE_SIZE == 0);
2101 
2102  /* How it works:
2103  * Suppose we have to mark dirty rectangle of 3x4 tiles:
2104  * x
2105  * xxx
2106  * xxxxx
2107  * xxxxx
2108  * xxx
2109  * x
2110  * This algorithm marks dirty columns of tiles, so it is done in 3+4-1 steps:
2111  * 1) x 2) x
2112  * xxx Oxx
2113  * Oxxxx xOxxx
2114  * xxxxx Oxxxx
2115  * xxx xxx
2116  * x x
2117  * And so forth...
2118  */
2119 
2120  int top_x = x_end; // coordinates of top dirty tile
2121  int top_y = y_start;
2122  int bot_x = top_x; // coordinates of bottom dirty tile
2123  int bot_y = top_y;
2124 
2125  do {
2126  /* topmost dirty point */
2127  TileIndex top_tile = TileVirtXY(top_x, top_y);
2128  Point top = RemapCoords(top_x, top_y, GetTileMaxPixelZ(top_tile));
2129 
2130  /* bottommost point */
2131  TileIndex bottom_tile = TileVirtXY(bot_x, bot_y);
2132  Point bot = RemapCoords(bot_x + TILE_SIZE, bot_y + TILE_SIZE, GetTilePixelZ(bottom_tile)); // bottommost point
2133 
2134  /* the 'x' coordinate of 'top' and 'bot' is the same (and always in the same distance from tile middle),
2135  * tile height/slope affects only the 'y' on-screen coordinate! */
2136 
2137  int l = top.x - TILE_PIXELS * ZOOM_BASE; // 'x' coordinate of left side of the dirty rectangle
2138  int t = top.y; // 'y' coordinate of top side of the dirty rectangle
2139  int r = top.x + TILE_PIXELS * ZOOM_BASE; // 'x' coordinate of right side of the dirty rectangle
2140  int b = bot.y; // 'y' coordinate of bottom side of the dirty rectangle
2141 
2142  static const int OVERLAY_WIDTH = 4 * ZOOM_BASE; // part of selection sprites is drawn outside the selected area (in particular: terraforming)
2143 
2144  /* For halftile foundations on SLOPE_STEEP_S the sprite extents some more towards the top */
2145  MarkAllViewportsDirty(l - OVERLAY_WIDTH, t - OVERLAY_WIDTH - TILE_HEIGHT * ZOOM_BASE, r + OVERLAY_WIDTH, b + OVERLAY_WIDTH);
2146 
2147  /* haven't we reached the topmost tile yet? */
2148  if (top_x != x_start) {
2149  top_x -= TILE_SIZE;
2150  } else {
2151  top_y += TILE_SIZE;
2152  }
2153 
2154  /* the way the bottom tile changes is different when we reach the bottommost tile */
2155  if (bot_y != y_end) {
2156  bot_y += TILE_SIZE;
2157  } else {
2158  bot_x -= TILE_SIZE;
2159  }
2160  } while (bot_x >= top_x);
2161  } else { // Selecting in a 45 degrees rotated (diagonal) rectangle.
2162  /* a_size, b_size describe a rectangle with rotated coordinates */
2163  int a_size = x_size + y_size, b_size = x_size - y_size;
2164 
2165  int interval_a = a_size < 0 ? -(int)TILE_SIZE : (int)TILE_SIZE;
2166  int interval_b = b_size < 0 ? -(int)TILE_SIZE : (int)TILE_SIZE;
2167 
2168  for (int a = -interval_a; a != a_size + interval_a; a += interval_a) {
2169  for (int b = -interval_b; b != b_size + interval_b; b += interval_b) {
2170  uint x = (_thd.pos.x + (a + b) / 2) / TILE_SIZE;
2171  uint y = (_thd.pos.y + (a - b) / 2) / TILE_SIZE;
2172 
2173  if (x < Map::MaxX() && y < Map::MaxY()) {
2174  MarkTileDirtyByTile(TileXY(x, y));
2175  }
2176  }
2177  }
2178  }
2179 }
2180 
2181 
2182 void SetSelectionRed(bool b)
2183 {
2184  _thd.make_square_red = b;
2186 }
2187 
2196 static bool CheckClickOnViewportSign(const Viewport *vp, int x, int y, const ViewportSign *sign)
2197 {
2198  bool small = (vp->zoom >= ZOOM_LVL_OUT_4X);
2199  int sign_half_width = ScaleByZoom((small ? sign->width_small : sign->width_normal) / 2, vp->zoom);
2200  int sign_height = ScaleByZoom(WidgetDimensions::scaled.fullbevel.top + GetCharacterHeight(small ? FS_SMALL : FS_NORMAL) + WidgetDimensions::scaled.fullbevel.bottom, vp->zoom);
2201 
2202  return y >= sign->top && y < sign->top + sign_height &&
2203  x >= sign->center - sign_half_width && x < sign->center + sign_half_width;
2204 }
2205 
2206 
2214 static bool CheckClickOnViewportSign(const Viewport *vp, int x, int y)
2215 {
2216  if (_game_mode == GM_MENU) return false;
2217 
2218  x = ScaleByZoom(x - vp->left, vp->zoom) + vp->virtual_left;
2219  y = ScaleByZoom(y - vp->top, vp->zoom) + vp->virtual_top;
2220 
2221  Rect search_rect{ x - 1, y - 1, x + 1, y + 1 };
2222  search_rect = ExpandRectWithViewportSignMargins(search_rect, vp->zoom);
2223 
2226  bool show_towns = HasBit(_display_opt, DO_SHOW_TOWN_NAMES);
2227  bool show_signs = HasBit(_display_opt, DO_SHOW_SIGNS) && !IsInvisibilitySet(TO_SIGNS);
2228  bool show_competitors = HasBit(_display_opt, DO_SHOW_COMPETITOR_SIGNS);
2229 
2230  /* Topmost of each type that was hit */
2231  BaseStation *st = nullptr, *last_st = nullptr;
2232  Town *t = nullptr, *last_t = nullptr;
2233  Sign *si = nullptr, *last_si = nullptr;
2234 
2235  /* See ViewportAddKdtreeSigns() for details on the search logic */
2236  _viewport_sign_kdtree.FindContained(search_rect.left, search_rect.top, search_rect.right, search_rect.bottom, [&](const ViewportSignKdtreeItem & item) {
2237  switch (item.type) {
2238  case ViewportSignKdtreeItem::VKI_STATION:
2239  if (!show_stations) break;
2240  st = BaseStation::Get(item.id.station);
2241  if (!show_competitors && _local_company != st->owner && st->owner != OWNER_NONE) break;
2242  if (CheckClickOnViewportSign(vp, x, y, &st->sign)) last_st = st;
2243  break;
2244 
2245  case ViewportSignKdtreeItem::VKI_WAYPOINT:
2246  if (!show_waypoints) break;
2247  st = BaseStation::Get(item.id.station);
2248  if (!show_competitors && _local_company != st->owner && st->owner != OWNER_NONE) break;
2249  if (CheckClickOnViewportSign(vp, x, y, &st->sign)) last_st = st;
2250  break;
2251 
2252  case ViewportSignKdtreeItem::VKI_TOWN:
2253  if (!show_towns) break;
2254  t = Town::Get(item.id.town);
2255  if (CheckClickOnViewportSign(vp, x, y, &t->cache.sign)) last_t = t;
2256  break;
2257 
2258  case ViewportSignKdtreeItem::VKI_SIGN:
2259  if (!show_signs) break;
2260  si = Sign::Get(item.id.sign);
2261  if (!show_competitors && _local_company != si->owner && si->owner != OWNER_DEITY) break;
2262  if (CheckClickOnViewportSign(vp, x, y, &si->sign)) last_si = si;
2263  break;
2264 
2265  default:
2266  NOT_REACHED();
2267  }
2268  });
2269 
2270  /* Select which hit to handle based on priority */
2271  if (last_st != nullptr) {
2272  if (Station::IsExpected(last_st)) {
2273  ShowStationViewWindow(last_st->index);
2274  } else {
2276  }
2277  return true;
2278  } else if (last_t != nullptr) {
2279  ShowTownViewWindow(last_t->index);
2280  return true;
2281  } else if (last_si != nullptr) {
2282  HandleClickOnSign(last_si);
2283  return true;
2284  } else {
2285  return false;
2286  }
2287 }
2288 
2289 
2290 ViewportSignKdtreeItem ViewportSignKdtreeItem::MakeStation(StationID id)
2291 {
2293  item.type = VKI_STATION;
2294  item.id.station = id;
2295 
2296  const Station *st = Station::Get(id);
2297  assert(st->sign.kdtree_valid);
2298  item.center = st->sign.center;
2299  item.top = st->sign.top;
2300 
2301  /* Assume the sign can be a candidate for drawing, so measure its width */
2302  _viewport_sign_maxwidth = std::max<int>({_viewport_sign_maxwidth, st->sign.width_normal, st->sign.width_small});
2303 
2304  return item;
2305 }
2306 
2307 ViewportSignKdtreeItem ViewportSignKdtreeItem::MakeWaypoint(StationID id)
2308 {
2310  item.type = VKI_WAYPOINT;
2311  item.id.station = id;
2312 
2313  const Waypoint *st = Waypoint::Get(id);
2314  assert(st->sign.kdtree_valid);
2315  item.center = st->sign.center;
2316  item.top = st->sign.top;
2317 
2318  /* Assume the sign can be a candidate for drawing, so measure its width */
2319  _viewport_sign_maxwidth = std::max<int>({_viewport_sign_maxwidth, st->sign.width_normal, st->sign.width_small});
2320 
2321  return item;
2322 }
2323 
2324 ViewportSignKdtreeItem ViewportSignKdtreeItem::MakeTown(TownID id)
2325 {
2327  item.type = VKI_TOWN;
2328  item.id.town = id;
2329 
2330  const Town *town = Town::Get(id);
2331  assert(town->cache.sign.kdtree_valid);
2332  item.center = town->cache.sign.center;
2333  item.top = town->cache.sign.top;
2334 
2335  /* Assume the sign can be a candidate for drawing, so measure its width */
2336  _viewport_sign_maxwidth = std::max<int>({_viewport_sign_maxwidth, town->cache.sign.width_normal, town->cache.sign.width_small});
2337 
2338  return item;
2339 }
2340 
2341 ViewportSignKdtreeItem ViewportSignKdtreeItem::MakeSign(SignID id)
2342 {
2344  item.type = VKI_SIGN;
2345  item.id.sign = id;
2346 
2347  const Sign *sign = Sign::Get(id);
2348  assert(sign->sign.kdtree_valid);
2349  item.center = sign->sign.center;
2350  item.top = sign->sign.top;
2351 
2352  /* Assume the sign can be a candidate for drawing, so measure its width */
2353  _viewport_sign_maxwidth = std::max<int>({_viewport_sign_maxwidth, sign->sign.width_normal, sign->sign.width_small});
2354 
2355  return item;
2356 }
2357 
2358 void RebuildViewportKdtree()
2359 {
2360  /* Reset biggest size sign seen */
2361  _viewport_sign_maxwidth = 0;
2362 
2363  std::vector<ViewportSignKdtreeItem> items;
2365 
2366  for (const Station *st : Station::Iterate()) {
2367  if (st->sign.kdtree_valid) items.push_back(ViewportSignKdtreeItem::MakeStation(st->index));
2368  }
2369 
2370  for (const Waypoint *wp : Waypoint::Iterate()) {
2371  if (wp->sign.kdtree_valid) items.push_back(ViewportSignKdtreeItem::MakeWaypoint(wp->index));
2372  }
2373 
2374  for (const Town *town : Town::Iterate()) {
2375  if (town->cache.sign.kdtree_valid) items.push_back(ViewportSignKdtreeItem::MakeTown(town->index));
2376  }
2377 
2378  for (const Sign *sign : Sign::Iterate()) {
2379  if (sign->sign.kdtree_valid) items.push_back(ViewportSignKdtreeItem::MakeSign(sign->index));
2380  }
2381 
2382  _viewport_sign_kdtree.Build(items.begin(), items.end());
2383 }
2384 
2385 
2386 static bool CheckClickOnLandscape(const Viewport *vp, int x, int y)
2387 {
2388  Point pt = TranslateXYToTileCoord(vp, x, y);
2389 
2390  if (pt.x != -1) return ClickTile(TileVirtXY(pt.x, pt.y));
2391  return true;
2392 }
2393 
2394 static void PlaceObject()
2395 {
2396  Point pt;
2397  Window *w;
2398 
2399  pt = GetTileBelowCursor();
2400  if (pt.x == -1) return;
2401 
2402  if ((_thd.place_mode & HT_DRAG_MASK) == HT_POINT) {
2403  pt.x += TILE_SIZE / 2;
2404  pt.y += TILE_SIZE / 2;
2405  }
2406 
2407  _tile_fract_coords.x = pt.x & TILE_UNIT_MASK;
2408  _tile_fract_coords.y = pt.y & TILE_UNIT_MASK;
2409 
2410  w = _thd.GetCallbackWnd();
2411  if (w != nullptr) w->OnPlaceObject(pt, TileVirtXY(pt.x, pt.y));
2412 }
2413 
2414 
2415 bool HandleViewportClicked(const Viewport *vp, int x, int y)
2416 {
2417  const Vehicle *v = CheckClickOnVehicle(vp, x, y);
2418 
2419  if (_thd.place_mode & HT_VEHICLE) {
2420  if (v != nullptr && VehicleClicked(v)) return true;
2421  }
2422 
2423  /* Vehicle placement mode already handled above. */
2424  if ((_thd.place_mode & HT_DRAG_MASK) != HT_NONE) {
2425  PlaceObject();
2426  return true;
2427  }
2428 
2429  if (CheckClickOnViewportSign(vp, x, y)) return true;
2430  bool result = CheckClickOnLandscape(vp, x, y);
2431 
2432  if (v != nullptr) {
2433  Debug(misc, 2, "Vehicle {} (index {}) at {}", v->unitnumber, v->index, fmt::ptr(v));
2435  v = v->First();
2436  if (_ctrl_pressed && v->owner == _local_company) {
2437  StartStopVehicle(v, true);
2438  } else {
2440  }
2441  }
2442  return true;
2443  }
2444  return result;
2445 }
2446 
2447 void RebuildViewportOverlay(Window *w)
2448 {
2449  if (w->viewport->overlay != nullptr &&
2450  w->viewport->overlay->GetCompanyMask() != 0 &&
2451  w->viewport->overlay->GetCargoMask() != 0) {
2452  w->viewport->overlay->SetDirty();
2453  w->SetDirty();
2454  }
2455 }
2456 
2466 bool ScrollWindowTo(int x, int y, int z, Window *w, bool instant)
2467 {
2468  /* The slope cannot be acquired outside of the map, so make sure we are always within the map. */
2469  if (z == -1) {
2470  if ( x >= 0 && x <= (int)Map::SizeX() * (int)TILE_SIZE - 1
2471  && y >= 0 && y <= (int)Map::SizeY() * (int)TILE_SIZE - 1) {
2472  z = GetSlopePixelZ(x, y);
2473  } else {
2474  z = TileHeightOutsideMap(x / (int)TILE_SIZE, y / (int)TILE_SIZE);
2475  }
2476  }
2477 
2478  Point pt = MapXYZToViewport(w->viewport, x, y, z);
2480 
2481  if (w->viewport->dest_scrollpos_x == pt.x && w->viewport->dest_scrollpos_y == pt.y) return false;
2482 
2483  if (instant) {
2484  w->viewport->scrollpos_x = pt.x;
2485  w->viewport->scrollpos_y = pt.y;
2486  RebuildViewportOverlay(w);
2487  }
2488 
2489  w->viewport->dest_scrollpos_x = pt.x;
2490  w->viewport->dest_scrollpos_y = pt.y;
2491  return true;
2492 }
2493 
2501 bool ScrollWindowToTile(TileIndex tile, Window *w, bool instant)
2502 {
2503  return ScrollWindowTo(TileX(tile) * TILE_SIZE, TileY(tile) * TILE_SIZE, -1, w, instant);
2504 }
2505 
2512 bool ScrollMainWindowToTile(TileIndex tile, bool instant)
2513 {
2514  return ScrollMainWindowTo(TileX(tile) * TILE_SIZE + TILE_SIZE / 2, TileY(tile) * TILE_SIZE + TILE_SIZE / 2, -1, instant);
2515 }
2516 
2522 {
2523  TileIndex old;
2524 
2525  old = _thd.redsq;
2526  _thd.redsq = tile;
2527 
2528  if (tile != old) {
2529  if (tile != INVALID_TILE) MarkTileDirtyByTile(tile);
2530  if (old != INVALID_TILE) MarkTileDirtyByTile(old);
2531  }
2532 }
2533 
2539 void SetTileSelectSize(int w, int h)
2540 {
2541  _thd.new_size.x = w * TILE_SIZE;
2542  _thd.new_size.y = h * TILE_SIZE;
2543  _thd.new_outersize.x = 0;
2544  _thd.new_outersize.y = 0;
2545 }
2546 
2547 void SetTileSelectBigSize(int ox, int oy, int sx, int sy)
2548 {
2549  _thd.offs.x = ox * TILE_SIZE;
2550  _thd.offs.y = oy * TILE_SIZE;
2551  _thd.new_outersize.x = sx * TILE_SIZE;
2552  _thd.new_outersize.y = sy * TILE_SIZE;
2553 }
2554 
2556 static HighLightStyle GetAutorailHT(int x, int y)
2557 {
2558  return HT_RAIL | _autorail_piece[x & TILE_UNIT_MASK][y & TILE_UNIT_MASK];
2559 }
2560 
2565 {
2566  this->pos.x = 0;
2567  this->pos.y = 0;
2568  this->new_pos.x = 0;
2569  this->new_pos.y = 0;
2570 }
2571 
2577 {
2578  return (this->place_mode & HT_DIAGONAL) != 0 && _ctrl_pressed && _left_button_down;
2579 }
2580 
2586 {
2587  return FindWindowById(this->window_class, this->window_number);
2588 }
2589 
2590 
2591 
2600 {
2601  int x1;
2602  int y1;
2603 
2604  if (_thd.freeze) return;
2605 
2606  HighLightStyle new_drawstyle = HT_NONE;
2607  bool new_diagonal = false;
2608 
2609  if ((_thd.place_mode & HT_DRAG_MASK) == HT_SPECIAL) {
2610  x1 = _thd.selend.x;
2611  y1 = _thd.selend.y;
2612  if (x1 != -1) {
2613  int x2 = _thd.selstart.x & ~TILE_UNIT_MASK;
2614  int y2 = _thd.selstart.y & ~TILE_UNIT_MASK;
2615  x1 &= ~TILE_UNIT_MASK;
2616  y1 &= ~TILE_UNIT_MASK;
2617 
2618  if (_thd.IsDraggingDiagonal()) {
2619  new_diagonal = true;
2620  } else {
2621  if (x1 >= x2) Swap(x1, x2);
2622  if (y1 >= y2) Swap(y1, y2);
2623  }
2624  _thd.new_pos.x = x1;
2625  _thd.new_pos.y = y1;
2626  _thd.new_size.x = x2 - x1;
2627  _thd.new_size.y = y2 - y1;
2628  if (!new_diagonal) {
2629  _thd.new_size.x += TILE_SIZE;
2630  _thd.new_size.y += TILE_SIZE;
2631  }
2632  new_drawstyle = _thd.next_drawstyle;
2633  }
2634  } else if ((_thd.place_mode & HT_DRAG_MASK) != HT_NONE) {
2635  Point pt = GetTileBelowCursor();
2636  x1 = pt.x;
2637  y1 = pt.y;
2638  if (x1 != -1) {
2639  switch (_thd.place_mode & HT_DRAG_MASK) {
2640  case HT_RECT:
2641  new_drawstyle = HT_RECT;
2642  break;
2643  case HT_POINT:
2644  new_drawstyle = HT_POINT;
2645  x1 += TILE_SIZE / 2;
2646  y1 += TILE_SIZE / 2;
2647  break;
2648  case HT_RAIL:
2649  /* Draw one highlighted tile in any direction */
2650  new_drawstyle = GetAutorailHT(pt.x, pt.y);
2651  break;
2652  case HT_LINE:
2653  switch (_thd.place_mode & HT_DIR_MASK) {
2654  case HT_DIR_X: new_drawstyle = HT_LINE | HT_DIR_X; break;
2655  case HT_DIR_Y: new_drawstyle = HT_LINE | HT_DIR_Y; break;
2656 
2657  case HT_DIR_HU:
2658  case HT_DIR_HL:
2659  new_drawstyle = (pt.x & TILE_UNIT_MASK) + (pt.y & TILE_UNIT_MASK) <= TILE_SIZE ? HT_LINE | HT_DIR_HU : HT_LINE | HT_DIR_HL;
2660  break;
2661 
2662  case HT_DIR_VL:
2663  case HT_DIR_VR:
2664  new_drawstyle = (pt.x & TILE_UNIT_MASK) > (pt.y & TILE_UNIT_MASK) ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR;
2665  break;
2666 
2667  default: NOT_REACHED();
2668  }
2669  _thd.selstart.x = x1 & ~TILE_UNIT_MASK;
2670  _thd.selstart.y = y1 & ~TILE_UNIT_MASK;
2671  break;
2672  default:
2673  NOT_REACHED();
2674  }
2675  _thd.new_pos.x = x1 & ~TILE_UNIT_MASK;
2676  _thd.new_pos.y = y1 & ~TILE_UNIT_MASK;
2677  }
2678  }
2679 
2680  /* redraw selection */
2681  if (_thd.drawstyle != new_drawstyle ||
2682  _thd.pos.x != _thd.new_pos.x || _thd.pos.y != _thd.new_pos.y ||
2683  _thd.size.x != _thd.new_size.x || _thd.size.y != _thd.new_size.y ||
2684  _thd.outersize.x != _thd.new_outersize.x ||
2685  _thd.outersize.y != _thd.new_outersize.y ||
2686  _thd.diagonal != new_diagonal) {
2687  /* Clear the old tile selection? */
2689 
2690  _thd.drawstyle = new_drawstyle;
2691  _thd.pos = _thd.new_pos;
2692  _thd.size = _thd.new_size;
2693  _thd.outersize = _thd.new_outersize;
2694  _thd.diagonal = new_diagonal;
2695  _thd.dirty = 0xff;
2696 
2697  /* Draw the new tile selection? */
2698  if ((new_drawstyle & HT_DRAG_MASK) != HT_NONE) SetSelectionTilesDirty();
2699  }
2700 }
2701 
2707 static inline void ShowMeasurementTooltips(StringID str, uint paramcount)
2708 {
2709  if (!_settings_client.gui.measure_tooltip) return;
2710  GuiShowTooltips(_thd.GetCallbackWnd(), str, TCC_EXIT_VIEWPORT, paramcount);
2711 }
2712 
2713 static void HideMeasurementTooltips()
2714 {
2716 }
2717 
2720 {
2721  _thd.select_method = method;
2722  _thd.select_proc = process;
2723  _thd.selend.x = TileX(tile) * TILE_SIZE;
2724  _thd.selstart.x = TileX(tile) * TILE_SIZE;
2725  _thd.selend.y = TileY(tile) * TILE_SIZE;
2726  _thd.selstart.y = TileY(tile) * TILE_SIZE;
2727 
2728  /* Needed so several things (road, autoroad, bridges, ...) are placed correctly.
2729  * In effect, placement starts from the centre of a tile
2730  */
2731  if (method == VPM_X_OR_Y || method == VPM_FIX_X || method == VPM_FIX_Y) {
2732  _thd.selend.x += TILE_SIZE / 2;
2733  _thd.selend.y += TILE_SIZE / 2;
2734  _thd.selstart.x += TILE_SIZE / 2;
2735  _thd.selstart.y += TILE_SIZE / 2;
2736  }
2737 
2738  HighLightStyle others = _thd.place_mode & ~(HT_DRAG_MASK | HT_DIR_MASK);
2739  if ((_thd.place_mode & HT_DRAG_MASK) == HT_RECT) {
2740  _thd.place_mode = HT_SPECIAL | others;
2741  _thd.next_drawstyle = HT_RECT | others;
2742  } else if (_thd.place_mode & (HT_RAIL | HT_LINE)) {
2743  _thd.place_mode = HT_SPECIAL | others;
2744  _thd.next_drawstyle = _thd.drawstyle | others;
2745  } else {
2746  _thd.place_mode = HT_SPECIAL | others;
2747  _thd.next_drawstyle = HT_POINT | others;
2748  }
2750 }
2751 
2754 {
2755  _thd.select_method = VPM_X_AND_Y;
2756  _thd.select_proc = process;
2757  _thd.selstart.x = 0;
2758  _thd.selstart.y = 0;
2759  _thd.next_drawstyle = HT_RECT;
2760 
2762 }
2763 
2764 void VpSetPlaceSizingLimit(int limit)
2765 {
2766  _thd.sizelimit = limit;
2767 }
2768 
2775 {
2776  uint64_t distance = DistanceManhattan(from, to) + 1;
2777 
2778  _thd.selend.x = TileX(to) * TILE_SIZE;
2779  _thd.selend.y = TileY(to) * TILE_SIZE;
2780  _thd.selstart.x = TileX(from) * TILE_SIZE;
2781  _thd.selstart.y = TileY(from) * TILE_SIZE;
2782  _thd.next_drawstyle = HT_RECT;
2783 
2784  /* show measurement only if there is any length to speak of */
2785  if (distance > 1) {
2786  SetDParam(0, distance);
2787  ShowMeasurementTooltips(STR_MEASURE_LENGTH, 1);
2788  } else {
2789  HideMeasurementTooltips();
2790  }
2791 }
2792 
2793 static void VpStartPreSizing()
2794 {
2795  _thd.selend.x = -1;
2797 }
2798 
2804 {
2805  int fxpy = _tile_fract_coords.x + _tile_fract_coords.y;
2806  int sxpy = (_thd.selend.x & TILE_UNIT_MASK) + (_thd.selend.y & TILE_UNIT_MASK);
2807  int fxmy = _tile_fract_coords.x - _tile_fract_coords.y;
2808  int sxmy = (_thd.selend.x & TILE_UNIT_MASK) - (_thd.selend.y & TILE_UNIT_MASK);
2809 
2810  switch (mode) {
2811  default: NOT_REACHED();
2812  case 0: // end piece is lower right
2813  if (fxpy >= 20 && sxpy <= 12) return HT_DIR_HL;
2814  if (fxmy < -3 && sxmy > 3) return HT_DIR_VR;
2815  return HT_DIR_Y;
2816 
2817  case 1:
2818  if (fxmy > 3 && sxmy < -3) return HT_DIR_VL;
2819  if (fxpy <= 12 && sxpy >= 20) return HT_DIR_HU;
2820  return HT_DIR_Y;
2821 
2822  case 2:
2823  if (fxmy > 3 && sxmy < -3) return HT_DIR_VL;
2824  if (fxpy >= 20 && sxpy <= 12) return HT_DIR_HL;
2825  return HT_DIR_X;
2826 
2827  case 3:
2828  if (fxmy < -3 && sxmy > 3) return HT_DIR_VR;
2829  if (fxpy <= 12 && sxpy >= 20) return HT_DIR_HU;
2830  return HT_DIR_X;
2831  }
2832 }
2833 
2847 static bool SwapDirection(HighLightStyle style, TileIndex start_tile, TileIndex end_tile)
2848 {
2849  uint start_x = TileX(start_tile);
2850  uint start_y = TileY(start_tile);
2851  uint end_x = TileX(end_tile);
2852  uint end_y = TileY(end_tile);
2853 
2854  switch (style & HT_DRAG_MASK) {
2855  case HT_RAIL:
2856  case HT_LINE: return (end_x > start_x || (end_x == start_x && end_y > start_y));
2857 
2858  case HT_RECT:
2859  case HT_POINT: return (end_x != start_x && end_y < start_y);
2860  default: NOT_REACHED();
2861  }
2862 
2863  return false;
2864 }
2865 
2881 static int CalcHeightdiff(HighLightStyle style, uint distance, TileIndex start_tile, TileIndex end_tile)
2882 {
2883  bool swap = SwapDirection(style, start_tile, end_tile);
2884  uint h0, h1; // Start height and end height.
2885 
2886  if (start_tile == end_tile) return 0;
2887  if (swap) Swap(start_tile, end_tile);
2888 
2889  switch (style & HT_DRAG_MASK) {
2890  case HT_RECT:
2891  /* In the case of an area we can determine whether we were dragging south or
2892  * east by checking the X-coordinates of the tiles */
2893  if (TileX(end_tile) > TileX(start_tile)) {
2894  /* Dragging south does not need to change the start tile. */
2895  end_tile = TileAddByDir(end_tile, DIR_S);
2896  } else {
2897  /* Dragging east. */
2898  start_tile = TileAddByDir(start_tile, DIR_SW);
2899  end_tile = TileAddByDir(end_tile, DIR_SE);
2900  }
2901  [[fallthrough]];
2902 
2903  case HT_POINT:
2904  h0 = TileHeight(start_tile);
2905  h1 = TileHeight(end_tile);
2906  break;
2907  default: { // All other types, this is mostly only line/autorail
2908  static const HighLightStyle flip_style_direction[] = {
2910  };
2911  static const std::pair<TileIndexDiffC, TileIndexDiffC> start_heightdiff_line_by_dir[] = {
2912  { {1, 0}, {1, 1} }, // HT_DIR_X
2913  { {0, 1}, {1, 1} }, // HT_DIR_Y
2914  { {1, 0}, {0, 0} }, // HT_DIR_HU
2915  { {1, 0}, {1, 1} }, // HT_DIR_HL
2916  { {1, 0}, {1, 1} }, // HT_DIR_VL
2917  { {0, 1}, {1, 1} }, // HT_DIR_VR
2918  };
2919  static const std::pair<TileIndexDiffC, TileIndexDiffC> end_heightdiff_line_by_dir[] = {
2920  { {0, 1}, {0, 0} }, // HT_DIR_X
2921  { {1, 0}, {0, 0} }, // HT_DIR_Y
2922  { {0, 1}, {0, 0} }, // HT_DIR_HU
2923  { {1, 1}, {0, 1} }, // HT_DIR_HL
2924  { {1, 0}, {0, 0} }, // HT_DIR_VL
2925  { {0, 0}, {0, 1} }, // HT_DIR_VR
2926  };
2927  static_assert(std::size(start_heightdiff_line_by_dir) == HT_DIR_END);
2928  static_assert(std::size(end_heightdiff_line_by_dir) == HT_DIR_END);
2929 
2930  distance %= 2; // we're only interested if the distance is even or uneven
2931  style &= HT_DIR_MASK;
2932  assert(style < HT_DIR_END);
2933 
2934  /* To handle autorail, we do some magic to be able to use a lookup table.
2935  * Firstly if we drag the other way around, we switch start&end, and if needed
2936  * also flip the drag-position. Eg if it was on the left, and the distance is even
2937  * that means the end, which is now the start is on the right */
2938  if (swap && distance == 0) style = flip_style_direction[style];
2939 
2940  /* Lambda to help calculating the height at one side of the line. */
2941  auto get_height = [](auto &tile, auto &heightdiffs) {
2942  return std::max(
2943  TileHeight(TileAdd(tile, ToTileIndexDiff(heightdiffs.first))),
2944  TileHeight(TileAdd(tile, ToTileIndexDiff(heightdiffs.second))));
2945  };
2946 
2947  /* Use lookup table for start-tile based on HighLightStyle direction */
2948  h0 = get_height(start_tile, start_heightdiff_line_by_dir[style]);
2949 
2950  /* Use lookup table for end-tile based on HighLightStyle direction
2951  * flip around side (lower/upper, left/right) based on distance */
2952  if (distance == 0) style = flip_style_direction[style];
2953  h1 = get_height(end_tile, end_heightdiff_line_by_dir[style]);
2954  break;
2955  }
2956  }
2957 
2958  if (swap) Swap(h0, h1);
2959  return (int)(h1 - h0) * TILE_HEIGHT_STEP;
2960 }
2961 
2962 static const StringID measure_strings_length[] = {STR_NULL, STR_MEASURE_LENGTH, STR_MEASURE_LENGTH_HEIGHTDIFF};
2963 
2970 static void CheckUnderflow(int &test, int &other, int mult)
2971 {
2972  if (test >= 0) return;
2973 
2974  other += mult * test;
2975  test = 0;
2976 }
2977 
2985 static void CheckOverflow(int &test, int &other, int max, int mult)
2986 {
2987  if (test <= max) return;
2988 
2989  other += mult * (test - max);
2990  test = max;
2991 }
2992 
2994 static void CalcRaildirsDrawstyle(int x, int y, int method)
2995 {
2996  HighLightStyle b;
2997 
2998  int dx = _thd.selstart.x - (_thd.selend.x & ~TILE_UNIT_MASK);
2999  int dy = _thd.selstart.y - (_thd.selend.y & ~TILE_UNIT_MASK);
3000  uint w = abs(dx) + TILE_SIZE;
3001  uint h = abs(dy) + TILE_SIZE;
3002 
3003  if (method & ~(VPM_RAILDIRS | VPM_SIGNALDIRS)) {
3004  /* We 'force' a selection direction; first four rail buttons. */
3005  method &= ~(VPM_RAILDIRS | VPM_SIGNALDIRS);
3006  int raw_dx = _thd.selstart.x - _thd.selend.x;
3007  int raw_dy = _thd.selstart.y - _thd.selend.y;
3008  switch (method) {
3009  case VPM_FIX_X:
3010  b = HT_LINE | HT_DIR_Y;
3011  x = _thd.selstart.x;
3012  break;
3013 
3014  case VPM_FIX_Y:
3015  b = HT_LINE | HT_DIR_X;
3016  y = _thd.selstart.y;
3017  break;
3018 
3019  case VPM_FIX_HORIZONTAL:
3020  if (dx == -dy) {
3021  /* We are on a straight horizontal line. Determine the 'rail'
3022  * to build based the sub tile location. */
3024  } else {
3025  /* We are not on a straight line. Determine the rail to build
3026  * based on whether we are above or below it. */
3027  b = dx + dy >= (int)TILE_SIZE ? HT_LINE | HT_DIR_HU : HT_LINE | HT_DIR_HL;
3028 
3029  /* Calculate where a horizontal line through the start point and
3030  * a vertical line from the selected end point intersect and
3031  * use that point as the end point. */
3032  int offset = (raw_dx - raw_dy) / 2;
3033  x = _thd.selstart.x - (offset & ~TILE_UNIT_MASK);
3034  y = _thd.selstart.y + (offset & ~TILE_UNIT_MASK);
3035 
3036  /* 'Build' the last half rail tile if needed */
3037  if ((offset & TILE_UNIT_MASK) > (TILE_SIZE / 2)) {
3038  if (dx + dy >= (int)TILE_SIZE) {
3039  x += (dx + dy < 0) ? (int)TILE_SIZE : -(int)TILE_SIZE;
3040  } else {
3041  y += (dx + dy < 0) ? (int)TILE_SIZE : -(int)TILE_SIZE;
3042  }
3043  }
3044 
3045  /* Make sure we do not overflow the map! */
3046  CheckUnderflow(x, y, 1);
3047  CheckUnderflow(y, x, 1);
3048  CheckOverflow(x, y, (Map::MaxX() - 1) * TILE_SIZE, 1);
3049  CheckOverflow(y, x, (Map::MaxY() - 1) * TILE_SIZE, 1);
3050  assert(x >= 0 && y >= 0 && x <= (int)(Map::MaxX() * TILE_SIZE) && y <= (int)(Map::MaxY() * TILE_SIZE));
3051  }
3052  break;
3053 
3054  case VPM_FIX_VERTICAL:
3055  if (dx == dy) {
3056  /* We are on a straight vertical line. Determine the 'rail'
3057  * to build based the sub tile location. */
3058  b = (x & TILE_UNIT_MASK) > (y & TILE_UNIT_MASK) ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR;
3059  } else {
3060  /* We are not on a straight line. Determine the rail to build
3061  * based on whether we are left or right from it. */
3062  b = dx < dy ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR;
3063 
3064  /* Calculate where a vertical line through the start point and
3065  * a horizontal line from the selected end point intersect and
3066  * use that point as the end point. */
3067  int offset = (raw_dx + raw_dy + (int)TILE_SIZE) / 2;
3068  x = _thd.selstart.x - (offset & ~TILE_UNIT_MASK);
3069  y = _thd.selstart.y - (offset & ~TILE_UNIT_MASK);
3070 
3071  /* 'Build' the last half rail tile if needed */
3072  if ((offset & TILE_UNIT_MASK) > (TILE_SIZE / 2)) {
3073  if (dx - dy < 0) {
3074  y += (dx > dy) ? (int)TILE_SIZE : -(int)TILE_SIZE;
3075  } else {
3076  x += (dx < dy) ? (int)TILE_SIZE : -(int)TILE_SIZE;
3077  }
3078  }
3079 
3080  /* Make sure we do not overflow the map! */
3081  CheckUnderflow(x, y, -1);
3082  CheckUnderflow(y, x, -1);
3083  CheckOverflow(x, y, (Map::MaxX() - 1) * TILE_SIZE, -1);
3084  CheckOverflow(y, x, (Map::MaxY() - 1) * TILE_SIZE, -1);
3085  assert(x >= 0 && y >= 0 && x <= (int)(Map::MaxX() * TILE_SIZE) && y <= (int)(Map::MaxY() * TILE_SIZE));
3086  }
3087  break;
3088 
3089  default:
3090  NOT_REACHED();
3091  }
3092  } else if (TileVirtXY(_thd.selstart.x, _thd.selstart.y) == TileVirtXY(x, y)) { // check if we're only within one tile
3093  if (method & VPM_RAILDIRS) {
3094  b = GetAutorailHT(x, y);
3095  } else { // rect for autosignals on one tile
3096  b = HT_RECT;
3097  }
3098  } else if (h == TILE_SIZE) { // Is this in X direction?
3099  if (dx == (int)TILE_SIZE) { // 2x1 special handling
3100  b = (Check2x1AutoRail(3)) | HT_LINE;
3101  } else if (dx == -(int)TILE_SIZE) {
3102  b = (Check2x1AutoRail(2)) | HT_LINE;
3103  } else {
3104  b = HT_LINE | HT_DIR_X;
3105  }
3106  y = _thd.selstart.y;
3107  } else if (w == TILE_SIZE) { // Or Y direction?
3108  if (dy == (int)TILE_SIZE) { // 2x1 special handling
3109  b = (Check2x1AutoRail(1)) | HT_LINE;
3110  } else if (dy == -(int)TILE_SIZE) { // 2x1 other direction
3111  b = (Check2x1AutoRail(0)) | HT_LINE;
3112  } else {
3113  b = HT_LINE | HT_DIR_Y;
3114  }
3115  x = _thd.selstart.x;
3116  } else if (w > h * 2) { // still count as x dir?
3117  b = HT_LINE | HT_DIR_X;
3118  y = _thd.selstart.y;
3119  } else if (h > w * 2) { // still count as y dir?
3120  b = HT_LINE | HT_DIR_Y;
3121  x = _thd.selstart.x;
3122  } else { // complicated direction
3123  int d = w - h;
3124  _thd.selend.x = _thd.selend.x & ~TILE_UNIT_MASK;
3125  _thd.selend.y = _thd.selend.y & ~TILE_UNIT_MASK;
3126 
3127  /* four cases. */
3128  if (x > _thd.selstart.x) {
3129  if (y > _thd.selstart.y) {
3130  /* south */
3131  if (d == 0) {
3132  b = (x & TILE_UNIT_MASK) > (y & TILE_UNIT_MASK) ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR;
3133  } else if (d >= 0) {
3134  x = _thd.selstart.x + h;
3135  b = HT_LINE | HT_DIR_VL;
3136  } else {
3137  y = _thd.selstart.y + w;
3138  b = HT_LINE | HT_DIR_VR;
3139  }
3140  } else {
3141  /* west */
3142  if (d == 0) {
3144  } else if (d >= 0) {
3145  x = _thd.selstart.x + h;
3146  b = HT_LINE | HT_DIR_HL;
3147  } else {
3148  y = _thd.selstart.y - w;
3149  b = HT_LINE | HT_DIR_HU;
3150  }
3151  }
3152  } else {
3153  if (y > _thd.selstart.y) {
3154  /* east */
3155  if (d == 0) {
3157  } else if (d >= 0) {
3158  x = _thd.selstart.x - h;
3159  b = HT_LINE | HT_DIR_HU;
3160  } else {
3161  y = _thd.selstart.y + w;
3162  b = HT_LINE | HT_DIR_HL;
3163  }
3164  } else {
3165  /* north */
3166  if (d == 0) {
3167  b = (x & TILE_UNIT_MASK) > (y & TILE_UNIT_MASK) ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR;
3168  } else if (d >= 0) {
3169  x = _thd.selstart.x - h;
3170  b = HT_LINE | HT_DIR_VR;
3171  } else {
3172  y = _thd.selstart.y - w;
3173  b = HT_LINE | HT_DIR_VL;
3174  }
3175  }
3176  }
3177  }
3178 
3180  TileIndex t0 = TileVirtXY(_thd.selstart.x, _thd.selstart.y);
3181  TileIndex t1 = TileVirtXY(x, y);
3182  uint distance = DistanceManhattan(t0, t1) + 1;
3183  uint8_t index = 0;
3184 
3185  if (distance != 1) {
3186  int heightdiff = CalcHeightdiff(b, distance, t0, t1);
3187  /* If we are showing a tooltip for horizontal or vertical drags,
3188  * 2 tiles have a length of 1. To bias towards the ceiling we add
3189  * one before division. It feels more natural to count 3 lengths as 2 */
3190  if ((b & HT_DIR_MASK) != HT_DIR_X && (b & HT_DIR_MASK) != HT_DIR_Y) {
3191  distance = CeilDiv(distance, 2);
3192  }
3193 
3194  SetDParam(index++, distance);
3195  if (heightdiff != 0) SetDParam(index++, heightdiff);
3196  }
3197 
3198  ShowMeasurementTooltips(measure_strings_length[index], index);
3199  }
3200 
3201  _thd.selend.x = x;
3202  _thd.selend.y = y;
3203  _thd.next_drawstyle = b;
3204 }
3205 
3214 {
3215  int sx, sy;
3216  HighLightStyle style;
3217 
3218  if (x == -1) {
3219  _thd.selend.x = -1;
3220  return;
3221  }
3222 
3223  /* Special handling of drag in any (8-way) direction */
3224  if (method & (VPM_RAILDIRS | VPM_SIGNALDIRS)) {
3225  _thd.selend.x = x;
3226  _thd.selend.y = y;
3227  CalcRaildirsDrawstyle(x, y, method);
3228  return;
3229  }
3230 
3231  /* Needed so level-land is placed correctly */
3232  if ((_thd.next_drawstyle & HT_DRAG_MASK) == HT_POINT) {
3233  x += TILE_SIZE / 2;
3234  y += TILE_SIZE / 2;
3235  }
3236 
3237  sx = _thd.selstart.x;
3238  sy = _thd.selstart.y;
3239 
3240  int limit = 0;
3241 
3242  switch (method) {
3243  case VPM_X_OR_Y: // drag in X or Y direction
3244  if (abs(sy - y) < abs(sx - x)) {
3245  y = sy;
3246  style = HT_DIR_X;
3247  } else {
3248  x = sx;
3249  style = HT_DIR_Y;
3250  }
3251  goto calc_heightdiff_single_direction;
3252 
3253  case VPM_X_LIMITED: // Drag in X direction (limited size).
3254  limit = (_thd.sizelimit - 1) * TILE_SIZE;
3255  [[fallthrough]];
3256 
3257  case VPM_FIX_X: // drag in Y direction
3258  x = sx;
3259  style = HT_DIR_Y;
3260  goto calc_heightdiff_single_direction;
3261 
3262  case VPM_Y_LIMITED: // Drag in Y direction (limited size).
3263  limit = (_thd.sizelimit - 1) * TILE_SIZE;
3264  [[fallthrough]];
3265 
3266  case VPM_FIX_Y: // drag in X direction
3267  y = sy;
3268  style = HT_DIR_X;
3269 
3270 calc_heightdiff_single_direction:;
3271  if (limit > 0) {
3272  x = sx + Clamp(x - sx, -limit, limit);
3273  y = sy + Clamp(y - sy, -limit, limit);
3274  }
3276  TileIndex t0 = TileVirtXY(sx, sy);
3277  TileIndex t1 = TileVirtXY(x, y);
3278  uint distance = DistanceManhattan(t0, t1) + 1;
3279  uint8_t index = 0;
3280 
3281  if (distance != 1) {
3282  /* With current code passing a HT_LINE style to calculate the height
3283  * difference is enough. However if/when a point-tool is created
3284  * with this method, function should be called with new_style (below)
3285  * instead of HT_LINE | style case HT_POINT is handled specially
3286  * new_style := (_thd.next_drawstyle & HT_RECT) ? HT_LINE | style : _thd.next_drawstyle; */
3287  int heightdiff = CalcHeightdiff(HT_LINE | style, 0, t0, t1);
3288 
3289  SetDParam(index++, distance);
3290  if (heightdiff != 0) SetDParam(index++, heightdiff);
3291  }
3292 
3293  ShowMeasurementTooltips(measure_strings_length[index], index);
3294  }
3295  break;
3296 
3297  case VPM_X_AND_Y_LIMITED: // Drag an X by Y constrained rect area.
3298  limit = (_thd.sizelimit - 1) * TILE_SIZE;
3299  x = sx + Clamp(x - sx, -limit, limit);
3300  y = sy + Clamp(y - sy, -limit, limit);
3301  [[fallthrough]];
3302 
3303  case VPM_X_AND_Y: // drag an X by Y area
3305  static const StringID measure_strings_area[] = {
3306  STR_NULL, STR_NULL, STR_MEASURE_AREA, STR_MEASURE_AREA_HEIGHTDIFF
3307  };
3308 
3309  TileIndex t0 = TileVirtXY(sx, sy);
3310  TileIndex t1 = TileVirtXY(x, y);
3311  uint dx = Delta(TileX(t0), TileX(t1)) + 1;
3312  uint dy = Delta(TileY(t0), TileY(t1)) + 1;
3313  uint8_t index = 0;
3314 
3315  /* If dragging an area (eg dynamite tool) and it is actually a single
3316  * row/column, change the type to 'line' to get proper calculation for height */
3317  style = (HighLightStyle)_thd.next_drawstyle;
3318  if (_thd.IsDraggingDiagonal()) {
3319  /* Determine the "area" of the diagonal dragged selection.
3320  * We assume the area is the number of tiles along the X
3321  * edge and the number of tiles along the Y edge. However,
3322  * multiplying these two numbers does not give the exact
3323  * number of tiles; basically we are counting the black
3324  * squares on a chess board and ignore the white ones to
3325  * make the tile counts at the edges match up. There is no
3326  * other way to make a proper count though.
3327  *
3328  * First convert to the rotated coordinate system. */
3329  int dist_x = TileX(t0) - TileX(t1);
3330  int dist_y = TileY(t0) - TileY(t1);
3331  int a_max = dist_x + dist_y;
3332  int b_max = dist_y - dist_x;
3333 
3334  /* Now determine the size along the edge, but due to the
3335  * chess board principle this counts double. */
3336  a_max = abs(a_max + (a_max > 0 ? 2 : -2)) / 2;
3337  b_max = abs(b_max + (b_max > 0 ? 2 : -2)) / 2;
3338 
3339  /* We get a 1x1 on normal 2x1 rectangles, due to it being
3340  * a seen as two sides. As the result for actual building
3341  * will be the same as non-diagonal dragging revert to that
3342  * behaviour to give it a more normally looking size. */
3343  if (a_max != 1 || b_max != 1) {
3344  dx = a_max;
3345  dy = b_max;
3346  }
3347  } else if (style & HT_RECT) {
3348  if (dx == 1) {
3349  style = HT_LINE | HT_DIR_Y;
3350  } else if (dy == 1) {
3351  style = HT_LINE | HT_DIR_X;
3352  }
3353  }
3354 
3355  if (dx != 1 || dy != 1) {
3356  int heightdiff = CalcHeightdiff(style, 0, t0, t1);
3357 
3358  SetDParam(index++, dx - (style & HT_POINT ? 1 : 0));
3359  SetDParam(index++, dy - (style & HT_POINT ? 1 : 0));
3360  if (heightdiff != 0) SetDParam(index++, heightdiff);
3361  }
3362 
3363  ShowMeasurementTooltips(measure_strings_area[index], index);
3364  }
3365  break;
3366 
3367  default: NOT_REACHED();
3368  }
3369 
3370  _thd.selend.x = x;
3371  _thd.selend.y = y;
3372 }
3373 
3379 {
3381 
3382  /* stop drag mode if the window has been closed */
3383  Window *w = _thd.GetCallbackWnd();
3384  if (w == nullptr) {
3386  return ES_HANDLED;
3387  }
3388 
3389  /* while dragging execute the drag procedure of the corresponding window (mostly VpSelectTilesWithMethod() ) */
3390  if (_left_button_down) {
3392  /* Only register a drag event when the mouse moved. */
3393  if (_thd.new_pos.x == _thd.selstart.x && _thd.new_pos.y == _thd.selstart.y) return ES_HANDLED;
3394  _thd.selstart.x = _thd.new_pos.x;
3395  _thd.selstart.y = _thd.new_pos.y;
3396  }
3397 
3398  w->OnPlaceDrag(_thd.select_method, _thd.select_proc, GetTileBelowCursor());
3399  return ES_HANDLED;
3400  }
3401 
3402  /* Mouse button released. */
3405 
3406  /* Keep the selected tool, but reset it to the original mode. */
3407  HighLightStyle others = _thd.place_mode & ~(HT_DRAG_MASK | HT_DIR_MASK);
3408  if ((_thd.next_drawstyle & HT_DRAG_MASK) == HT_RECT) {
3409  _thd.place_mode = HT_RECT | others;
3410  } else if (_thd.select_method & VPM_SIGNALDIRS) {
3411  _thd.place_mode = HT_RECT | others;
3412  } else if (_thd.select_method & VPM_RAILDIRS) {
3413  _thd.place_mode = (_thd.select_method & ~VPM_RAILDIRS) ? _thd.next_drawstyle : (HT_RAIL | others);
3414  } else {
3415  _thd.place_mode = HT_POINT | others;
3416  }
3417  SetTileSelectSize(1, 1);
3418 
3419  HideMeasurementTooltips();
3420  w->OnPlaceMouseUp(_thd.select_method, _thd.select_proc, _thd.selend, TileVirtXY(_thd.selstart.x, _thd.selstart.y), TileVirtXY(_thd.selend.x, _thd.selend.y));
3421 
3422  return ES_HANDLED;
3423 }
3424 
3433 {
3434  SetObjectToPlace(icon, pal, mode, w->window_class, w->window_number);
3435 }
3436 
3437 #include "table/animcursors.h"
3438 
3447 void SetObjectToPlace(CursorID icon, PaletteID pal, HighLightStyle mode, WindowClass window_class, WindowNumber window_num)
3448 {
3449  if (_thd.window_class != WC_INVALID) {
3450  /* Undo clicking on button and drag & drop */
3451  Window *w = _thd.GetCallbackWnd();
3452  /* Call the abort function, but set the window class to something
3453  * that will never be used to avoid infinite loops. Setting it to
3454  * the 'next' window class must not be done because recursion into
3455  * this function might in some cases reset the newly set object to
3456  * place or not properly reset the original selection. */
3457  _thd.window_class = WC_INVALID;
3458  if (w != nullptr) {
3459  w->OnPlaceObjectAbort();
3460  HideMeasurementTooltips();
3461  }
3462  }
3463 
3464  /* Mark the old selection dirty, in case the selection shape or colour changes */
3466 
3467  SetTileSelectSize(1, 1);
3468 
3469  _thd.make_square_red = false;
3470 
3471  if (mode == HT_DRAG) { // HT_DRAG is for dragdropping trains in the depot window
3472  mode = HT_NONE;
3474  } else {
3476  }
3477 
3478  _thd.place_mode = mode;
3479  _thd.window_class = window_class;
3480  _thd.window_number = window_num;
3481 
3482  if ((mode & HT_DRAG_MASK) == HT_SPECIAL) { // special tools, like tunnels or docks start with presizing mode
3483  VpStartPreSizing();
3484  }
3485 
3486  if ((icon & ANIMCURSOR_FLAG) != 0) {
3488  } else {
3489  SetMouseCursor(icon, pal);
3490  }
3491 
3492 }
3493 
3496 {
3498 }
3499 
3500 Point GetViewportStationMiddle(const Viewport *vp, const Station *st)
3501 {
3502  int x = TileX(st->xy) * TILE_SIZE;
3503  int y = TileY(st->xy) * TILE_SIZE;
3504  int z = GetSlopePixelZ(Clamp(x, 0, Map::SizeX() * TILE_SIZE - 1), Clamp(y, 0, Map::SizeY() * TILE_SIZE - 1));
3505 
3506  Point p = RemapCoords(x, y, z);
3507  p.x = UnScaleByZoom(p.x - vp->virtual_left, vp->zoom) + vp->left;
3508  p.y = UnScaleByZoom(p.y - vp->virtual_top, vp->zoom) + vp->top;
3509  return p;
3510 }
3511 
3516 };
3517 
3520 #ifdef WITH_SSE
3521  { &ViewportSortParentSpritesSSE41Checker, &ViewportSortParentSpritesSSE41 },
3522 #endif
3524 };
3525 
3528 {
3529  for (const auto &sprite_sorter : _vp_sprite_sorters) {
3530  if (sprite_sorter.fct_checker()) {
3531  _vp_sprite_sorter = sprite_sorter.fct_sorter;
3532  break;
3533  }
3534  }
3535  assert(_vp_sprite_sorter != nullptr);
3536 }
3537 
3547 {
3548  if (_current_company != OWNER_DEITY) return CMD_ERROR;
3549  switch (target) {
3550  case VST_EVERYONE:
3551  break;
3552  case VST_COMPANY:
3553  if (_local_company != (CompanyID)ref) return CommandCost();
3554  break;
3555  case VST_CLIENT:
3556  if (_network_own_client_id != (ClientID)ref) return CommandCost();
3557  break;
3558  default:
3559  return CMD_ERROR;
3560  }
3561 
3562  if (flags & DC_EXEC) {
3564  ScrollMainWindowToTile(tile);
3565  }
3566  return CommandCost();
3567 }
3568 
3569 void MarkCatchmentTilesDirty()
3570 {
3571  if (_viewport_highlight_town != nullptr) {
3573  return;
3574  }
3575  if (_viewport_highlight_station != nullptr) {
3578  _viewport_highlight_station = nullptr;
3579  } else {
3581  for (TileIndex tile = it; tile != INVALID_TILE; tile = ++it) {
3582  MarkTileDirtyByTile(tile);
3583  }
3584  }
3585  }
3586  if (_viewport_highlight_waypoint != nullptr) {
3588  _viewport_highlight_waypoint = nullptr;
3589  }
3591  }
3592 }
3593 
3594 static void SetWindowDirtyForViewportCatchment()
3595 {
3599 }
3600 
3601 static void ClearViewportCatchment()
3602 {
3603  MarkCatchmentTilesDirty();
3604  _viewport_highlight_station = nullptr;
3605  _viewport_highlight_waypoint = nullptr;
3606  _viewport_highlight_town = nullptr;
3607 }
3608 
3615 void SetViewportCatchmentStation(const Station *st, bool sel)
3616 {
3617  SetWindowDirtyForViewportCatchment();
3618  if (sel && _viewport_highlight_station != st) {
3619  ClearViewportCatchment();
3621  MarkCatchmentTilesDirty();
3622  } else if (!sel && _viewport_highlight_station == st) {
3623  MarkCatchmentTilesDirty();
3624  _viewport_highlight_station = nullptr;
3625  }
3627 }
3628 
3635 void SetViewportCatchmentWaypoint(const Waypoint *wp, bool sel)
3636 {
3637  SetWindowDirtyForViewportCatchment();
3638  if (sel && _viewport_highlight_waypoint != wp) {
3639  ClearViewportCatchment();
3641  MarkCatchmentTilesDirty();
3642  } else if (!sel && _viewport_highlight_waypoint == wp) {
3643  MarkCatchmentTilesDirty();
3644  _viewport_highlight_waypoint = nullptr;
3645  }
3647 }
3648 
3655 void SetViewportCatchmentTown(const Town *t, bool sel)
3656 {
3657  SetWindowDirtyForViewportCatchment();
3658  if (sel && _viewport_highlight_town != t) {
3659  ClearViewportCatchment();
3662  } else if (!sel && _viewport_highlight_town == t) {
3663  _viewport_highlight_town = nullptr;
3665  }
3667 }
Sprite::height
uint16_t height
Height of the sprite.
Definition: spritecache.h:18
DO_SHOW_COMPETITOR_SIGNS
@ DO_SHOW_COMPETITOR_SIGNS
Display signs, station names and waypoint names of opponent companies. Buoys and oilrig-stations are ...
Definition: openttd.h:52
ES_HANDLED
@ ES_HANDLED
The passed event is handled.
Definition: window_type.h:738
Window::WindowIterator
Iterator to iterate all valid Windows.
Definition: window_gui.h:871
HT_DIR_HL
@ HT_DIR_HL
horizontal lower
Definition: tilehighlight_type.h:36
_viewport_highlight_waypoint
const Waypoint * _viewport_highlight_waypoint
Currently selected waypoint for coverage area highlight.
Definition: viewport.cpp:1003
TileY
static debug_inline uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:437
TileInfo::z
int z
Height.
Definition: tile_cmd.h:48
MP_HOUSE
@ MP_HOUSE
A house by a town.
Definition: tile_type.h:51
CalcRaildirsDrawstyle
static void CalcRaildirsDrawstyle(int x, int y, int method)
while dragging
Definition: viewport.cpp:2994
_display_opt
uint8_t _display_opt
What do we want to draw/do?
Definition: transparency_gui.cpp:26
ViewportData
Data structure for a window viewport.
Definition: window_gui.h:255
GetColourGradient
uint8_t GetColourGradient(Colours colour, ColourShade shade)
Get colour gradient palette index.
Definition: palette.cpp:314
SPRITE_MASK
@ SPRITE_MASK
The mask to for the main sprite.
Definition: sprites.h:1560
SetTileSelectSize
void SetTileSelectSize(int w, int h)
Highlight w by h tiles at the cursor.
Definition: viewport.cpp:2539
ParentSpriteToDraw::image
SpriteID image
sprite to draw
Definition: viewport_sprite_sorter.h:30
OWNER_DEITY
@ OWNER_DEITY
The object is owned by a superuser / goal script.
Definition: company_type.h:27
Window::OnPlaceObject
virtual void OnPlaceObject([[maybe_unused]] Point pt, [[maybe_unused]] TileIndex tile)
The user clicked some place on the map when a tile highlight mode has been set.
Definition: window_gui.h:797
ViewportDrawer::foundation_offset
Point foundation_offset[FOUNDATION_PART_END]
Pixel offset for ground sprites on the foundations.
Definition: viewport.cpp:181
AddTileSpriteToDraw
static void AddTileSpriteToDraw(SpriteID image, PaletteID pal, int32_t x, int32_t y, int z, const SubSprite *sub=nullptr, int extra_offs_x=0, int extra_offs_y=0)
Schedules a tile sprite for drawing.
Definition: viewport.cpp:512
DrawAutorailSelection
static void DrawAutorailSelection(const TileInfo *ti, uint autorail_type)
Draws autorail highlights.
Definition: viewport.cpp:965
TileHighlightData::size
Point size
Size, in tile "units", of the white/red selection area.
Definition: tilehighlight_type.h:48
factory.hpp
FindWindowFromPt
Window * FindWindowFromPt(int x, int y)
Do a search for a window at specific coordinates.
Definition: window.cpp:1768
DrawBox
void DrawBox(int x, int y, int dx1, int dy1, int dx2, int dy2, int dx3, int dy3)
Draws the projection of a parallelepiped.
Definition: gfx.cpp:418
TileHighlightData::outersize
Point outersize
Size, in tile "units", of the blue coverage area excluding the side of the selected area.
Definition: tilehighlight_type.h:50
WC_INVALID
@ WC_INVALID
Invalid window.
Definition: window_type.h:718
TileAdd
constexpr TileIndex TileAdd(TileIndex tile, TileIndexDiff offset)
Adds a given offset to a tile.
Definition: map_func.h:466
SetBit
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
Definition: bitmath_func.hpp:121
CheckUnderflow
static void CheckUnderflow(int &test, int &other, int mult)
Check for underflowing the map.
Definition: viewport.cpp:2970
DIR_SW
@ DIR_SW
Southwest.
Definition: direction_type.h:31
Pool::PoolItem<&_vehicle_pool >::Get
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:339
ScrollMainWindowToTile
bool ScrollMainWindowToTile(TileIndex tile, bool instant)
Scrolls the viewport of the main window to a given location.
Definition: viewport.cpp:2512
vehicle_gui.h
PALETTE_SEL_TILE_RED
static const PaletteID PALETTE_SEL_TILE_RED
makes a square red. is used when removing rails or other stuff
Definition: sprites.h:1575
VPM_FIX_VERTICAL
@ VPM_FIX_VERTICAL
drag only in vertical direction
Definition: viewport_type.h:99
MAX_TILE_EXTENT_LEFT
static const int MAX_TILE_EXTENT_LEFT
Maximum left extent of tile relative to north corner.
Definition: viewport.cpp:109
AddDirtyBlock
void AddDirtyBlock(int left, int top, int right, int bottom)
Extend the internal _invalid_rect rectangle to contain the rectangle defined by the given parameters.
Definition: gfx.cpp:1486
SetWindowDirty
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting)
Definition: window.cpp:3090
IsHalftileSlope
static constexpr bool IsHalftileSlope(Slope s)
Checks for non-continuous slope on halftile foundations.
Definition: slope_func.h:47
Sprite::x_offs
int16_t x_offs
Number of pixels to shift the sprite to the right.
Definition: spritecache.h:20
ScrollWindowTo
bool ScrollWindowTo(int x, int y, int z, Window *w, bool instant)
Scrolls the viewport in a window to a given location.
Definition: viewport.cpp:2466
VpHandlePlaceSizingDrag
EventState VpHandlePlaceSizingDrag()
Handle the mouse while dragging for placement/resizing.
Definition: viewport.cpp:3378
ZOOM_OUT
@ ZOOM_OUT
Zoom out (get helicopter view).
Definition: viewport_type.h:78
command_func.h
IsInsideMM
constexpr bool IsInsideMM(const T x, const size_t min, const size_t max) noexcept
Checks if a value is in an interval.
Definition: math_func.hpp:268
_animcursors
static const AnimCursor *const _animcursors[]
This is an array of pointers to all the animated cursor definitions we have above.
Definition: animcursors.h:85
WidgetDimensions::scaled
static WidgetDimensions scaled
Widget dimensions scaled for current zoom level.
Definition: window_gui.h:68
_tile_type_procs
const TileTypeProcs *const _tile_type_procs[16]
Tile callback functions for each type of tile.
Definition: landscape.cpp:65
HT_DIR_VR
@ HT_DIR_VR
vertical right
Definition: tilehighlight_type.h:38
Swap
constexpr void Swap(T &a, T &b)
Type safe swap operation.
Definition: math_func.hpp:283
CMD_ERROR
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
Definition: command_func.h:28
Kdtree
K-dimensional tree, specialised for 2-dimensional space.
Definition: kdtree.hpp:35
ClosestTownFromTile
Town * ClosestTownFromTile(TileIndex tile, uint threshold)
Return the town closest (in distance or ownership) to a given tile, within a given threshold.
Definition: town_cmd.cpp:3864
TileHighlightData::offs
Point offs
Offset, in tile "units", for the blue coverage area from the selected area's northern tile.
Definition: tilehighlight_type.h:49
_special_mouse_mode
SpecialMouseMode _special_mouse_mode
Mode of the mouse.
Definition: window.cpp:93
TileInfo
Tile information, used while rendering the tile.
Definition: tile_cmd.h:43
_left_button_down
bool _left_button_down
Is left mouse button pressed?
Definition: gfx.cpp:41
PALETTE_TILE_RED_PULSATING
static const PaletteID PALETTE_TILE_RED_PULSATING
pulsating red tile drawn if you try to build a wrong tunnel or raise/lower land where it is not possi...
Definition: sprites.h:1574
Map::MaxX
static debug_inline uint MaxX()
Gets the maximum X coordinate within the map, including MP_VOID.
Definition: map_func.h:297
company_base.h
ViewportDragDropSelectionProcess
ViewportDragDropSelectionProcess
Drag and drop selection process, or, what to do with an area of land when you've selected it.
Definition: viewport_type.h:111
TileSpriteToDraw::sub
const SubSprite * sub
only draw a rectangular part of the sprite
Definition: viewport.cpp:126
Blitter
How all blitters should look like.
Definition: base.hpp:29
signs_func.h
Station
Station data structure.
Definition: station_base.h:439
DrawTileHighlightType
static void DrawTileHighlightType(const TileInfo *ti, TileHighlightType tht)
Draw tile highlight for coverage area highlight.
Definition: viewport.cpp:1047
Viewport::width
int width
Screen width of the viewport.
Definition: viewport_type.h:25
animcursors.h
Window::SetWidgetDirty
void SetWidgetDirty(WidgetID widget_index) const
Invalidate a widget, i.e.
Definition: window.cpp:551
TileHighlightData::select_method
ViewportPlaceMethod select_method
The method which governs how tiles are selected.
Definition: tilehighlight_type.h:74
RemoveHalftileSlope
static constexpr Slope RemoveHalftileSlope(Slope s)
Removes a halftile slope from a slope.
Definition: slope_func.h:60
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
ZOOM_LVL_END
@ ZOOM_LVL_END
End for iteration.
Definition: zoom_type.h:25
Window::viewport
ViewportData * viewport
Pointer to viewport data, if present.
Definition: window_gui.h:321
CloseWindowById
void CloseWindowById(WindowClass cls, WindowNumber number, bool force, int data)
Close a window by its class and window number (if it is open).
Definition: window.cpp:1140
Viewport::height
int height
Screen height of the viewport.
Definition: viewport_type.h:26
BitmapTileIterator
Iterator to iterate over all tiles belonging to a bitmaptilearea.
Definition: bitmap_type.h:106
SetRedErrorSquare
void SetRedErrorSquare(TileIndex tile)
Set a tile to display a red error square.
Definition: viewport.cpp:2521
GB
constexpr static debug_inline uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
TileHighlightData::new_size
Point new_size
New value for size; used to determine whether to redraw the selection.
Definition: tilehighlight_type.h:56
Map::ScaleBySize1D
static uint ScaleBySize1D(uint n)
Scales the given value by the maps circumference, where the given value is for a 256 by 256 map.
Definition: map_func.h:341
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
IsTransparencySet
bool IsTransparencySet(TransparencyOption to)
Check if the transparency option bit is set and if we aren't in the game menu (there's never transpar...
Definition: transparency.h:48
Pool::PoolItem::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:238
TileHighlightData::IsDraggingDiagonal
bool IsDraggingDiagonal()
Is the user dragging a 'diagonal rectangle'?
Definition: viewport.cpp:2576
HandleClickOnSign
void HandleClickOnSign(const Sign *si)
Handle clicking on a sign.
Definition: signs_gui.cpp:556
Viewport::top
int top
Screen coordinate top edge of the viewport.
Definition: viewport_type.h:24
Blitter::SetPixel
virtual void SetPixel(void *video, int x, int y, uint8_t colour)=0
Draw a pixel with a given colour on the video-buffer.
FindWindowById
Window * FindWindowById(WindowClass cls, WindowNumber number)
Find a window by its class and window number.
Definition: window.cpp:1098
ViewportDrawer
Data structure storing rendering information.
Definition: viewport.cpp:165
INVALID_TILE
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:95
PALETTE_TO_TRANSPARENT
static const PaletteID PALETTE_TO_TRANSPARENT
This sets the sprite to transparent.
Definition: sprites.h:1607
DIR_S
@ DIR_S
South.
Definition: direction_type.h:30
SpriteType::Normal
@ Normal
The most basic (normal) sprite.
FOUNDATION_PART_HALFTILE
@ FOUNDATION_PART_HALFTILE
Second part (halftile foundation)
Definition: viewport.cpp:145
Waypoint
Representation of a waypoint.
Definition: waypoint_base.h:23
_ctrl_pressed
bool _ctrl_pressed
Is Ctrl pressed?
Definition: gfx.cpp:38
IsSteepSlope
static constexpr bool IsSteepSlope(Slope s)
Checks if a slope is steep.
Definition: slope_func.h:36
TextColour
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition: gfx_type.h:260
vehicle_base.h
DoZoomInOutWindow
bool DoZoomInOutWindow(ZoomStateChange how, Window *w)
Zooms a viewport in a window in or out.
Definition: main_gui.cpp:93
ParentSpriteToDraw::xmax
int32_t xmax
maximal world X coordinate of bounding box
Definition: viewport_sprite_sorter.h:25
zoom_func.h
TILE_SIZE
static const uint TILE_SIZE
Tile size in world coordinates.
Definition: tile_type.h:15
CeilDiv
constexpr uint CeilDiv(uint a, uint b)
Computes ceil(a / b) for non-negative a and b.
Definition: math_func.hpp:320
SpecializedStation< Station, false >::Get
static Station * Get(size_t index)
Gets station with given index.
Definition: base_station_base.h:254
VPM_FIX_Y
@ VPM_FIX_Y
drag only in Y axis
Definition: viewport_type.h:95
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:56
Town::xy
TileIndex xy
town center tile
Definition: town.h:55
ViewportData::dest_scrollpos_y
int32_t dest_scrollpos_y
Current destination y coordinate to display (virtual screen coordinate of topleft corner of the viewp...
Definition: window_gui.h:260
town.h
TileInfo::y
int y
Y position of the tile in unit coordinates.
Definition: tile_cmd.h:45
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
TileHighlightData::sizelimit
uint8_t sizelimit
Whether the selection is limited in length, and what the maximum length is.
Definition: tilehighlight_type.h:62
GetHalftileSlopeCorner
static constexpr Corner GetHalftileSlopeCorner(Slope s)
Returns the leveled halftile of a halftile slope.
Definition: slope_func.h:148
WC_STATION_VIEW
@ WC_STATION_VIEW
Station view; Window numbers:
Definition: window_type.h:345
VST_COMPANY
@ VST_COMPANY
All players in specific company.
Definition: viewport_type.h:150
Vehicle
Vehicle data structure.
Definition: vehicle_base.h:244
ViewportPlaceMethod
ViewportPlaceMethod
Viewport place method (type of highlighted area and placed objects)
Definition: viewport_type.h:92
Viewport::virtual_top
int virtual_top
Virtual top coordinate.
Definition: viewport_type.h:29
Vehicle::owner
Owner owner
Which company owns the vehicle?
Definition: vehicle_base.h:309
ViewportSign
Location information about a sign as seen on the viewport.
Definition: viewport_type.h:38
SignID
uint16_t SignID
The type of the IDs of signs.
Definition: signs_type.h:14
HT_DIR_Y
@ HT_DIR_Y
Y direction.
Definition: tilehighlight_type.h:34
DC_EXEC
@ DC_EXEC
execute the given command
Definition: command_type.h:376
GetSlopePixelZ
int GetSlopePixelZ(int x, int y, bool ground_vehicle)
Return world Z coordinate of a given point of a tile.
Definition: landscape.cpp:303
SubSprite
Used to only draw a part of the sprite.
Definition: gfx_type.h:231
Kdtree::Build
void Build(It begin, It end)
Clear and rebuild the tree from a new sequence of elements,.
Definition: kdtree.hpp:362
PaletteID
uint32_t PaletteID
The number of the palette.
Definition: gfx_type.h:19
FR_TRANSPARENT
@ FR_TRANSPARENT
Makes the background transparent if set.
Definition: window_gui.h:26
GUISettings::zoom_max
ZoomLevel zoom_max
maximum zoom out level
Definition: settings_type.h:166
TileTypeProcs::draw_tile_proc
DrawTileProc * draw_tile_proc
Called to render the tile and its contents to the screen.
Definition: tile_cmd.h:159
ParentSpriteToDraw::y
int32_t y
screen Y coordinate of sprite
Definition: viewport_sprite_sorter.h:28
BaseStation::owner
Owner owner
The owner of this station.
Definition: base_station_base.h:69
ParentSpriteToDraw::top
int32_t top
minimal screen Y coordinate of sprite (= y + sprite->y_offs), reference point for child sprites
Definition: viewport_sprite_sorter.h:35
MarkViewportDirty
static bool MarkViewportDirty(const Viewport *vp, int left, int top, int right, int bottom)
Marks a viewport as dirty for repaint if it displays (a part of) the area the needs to be repainted.
Definition: viewport.cpp:1980
Town::show_zone
bool show_zone
NOSAVE: mark town to show the local authority zone in the viewports.
Definition: town.h:104
autorail.h
DoCommandFlag
DoCommandFlag
List of flags for a command.
Definition: command_type.h:374
Kdtree::Count
size_t Count() const
Get number of elements stored in tree.
Definition: kdtree.hpp:430
CheckClickOnViewportSign
static bool CheckClickOnViewportSign(const Viewport *vp, int x, int y, const ViewportSign *sign)
Test whether a sign is below the mouse.
Definition: viewport.cpp:2196
Debug
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
SwapDirection
static bool SwapDirection(HighLightStyle style, TileIndex start_tile, TileIndex end_tile)
Check if the direction of start and end tile should be swapped based on the dragging-style.
Definition: viewport.cpp:2847
SPRITE_COMBINE_ACTIVE
@ SPRITE_COMBINE_ACTIVE
Sprite combining is active. AddSortableSpriteToDraw outputs child sprites.
Definition: viewport.cpp:156
CalcHeightdiff
static int CalcHeightdiff(HighLightStyle style, uint distance, TileIndex start_tile, TileIndex end_tile)
Calculates height difference between one tile and another.
Definition: viewport.cpp:2881
Vehicle::x_pos
int32_t x_pos
x coordinate.
Definition: vehicle_base.h:304
FOUNDATION_PART_NORMAL
@ FOUNDATION_PART_NORMAL
First part (normal foundation or no foundation)
Definition: viewport.cpp:144
ParentSpriteToDraw::zmax
int32_t zmax
maximal world Z coordinate of bounding box
Definition: viewport_sprite_sorter.h:27
ZOOM_LVL_MIN
@ ZOOM_LVL_MIN
Minimum zoom level.
Definition: zoom_type.h:41
CheckClickOnVehicle
Vehicle * CheckClickOnVehicle(const Viewport *vp, int x, int y)
Find the vehicle close to the clicked coordinates.
Definition: vehicle.cpp:1248
TileHighlightData
Metadata about the current highlighting.
Definition: tilehighlight_type.h:46
SpecializedStation< Station, false >::Iterate
static Pool::IterateWrapper< Station > Iterate(size_t from=0)
Returns an iterable ensemble of all valid stations of type T.
Definition: base_station_base.h:305
VPM_RAILDIRS
@ VPM_RAILDIRS
all rail directions
Definition: viewport_type.h:102
SlopeWithThreeCornersRaised
Slope SlopeWithThreeCornersRaised(Corner corner)
Returns the slope with all except one corner raised.
Definition: slope_func.h:206
ViewportSign::top
int32_t top
The top of the sign.
Definition: viewport_type.h:40
TileInfo::tileh
Slope tileh
Slope of the tile.
Definition: tile_cmd.h:46
TileHighlightData::dirty
uint8_t dirty
Whether the build station window needs to redraw due to the changed selection.
Definition: tilehighlight_type.h:58
GetTileType
static debug_inline TileType GetTileType(Tile tile)
Get the tiletype of a given tile.
Definition: tile_map.h:96
ZOOM_LVL_BEGIN
@ ZOOM_LVL_BEGIN
Begin for iteration.
Definition: zoom_type.h:18
GUISettings::measure_tooltip
bool measure_tooltip
show a permanent tooltip when dragging tools
Definition: settings_type.h:155
ViewportDrawer::last_foundation_child
int * last_foundation_child[FOUNDATION_PART_END]
Tail of ChildSprite list of the foundations. (index into child_screen_sprites_to_draw)
Definition: viewport.cpp:180
WidgetID
int WidgetID
Widget ID.
Definition: window_type.h:18
ViewportSSCSS::fct_checker
VpSorterChecker fct_checker
The check function.
Definition: viewport.cpp:3514
ClampSmoothScroll
static void ClampSmoothScroll(uint32_t delta_ms, int64_t delta_hi, int64_t delta_lo, int &delta_hi_clamped, int &delta_lo_clamped)
Clamp the smooth scroll to a maxmimum speed and distance based on time elapsed.
Definition: viewport.cpp:1886
IsInvisibilitySet
bool IsInvisibilitySet(TransparencyOption to)
Check if the invisibility option bit is set and if we aren't in the game menu (there's never transpar...
Definition: transparency.h:59
window_gui.h
ViewportDrawer::foundation
int foundation[FOUNDATION_PART_END]
Foundation sprites (index into parent_sprites_to_draw).
Definition: viewport.cpp:178
_company_colours
Colours _company_colours[MAX_COMPANIES]
NOSAVE: can be determined from company structs.
Definition: company_cmd.cpp:54
ZOOM_IN
@ ZOOM_IN
Zoom in (get more detailed view).
Definition: viewport_type.h:77
DistanceManhattan
uint DistanceManhattan(TileIndex t0, TileIndex t1)
Gets the Manhattan distance between the two given tiles.
Definition: map.cpp:140
ParentSpriteToDraw::x
int32_t x
screen X coordinate of sprite
Definition: viewport_sprite_sorter.h:22
IsInsideBS
constexpr bool IsInsideBS(const T x, const size_t base, const size_t size)
Checks if a value is between a window started at some base point.
Definition: math_func.hpp:252
Viewport
Data structure for viewport, display of a part of the world.
Definition: viewport_type.h:22
TILE_UNIT_MASK
static const uint TILE_UNIT_MASK
For masking in/out the inner-tile world coordinate units.
Definition: tile_type.h:16
EconomySettings::dist_local_authority
uint8_t dist_local_authority
distance for town local authority, default 20
Definition: settings_type.h:518
ViewportSign::center
int32_t center
The center position of the sign.
Definition: viewport_type.h:39
ZOOM_LVL_OUT_4X
@ ZOOM_LVL_OUT_4X
Zoomed 4 times out.
Definition: zoom_type.h:23
ToTileIndexDiff
TileIndexDiff ToTileIndexDiff(TileIndexDiffC tidc)
Return the offset between two tiles from a TileIndexDiffC struct.
Definition: map_func.h:452
BaseStation::sign
TrackedViewportSign sign
NOSAVE: Dimensions of sign.
Definition: base_station_base.h:61
IsInsideRotatedRectangle
bool IsInsideRotatedRectangle(int x, int y)
Checks whether a point is inside the selected a diagonal rectangle given by _thd.size and _thd....
Definition: viewport.cpp:806
CommandCost
Common return value for all commands.
Definition: command_type.h:23
WSM_PRESIZE
@ WSM_PRESIZE
Presizing mode (docks, tunnels).
Definition: window_gui.h:1046
ViewportDrawer::foundation_part
FoundationPart foundation_part
Currently active foundation for ground sprite drawing.
Definition: viewport.cpp:179
ParentSpriteToDraw::pal
PaletteID pal
palette to use
Definition: viewport_sprite_sorter.h:31
tilehighlight_func.h
WindowNumber
int32_t WindowNumber
Number to differentiate different windows of the same class.
Definition: window_type.h:731
UpdateTileSelection
void UpdateTileSelection()
Updates tile highlighting for all cases.
Definition: viewport.cpp:2599
FS_NORMAL
@ FS_NORMAL
Index of the normal font in the font tables.
Definition: gfx_type.h:209
StartStopVehicle
void StartStopVehicle(const Vehicle *v, bool texteffect)
Executes CMD_START_STOP_VEHICLE for given vehicle.
Definition: vehicle_gui.cpp:2918
HT_DIR_VL
@ HT_DIR_VL
vertical left
Definition: tilehighlight_type.h:37
OWNER_NONE
@ OWNER_NONE
The tile has no ownership.
Definition: company_type.h:25
SetMouseCursor
void SetMouseCursor(CursorID sprite, PaletteID pal)
Assign a single non-animated sprite to the cursor.
Definition: gfx.cpp:1686
MAX_SPRITES
@ MAX_SPRITES
Maximum number of sprites that can be loaded at a given time.
Definition: sprites.h:1559
ParentSpriteToDraw::zmin
int32_t zmin
minimal world Z coordinate of bounding box
Definition: viewport_sprite_sorter.h:21
SetSelectionTilesDirty
static void SetSelectionTilesDirty()
Marks the selected tiles as dirty.
Definition: viewport.cpp:2071
ViewportDrawer::parent_sprites_to_sort
ParentSpriteToSortVector parent_sprites_to_sort
Parent sprite pointer array used for sorting.
Definition: viewport.cpp:171
VPM_FIX_X
@ VPM_FIX_X
drag only in X axis
Definition: viewport_type.h:94
DrawTileSelectionRect
static void DrawTileSelectionRect(const TileInfo *ti, PaletteID pal)
Draws a selection rectangle on a tile.
Definition: viewport.cpp:905
Viewport::virtual_left
int virtual_left
Virtual left coordinate.
Definition: viewport_type.h:28
SetObjectToPlace
void SetObjectToPlace(CursorID icon, PaletteID pal, HighLightStyle mode, WindowClass window_class, WindowNumber window_num)
Change the cursor and mouse click/drag handling to a mode for performing special operations like tile...
Definition: viewport.cpp:3447
TileHighlightData::window_number
WindowNumber window_number
The WindowNumber of the window that is responsible for the selection mode.
Definition: tilehighlight_type.h:69
Window::height
int height
Height of the window (number of pixels down in y direction)
Definition: window_gui.h:315
GetTilePixelSlope
std::tuple< Slope, int > GetTilePixelSlope(TileIndex tile)
Return the slope of a given tile.
Definition: tile_map.h:289
ANIMCURSOR_FLAG
static const CursorID ANIMCURSOR_FLAG
Flag for saying a cursor sprite is an animated cursor.
Definition: sprites.h:1507
VehicleClicked
bool VehicleClicked(const Vehicle *v)
Dispatch a "vehicle selected" event if any window waits for it.
Definition: vehicle_gui.cpp:3423
ViewportData::dest_scrollpos_x
int32_t dest_scrollpos_x
Current destination x coordinate to display (virtual screen coordinate of topleft corner of the viewp...
Definition: window_gui.h:259
INVALID_VEHICLE
static const VehicleID INVALID_VEHICLE
Constant representing a non-existing vehicle.
Definition: vehicle_type.h:54
Window::SetDirty
void SetDirty() const
Mark entire window as dirty (in need of re-paint)
Definition: window.cpp:940
Viewport::left
int left
Screen coordinate left edge of the viewport.
Definition: viewport_type.h:23
AddCombinedSprite
static void AddCombinedSprite(SpriteID image, PaletteID pal, int x, int y, int z, const SubSprite *sub)
Adds a child sprite to a parent sprite.
Definition: viewport.cpp:633
FS_SMALL
@ FS_SMALL
Index of the small font in the font tables.
Definition: gfx_type.h:210
ScrollWindowToTile
bool ScrollWindowToTile(TileIndex tile, Window *w, bool instant)
Scrolls the viewport in a window to a given location.
Definition: viewport.cpp:2501
HT_DIAGONAL
@ HT_DIAGONAL
Also allow 'diagonal rectangles'. Only usable in combination with HT_RECT or HT_POINT.
Definition: tilehighlight_type.h:28
ParentSpriteToDraw::first_child
int32_t first_child
the first child to draw.
Definition: viewport_sprite_sorter.h:37
GUISettings::population_in_label
bool population_in_label
show the population of a town in its label?
Definition: settings_type.h:175
SpecializedStation< Station, false >::IsExpected
static bool IsExpected(const BaseStation *st)
Helper for checking whether the given station is of this type.
Definition: base_station_base.h:235
VpSorterChecker
bool(* VpSorterChecker)()
Type for method for checking whether a viewport sprite sorter exists.
Definition: viewport_sprite_sorter.h:44
ParentSpriteToDraw::sub
const SubSprite * sub
only draw a rectangular part of the sprite
Definition: viewport_sprite_sorter.h:32
VST_EVERYONE
@ VST_EVERYONE
All players.
Definition: viewport_type.h:149
Window::OnPlaceMouseUp
virtual void OnPlaceMouseUp([[maybe_unused]] ViewportPlaceMethod select_method, [[maybe_unused]] ViewportDragDropSelectionProcess select_proc, [[maybe_unused]] Point pt, [[maybe_unused]] TileIndex start_tile, [[maybe_unused]] TileIndex end_tile)
The user has dragged over the map when the tile highlight mode has been set.
Definition: window_gui.h:839
MAX_BUILDING_PIXELS
static const uint MAX_BUILDING_PIXELS
Maximum height of a building in pixels in #ZOOM_BASE. (Also applies to "bridge buildings" on the brid...
Definition: tile_type.h:20
ES_NOT_HANDLED
@ ES_NOT_HANDLED
The passed event is not handled.
Definition: window_type.h:739
Town::stations_near
StationList stations_near
NOSAVE: List of nearby stations.
Definition: town.h:91
Sprite::width
uint16_t width
Width of the sprite.
Definition: spritecache.h:19
Corner
Corner
Enumeration of tile corners.
Definition: slope_type.h:22
HT_RAIL
@ HT_RAIL
autorail (one piece), lower bits: direction
Definition: tilehighlight_type.h:26
GetNorthernBridgeEnd
TileIndex GetNorthernBridgeEnd(TileIndex t)
Finds the northern end of a bridge starting at a middle tile.
Definition: bridge_map.cpp:39
ChildScreenSpriteToDraw::next
int next
next child to draw (-1 at the end)
Definition: viewport.cpp:138
ViewportData::scrollpos_y
int32_t scrollpos_y
Currently shown y coordinate (virtual screen coordinate of topleft corner of the viewport).
Definition: window_gui.h:258
ViewportScrollTarget
ViewportScrollTarget
Target of the viewport scrolling GS method.
Definition: viewport_type.h:148
EndSpriteCombine
void EndSpriteCombine()
Terminates a block of sprites started by StartSpriteCombine.
Definition: viewport.cpp:779
Window::OnPlaceDrag
virtual void OnPlaceDrag([[maybe_unused]] ViewportPlaceMethod select_method, [[maybe_unused]] ViewportDragDropSelectionProcess select_proc, [[maybe_unused]] Point pt)
The user is dragging over the map when the tile highlight mode has been set.
Definition: window_gui.h:828
CheckOverflow
static void CheckOverflow(int &test, int &other, int max, int mult)
Check for overflowing the map.
Definition: viewport.cpp:2985
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:57
linkgraph_gui.h
IsCompanyBuildableVehicleType
bool IsCompanyBuildableVehicleType(VehicleType type)
Is the given vehicle type buildable by a company?
Definition: vehicle_func.h:91
TC_IS_PALETTE_COLOUR
@ TC_IS_PALETTE_COLOUR
Colour value is already a real palette colour index, not an index of a StringColour.
Definition: gfx_type.h:283
ViewportSign::MarkDirty
void MarkDirty(ZoomLevel maxzoom=ZOOM_LVL_MAX) const
Mark the sign dirty in all viewports.
Definition: viewport.cpp:1488
BlitterFactory::GetCurrentBlitter
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition: factory.hpp:138
HighlightTownLocalAuthorityTiles
static void HighlightTownLocalAuthorityTiles(const TileInfo *ti)
Highlights tiles insede local authority of selected towns.
Definition: viewport.cpp:1062
GameSettings::economy
EconomySettings economy
settings to change the economy
Definition: settings_type.h:603
_local_company
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Definition: company_cmd.cpp:52
ViewportDrawBoundingBoxes
static void ViewportDrawBoundingBoxes(const ParentSpriteToSortVector *psd)
Draws the bounding boxes of all ParentSprites.
Definition: viewport.cpp:1670
DO_SHOW_STATION_NAMES
@ DO_SHOW_STATION_NAMES
Display station names.
Definition: openttd.h:47
safeguards.h
Window::left
int left
x position of left edge of the window
Definition: window_gui.h:312
WC_WAYPOINT_VIEW
@ WC_WAYPOINT_VIEW
Waypoint view; Window numbers:
Definition: window_type.h:357
TileHighlightData::make_square_red
bool make_square_red
Whether to give a tile a red selection.
Definition: tilehighlight_type.h:71
CursorID
uint32_t CursorID
The number of the cursor (sprite)
Definition: gfx_type.h:20
WindowClass
WindowClass
Window classes.
Definition: window_type.h:44
RedrawScreenRect
void RedrawScreenRect(int left, int top, int right, int bottom)
Repaints a specific rectangle of the screen.
Definition: gfx.cpp:1371
ParentSpriteToDraw::ymax
int32_t ymax
maximal world Y coordinate of bounding box
Definition: viewport_sprite_sorter.h:26
HT_NONE
@ HT_NONE
default
Definition: tilehighlight_type.h:20
StartSpriteCombine
void StartSpriteCombine()
Starts a block of sprites, which are "combined" into a single bounding box.
Definition: viewport.cpp:769
TileHighlightData::pos
Point pos
Location, in tile "units", of the northern tile of the selected area.
Definition: tilehighlight_type.h:47
VpSpriteSorter
void(* VpSpriteSorter)(ParentSpriteToSortVector *psd)
Type for the actual viewport sprite sorter.
Definition: viewport_sprite_sorter.h:46
SPRITE_COMBINE_NONE
@ SPRITE_COMBINE_NONE
Every AddSortableSpriteToDraw start its own bounding box.
Definition: viewport.cpp:154
ViewportDrawer::combine_sprites
SpriteCombineMode combine_sprites
Current mode of "sprite combining".
Definition: viewport.cpp:176
TileHighlightData::new_outersize
Point new_outersize
New value for outersize; used to determine whether to redraw the selection.
Definition: tilehighlight_type.h:57
waypoint_func.h
Viewport::virtual_width
int virtual_width
width << zoom
Definition: viewport_type.h:30
Point
Coordinates of a point in 2D.
Definition: geometry_type.hpp:21
ScrollMainWindowTo
bool ScrollMainWindowTo(int x, int y, int z, bool instant)
Scrolls the main window to given coordinates.
Definition: smallmap_gui.cpp:2080
WSM_DRAGDROP
@ WSM_DRAGDROP
Drag&drop an object.
Definition: window_gui.h:1044
OffsetGroundSprite
void OffsetGroundSprite(int x, int y)
Called when a foundation has been drawn for the current tile.
Definition: viewport.cpp:601
WSM_SIZING
@ WSM_SIZING
Sizing mode.
Definition: window_gui.h:1045
SpecializedStation< Waypoint, true >::From
static Waypoint * From(BaseStation *st)
Converts a BaseStation to SpecializedStation with type checking.
Definition: base_station_base.h:283
HT_DIR_MASK
@ HT_DIR_MASK
masks the drag-direction
Definition: tilehighlight_type.h:40
SLOPE_N
@ SLOPE_N
the north corner of the tile is raised
Definition: slope_type.h:53
SetAnimatedMouseCursor
void SetAnimatedMouseCursor(const AnimCursor *table)
Assign an animation to the cursor.
Definition: gfx.cpp:1699
ShowStationViewWindow
void ShowStationViewWindow(StationID station)
Opens StationViewWindow for given station.
Definition: station_gui.cpp:2178
_viewport_highlight_town
const Town * _viewport_highlight_town
Currently selected town for coverage area highlight.
Definition: viewport.cpp:1004
stdafx.h
Window::window_number
WindowNumber window_number
Window number within the window class.
Definition: window_gui.h:305
TileAddByDir
TileIndex TileAddByDir(TileIndex tile, Direction dir)
Adds a Direction to a tile.
Definition: map_func.h:594
landscape.h
ViewportSign::width_normal
uint16_t width_normal
The width when not zoomed out (normal font)
Definition: viewport_type.h:41
SpriteID
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:18
PALETTE_MODIFIER_TRANSPARENT
@ PALETTE_MODIFIER_TRANSPARENT
when a sprite is to be displayed transparently, this bit needs to be set.
Definition: sprites.h:1549
VpStartPlaceSizing
void VpStartPlaceSizing(TileIndex tile, ViewportPlaceMethod method, ViewportDragDropSelectionProcess process)
highlighting tiles while only going over them with the mouse
Definition: viewport.cpp:2719
viewport_func.h
SA_HOR_CENTER
@ SA_HOR_CENTER
Horizontally center the text.
Definition: gfx_type.h:346
bridge_map.h
Window::AllWindows
Iterable ensemble of all valid Windows.
Definition: window_gui.h:916
InverseRemapCoords2
Point InverseRemapCoords2(int x, int y, bool clamp_to_map, bool *clamped)
Map 2D viewport or smallmap coordinate to 3D world or tile coordinate.
Definition: landscape.cpp:111
ViewportAddLandscape
static void ViewportAddLandscape()
Add the landscape to the viewport, i.e.
Definition: viewport.cpp:1186
UnScaleByZoomLower
int UnScaleByZoomLower(int value, ZoomLevel zoom)
Scale by zoom level, usually shift right (when zoom > ZOOM_LVL_MIN)
Definition: zoom_func.h:67
Vehicle::z_pos
int32_t z_pos
z coordinate.
Definition: vehicle_base.h:306
HT_VEHICLE
@ HT_VEHICLE
vehicle is accepted as target as well (bitmask)
Definition: tilehighlight_type.h:27
string_colours.h
AddSortableSpriteToDraw
void AddSortableSpriteToDraw(SpriteID image, PaletteID pal, int x, int y, int w, int h, int dz, int z, bool transparent, int bb_offset_x, int bb_offset_y, int bb_offset_z, const SubSprite *sub)
Draw a (transparent) sprite at given coordinates with a given bounding box.
Definition: viewport.cpp:673
TileSpriteToDraw::y
int32_t y
screen Y coordinate of sprite
Definition: viewport.cpp:128
ViewportSignKdtreeItem
Definition: viewport_kdtree.h:19
IsValidTile
bool IsValidTile(Tile tile)
Checks if a tile is valid.
Definition: tile_map.h:161
HT_DRAG
@ HT_DRAG
dragging items in the depot windows
Definition: tilehighlight_type.h:24
GetAutorailHT
static HighLightStyle GetAutorailHT(int x, int y)
returns the best autorail highlight type from map coordinates
Definition: viewport.cpp:2556
_network_own_client_id
ClientID _network_own_client_id
Our client identifier.
Definition: network.cpp:70
_string_colourmap
static const uint8_t _string_colourmap[17]
Colour mapping for TextColour.
Definition: string_colours.h:11
MarkAllViewportsDirty
bool MarkAllViewportsDirty(int left, int top, int right, int bottom)
Mark all viewports that display an area as dirty (in need of repaint).
Definition: viewport.cpp:2019
Window::SetWidgetDisabledState
void SetWidgetDisabledState(WidgetID widget_index, bool disab_stat)
Sets the enabled/disabled status of a widget.
Definition: window_gui.h:390
ChildScreenSpriteToDraw
Definition: viewport.cpp:131
GUISettings::zoom_min
ZoomLevel zoom_min
minimum zoom out level
Definition: settings_type.h:165
Map::SizeX
static debug_inline uint SizeX()
Get the size of the map along the X.
Definition: map_func.h:270
TileHighlightData::drawstyle
HighLightStyle drawstyle
Lower bits 0-3 are reserved for detailed highlight information.
Definition: tilehighlight_type.h:64
TileHighlightData::place_mode
HighLightStyle place_mode
Method which is used to place the selection.
Definition: tilehighlight_type.h:67
MAX_TILE_EXTENT_BOTTOM
static const int MAX_TILE_EXTENT_BOTTOM
Maximum bottom extent of tile relative to north corner (worst case: SLOPE_STEEP_N).
Definition: viewport.cpp:112
PerformanceAccumulator
RAII class for measuring multi-step elements of performance.
Definition: framerate_type.h:114
VpStartDragging
void VpStartDragging(ViewportDragDropSelectionProcess process)
Drag over the map while holding the left mouse down.
Definition: viewport.cpp:2753
RemapCoords2
Point RemapCoords2(int x, int y)
Map 3D world or tile coordinate to equivalent 2D coordinate as used in the viewports and smallmap.
Definition: landscape.h:95
ClampViewportToMap
static void ClampViewportToMap(const Viewport *vp, int *scroll_x, int *scroll_y)
Ensure that a given viewport has a valid scroll position.
Definition: viewport.cpp:1854
TilePixelHeight
uint TilePixelHeight(Tile tile)
Returns the height of a tile in pixels.
Definition: tile_map.h:72
TileHighlightData::selstart
Point selstart
The location where the dragging started.
Definition: tilehighlight_type.h:60
_current_company
CompanyID _current_company
Company currently doing an action.
Definition: company_cmd.cpp:53
GuiShowTooltips
void GuiShowTooltips(Window *parent, StringID str, TooltipCloseCondition close_tooltip, uint paramcount)
Shows a tooltip.
Definition: misc_gui.cpp:760
vehicle_func.h
IsInRangeInclusive
static bool IsInRangeInclusive(int begin, int end, int check)
Check if the parameter "check" is inside the interval between begin and end, including both begin and...
Definition: viewport.cpp:794
station_base.h
Pool::PoolItem<&_town_pool >::Iterate
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:388
PALETTE_CRASH
static const PaletteID PALETTE_CRASH
Recolour sprite greying of crashed vehicles.
Definition: sprites.h:1610
ResetObjectToPlace
void ResetObjectToPlace()
Reset the cursor and mouse mode handling back to default (normal cursor, only clicking in windows).
Definition: viewport.cpp:3495
Map::MaxY
static uint MaxY()
Gets the maximum Y coordinate within the map, including MP_VOID.
Definition: map_func.h:306
strings_func.h
UnScaleByZoom
int UnScaleByZoom(int value, ZoomLevel zoom)
Scale by zoom level, usually shift right (when zoom > ZOOM_LVL_MIN) When shifting right,...
Definition: zoom_func.h:34
Vehicle::First
Vehicle * First() const
Get the first vehicle of this vehicle chain.
Definition: vehicle_base.h:645
Window::OnPlaceObjectAbort
virtual void OnPlaceObjectAbort()
The user cancelled a tile highlight mode that has been set.
Definition: window_gui.h:818
ClientID
ClientID
'Unique' identifier to be given to clients
Definition: network_type.h:49
MP_VOID
@ MP_VOID
Invisible tiles at the SW and SE border.
Definition: tile_type.h:55
AddChildSpriteToFoundation
static void AddChildSpriteToFoundation(SpriteID image, PaletteID pal, const SubSprite *sub, FoundationPart foundation_part, int extra_offs_x, int extra_offs_y)
Adds a child sprite to the active foundation.
Definition: viewport.cpp:537
Blitter::MoveTo
virtual void * MoveTo(void *video, int x, int y)=0
Move the destination pointer the requested amount x and y, keeping in mind any pitch and bpp of the r...
Pool::PoolItem<&_station_pool >::GetNumItems
static size_t GetNumItems()
Returns number of valid items in the pool.
Definition: pool_type.hpp:369
VPM_Y_LIMITED
@ VPM_Y_LIMITED
Drag only in Y axis with limited size.
Definition: viewport_type.h:101
Slope
Slope
Enumeration for the slope-type.
Definition: slope_type.h:48
TileHighlightData::diagonal
bool diagonal
Whether the dragged area is a 45 degrees rotated rectangle.
Definition: tilehighlight_type.h:51
DrawTileSelection
static void DrawTileSelection(const TileInfo *ti)
Checks if the specified tile is selected and if so draws selection using correct selectionstyle.
Definition: viewport.cpp:1096
abs
constexpr T abs(const T a)
Returns the absolute value of (scalar) variable.
Definition: math_func.hpp:23
TileType
TileType
The different types of tiles.
Definition: tile_type.h:47
HT_DIR_END
@ HT_DIR_END
end marker
Definition: tilehighlight_type.h:39
HT_LINE
@ HT_LINE
used for autorail highlighting (longer stretches), lower bits: direction
Definition: tilehighlight_type.h:25
SetDParam
void SetDParam(size_t n, uint64_t v)
Set a string parameter v at index n in the global string parameter array.
Definition: strings.cpp:104
ViewportSign::UpdatePosition
void UpdatePosition(int center, int top, StringID str, StringID str_small=STR_NULL)
Update the position of the viewport sign.
Definition: viewport.cpp:1463
GetStationIndex
StationID GetStationIndex(Tile t)
Get StationID from a tile.
Definition: station_map.h:28
GetMainWindow
Window * GetMainWindow()
Get the main window, i.e.
Definition: window.cpp:1127
PALETTE_SEL_TILE_BLUE
static const PaletteID PALETTE_SEL_TILE_BLUE
This draws a blueish square (catchment areas for example)
Definition: sprites.h:1576
OrthogonalTileArea::tile
TileIndex tile
The base tile of the area.
Definition: tilearea_type.h:19
SetObjectToPlaceWnd
void SetObjectToPlaceWnd(CursorID icon, PaletteID pal, HighLightStyle mode, Window *w)
Change the cursor and mouse click/drag handling to a mode for performing special operations like tile...
Definition: viewport.cpp:3432
framerate_type.h
Sprite::y_offs
int16_t y_offs
Number of pixels to shift the sprite downwards.
Definition: spritecache.h:21
_vp_sprite_sorters
static ViewportSSCSS _vp_sprite_sorters[]
List of sorters ordered from best to worst.
Definition: viewport.cpp:3519
WC_TOOLTIPS
@ WC_TOOLTIPS
Tooltip window; Window numbers:
Definition: window_type.h:116
ParentSpriteToDraw::xmin
int32_t xmin
minimal world X coordinate of bounding box
Definition: viewport_sprite_sorter.h:19
MarkTileDirtyByTile
void MarkTileDirtyByTile(TileIndex tile, int bridge_level_offset, int tile_height_override)
Mark a tile given by its index dirty for repaint.
Definition: viewport.cpp:2054
GetViewportY
static int GetViewportY(Point tile)
Returns the y coordinate in the viewport coordinate system where the given tile is painted.
Definition: viewport.cpp:1177
DrawGroundSpriteAt
void DrawGroundSpriteAt(SpriteID image, PaletteID pal, int32_t x, int32_t y, int z, const SubSprite *sub, int extra_offs_x, int extra_offs_y)
Draws a ground sprite at a specific world-coordinate relative to the current tile.
Definition: viewport.cpp:566
ViewportSortParentSprites
static void ViewportSortParentSprites(ParentSpriteToSortVector *psdv)
Sort parent sprites pointer array replicating the way original sorter did it.
Definition: viewport.cpp:1528
AutoRestoreBackup
Class to backup a specific variable and restore it upon destruction of this object to prevent stack v...
Definition: backup_type.hpp:150
ViewportData::scrollpos_x
int32_t scrollpos_x
Currently shown x coordinate (virtual screen coordinate of topleft corner of the viewport).
Definition: window_gui.h:257
Kdtree::FindNearest
T FindNearest(CoordT x, CoordT y) const
Find the element closest to given coordinate, in Manhattan distance.
Definition: kdtree.hpp:441
MP_STATION
@ MP_STATION
A tile of a station.
Definition: tile_type.h:53
GetString
std::string GetString(StringID string)
Resolve the given StringID into a std::string with all the associated DParam lookups and formatting.
Definition: strings.cpp:319
Town::cache
TownCache cache
Container for all cacheable data.
Definition: town.h:57
waypoint_base.h
EventState
EventState
State of handling an event.
Definition: window_type.h:737
TrackedViewportSign::kdtree_valid
bool kdtree_valid
Are the sign data valid for use with the _viewport_sign_kdtree?
Definition: viewport_type.h:52
HT_RECT
@ HT_RECT
rectangle (stations, depots, ...)
Definition: tilehighlight_type.h:21
VPM_FIX_HORIZONTAL
@ VPM_FIX_HORIZONTAL
drag only in horizontal direction
Definition: viewport_type.h:98
VPM_X_AND_Y
@ VPM_X_AND_Y
area of land in X and Y directions
Definition: viewport_type.h:96
DO_SHOW_SIGNS
@ DO_SHOW_SIGNS
Display signs.
Definition: openttd.h:48
Sign
Definition: signs_base.h:21
TileHeightOutsideMap
uint TileHeightOutsideMap(int x, int y)
Returns the height of a tile, also for tiles outside the map (virtual "black" tiles).
Definition: tile_map.h:42
Kdtree::FindContained
void FindContained(CoordT x1, CoordT y1, CoordT x2, CoordT y2, const Outputter &outputter) const
Find all items contained within the given rectangle.
Definition: kdtree.hpp:459
WSM_DRAGGING
@ WSM_DRAGGING
Dragging mode (trees).
Definition: window_gui.h:1047
Window::window_class
WindowClass window_class
Window class.
Definition: window_gui.h:304
SetViewportCatchmentWaypoint
void SetViewportCatchmentWaypoint(const Waypoint *wp, bool sel)
Select or deselect waypoint for coverage area highlight.
Definition: viewport.cpp:3635
Station::catchment_tiles
BitmapTileArea catchment_tiles
NOSAVE: Set of individual tiles covered by catchment area.
Definition: station_base.h:459
Check2x1AutoRail
static HighLightStyle Check2x1AutoRail(int mode)
returns information about the 2x1 piece to be build.
Definition: viewport.cpp:2803
IsBridgeAbove
bool IsBridgeAbove(Tile t)
checks if a bridge is set above the ground of this tile
Definition: bridge_map.h:45
WC_MAIN_WINDOW
@ WC_MAIN_WINDOW
Main window; Window numbers:
Definition: window_type.h:51
BaseStation::xy
TileIndex xy
Base tile of the station.
Definition: base_station_base.h:60
Vehicle::unitnumber
UnitID unitnumber
unit number, for display purposes only
Definition: vehicle_base.h:326
UpdateViewportPosition
void UpdateViewportPosition(Window *w, uint32_t delta_ms)
Update the viewport position being displayed.
Definition: viewport.cpp:1913
HT_DRAG_MASK
@ HT_DRAG_MASK
Mask for the tile drag-type modes.
Definition: tilehighlight_type.h:29
BaseStation
Base class for all station-ish types.
Definition: base_station_base.h:59
DO_SHOW_TOWN_NAMES
@ DO_SHOW_TOWN_NAMES
Display town names.
Definition: openttd.h:46
company_func.h
TileHighlightData::select_proc
ViewportDragDropSelectionProcess select_proc
The procedure that has to be called when the selection is done.
Definition: tilehighlight_type.h:75
SetViewportCatchmentStation
void SetViewportCatchmentStation(const Station *st, bool sel)
Select or deselect station for coverage area highlight.
Definition: viewport.cpp:3615
WidgetDimensions::fullbevel
RectPadding fullbevel
Always-scaled bevel thickness.
Definition: window_gui.h:41
ViewportAddString
void ViewportAddString(const DrawPixelInfo *dpi, ZoomLevel small_from, const ViewportSign *sign, StringID string_normal, StringID string_small, StringID string_small_shadow, Colours colour)
Add a string to draw in the viewport.
Definition: viewport.cpp:1308
TO_SIGNS
@ TO_SIGNS
signs
Definition: transparency.h:23
Window::top
int top
y position of top edge of the window
Definition: window_gui.h:313
FoundationPart
FoundationPart
Enumeration of multi-part foundations.
Definition: viewport.cpp:142
Vehicle::y_pos
int32_t y_pos
y coordinate.
Definition: vehicle_base.h:305
GetTileHighlightType
static TileHighlightType GetTileHighlightType(TileIndex t)
Get tile highlight type of coverage area for a given tile.
Definition: viewport.cpp:1011
Window::DrawViewport
void DrawViewport() const
Draw the viewport of this window.
Definition: viewport.cpp:1829
IsPtInWindowViewport
Viewport * IsPtInWindowViewport(const Window *w, int x, int y)
Is a xy position inside the viewport of the window?
Definition: viewport.cpp:408
InitializeWindowViewport
void InitializeWindowViewport(Window *w, int x, int y, int width, int height, std::variant< TileIndex, VehicleID > focus, ZoomLevel zoom)
Initialize viewport of the window for use.
Definition: viewport.cpp:219
ViewportDrawDirtyBlocks
static void ViewportDrawDirtyBlocks()
Draw/colour the blocks that have been redrawn.
Definition: viewport.cpp:1688
DrawFrameRect
void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags)
Draw frame rectangle.
Definition: widget.cpp:281
HighLightStyle
HighLightStyle
Highlighting draw styles.
Definition: tilehighlight_type.h:19
ParentSpriteToDraw
Parent sprite that should be drawn.
Definition: viewport_sprite_sorter.h:17
ChildScreenSpriteToDraw::sub
const SubSprite * sub
only draw a rectangular part of the sprite
Definition: viewport.cpp:134
ConstructionSettings::max_bridge_height
uint8_t max_bridge_height
maximum height of bridges
Definition: settings_type.h:386
ParentSpriteToDraw::left
int32_t left
minimal screen X coordinate of sprite (= x + sprite->x_offs), reference point for child sprites
Definition: viewport_sprite_sorter.h:34
ViewportData::follow_vehicle
VehicleID follow_vehicle
VehicleID to follow if following a vehicle, INVALID_VEHICLE otherwise.
Definition: window_gui.h:256
DrawSelectionSprite
static void DrawSelectionSprite(SpriteID image, PaletteID pal, const TileInfo *ti, int z_offset, FoundationPart foundation_part, int extra_offs_x=0, int extra_offs_y=0)
Draws sprites between ground sprite and everything above.
Definition: viewport.cpp:887
ShowVehicleViewWindow
void ShowVehicleViewWindow(const Vehicle *v)
Shows the vehicle view window of the given vehicle.
Definition: vehicle_gui.cpp:3413
window_func.h
VpSetPresizeRange
void VpSetPresizeRange(TileIndex from, TileIndex to)
Highlights all tiles between a set of two tiles.
Definition: viewport.cpp:2774
GetCharacterHeight
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
Definition: fontcache.cpp:77
Town
Town data structure.
Definition: town.h:54
Window::width
int width
width of the window (number of pixels to the right in x direction)
Definition: window_gui.h:314
VPM_X_LIMITED
@ VPM_X_LIMITED
Drag only in X axis with limited size.
Definition: viewport_type.h:100
Delta
constexpr T Delta(const T a, const T b)
Returns the (absolute) difference between two (scalar) variables.
Definition: math_func.hpp:234
SlopeToSpriteOffset
uint SlopeToSpriteOffset(Slope s)
Returns the Sprite offset for a given Slope.
Definition: slope_func.h:415
Viewport::zoom
ZoomLevel zoom
The zoom level of the viewport.
Definition: viewport_type.h:33
AddChildSpriteScreen
void AddChildSpriteScreen(SpriteID image, PaletteID pal, int x, int y, bool transparent, const SubSprite *sub, bool scale, bool relative)
Add a child sprite to a parent sprite.
Definition: viewport.cpp:829
MarkWholeScreenDirty
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition: gfx.cpp:1529
TileXY
static debug_inline TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition: map_func.h:385
GetTileMaxPixelZ
int GetTileMaxPixelZ(TileIndex tile)
Get top height of the tile.
Definition: tile_map.h:312
TileHeight
static debug_inline uint TileHeight(Tile tile)
Returns the height of a tile.
Definition: tile_map.h:29
TileHighlightData::window_class
WindowClass window_class
The WindowClass of the window that is responsible for the selection mode.
Definition: tilehighlight_type.h:68
TILE_HEIGHT
static const uint TILE_HEIGHT
Height of a height level in world coordinate AND in pixels in #ZOOM_BASE.
Definition: tile_type.h:18
ViewportSign::width_small
uint16_t width_small
The width when zoomed out (small font)
Definition: viewport_type.h:42
DrawString
int DrawString(int left, int right, int top, std::string_view str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly truncated to make it fit in its allocated space.
Definition: gfx.cpp:657
TILE_HEIGHT_STEP
static const int TILE_HEIGHT_STEP
One Z unit tile height difference is displayed as 50m.
Definition: viewport_func.h:20
TileVirtXY
static debug_inline TileIndex TileVirtXY(uint x, uint y)
Get a tile from the virtual XY-coordinate.
Definition: map_func.h:416
ViewportSortParentSpritesChecker
static bool ViewportSortParentSpritesChecker()
This fallback sprite checker always exists.
Definition: viewport.cpp:1522
WC_TOWN_VIEW
@ WC_TOWN_VIEW
Town view; Window numbers:
Definition: window_type.h:333
DIR_SE
@ DIR_SE
Southeast.
Definition: direction_type.h:29
ParentSpriteToDraw::ymin
int32_t ymin
minimal world Y coordinate of bounding box
Definition: viewport_sprite_sorter.h:20
ViewportSSCSS::fct_sorter
VpSpriteSorter fct_sorter
The sorting function.
Definition: viewport.cpp:3515
TileInfo::x
int x
X position of the tile in unit coordinates.
Definition: tile_cmd.h:44
SPR_CURSOR_MOUSE
static const CursorID SPR_CURSOR_MOUSE
Cursor sprite numbers.
Definition: sprites.h:1390
BaseStation::IsInUse
bool IsInUse() const
Check whether the base station currently is in use; in use means that it is not scheduled for deletio...
Definition: base_station_base.h:177
TileInfo::tile
TileIndex tile
Tile index.
Definition: tile_cmd.h:47
ShowMeasurementTooltips
static void ShowMeasurementTooltips(StringID str, uint paramcount)
Displays the measurement tooltips when selecting multiple tiles.
Definition: viewport.cpp:2707
GameSettings::construction
ConstructionSettings construction
construction of things in-game
Definition: settings_type.h:595
HandleZoomMessage
void HandleZoomMessage(Window *w, const Viewport *vp, WidgetID widget_zoom_in, WidgetID widget_zoom_out)
Update the status of the zoom-buttons according to the zoom-level of the viewport.
Definition: viewport.cpp:491
SlopeWithOneCornerRaised
Slope SlopeWithOneCornerRaised(Corner corner)
Returns the slope with a specific corner raised.
Definition: slope_func.h:99
Window
Data structure for an opened window.
Definition: window_gui.h:276
TILE_PIXELS
static const uint TILE_PIXELS
Pixel distance between tile columns/rows in #ZOOM_BASE.
Definition: tile_type.h:17
IsTileType
static debug_inline bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
Definition: tile_map.h:150
TilePixelHeightOutsideMap
uint TilePixelHeightOutsideMap(int x, int y)
Returns the height of a tile in pixels, also for tiles outside the map (virtual "black" tiles).
Definition: tile_map.h:84
MAX_TILE_EXTENT_RIGHT
static const int MAX_TILE_EXTENT_RIGHT
Maximum right extent of tile relative to north corner.
Definition: viewport.cpp:110
TileHighlightData::next_drawstyle
HighLightStyle next_drawstyle
Queued, but not yet drawn style.
Definition: tilehighlight_type.h:65
_viewport_highlight_station
const Station * _viewport_highlight_station
Currently selected station for coverage area highlight.
Definition: viewport.cpp:1002
VpSelectTilesWithMethod
void VpSelectTilesWithMethod(int x, int y, ViewportPlaceMethod method)
Selects tiles while dragging.
Definition: viewport.cpp:3213
TranslateXYToTileCoord
Point TranslateXYToTileCoord(const Viewport *vp, int x, int y, bool clamp_to_map)
Translate screen coordinate in a viewport to underlying tile coordinate.
Definition: viewport.cpp:432
Clamp
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:79
TileX
static debug_inline uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:427
MAX_TILE_EXTENT_TOP
static const int MAX_TILE_EXTENT_TOP
Maximum top extent of tile relative to north corner (not considering bridges).
Definition: viewport.cpp:111
InitializeSpriteSorter
void InitializeSpriteSorter()
Choose the "best" sprite sorter and set _vp_sprite_sorter.
Definition: viewport.cpp:3527
viewport_sprite_sorter.h
Viewport::virtual_height
int virtual_height
height << zoom
Definition: viewport_type.h:31
GetBridgePixelHeight
int GetBridgePixelHeight(TileIndex tile)
Get the height ('z') of a bridge in pixels.
Definition: bridge_map.h:84
HT_POINT
@ HT_POINT
point (lower land, raise land, level land, ...)
Definition: tilehighlight_type.h:22
InverseRemapCoords
Point InverseRemapCoords(int x, int y)
Map 2D viewport or smallmap coordinate to 3D world or tile coordinate.
Definition: landscape.h:109
VPM_X_AND_Y_LIMITED
@ VPM_X_AND_Y_LIMITED
area of land of limited size
Definition: viewport_type.h:97
GetTilePixelSlopeOutsideMap
std::tuple< Slope, int > GetTilePixelSlopeOutsideMap(int x, int y)
Return the slope of a given tile, also for tiles outside the map (virtual "black" tiles).
Definition: tile_map.cpp:78
HT_DIR_HU
@ HT_DIR_HU
horizontal upper
Definition: tilehighlight_type.h:35
SpriteCombineMode
SpriteCombineMode
Mode of "sprite combining".
Definition: viewport.cpp:153
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition: geometry_type.hpp:75
CursorVars::pos
Point pos
logical mouse position
Definition: gfx_type.h:125
ScaleByZoom
int ScaleByZoom(int value, ZoomLevel zoom)
Scale by zoom level, usually shift left (when zoom > ZOOM_LVL_MIN) When shifting right,...
Definition: zoom_func.h:22
Sprite
Data structure describing a sprite.
Definition: spritecache.h:17
VPM_X_OR_Y
@ VPM_X_OR_Y
drag in X or Y direction
Definition: viewport_type.h:93
FOUNDATION_PART_NONE
@ FOUNDATION_PART_NONE
Neither foundation nor groundsprite drawn yet.
Definition: viewport.cpp:143
StringSpriteToDraw
Definition: viewport.cpp:114
DO_SHOW_WAYPOINT_NAMES
@ DO_SHOW_WAYPOINT_NAMES
Display waypoint names.
Definition: openttd.h:51
TileHighlightData::new_pos
Point new_pos
New value for pos; used to determine whether to redraw the selection.
Definition: tilehighlight_type.h:55
GetTilePixelZ
int GetTilePixelZ(TileIndex tile)
Get bottom height of the tile.
Definition: tile_map.h:302
ShowWaypointWindow
void ShowWaypointWindow(const Waypoint *wp)
Show the window for the given waypoint.
Definition: waypoint_gui.cpp:223
SLOPE_STEEP_N
@ SLOPE_STEEP_N
a steep slope falling to south (from north)
Definition: slope_type.h:69
HT_SPECIAL
@ HT_SPECIAL
special mode used for highlighting while dragging (and for tunnels/docks)
Definition: tilehighlight_type.h:23
Align
constexpr T Align(const T x, uint n)
Return the smallest multiple of n equal or greater than x.
Definition: math_func.hpp:37
ViewportSSCSS
Helper class for getting the best sprite sorter.
Definition: viewport.cpp:3513
town_kdtree.h
CmdScrollViewport
CommandCost CmdScrollViewport(DoCommandFlag flags, TileIndex tile, ViewportScrollTarget target, uint32_t ref)
Scroll players main viewport.
Definition: viewport.cpp:3546
network_func.h
ViewportAddVehicles
void ViewportAddVehicles(DrawPixelInfo *dpi)
Add the vehicle sprites that should be drawn at a part of the screen.
Definition: vehicle.cpp:1154
TileHighlightData::redsq
TileIndex redsq
The tile that has to get a red selection.
Definition: tilehighlight_type.h:72
TileHighlightData::freeze
bool freeze
Freeze highlight in place.
Definition: tilehighlight_type.h:53
TileSpriteToDraw
Definition: viewport.cpp:123
VPM_SIGNALDIRS
@ VPM_SIGNALDIRS
similar to VMP_RAILDIRS, but with different cursor
Definition: viewport_type.h:103
viewport_cmd.h
signs_base.h
GetStringBoundingBox
Dimension GetStringBoundingBox(std::string_view str, FontSize start_fontsize)
Return the string dimension in pixels.
Definition: gfx.cpp:851
PFE_DRAWWORLD
@ PFE_DRAWWORLD
Time spent drawing world viewports in GUI.
Definition: framerate_type.h:58
WSM_NONE
@ WSM_NONE
No special mouse mode.
Definition: window_gui.h:1043
DrawGroundSprite
void DrawGroundSprite(SpriteID image, PaletteID pal, const SubSprite *sub, int extra_offs_x, int extra_offs_y)
Draws a ground sprite for the current tile.
Definition: viewport.cpp:589
Map::SizeY
static uint SizeY()
Get the size of the map along the Y.
Definition: map_func.h:279
GUISettings::smooth_scroll
bool smooth_scroll
smooth scroll viewports
Definition: settings_type.h:154
ClientSettings::gui
GUISettings gui
settings related to the GUI
Definition: settings_type.h:611
ZoomLevel
ZoomLevel
All zoom levels we know.
Definition: zoom_type.h:16
TownCache::sign
TrackedViewportSign sign
Location of name sign, UpdateVirtCoord updates this.
Definition: town.h:45
RemapCoords
Point RemapCoords(int x, int y, int z)
Map 3D world or tile coordinate to equivalent 2D coordinate as used in the viewports and smallmap.
Definition: landscape.h:79
TileHighlightData::selend
Point selend
The location where the drag currently ends.
Definition: tilehighlight_type.h:61
VST_CLIENT
@ VST_CLIENT
Single player.
Definition: viewport_type.h:151
HT_DIR_X
@ HT_DIR_X
X direction.
Definition: tilehighlight_type.h:33
SetViewportCatchmentTown
void SetViewportCatchmentTown(const Town *t, bool sel)
Select or deselect town for coverage area highlight.
Definition: viewport.cpp:3655
TileHighlightData::GetCallbackWnd
Window * GetCallbackWnd()
Get the window that started the current highlighting.
Definition: viewport.cpp:2585
DrawPixelInfo
Data about how and where to blit pixels.
Definition: gfx_type.h:157
TileHighlightData::Reset
void Reset()
Reset tile highlighting.
Definition: viewport.cpp:2564
OppositeCorner
Corner OppositeCorner(Corner corner)
Returns the opposite corner.
Definition: slope_func.h:184
GetTownIndex
TownID GetTownIndex(Tile t)
Get the index of which town this house/street is attached to.
Definition: town_map.h:23
TileSpriteToDraw::x
int32_t x
screen X coordinate of sprite
Definition: viewport.cpp:127
backup_type.hpp
SPRITE_COMBINE_PENDING
@ SPRITE_COMBINE_PENDING
Sprite combining will start with the next unclipped sprite.
Definition: viewport.cpp:155
DrawSpriteViewport
void DrawSpriteViewport(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub)
Draw a sprite in a viewport.
Definition: gfx.cpp:960
HasBit
constexpr debug_inline bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103