OpenTTD
viewport.cpp
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
65 #include "stdafx.h"
66 #include "landscape.h"
67 #include "viewport_func.h"
68 #include "station_base.h"
69 #include "waypoint_base.h"
70 #include "town.h"
71 #include "signs_base.h"
72 #include "signs_func.h"
73 #include "vehicle_base.h"
74 #include "vehicle_gui.h"
75 #include "blitter/factory.hpp"
76 #include "strings_func.h"
77 #include "zoom_func.h"
78 #include "vehicle_func.h"
79 #include "company_func.h"
80 #include "waypoint_func.h"
81 #include "window_func.h"
82 #include "tilehighlight_func.h"
83 #include "window_gui.h"
85 #include "viewport_sprite_sorter.h"
86 #include "bridge_map.h"
87 #include "company_base.h"
88 #include "command_func.h"
89 #include "network/network_func.h"
90 #include "framerate_type.h"
91 
92 #include <map>
93 
94 #include "table/strings.h"
95 #include "table/string_colours.h"
96 
97 #include "safeguards.h"
98 
99 Point _tile_fract_coords;
100 
101 
102 static const int MAX_TILE_EXTENT_LEFT = ZOOM_LVL_BASE * TILE_PIXELS;
103 static const int MAX_TILE_EXTENT_RIGHT = ZOOM_LVL_BASE * TILE_PIXELS;
104 static const int MAX_TILE_EXTENT_TOP = ZOOM_LVL_BASE * MAX_BUILDING_PIXELS;
105 static const int MAX_TILE_EXTENT_BOTTOM = ZOOM_LVL_BASE * (TILE_PIXELS + 2 * TILE_HEIGHT);
106 
108  StringID string;
109  Colours colour;
110  int32 x;
111  int32 y;
112  uint64 params[2];
113  uint16 width;
114 };
115 
117  SpriteID image;
118  PaletteID pal;
119  const SubSprite *sub;
120  int32 x;
121  int32 y;
122 };
123 
125  SpriteID image;
126  PaletteID pal;
127  const SubSprite *sub;
128  int32 x;
129  int32 y;
130  int next;
131 };
132 
138  FOUNDATION_PART_END
139 };
140 
149 };
150 
155 
158  DrawPixelInfo dpi;
159 
160  StringSpriteToDrawVector string_sprites_to_draw;
161  TileSpriteToDrawVector tile_sprites_to_draw;
162  ParentSpriteToDrawVector parent_sprites_to_draw;
164  ChildScreenSpriteToDrawVector child_screen_sprites_to_draw;
165 
166  int *last_child;
167 
169 
170  int foundation[FOUNDATION_PART_END];
172  int *last_foundation_child[FOUNDATION_PART_END];
173  Point foundation_offset[FOUNDATION_PART_END];
174 };
175 
176 static void MarkViewportDirty(const ViewPort *vp, int left, int top, int right, int bottom);
177 
178 static ViewportDrawer _vd;
179 
180 TileHighlightData _thd;
181 static TileInfo *_cur_ti;
182 bool _draw_bounding_boxes = false;
183 bool _draw_dirty_blocks = false;
184 uint _dirty_block_colour = 0;
185 static VpSpriteSorter _vp_sprite_sorter = NULL;
186 
187 static Point MapXYZToViewport(const ViewPort *vp, int x, int y, int z)
188 {
189  Point p = RemapCoords(x, y, z);
190  p.x -= vp->virtual_width / 2;
191  p.y -= vp->virtual_height / 2;
192  return p;
193 }
194 
195 void DeleteWindowViewport(Window *w)
196 {
197  if (w->viewport == NULL) return;
198 
199  delete w->viewport->overlay;
200  free(w->viewport);
201  w->viewport = NULL;
202 }
203 
216 void InitializeWindowViewport(Window *w, int x, int y,
217  int width, int height, uint32 follow_flags, ZoomLevel zoom)
218 {
219  assert(w->viewport == NULL);
220 
221  ViewportData *vp = CallocT<ViewportData>(1);
222 
223  vp->left = x + w->left;
224  vp->top = y + w->top;
225  vp->width = width;
226  vp->height = height;
227 
229 
230  vp->virtual_width = ScaleByZoom(width, zoom);
231  vp->virtual_height = ScaleByZoom(height, zoom);
232 
233  Point pt;
234 
235  if (follow_flags & 0x80000000) {
236  const Vehicle *veh;
237 
238  vp->follow_vehicle = (VehicleID)(follow_flags & 0xFFFFF);
239  veh = Vehicle::Get(vp->follow_vehicle);
240  pt = MapXYZToViewport(vp, veh->x_pos, veh->y_pos, veh->z_pos);
241  } else {
242  uint x = TileX(follow_flags) * TILE_SIZE;
243  uint y = TileY(follow_flags) * TILE_SIZE;
244 
246  pt = MapXYZToViewport(vp, x, y, GetSlopePixelZ(x, y));
247  }
248 
249  vp->scrollpos_x = pt.x;
250  vp->scrollpos_y = pt.y;
251  vp->dest_scrollpos_x = pt.x;
252  vp->dest_scrollpos_y = pt.y;
253 
254  vp->overlay = NULL;
255 
256  w->viewport = vp;
257  vp->virtual_left = 0; // pt.x;
258  vp->virtual_top = 0; // pt.y;
259 }
260 
261 static Point _vp_move_offs;
262 
263 static void DoSetViewportPosition(const Window *w, int left, int top, int width, int height)
264 {
266  if (left + width > w->left &&
267  w->left + w->width > left &&
268  top + height > w->top &&
269  w->top + w->height > top) {
270 
271  if (left < w->left) {
272  DoSetViewportPosition(w, left, top, w->left - left, height);
273  DoSetViewportPosition(w, left + (w->left - left), top, width - (w->left - left), height);
274  return;
275  }
276 
277  if (left + width > w->left + w->width) {
278  DoSetViewportPosition(w, left, top, (w->left + w->width - left), height);
279  DoSetViewportPosition(w, left + (w->left + w->width - left), top, width - (w->left + w->width - left), height);
280  return;
281  }
282 
283  if (top < w->top) {
284  DoSetViewportPosition(w, left, top, width, (w->top - top));
285  DoSetViewportPosition(w, left, top + (w->top - top), width, height - (w->top - top));
286  return;
287  }
288 
289  if (top + height > w->top + w->height) {
290  DoSetViewportPosition(w, left, top, width, (w->top + w->height - top));
291  DoSetViewportPosition(w, left, top + (w->top + w->height - top), width, height - (w->top + w->height - top));
292  return;
293  }
294 
295  return;
296  }
297  }
298 
299  {
300  int xo = _vp_move_offs.x;
301  int yo = _vp_move_offs.y;
302 
303  if (abs(xo) >= width || abs(yo) >= height) {
304  /* fully_outside */
305  RedrawScreenRect(left, top, left + width, top + height);
306  return;
307  }
308 
309  GfxScroll(left, top, width, height, xo, yo);
310 
311  if (xo > 0) {
312  RedrawScreenRect(left, top, xo + left, top + height);
313  left += xo;
314  width -= xo;
315  } else if (xo < 0) {
316  RedrawScreenRect(left + width + xo, top, left + width, top + height);
317  width += xo;
318  }
319 
320  if (yo > 0) {
321  RedrawScreenRect(left, top, width + left, top + yo);
322  } else if (yo < 0) {
323  RedrawScreenRect(left, top + height + yo, width + left, top + height);
324  }
325  }
326 }
327 
328 static void SetViewportPosition(Window *w, int x, int y)
329 {
330  ViewPort *vp = w->viewport;
331  int old_left = vp->virtual_left;
332  int old_top = vp->virtual_top;
333  int i;
334  int left, top, width, height;
335 
336  vp->virtual_left = x;
337  vp->virtual_top = y;
338 
339  /* Viewport is bound to its left top corner, so it must be rounded down (UnScaleByZoomLower)
340  * else glitch described in FS#1412 will happen (offset by 1 pixel with zoom level > NORMAL)
341  */
342  old_left = UnScaleByZoomLower(old_left, vp->zoom);
343  old_top = UnScaleByZoomLower(old_top, vp->zoom);
344  x = UnScaleByZoomLower(x, vp->zoom);
345  y = UnScaleByZoomLower(y, vp->zoom);
346 
347  old_left -= x;
348  old_top -= y;
349 
350  if (old_top == 0 && old_left == 0) return;
351 
352  _vp_move_offs.x = old_left;
353  _vp_move_offs.y = old_top;
354 
355  left = vp->left;
356  top = vp->top;
357  width = vp->width;
358  height = vp->height;
359 
360  if (left < 0) {
361  width += left;
362  left = 0;
363  }
364 
365  i = left + width - _screen.width;
366  if (i >= 0) width -= i;
367 
368  if (width > 0) {
369  if (top < 0) {
370  height += top;
371  top = 0;
372  }
373 
374  i = top + height - _screen.height;
375  if (i >= 0) height -= i;
376 
377  if (height > 0) DoSetViewportPosition(w->z_front, left, top, width, height);
378  }
379 }
380 
389 ViewPort *IsPtInWindowViewport(const Window *w, int x, int y)
390 {
391  ViewPort *vp = w->viewport;
392 
393  if (vp != NULL &&
394  IsInsideMM(x, vp->left, vp->left + vp->width) &&
395  IsInsideMM(y, vp->top, vp->top + vp->height))
396  return vp;
397 
398  return NULL;
399 }
400 
413 Point TranslateXYToTileCoord(const ViewPort *vp, int x, int y, bool clamp_to_map)
414 {
415  if (!IsInsideBS(x, vp->left, vp->width) || !IsInsideBS(y, vp->top, vp->height)) {
416  Point pt = { -1, -1 };
417  return pt;
418  }
419 
420  return InverseRemapCoords2(
421  ScaleByZoom(x - vp->left, vp->zoom) + vp->virtual_left,
422  ScaleByZoom(y - vp->top, vp->zoom) + vp->virtual_top, clamp_to_map);
423 }
424 
425 /* When used for zooming, check area below current coordinates (x,y)
426  * and return the tile of the zoomed out/in position (zoom_x, zoom_y)
427  * when you just want the tile, make x = zoom_x and y = zoom_y */
428 static Point GetTileFromScreenXY(int x, int y, int zoom_x, int zoom_y)
429 {
430  Window *w;
431  ViewPort *vp;
432  Point pt;
433 
434  if ( (w = FindWindowFromPt(x, y)) != NULL &&
435  (vp = IsPtInWindowViewport(w, x, y)) != NULL)
436  return TranslateXYToTileCoord(vp, zoom_x, zoom_y);
437 
438  pt.y = pt.x = -1;
439  return pt;
440 }
441 
442 Point GetTileBelowCursor()
443 {
444  return GetTileFromScreenXY(_cursor.pos.x, _cursor.pos.y, _cursor.pos.x, _cursor.pos.y);
445 }
446 
447 
448 Point GetTileZoomCenterWindow(bool in, Window * w)
449 {
450  int x, y;
451  ViewPort *vp = w->viewport;
452 
453  if (in) {
454  x = ((_cursor.pos.x - vp->left) >> 1) + (vp->width >> 2);
455  y = ((_cursor.pos.y - vp->top) >> 1) + (vp->height >> 2);
456  } else {
457  x = vp->width - (_cursor.pos.x - vp->left);
458  y = vp->height - (_cursor.pos.y - vp->top);
459  }
460  /* Get the tile below the cursor and center on the zoomed-out center */
461  return GetTileFromScreenXY(_cursor.pos.x, _cursor.pos.y, x + vp->left, y + vp->top);
462 }
463 
472 void HandleZoomMessage(Window *w, const ViewPort *vp, byte widget_zoom_in, byte widget_zoom_out)
473 {
474  w->SetWidgetDisabledState(widget_zoom_in, vp->zoom <= _settings_client.gui.zoom_min);
475  w->SetWidgetDirty(widget_zoom_in);
476 
477  w->SetWidgetDisabledState(widget_zoom_out, vp->zoom >= _settings_client.gui.zoom_max);
478  w->SetWidgetDirty(widget_zoom_out);
479 }
480 
493 static void AddTileSpriteToDraw(SpriteID image, PaletteID pal, int32 x, int32 y, int z, const SubSprite *sub = NULL, int extra_offs_x = 0, int extra_offs_y = 0)
494 {
495  assert((image & SPRITE_MASK) < MAX_SPRITES);
496 
497  TileSpriteToDraw *ts = _vd.tile_sprites_to_draw.Append();
498  ts->image = image;
499  ts->pal = pal;
500  ts->sub = sub;
501  Point pt = RemapCoords(x, y, z);
502  ts->x = pt.x + extra_offs_x;
503  ts->y = pt.y + extra_offs_y;
504 }
505 
518 static void AddChildSpriteToFoundation(SpriteID image, PaletteID pal, const SubSprite *sub, FoundationPart foundation_part, int extra_offs_x, int extra_offs_y)
519 {
520  assert(IsInsideMM(foundation_part, 0, FOUNDATION_PART_END));
521  assert(_vd.foundation[foundation_part] != -1);
522  Point offs = _vd.foundation_offset[foundation_part];
523 
524  /* Change the active ChildSprite list to the one of the foundation */
525  int *old_child = _vd.last_child;
526  _vd.last_child = _vd.last_foundation_child[foundation_part];
527 
528  AddChildSpriteScreen(image, pal, offs.x + extra_offs_x, offs.y + extra_offs_y, false, sub, false);
529 
530  /* Switch back to last ChildSprite list */
531  _vd.last_child = old_child;
532 }
533 
547 void DrawGroundSpriteAt(SpriteID image, PaletteID pal, int32 x, int32 y, int z, const SubSprite *sub, int extra_offs_x, int extra_offs_y)
548 {
549  /* Switch to first foundation part, if no foundation was drawn */
551 
552  if (_vd.foundation[_vd.foundation_part] != -1) {
553  Point pt = RemapCoords(x, y, z);
554  AddChildSpriteToFoundation(image, pal, sub, _vd.foundation_part, pt.x + extra_offs_x * ZOOM_LVL_BASE, pt.y + extra_offs_y * ZOOM_LVL_BASE);
555  } else {
556  AddTileSpriteToDraw(image, pal, _cur_ti->x + x, _cur_ti->y + y, _cur_ti->z + z, sub, extra_offs_x * ZOOM_LVL_BASE, extra_offs_y * ZOOM_LVL_BASE);
557  }
558 }
559 
570 void DrawGroundSprite(SpriteID image, PaletteID pal, const SubSprite *sub, int extra_offs_x, int extra_offs_y)
571 {
572  DrawGroundSpriteAt(image, pal, 0, 0, 0, sub, extra_offs_x, extra_offs_y);
573 }
574 
582 void OffsetGroundSprite(int x, int y)
583 {
584  /* Switch to next foundation part */
585  switch (_vd.foundation_part) {
588  break;
591  break;
592  default: NOT_REACHED();
593  }
594 
595  /* _vd.last_child == NULL if foundation sprite was clipped by the viewport bounds */
596  if (_vd.last_child != NULL) _vd.foundation[_vd.foundation_part] = _vd.parent_sprites_to_draw.Length() - 1;
597 
598  _vd.foundation_offset[_vd.foundation_part].x = x * ZOOM_LVL_BASE;
599  _vd.foundation_offset[_vd.foundation_part].y = y * ZOOM_LVL_BASE;
600  _vd.last_foundation_child[_vd.foundation_part] = _vd.last_child;
601 }
602 
614 static void AddCombinedSprite(SpriteID image, PaletteID pal, int x, int y, int z, const SubSprite *sub)
615 {
616  Point pt = RemapCoords(x, y, z);
617  const Sprite *spr = GetSprite(image & SPRITE_MASK, ST_NORMAL);
618 
619  if (pt.x + spr->x_offs >= _vd.dpi.left + _vd.dpi.width ||
620  pt.x + spr->x_offs + spr->width <= _vd.dpi.left ||
621  pt.y + spr->y_offs >= _vd.dpi.top + _vd.dpi.height ||
622  pt.y + spr->y_offs + spr->height <= _vd.dpi.top)
623  return;
624 
625  const ParentSpriteToDraw *pstd = _vd.parent_sprites_to_draw.End() - 1;
626  AddChildSpriteScreen(image, pal, pt.x - pstd->left, pt.y - pstd->top, false, sub, false);
627 }
628 
654 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)
655 {
656  int32 left, right, top, bottom;
657 
658  assert((image & SPRITE_MASK) < MAX_SPRITES);
659 
660  /* make the sprites transparent with the right palette */
661  if (transparent) {
664  }
665 
667  AddCombinedSprite(image, pal, x, y, z, sub);
668  return;
669  }
670 
671  _vd.last_child = NULL;
672 
673  Point pt = RemapCoords(x, y, z);
674  int tmp_left, tmp_top, tmp_x = pt.x, tmp_y = pt.y;
675 
676  /* Compute screen extents of sprite */
677  if (image == SPR_EMPTY_BOUNDING_BOX) {
678  left = tmp_left = RemapCoords(x + w , y + bb_offset_y, z + bb_offset_z).x;
679  right = RemapCoords(x + bb_offset_x, y + h , z + bb_offset_z).x + 1;
680  top = tmp_top = RemapCoords(x + bb_offset_x, y + bb_offset_y, z + dz ).y;
681  bottom = RemapCoords(x + w , y + h , z + bb_offset_z).y + 1;
682  } else {
683  const Sprite *spr = GetSprite(image & SPRITE_MASK, ST_NORMAL);
684  left = tmp_left = (pt.x += spr->x_offs);
685  right = (pt.x + spr->width );
686  top = tmp_top = (pt.y += spr->y_offs);
687  bottom = (pt.y + spr->height);
688  }
689 
690  if (_draw_bounding_boxes && (image != SPR_EMPTY_BOUNDING_BOX)) {
691  /* Compute maximal extents of sprite and its bounding box */
692  left = min(left , RemapCoords(x + w , y + bb_offset_y, z + bb_offset_z).x);
693  right = max(right , RemapCoords(x + bb_offset_x, y + h , z + bb_offset_z).x + 1);
694  top = min(top , RemapCoords(x + bb_offset_x, y + bb_offset_y, z + dz ).y);
695  bottom = max(bottom, RemapCoords(x + w , y + h , z + bb_offset_z).y + 1);
696  }
697 
698  /* Do not add the sprite to the viewport, if it is outside */
699  if (left >= _vd.dpi.left + _vd.dpi.width ||
700  right <= _vd.dpi.left ||
701  top >= _vd.dpi.top + _vd.dpi.height ||
702  bottom <= _vd.dpi.top) {
703  return;
704  }
705 
706  ParentSpriteToDraw *ps = _vd.parent_sprites_to_draw.Append();
707  ps->x = tmp_x;
708  ps->y = tmp_y;
709 
710  ps->left = tmp_left;
711  ps->top = tmp_top;
712 
713  ps->image = image;
714  ps->pal = pal;
715  ps->sub = sub;
716  ps->xmin = x + bb_offset_x;
717  ps->xmax = x + max(bb_offset_x, w) - 1;
718 
719  ps->ymin = y + bb_offset_y;
720  ps->ymax = y + max(bb_offset_y, h) - 1;
721 
722  ps->zmin = z + bb_offset_z;
723  ps->zmax = z + max(bb_offset_z, dz) - 1;
724 
725  ps->comparison_done = false;
726  ps->first_child = -1;
727 
728  _vd.last_child = &ps->first_child;
729 
731 }
732 
752 {
753  assert(_vd.combine_sprites == SPRITE_COMBINE_NONE);
755 }
756 
762 {
763  assert(_vd.combine_sprites != SPRITE_COMBINE_NONE);
765 }
766 
776 static bool IsInRangeInclusive(int begin, int end, int check)
777 {
778  if (begin > end) Swap(begin, end);
779  return begin <= check && check <= end;
780 }
781 
788 bool IsInsideRotatedRectangle(int x, int y)
789 {
790  int dist_a = (_thd.size.x + _thd.size.y); // Rotated coordinate system for selected rectangle.
791  int dist_b = (_thd.size.x - _thd.size.y); // We don't have to divide by 2. It's all relative!
792  int a = ((x - _thd.pos.x) + (y - _thd.pos.y)); // Rotated coordinate system for the point under scrutiny.
793  int b = ((x - _thd.pos.x) - (y - _thd.pos.y));
794 
795  /* Check if a and b are between 0 and dist_a or dist_b respectively. */
796  return IsInRangeInclusive(dist_a, 0, a) && IsInRangeInclusive(dist_b, 0, b);
797 }
798 
809 void AddChildSpriteScreen(SpriteID image, PaletteID pal, int x, int y, bool transparent, const SubSprite *sub, bool scale)
810 {
811  assert((image & SPRITE_MASK) < MAX_SPRITES);
812 
813  /* If the ParentSprite was clipped by the viewport bounds, do not draw the ChildSprites either */
814  if (_vd.last_child == NULL) return;
815 
816  /* make the sprites transparent with the right palette */
817  if (transparent) {
820  }
821 
822  *_vd.last_child = _vd.child_screen_sprites_to_draw.Length();
823 
824  ChildScreenSpriteToDraw *cs = _vd.child_screen_sprites_to_draw.Append();
825  cs->image = image;
826  cs->pal = pal;
827  cs->sub = sub;
828  cs->x = scale ? x * ZOOM_LVL_BASE : x;
829  cs->y = scale ? y * ZOOM_LVL_BASE : y;
830  cs->next = -1;
831 
832  /* Append the sprite to the active ChildSprite list.
833  * If the active ParentSprite is a foundation, update last_foundation_child as well.
834  * Note: ChildSprites of foundations are NOT sequential in the vector, as selection sprites are added at last. */
835  if (_vd.last_foundation_child[0] == _vd.last_child) _vd.last_foundation_child[0] = &cs->next;
836  if (_vd.last_foundation_child[1] == _vd.last_child) _vd.last_foundation_child[1] = &cs->next;
837  _vd.last_child = &cs->next;
838 }
839 
840 static void AddStringToDraw(int x, int y, StringID string, uint64 params_1, uint64 params_2, Colours colour, uint16 width)
841 {
842  assert(width != 0);
843  StringSpriteToDraw *ss = _vd.string_sprites_to_draw.Append();
844  ss->string = string;
845  ss->x = x;
846  ss->y = y;
847  ss->params[0] = params_1;
848  ss->params[1] = params_2;
849  ss->width = width;
850  ss->colour = colour;
851 }
852 
853 
865 static void DrawSelectionSprite(SpriteID image, PaletteID pal, const TileInfo *ti, int z_offset, FoundationPart foundation_part)
866 {
867  /* FIXME: This is not totally valid for some autorail highlights that extend over the edges of the tile. */
868  if (_vd.foundation[foundation_part] == -1) {
869  /* draw on real ground */
870  AddTileSpriteToDraw(image, pal, ti->x, ti->y, ti->z + z_offset);
871  } else {
872  /* draw on top of foundation */
873  AddChildSpriteToFoundation(image, pal, NULL, foundation_part, 0, -z_offset * ZOOM_LVL_BASE);
874  }
875 }
876 
883 static void DrawTileSelectionRect(const TileInfo *ti, PaletteID pal)
884 {
885  if (!IsValidTile(ti->tile)) return;
886 
887  SpriteID sel;
888  if (IsHalftileSlope(ti->tileh)) {
889  Corner halftile_corner = GetHalftileSlopeCorner(ti->tileh);
890  SpriteID sel2 = SPR_HALFTILE_SELECTION_FLAT + halftile_corner;
892 
893  Corner opposite_corner = OppositeCorner(halftile_corner);
894  if (IsSteepSlope(ti->tileh)) {
895  sel = SPR_HALFTILE_SELECTION_DOWN;
896  } else {
897  sel = ((ti->tileh & SlopeWithOneCornerRaised(opposite_corner)) != 0 ? SPR_HALFTILE_SELECTION_UP : SPR_HALFTILE_SELECTION_FLAT);
898  }
899  sel += opposite_corner;
900  } else {
901  sel = SPR_SELECT_TILE + SlopeToSpriteOffset(ti->tileh);
902  }
904 }
905 
906 static bool IsPartOfAutoLine(int px, int py)
907 {
908  px -= _thd.selstart.x;
909  py -= _thd.selstart.y;
910 
911  if ((_thd.drawstyle & HT_DRAG_MASK) != HT_LINE) return false;
912 
913  switch (_thd.drawstyle & HT_DIR_MASK) {
914  case HT_DIR_X: return py == 0; // x direction
915  case HT_DIR_Y: return px == 0; // y direction
916  case HT_DIR_HU: return px == -py || px == -py - 16; // horizontal upper
917  case HT_DIR_HL: return px == -py || px == -py + 16; // horizontal lower
918  case HT_DIR_VL: return px == py || px == py + 16; // vertical left
919  case HT_DIR_VR: return px == py || px == py - 16; // vertical right
920  default:
921  NOT_REACHED();
922  }
923 }
924 
925 /* [direction][side] */
926 static const HighLightStyle _autorail_type[6][2] = {
927  { HT_DIR_X, HT_DIR_X },
928  { HT_DIR_Y, HT_DIR_Y },
929  { HT_DIR_HU, HT_DIR_HL },
930  { HT_DIR_HL, HT_DIR_HU },
931  { HT_DIR_VL, HT_DIR_VR },
932  { HT_DIR_VR, HT_DIR_VL }
933 };
934 
935 #include "table/autorail.h"
936 
943 static void DrawAutorailSelection(const TileInfo *ti, uint autorail_type)
944 {
945  SpriteID image;
946  PaletteID pal;
947  int offset;
948 
949  FoundationPart foundation_part = FOUNDATION_PART_NORMAL;
950  Slope autorail_tileh = RemoveHalftileSlope(ti->tileh);
951  if (IsHalftileSlope(ti->tileh)) {
952  static const uint _lower_rail[4] = { 5U, 2U, 4U, 3U };
953  Corner halftile_corner = GetHalftileSlopeCorner(ti->tileh);
954  if (autorail_type != _lower_rail[halftile_corner]) {
955  foundation_part = FOUNDATION_PART_HALFTILE;
956  /* Here we draw the highlights of the "three-corners-raised"-slope. That looks ok to me. */
957  autorail_tileh = SlopeWithThreeCornersRaised(OppositeCorner(halftile_corner));
958  }
959  }
960 
961  offset = _AutorailTilehSprite[autorail_tileh][autorail_type];
962  if (offset >= 0) {
963  image = SPR_AUTORAIL_BASE + offset;
964  pal = PAL_NONE;
965  } else {
966  image = SPR_AUTORAIL_BASE - offset;
967  pal = PALETTE_SEL_TILE_RED;
968  }
969 
970  DrawSelectionSprite(image, _thd.make_square_red ? PALETTE_SEL_TILE_RED : pal, ti, 7, foundation_part);
971 }
972 
977 static void DrawTileSelection(const TileInfo *ti)
978 {
979  /* Draw a red error square? */
980  bool is_redsq = _thd.redsq == ti->tile;
982 
983  /* No tile selection active? */
984  if ((_thd.drawstyle & HT_DRAG_MASK) == HT_NONE) return;
985 
986  if (_thd.diagonal) { // We're drawing a 45 degrees rotated (diagonal) rectangle
987  if (IsInsideRotatedRectangle((int)ti->x, (int)ti->y)) goto draw_inner;
988  return;
989  }
990 
991  /* Inside the inner area? */
992  if (IsInsideBS(ti->x, _thd.pos.x, _thd.size.x) &&
993  IsInsideBS(ti->y, _thd.pos.y, _thd.size.y)) {
994 draw_inner:
995  if (_thd.drawstyle & HT_RECT) {
996  if (!is_redsq) DrawTileSelectionRect(ti, _thd.make_square_red ? PALETTE_SEL_TILE_RED : PAL_NONE);
997  } else if (_thd.drawstyle & HT_POINT) {
998  /* Figure out the Z coordinate for the single dot. */
999  int z = 0;
1000  FoundationPart foundation_part = FOUNDATION_PART_NORMAL;
1001  if (ti->tileh & SLOPE_N) {
1002  z += TILE_HEIGHT;
1004  }
1005  if (IsHalftileSlope(ti->tileh)) {
1006  Corner halftile_corner = GetHalftileSlopeCorner(ti->tileh);
1007  if ((halftile_corner == CORNER_W) || (halftile_corner == CORNER_E)) z += TILE_HEIGHT;
1008  if (halftile_corner != CORNER_S) {
1009  foundation_part = FOUNDATION_PART_HALFTILE;
1010  if (IsSteepSlope(ti->tileh)) z -= TILE_HEIGHT;
1011  }
1012  }
1013  DrawSelectionSprite(_cur_dpi->zoom <= ZOOM_LVL_DETAIL ? SPR_DOT : SPR_DOT_SMALL, PAL_NONE, ti, z, foundation_part);
1014  } else if (_thd.drawstyle & HT_RAIL) {
1015  /* autorail highlight piece under cursor */
1016  HighLightStyle type = _thd.drawstyle & HT_DIR_MASK;
1017  assert(type < HT_DIR_END);
1018  DrawAutorailSelection(ti, _autorail_type[type][0]);
1019  } else if (IsPartOfAutoLine(ti->x, ti->y)) {
1020  /* autorail highlighting long line */
1021  HighLightStyle dir = _thd.drawstyle & HT_DIR_MASK;
1022  uint side;
1023 
1024  if (dir == HT_DIR_X || dir == HT_DIR_Y) {
1025  side = 0;
1026  } else {
1027  TileIndex start = TileVirtXY(_thd.selstart.x, _thd.selstart.y);
1028  side = Delta(Delta(TileX(start), TileX(ti->tile)), Delta(TileY(start), TileY(ti->tile)));
1029  }
1030 
1031  DrawAutorailSelection(ti, _autorail_type[dir][side]);
1032  }
1033  return;
1034  }
1035 
1036  /* Check if it's inside the outer area? */
1037  if (!is_redsq && _thd.outersize.x > 0 &&
1038  IsInsideBS(ti->x, _thd.pos.x + _thd.offs.x, _thd.size.x + _thd.outersize.x) &&
1039  IsInsideBS(ti->y, _thd.pos.y + _thd.offs.y, _thd.size.y + _thd.outersize.y)) {
1040  /* Draw a blue rect. */
1042  return;
1043  }
1044 }
1045 
1052 static int GetViewportY(Point tile)
1053 {
1054  /* Each increment in X or Y direction moves down by half a tile, i.e. TILE_PIXELS / 2. */
1055  return (tile.y * (int)(TILE_PIXELS / 2) + tile.x * (int)(TILE_PIXELS / 2) - TilePixelHeightOutsideMap(tile.x, tile.y)) << ZOOM_LVL_SHIFT;
1056 }
1057 
1062 {
1063  assert(_vd.dpi.top <= _vd.dpi.top + _vd.dpi.height);
1064  assert(_vd.dpi.left <= _vd.dpi.left + _vd.dpi.width);
1065 
1066  Point upper_left = InverseRemapCoords(_vd.dpi.left, _vd.dpi.top);
1067  Point upper_right = InverseRemapCoords(_vd.dpi.left + _vd.dpi.width, _vd.dpi.top);
1068 
1069  /* Transformations between tile coordinates and viewport rows/columns: See vp_column_row
1070  * column = y - x
1071  * row = x + y
1072  * x = (row - column) / 2
1073  * y = (row + column) / 2
1074  * Note: (row, columns) pairs are only valid, if they are both even or both odd.
1075  */
1076 
1077  /* Columns overlap with neighbouring columns by a half tile.
1078  * - Left column is column of upper_left (rounded down) and one column to the left.
1079  * - Right column is column of upper_right (rounded up) and one column to the right.
1080  * Note: Integer-division does not round down for negative numbers, so ensure rounding with another increment/decrement.
1081  */
1082  int left_column = (upper_left.y - upper_left.x) / (int)TILE_SIZE - 2;
1083  int right_column = (upper_right.y - upper_right.x) / (int)TILE_SIZE + 2;
1084 
1085  int potential_bridge_height = ZOOM_LVL_BASE * TILE_HEIGHT * _settings_game.construction.max_bridge_height;
1086 
1087  /* Rows overlap with neighbouring rows by a half tile.
1088  * The first row that could possibly be visible is the row above upper_left (if it is at height 0).
1089  * Due to integer-division not rounding down for negative numbers, we need another decrement.
1090  */
1091  int row = (upper_left.x + upper_left.y) / (int)TILE_SIZE - 2;
1092  bool last_row = false;
1093  for (; !last_row; row++) {
1094  last_row = true;
1095  for (int column = left_column; column <= right_column; column++) {
1096  /* Valid row/column? */
1097  if ((row + column) % 2 != 0) continue;
1098 
1099  Point tilecoord;
1100  tilecoord.x = (row - column) / 2;
1101  tilecoord.y = (row + column) / 2;
1102  assert(column == tilecoord.y - tilecoord.x);
1103  assert(row == tilecoord.y + tilecoord.x);
1104 
1105  TileType tile_type;
1106  TileInfo tile_info;
1107  _cur_ti = &tile_info;
1108  tile_info.x = tilecoord.x * TILE_SIZE; // FIXME tile_info should use signed integers
1109  tile_info.y = tilecoord.y * TILE_SIZE;
1110 
1111  if (IsInsideBS(tilecoord.x, 0, MapSizeX()) && IsInsideBS(tilecoord.y, 0, MapSizeY())) {
1112  /* This includes the south border at MapMaxX / MapMaxY. When terraforming we still draw tile selections there. */
1113  tile_info.tile = TileXY(tilecoord.x, tilecoord.y);
1114  tile_type = GetTileType(tile_info.tile);
1115  } else {
1116  tile_info.tile = INVALID_TILE;
1117  tile_type = MP_VOID;
1118  }
1119 
1120  if (tile_type != MP_VOID) {
1121  /* We are inside the map => paint landscape. */
1122  tile_info.tileh = GetTilePixelSlope(tile_info.tile, &tile_info.z);
1123  } else {
1124  /* We are outside the map => paint black. */
1125  tile_info.tileh = GetTilePixelSlopeOutsideMap(tilecoord.x, tilecoord.y, &tile_info.z);
1126  }
1127 
1128  int viewport_y = GetViewportY(tilecoord);
1129 
1130  if (viewport_y + MAX_TILE_EXTENT_BOTTOM < _vd.dpi.top) {
1131  /* The tile in this column is not visible yet.
1132  * Tiles in other columns may be visible, but we need more rows in any case. */
1133  last_row = false;
1134  continue;
1135  }
1136 
1137  int min_visible_height = viewport_y - (_vd.dpi.top + _vd.dpi.height);
1138  bool tile_visible = min_visible_height <= 0;
1139 
1140  if (tile_type != MP_VOID) {
1141  /* Is tile with buildings visible? */
1142  if (min_visible_height < MAX_TILE_EXTENT_TOP) tile_visible = true;
1143 
1144  if (IsBridgeAbove(tile_info.tile)) {
1145  /* Is the bridge visible? */
1146  TileIndex bridge_tile = GetNorthernBridgeEnd(tile_info.tile);
1147  int bridge_height = ZOOM_LVL_BASE * (GetBridgePixelHeight(bridge_tile) - TilePixelHeight(tile_info.tile));
1148  if (min_visible_height < bridge_height + MAX_TILE_EXTENT_TOP) tile_visible = true;
1149  }
1150 
1151  /* Would a higher bridge on a more southern tile be visible?
1152  * If yes, we need to loop over more rows to possibly find one. */
1153  if (min_visible_height < potential_bridge_height + MAX_TILE_EXTENT_TOP) last_row = false;
1154  } else {
1155  /* Outside of map. If we are on the north border of the map, there may still be a bridge visible,
1156  * so we need to loop over more rows to possibly find one. */
1157  if ((tilecoord.x <= 0 || tilecoord.y <= 0) && min_visible_height < potential_bridge_height + MAX_TILE_EXTENT_TOP) last_row = false;
1158  }
1159 
1160  if (tile_visible) {
1161  last_row = false;
1163  _vd.foundation[0] = -1;
1164  _vd.foundation[1] = -1;
1165  _vd.last_foundation_child[0] = NULL;
1166  _vd.last_foundation_child[1] = NULL;
1167 
1168  _tile_type_procs[tile_type]->draw_tile_proc(&tile_info);
1169  if (tile_info.tile != INVALID_TILE) DrawTileSelection(&tile_info);
1170  }
1171  }
1172  }
1173 }
1174 
1185 void ViewportAddString(const DrawPixelInfo *dpi, ZoomLevel small_from, const ViewportSign *sign, StringID string_normal, StringID string_small, StringID string_small_shadow, uint64 params_1, uint64 params_2, Colours colour)
1186 {
1187  bool small = dpi->zoom >= small_from;
1188 
1189  int left = dpi->left;
1190  int top = dpi->top;
1191  int right = left + dpi->width;
1192  int bottom = top + dpi->height;
1193 
1194  int sign_height = ScaleByZoom(VPSM_TOP + FONT_HEIGHT_NORMAL + VPSM_BOTTOM, dpi->zoom);
1195  int sign_half_width = ScaleByZoom((small ? sign->width_small : sign->width_normal) / 2, dpi->zoom);
1196 
1197  if (bottom < sign->top ||
1198  top > sign->top + sign_height ||
1199  right < sign->center - sign_half_width ||
1200  left > sign->center + sign_half_width) {
1201  return;
1202  }
1203 
1204  if (!small) {
1205  AddStringToDraw(sign->center - sign_half_width, sign->top, string_normal, params_1, params_2, colour, sign->width_normal);
1206  } else {
1207  int shadow_offset = 0;
1208  if (string_small_shadow != STR_NULL) {
1209  shadow_offset = 4;
1210  AddStringToDraw(sign->center - sign_half_width + shadow_offset, sign->top, string_small_shadow, params_1, params_2, INVALID_COLOUR, sign->width_small);
1211  }
1212  AddStringToDraw(sign->center - sign_half_width, sign->top - shadow_offset, string_small, params_1, params_2,
1213  colour, sign->width_small | 0x8000);
1214  }
1215 }
1216 
1217 static void ViewportAddTownNames(DrawPixelInfo *dpi)
1218 {
1219  if (!HasBit(_display_opt, DO_SHOW_TOWN_NAMES) || _game_mode == GM_MENU) return;
1220 
1221  const Town *t;
1222  FOR_ALL_TOWNS(t) {
1224  _settings_client.gui.population_in_label ? STR_VIEWPORT_TOWN_POP : STR_VIEWPORT_TOWN,
1225  STR_VIEWPORT_TOWN_TINY_WHITE, STR_VIEWPORT_TOWN_TINY_BLACK,
1226  t->index, t->cache.population);
1227  }
1228 }
1229 
1230 
1231 static void ViewportAddStationNames(DrawPixelInfo *dpi)
1232 {
1233  if (!(HasBit(_display_opt, DO_SHOW_STATION_NAMES) || HasBit(_display_opt, DO_SHOW_WAYPOINT_NAMES)) || _game_mode == GM_MENU) return;
1234 
1235  const BaseStation *st;
1236  FOR_ALL_BASE_STATIONS(st) {
1237  /* Check whether the base station is a station or a waypoint */
1238  bool is_station = Station::IsExpected(st);
1239 
1240  /* Don't draw if the display options are disabled */
1241  if (!HasBit(_display_opt, is_station ? DO_SHOW_STATION_NAMES : DO_SHOW_WAYPOINT_NAMES)) continue;
1242 
1243  /* Don't draw if station is owned by another company and competitor station names are hidden. Stations owned by none are never ignored. */
1244  if (!HasBit(_display_opt, DO_SHOW_COMPETITOR_SIGNS) && _local_company != st->owner && st->owner != OWNER_NONE) continue;
1245 
1247  is_station ? STR_VIEWPORT_STATION : STR_VIEWPORT_WAYPOINT,
1248  (is_station ? STR_VIEWPORT_STATION : STR_VIEWPORT_WAYPOINT) + 1, STR_NULL,
1249  st->index, st->facilities, (st->owner == OWNER_NONE || !st->IsInUse()) ? COLOUR_GREY : _company_colours[st->owner]);
1250  }
1251 }
1252 
1253 
1254 static void ViewportAddSigns(DrawPixelInfo *dpi)
1255 {
1256  /* Signs are turned off or are invisible */
1258 
1259  const Sign *si;
1260  FOR_ALL_SIGNS(si) {
1261  /* Don't draw if sign is owned by another company and competitor signs should be hidden.
1262  * Note: It is intentional that also signs owned by OWNER_NONE are hidden. Bankrupt
1263  * companies can leave OWNER_NONE signs after them. */
1264  if (!HasBit(_display_opt, DO_SHOW_COMPETITOR_SIGNS) && _local_company != si->owner && si->owner != OWNER_DEITY) continue;
1265 
1266  ViewportAddString(dpi, ZOOM_LVL_OUT_16X, &si->sign,
1267  STR_WHITE_SIGN,
1268  (IsTransparencySet(TO_SIGNS) || si->owner == OWNER_DEITY) ? STR_VIEWPORT_SIGN_SMALL_WHITE : STR_VIEWPORT_SIGN_SMALL_BLACK, STR_NULL,
1269  si->index, 0, (si->owner == OWNER_NONE) ? COLOUR_GREY : (si->owner == OWNER_DEITY ? INVALID_COLOUR : _company_colours[si->owner]));
1270  }
1271 }
1272 
1280 void ViewportSign::UpdatePosition(int center, int top, StringID str, StringID str_small)
1281 {
1282  if (this->width_normal != 0) this->MarkDirty();
1283 
1284  this->top = top;
1285 
1286  char buffer[DRAW_STRING_BUFFER];
1287 
1288  GetString(buffer, str, lastof(buffer));
1289  this->width_normal = VPSM_LEFT + Align(GetStringBoundingBox(buffer).width, 2) + VPSM_RIGHT;
1290  this->center = center;
1291 
1292  /* zoomed out version */
1293  if (str_small != STR_NULL) {
1294  GetString(buffer, str_small, lastof(buffer));
1295  }
1296  this->width_small = VPSM_LEFT + Align(GetStringBoundingBox(buffer, FS_SMALL).width, 2) + VPSM_RIGHT;
1297 
1298  this->MarkDirty();
1299 }
1300 
1308 {
1309  Rect zoomlevels[ZOOM_LVL_COUNT];
1310 
1311  for (ZoomLevel zoom = ZOOM_LVL_BEGIN; zoom != ZOOM_LVL_END; zoom++) {
1312  /* FIXME: This doesn't switch to width_small when appropriate. */
1313  zoomlevels[zoom].left = this->center - ScaleByZoom(this->width_normal / 2 + 1, zoom);
1314  zoomlevels[zoom].top = this->top - ScaleByZoom(1, zoom);
1315  zoomlevels[zoom].right = this->center + ScaleByZoom(this->width_normal / 2 + 1, zoom);
1316  zoomlevels[zoom].bottom = this->top + ScaleByZoom(VPSM_TOP + FONT_HEIGHT_NORMAL + VPSM_BOTTOM + 1, zoom);
1317  }
1318 
1319  Window *w;
1320  FOR_ALL_WINDOWS_FROM_BACK(w) {
1321  ViewPort *vp = w->viewport;
1322  if (vp != NULL && vp->zoom <= maxzoom) {
1323  assert(vp->width != 0);
1324  Rect &zl = zoomlevels[vp->zoom];
1325  MarkViewportDirty(vp, zl.left, zl.top, zl.right, zl.bottom);
1326  }
1327  }
1328 }
1329 
1330 static void ViewportDrawTileSprites(const TileSpriteToDrawVector *tstdv)
1331 {
1332  const TileSpriteToDraw *tsend = tstdv->End();
1333  for (const TileSpriteToDraw *ts = tstdv->Begin(); ts != tsend; ++ts) {
1334  DrawSpriteViewport(ts->image, ts->pal, ts->x, ts->y, ts->sub);
1335  }
1336 }
1337 
1340 {
1341  return true;
1342 }
1343 
1346 {
1347  ParentSpriteToDraw **psdvend = psdv->End();
1348  ParentSpriteToDraw **psd = psdv->Begin();
1349  while (psd != psdvend) {
1350  ParentSpriteToDraw *ps = *psd;
1351 
1352  if (ps->comparison_done) {
1353  psd++;
1354  continue;
1355  }
1356 
1357  ps->comparison_done = true;
1358 
1359  for (ParentSpriteToDraw **psd2 = psd + 1; psd2 != psdvend; psd2++) {
1360  ParentSpriteToDraw *ps2 = *psd2;
1361 
1362  if (ps2->comparison_done) continue;
1363 
1364  /* Decide which comparator to use, based on whether the bounding
1365  * boxes overlap
1366  */
1367  if (ps->xmax >= ps2->xmin && ps->xmin <= ps2->xmax && // overlap in X?
1368  ps->ymax >= ps2->ymin && ps->ymin <= ps2->ymax && // overlap in Y?
1369  ps->zmax >= ps2->zmin && ps->zmin <= ps2->zmax) { // overlap in Z?
1370  /* Use X+Y+Z as the sorting order, so sprites closer to the bottom of
1371  * the screen and with higher Z elevation, are drawn in front.
1372  * Here X,Y,Z are the coordinates of the "center of mass" of the sprite,
1373  * i.e. X=(left+right)/2, etc.
1374  * However, since we only care about order, don't actually divide / 2
1375  */
1376  if (ps->xmin + ps->xmax + ps->ymin + ps->ymax + ps->zmin + ps->zmax <=
1377  ps2->xmin + ps2->xmax + ps2->ymin + ps2->ymax + ps2->zmin + ps2->zmax) {
1378  continue;
1379  }
1380  } else {
1381  /* We only change the order, if it is definite.
1382  * I.e. every single order of X, Y, Z says ps2 is behind ps or they overlap.
1383  * That is: If one partial order says ps behind ps2, do not change the order.
1384  */
1385  if (ps->xmax < ps2->xmin ||
1386  ps->ymax < ps2->ymin ||
1387  ps->zmax < ps2->zmin) {
1388  continue;
1389  }
1390  }
1391 
1392  /* Move ps2 in front of ps */
1393  ParentSpriteToDraw *temp = ps2;
1394  for (ParentSpriteToDraw **psd3 = psd2; psd3 > psd; psd3--) {
1395  *psd3 = *(psd3 - 1);
1396  }
1397  *psd = temp;
1398  }
1399  }
1400 }
1401 
1402 static void ViewportDrawParentSprites(const ParentSpriteToSortVector *psd, const ChildScreenSpriteToDrawVector *csstdv)
1403 {
1404  const ParentSpriteToDraw * const *psd_end = psd->End();
1405  for (const ParentSpriteToDraw * const *it = psd->Begin(); it != psd_end; it++) {
1406  const ParentSpriteToDraw *ps = *it;
1407  if (ps->image != SPR_EMPTY_BOUNDING_BOX) DrawSpriteViewport(ps->image, ps->pal, ps->x, ps->y, ps->sub);
1408 
1409  int child_idx = ps->first_child;
1410  while (child_idx >= 0) {
1411  const ChildScreenSpriteToDraw *cs = csstdv->Get(child_idx);
1412  child_idx = cs->next;
1413  DrawSpriteViewport(cs->image, cs->pal, ps->left + cs->x, ps->top + cs->y, cs->sub);
1414  }
1415  }
1416 }
1417 
1423 {
1424  const ParentSpriteToDraw * const *psd_end = psd->End();
1425  for (const ParentSpriteToDraw * const *it = psd->Begin(); it != psd_end; it++) {
1426  const ParentSpriteToDraw *ps = *it;
1427  Point pt1 = RemapCoords(ps->xmax + 1, ps->ymax + 1, ps->zmax + 1); // top front corner
1428  Point pt2 = RemapCoords(ps->xmin , ps->ymax + 1, ps->zmax + 1); // top left corner
1429  Point pt3 = RemapCoords(ps->xmax + 1, ps->ymin , ps->zmax + 1); // top right corner
1430  Point pt4 = RemapCoords(ps->xmax + 1, ps->ymax + 1, ps->zmin ); // bottom front corner
1431 
1432  DrawBox( pt1.x, pt1.y,
1433  pt2.x - pt1.x, pt2.y - pt1.y,
1434  pt3.x - pt1.x, pt3.y - pt1.y,
1435  pt4.x - pt1.x, pt4.y - pt1.y);
1436  }
1437 }
1438 
1443 {
1445  const DrawPixelInfo *dpi = _cur_dpi;
1446  void *dst;
1447  int right = UnScaleByZoom(dpi->width, dpi->zoom);
1448  int bottom = UnScaleByZoom(dpi->height, dpi->zoom);
1449 
1450  int colour = _string_colourmap[_dirty_block_colour & 0xF];
1451 
1452  dst = dpi->dst_ptr;
1453 
1454  byte bo = UnScaleByZoom(dpi->left + dpi->top, dpi->zoom) & 1;
1455  do {
1456  for (int i = (bo ^= 1); i < right; i += 2) blitter->SetPixel(dst, i, 0, (uint8)colour);
1457  dst = blitter->MoveTo(dst, 0, 1);
1458  } while (--bottom > 0);
1459 }
1460 
1461 static void ViewportDrawStrings(ZoomLevel zoom, const StringSpriteToDrawVector *sstdv)
1462 {
1463  const StringSpriteToDraw *ssend = sstdv->End();
1464  for (const StringSpriteToDraw *ss = sstdv->Begin(); ss != ssend; ++ss) {
1465  TextColour colour = TC_BLACK;
1466  bool small = HasBit(ss->width, 15);
1467  int w = GB(ss->width, 0, 15);
1468  int x = UnScaleByZoom(ss->x, zoom);
1469  int y = UnScaleByZoom(ss->y, zoom);
1470  int h = VPSM_TOP + (small ? FONT_HEIGHT_SMALL : FONT_HEIGHT_NORMAL) + VPSM_BOTTOM;
1471 
1472  SetDParam(0, ss->params[0]);
1473  SetDParam(1, ss->params[1]);
1474 
1475  if (ss->colour != INVALID_COLOUR) {
1476  /* Do not draw signs nor station names if they are set invisible */
1477  if (IsInvisibilitySet(TO_SIGNS) && ss->string != STR_WHITE_SIGN) continue;
1478 
1479  if (IsTransparencySet(TO_SIGNS) && ss->string != STR_WHITE_SIGN) {
1480  /* Don't draw the rectangle.
1481  * Real colours need the TC_IS_PALETTE_COLOUR flag.
1482  * Otherwise colours from _string_colourmap are assumed. */
1483  colour = (TextColour)_colour_gradient[ss->colour][6] | TC_IS_PALETTE_COLOUR;
1484  } else {
1485  /* Draw the rectangle if 'transparent station signs' is off,
1486  * or if we are drawing a general text sign (STR_WHITE_SIGN). */
1487  DrawFrameRect(
1488  x, y, x + w, y + h, ss->colour,
1490  );
1491  }
1492  }
1493 
1494  DrawString(x + VPSM_LEFT, x + w - 1 - VPSM_RIGHT, y + VPSM_TOP, ss->string, colour, SA_HOR_CENTER);
1495  }
1496 }
1497 
1498 void ViewportDoDraw(const ViewPort *vp, int left, int top, int right, int bottom)
1499 {
1500  DrawPixelInfo *old_dpi = _cur_dpi;
1501  _cur_dpi = &_vd.dpi;
1502 
1503  _vd.dpi.zoom = vp->zoom;
1504  int mask = ScaleByZoom(-1, vp->zoom);
1505 
1507 
1508  _vd.dpi.width = (right - left) & mask;
1509  _vd.dpi.height = (bottom - top) & mask;
1510  _vd.dpi.left = left & mask;
1511  _vd.dpi.top = top & mask;
1512  _vd.dpi.pitch = old_dpi->pitch;
1513  _vd.last_child = NULL;
1514 
1515  int x = UnScaleByZoom(_vd.dpi.left - (vp->virtual_left & mask), vp->zoom) + vp->left;
1516  int y = UnScaleByZoom(_vd.dpi.top - (vp->virtual_top & mask), vp->zoom) + vp->top;
1517 
1518  _vd.dpi.dst_ptr = BlitterFactory::GetCurrentBlitter()->MoveTo(old_dpi->dst_ptr, x - old_dpi->left, y - old_dpi->top);
1519 
1521  ViewportAddVehicles(&_vd.dpi);
1522 
1523  ViewportAddTownNames(&_vd.dpi);
1524  ViewportAddStationNames(&_vd.dpi);
1525  ViewportAddSigns(&_vd.dpi);
1526 
1527  DrawTextEffects(&_vd.dpi);
1528 
1529  if (_vd.tile_sprites_to_draw.Length() != 0) ViewportDrawTileSprites(&_vd.tile_sprites_to_draw);
1530 
1531  ParentSpriteToDraw *psd_end = _vd.parent_sprites_to_draw.End();
1532  for (ParentSpriteToDraw *it = _vd.parent_sprites_to_draw.Begin(); it != psd_end; it++) {
1533  *_vd.parent_sprites_to_sort.Append() = it;
1534  }
1535 
1536  _vp_sprite_sorter(&_vd.parent_sprites_to_sort);
1537  ViewportDrawParentSprites(&_vd.parent_sprites_to_sort, &_vd.child_screen_sprites_to_draw);
1538 
1539  if (_draw_bounding_boxes) ViewportDrawBoundingBoxes(&_vd.parent_sprites_to_sort);
1540  if (_draw_dirty_blocks) ViewportDrawDirtyBlocks();
1541 
1542  DrawPixelInfo dp = _vd.dpi;
1543  ZoomLevel zoom = _vd.dpi.zoom;
1544  dp.zoom = ZOOM_LVL_NORMAL;
1545  dp.width = UnScaleByZoom(dp.width, zoom);
1546  dp.height = UnScaleByZoom(dp.height, zoom);
1547  _cur_dpi = &dp;
1548 
1549  if (vp->overlay != NULL && vp->overlay->GetCargoMask() != 0 && vp->overlay->GetCompanyMask() != 0) {
1550  /* translate to window coordinates */
1551  dp.left = x;
1552  dp.top = y;
1553  vp->overlay->Draw(&dp);
1554  }
1555 
1556  if (_vd.string_sprites_to_draw.Length() != 0) {
1557  /* translate to world coordinates */
1558  dp.left = UnScaleByZoom(_vd.dpi.left, zoom);
1559  dp.top = UnScaleByZoom(_vd.dpi.top, zoom);
1560  ViewportDrawStrings(zoom, &_vd.string_sprites_to_draw);
1561  }
1562 
1563  _cur_dpi = old_dpi;
1564 
1565  _vd.string_sprites_to_draw.Clear();
1566  _vd.tile_sprites_to_draw.Clear();
1567  _vd.parent_sprites_to_draw.Clear();
1569  _vd.child_screen_sprites_to_draw.Clear();
1570 }
1571 
1576 static void ViewportDrawChk(const ViewPort *vp, int left, int top, int right, int bottom)
1577 {
1578  if (ScaleByZoom(bottom - top, vp->zoom) * ScaleByZoom(right - left, vp->zoom) > 180000 * ZOOM_LVL_BASE * ZOOM_LVL_BASE) {
1579  if ((bottom - top) > (right - left)) {
1580  int t = (top + bottom) >> 1;
1581  ViewportDrawChk(vp, left, top, right, t);
1582  ViewportDrawChk(vp, left, t, right, bottom);
1583  } else {
1584  int t = (left + right) >> 1;
1585  ViewportDrawChk(vp, left, top, t, bottom);
1586  ViewportDrawChk(vp, t, top, right, bottom);
1587  }
1588  } else {
1589  ViewportDoDraw(vp,
1590  ScaleByZoom(left - vp->left, vp->zoom) + vp->virtual_left,
1591  ScaleByZoom(top - vp->top, vp->zoom) + vp->virtual_top,
1592  ScaleByZoom(right - vp->left, vp->zoom) + vp->virtual_left,
1593  ScaleByZoom(bottom - vp->top, vp->zoom) + vp->virtual_top
1594  );
1595  }
1596 }
1597 
1598 static inline void ViewportDraw(const ViewPort *vp, int left, int top, int right, int bottom)
1599 {
1600  if (right <= vp->left || bottom <= vp->top) return;
1601 
1602  if (left >= vp->left + vp->width) return;
1603 
1604  if (left < vp->left) left = vp->left;
1605  if (right > vp->left + vp->width) right = vp->left + vp->width;
1606 
1607  if (top >= vp->top + vp->height) return;
1608 
1609  if (top < vp->top) top = vp->top;
1610  if (bottom > vp->top + vp->height) bottom = vp->top + vp->height;
1611 
1612  ViewportDrawChk(vp, left, top, right, bottom);
1613 }
1614 
1619 {
1621 
1622  DrawPixelInfo *dpi = _cur_dpi;
1623 
1624  dpi->left += this->left;
1625  dpi->top += this->top;
1626 
1627  ViewportDraw(this->viewport, dpi->left, dpi->top, dpi->left + dpi->width, dpi->top + dpi->height);
1628 
1629  dpi->left -= this->left;
1630  dpi->top -= this->top;
1631 }
1632 
1643 static inline void ClampViewportToMap(const ViewPort *vp, int *scroll_x, int *scroll_y)
1644 {
1645  /* Centre of the viewport is hot spot. */
1646  Point pt = {
1647  *scroll_x + vp->virtual_width / 2,
1648  *scroll_y + vp->virtual_height / 2
1649  };
1650 
1651  /* Find nearest tile that is within borders of the map. */
1652  bool clamped;
1653  pt = InverseRemapCoords2(pt.x, pt.y, true, &clamped);
1654 
1655  if (clamped) {
1656  /* Convert back to viewport coordinates and remove centering. */
1657  pt = RemapCoords2(pt.x, pt.y);
1658  *scroll_x = pt.x - vp->virtual_width / 2;
1659  *scroll_y = pt.y - vp->virtual_height / 2;
1660  }
1661 }
1662 
1668 {
1669  const ViewPort *vp = w->viewport;
1670 
1672  const Vehicle *veh = Vehicle::Get(w->viewport->follow_vehicle);
1673  Point pt = MapXYZToViewport(vp, veh->x_pos, veh->y_pos, veh->z_pos);
1674 
1675  w->viewport->scrollpos_x = pt.x;
1676  w->viewport->scrollpos_y = pt.y;
1677  SetViewportPosition(w, pt.x, pt.y);
1678  } else {
1679  /* Ensure the destination location is within the map */
1681 
1682  int delta_x = w->viewport->dest_scrollpos_x - w->viewport->scrollpos_x;
1683  int delta_y = w->viewport->dest_scrollpos_y - w->viewport->scrollpos_y;
1684 
1685  bool update_overlay = false;
1686  if (delta_x != 0 || delta_y != 0) {
1688  int max_scroll = ScaleByMapSize1D(512 * ZOOM_LVL_BASE);
1689  /* Not at our desired position yet... */
1690  w->viewport->scrollpos_x += Clamp(DivAwayFromZero(delta_x, 4), -max_scroll, max_scroll);
1691  w->viewport->scrollpos_y += Clamp(DivAwayFromZero(delta_y, 4), -max_scroll, max_scroll);
1692  } else {
1695  }
1696  update_overlay = (w->viewport->scrollpos_x == w->viewport->dest_scrollpos_x &&
1698  }
1699 
1701 
1702  SetViewportPosition(w, w->viewport->scrollpos_x, w->viewport->scrollpos_y);
1703  if (update_overlay) RebuildViewportOverlay(w);
1704  }
1705 }
1706 
1716 static void MarkViewportDirty(const ViewPort *vp, int left, int top, int right, int bottom)
1717 {
1718  /* Rounding wrt. zoom-out level */
1719  right += (1 << vp->zoom) - 1;
1720  bottom += (1 << vp->zoom) - 1;
1721 
1722  right -= vp->virtual_left;
1723  if (right <= 0) return;
1724 
1725  bottom -= vp->virtual_top;
1726  if (bottom <= 0) return;
1727 
1728  left = max(0, left - vp->virtual_left);
1729 
1730  if (left >= vp->virtual_width) return;
1731 
1732  top = max(0, top - vp->virtual_top);
1733 
1734  if (top >= vp->virtual_height) return;
1735 
1737  UnScaleByZoomLower(left, vp->zoom) + vp->left,
1738  UnScaleByZoomLower(top, vp->zoom) + vp->top,
1739  UnScaleByZoom(right, vp->zoom) + vp->left + 1,
1740  UnScaleByZoom(bottom, vp->zoom) + vp->top + 1
1741  );
1742 }
1743 
1752 void MarkAllViewportsDirty(int left, int top, int right, int bottom)
1753 {
1754  Window *w;
1755  FOR_ALL_WINDOWS_FROM_BACK(w) {
1756  ViewPort *vp = w->viewport;
1757  if (vp != NULL) {
1758  assert(vp->width != 0);
1759  MarkViewportDirty(vp, left, top, right, bottom);
1760  }
1761  }
1762 }
1763 
1764 void ConstrainAllViewportsZoom()
1765 {
1766  Window *w;
1767  FOR_ALL_WINDOWS_FROM_FRONT(w) {
1768  if (w->viewport == NULL) continue;
1769 
1771  if (zoom != w->viewport->zoom) {
1772  while (w->viewport->zoom < zoom) DoZoomInOutWindow(ZOOM_OUT, w);
1773  while (w->viewport->zoom > zoom) DoZoomInOutWindow(ZOOM_IN, w);
1774  }
1775  }
1776 }
1777 
1785 void MarkTileDirtyByTile(TileIndex tile, int bridge_level_offset, int tile_height_override)
1786 {
1787  Point pt = RemapCoords(TileX(tile) * TILE_SIZE, TileY(tile) * TILE_SIZE, tile_height_override * TILE_HEIGHT);
1789  pt.x - MAX_TILE_EXTENT_LEFT,
1790  pt.y - MAX_TILE_EXTENT_TOP - ZOOM_LVL_BASE * TILE_HEIGHT * bridge_level_offset,
1791  pt.x + MAX_TILE_EXTENT_RIGHT,
1792  pt.y + MAX_TILE_EXTENT_BOTTOM);
1793 }
1794 
1803 {
1804  int x_size = _thd.size.x;
1805  int y_size = _thd.size.y;
1806 
1807  if (!_thd.diagonal) { // Selecting in a straight rectangle (or a single square)
1808  int x_start = _thd.pos.x;
1809  int y_start = _thd.pos.y;
1810 
1811  if (_thd.outersize.x != 0) {
1812  x_size += _thd.outersize.x;
1813  x_start += _thd.offs.x;
1814  y_size += _thd.outersize.y;
1815  y_start += _thd.offs.y;
1816  }
1817 
1818  x_size -= TILE_SIZE;
1819  y_size -= TILE_SIZE;
1820 
1821  assert(x_size >= 0);
1822  assert(y_size >= 0);
1823 
1824  int x_end = Clamp(x_start + x_size, 0, MapSizeX() * TILE_SIZE - TILE_SIZE);
1825  int y_end = Clamp(y_start + y_size, 0, MapSizeY() * TILE_SIZE - TILE_SIZE);
1826 
1827  x_start = Clamp(x_start, 0, MapSizeX() * TILE_SIZE - TILE_SIZE);
1828  y_start = Clamp(y_start, 0, MapSizeY() * TILE_SIZE - TILE_SIZE);
1829 
1830  /* make sure everything is multiple of TILE_SIZE */
1831  assert((x_end | y_end | x_start | y_start) % TILE_SIZE == 0);
1832 
1833  /* How it works:
1834  * Suppose we have to mark dirty rectangle of 3x4 tiles:
1835  * x
1836  * xxx
1837  * xxxxx
1838  * xxxxx
1839  * xxx
1840  * x
1841  * This algorithm marks dirty columns of tiles, so it is done in 3+4-1 steps:
1842  * 1) x 2) x
1843  * xxx Oxx
1844  * Oxxxx xOxxx
1845  * xxxxx Oxxxx
1846  * xxx xxx
1847  * x x
1848  * And so forth...
1849  */
1850 
1851  int top_x = x_end; // coordinates of top dirty tile
1852  int top_y = y_start;
1853  int bot_x = top_x; // coordinates of bottom dirty tile
1854  int bot_y = top_y;
1855 
1856  do {
1857  /* topmost dirty point */
1858  TileIndex top_tile = TileVirtXY(top_x, top_y);
1859  Point top = RemapCoords(top_x, top_y, GetTileMaxPixelZ(top_tile));
1860 
1861  /* bottommost point */
1862  TileIndex bottom_tile = TileVirtXY(bot_x, bot_y);
1863  Point bot = RemapCoords(bot_x + TILE_SIZE, bot_y + TILE_SIZE, GetTilePixelZ(bottom_tile)); // bottommost point
1864 
1865  /* the 'x' coordinate of 'top' and 'bot' is the same (and always in the same distance from tile middle),
1866  * tile height/slope affects only the 'y' on-screen coordinate! */
1867 
1868  int l = top.x - TILE_PIXELS * ZOOM_LVL_BASE; // 'x' coordinate of left side of the dirty rectangle
1869  int t = top.y; // 'y' coordinate of top side of the dirty rectangle
1870  int r = top.x + TILE_PIXELS * ZOOM_LVL_BASE; // 'x' coordinate of right side of the dirty rectangle
1871  int b = bot.y; // 'y' coordinate of bottom side of the dirty rectangle
1872 
1873  static const int OVERLAY_WIDTH = 4 * ZOOM_LVL_BASE; // part of selection sprites is drawn outside the selected area (in particular: terraforming)
1874 
1875  /* For halftile foundations on SLOPE_STEEP_S the sprite extents some more towards the top */
1876  MarkAllViewportsDirty(l - OVERLAY_WIDTH, t - OVERLAY_WIDTH - TILE_HEIGHT * ZOOM_LVL_BASE, r + OVERLAY_WIDTH, b + OVERLAY_WIDTH);
1877 
1878  /* haven't we reached the topmost tile yet? */
1879  if (top_x != x_start) {
1880  top_x -= TILE_SIZE;
1881  } else {
1882  top_y += TILE_SIZE;
1883  }
1884 
1885  /* the way the bottom tile changes is different when we reach the bottommost tile */
1886  if (bot_y != y_end) {
1887  bot_y += TILE_SIZE;
1888  } else {
1889  bot_x -= TILE_SIZE;
1890  }
1891  } while (bot_x >= top_x);
1892  } else { // Selecting in a 45 degrees rotated (diagonal) rectangle.
1893  /* a_size, b_size describe a rectangle with rotated coordinates */
1894  int a_size = x_size + y_size, b_size = x_size - y_size;
1895 
1896  int interval_a = a_size < 0 ? -(int)TILE_SIZE : (int)TILE_SIZE;
1897  int interval_b = b_size < 0 ? -(int)TILE_SIZE : (int)TILE_SIZE;
1898 
1899  for (int a = -interval_a; a != a_size + interval_a; a += interval_a) {
1900  for (int b = -interval_b; b != b_size + interval_b; b += interval_b) {
1901  uint x = (_thd.pos.x + (a + b) / 2) / TILE_SIZE;
1902  uint y = (_thd.pos.y + (a - b) / 2) / TILE_SIZE;
1903 
1904  if (x < MapMaxX() && y < MapMaxY()) {
1905  MarkTileDirtyByTile(TileXY(x, y));
1906  }
1907  }
1908  }
1909  }
1910 }
1911 
1912 
1913 void SetSelectionRed(bool b)
1914 {
1915  _thd.make_square_red = b;
1917 }
1918 
1927 static bool CheckClickOnViewportSign(const ViewPort *vp, int x, int y, const ViewportSign *sign)
1928 {
1929  bool small = (vp->zoom >= ZOOM_LVL_OUT_16X);
1930  int sign_half_width = ScaleByZoom((small ? sign->width_small : sign->width_normal) / 2, vp->zoom);
1931  int sign_height = ScaleByZoom(VPSM_TOP + (small ? FONT_HEIGHT_SMALL : FONT_HEIGHT_NORMAL) + VPSM_BOTTOM, vp->zoom);
1932 
1933  x = ScaleByZoom(x - vp->left, vp->zoom) + vp->virtual_left;
1934  y = ScaleByZoom(y - vp->top, vp->zoom) + vp->virtual_top;
1935 
1936  return y >= sign->top && y < sign->top + sign_height &&
1937  x >= sign->center - sign_half_width && x < sign->center + sign_half_width;
1938 }
1939 
1940 static bool CheckClickOnTown(const ViewPort *vp, int x, int y)
1941 {
1942  if (!HasBit(_display_opt, DO_SHOW_TOWN_NAMES)) return false;
1943 
1944  const Town *t;
1945  FOR_ALL_TOWNS(t) {
1946  if (CheckClickOnViewportSign(vp, x, y, &t->cache.sign)) {
1947  ShowTownViewWindow(t->index);
1948  return true;
1949  }
1950  }
1951 
1952  return false;
1953 }
1954 
1955 static bool CheckClickOnStation(const ViewPort *vp, int x, int y)
1956 {
1958 
1959  const BaseStation *st;
1960  FOR_ALL_BASE_STATIONS(st) {
1961  /* Check whether the base station is a station or a waypoint */
1962  bool is_station = Station::IsExpected(st);
1963 
1964  /* Don't check if the display options are disabled */
1965  if (!HasBit(_display_opt, is_station ? DO_SHOW_STATION_NAMES : DO_SHOW_WAYPOINT_NAMES)) continue;
1966 
1967  /* Don't check if competitor signs are not shown and the sign isn't owned by the local company */
1968  if (!HasBit(_display_opt, DO_SHOW_COMPETITOR_SIGNS) && _local_company != st->owner && st->owner != OWNER_NONE) continue;
1969 
1970  if (CheckClickOnViewportSign(vp, x, y, &st->sign)) {
1971  if (is_station) {
1973  } else {
1975  }
1976  return true;
1977  }
1978  }
1979 
1980  return false;
1981 }
1982 
1983 
1984 static bool CheckClickOnSign(const ViewPort *vp, int x, int y)
1985 {
1986  /* Signs are turned off, or they are transparent and invisibility is ON, or company is a spectator */
1988 
1989  const Sign *si;
1990  FOR_ALL_SIGNS(si) {
1991  /* If competitor signs are hidden, don't check signs that aren't owned by local company */
1992  if (!HasBit(_display_opt, DO_SHOW_COMPETITOR_SIGNS) && _local_company != si->owner && si->owner != OWNER_DEITY) continue;
1993  if (si->owner == OWNER_DEITY && _game_mode != GM_EDITOR) continue;
1994 
1995  if (CheckClickOnViewportSign(vp, x, y, &si->sign)) {
1996  HandleClickOnSign(si);
1997  return true;
1998  }
1999  }
2000 
2001  return false;
2002 }
2003 
2004 
2005 static bool CheckClickOnLandscape(const ViewPort *vp, int x, int y)
2006 {
2007  Point pt = TranslateXYToTileCoord(vp, x, y);
2008 
2009  if (pt.x != -1) return ClickTile(TileVirtXY(pt.x, pt.y));
2010  return true;
2011 }
2012 
2013 static void PlaceObject()
2014 {
2015  Point pt;
2016  Window *w;
2017 
2018  pt = GetTileBelowCursor();
2019  if (pt.x == -1) return;
2020 
2021  if ((_thd.place_mode & HT_DRAG_MASK) == HT_POINT) {
2022  pt.x += TILE_SIZE / 2;
2023  pt.y += TILE_SIZE / 2;
2024  }
2025 
2026  _tile_fract_coords.x = pt.x & TILE_UNIT_MASK;
2027  _tile_fract_coords.y = pt.y & TILE_UNIT_MASK;
2028 
2029  w = _thd.GetCallbackWnd();
2030  if (w != NULL) w->OnPlaceObject(pt, TileVirtXY(pt.x, pt.y));
2031 }
2032 
2033 
2034 bool HandleViewportClicked(const ViewPort *vp, int x, int y)
2035 {
2036  const Vehicle *v = CheckClickOnVehicle(vp, x, y);
2037 
2038  if (_thd.place_mode & HT_VEHICLE) {
2039  if (v != NULL && VehicleClicked(v)) return true;
2040  }
2041 
2042  /* Vehicle placement mode already handled above. */
2043  if ((_thd.place_mode & HT_DRAG_MASK) != HT_NONE) {
2044  PlaceObject();
2045  return true;
2046  }
2047 
2048  if (CheckClickOnTown(vp, x, y)) return true;
2049  if (CheckClickOnStation(vp, x, y)) return true;
2050  if (CheckClickOnSign(vp, x, y)) return true;
2051  bool result = CheckClickOnLandscape(vp, x, y);
2052 
2053  if (v != NULL) {
2054  DEBUG(misc, 2, "Vehicle %d (index %d) at %p", v->unitnumber, v->index, v);
2056  v = v->First();
2057  if (_ctrl_pressed && v->owner == _local_company) {
2058  StartStopVehicle(v, true);
2059  } else {
2061  }
2062  }
2063  return true;
2064  }
2065  return result;
2066 }
2067 
2068 void RebuildViewportOverlay(Window *w)
2069 {
2070  if (w->viewport->overlay != NULL &&
2071  w->viewport->overlay->GetCompanyMask() != 0 &&
2072  w->viewport->overlay->GetCargoMask() != 0) {
2073  w->viewport->overlay->SetDirty();
2074  w->SetDirty();
2075  }
2076 }
2077 
2087 bool ScrollWindowTo(int x, int y, int z, Window *w, bool instant)
2088 {
2089  /* The slope cannot be acquired outside of the map, so make sure we are always within the map. */
2090  if (z == -1) {
2091  if ( x >= 0 && x <= (int)MapSizeX() * (int)TILE_SIZE - 1
2092  && y >= 0 && y <= (int)MapSizeY() * (int)TILE_SIZE - 1) {
2093  z = GetSlopePixelZ(x, y);
2094  } else {
2095  z = TileHeightOutsideMap(x / (int)TILE_SIZE, y / (int)TILE_SIZE);
2096  }
2097  }
2098 
2099  Point pt = MapXYZToViewport(w->viewport, x, y, z);
2101 
2102  if (w->viewport->dest_scrollpos_x == pt.x && w->viewport->dest_scrollpos_y == pt.y) return false;
2103 
2104  if (instant) {
2105  w->viewport->scrollpos_x = pt.x;
2106  w->viewport->scrollpos_y = pt.y;
2107  RebuildViewportOverlay(w);
2108  }
2109 
2110  w->viewport->dest_scrollpos_x = pt.x;
2111  w->viewport->dest_scrollpos_y = pt.y;
2112  return true;
2113 }
2114 
2122 bool ScrollWindowToTile(TileIndex tile, Window *w, bool instant)
2123 {
2124  return ScrollWindowTo(TileX(tile) * TILE_SIZE, TileY(tile) * TILE_SIZE, -1, w, instant);
2125 }
2126 
2133 bool ScrollMainWindowToTile(TileIndex tile, bool instant)
2134 {
2135  return ScrollMainWindowTo(TileX(tile) * TILE_SIZE + TILE_SIZE / 2, TileY(tile) * TILE_SIZE + TILE_SIZE / 2, -1, instant);
2136 }
2137 
2143 {
2144  TileIndex old;
2145 
2146  old = _thd.redsq;
2147  _thd.redsq = tile;
2148 
2149  if (tile != old) {
2150  if (tile != INVALID_TILE) MarkTileDirtyByTile(tile);
2151  if (old != INVALID_TILE) MarkTileDirtyByTile(old);
2152  }
2153 }
2154 
2160 void SetTileSelectSize(int w, int h)
2161 {
2162  _thd.new_size.x = w * TILE_SIZE;
2163  _thd.new_size.y = h * TILE_SIZE;
2164  _thd.new_outersize.x = 0;
2165  _thd.new_outersize.y = 0;
2166 }
2167 
2168 void SetTileSelectBigSize(int ox, int oy, int sx, int sy)
2169 {
2170  _thd.offs.x = ox * TILE_SIZE;
2171  _thd.offs.y = oy * TILE_SIZE;
2172  _thd.new_outersize.x = sx * TILE_SIZE;
2173  _thd.new_outersize.y = sy * TILE_SIZE;
2174 }
2175 
2177 static HighLightStyle GetAutorailHT(int x, int y)
2178 {
2179  return HT_RAIL | _autorail_piece[x & TILE_UNIT_MASK][y & TILE_UNIT_MASK];
2180 }
2181 
2186 {
2187  this->pos.x = 0;
2188  this->pos.y = 0;
2189  this->new_pos.x = 0;
2190  this->new_pos.y = 0;
2191 }
2192 
2198 {
2199  return (this->place_mode & HT_DIAGONAL) != 0 && _ctrl_pressed && _left_button_down;
2200 }
2201 
2207 {
2208  return FindWindowById(this->window_class, this->window_number);
2209 }
2210 
2211 
2212 
2221 {
2222  int x1;
2223  int y1;
2224 
2225  HighLightStyle new_drawstyle = HT_NONE;
2226  bool new_diagonal = false;
2227 
2228  if ((_thd.place_mode & HT_DRAG_MASK) == HT_SPECIAL) {
2229  x1 = _thd.selend.x;
2230  y1 = _thd.selend.y;
2231  if (x1 != -1) {
2232  int x2 = _thd.selstart.x & ~TILE_UNIT_MASK;
2233  int y2 = _thd.selstart.y & ~TILE_UNIT_MASK;
2234  x1 &= ~TILE_UNIT_MASK;
2235  y1 &= ~TILE_UNIT_MASK;
2236 
2237  if (_thd.IsDraggingDiagonal()) {
2238  new_diagonal = true;
2239  } else {
2240  if (x1 >= x2) Swap(x1, x2);
2241  if (y1 >= y2) Swap(y1, y2);
2242  }
2243  _thd.new_pos.x = x1;
2244  _thd.new_pos.y = y1;
2245  _thd.new_size.x = x2 - x1;
2246  _thd.new_size.y = y2 - y1;
2247  if (!new_diagonal) {
2248  _thd.new_size.x += TILE_SIZE;
2249  _thd.new_size.y += TILE_SIZE;
2250  }
2251  new_drawstyle = _thd.next_drawstyle;
2252  }
2253  } else if ((_thd.place_mode & HT_DRAG_MASK) != HT_NONE) {
2254  Point pt = GetTileBelowCursor();
2255  x1 = pt.x;
2256  y1 = pt.y;
2257  if (x1 != -1) {
2258  switch (_thd.place_mode & HT_DRAG_MASK) {
2259  case HT_RECT:
2260  new_drawstyle = HT_RECT;
2261  break;
2262  case HT_POINT:
2263  new_drawstyle = HT_POINT;
2264  x1 += TILE_SIZE / 2;
2265  y1 += TILE_SIZE / 2;
2266  break;
2267  case HT_RAIL:
2268  /* Draw one highlighted tile in any direction */
2269  new_drawstyle = GetAutorailHT(pt.x, pt.y);
2270  break;
2271  case HT_LINE:
2272  switch (_thd.place_mode & HT_DIR_MASK) {
2273  case HT_DIR_X: new_drawstyle = HT_LINE | HT_DIR_X; break;
2274  case HT_DIR_Y: new_drawstyle = HT_LINE | HT_DIR_Y; break;
2275 
2276  case HT_DIR_HU:
2277  case HT_DIR_HL:
2278  new_drawstyle = (pt.x & TILE_UNIT_MASK) + (pt.y & TILE_UNIT_MASK) <= TILE_SIZE ? HT_LINE | HT_DIR_HU : HT_LINE | HT_DIR_HL;
2279  break;
2280 
2281  case HT_DIR_VL:
2282  case HT_DIR_VR:
2283  new_drawstyle = (pt.x & TILE_UNIT_MASK) > (pt.y & TILE_UNIT_MASK) ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR;
2284  break;
2285 
2286  default: NOT_REACHED();
2287  }
2288  _thd.selstart.x = x1 & ~TILE_UNIT_MASK;
2289  _thd.selstart.y = y1 & ~TILE_UNIT_MASK;
2290  break;
2291  default:
2292  NOT_REACHED();
2293  }
2294  _thd.new_pos.x = x1 & ~TILE_UNIT_MASK;
2295  _thd.new_pos.y = y1 & ~TILE_UNIT_MASK;
2296  }
2297  }
2298 
2299  /* redraw selection */
2300  if (_thd.drawstyle != new_drawstyle ||
2301  _thd.pos.x != _thd.new_pos.x || _thd.pos.y != _thd.new_pos.y ||
2302  _thd.size.x != _thd.new_size.x || _thd.size.y != _thd.new_size.y ||
2303  _thd.outersize.x != _thd.new_outersize.x ||
2304  _thd.outersize.y != _thd.new_outersize.y ||
2305  _thd.diagonal != new_diagonal) {
2306  /* Clear the old tile selection? */
2308 
2309  _thd.drawstyle = new_drawstyle;
2310  _thd.pos = _thd.new_pos;
2311  _thd.size = _thd.new_size;
2312  _thd.outersize = _thd.new_outersize;
2313  _thd.diagonal = new_diagonal;
2314  _thd.dirty = 0xff;
2315 
2316  /* Draw the new tile selection? */
2317  if ((new_drawstyle & HT_DRAG_MASK) != HT_NONE) SetSelectionTilesDirty();
2318  }
2319 }
2320 
2328 static inline void ShowMeasurementTooltips(StringID str, uint paramcount, const uint64 params[], TooltipCloseCondition close_cond = TCC_NONE)
2329 {
2330  if (!_settings_client.gui.measure_tooltip) return;
2331  GuiShowTooltips(_thd.GetCallbackWnd(), str, paramcount, params, close_cond);
2332 }
2333 
2334 static void HideMeasurementTooltips()
2335 {
2337 }
2338 
2341 {
2342  _thd.select_method = method;
2343  _thd.select_proc = process;
2344  _thd.selend.x = TileX(tile) * TILE_SIZE;
2345  _thd.selstart.x = TileX(tile) * TILE_SIZE;
2346  _thd.selend.y = TileY(tile) * TILE_SIZE;
2347  _thd.selstart.y = TileY(tile) * TILE_SIZE;
2348 
2349  /* Needed so several things (road, autoroad, bridges, ...) are placed correctly.
2350  * In effect, placement starts from the centre of a tile
2351  */
2352  if (method == VPM_X_OR_Y || method == VPM_FIX_X || method == VPM_FIX_Y) {
2353  _thd.selend.x += TILE_SIZE / 2;
2354  _thd.selend.y += TILE_SIZE / 2;
2355  _thd.selstart.x += TILE_SIZE / 2;
2356  _thd.selstart.y += TILE_SIZE / 2;
2357  }
2358 
2359  HighLightStyle others = _thd.place_mode & ~(HT_DRAG_MASK | HT_DIR_MASK);
2360  if ((_thd.place_mode & HT_DRAG_MASK) == HT_RECT) {
2361  _thd.place_mode = HT_SPECIAL | others;
2362  _thd.next_drawstyle = HT_RECT | others;
2363  } else if (_thd.place_mode & (HT_RAIL | HT_LINE)) {
2364  _thd.place_mode = HT_SPECIAL | others;
2365  _thd.next_drawstyle = _thd.drawstyle | others;
2366  } else {
2367  _thd.place_mode = HT_SPECIAL | others;
2368  _thd.next_drawstyle = HT_POINT | others;
2369  }
2371 }
2372 
2373 void VpSetPlaceSizingLimit(int limit)
2374 {
2375  _thd.sizelimit = limit;
2376 }
2377 
2384 {
2385  uint64 distance = DistanceManhattan(from, to) + 1;
2386 
2387  _thd.selend.x = TileX(to) * TILE_SIZE;
2388  _thd.selend.y = TileY(to) * TILE_SIZE;
2389  _thd.selstart.x = TileX(from) * TILE_SIZE;
2390  _thd.selstart.y = TileY(from) * TILE_SIZE;
2391  _thd.next_drawstyle = HT_RECT;
2392 
2393  /* show measurement only if there is any length to speak of */
2394  if (distance > 1) {
2395  ShowMeasurementTooltips(STR_MEASURE_LENGTH, 1, &distance);
2396  } else {
2397  HideMeasurementTooltips();
2398  }
2399 }
2400 
2401 static void VpStartPreSizing()
2402 {
2403  _thd.selend.x = -1;
2405 }
2406 
2412 {
2413  int fxpy = _tile_fract_coords.x + _tile_fract_coords.y;
2414  int sxpy = (_thd.selend.x & TILE_UNIT_MASK) + (_thd.selend.y & TILE_UNIT_MASK);
2415  int fxmy = _tile_fract_coords.x - _tile_fract_coords.y;
2416  int sxmy = (_thd.selend.x & TILE_UNIT_MASK) - (_thd.selend.y & TILE_UNIT_MASK);
2417 
2418  switch (mode) {
2419  default: NOT_REACHED();
2420  case 0: // end piece is lower right
2421  if (fxpy >= 20 && sxpy <= 12) return HT_DIR_HL;
2422  if (fxmy < -3 && sxmy > 3) return HT_DIR_VR;
2423  return HT_DIR_Y;
2424 
2425  case 1:
2426  if (fxmy > 3 && sxmy < -3) return HT_DIR_VL;
2427  if (fxpy <= 12 && sxpy >= 20) return HT_DIR_HU;
2428  return HT_DIR_Y;
2429 
2430  case 2:
2431  if (fxmy > 3 && sxmy < -3) return HT_DIR_VL;
2432  if (fxpy >= 20 && sxpy <= 12) return HT_DIR_HL;
2433  return HT_DIR_X;
2434 
2435  case 3:
2436  if (fxmy < -3 && sxmy > 3) return HT_DIR_VR;
2437  if (fxpy <= 12 && sxpy >= 20) return HT_DIR_HU;
2438  return HT_DIR_X;
2439  }
2440 }
2441 
2455 static bool SwapDirection(HighLightStyle style, TileIndex start_tile, TileIndex end_tile)
2456 {
2457  uint start_x = TileX(start_tile);
2458  uint start_y = TileY(start_tile);
2459  uint end_x = TileX(end_tile);
2460  uint end_y = TileY(end_tile);
2461 
2462  switch (style & HT_DRAG_MASK) {
2463  case HT_RAIL:
2464  case HT_LINE: return (end_x > start_x || (end_x == start_x && end_y > start_y));
2465 
2466  case HT_RECT:
2467  case HT_POINT: return (end_x != start_x && end_y < start_y);
2468  default: NOT_REACHED();
2469  }
2470 
2471  return false;
2472 }
2473 
2489 static int CalcHeightdiff(HighLightStyle style, uint distance, TileIndex start_tile, TileIndex end_tile)
2490 {
2491  bool swap = SwapDirection(style, start_tile, end_tile);
2492  uint h0, h1; // Start height and end height.
2493 
2494  if (start_tile == end_tile) return 0;
2495  if (swap) Swap(start_tile, end_tile);
2496 
2497  switch (style & HT_DRAG_MASK) {
2498  case HT_RECT: {
2499  static const TileIndexDiffC heightdiff_area_by_dir[] = {
2500  /* Start */ {1, 0}, /* Dragging east */ {0, 0}, // Dragging south
2501  /* End */ {0, 1}, /* Dragging east */ {1, 1} // Dragging south
2502  };
2503 
2504  /* In the case of an area we can determine whether we were dragging south or
2505  * east by checking the X-coordinates of the tiles */
2506  byte style_t = (byte)(TileX(end_tile) > TileX(start_tile));
2507  start_tile = TILE_ADD(start_tile, ToTileIndexDiff(heightdiff_area_by_dir[style_t]));
2508  end_tile = TILE_ADD(end_tile, ToTileIndexDiff(heightdiff_area_by_dir[2 + style_t]));
2509  FALLTHROUGH;
2510  }
2511 
2512  case HT_POINT:
2513  h0 = TileHeight(start_tile);
2514  h1 = TileHeight(end_tile);
2515  break;
2516  default: { // All other types, this is mostly only line/autorail
2517  static const HighLightStyle flip_style_direction[] = {
2519  };
2520  static const TileIndexDiffC heightdiff_line_by_dir[] = {
2521  /* Start */ {1, 0}, {1, 1}, /* HT_DIR_X */ {0, 1}, {1, 1}, // HT_DIR_Y
2522  /* Start */ {1, 0}, {0, 0}, /* HT_DIR_HU */ {1, 0}, {1, 1}, // HT_DIR_HL
2523  /* Start */ {1, 0}, {1, 1}, /* HT_DIR_VL */ {0, 1}, {1, 1}, // HT_DIR_VR
2524 
2525  /* Start */ {0, 1}, {0, 0}, /* HT_DIR_X */ {1, 0}, {0, 0}, // HT_DIR_Y
2526  /* End */ {0, 1}, {0, 0}, /* HT_DIR_HU */ {1, 1}, {0, 1}, // HT_DIR_HL
2527  /* End */ {1, 0}, {0, 0}, /* HT_DIR_VL */ {0, 0}, {0, 1}, // HT_DIR_VR
2528  };
2529 
2530  distance %= 2; // we're only interested if the distance is even or uneven
2531  style &= HT_DIR_MASK;
2532 
2533  /* To handle autorail, we do some magic to be able to use a lookup table.
2534  * Firstly if we drag the other way around, we switch start&end, and if needed
2535  * also flip the drag-position. Eg if it was on the left, and the distance is even
2536  * that means the end, which is now the start is on the right */
2537  if (swap && distance == 0) style = flip_style_direction[style];
2538 
2539  /* Use lookup table for start-tile based on HighLightStyle direction */
2540  byte style_t = style * 2;
2541  assert(style_t < lengthof(heightdiff_line_by_dir) - 13);
2542  h0 = TileHeight(TILE_ADD(start_tile, ToTileIndexDiff(heightdiff_line_by_dir[style_t])));
2543  uint ht = TileHeight(TILE_ADD(start_tile, ToTileIndexDiff(heightdiff_line_by_dir[style_t + 1])));
2544  h0 = max(h0, ht);
2545 
2546  /* Use lookup table for end-tile based on HighLightStyle direction
2547  * flip around side (lower/upper, left/right) based on distance */
2548  if (distance == 0) style_t = flip_style_direction[style] * 2;
2549  assert(style_t < lengthof(heightdiff_line_by_dir) - 13);
2550  h1 = TileHeight(TILE_ADD(end_tile, ToTileIndexDiff(heightdiff_line_by_dir[12 + style_t])));
2551  ht = TileHeight(TILE_ADD(end_tile, ToTileIndexDiff(heightdiff_line_by_dir[12 + style_t + 1])));
2552  h1 = max(h1, ht);
2553  break;
2554  }
2555  }
2556 
2557  if (swap) Swap(h0, h1);
2558  return (int)(h1 - h0) * TILE_HEIGHT_STEP;
2559 }
2560 
2561 static const StringID measure_strings_length[] = {STR_NULL, STR_MEASURE_LENGTH, STR_MEASURE_LENGTH_HEIGHTDIFF};
2562 
2569 static void CheckUnderflow(int &test, int &other, int mult)
2570 {
2571  if (test >= 0) return;
2572 
2573  other += mult * test;
2574  test = 0;
2575 }
2576 
2584 static void CheckOverflow(int &test, int &other, int max, int mult)
2585 {
2586  if (test <= max) return;
2587 
2588  other += mult * (test - max);
2589  test = max;
2590 }
2591 
2593 static void CalcRaildirsDrawstyle(int x, int y, int method)
2594 {
2595  HighLightStyle b;
2596 
2597  int dx = _thd.selstart.x - (_thd.selend.x & ~TILE_UNIT_MASK);
2598  int dy = _thd.selstart.y - (_thd.selend.y & ~TILE_UNIT_MASK);
2599  uint w = abs(dx) + TILE_SIZE;
2600  uint h = abs(dy) + TILE_SIZE;
2601 
2602  if (method & ~(VPM_RAILDIRS | VPM_SIGNALDIRS)) {
2603  /* We 'force' a selection direction; first four rail buttons. */
2604  method &= ~(VPM_RAILDIRS | VPM_SIGNALDIRS);
2605  int raw_dx = _thd.selstart.x - _thd.selend.x;
2606  int raw_dy = _thd.selstart.y - _thd.selend.y;
2607  switch (method) {
2608  case VPM_FIX_X:
2609  b = HT_LINE | HT_DIR_Y;
2610  x = _thd.selstart.x;
2611  break;
2612 
2613  case VPM_FIX_Y:
2614  b = HT_LINE | HT_DIR_X;
2615  y = _thd.selstart.y;
2616  break;
2617 
2618  case VPM_FIX_HORIZONTAL:
2619  if (dx == -dy) {
2620  /* We are on a straight horizontal line. Determine the 'rail'
2621  * to build based the sub tile location. */
2623  } else {
2624  /* We are not on a straight line. Determine the rail to build
2625  * based on whether we are above or below it. */
2626  b = dx + dy >= (int)TILE_SIZE ? HT_LINE | HT_DIR_HU : HT_LINE | HT_DIR_HL;
2627 
2628  /* Calculate where a horizontal line through the start point and
2629  * a vertical line from the selected end point intersect and
2630  * use that point as the end point. */
2631  int offset = (raw_dx - raw_dy) / 2;
2632  x = _thd.selstart.x - (offset & ~TILE_UNIT_MASK);
2633  y = _thd.selstart.y + (offset & ~TILE_UNIT_MASK);
2634 
2635  /* 'Build' the last half rail tile if needed */
2636  if ((offset & TILE_UNIT_MASK) > (TILE_SIZE / 2)) {
2637  if (dx + dy >= (int)TILE_SIZE) {
2638  x += (dx + dy < 0) ? (int)TILE_SIZE : -(int)TILE_SIZE;
2639  } else {
2640  y += (dx + dy < 0) ? (int)TILE_SIZE : -(int)TILE_SIZE;
2641  }
2642  }
2643 
2644  /* Make sure we do not overflow the map! */
2645  CheckUnderflow(x, y, 1);
2646  CheckUnderflow(y, x, 1);
2647  CheckOverflow(x, y, (MapMaxX() - 1) * TILE_SIZE, 1);
2648  CheckOverflow(y, x, (MapMaxY() - 1) * TILE_SIZE, 1);
2649  assert(x >= 0 && y >= 0 && x <= (int)(MapMaxX() * TILE_SIZE) && y <= (int)(MapMaxY() * TILE_SIZE));
2650  }
2651  break;
2652 
2653  case VPM_FIX_VERTICAL:
2654  if (dx == dy) {
2655  /* We are on a straight vertical line. Determine the 'rail'
2656  * to build based the sub tile location. */
2657  b = (x & TILE_UNIT_MASK) > (y & TILE_UNIT_MASK) ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR;
2658  } else {
2659  /* We are not on a straight line. Determine the rail to build
2660  * based on whether we are left or right from it. */
2661  b = dx < dy ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR;
2662 
2663  /* Calculate where a vertical line through the start point and
2664  * a horizontal line from the selected end point intersect and
2665  * use that point as the end point. */
2666  int offset = (raw_dx + raw_dy + (int)TILE_SIZE) / 2;
2667  x = _thd.selstart.x - (offset & ~TILE_UNIT_MASK);
2668  y = _thd.selstart.y - (offset & ~TILE_UNIT_MASK);
2669 
2670  /* 'Build' the last half rail tile if needed */
2671  if ((offset & TILE_UNIT_MASK) > (TILE_SIZE / 2)) {
2672  if (dx - dy < 0) {
2673  y += (dx > dy) ? (int)TILE_SIZE : -(int)TILE_SIZE;
2674  } else {
2675  x += (dx < dy) ? (int)TILE_SIZE : -(int)TILE_SIZE;
2676  }
2677  }
2678 
2679  /* Make sure we do not overflow the map! */
2680  CheckUnderflow(x, y, -1);
2681  CheckUnderflow(y, x, -1);
2682  CheckOverflow(x, y, (MapMaxX() - 1) * TILE_SIZE, -1);
2683  CheckOverflow(y, x, (MapMaxY() - 1) * TILE_SIZE, -1);
2684  assert(x >= 0 && y >= 0 && x <= (int)(MapMaxX() * TILE_SIZE) && y <= (int)(MapMaxY() * TILE_SIZE));
2685  }
2686  break;
2687 
2688  default:
2689  NOT_REACHED();
2690  }
2691  } else if (TileVirtXY(_thd.selstart.x, _thd.selstart.y) == TileVirtXY(x, y)) { // check if we're only within one tile
2692  if (method & VPM_RAILDIRS) {
2693  b = GetAutorailHT(x, y);
2694  } else { // rect for autosignals on one tile
2695  b = HT_RECT;
2696  }
2697  } else if (h == TILE_SIZE) { // Is this in X direction?
2698  if (dx == (int)TILE_SIZE) { // 2x1 special handling
2699  b = (Check2x1AutoRail(3)) | HT_LINE;
2700  } else if (dx == -(int)TILE_SIZE) {
2701  b = (Check2x1AutoRail(2)) | HT_LINE;
2702  } else {
2703  b = HT_LINE | HT_DIR_X;
2704  }
2705  y = _thd.selstart.y;
2706  } else if (w == TILE_SIZE) { // Or Y direction?
2707  if (dy == (int)TILE_SIZE) { // 2x1 special handling
2708  b = (Check2x1AutoRail(1)) | HT_LINE;
2709  } else if (dy == -(int)TILE_SIZE) { // 2x1 other direction
2710  b = (Check2x1AutoRail(0)) | HT_LINE;
2711  } else {
2712  b = HT_LINE | HT_DIR_Y;
2713  }
2714  x = _thd.selstart.x;
2715  } else if (w > h * 2) { // still count as x dir?
2716  b = HT_LINE | HT_DIR_X;
2717  y = _thd.selstart.y;
2718  } else if (h > w * 2) { // still count as y dir?
2719  b = HT_LINE | HT_DIR_Y;
2720  x = _thd.selstart.x;
2721  } else { // complicated direction
2722  int d = w - h;
2723  _thd.selend.x = _thd.selend.x & ~TILE_UNIT_MASK;
2724  _thd.selend.y = _thd.selend.y & ~TILE_UNIT_MASK;
2725 
2726  /* four cases. */
2727  if (x > _thd.selstart.x) {
2728  if (y > _thd.selstart.y) {
2729  /* south */
2730  if (d == 0) {
2731  b = (x & TILE_UNIT_MASK) > (y & TILE_UNIT_MASK) ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR;
2732  } else if (d >= 0) {
2733  x = _thd.selstart.x + h;
2734  b = HT_LINE | HT_DIR_VL;
2735  } else {
2736  y = _thd.selstart.y + w;
2737  b = HT_LINE | HT_DIR_VR;
2738  }
2739  } else {
2740  /* west */
2741  if (d == 0) {
2743  } else if (d >= 0) {
2744  x = _thd.selstart.x + h;
2745  b = HT_LINE | HT_DIR_HL;
2746  } else {
2747  y = _thd.selstart.y - w;
2748  b = HT_LINE | HT_DIR_HU;
2749  }
2750  }
2751  } else {
2752  if (y > _thd.selstart.y) {
2753  /* east */
2754  if (d == 0) {
2756  } else if (d >= 0) {
2757  x = _thd.selstart.x - h;
2758  b = HT_LINE | HT_DIR_HU;
2759  } else {
2760  y = _thd.selstart.y + w;
2761  b = HT_LINE | HT_DIR_HL;
2762  }
2763  } else {
2764  /* north */
2765  if (d == 0) {
2766  b = (x & TILE_UNIT_MASK) > (y & TILE_UNIT_MASK) ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR;
2767  } else if (d >= 0) {
2768  x = _thd.selstart.x - h;
2769  b = HT_LINE | HT_DIR_VR;
2770  } else {
2771  y = _thd.selstart.y - w;
2772  b = HT_LINE | HT_DIR_VL;
2773  }
2774  }
2775  }
2776  }
2777 
2779  TileIndex t0 = TileVirtXY(_thd.selstart.x, _thd.selstart.y);
2780  TileIndex t1 = TileVirtXY(x, y);
2781  uint distance = DistanceManhattan(t0, t1) + 1;
2782  byte index = 0;
2783  uint64 params[2];
2784 
2785  if (distance != 1) {
2786  int heightdiff = CalcHeightdiff(b, distance, t0, t1);
2787  /* If we are showing a tooltip for horizontal or vertical drags,
2788  * 2 tiles have a length of 1. To bias towards the ceiling we add
2789  * one before division. It feels more natural to count 3 lengths as 2 */
2790  if ((b & HT_DIR_MASK) != HT_DIR_X && (b & HT_DIR_MASK) != HT_DIR_Y) {
2791  distance = CeilDiv(distance, 2);
2792  }
2793 
2794  params[index++] = distance;
2795  if (heightdiff != 0) params[index++] = heightdiff;
2796  }
2797 
2798  ShowMeasurementTooltips(measure_strings_length[index], index, params);
2799  }
2800 
2801  _thd.selend.x = x;
2802  _thd.selend.y = y;
2803  _thd.next_drawstyle = b;
2804 }
2805 
2814 {
2815  int sx, sy;
2816  HighLightStyle style;
2817 
2818  if (x == -1) {
2819  _thd.selend.x = -1;
2820  return;
2821  }
2822 
2823  /* Special handling of drag in any (8-way) direction */
2824  if (method & (VPM_RAILDIRS | VPM_SIGNALDIRS)) {
2825  _thd.selend.x = x;
2826  _thd.selend.y = y;
2827  CalcRaildirsDrawstyle(x, y, method);
2828  return;
2829  }
2830 
2831  /* Needed so level-land is placed correctly */
2832  if ((_thd.next_drawstyle & HT_DRAG_MASK) == HT_POINT) {
2833  x += TILE_SIZE / 2;
2834  y += TILE_SIZE / 2;
2835  }
2836 
2837  sx = _thd.selstart.x;
2838  sy = _thd.selstart.y;
2839 
2840  int limit = 0;
2841 
2842  switch (method) {
2843  case VPM_X_OR_Y: // drag in X or Y direction
2844  if (abs(sy - y) < abs(sx - x)) {
2845  y = sy;
2846  style = HT_DIR_X;
2847  } else {
2848  x = sx;
2849  style = HT_DIR_Y;
2850  }
2851  goto calc_heightdiff_single_direction;
2852 
2853  case VPM_X_LIMITED: // Drag in X direction (limited size).
2854  limit = (_thd.sizelimit - 1) * TILE_SIZE;
2855  FALLTHROUGH;
2856 
2857  case VPM_FIX_X: // drag in Y direction
2858  x = sx;
2859  style = HT_DIR_Y;
2860  goto calc_heightdiff_single_direction;
2861 
2862  case VPM_Y_LIMITED: // Drag in Y direction (limited size).
2863  limit = (_thd.sizelimit - 1) * TILE_SIZE;
2864  FALLTHROUGH;
2865 
2866  case VPM_FIX_Y: // drag in X direction
2867  y = sy;
2868  style = HT_DIR_X;
2869 
2870 calc_heightdiff_single_direction:;
2871  if (limit > 0) {
2872  x = sx + Clamp(x - sx, -limit, limit);
2873  y = sy + Clamp(y - sy, -limit, limit);
2874  }
2876  TileIndex t0 = TileVirtXY(sx, sy);
2877  TileIndex t1 = TileVirtXY(x, y);
2878  uint distance = DistanceManhattan(t0, t1) + 1;
2879  byte index = 0;
2880  uint64 params[2];
2881 
2882  if (distance != 1) {
2883  /* With current code passing a HT_LINE style to calculate the height
2884  * difference is enough. However if/when a point-tool is created
2885  * with this method, function should be called with new_style (below)
2886  * instead of HT_LINE | style case HT_POINT is handled specially
2887  * new_style := (_thd.next_drawstyle & HT_RECT) ? HT_LINE | style : _thd.next_drawstyle; */
2888  int heightdiff = CalcHeightdiff(HT_LINE | style, 0, t0, t1);
2889 
2890  params[index++] = distance;
2891  if (heightdiff != 0) params[index++] = heightdiff;
2892  }
2893 
2894  ShowMeasurementTooltips(measure_strings_length[index], index, params);
2895  }
2896  break;
2897 
2898  case VPM_X_AND_Y_LIMITED: // Drag an X by Y constrained rect area.
2899  limit = (_thd.sizelimit - 1) * TILE_SIZE;
2900  x = sx + Clamp(x - sx, -limit, limit);
2901  y = sy + Clamp(y - sy, -limit, limit);
2902  FALLTHROUGH;
2903 
2904  case VPM_X_AND_Y: // drag an X by Y area
2906  static const StringID measure_strings_area[] = {
2907  STR_NULL, STR_NULL, STR_MEASURE_AREA, STR_MEASURE_AREA_HEIGHTDIFF
2908  };
2909 
2910  TileIndex t0 = TileVirtXY(sx, sy);
2911  TileIndex t1 = TileVirtXY(x, y);
2912  uint dx = Delta(TileX(t0), TileX(t1)) + 1;
2913  uint dy = Delta(TileY(t0), TileY(t1)) + 1;
2914  byte index = 0;
2915  uint64 params[3];
2916 
2917  /* If dragging an area (eg dynamite tool) and it is actually a single
2918  * row/column, change the type to 'line' to get proper calculation for height */
2919  style = (HighLightStyle)_thd.next_drawstyle;
2920  if (_thd.IsDraggingDiagonal()) {
2921  /* Determine the "area" of the diagonal dragged selection.
2922  * We assume the area is the number of tiles along the X
2923  * edge and the number of tiles along the Y edge. However,
2924  * multiplying these two numbers does not give the exact
2925  * number of tiles; basically we are counting the black
2926  * squares on a chess board and ignore the white ones to
2927  * make the tile counts at the edges match up. There is no
2928  * other way to make a proper count though.
2929  *
2930  * First convert to the rotated coordinate system. */
2931  int dist_x = TileX(t0) - TileX(t1);
2932  int dist_y = TileY(t0) - TileY(t1);
2933  int a_max = dist_x + dist_y;
2934  int b_max = dist_y - dist_x;
2935 
2936  /* Now determine the size along the edge, but due to the
2937  * chess board principle this counts double. */
2938  a_max = abs(a_max + (a_max > 0 ? 2 : -2)) / 2;
2939  b_max = abs(b_max + (b_max > 0 ? 2 : -2)) / 2;
2940 
2941  /* We get a 1x1 on normal 2x1 rectangles, due to it being
2942  * a seen as two sides. As the result for actual building
2943  * will be the same as non-diagonal dragging revert to that
2944  * behaviour to give it a more normally looking size. */
2945  if (a_max != 1 || b_max != 1) {
2946  dx = a_max;
2947  dy = b_max;
2948  }
2949  } else if (style & HT_RECT) {
2950  if (dx == 1) {
2951  style = HT_LINE | HT_DIR_Y;
2952  } else if (dy == 1) {
2953  style = HT_LINE | HT_DIR_X;
2954  }
2955  }
2956 
2957  if (dx != 1 || dy != 1) {
2958  int heightdiff = CalcHeightdiff(style, 0, t0, t1);
2959 
2960  params[index++] = dx - (style & HT_POINT ? 1 : 0);
2961  params[index++] = dy - (style & HT_POINT ? 1 : 0);
2962  if (heightdiff != 0) params[index++] = heightdiff;
2963  }
2964 
2965  ShowMeasurementTooltips(measure_strings_area[index], index, params);
2966  }
2967  break;
2968 
2969  default: NOT_REACHED();
2970  }
2971 
2972  _thd.selend.x = x;
2973  _thd.selend.y = y;
2974 }
2975 
2981 {
2983 
2984  /* stop drag mode if the window has been closed */
2985  Window *w = _thd.GetCallbackWnd();
2986  if (w == NULL) {
2988  return ES_HANDLED;
2989  }
2990 
2991  /* while dragging execute the drag procedure of the corresponding window (mostly VpSelectTilesWithMethod() ) */
2992  if (_left_button_down) {
2993  w->OnPlaceDrag(_thd.select_method, _thd.select_proc, GetTileBelowCursor());
2994  return ES_HANDLED;
2995  }
2996 
2997  /* mouse button released..
2998  * keep the selected tool, but reset it to the original mode. */
3000  HighLightStyle others = _thd.place_mode & ~(HT_DRAG_MASK | HT_DIR_MASK);
3001  if ((_thd.next_drawstyle & HT_DRAG_MASK) == HT_RECT) {
3002  _thd.place_mode = HT_RECT | others;
3003  } else if (_thd.select_method & VPM_SIGNALDIRS) {
3004  _thd.place_mode = HT_RECT | others;
3005  } else if (_thd.select_method & VPM_RAILDIRS) {
3006  _thd.place_mode = (_thd.select_method & ~VPM_RAILDIRS) ? _thd.next_drawstyle : (HT_RAIL | others);
3007  } else {
3008  _thd.place_mode = HT_POINT | others;
3009  }
3010  SetTileSelectSize(1, 1);
3011 
3012  HideMeasurementTooltips();
3013  w->OnPlaceMouseUp(_thd.select_method, _thd.select_proc, _thd.selend, TileVirtXY(_thd.selstart.x, _thd.selstart.y), TileVirtXY(_thd.selend.x, _thd.selend.y));
3014 
3015  return ES_HANDLED;
3016 }
3017 
3026 {
3027  SetObjectToPlace(icon, pal, mode, w->window_class, w->window_number);
3028 }
3029 
3030 #include "table/animcursors.h"
3031 
3040 void SetObjectToPlace(CursorID icon, PaletteID pal, HighLightStyle mode, WindowClass window_class, WindowNumber window_num)
3041 {
3042  if (_thd.window_class != WC_INVALID) {
3043  /* Undo clicking on button and drag & drop */
3044  Window *w = _thd.GetCallbackWnd();
3045  /* Call the abort function, but set the window class to something
3046  * that will never be used to avoid infinite loops. Setting it to
3047  * the 'next' window class must not be done because recursion into
3048  * this function might in some cases reset the newly set object to
3049  * place or not properly reset the original selection. */
3050  _thd.window_class = WC_INVALID;
3051  if (w != NULL) {
3052  w->OnPlaceObjectAbort();
3053  HideMeasurementTooltips();
3054  }
3055  }
3056 
3057  /* Mark the old selection dirty, in case the selection shape or colour changes */
3059 
3060  SetTileSelectSize(1, 1);
3061 
3062  _thd.make_square_red = false;
3063 
3064  if (mode == HT_DRAG) { // HT_DRAG is for dragdropping trains in the depot window
3065  mode = HT_NONE;
3067  } else {
3069  }
3070 
3071  _thd.place_mode = mode;
3072  _thd.window_class = window_class;
3073  _thd.window_number = window_num;
3074 
3075  if ((mode & HT_DRAG_MASK) == HT_SPECIAL) { // special tools, like tunnels or docks start with presizing mode
3076  VpStartPreSizing();
3077  }
3078 
3079  if ((icon & ANIMCURSOR_FLAG) != 0) {
3080  SetAnimatedMouseCursor(_animcursors[icon & ~ANIMCURSOR_FLAG]);
3081  } else {
3082  SetMouseCursor(icon, pal);
3083  }
3084 
3085 }
3086 
3089 {
3091 }
3092 
3093 Point GetViewportStationMiddle(const ViewPort *vp, const Station *st)
3094 {
3095  int x = TileX(st->xy) * TILE_SIZE;
3096  int y = TileY(st->xy) * TILE_SIZE;
3097  int z = GetSlopePixelZ(Clamp(x, 0, MapSizeX() * TILE_SIZE - 1), Clamp(y, 0, MapSizeY() * TILE_SIZE - 1));
3098 
3099  Point p = RemapCoords(x, y, z);
3100  p.x = UnScaleByZoom(p.x - vp->virtual_left, vp->zoom) + vp->left;
3101  p.y = UnScaleByZoom(p.y - vp->virtual_top, vp->zoom) + vp->top;
3102  return p;
3103 }
3104 
3109 };
3110 
3113 #ifdef WITH_SSE
3114  { &ViewportSortParentSpritesSSE41Checker, &ViewportSortParentSpritesSSE41 },
3115 #endif
3117 };
3118 
3121 {
3122  for (uint i = 0; i < lengthof(_vp_sprite_sorters); i++) {
3123  if (_vp_sprite_sorters[i].fct_checker()) {
3124  _vp_sprite_sorter = _vp_sprite_sorters[i].fct_sorter;
3125  break;
3126  }
3127  }
3128  assert(_vp_sprite_sorter != NULL);
3129 }
3130 
3140 CommandCost CmdScrollViewport(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
3141 {
3142  if (_current_company != OWNER_DEITY) return CMD_ERROR;
3144  switch (target) {
3145  case VST_EVERYONE:
3146  break;
3147  case VST_COMPANY:
3148  if (_local_company != (CompanyID)p2) return CommandCost();
3149  break;
3150  case VST_CLIENT:
3151 #ifdef ENABLE_NETWORK
3152  if (_network_own_client_id != (ClientID)p2) return CommandCost();
3153  break;
3154 #else
3155  return CommandCost();
3156 #endif
3157  default:
3158  return CMD_ERROR;
3159  }
3160 
3161  if (flags & DC_EXEC) {
3163  ScrollMainWindowToTile(tile);
3164  }
3165  return CommandCost();
3166 }
EventState
State of handling an event.
Definition: window_type.h:713
void ViewportAddVehicles(DrawPixelInfo *dpi)
Add the vehicle sprites that should be drawn at a part of the screen.
Definition: vehicle.cpp:1111
bool DoZoomInOutWindow(ZoomStateChange how, Window *w)
Zooms a viewport in a window in or out.
Definition: main_gui.cpp:144
Functions related to OTTD&#39;s strings.
static TileType GetTileType(TileIndex tile)
Get the tiletype of a given tile.
Definition: tile_map.h:98
ViewportDragDropSelectionProcess
Drag and drop selection process, or, what to do with an area of land when you&#39;ve selected it...
Definition: viewport_type.h:97
Y direction.
bool measure_tooltip
show a permanent tooltip when dragging tools
static bool IsHalftileSlope(Slope s)
Checks for non-continuous slope on halftile foundations.
Definition: slope_func.h:49
static void DrawAutorailSelection(const TileInfo *ti, uint autorail_type)
Draws autorail highlights.
Definition: viewport.cpp:943
static const PaletteID PALETTE_SEL_TILE_RED
makes a square red. is used when removing rails or other stuff
Definition: sprites.h:1545
static void Swap(T &a, T &b)
Type safe swap operation.
Definition: math_func.hpp:277
the north corner of the tile is raised
Definition: slope_type.h:55
Corner
Enumeration of tile corners.
Definition: slope_type.h:24
uint32 PaletteID
The number of the palette.
Definition: gfx_type.h:20
static const uint TILE_PIXELS
Pixel distance between tile columns/rows in #ZOOM_LVL_BASE.
Definition: tile_type.h:17
WindowNumber window_number
The WindowNumber of the window that is responsible for the selection mode.
static uint MapSizeX()
Get the size of the map along the X.
Definition: map_func.h:74
static const int MAX_TILE_EXTENT_RIGHT
Maximum right extent of tile relative to north corner.
Definition: viewport.cpp:103
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:77
bool IsDraggingDiagonal()
Is the user dragging a &#39;diagonal rectangle&#39;?
Definition: viewport.cpp:2197
int32 zmin
minimal world Z coordinate of bounding box
Definition of stuff that is very close to a company, like the company struct itself.
int32 zmax
maximal world Z coordinate of bounding box
static const PaletteID PALETTE_TO_TRANSPARENT
This sets the sprite to transparent.
Definition: sprites.h:1577
Window * FindWindowFromPt(int x, int y)
Do a search for a window at specific coordinates.
Definition: window.cpp:1862
void ShowWaypointWindow(const Waypoint *wp)
Show the window for the given waypoint.
int virtual_left
Virtual left coordinate.
Definition: viewport_type.h:30
int32 top
minimal screen Y coordinate of sprite (= y + sprite->y_offs), reference point for child sprites ...
void InitializeWindowViewport(Window *w, int x, int y, int width, int height, uint32 follow_flags, ZoomLevel zoom)
Initialize viewport of the window for use.
Definition: viewport.cpp:216
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:2455
Begin for iteration.
Definition: zoom_type.h:23
Data about how and where to blit pixels.
Definition: gfx_type.h:156
static uint MapSizeY()
Get the size of the map along the Y.
Definition: map_func.h:84
Horizontally center the text.
Definition: gfx_func.h:99
Tile information, used while rendering the tile.
Definition: tile_cmd.h:44
Sprite combining will start with the next unclipped sprite.
Definition: viewport.cpp:147
SpriteCombineMode
Mode of "sprite combining".
Definition: viewport.cpp:145
void SetWidgetDisabledState(byte widget_index, bool disab_stat)
Sets the enabled/disabled status of a widget.
Definition: window_gui.h:394
Point pos
Location, in tile "units", of the northern tile of the selected area.
No special mouse mode.
Definition: window_gui.h:910
Point pos
logical mouse position
Definition: gfx_type.h:119
All zoomlevels below or equal to this, will result in details on the screen, like road-work...
Definition: zoom_type.h:45
area of land of limited size
Definition: viewport_type.h:83
void(* VpSpriteSorter)(ParentSpriteToSortVector *psd)
Type for the actual viewport sprite sorter.
ZoomLevelByte zoom_max
maximum zoom out level
The mask to for the main sprite.
Definition: sprites.h:1530
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:1544
uint32 GetCompanyMask()
Get a bitmask of the currently shown companies.
Definition: linkgraph_gui.h:70
signs
Definition: transparency.h:25
byte _display_opt
What do we want to draw/do?
int left
x position of left edge of the window
Definition: window_gui.h:319
End for iteration.
Definition: zoom_type.h:30
SpecialMouseMode _special_mouse_mode
Mode of the mouse.
Definition: window.cpp:82
bool VehicleClicked(const Vehicle *v)
Dispatch a "vehicle selected" event if any window waits for it.
Zoom out (get helicopter view).
Definition: viewport_type.h:64
static int GetTilePixelZ(TileIndex tile)
Get bottom height of the tile.
Definition: tile_map.h:296
int height
Screen height of the viewport.
Definition: viewport_type.h:28
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:246
int32 y
screen Y coordinate of sprite
Definition: viewport.cpp:121
Display waypoint names.
Definition: openttd.h:48
void HandleZoomMessage(Window *w, const ViewPort *vp, byte widget_zoom_in, byte widget_zoom_out)
Update the status of the zoom-buttons according to the zoom-level of the viewport.
Definition: viewport.cpp:472
static 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:84
static bool IsInsideMM(const T x, const uint min, const uint max)
Checks if a value is in an interval.
Definition: math_func.hpp:266
vertical right
VpSorterChecker fct_checker
The check function.
Definition: viewport.cpp:3107
TileType
The different types of tiles.
Definition: tile_type.h:42
void SetWidgetDirty(byte widget_index) const
Invalidate a widget, i.e.
Definition: window.cpp:581
static int UnScaleByZoomLower(int value, ZoomLevel zoom)
Scale by zoom level, usually shift right (when zoom > ZOOM_LVL_NORMAL)
Definition: zoom_func.h:61
int next
next child to draw (-1 at the end)
Definition: viewport.cpp:130
static T SetBit(T &x, const uint8 y)
Set a bit in a variable.
Dragging an object.
Definition: window_gui.h:911
Window * FindWindowById(WindowClass cls, WindowNumber number)
Find a window by its class and window number.
Definition: window.cpp:1114
The passed event is not handled.
Definition: window_type.h:715
X direction.
Display station names.
Definition: openttd.h:44
byte _colour_gradient[COLOUR_END][8]
All 16 colour gradients 8 colours per gradient from darkest (0) to lightest (7)
Definition: gfx.cpp:53
void SetTileSelectSize(int w, int h)
Highlight w by h tiles at the cursor.
Definition: viewport.cpp:2160
static int UnScaleByZoom(int value, ZoomLevel zoom)
Scale by zoom level, usually shift right (when zoom > ZOOM_LVL_NORMAL) When shifting right...
Definition: zoom_func.h:37
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
Definition: command_func.h:25
drag only in X axis
Definition: viewport_type.h:80
Data structure describing a sprite.
Definition: spritecache.h:18
Slope tileh
Slope of the tile.
Definition: tile_cmd.h:47
Sprite combining is active. AddSortableSpriteToDraw outputs child sprites.
Definition: viewport.cpp:148
Types for recording game performance data.
Drag only in X axis with limited size.
Definition: viewport_type.h:86
static const AnimCursor *const _animcursors[]
This is an array of pointers to all the animated cursor definitions we have above.
Definition: animcursors.h:87
similar to VMP_RAILDIRS, but with different cursor
Definition: viewport_type.h:89
bool IsInUse() const
Check whether the base station currently is in use; in use means that it is not scheduled for deletio...
void GuiShowTooltips(Window *parent, StringID str, uint paramcount, const uint64 params[], TooltipCloseCondition close_tooltip)
Shows a tooltip.
Definition: misc_gui.cpp:741
Slope GetTilePixelSlopeOutsideMap(int x, int y, int *h)
Return the slope of a given tile, also for tiles outside the map (virtual "black" tiles)...
Definition: tile_map.cpp:84
static void CheckUnderflow(int &test, int &other, int mult)
Check for underflowing the map.
Definition: viewport.cpp:2569
Point size
Size, in tile "units", of the white/red selection area.
virtual void SetPixel(void *video, int x, int y, uint8 colour)=0
Draw a pixel with a given colour on the video-buffer.
bool population_in_label
show the population of a town in his label?
drag only in Y axis
Definition: viewport_type.h:81
Drag only in Y axis with limited size.
Definition: viewport_type.h:87
Functions related to vehicles.
static const int MAX_TILE_EXTENT_LEFT
Maximum left extent of tile relative to north corner.
Definition: viewport.cpp:102
int virtual_height
height << zoom
Definition: viewport_type.h:33
Point new_size
New value for size; used to determine whether to redraw the selection.
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:207
Vehicle data structure.
Definition: vehicle_base.h:212
Point new_pos
New value for pos; used to determine whether to redraw the selection.
int top
y position of top edge of the window
Definition: window_gui.h:320
static bool IsExpected(const BaseStation *st)
Helper for checking whether the given station is of this type.
static bool IsInsideBS(const T x, const uint base, const uint size)
Checks if a value is between a window started at some base point.
Definition: math_func.hpp:250
void Clear()
Remove all items from the list.
Display town names.
Definition: openttd.h:43
static int ScaleByZoom(int value, ZoomLevel zoom)
Scale by zoom level, usually shift left (when zoom > ZOOM_LVL_NORMAL) When shifting right...
Definition: zoom_func.h:24
const T * Begin() const
Get the pointer to the first item (const)
Tindex index
Index of this pool item.
Definition: pool_type.hpp:147
int foundation[FOUNDATION_PART_END]
Foundation sprites (index into parent_sprites_to_draw).
Definition: viewport.cpp:170
static const int DRAW_STRING_BUFFER
Size of the buffer used for drawing strings.
Definition: gfx_func.h:87
static bool IsSteepSlope(Slope s)
Checks if a slope is steep.
Definition: slope_func.h:38
Display signs, station names and waypoint names of opponent companies. Buoys and oilrig-stations are ...
Definition: openttd.h:49
Zoom in (get more detailed view).
Definition: viewport_type.h:63
Maximum number of sprites that can be loaded at a given time.
Definition: sprites.h:1529
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:50
The client is spectating.
Definition: company_type.h:37
All players.
SpriteCombineMode combine_sprites
Current mode of "sprite combining".
Definition: viewport.cpp:168
Second part (halftile foundation)
Definition: viewport.cpp:137
static Corner GetHalftileSlopeCorner(Slope s)
Returns the leveled halftile of a halftile slope.
Definition: slope_func.h:150
WindowClass
Window classes.
Definition: window_type.h:39
ParentSpriteToSortVector parent_sprites_to_sort
Parent sprite pointer array used for sorting.
Definition: viewport.cpp:163
The most basic (normal) sprite.
Definition: gfx_type.h:298
static int DivAwayFromZero(int a, uint b)
Computes (a / b) rounded away from zero.
Definition: math_func.hpp:355
virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt)
The user is dragging over the map when the tile highlight mode has been set.
Definition: window_gui.h:778
Invalid window.
Definition: window_type.h:696
virtual void OnPlaceObjectAbort()
The user cancelled a tile highlight mode that has been set.
Definition: window_gui.h:768
Common return value for all commands.
Definition: command_type.h:25
How all blitters should look like.
Definition: base.hpp:30
static Slope SlopeWithOneCornerRaised(Corner corner)
Returns the slope with a specific corner raised.
Definition: slope_func.h:101
static const VehicleID INVALID_VEHICLE
Constant representing a non-existing vehicle.
Definition: vehicle_type.h:59
void StartSpriteCombine()
Starts a block of sprites, which are "combined" into a single bounding box.
Definition: viewport.cpp:751
Functions related to signs.
void UpdatePosition(int center, int top, StringID str, StringID str_small=STR_NULL)
Update the position of the viewport sign.
Definition: viewport.cpp:1280
static T max(const T a, const T b)
Returns the maximum of two values.
Definition: math_func.hpp:26
vertical left
uint32 population
Current population of people.
Definition: town.h:47
Presizing mode (docks, tunnels).
Definition: window_gui.h:913
const T * End() const
Get the pointer behind the last valid item (const)
Functions related to the vehicle&#39;s GUIs.
Left margin.
Definition: viewport_type.h:41
int z
Height.
Definition: tile_cmd.h:49
Window * GetCallbackWnd()
Get the window that started the current highlighting.
Definition: viewport.cpp:2206
static const uint TILE_SIZE
Tile size in world coordinates.
Definition: tile_type.h:15
FoundationPart foundation_part
Currently active foundation for ground sprite drawing.
Definition: viewport.cpp:171
void MarkDirty(ZoomLevel maxzoom=ZOOM_LVL_MAX) const
Mark the sign dirty in all viewports.
Definition: viewport.cpp:1307
int32 x
screen X coordinate of sprite
Definition: viewport.cpp:120
Highlight/sprite information for autorail.
CommandCost CmdScrollViewport(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Scroll players main viewport.
Definition: viewport.cpp:3140
HighLightStyle place_mode
Method which is used to place the selection.
Functions, definitions and such used only by the GUI.
TileIndex GetNorthernBridgeEnd(TileIndex t)
Finds the northern end of a bridge starting at a middle tile.
Definition: bridge_map.cpp:41
static Waypoint * From(BaseStation *st)
Converts a BaseStation to SpecializedStation with type checking.
ViewportScrollTarget
Target of the viewport scrolling GS method.
Display signs.
Definition: openttd.h:45
Point foundation_offset[FOUNDATION_PART_END]
Pixel offset for ground sprites on the foundations.
Definition: viewport.cpp:173
void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags)
Draw frame rectangle.
Definition: widget.cpp:177
Right margin.
Definition: viewport_type.h:42
T * Append(uint to_add=1)
Append an item and return it.
CompanyByte _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Definition: company_cmd.cpp:46
Functions related to (drawing on) viewports.
void Draw(const DrawPixelInfo *dpi)
Draw the linkgraph overlay or some part of it, in the area given.
The object is owned by a superuser / goal script.
Definition: company_type.h:29
Declaration of linkgraph overlay GUI.
Data structure for an opened window.
Definition: window_gui.h:278
bool _ctrl_pressed
Is Ctrl pressed?
Definition: gfx.cpp:36
Data structure storing rendering information.
Definition: viewport.cpp:157
static bool IsValidTile(TileIndex tile)
Checks if a tile is valid.
Definition: tile_map.h:163
void EndSpriteCombine()
Terminates a block of sprites started by StartSpriteCombine.
Definition: viewport.cpp:761
static bool IsBridgeAbove(TileIndex t)
checks if a bridge is set above the ground of this tile
Definition: bridge_map.h:45
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:413
dragging items in the depot windows
ViewportSign sign
NOSAVE: Dimensions of sign.
static HighLightStyle GetAutorailHT(int x, int y)
returns the best autorail highlight type from map coordinates
Definition: viewport.cpp:2177
Point selstart
The location where the dragging started.
Main window; Window numbers:
Definition: window_type.h:46
byte sizelimit
Whether the selection is limited in length, and what the maximum length is.
First part (normal foundation or no foundation)
Definition: viewport.cpp:136
Point selend
The location where the drag currently ends.
uint x
X position of the tile in unit coordinates.
Definition: tile_cmd.h:45
Every AddSortableSpriteToDraw start its own bounding box.
Definition: viewport.cpp:146
int16 y_offs
Number of pixels to shift the sprite downwards.
Definition: spritecache.h:22
ViewportSign sign
Location of name sign, UpdateVirtCoord updates this.
Definition: town.h:48
TileIndex tile
Tile index.
Definition: tile_cmd.h:48
UnitID unitnumber
unit number, for display purposes only
Definition: vehicle_base.h:291
#define FONT_HEIGHT_SMALL
Height of characters in the small (FS_SMALL) font.
Definition: gfx_func.h:177
int32 ymin
minimal world Y coordinate of bounding box
uint Length() const
Get the number of items in the list.
int32 first_child
the first child to draw.
masks the drag-direction
void ResetObjectToPlace()
Reset the cursor and mouse mode handling back to default (normal cursor, only clicking in windows)...
Definition: viewport.cpp:3088
static T Align(const T x, uint n)
Return the smallest multiple of n equal or greater than x.
Definition: math_func.hpp:97
Number of zoom levels.
Definition: zoom_type.h:32
int32 top
The top of the sign.
Definition: viewport_type.h:50
void SetRedErrorSquare(TileIndex tile)
Set a tile to display a red error square.
Definition: viewport.cpp:2142
Tooltip window; Window numbers:
Definition: window_type.h:111
void VpStartPlaceSizing(TileIndex tile, ViewportPlaceMethod method, ViewportDragDropSelectionProcess process)
highlighting tiles while only going over them with the mouse
Definition: viewport.cpp:2340
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:3040
int32 x
screen X coordinate of sprite
void 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:1752
Point offs
Offset, in tile "units", for the blue coverage area from the selected area&#39;s northern tile...
uint32 VehicleID
The type all our vehicle IDs have.
Definition: vehicle_type.h:18
ViewportPlaceMethod
Viewport place method (type of highlighted area and placed objects)
Definition: viewport_type.h:78
end marker
void DrawSpriteViewport(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub)
Draw a sprite in a viewport.
Definition: gfx.cpp:807
DoCommandFlag
List of flags for a command.
Definition: command_type.h:343
bool _left_button_down
Is left mouse button pressed?
Definition: gfx.cpp:39
#define FONT_HEIGHT_NORMAL
Height of characters in the normal (FS_NORMAL) font.
Definition: gfx_func.h:180
drag in X or Y direction
Definition: viewport_type.h:79
ClientID _network_own_client_id
Our client identifier.
Definition: network.cpp:63
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:76
static const CursorID ANIMCURSOR_FLAG
Flag for saying a cursor sprite is an animated cursor.
Definition: sprites.h:1477
static void ShowMeasurementTooltips(StringID str, uint paramcount, const uint64 params[], TooltipCloseCondition close_cond=TCC_NONE)
Displays the measurement tooltips when selecting multiple tiles.
Definition: viewport.cpp:2328
static ViewportSSCSS _vp_sprite_sorters[]
List of sorters ordered from best to worst.
Definition: viewport.cpp:3112
static void ViewportDrawChk(const ViewPort *vp, int left, int top, int right, int bottom)
Make sure we don&#39;t draw a too big area at a time.
Definition: viewport.cpp:1576
Definition of base types and functions in a cross-platform compatible way.
Location information about a sign as seen on the viewport.
Definition: viewport_type.h:48
static void ViewportDrawBoundingBoxes(const ParentSpriteToSortVector *psd)
Draws the bounding boxes of all ParentSprites.
Definition: viewport.cpp:1422
static void ViewportSortParentSprites(ParentSpriteToSortVector *psdv)
Sort parent sprites pointer array.
Definition: viewport.cpp:1345
A number of safeguards to prevent using unsafe methods.
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition: gfx_type.h:247
static void SetSelectionTilesDirty()
Marks the selected tiles as dirty.
Definition: viewport.cpp:1802
Base of waypoints.
static void CheckOverflow(int &test, int &other, int max, int mult)
Check for overflowing the map.
Definition: viewport.cpp:2584
rectangle (stations, depots, ...)
uint y
Y position of the tile in unit coordinates.
Definition: tile_cmd.h:46
int32 y
screen Y coordinate of sprite
static uint CeilDiv(uint a, uint b)
Computes ceil(a / b) for non-negative a and b.
Definition: math_func.hpp:316
ViewportPlaceMethod select_method
The method which governs how tiles are selected.
void StartStopVehicle(const Vehicle *v, bool texteffect)
Executes CMD_START_STOP_VEHICLE for given vehicle.
Data structure for a window viewport.
Definition: window_gui.h:258
static Slope GetTilePixelSlope(TileIndex tile, int *h)
Return the slope of a given tile.
Definition: tile_map.h:282
static const uint TILE_HEIGHT
Height of a height level in world coordinate AND in pixels in #ZOOM_LVL_BASE.
Definition: tile_type.h:18
int32 scrollpos_x
Currently shown x coordinate (virtual screen coordinate of topleft corner of the viewport).
Definition: window_gui.h:260
Single player.
int32 xmin
minimal world X coordinate of bounding box
HighLightStyle drawstyle
Lower bits 0-3 are reserved for detailed highlight information.
uint16 width_normal
The width when not zoomed out (normal font)
Definition: viewport_type.h:51
static void DrawTileSelectionRect(const TileInfo *ti, PaletteID pal)
Draws a selection rectangle on a tile.
Definition: viewport.cpp:883
Map accessor functions for bridges.
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:788
static Slope SlopeWithThreeCornersRaised(Corner corner)
Returns the slope with all except one corner raised.
Definition: slope_func.h:208
int virtual_width
width << zoom
Definition: viewport_type.h:32
The tile has no ownership.
Definition: company_type.h:27
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:1785
static const PaletteID PALETTE_SEL_TILE_BLUE
This draws a blueish square (catchment areas for example)
Definition: sprites.h:1546
Sizing mode.
Definition: window_gui.h:912
int DrawString(int left, int right, int top, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly truncated to make it fit in its allocated space.
Definition: gfx.cpp:500
void ShowVehicleViewWindow(const Vehicle *v)
Shows the vehicle view window of the given vehicle.
static int CalcHeightdiff(HighLightStyle style, uint distance, TileIndex start_tile, TileIndex end_tile)
Calculates height difference between one tile and another.
Definition: viewport.cpp:2489
StationFacilityByte facilities
The facilities that this station has.
void SetDirtyBlocks(int left, int top, int right, int bottom)
This function extends the internal _invalid_rect rectangle as it now contains the rectangle defined b...
Definition: gfx.cpp:1420
bool smooth_scroll
smooth scroll viewports
Definition: settings_type.h:99
autorail (one piece), lower bits: direction
static int GetViewportY(Point tile)
Returns the y coordinate in the viewport coordinate system where the given tile is painted...
Definition: viewport.cpp:1052
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:42
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition: factory.hpp:147
VehicleID follow_vehicle
VehicleID to follow if following a vehicle, INVALID_VEHICLE otherwise.
Definition: window_gui.h:259
static T min(const T a, const T b)
Returns the minimum of two values.
Definition: math_func.hpp:42
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:1643
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:614
bool ScrollWindowToTile(TileIndex tile, Window *w, bool instant)
Scrolls the viewport in a window to a given location.
Definition: viewport.cpp:2122
Also allow &#39;diagonal rectangles&#39;. Only usable in combination with HT_RECT or HT_POINT.
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:3025
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:18
Vehicle * First() const
Get the first vehicle of this vehicle chain.
Definition: vehicle_base.h:594
Point new_outersize
New value for outersize; used to determine whether to redraw the selection.
drag only in horizontal direction
Definition: viewport_type.h:84
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:279
ViewPort * IsPtInWindowViewport(const Window *w, int x, int y)
Is a xy position inside the viewport of the window?
Definition: viewport.cpp:389
int32 dest_scrollpos_y
Current destination y coordinate to display (virtual screen coordinate of topleft corner of the viewp...
Definition: window_gui.h:263
const SubSprite * sub
only draw a rectangular part of the sprite
Definition: viewport.cpp:127
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:654
static Slope RemoveHalftileSlope(Slope s)
Removes a halftile slope from a slope.
Definition: slope_func.h:62
void ShowStationViewWindow(StationID station)
Opens StationViewWindow for given station.
bool make_square_red
Whether to give a tile a red selection.
uint16 height
Height of the sprite.
Definition: spritecache.h:19
static T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:139
static const int MAX_TILE_EXTENT_TOP
Maximum top extent of tile relative to north corner (not considering bridges).
Definition: viewport.cpp:104
void SetDirty() const
Mark entire window as dirty (in need of re-paint)
Definition: window.cpp:968
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:36
point (lower land, raise land, level land, ...)
Dimension GetStringBoundingBox(const char *str, FontSize start_fontsize)
Return the string dimension in pixels.
Definition: gfx.cpp:700
static HighLightStyle Check2x1AutoRail(int mode)
returns information about the 2x1 piece to be build.
Definition: viewport.cpp:2411
int32 center
The center position of the sign.
Definition: viewport_type.h:49
static void 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:1716
const SubSprite * sub
only draw a rectangular part of the sprite
ViewportDragDropSelectionProcess select_proc
The procedure that has to be called when the selection is done.
void SetMouseCursor(CursorID sprite, PaletteID pal)
Assign a single non-animated sprite to the cursor.
Definition: gfx.cpp:1619
OwnerByte owner
The owner of this station.
Mask for the tile drag-type modes.
void UpdateTileSelection()
Updates tile highlighting for all cases.
Definition: viewport.cpp:2220
static uint ScaleByMapSize1D(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:138
A pair-construct of a TileIndexDiff.
Definition: map_type.h:59
int * last_foundation_child[FOUNDATION_PART_END]
Tail of ChildSprite list of the foundations. (index into child_screen_sprites_to_draw) ...
Definition: viewport.cpp:172
int left
Screen coordinate left egde of the viewport.
Definition: viewport_type.h:25
static const int TILE_HEIGHT_STEP
One Z unit tile height difference is displayed as 50m.
Definition: viewport_func.h:21
void DrawViewport() const
Draw the viewport of this window.
Definition: viewport.cpp:1618
void DeleteWindowById(WindowClass cls, WindowNumber number, bool force)
Delete a window by its class and window number (if it is open).
Definition: window.cpp:1146
ZoomLevelByte zoom_min
minimum zoom out level
static TileIndexDiff ToTileIndexDiff(TileIndexDiffC tidc)
Return the offset between to tiles from a TileIndexDiffC struct.
Definition: map_func.h:232
execute the given command
Definition: command_type.h:345
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:776
void SetAnimatedMouseCursor(const AnimCursor *table)
Assign an animation to the cursor.
Definition: gfx.cpp:1632
ZoomLevel
All zoom levels we know.
Definition: zoom_type.h:21
uint16 width
Width of the sprite.
Definition: spritecache.h:20
Functions related to companies.
default
static TileIndex TileVirtXY(uint x, uint y)
Get a tile from the virtual XY-coordinate.
Definition: map_func.h:196
bool ScrollMainWindowTo(int x, int y, int z, bool instant)
Scrolls the main window to given coordinates.
int32 dest_scrollpos_x
Current destination x coordinate to display (virtual screen coordinate of topleft corner of the viewp...
Definition: window_gui.h:262
area of land in X and Y directions
Definition: viewport_type.h:82
static bool ViewportSortParentSpritesChecker()
This fallback sprite checker always exists.
Definition: viewport.cpp:1339
Bottom margin.
Definition: viewport_type.h:44
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:518
Parent sprite that should be drawn.
static 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:86
GUISettings gui
settings related to the GUI
SpriteID image
sprite to draw
Invisible tiles at the SW and SE border.
Definition: tile_type.h:50
Base class for all vehicles.
Data structure for viewport, display of a part of the world.
Definition: viewport_type.h:24
static void DrawTileSelection(const TileInfo *ti)
Checks if the specified tile is selected and if so draws selection using correct selectionstyle.
Definition: viewport.cpp:977
static void ViewportAddLandscape()
Add the landscape to the viewport, i.e.
Definition: viewport.cpp:1061
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:105
uint32 SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:19
Time spent drawing world viewports in GUI.
CompanyByte _current_company
Company currently doing an action.
Definition: company_cmd.cpp:47
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
Window * z_front
The window in front of us in z-order.
Definition: window_gui.h:340
static const byte _string_colourmap[17]
Colour mapping for TextColour.
uint DistanceManhattan(TileIndex t0, TileIndex t1)
Gets the Manhattan distance between the two given tiles.
Definition: map.cpp:159
bool diagonal
Whether the dragged area is a 45 degrees rotated rectangle.
All players in specific company.
void HandleClickOnSign(const Sign *si)
Handle clicking on a sign.
Definition: signs_gui.cpp:559
Helper class for getting the best sprite sorter.
Definition: viewport.cpp:3106
static int GetBridgePixelHeight(TileIndex tile)
Get the height (&#39;z&#39;) of a bridge in pixels.
Definition: bridge_map.h:84
bool(* VpSorterChecker)()
Type for method for checking whether a viewport sprite sorter exists.
TileIndex redsq
The tile that has to get a red selection.
static const int MAX_TILE_EXTENT_BOTTOM
Maximum bottom extent of tile relative to north corner (worst case: SLOPE_STEEP_N).
Definition: viewport.cpp:105
int32 ymax
maximal world Y coordinate of bounding box
static void AddTileSpriteToDraw(SpriteID image, PaletteID pal, int32 x, int32 y, int z, const SubSprite *sub=NULL, int extra_offs_x=0, int extra_offs_y=0)
Schedules a tile sprite for drawing.
Definition: viewport.cpp:493
ClientID
&#39;Unique&#39; identifier to be given to clients
Definition: network_type.h:43
Top margin.
Definition: viewport_type.h:43
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:217
int32 xmax
maximal world X coordinate of bounding box
when a sprite is to be displayed transparently, this bit needs to be set.
Definition: sprites.h:1519
OwnerByte owner
Which company owns the vehicle?
Definition: vehicle_base.h:273
static T abs(const T a)
Returns the absolute value of (scalar) variable.
Definition: math_func.hpp:83
void VpSelectTilesWithMethod(int x, int y, ViewportPlaceMethod method)
Selects tiles while dragging.
Definition: viewport.cpp:2813
TileIndex xy
Base tile of the station.
void Reset()
Reset tile highlighting.
Definition: viewport.cpp:2185
static void DrawSelectionSprite(SpriteID image, PaletteID pal, const TileInfo *ti, int z_offset, FoundationPart foundation_part)
Draws sprites between ground sprite and everything above.
Definition: viewport.cpp:865
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Functions related to zooming.
static Point InverseRemapCoords(int x, int y)
Map 2D viewport or smallmap coordinate to 3D world or tile coordinate.
Definition: landscape.h:114
Slope
Enumeration for the slope-type.
Definition: slope_type.h:50
Neither foundation nor groundsprite drawn yet.
Definition: viewport.cpp:135
const TileTypeProcs *const _tile_type_procs[16]
Tile callback functions for each type of tile.
Definition: landscape.cpp:62
TownCache cache
Container for all cacheable data.
Definition: town.h:58
static uint MapMaxY()
Gets the maximum Y coordinate within the map, including MP_VOID.
Definition: map_func.h:113
RAII class for measuring multi-step elements of performance.
Town data structure.
Definition: town.h:55
void VpSetPresizeRange(TileIndex from, TileIndex to)
Highlights all tiles between a set of two tiles.
Definition: viewport.cpp:2383
int16 x_offs
Number of pixels to shift the sprite to the right.
Definition: spritecache.h:21
Used to only draw a part of the sprite.
Definition: gfx_type.h:219
Vehicle * CheckClickOnVehicle(const ViewPort *vp, int x, int y)
Find the vehicle close to the clicked coordinates.
Definition: vehicle.cpp:1169
byte max_bridge_height
maximum height of bridges
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...
static const uint TILE_UNIT_MASK
For masking in/out the inner-tile world coordinate units.
Definition: tile_type.h:16
static const uint MAX_BUILDING_PIXELS
Maximum height of a building in pixels in #ZOOM_LVL_BASE. (Also applies to "bridge buildings" on the ...
Definition: tile_type.h:20
Functions related to OTTD&#39;s landscape.
int32 z_pos
z coordinate.
Definition: vehicle_base.h:270
bool ScrollMainWindowToTile(TileIndex tile, bool instant)
Scrolls the viewport of the main window to a given location.
Definition: viewport.cpp:2133
void OffsetGroundSprite(int x, int y)
Called when a foundation has been drawn for the current tile.
Definition: viewport.cpp:582
Functions related to commands.
Network functions used by other parts of OpenTTD.
VpSpriteSorter fct_sorter
The sorting function.
Definition: viewport.cpp:3108
uint32 CursorID
The number of the cursor (sprite)
Definition: gfx_type.h:21
Coordinates of a point in 2D.
used for autorail highlighting (longer stretches), lower bits: direction
a steep slope falling to south (from north)
Definition: slope_type.h:71
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:570
Colours _company_colours[MAX_COMPANIES]
NOSAVE: can be determined from company structs.
Definition: company_cmd.cpp:48
static uint TileHeight(TileIndex tile)
Returns the height of a tile.
Definition: tile_map.h:31
The colour translation of GRF&#39;s strings.
special mode used for highlighting while dragging (and for tunnels/docks)
static bool CheckClickOnViewportSign(const ViewPort *vp, int x, int y, const ViewportSign *sign)
Test whether a sign is below the mouse.
Definition: viewport.cpp:1927
EventState VpHandlePlaceSizingDrag()
Handle the mouse while dragging for placement/resizing.
Definition: viewport.cpp:2980
Index of the small font in the font tables.
Definition: gfx_type.h:205
Zoomed 16 times out.
Definition: zoom_type.h:28
static uint SlopeToSpriteOffset(Slope s)
Returns the Sprite offset for a given Slope.
Definition: slope_func.h:417
ConstructionSettings construction
construction of things in-game
Colour value is already a real palette colour index, not an index of a StringColour.
Definition: gfx_type.h:270
HighLightStyle
Highlighting draw styles.
Functions related to waypoints.
DrawTileProc * draw_tile_proc
Called to render the tile and its contents to the screen.
Definition: tile_cmd.h:145
void AddChildSpriteScreen(SpriteID image, PaletteID pal, int x, int y, bool transparent, const SubSprite *sub, bool scale)
Add a child sprite to a parent sprite.
Definition: viewport.cpp:809
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:114
drag only in vertical direction
Definition: viewport_type.h:85
vehicle is accepted as target as well (bitmask)
ZoomLevel zoom
The zoom level of the viewport.
Definition: viewport_type.h:35
horizontal lower
int width
width of the window (number of pixels to the right in x direction)
Definition: window_gui.h:321
Metadata about the current highlighting.
int32 x_pos
x coordinate.
Definition: vehicle_base.h:268
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
void InitializeSpriteSorter()
Choose the "best" sprite sorter and set _vp_sprite_sorter.
Definition: viewport.cpp:3120
Types related to sprite sorting.
PaletteID pal
palette to use
static const TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:85
Base of the town class.
static 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:44
const T * Get(uint index) const
Get the pointer to item "number" (const)
int virtual_top
Virtual top coordinate.
Definition: viewport_type.h:31
The normal zoom level.
Definition: zoom_type.h:24
void ViewportAddString(const DrawPixelInfo *dpi, ZoomLevel small_from, const ViewportSign *sign, StringID string_normal, StringID string_small, StringID string_small_shadow, uint64 params_1, uint64 params_2, Colours colour)
Add a string to draw in the viewport.
Definition: viewport.cpp:1185
int32 left
minimal screen X coordinate of sprite (= x + sprite->x_offs), reference point for child sprites ...
int32 WindowNumber
Number to differentiate different windows of the same class.
Definition: window_type.h:707
horizontal upper
WindowClass window_class
Window class.
Definition: window_gui.h:313
Specification of a rectangle with absolute coordinates of all edges.
WindowClass window_class
The WindowClass of the window that is responsible for the selection mode.
virtual void OnPlaceObject(Point pt, TileIndex tile)
The user clicked some place on the map when a tile highlight mode has been set.
Definition: window_gui.h:756
The passed event is handled.
Definition: window_type.h:714
int32 y_pos
y coordinate.
Definition: vehicle_base.h:269
uint16 width_small
The width when zoomed out (small font)
Definition: viewport_type.h:52
static uint MapMaxX()
Gets the maximum X coordinate within the map, including MP_VOID.
Definition: map_func.h:104
WindowNumber window_number
Window number within the window class.
Definition: window_gui.h:314
void DrawGroundSpriteAt(SpriteID image, PaletteID pal, int32 x, int32 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:547
static bool IsInvisibilitySet(TransparencyOption to)
Check if the invisibility option bit is set and if we aren&#39;t in the game menu (there&#39;s never transpar...
Definition: transparency.h:61
FoundationPart
Enumeration of multi-part foundations.
Definition: viewport.cpp:134
Functions related to tile highlights.
HighLightStyle next_drawstyle
Queued, but not yet drawn style.
Owner
Enum for all companies/owners.
Definition: company_type.h:20
Window functions not directly related to making/drawing windows.
all rail directions
Definition: viewport_type.h:88
int top
Screen coordinate top edge of the viewport.
Definition: viewport_type.h:26
void SetDirty()
Mark the linkgraph dirty to be rebuilt next time Draw() is called.
Definition: linkgraph_gui.h:64
static void ViewportDrawDirtyBlocks()
Draw/colour the blocks that have been redrawn.
Definition: viewport.cpp:1442
Point outersize
Size, in tile "units", of the blue coverage area excluding the side of the selected area...
Makes the background transparent if set.
Definition: window_gui.h:29
#define TILE_ADD(x, y)
Adds to tiles together.
Definition: map_func.h:246
ViewportData * viewport
Pointer to viewport data, if present.
Definition: window_gui.h:328
static const CursorID SPR_CURSOR_MOUSE
Cursor sprite numbers.
Definition: sprites.h:1360
Base classes/functions for stations.
static T Delta(const T a, const T b)
Returns the (absolute) difference between two (scalar) variables.
Definition: math_func.hpp:232
bool comparison_done
Used during sprite sorting: true if sprite has been compared with all other sprites.
CargoTypes GetCargoMask()
Get a bitmask of the currently shown cargoes.
Definition: linkgraph_gui.h:67
static 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:100
int32 scrollpos_y
Currently shown y coordinate (virtual screen coordinate of topleft corner of the viewport).
Definition: window_gui.h:261
static bool IsCompanyBuildableVehicleType(VehicleType type)
Is the given vehicle type buildable by a company?
Definition: vehicle_func.h:91
const SubSprite * sub
only draw a rectangular part of the sprite
Definition: viewport.cpp:119
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:2087
Base class for all station-ish types.
Station data structure.
Definition: station_base.h:446
static bool IsTransparencySet(TransparencyOption to)
Check if the transparency option bit is set and if we aren&#39;t in the game menu (there&#39;s never transpar...
Definition: transparency.h:50
Factory to &#39;query&#39; all available blitters.
byte dirty
Whether the build station window needs to redraw due to the changed selection.
static uint TilePixelHeight(TileIndex tile)
Returns the height of a tile in pixels.
Definition: tile_map.h:74
static TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition: map_func.h:165
static Corner OppositeCorner(Corner corner)
Returns the opposite corner.
Definition: slope_func.h:186
This file defines all the the animated cursors.
virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile)
The user has dragged over the map when the tile highlight mode has been set.
Definition: window_gui.h:789
int height
Height of the window (number of pixels down in y direction)
Definition: window_gui.h:322
void UpdateViewportPosition(Window *w)
Update the viewport position being displayed.
Definition: viewport.cpp:1667
#define FOR_ALL_WINDOWS_FROM_BACK_FROM(w, start)
Iterate over all windows.
Definition: window_gui.h:894
static int GetTileMaxPixelZ(TileIndex tile)
Get top height of the tile.
Definition: tile_map.h:306
Base class for signs.
static void CalcRaildirsDrawstyle(int x, int y, int method)
while dragging
Definition: viewport.cpp:2593
static void SetDParam(uint n, uint64 v)
Set a string parameter v at index n in the global string parameter array.
Definition: strings_func.h:201
int width
Screen width of the viewport.
Definition: viewport_type.h:27