OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

viewport.cpp
Go to the documentation of this file.
1 /* $Id: viewport.cpp 27162 2015-02-22 15:05:48Z frosch $ */
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"
84 #include "linkgraph/linkgraph_gui.h"
85 #include "viewport_sprite_sorter.h"
86 #include "bridge_map.h"
87
88 #include <map>
89
90 #include "table/strings.h"
91 #include "table/palettes.h"
92
93 #include "safeguards.h"
94
95 Point _tile_fract_coords;
96
97
98 static const int MAX_TILE_EXTENT_LEFT = ZOOM_LVL_BASE * TILE_PIXELS;
99 static const int MAX_TILE_EXTENT_RIGHT = ZOOM_LVL_BASE * TILE_PIXELS;
100 static const int MAX_TILE_EXTENT_TOP = ZOOM_LVL_BASE * MAX_BUILDING_PIXELS;
101 static const int MAX_TILE_EXTENT_BOTTOM = ZOOM_LVL_BASE * (TILE_PIXELS + 2 * TILE_HEIGHT);
102
103 struct StringSpriteToDraw {
104 StringID string;
105 Colours colour;
106 int32 x;
107 int32 y;
108 uint64 params[2];
109 uint16 width;
110 };
111
112 struct TileSpriteToDraw {
113 SpriteID image;
114 PaletteID pal;
115 const SubSprite *sub;
116 int32 x;
117 int32 y;
118 };
119
120 struct ChildScreenSpriteToDraw {
121 SpriteID image;
122 PaletteID pal;
123 const SubSprite *sub;
124 int32 x;
125 int32 y;
126 int next;
127 };
128
130 enum FoundationPart {
131 FOUNDATION_PART_NONE = 0xFF,
132 FOUNDATION_PART_NORMAL = 0,
133 FOUNDATION_PART_HALFTILE = 1,
134 FOUNDATION_PART_END
135 };
136
141 enum SpriteCombineMode {
142 SPRITE_COMBINE_NONE,
143 SPRITE_COMBINE_PENDING,
144 SPRITE_COMBINE_ACTIVE,
145 };
146
147 typedef SmallVector<TileSpriteToDraw, 64> TileSpriteToDrawVector;
148 typedef SmallVector<StringSpriteToDraw, 4> StringSpriteToDrawVector;
149 typedef SmallVector<ParentSpriteToDraw, 64> ParentSpriteToDrawVector;
150 typedef SmallVector<ChildScreenSpriteToDraw, 16> ChildScreenSpriteToDrawVector;
151
153 struct ViewportDrawer {
154 DrawPixelInfo dpi;
155
156 StringSpriteToDrawVector string_sprites_to_draw;
157 TileSpriteToDrawVector tile_sprites_to_draw;
158 ParentSpriteToDrawVector parent_sprites_to_draw;
159 ParentSpriteToSortVector parent_sprites_to_sort;
160 ChildScreenSpriteToDrawVector child_screen_sprites_to_draw;
161
162 int *last_child;
163
164 SpriteCombineMode combine_sprites;
165
166 int foundation[FOUNDATION_PART_END];
167 FoundationPart foundation_part;
168 int *last_foundation_child[FOUNDATION_PART_END];
169 Point foundation_offset[FOUNDATION_PART_END];
170 };
171
172 static void MarkViewportDirty(const ViewPort *vp, int left, int top, int right, int bottom);
173
174 static ViewportDrawer _vd;
175
176 TileHighlightData _thd;
177 static TileInfo *_cur_ti;
178 bool _draw_bounding_boxes = false;
179 bool _draw_dirty_blocks = false;
180 uint _dirty_block_colour = 0;
181 static VpSpriteSorter _vp_sprite_sorter = NULL;
182
183 static Point MapXYZToViewport(const ViewPort *vp, int x, int y, int z)
184 {
185 Point p = RemapCoords(x, y, z);
186 p.x -= vp->virtual_width / 2;
187 p.y -= vp->virtual_height / 2;
188 return p;
189 }
190
191 void DeleteWindowViewport(Window *w)
192 {
193 if (w->viewport == NULL) return;
194
195 delete w->viewport->overlay;
196 free(w->viewport);
197 w->viewport = NULL;
198 }
199
212 void InitializeWindowViewport(Window *w, int x, int y,
213 int width, int height, uint32 follow_flags, ZoomLevel zoom)
214 {
215 assert(w->viewport == NULL);
216
217 ViewportData *vp = CallocT<ViewportData>(1);
218
219 vp->left = x + w->left;
220 vp->top = y + w->top;
221 vp->width = width;
222 vp->height = height;
223
224 vp->zoom = static_cast<ZoomLevel>(Clamp(zoom, _settings_client.gui.zoom_min, _settings_client.gui.zoom_max));
225
226 vp->virtual_width = ScaleByZoom(width, zoom);
227 vp->virtual_height = ScaleByZoom(height, zoom);
228
229 Point pt;
230
231 if (follow_flags & 0x80000000) {
232 const Vehicle *veh;
233
234 vp->follow_vehicle = (VehicleID)(follow_flags & 0xFFFFF);
235 veh = Vehicle::Get(vp->follow_vehicle);
236 pt = MapXYZToViewport(vp, veh->x_pos, veh->y_pos, veh->z_pos);
237 } else {
238 uint x = TileX(follow_flags) * TILE_SIZE;
239 uint y = TileY(follow_flags) * TILE_SIZE;
240
241 vp->follow_vehicle = INVALID_VEHICLE;
242 pt = MapXYZToViewport(vp, x, y, GetSlopePixelZ(x, y));
243 }
244
245 vp->scrollpos_x = pt.x;
246 vp->scrollpos_y = pt.y;
247 vp->dest_scrollpos_x = pt.x;
248 vp->dest_scrollpos_y = pt.y;
249
250 vp->overlay = NULL;
251
252 w->viewport = vp;
253 vp->virtual_left = 0; // pt.x;
254 vp->virtual_top = 0; // pt.y;
255 }
256
257 static Point _vp_move_offs;
258
259 static void DoSetViewportPosition(const Window *w, int left, int top, int width, int height)
260 {
261 FOR_ALL_WINDOWS_FROM_BACK_FROM(w, w) {
262 if (left + width > w->left &&
263 w->left + w->width > left &&
264 top + height > w->top &&
265 w->top + w->height > top) {
266
267 if (left < w->left) {
268 DoSetViewportPosition(w, left, top, w->left - left, height);
269 DoSetViewportPosition(w, left + (w->left - left), top, width - (w->left - left), height);
270 return;
271 }
272
273 if (left + width > w->left + w->width) {
274 DoSetViewportPosition(w, left, top, (w->left + w->width - left), height);
275 DoSetViewportPosition(w, left + (w->left + w->width - left), top, width - (w->left + w->width - left), height);
276 return;
277 }
278
279 if (top < w->top) {
280 DoSetViewportPosition(w, left, top, width, (w->top - top));
281 DoSetViewportPosition(w, left, top + (w->top - top), width, height - (w->top - top));
282 return;
283 }
284
285 if (top + height > w->top + w->height) {
286 DoSetViewportPosition(w, left, top, width, (w->top + w->height - top));
287 DoSetViewportPosition(w, left, top + (w->top + w->height - top), width, height - (w->top + w->height - top));
288 return;
289 }
290
291 return;
292 }
293 }
294
295 {
296 int xo = _vp_move_offs.x;
297 int yo = _vp_move_offs.y;
298
299 if (abs(xo) >= width || abs(yo) >= height) {
300 /* fully_outside */
301 RedrawScreenRect(left, top, left + width, top + height);
302 return;
303 }
304
305 GfxScroll(left, top, width, height, xo, yo);
306
307 if (xo > 0) {
308 RedrawScreenRect(left, top, xo + left, top + height);
309 left += xo;
310 width -= xo;
311 } else if (xo < 0) {
312 RedrawScreenRect(left + width + xo, top, left + width, top + height);
313 width += xo;
314 }
315
316 if (yo > 0) {
317 RedrawScreenRect(left, top, width + left, top + yo);
318 } else if (yo < 0) {
319 RedrawScreenRect(left, top + height + yo, width + left, top + height);
320 }
321 }
322 }
323
324 static void SetViewportPosition(Window *w, int x, int y)
325 {
326 ViewPort *vp = w->viewport;
327 int old_left = vp->virtual_left;
328 int old_top = vp->virtual_top;
329 int i;
330 int left, top, width, height;
331
332 vp->virtual_left = x;
333 vp->virtual_top = y;
334
335 /* Viewport is bound to its left top corner, so it must be rounded down (UnScaleByZoomLower)
336 * else glitch described in FS#1412 will happen (offset by 1 pixel with zoom level > NORMAL)
337 */
338 old_left = UnScaleByZoomLower(old_left, vp->zoom);
339 old_top = UnScaleByZoomLower(old_top, vp->zoom);
340 x = UnScaleByZoomLower(x, vp->zoom);
341 y = UnScaleByZoomLower(y, vp->zoom);
342
343 old_left -= x;
344 old_top -= y;
345
346 if (old_top == 0 && old_left == 0) return;
347
348 _vp_move_offs.x = old_left;
349 _vp_move_offs.y = old_top;
350
351 left = vp->left;
352 top = vp->top;
353 width = vp->width;
354 height = vp->height;
355
356 if (left < 0) {
357 width += left;
358 left = 0;
359 }
360
361 i = left + width - _screen.width;
362 if (i >= 0) width -= i;
363
364 if (width > 0) {
365 if (top < 0) {
366 height += top;
367 top = 0;
368 }
369
370 i = top + height - _screen.height;
371 if (i >= 0) height -= i;
372
373 if (height > 0) DoSetViewportPosition(w->z_front, left, top, width, height);
374 }
375 }
376
385 ViewPort *IsPtInWindowViewport(const Window *w, int x, int y)
386 {
387 ViewPort *vp = w->viewport;
388
389 if (vp != NULL &&
390 IsInsideMM(x, vp->left, vp->left + vp->width) &&
391 IsInsideMM(y, vp->top, vp->top + vp->height))
392 return vp;
393
394 return NULL;
395 }
396
405 Point TranslateXYToTileCoord(const ViewPort *vp, int x, int y, bool clamp_to_map)
406 {
407 Point pt;
408 int a, b;
409 int z;
410
411 if ( (uint)(x -= vp->left) >= (uint)vp->width ||
412 (uint)(y -= vp->top) >= (uint)vp->height) {
413 Point pt = {-1, -1};
414 return pt;
415 }
416
417 x = (ScaleByZoom(x, vp->zoom) + vp->virtual_left) >> (2 + ZOOM_LVL_SHIFT);
418 y = (ScaleByZoom(y, vp->zoom) + vp->virtual_top) >> (1 + ZOOM_LVL_SHIFT);
419
420 a = y - x;
421 b = y + x;
422
423 if (clamp_to_map) {
424 /* Bring the coordinates near to a valid range. This is mostly due to the
425 * tiles on the north side of the map possibly being drawn too high due to
426 * the extra height levels. So at the top we allow a number of extra tiles.
427 * This number is based on the tile height and pixels. */
428 int extra_tiles = CeilDiv(_settings_game.construction.max_heightlevel * TILE_HEIGHT, TILE_PIXELS);
429 a = Clamp(a, -extra_tiles * TILE_SIZE, MapMaxX() * TILE_SIZE - 1);
430 b = Clamp(b, -extra_tiles * TILE_SIZE, MapMaxY() * TILE_SIZE - 1);
431 }
432
433 /* (a, b) is the X/Y-world coordinate that belongs to (x,y) if the landscape would be completely flat on height 0.
434 * Now find the Z-world coordinate by fix point iteration.
435 * This is a bit tricky because the tile height is non-continuous at foundations.
436 * The clicked point should be approached from the back, otherwise there are regions that are not clickable.
437 * (FOUNDATION_HALFTILE_LOWER on SLOPE_STEEP_S hides north halftile completely)
438 * So give it a z-malus of 4 in the first iterations.
439 */
440 z = 0;
441
442 int min_coord = _settings_game.construction.freeform_edges ? TILE_SIZE : 0;
443
444 for (int i = 0; i < 5; i++) z = GetSlopePixelZ(Clamp(a + max(z, 4) - 4, min_coord, MapMaxX() * TILE_SIZE - 1), Clamp(b + max(z, 4) - 4, min_coord, MapMaxY() * TILE_SIZE - 1)) / 2;
445 for (int malus = 3; malus > 0; malus--) z = GetSlopePixelZ(Clamp(a + max(z, malus) - malus, min_coord, MapMaxX() * TILE_SIZE - 1), Clamp(b + max(z, malus) - malus, min_coord, MapMaxY() * TILE_SIZE - 1)) / 2;
446 for (int i = 0; i < 5; i++) z = GetSlopePixelZ(Clamp(a + z, min_coord, MapMaxX() * TILE_SIZE - 1), Clamp(b + z, min_coord, MapMaxY() * TILE_SIZE - 1)) / 2;
447
448 if (clamp_to_map) {
449 pt.x = Clamp(a + z, min_coord, MapMaxX() * TILE_SIZE - 1);
450 pt.y = Clamp(b + z, min_coord, MapMaxY() * TILE_SIZE - 1);
451 } else {
452 pt.x = a + z;
453 pt.y = b + z;
454 }
455
456 return pt;
457 }
458
459 /* When used for zooming, check area below current coordinates (x,y)
460 * and return the tile of the zoomed out/in position (zoom_x, zoom_y)
461 * when you just want the tile, make x = zoom_x and y = zoom_y */
462 static Point GetTileFromScreenXY(int x, int y, int zoom_x, int zoom_y)
463 {
464 Window *w;
465 ViewPort *vp;
466 Point pt;
467
468 if ( (w = FindWindowFromPt(x, y)) != NULL &&
469 (vp = IsPtInWindowViewport(w, x, y)) != NULL)
470 return TranslateXYToTileCoord(vp, zoom_x, zoom_y);
471
472 pt.y = pt.x = -1;
473 return pt;
474 }
475
476 Point GetTileBelowCursor()
477 {
478 return GetTileFromScreenXY(_cursor.pos.x, _cursor.pos.y, _cursor.pos.x, _cursor.pos.y);
479 }
480
481
482 Point GetTileZoomCenterWindow(bool in, Window * w)
483 {
484 int x, y;
485 ViewPort *vp = w->viewport;
486
487 if (in) {
488 x = ((_cursor.pos.x - vp->left) >> 1) + (vp->width >> 2);
489 y = ((_cursor.pos.y - vp->top) >> 1) + (vp->height >> 2);
490 } else {
491 x = vp->width - (_cursor.pos.x - vp->left);
492 y = vp->height - (_cursor.pos.y - vp->top);
493 }
494 /* Get the tile below the cursor and center on the zoomed-out center */
495 return GetTileFromScreenXY(_cursor.pos.x, _cursor.pos.y, x + vp->left, y + vp->top);
496 }
497
506 void HandleZoomMessage(Window *w, const ViewPort *vp, byte widget_zoom_in, byte widget_zoom_out)
507 {
508 w->SetWidgetDisabledState(widget_zoom_in, vp->zoom <= _settings_client.gui.zoom_min);
509 w->SetWidgetDirty(widget_zoom_in);
510
511 w->SetWidgetDisabledState(widget_zoom_out, vp->zoom >= _settings_client.gui.zoom_max);
512 w->SetWidgetDirty(widget_zoom_out);
513 }
514
527 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)
528 {
529 assert((image & SPRITE_MASK) < MAX_SPRITES);
530
531 TileSpriteToDraw *ts = _vd.tile_sprites_to_draw.Append();
532 ts->image = image;
533 ts->pal = pal;
534 ts->sub = sub;
535 Point pt = RemapCoords(x, y, z);
536 ts->x = pt.x + extra_offs_x;
537 ts->y = pt.y + extra_offs_y;
538 }
539
552 static void AddChildSpriteToFoundation(SpriteID image, PaletteID pal, const SubSprite *sub, FoundationPart foundation_part, int extra_offs_x, int extra_offs_y)
553 {
554 assert(IsInsideMM(foundation_part, 0, FOUNDATION_PART_END));
555 assert(_vd.foundation[foundation_part] != -1);
556 Point offs = _vd.foundation_offset[foundation_part];
557
558 /* Change the active ChildSprite list to the one of the foundation */
559 int *old_child = _vd.last_child;
560 _vd.last_child = _vd.last_foundation_child[foundation_part];
561
562 AddChildSpriteScreen(image, pal, offs.x + extra_offs_x, offs.y + extra_offs_y, false, sub, false);
563
564 /* Switch back to last ChildSprite list */
565 _vd.last_child = old_child;
566 }
567
581 void DrawGroundSpriteAt(SpriteID image, PaletteID pal, int32 x, int32 y, int z, const SubSprite *sub, int extra_offs_x, int extra_offs_y)
582 {
583 /* Switch to first foundation part, if no foundation was drawn */
584 if (_vd.foundation_part == FOUNDATION_PART_NONE) _vd.foundation_part = FOUNDATION_PART_NORMAL;
585
586 if (_vd.foundation[_vd.foundation_part] != -1) {
587 Point pt = RemapCoords(x, y, z);
588 AddChildSpriteToFoundation(image, pal, sub, _vd.foundation_part, pt.x + extra_offs_x * ZOOM_LVL_BASE, pt.y + extra_offs_y * ZOOM_LVL_BASE);
589 } else {
590 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);
591 }
592 }
593
604 void DrawGroundSprite(SpriteID image, PaletteID pal, const SubSprite *sub, int extra_offs_x, int extra_offs_y)
605 {
606 DrawGroundSpriteAt(image, pal, 0, 0, 0, sub, extra_offs_x, extra_offs_y);
607 }
608
616 void OffsetGroundSprite(int x, int y)
617 {
618 /* Switch to next foundation part */
619 switch (_vd.foundation_part) {
620 case FOUNDATION_PART_NONE:
621 _vd.foundation_part = FOUNDATION_PART_NORMAL;
622 break;
623 case FOUNDATION_PART_NORMAL:
624 _vd.foundation_part = FOUNDATION_PART_HALFTILE;
625 break;
626 default: NOT_REACHED();
627 }
628
629 /* _vd.last_child == NULL if foundation sprite was clipped by the viewport bounds */
630 if (_vd.last_child != NULL) _vd.foundation[_vd.foundation_part] = _vd.parent_sprites_to_draw.Length() - 1;
631
632 _vd.foundation_offset[_vd.foundation_part].x = x * ZOOM_LVL_BASE;
633 _vd.foundation_offset[_vd.foundation_part].y = y * ZOOM_LVL_BASE;
634 _vd.last_foundation_child[_vd.foundation_part] = _vd.last_child;
635 }
636
648 static void AddCombinedSprite(SpriteID image, PaletteID pal, int x, int y, int z, const SubSprite *sub)
649 {
650 Point pt = RemapCoords(x, y, z);
651 const Sprite *spr = GetSprite(image & SPRITE_MASK, ST_NORMAL);
652
653 if (pt.x + spr->x_offs >= _vd.dpi.left + _vd.dpi.width ||
654 pt.x + spr->x_offs + spr->width <= _vd.dpi.left ||
655 pt.y + spr->y_offs >= _vd.dpi.top + _vd.dpi.height ||
656 pt.y + spr->y_offs + spr->height <= _vd.dpi.top)
657 return;
658
659 const ParentSpriteToDraw *pstd = _vd.parent_sprites_to_draw.End() - 1;
660 AddChildSpriteScreen(image, pal, pt.x - pstd->left, pt.y - pstd->top, false, sub, false);
661 }
662
688 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)
689 {
690 int32 left, right, top, bottom;
691
692 assert((image & SPRITE_MASK) < MAX_SPRITES);
693
694 /* make the sprites transparent with the right palette */
695 if (transparent) {
696 SetBit(image, PALETTE_MODIFIER_TRANSPARENT);
697 pal = PALETTE_TO_TRANSPARENT;
698 }
699
700 if (_vd.combine_sprites == SPRITE_COMBINE_ACTIVE) {
701 AddCombinedSprite(image, pal, x, y, z, sub);
702 return;
703 }
704
705 _vd.last_child = NULL;
706
707 Point pt = RemapCoords(x, y, z);
708 int tmp_left, tmp_top, tmp_x = pt.x, tmp_y = pt.y;
709
710 /* Compute screen extents of sprite */
711 if (image == SPR_EMPTY_BOUNDING_BOX) {
712 left = tmp_left = RemapCoords(x + w , y + bb_offset_y, z + bb_offset_z).x;
713 right = RemapCoords(x + bb_offset_x, y + h , z + bb_offset_z).x + 1;
714 top = tmp_top = RemapCoords(x + bb_offset_x, y + bb_offset_y, z + dz ).y;
715 bottom = RemapCoords(x + w , y + h , z + bb_offset_z).y + 1;
716 } else {
717 const Sprite *spr = GetSprite(image & SPRITE_MASK, ST_NORMAL);
718 left = tmp_left = (pt.x += spr->x_offs);
719 right = (pt.x + spr->width );
720 top = tmp_top = (pt.y += spr->y_offs);
721 bottom = (pt.y + spr->height);
722 }
723
724 if (_draw_bounding_boxes && (image != SPR_EMPTY_BOUNDING_BOX)) {
725 /* Compute maximal extents of sprite and its bounding box */
726 left = min(left , RemapCoords(x + w , y + bb_offset_y, z + bb_offset_z).x);
727 right = max(right , RemapCoords(x + bb_offset_x, y + h , z + bb_offset_z).x + 1);
728 top = min(top , RemapCoords(x + bb_offset_x, y + bb_offset_y, z + dz ).y);
729 bottom = max(bottom, RemapCoords(x + w , y + h , z + bb_offset_z).y + 1);
730 }
731
732 /* Do not add the sprite to the viewport, if it is outside */
733 if (left >= _vd.dpi.left + _vd.dpi.width ||
734 right <= _vd.dpi.left ||
735 top >= _vd.dpi.top + _vd.dpi.height ||
736 bottom <= _vd.dpi.top) {
737 return;
738 }
739
740 ParentSpriteToDraw *ps = _vd.parent_sprites_to_draw.Append();
741 ps->x = tmp_x;
742 ps->y = tmp_y;
743
744 ps->left = tmp_left;
745 ps->top = tmp_top;
746
747 ps->image = image;
748 ps->pal = pal;
749 ps->sub = sub;
750 ps->xmin = x + bb_offset_x;
751 ps->xmax = x + max(bb_offset_x, w) - 1;
752
753 ps->ymin = y + bb_offset_y;
754 ps->ymax = y + max(bb_offset_y, h) - 1;
755
756 ps->zmin = z + bb_offset_z;
757 ps->zmax = z + max(bb_offset_z, dz) - 1;
758
759 ps->comparison_done = false;
760 ps->first_child = -1;
761
762 _vd.last_child = &ps->first_child;
763
764 if (_vd.combine_sprites == SPRITE_COMBINE_PENDING) _vd.combine_sprites = SPRITE_COMBINE_ACTIVE;
765 }
766
785 void StartSpriteCombine()
786 {
787 assert(_vd.combine_sprites == SPRITE_COMBINE_NONE);
788 _vd.combine_sprites = SPRITE_COMBINE_PENDING;
789 }
790
795 void EndSpriteCombine()
796 {
797 assert(_vd.combine_sprites != SPRITE_COMBINE_NONE);
798 _vd.combine_sprites = SPRITE_COMBINE_NONE;
799 }
800
810 static bool IsInRangeInclusive(int begin, int end, int check)
811 {
812 if (begin > end) Swap(begin, end);
813 return begin <= check && check <= end;
814 }
815
822 bool IsInsideRotatedRectangle(int x, int y)
823 {
824 int dist_a = (_thd.size.x + _thd.size.y); // Rotated coordinate system for selected rectangle.
825 int dist_b = (_thd.size.x - _thd.size.y); // We don't have to divide by 2. It's all relative!
826 int a = ((x - _thd.pos.x) + (y - _thd.pos.y)); // Rotated coordinate system for the point under scrutiny.
827 int b = ((x - _thd.pos.x) - (y - _thd.pos.y));
828
829 /* Check if a and b are between 0 and dist_a or dist_b respectively. */
830 return IsInRangeInclusive(dist_a, 0, a) && IsInRangeInclusive(dist_b, 0, b);
831 }
832
843 void AddChildSpriteScreen(SpriteID image, PaletteID pal, int x, int y, bool transparent, const SubSprite *sub, bool scale)
844 {
845 assert((image & SPRITE_MASK) < MAX_SPRITES);
846
847 /* If the ParentSprite was clipped by the viewport bounds, do not draw the ChildSprites either */
848 if (_vd.last_child == NULL) return;
849
850 /* make the sprites transparent with the right palette */
851 if (transparent) {
852 SetBit(image, PALETTE_MODIFIER_TRANSPARENT);
853 pal = PALETTE_TO_TRANSPARENT;
854 }
855
856 *_vd.last_child = _vd.child_screen_sprites_to_draw.Length();
857
858 ChildScreenSpriteToDraw *cs = _vd.child_screen_sprites_to_draw.Append();
859 cs->image = image;
860 cs->pal = pal;
861 cs->sub = sub;
862 cs->x = scale ? x * ZOOM_LVL_BASE : x;
863 cs->y = scale ? y * ZOOM_LVL_BASE : y;
864 cs->next = -1;
865
866 /* Append the sprite to the active ChildSprite list.
867 * If the active ParentSprite is a foundation, update last_foundation_child as well.
868 * Note: ChildSprites of foundations are NOT sequential in the vector, as selection sprites are added at last. */
869 if (_vd.last_foundation_child[0] == _vd.last_child) _vd.last_foundation_child[0] = &cs->next;
870 if (_vd.last_foundation_child[1] == _vd.last_child) _vd.last_foundation_child[1] = &cs->next;
871 _vd.last_child = &cs->next;
872 }
873
874 static void AddStringToDraw(int x, int y, StringID string, uint64 params_1, uint64 params_2, Colours colour, uint16 width)
875 {
876 assert(width != 0);
877 StringSpriteToDraw *ss = _vd.string_sprites_to_draw.Append();
878 ss->string = string;
879 ss->x = x;
880 ss->y = y;
881 ss->params[0] = params_1;
882 ss->params[1] = params_2;
883 ss->width = width;
884 ss->colour = colour;
885 }
886
887
899 static void DrawSelectionSprite(SpriteID image, PaletteID pal, const TileInfo *ti, int z_offset, FoundationPart foundation_part)
900 {
901 /* FIXME: This is not totally valid for some autorail highlights that extend over the edges of the tile. */
902 if (_vd.foundation[foundation_part] == -1) {
903 /* draw on real ground */
904 AddTileSpriteToDraw(image, pal, ti->x, ti->y, ti->z + z_offset);
905 } else {
906 /* draw on top of foundation */
907 AddChildSpriteToFoundation(image, pal, NULL, foundation_part, 0, -z_offset * ZOOM_LVL_BASE);
908 }
909 }
910
917 static void DrawTileSelectionRect(const TileInfo *ti, PaletteID pal)
918 {
919 if (!IsValidTile(ti->tile)) return;
920
921 SpriteID sel;
922 if (IsHalftileSlope(ti->tileh)) {
923 Corner halftile_corner = GetHalftileSlopeCorner(ti->tileh);
924 SpriteID sel2 = SPR_HALFTILE_SELECTION_FLAT + halftile_corner;
925 DrawSelectionSprite(sel2, pal, ti, 7 + TILE_HEIGHT, FOUNDATION_PART_HALFTILE);
926
927 Corner opposite_corner = OppositeCorner(halftile_corner);
928 if (IsSteepSlope(ti->tileh)) {
929 sel = SPR_HALFTILE_SELECTION_DOWN;
930 } else {
931 sel = ((ti->tileh & SlopeWithOneCornerRaised(opposite_corner)) != 0 ? SPR_HALFTILE_SELECTION_UP : SPR_HALFTILE_SELECTION_FLAT);
932 }
933 sel += opposite_corner;
934 } else {
935 sel = SPR_SELECT_TILE + SlopeToSpriteOffset(ti->tileh);
936 }
937 DrawSelectionSprite(sel, pal, ti, 7, FOUNDATION_PART_NORMAL);
938 }
939
940 static bool IsPartOfAutoLine(int px, int py)
941 {
942 px -= _thd.selstart.x;
943 py -= _thd.selstart.y;
944
945 if ((_thd.drawstyle & HT_DRAG_MASK) != HT_LINE) return false;
946
947 switch (_thd.drawstyle & HT_DIR_MASK) {
948 case HT_DIR_X: return py == 0; // x direction
949 case HT_DIR_Y: return px == 0; // y direction
950 case HT_DIR_HU: return px == -py || px == -py - 16; // horizontal upper
951 case HT_DIR_HL: return px == -py || px == -py + 16; // horizontal lower
952 case HT_DIR_VL: return px == py || px == py + 16; // vertical left
953 case HT_DIR_VR: return px == py || px == py - 16; // vertical right
954 default:
955 NOT_REACHED();
956 }
957 }
958
959 /* [direction][side] */
960 static const HighLightStyle _autorail_type[6][2] = {
961 { HT_DIR_X, HT_DIR_X },
962 { HT_DIR_Y, HT_DIR_Y },
963 { HT_DIR_HU, HT_DIR_HL },
964 { HT_DIR_HL, HT_DIR_HU },
965 { HT_DIR_VL, HT_DIR_VR },
966 { HT_DIR_VR, HT_DIR_VL }
967 };
968
969 #include "table/autorail.h"
970
977 static void DrawAutorailSelection(const TileInfo *ti, uint autorail_type)
978 {
979 SpriteID image;
980 PaletteID pal;
981 int offset;
982
983 FoundationPart foundation_part = FOUNDATION_PART_NORMAL;
984 Slope autorail_tileh = RemoveHalftileSlope(ti->tileh);
985 if (IsHalftileSlope(ti->tileh)) {
986 static const uint _lower_rail[4] = { 5U, 2U, 4U, 3U };
987 Corner halftile_corner = GetHalftileSlopeCorner(ti->tileh);
988 if (autorail_type != _lower_rail[halftile_corner]) {
989 foundation_part = FOUNDATION_PART_HALFTILE;
990 /* Here we draw the highlights of the "three-corners-raised"-slope. That looks ok to me. */
991 autorail_tileh = SlopeWithThreeCornersRaised(OppositeCorner(halftile_corner));
992 }
993 }
994
995 offset = _AutorailTilehSprite[autorail_tileh][autorail_type];
996 if (offset >= 0) {
997 image = SPR_AUTORAIL_BASE + offset;
998 pal = PAL_NONE;
999 } else {
1000 image = SPR_AUTORAIL_BASE - offset;
1001 pal = PALETTE_SEL_TILE_RED;
1002 }
1003
1004 DrawSelectionSprite(image, _thd.make_square_red ? PALETTE_SEL_TILE_RED : pal, ti, 7, foundation_part);
1005 }
1006
1011 static void DrawTileSelection(const TileInfo *ti)
1012 {
1013 /* Draw a red error square? */
1014 bool is_redsq = _thd.redsq == ti->tile;
1015 if (is_redsq) DrawTileSelectionRect(ti, PALETTE_TILE_RED_PULSATING);
1016
1017 /* No tile selection active? */
1018 if ((_thd.drawstyle & HT_DRAG_MASK) == HT_NONE) return;
1019
1020 if (_thd.diagonal) { // We're drawing a 45 degrees rotated (diagonal) rectangle
1021 if (IsInsideRotatedRectangle((int)ti->x, (int)ti->y)) goto draw_inner;
1022 return;
1023 }
1024
1025 /* Inside the inner area? */
1026 if (IsInsideBS(ti->x, _thd.pos.x, _thd.size.x) &&
1027 IsInsideBS(ti->y, _thd.pos.y, _thd.size.y)) {
1028 draw_inner:
1029 if (_thd.drawstyle & HT_RECT) {
1030 if (!is_redsq) DrawTileSelectionRect(ti, _thd.make_square_red ? PALETTE_SEL_TILE_RED : PAL_NONE);
1031 } else if (_thd.drawstyle & HT_POINT) {
1032 /* Figure out the Z coordinate for the single dot. */
1033 int z = 0;
1034 FoundationPart foundation_part = FOUNDATION_PART_NORMAL;
1035 if (ti->tileh & SLOPE_N) {
1036 z += TILE_HEIGHT;
1037 if (RemoveHalftileSlope(ti->tileh) == SLOPE_STEEP_N) z += TILE_HEIGHT;
1038 }
1039 if (IsHalftileSlope(ti->tileh)) {
1040 Corner halftile_corner = GetHalftileSlopeCorner(ti->tileh);
1041 if ((halftile_corner == CORNER_W) || (halftile_corner == CORNER_E)) z += TILE_HEIGHT;
1042 if (halftile_corner != CORNER_S) {
1043 foundation_part = FOUNDATION_PART_HALFTILE;
1044 if (IsSteepSlope(ti->tileh)) z -= TILE_HEIGHT;
1045 }
1046 }
1047 DrawSelectionSprite(_cur_dpi->zoom <= ZOOM_LVL_DETAIL ? SPR_DOT : SPR_DOT_SMALL, PAL_NONE, ti, z, foundation_part);
1048 } else if (_thd.drawstyle & HT_RAIL) {
1049 /* autorail highlight piece under cursor */
1050 HighLightStyle type = _thd.drawstyle & HT_DIR_MASK;
1051 assert(type < HT_DIR_END);
1052 DrawAutorailSelection(ti, _autorail_type[type][0]);
1053 } else if (IsPartOfAutoLine(ti->x, ti->y)) {
1054 /* autorail highlighting long line */
1055 HighLightStyle dir = _thd.drawstyle & HT_DIR_MASK;
1056 uint side;
1057
1058 if (dir == HT_DIR_X || dir == HT_DIR_Y) {
1059 side = 0;
1060 } else {
1061 TileIndex start = TileVirtXY(_thd.selstart.x, _thd.selstart.y);
1062 side = Delta(Delta(TileX(start), TileX(ti->tile)), Delta(TileY(start), TileY(ti->tile)));
1063 }
1064
1065 DrawAutorailSelection(ti, _autorail_type[dir][side]);
1066 }
1067 return;
1068 }
1069
1070 /* Check if it's inside the outer area? */
1071 if (!is_redsq && _thd.outersize.x > 0 &&
1072 IsInsideBS(ti->x, _thd.pos.x + _thd.offs.x, _thd.size.x + _thd.outersize.x) &&
1073 IsInsideBS(ti->y, _thd.pos.y + _thd.offs.y, _thd.size.y + _thd.outersize.y)) {
1074 /* Draw a blue rect. */
1075 DrawTileSelectionRect(ti, PALETTE_SEL_TILE_BLUE);
1076 return;
1077 }
1078 }
1079
1086 static int GetViewportY(Point tile)
1087 {
1088 /* Each increment in X or Y direction moves down by half a tile, i.e. TILE_PIXELS / 2. */
1089 return (tile.y * (int)(TILE_PIXELS / 2) + tile.x * (int)(TILE_PIXELS / 2) - TilePixelHeightOutsideMap(tile.x, tile.y)) << ZOOM_LVL_SHIFT;
1090 }
1091
1095 static void ViewportAddLandscape()
1096 {
1097 assert(_vd.dpi.top <= _vd.dpi.top + _vd.dpi.height);
1098 assert(_vd.dpi.left <= _vd.dpi.left + _vd.dpi.width);
1099
1100 Point upper_left = InverseRemapCoords(_vd.dpi.left, _vd.dpi.top);
1101 Point upper_right = InverseRemapCoords(_vd.dpi.left + _vd.dpi.width, _vd.dpi.top);
1102
1103 /* Transformations between tile coordinates and viewport rows/columns: See vp_column_row
1104 * column = y - x
1105 * row = x + y
1106 * x = (row - column) / 2
1107 * y = (row + column) / 2
1108 * Note: (row, columns) pairs are only valid, if they are both even or both odd.
1109 */
1110
1111 /* Columns overlap with neighbouring columns by a half tile.
1112 * - Left column is column of upper_left (rounded down) and one column to the left.
1113 * - Right column is column of upper_right (rounded up) and one column to the right.
1114 * Note: Integer-division does not round down for negative numbers, so ensure rounding with another increment/decrement.
1115 */
1116 int left_column = (upper_left.y - upper_left.x) / (int)TILE_SIZE - 2;
1117 int right_column = (upper_right.y - upper_right.x) / (int)TILE_SIZE + 2;
1118
1119 int potential_bridge_height = ZOOM_LVL_BASE * TILE_HEIGHT * _settings_game.construction.max_bridge_height;
1120
1121 /* Rows overlap with neighbouring rows by a half tile.
1122 * The first row that could possibly be visible is the row above upper_left (if it is at height 0).
1123 * Due to integer-division not rounding down for negative numbers, we need another decrement.
1124 */
1125 int row = (upper_left.x + upper_left.y) / (int)TILE_SIZE - 2;
1126 bool last_row = false;
1127 for (; !last_row; row++) {
1128 last_row = true;
1129 for (int column = left_column; column <= right_column; column++) {
1130 /* Valid row/column? */
1131 if ((row + column) % 2 != 0) continue;
1132
1133 Point tilecoord;
1134 tilecoord.x = (row - column) / 2;
1135 tilecoord.y = (row + column) / 2;
1136 assert(column == tilecoord.y - tilecoord.x);
1137 assert(row == tilecoord.y + tilecoord.x);
1138
1139 TileType tile_type;
1140 TileInfo tile_info;
1141 _cur_ti = &tile_info;
1142 tile_info.x = tilecoord.x * TILE_SIZE; // FIXME tile_info should use signed integers
1143 tile_info.y = tilecoord.y * TILE_SIZE;
1144
1145 if (IsInsideBS(tilecoord.x, 0, MapSizeX()) && IsInsideBS(tilecoord.y, 0, MapSizeY())) {
1146 /* This includes the south border at MapMaxX / MapMaxY. When terraforming we still draw tile selections there. */
1147 tile_info.tile = TileXY(tilecoord.x, tilecoord.y);
1148 tile_type = GetTileType(tile_info.tile);
1149 } else {
1150 tile_info.tile = INVALID_TILE;
1151 tile_type = MP_VOID;
1152 }
1153
1154 if (tile_type != MP_VOID) {
1155 /* We are inside the map => paint landscape. */
1156 tile_info.tileh = GetTilePixelSlope(tile_info.tile, &tile_info.z);
1157 } else {
1158 /* We are outside the map => paint black. */
1159 tile_info.tileh = GetTilePixelSlopeOutsideMap(tilecoord.x, tilecoord.y, &tile_info.z);
1160 }
1161
1162 int viewport_y = GetViewportY(tilecoord);
1163
1164 if (viewport_y + MAX_TILE_EXTENT_BOTTOM < _vd.dpi.top) {
1165 /* The tile in this column is not visible yet.
1166 * Tiles in other columns may be visible, but we need more rows in any case. */
1167 last_row = false;
1168 continue;
1169 }
1170
1171 int min_visible_height = viewport_y - (_vd.dpi.top + _vd.dpi.height);
1172 bool tile_visible = min_visible_height <= 0;
1173
1174 if (tile_type != MP_VOID) {
1175 /* Is tile with buildings visible? */
1176 if (min_visible_height < MAX_TILE_EXTENT_TOP) tile_visible = true;
1177
1178 if (IsBridgeAbove(tile_info.tile)) {
1179 /* Is the bridge visible? */
1180 TileIndex bridge_tile = GetNorthernBridgeEnd(tile_info.tile);
1181 int bridge_height = ZOOM_LVL_BASE * (GetBridgePixelHeight(bridge_tile) - TilePixelHeight(tile_info.tile));
1182 if (min_visible_height < bridge_height + MAX_TILE_EXTENT_TOP) tile_visible = true;
1183 }
1184
1185 /* Would a higher bridge on a more southern tile be visible?
1186 * If yes, we need to loop over more rows to possibly find one. */
1187 if (min_visible_height < potential_bridge_height + MAX_TILE_EXTENT_TOP) last_row = false;
1188 } else {
1189 /* Outside of map. If we are on the north border of the map, there may still be a bridge visible,
1190 * so we need to loop over more rows to possibly find one. */
1191 if ((tilecoord.x <= 0 || tilecoord.y <= 0) && min_visible_height < potential_bridge_height + MAX_TILE_EXTENT_TOP) last_row = false;
1192 }
1193
1194 if (tile_visible) {
1195 last_row = false;
1196 _vd.foundation_part = FOUNDATION_PART_NONE;
1197 _vd.foundation[0] = -1;
1198 _vd.foundation[1] = -1;
1199 _vd.last_foundation_child[0] = NULL;
1200 _vd.last_foundation_child[1] = NULL;
1201
1202 _tile_type_procs[tile_type]->draw_tile_proc(&tile_info);
1203 if (tile_info.tile != INVALID_TILE) DrawTileSelection(&tile_info);
1204 }
1205 }
1206 }
1207 }
1208
1219 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)
1220 {
1221 bool small = dpi->zoom >= small_from;
1222
1223 int left = dpi->left;
1224 int top = dpi->top;
1225 int right = left + dpi->width;
1226 int bottom = top + dpi->height;
1227
1228 int sign_height = ScaleByZoom(VPSM_TOP + FONT_HEIGHT_NORMAL + VPSM_BOTTOM, dpi->zoom);
1229 int sign_half_width = ScaleByZoom((small ? sign->width_small : sign->width_normal) / 2, dpi->zoom);
1230
1231 if (bottom < sign->top ||
1232 top > sign->top + sign_height ||
1233 right < sign->center - sign_half_width ||
1234 left > sign->center + sign_half_width) {
1235 return;
1236 }
1237
1238 if (!small) {
1239 AddStringToDraw(sign->center - sign_half_width, sign->top, string_normal, params_1, params_2, colour, sign->width_normal);
1240 } else {
1241 int shadow_offset = 0;
1242 if (string_small_shadow != STR_NULL) {
1243 shadow_offset = 4;
1244 AddStringToDraw(sign->center - sign_half_width + shadow_offset, sign->top, string_small_shadow, params_1, params_2, INVALID_COLOUR, sign->width_small);
1245 }
1246 AddStringToDraw(sign->center - sign_half_width, sign->top - shadow_offset, string_small, params_1, params_2,
1247 colour, sign->width_small | 0x8000);
1248 }
1249 }
1250
1251 static void ViewportAddTownNames(DrawPixelInfo *dpi)
1252 {
1253 if (!HasBit(_display_opt, DO_SHOW_TOWN_NAMES) || _game_mode == GM_MENU) return;
1254
1255 const Town *t;
1256 FOR_ALL_TOWNS(t) {
1257 ViewportAddString(dpi, ZOOM_LVL_OUT_16X, &t->cache.sign,
1258 _settings_client.gui.population_in_label ? STR_VIEWPORT_TOWN_POP : STR_VIEWPORT_TOWN,
1259 STR_VIEWPORT_TOWN_TINY_WHITE, STR_VIEWPORT_TOWN_TINY_BLACK,
1260 t->index, t->cache.population);
1261 }
1262 }
1263
1264
1265 static void ViewportAddStationNames(DrawPixelInfo *dpi)
1266 {
1267 if (!(HasBit(_display_opt, DO_SHOW_STATION_NAMES) || HasBit(_display_opt, DO_SHOW_WAYPOINT_NAMES)) || _game_mode == GM_MENU) return;
1268
1269 const BaseStation *st;
1270 FOR_ALL_BASE_STATIONS(st) {
1271 /* Check whether the base station is a station or a waypoint */
1272 bool is_station = Station::IsExpected(st);
1273
1274 /* Don't draw if the display options are disabled */
1275 if (!HasBit(_display_opt, is_station ? DO_SHOW_STATION_NAMES : DO_SHOW_WAYPOINT_NAMES)) continue;
1276
1277 /* Don't draw if station is owned by another company and competitor station names are hidden. Stations owned by none are never ignored. */
1278 if (!HasBit(_display_opt, DO_SHOW_COMPETITOR_SIGNS) && _local_company != st->owner && st->owner != OWNER_NONE) continue;
1279
1280 ViewportAddString(dpi, ZOOM_LVL_OUT_16X, &st->sign,
1281 is_station ? STR_VIEWPORT_STATION : STR_VIEWPORT_WAYPOINT,
1282 (is_station ? STR_VIEWPORT_STATION : STR_VIEWPORT_WAYPOINT) + 1, STR_NULL,
1283 st->index, st->facilities, (st->owner == OWNER_NONE || !st->IsInUse()) ? COLOUR_GREY : _company_colours[st->owner]);
1284 }
1285 }
1286
1287
1288 static void ViewportAddSigns(DrawPixelInfo *dpi)
1289 {
1290 /* Signs are turned off or are invisible */
1291 if (!HasBit(_display_opt, DO_SHOW_SIGNS) || IsInvisibilitySet(TO_SIGNS)) return;
1292
1293 const Sign *si;
1294 FOR_ALL_SIGNS(si) {
1295 /* Don't draw if sign is owned by another company and competitor signs should be hidden.
1296 * Note: It is intentional that also signs owned by OWNER_NONE are hidden. Bankrupt
1297 * companies can leave OWNER_NONE signs after them. */
1298 if (!HasBit(_display_opt, DO_SHOW_COMPETITOR_SIGNS) && _local_company != si->owner && si->owner != OWNER_DEITY) continue;
1299
1300 ViewportAddString(dpi, ZOOM_LVL_OUT_16X, &si->sign,
1301 STR_WHITE_SIGN,
1302 (IsTransparencySet(TO_SIGNS) || si->owner == OWNER_DEITY) ? STR_VIEWPORT_SIGN_SMALL_WHITE : STR_VIEWPORT_SIGN_SMALL_BLACK, STR_NULL,
1303 si->index, 0, (si->owner == OWNER_NONE) ? COLOUR_GREY : (si->owner == OWNER_DEITY ? INVALID_COLOUR : _company_colours[si->owner]));
1304 }
1305 }
1306
1313 void ViewportSign::UpdatePosition(int center, int top, StringID str)
1314 {
1315 if (this->width_normal != 0) this->MarkDirty();
1316
1317 this->top = top;
1318
1319 char buffer[DRAW_STRING_BUFFER];
1320
1321 GetString(buffer, str, lastof(buffer));
1322 this->width_normal = VPSM_LEFT + Align(GetStringBoundingBox(buffer).width, 2) + VPSM_RIGHT;
1323 this->center = center;
1324
1325 /* zoomed out version */
1326 this->width_small = VPSM_LEFT + Align(GetStringBoundingBox(buffer, FS_SMALL).width, 2) + VPSM_RIGHT;
1327
1328 this->MarkDirty();
1329 }
1330
1337 void ViewportSign::MarkDirty(ZoomLevel maxzoom) const
1338 {
1339 Rect zoomlevels[ZOOM_LVL_COUNT];
1340
1341 for (ZoomLevel zoom = ZOOM_LVL_BEGIN; zoom != ZOOM_LVL_END; zoom++) {
1342 /* FIXME: This doesn't switch to width_small when appropriate. */
1343 zoomlevels[zoom].left = this->center - ScaleByZoom(this->width_normal / 2 + 1, zoom);
1344 zoomlevels[zoom].top = this->top - ScaleByZoom(1, zoom);
1345 zoomlevels[zoom].right = this->center + ScaleByZoom(this->width_normal / 2 + 1, zoom);
1346 zoomlevels[zoom].bottom = this->top + ScaleByZoom(VPSM_TOP + FONT_HEIGHT_NORMAL + VPSM_BOTTOM + 1, zoom);
1347 }
1348
1349 Window *w;
1350 FOR_ALL_WINDOWS_FROM_BACK(w) {
1351 ViewPort *vp = w->viewport;
1352 if (vp != NULL && vp->zoom <= maxzoom) {
1353 assert(vp->width != 0);
1354 Rect &zl = zoomlevels[vp->zoom];
1355 MarkViewportDirty(vp, zl.left, zl.top, zl.right, zl.bottom);
1356 }
1357 }
1358 }
1359
1360 static void ViewportDrawTileSprites(const TileSpriteToDrawVector *tstdv)
1361 {
1362 const TileSpriteToDraw *tsend = tstdv->End();
1363 for (const TileSpriteToDraw *ts = tstdv->Begin(); ts != tsend; ++ts) {
1364 DrawSpriteViewport(ts->image, ts->pal, ts->x, ts->y, ts->sub);
1365 }
1366 }
1367
1369 static bool ViewportSortParentSpritesChecker()
1370 {
1371 return true;
1372 }
1373
1375 static void ViewportSortParentSprites(ParentSpriteToSortVector *psdv)
1376 {
1377 ParentSpriteToDraw **psdvend = psdv->End();
1378 ParentSpriteToDraw **psd = psdv->Begin();
1379 while (psd != psdvend) {
1380 ParentSpriteToDraw *ps = *psd;
1381
1382 if (ps->comparison_done) {
1383 psd++;
1384 continue;
1385 }
1386
1387 ps->comparison_done = true;
1388
1389 for (ParentSpriteToDraw **psd2 = psd + 1; psd2 != psdvend; psd2++) {
1390 ParentSpriteToDraw *ps2 = *psd2;
1391
1392 if (ps2->comparison_done) continue;
1393
1394 /* Decide which comparator to use, based on whether the bounding
1395 * boxes overlap
1396 */
1397 if (ps->xmax >= ps2->xmin && ps->xmin <= ps2->xmax && // overlap in X?
1398 ps->ymax >= ps2->ymin && ps->ymin <= ps2->ymax && // overlap in Y?
1399 ps->zmax >= ps2->zmin && ps->zmin <= ps2->zmax) { // overlap in Z?
1400 /* Use X+Y+Z as the sorting order, so sprites closer to the bottom of
1401 * the screen and with higher Z elevation, are drawn in front.
1402 * Here X,Y,Z are the coordinates of the "center of mass" of the sprite,
1403 * i.e. X=(left+right)/2, etc.
1404 * However, since we only care about order, don't actually divide / 2
1405 */
1406 if (ps->xmin + ps->xmax + ps->ymin + ps->ymax + ps->zmin + ps->zmax <=
1407 ps2->xmin + ps2->xmax + ps2->ymin + ps2->ymax + ps2->zmin + ps2->zmax) {
1408 continue;
1409 }
1410 } else {
1411 /* We only change the order, if it is definite.
1412 * I.e. every single order of X, Y, Z says ps2 is behind ps or they overlap.
1413 * That is: If one partial order says ps behind ps2, do not change the order.
1414 */
1415 if (ps->xmax < ps2->xmin ||
1416 ps->ymax < ps2->ymin ||
1417 ps->zmax < ps2->zmin) {
1418 continue;
1419 }
1420 }
1421
1422 /* Move ps2 in front of ps */
1423 ParentSpriteToDraw *temp = ps2;
1424 for (ParentSpriteToDraw **psd3 = psd2; psd3 > psd; psd3--) {
1425 *psd3 = *(psd3 - 1);
1426 }
1427 *psd = temp;
1428 }
1429 }
1430 }
1431
1432 static void ViewportDrawParentSprites(const ParentSpriteToSortVector *psd, const ChildScreenSpriteToDrawVector *csstdv)
1433 {
1434 const ParentSpriteToDraw * const *psd_end = psd->End();
1435 for (const ParentSpriteToDraw * const *it = psd->Begin(); it != psd_end; it++) {
1436 const ParentSpriteToDraw *ps = *it;
1437 if (ps->image != SPR_EMPTY_BOUNDING_BOX) DrawSpriteViewport(ps->image, ps->pal, ps->x, ps->y, ps->sub);
1438
1439 int child_idx = ps->first_child;
1440 while (child_idx >= 0) {
1441 const ChildScreenSpriteToDraw *cs = csstdv->Get(child_idx);
1442 child_idx = cs->next;
1443 DrawSpriteViewport(cs->image, cs->pal, ps->left + cs->x, ps->top + cs->y, cs->sub);
1444 }
1445 }
1446 }
1447
1452 static void ViewportDrawBoundingBoxes(const ParentSpriteToSortVector *psd)
1453 {
1454 const ParentSpriteToDraw * const *psd_end = psd->End();
1455 for (const ParentSpriteToDraw * const *it = psd->Begin(); it != psd_end; it++) {
1456 const ParentSpriteToDraw *ps = *it;
1457 Point pt1 = RemapCoords(ps->xmax + 1, ps->ymax + 1, ps->zmax + 1); // top front corner
1458 Point pt2 = RemapCoords(ps->xmin , ps->ymax + 1, ps->zmax + 1); // top left corner
1459 Point pt3 = RemapCoords(ps->xmax + 1, ps->ymin , ps->zmax + 1); // top right corner
1460 Point pt4 = RemapCoords(ps->xmax + 1, ps->ymax + 1, ps->zmin ); // bottom front corner
1461
1462 DrawBox( pt1.x, pt1.y,
1463 pt2.x - pt1.x, pt2.y - pt1.y,
1464 pt3.x - pt1.x, pt3.y - pt1.y,
1465 pt4.x - pt1.x, pt4.y - pt1.y);
1466 }
1467 }
1468
1472 static void ViewportDrawDirtyBlocks()
1473 {
1474 Blitter *blitter = BlitterFactory::GetCurrentBlitter();
1475 const DrawPixelInfo *dpi = _cur_dpi;
1476 void *dst;
1477 int right = UnScaleByZoom(dpi->width, dpi->zoom);
1478 int bottom = UnScaleByZoom(dpi->height, dpi->zoom);
1479
1480 int colour = _string_colourmap[_dirty_block_colour & 0xF];
1481
1482 dst = dpi->dst_ptr;
1483
1484 byte bo = UnScaleByZoom(dpi->left + dpi->top, dpi->zoom) & 1;
1485 do {
1486 for (int i = (bo ^= 1); i < right; i += 2) blitter->SetPixel(dst, i, 0, (uint8)colour);
1487 dst = blitter->MoveTo(dst, 0, 1);
1488 } while (--bottom > 0);
1489 }
1490
1491 static void ViewportDrawStrings(ZoomLevel zoom, const StringSpriteToDrawVector *sstdv)
1492 {
1493 const StringSpriteToDraw *ssend = sstdv->End();
1494 for (const StringSpriteToDraw *ss = sstdv->Begin(); ss != ssend; ++ss) {
1495 TextColour colour = TC_BLACK;
1496 bool small = HasBit(ss->width, 15);
1497 int w = GB(ss->width, 0, 15);
1498 int x = UnScaleByZoom(ss->x, zoom);
1499 int y = UnScaleByZoom(ss->y, zoom);
1500 int h = VPSM_TOP + (small ? FONT_HEIGHT_SMALL : FONT_HEIGHT_NORMAL) + VPSM_BOTTOM;
1501
1502 SetDParam(0, ss->params[0]);
1503 SetDParam(1, ss->params[1]);
1504
1505 if (ss->colour != INVALID_COLOUR) {
1506 /* Do not draw signs nor station names if they are set invisible */
1507 if (IsInvisibilitySet(TO_SIGNS) && ss->string != STR_WHITE_SIGN) continue;
1508
1509 if (IsTransparencySet(TO_SIGNS) && ss->string != STR_WHITE_SIGN) {
1510 /* Don't draw the rectangle.
1511 * Real colours need the TC_IS_PALETTE_COLOUR flag.
1512 * Otherwise colours from _string_colourmap are assumed. */
1513 colour = (TextColour)_colour_gradient[ss->colour][6] | TC_IS_PALETTE_COLOUR;
1514 } else {
1515 /* Draw the rectangle if 'transparent station signs' is off,
1516 * or if we are drawing a general text sign (STR_WHITE_SIGN). */
1517 DrawFrameRect(
1518 x, y, x + w, y + h, ss->colour,
1519 IsTransparencySet(TO_SIGNS) ? FR_TRANSPARENT : FR_NONE
1520 );
1521 }
1522 }
1523
1524 DrawString(x + VPSM_LEFT, x + w - 1 - VPSM_RIGHT, y + VPSM_TOP, ss->string, colour, SA_HOR_CENTER);
1525 }
1526 }
1527
1528 void ViewportDoDraw(const ViewPort *vp, int left, int top, int right, int bottom)
1529 {
1530 DrawPixelInfo *old_dpi = _cur_dpi;
1531 _cur_dpi = &_vd.dpi;
1532
1533 _vd.dpi.zoom = vp->zoom;
1534 int mask = ScaleByZoom(-1, vp->zoom);
1535
1536 _vd.combine_sprites = SPRITE_COMBINE_NONE;
1537
1538 _vd.dpi.width = (right - left) & mask;
1539 _vd.dpi.height = (bottom - top) & mask;
1540 _vd.dpi.left = left & mask;
1541 _vd.dpi.top = top & mask;
1542 _vd.dpi.pitch = old_dpi->pitch;
1543 _vd.last_child = NULL;
1544
1545 int x = UnScaleByZoom(_vd.dpi.left - (vp->virtual_left & mask), vp->zoom) + vp->left;
1546 int y = UnScaleByZoom(_vd.dpi.top - (vp->virtual_top & mask), vp->zoom) + vp->top;
1547
1548 _vd.dpi.dst_ptr = BlitterFactory::GetCurrentBlitter()->MoveTo(old_dpi->dst_ptr, x - old_dpi->left, y - old_dpi->top);
1549
1550 ViewportAddLandscape();
1551 ViewportAddVehicles(&_vd.dpi);
1552
1553 ViewportAddTownNames(&_vd.dpi);
1554 ViewportAddStationNames(&_vd.dpi);
1555 ViewportAddSigns(&_vd.dpi);
1556
1557 DrawTextEffects(&_vd.dpi);
1558
1559 if (_vd.tile_sprites_to_draw.Length() != 0) ViewportDrawTileSprites(&_vd.tile_sprites_to_draw);
1560
1561 ParentSpriteToDraw *psd_end = _vd.parent_sprites_to_draw.End();
1562 for (ParentSpriteToDraw *it = _vd.parent_sprites_to_draw.Begin(); it != psd_end; it++) {
1563 *_vd.parent_sprites_to_sort.Append() = it;
1564 }
1565
1566 _vp_sprite_sorter(&_vd.parent_sprites_to_sort);
1567 ViewportDrawParentSprites(&_vd.parent_sprites_to_sort, &_vd.child_screen_sprites_to_draw);
1568
1569 if (_draw_bounding_boxes) ViewportDrawBoundingBoxes(&_vd.parent_sprites_to_sort);
1570 if (_draw_dirty_blocks) ViewportDrawDirtyBlocks();
1571
1572 DrawPixelInfo dp = _vd.dpi;
1573 ZoomLevel zoom = _vd.dpi.zoom;
1574 dp.zoom = ZOOM_LVL_NORMAL;
1575 dp.width = UnScaleByZoom(dp.width, zoom);
1576 dp.height = UnScaleByZoom(dp.height, zoom);
1577 _cur_dpi = &dp;
1578
1579 if (vp->overlay != NULL && vp->overlay->GetCargoMask() != 0 && vp->overlay->GetCompanyMask() != 0) {
1580 /* translate to window coordinates */
1581 dp.left = x;
1582 dp.top = y;
1583 vp->overlay->Draw(&dp);
1584 }
1585
1586 if (_vd.string_sprites_to_draw.Length() != 0) {
1587 /* translate to world coordinates */
1588 dp.left = UnScaleByZoom(_vd.dpi.left, zoom);
1589 dp.top = UnScaleByZoom(_vd.dpi.top, zoom);
1590 ViewportDrawStrings(zoom, &_vd.string_sprites_to_draw);
1591 }
1592
1593 _cur_dpi = old_dpi;
1594
1595 _vd.string_sprites_to_draw.Clear();
1596 _vd.tile_sprites_to_draw.Clear();
1597 _vd.parent_sprites_to_draw.Clear();
1598 _vd.parent_sprites_to_sort.Clear();
1599 _vd.child_screen_sprites_to_draw.Clear();
1600 }
1601
1606 static void ViewportDrawChk(const ViewPort *vp, int left, int top, int right, int bottom)
1607 {
1608 if (ScaleByZoom(bottom - top, vp->zoom) * ScaleByZoom(right - left, vp->zoom) > 180000 * ZOOM_LVL_BASE * ZOOM_LVL_BASE) {
1609 if ((bottom - top) > (right - left)) {
1610 int t = (top + bottom) >> 1;
1611 ViewportDrawChk(vp, left, top, right, t);
1612 ViewportDrawChk(vp, left, t, right, bottom);
1613 } else {
1614 int t = (left + right) >> 1;
1615 ViewportDrawChk(vp, left, top, t, bottom);
1616 ViewportDrawChk(vp, t, top, right, bottom);
1617 }
1618 } else {
1619 ViewportDoDraw(vp,
1620 ScaleByZoom(left - vp->left, vp->zoom) + vp->virtual_left,
1621 ScaleByZoom(top - vp->top, vp->zoom) + vp->virtual_top,
1622 ScaleByZoom(right - vp->left, vp->zoom) + vp->virtual_left,
1623 ScaleByZoom(bottom - vp->top, vp->zoom) + vp->virtual_top
1624 );
1625 }
1626 }
1627
1628 static inline void ViewportDraw(const ViewPort *vp, int left, int top, int right, int bottom)
1629 {
1630 if (right <= vp->left || bottom <= vp->top) return;
1631
1632 if (left >= vp->left + vp->width) return;
1633
1634 if (left < vp->left) left = vp->left;
1635 if (right > vp->left + vp->width) right = vp->left + vp->width;
1636
1637 if (top >= vp->top + vp->height) return;
1638
1639 if (top < vp->top) top = vp->top;
1640 if (bottom > vp->top + vp->height) bottom = vp->top + vp->height;
1641
1642 ViewportDrawChk(vp, left, top, right, bottom);
1643 }
1644
1648 void Window::DrawViewport() const
1649 {
1650 DrawPixelInfo *dpi = _cur_dpi;
1651
1652 dpi->left += this->left;
1653 dpi->top += this->top;
1654
1655 ViewportDraw(this->viewport, dpi->left, dpi->top, dpi->left + dpi->width, dpi->top + dpi->height);
1656
1657 dpi->left -= this->left;
1658 dpi->top -= this->top;
1659 }
1660
1669 typedef bool ContinueMapEdgeSearch(int iter, int iter_limit, int sy, int sy_limit);
1670
1672 static inline bool ContinueLowerMapEdgeSearch(int iter, int iter_limit, int sy, int sy_limit) { return iter > 0 && sy > sy_limit; }
1674 static inline bool ContinueUpperMapEdgeSearch(int iter, int iter_limit, int sy, int sy_limit) { return iter < iter_limit && sy < sy_limit; }
1675
1689 static int SearchMapEdge(Point &curr_tile, int &iter, int iter_limit, int offset, int sy_limit, ContinueMapEdgeSearch continue_criteria)
1690 {
1691 int sy;
1692 do {
1693 iter = Clamp(iter + offset, 0, iter_limit);
1694 sy = GetViewportY(curr_tile);
1695 } while (continue_criteria(iter, iter_limit, sy, sy_limit));
1696
1697 return iter;
1698 }
1699
1714 static inline int ClampXYToMap(Point &curr_tile, int &iter, int iter_limit, int start, int &other_ref, int other_value, int vp_value, int other_limit, int vp_top, int vp_bottom)
1715 {
1716 bool upper_edge = other_value < _settings_game.construction.max_heightlevel / 4;
1717
1718 /*
1719 * First get an estimate of the tiles relevant for us at that edge. Relevant in the sense
1720 * "at least close to the visible area". Thus, we don't look at exactly each tile, inspecting
1721 * e.g. every tenth should be enough. After all, the desired screen limit is set such that
1722 * the bordermost tiles are painted in the middle of the screen when one hits the limit,
1723 * i.e. it is no harm if there is some small error in that calculation
1724 */
1725
1726 other_ref = upper_edge ? 0 : other_limit;
1727 iter = start;
1728 int min_iter = SearchMapEdge(curr_tile, iter, iter_limit, upper_edge ? -10 : +10, vp_top, upper_edge ? ContinueLowerMapEdgeSearch : ContinueUpperMapEdgeSearch);
1729 iter = start;
1730 int max_iter = SearchMapEdge(curr_tile, iter, iter_limit, upper_edge ? +10 : -10, vp_bottom, upper_edge ? ContinueUpperMapEdgeSearch : ContinueLowerMapEdgeSearch);
1731
1732 max_iter = min(max_iter + _settings_game.construction.max_heightlevel / 4, iter_limit);
1733 min_iter = min(min_iter, max_iter);
1734
1735 /* Now, calculate the highest heightlevel of these tiles. Again just as an estimate. */
1736 int max_heightlevel_at_edge = 0;
1737 for (iter = min_iter; iter <= max_iter; iter += 10) {
1738 max_heightlevel_at_edge = max(max_heightlevel_at_edge, (int)TileHeight(TileXY(curr_tile.x, curr_tile.y)));
1739 }
1740
1741 /* Based on that heightlevel, calculate the limit. For the upper edge a tile with height zero would
1742 * get a limit of zero, on the other side it depends on the number of tiles along the axis. */
1743 return upper_edge ?
1744 max(vp_value, -max_heightlevel_at_edge * (int)(TILE_HEIGHT * 2 * ZOOM_LVL_BASE)) :
1745 min(vp_value, (other_limit * TILE_SIZE * 4 - max_heightlevel_at_edge * TILE_HEIGHT * 2) * ZOOM_LVL_BASE);
1746 }
1747
1748 static inline void ClampViewportToMap(const ViewPort *vp, int &x, int &y)
1749 {
1750 int original_y = y;
1751
1752 /* Centre of the viewport is hot spot */
1753 x += vp->virtual_width / 2;
1754 y += vp->virtual_height / 2;
1755
1756 /* Convert viewport coordinates to map coordinates
1757 * Calculation is scaled by 4 to avoid rounding errors */
1758 int vx = -x + y * 2;
1759 int vy = x + y * 2;
1760
1761 /* Find out which tile corresponds to (vx,vy) if one assumes height zero. The cast is necessary to prevent C++ from
1762 * converting the result to an uint, which gives an overflow instead of a negative result... */
1763 int tx = vx / (int)(TILE_SIZE * 4 * ZOOM_LVL_BASE);
1764 int ty = vy / (int)(TILE_SIZE * 4 * ZOOM_LVL_BASE);
1765
1766 Point curr_tile;
1767 vx = ClampXYToMap(curr_tile, curr_tile.y, MapMaxY(), ty, curr_tile.x, tx, vx, MapMaxX(), original_y, original_y + vp->virtual_height);
1768 vy = ClampXYToMap(curr_tile, curr_tile.x, MapMaxX(), tx, curr_tile.y, ty, vy, MapMaxY(), original_y, original_y + vp->virtual_height);
1769
1770 /* Convert map coordinates to viewport coordinates */
1771 x = (-vx + vy) / 2;
1772 y = ( vx + vy) / 4;
1773
1774 /* Remove centering */
1775 x -= vp->virtual_width / 2;
1776 y -= vp->virtual_height / 2;
1777 }
1778
1783 void UpdateViewportPosition(Window *w)
1784 {
1785 const ViewPort *vp = w->viewport;
1786
1787 if (w->viewport->follow_vehicle != INVALID_VEHICLE) {
1788 const Vehicle *veh = Vehicle::Get(w->viewport->follow_vehicle);
1789 Point pt = MapXYZToViewport(vp, veh->x_pos, veh->y_pos, veh->z_pos);
1790
1791 w->viewport->scrollpos_x = pt.x;
1792 w->viewport->scrollpos_y = pt.y;
1793 SetViewportPosition(w, pt.x, pt.y);
1794 } else {
1795 /* Ensure the destination location is within the map */
1796 ClampViewportToMap(vp, w->viewport->dest_scrollpos_x, w->viewport->dest_scrollpos_y);
1797
1798 int delta_x = w->viewport->dest_scrollpos_x - w->viewport->scrollpos_x;
1799 int delta_y = w->viewport->dest_scrollpos_y - w->viewport->scrollpos_y;
1800
1801 bool update_overlay = false;
1802 if (delta_x != 0 || delta_y != 0) {
1803 if (_settings_client.gui.smooth_scroll) {
1804 int max_scroll = ScaleByMapSize1D(512 * ZOOM_LVL_BASE);
1805 /* Not at our desired position yet... */
1806 w->viewport->scrollpos_x += Clamp(delta_x / 4, -max_scroll, max_scroll);
1807 w->viewport->scrollpos_y += Clamp(delta_y / 4, -max_scroll, max_scroll);
1808 } else {
1809 w->viewport->scrollpos_x = w->viewport->dest_scrollpos_x;
1810 w->viewport->scrollpos_y = w->viewport->dest_scrollpos_y;
1811 }
1812 update_overlay = (w->viewport->scrollpos_x == w->viewport->dest_scrollpos_x &&
1813 w->viewport->scrollpos_y == w->viewport->dest_scrollpos_y);
1814 }
1815
1816 ClampViewportToMap(vp, w->viewport->scrollpos_x, w->viewport->scrollpos_y);
1817
1818 SetViewportPosition(w, w->viewport->scrollpos_x, w->viewport->scrollpos_y);
1819 if (update_overlay) RebuildViewportOverlay(w);
1820 }
1821 }
1822
1832 static void MarkViewportDirty(const ViewPort *vp, int left, int top, int right, int bottom)
1833 {
1834 /* Rounding wrt. zoom-out level */
1835 right += (1 << vp->zoom) - 1;
1836 bottom += (1 << vp->zoom) - 1;
1837
1838 right -= vp->virtual_left;
1839 if (right <= 0) return;
1840
1841 bottom -= vp->virtual_top;
1842 if (bottom <= 0) return;
1843
1844 left = max(0, left - vp->virtual_left);
1845
1846 if (left >= vp->virtual_width) return;
1847
1848 top = max(0, top - vp->virtual_top);
1849
1850 if (top >= vp->virtual_height) return;
1851
1852 SetDirtyBlocks(
1853 UnScaleByZoomLower(left, vp->zoom) + vp->left,
1854 UnScaleByZoomLower(top, vp->zoom) + vp->top,
1855 UnScaleByZoom(right, vp->zoom) + vp->left + 1,
1856 UnScaleByZoom(bottom, vp->zoom) + vp->top + 1
1857 );
1858 }
1859
1868 void MarkAllViewportsDirty(int left, int top, int right, int bottom)
1869 {
1870 Window *w;
1871 FOR_ALL_WINDOWS_FROM_BACK(w) {
1872 ViewPort *vp = w->viewport;
1873 if (vp != NULL) {
1874 assert(vp->width != 0);
1875 MarkViewportDirty(vp, left, top, right, bottom);
1876 }
1877 }
1878 }
1879
1880 void ConstrainAllViewportsZoom()
1881 {
1882 Window *w;
1883 FOR_ALL_WINDOWS_FROM_FRONT(w) {
1884 if (w->viewport == NULL) continue;
1885
1886 ZoomLevel zoom = static_cast<ZoomLevel>(Clamp(w->viewport->zoom, _settings_client.gui.zoom_min, _settings_client.gui.zoom_max));
1887 if (zoom != w->viewport->zoom) {
1888 while (w->viewport->zoom < zoom) DoZoomInOutWindow(ZOOM_OUT, w);
1889 while (w->viewport->zoom > zoom) DoZoomInOutWindow(ZOOM_IN, w);
1890 }
1891 }
1892 }
1893
1900 void MarkTileDirtyByTile(TileIndex tile, int bridge_level_offset)
1901 {
1902 Point pt = RemapCoords(TileX(tile) * TILE_SIZE, TileY(tile) * TILE_SIZE, TilePixelHeight(tile));
1903 MarkAllViewportsDirty(
1904 pt.x - MAX_TILE_EXTENT_LEFT,
1905 pt.y - MAX_TILE_EXTENT_TOP - ZOOM_LVL_BASE * TILE_HEIGHT * bridge_level_offset,
1906 pt.x + MAX_TILE_EXTENT_RIGHT,
1907 pt.y + MAX_TILE_EXTENT_BOTTOM);
1908 }
1909
1916 void MarkTileDirtyByTileOutsideMap(int x, int y)
1917 {
1918 Point pt = RemapCoords(x * TILE_SIZE, y * TILE_SIZE, TilePixelHeightOutsideMap(x, y));
1919 MarkAllViewportsDirty(
1920 pt.x - MAX_TILE_EXTENT_LEFT,
1921 pt.y, // no buildings outside of map
1922 pt.x + MAX_TILE_EXTENT_RIGHT,
1923 pt.y + MAX_TILE_EXTENT_BOTTOM);
1924 }
1925
1933 static void SetSelectionTilesDirty()
1934 {
1935 int x_size = _thd.size.x;
1936 int y_size = _thd.size.y;
1937
1938 if (!_thd.diagonal) { // Selecting in a straight rectangle (or a single square)
1939 int x_start = _thd.pos.x;
1940 int y_start = _thd.pos.y;
1941
1942 if (_thd.outersize.x != 0) {
1943 x_size += _thd.outersize.x;
1944 x_start += _thd.offs.x;
1945 y_size += _thd.outersize.y;
1946 y_start += _thd.offs.y;
1947 }
1948
1949 x_size -= TILE_SIZE;
1950 y_size -= TILE_SIZE;
1951
1952 assert(x_size >= 0);
1953 assert(y_size >= 0);
1954
1955 int x_end = Clamp(x_start + x_size, 0, MapSizeX() * TILE_SIZE - TILE_SIZE);
1956 int y_end = Clamp(y_start + y_size, 0, MapSizeY() * TILE_SIZE - TILE_SIZE);
1957
1958 x_start = Clamp(x_start, 0, MapSizeX() * TILE_SIZE - TILE_SIZE);
1959 y_start = Clamp(y_start, 0, MapSizeY() * TILE_SIZE - TILE_SIZE);
1960
1961 /* make sure everything is multiple of TILE_SIZE */
1962 assert((x_end | y_end | x_start | y_start) % TILE_SIZE == 0);
1963
1964 /* How it works:
1965 * Suppose we have to mark dirty rectangle of 3x4 tiles:
1966 * x
1967 * xxx
1968 * xxxxx
1969 * xxxxx
1970 * xxx
1971 * x
1972 * This algorithm marks dirty columns of tiles, so it is done in 3+4-1 steps:
1973 * 1) x 2) x
1974 * xxx Oxx
1975 * Oxxxx xOxxx
1976 * xxxxx Oxxxx
1977 * xxx xxx
1978 * x x
1979 * And so forth...
1980 */
1981
1982 int top_x = x_end; // coordinates of top dirty tile
1983 int top_y = y_start;
1984 int bot_x = top_x; // coordinates of bottom dirty tile
1985 int bot_y = top_y;
1986
1987 do {
1988 /* topmost dirty point */
1989 TileIndex top_tile = TileVirtXY(top_x, top_y);
1990 Point top = RemapCoords(top_x, top_y, GetTileMaxPixelZ(top_tile));
1991
1992 /* bottommost point */
1993 TileIndex bottom_tile = TileVirtXY(bot_x, bot_y);
1994 Point bot = RemapCoords(bot_x + TILE_SIZE, bot_y + TILE_SIZE, GetTilePixelZ(bottom_tile)); // bottommost point
1995
1996 /* the 'x' coordinate of 'top' and 'bot' is the same (and always in the same distance from tile middle),
1997 * tile height/slope affects only the 'y' on-screen coordinate! */
1998
1999 int l = top.x - TILE_PIXELS * ZOOM_LVL_BASE; // 'x' coordinate of left side of the dirty rectangle
2000 int t = top.y; // 'y' coordinate of top side of the dirty rectangle
2001 int r = top.x + TILE_PIXELS * ZOOM_LVL_BASE; // 'x' coordinate of right side of the dirty rectangle
2002 int b = bot.y; // 'y' coordinate of bottom side of the dirty rectangle
2003
2004 static const int OVERLAY_WIDTH = 4 * ZOOM_LVL_BASE; // part of selection sprites is drawn outside the selected area (in particular: terraforming)
2005
2006 /* For halftile foundations on SLOPE_STEEP_S the sprite extents some more towards the top */
2007 MarkAllViewportsDirty(l - OVERLAY_WIDTH, t - OVERLAY_WIDTH - TILE_HEIGHT * ZOOM_LVL_BASE, r + OVERLAY_WIDTH, b + OVERLAY_WIDTH);
2008
2009 /* haven't we reached the topmost tile yet? */
2010 if (top_x != x_start) {
2011 top_x -= TILE_SIZE;
2012 } else {
2013 top_y += TILE_SIZE;
2014 }
2015
2016 /* the way the bottom tile changes is different when we reach the bottommost tile */
2017 if (bot_y != y_end) {
2018 bot_y += TILE_SIZE;
2019 } else {
2020 bot_x -= TILE_SIZE;
2021 }
2022 } while (bot_x >= top_x);
2023 } else { // Selecting in a 45 degrees rotated (diagonal) rectangle.
2024 /* a_size, b_size describe a rectangle with rotated coordinates */
2025 int a_size = x_size + y_size, b_size = x_size - y_size;
2026
2027 int interval_a = a_size < 0 ? -(int)TILE_SIZE : (int)TILE_SIZE;
2028 int interval_b = b_size < 0 ? -(int)TILE_SIZE : (int)TILE_SIZE;
2029
2030 for (int a = -interval_a; a != a_size + interval_a; a += interval_a) {
2031 for (int b = -interval_b; b != b_size + interval_b; b += interval_b) {
2032 uint x = (_thd.pos.x + (a + b) / 2) / TILE_SIZE;
2033 uint y = (_thd.pos.y + (a - b) / 2) / TILE_SIZE;
2034
2035 if (x < MapMaxX() && y < MapMaxY()) {
2036 MarkTileDirtyByTile(TileXY(x, y));
2037 }
2038 }
2039 }
2040 }
2041 }
2042
2043
2044 void SetSelectionRed(bool b)
2045 {
2046 _thd.make_square_red = b;
2047 SetSelectionTilesDirty();
2048 }
2049
2058 static bool CheckClickOnViewportSign(const ViewPort *vp, int x, int y, const ViewportSign *sign)
2059 {
2060 bool small = (vp->zoom >= ZOOM_LVL_OUT_16X);
2061 int sign_half_width = ScaleByZoom((small ? sign->width_small : sign->width_normal) / 2, vp->zoom);
2062 int sign_height = ScaleByZoom(VPSM_TOP + (small ? FONT_HEIGHT_SMALL : FONT_HEIGHT_NORMAL) + VPSM_BOTTOM, vp->zoom);
2063
2064 x = ScaleByZoom(x - vp->left, vp->zoom) + vp->virtual_left;
2065 y = ScaleByZoom(y - vp->top, vp->zoom) + vp->virtual_top;
2066
2067 return y >= sign->top && y < sign->top + sign_height &&
2068 x >= sign->center - sign_half_width && x < sign->center + sign_half_width;
2069 }
2070
2071 static bool CheckClickOnTown(const ViewPort *vp, int x, int y)
2072 {
2073 if (!HasBit(_display_opt, DO_SHOW_TOWN_NAMES)) return false;
2074
2075 const Town *t;
2076 FOR_ALL_TOWNS(t) {
2077 if (CheckClickOnViewportSign(vp, x, y, &t->cache.sign)) {
2078 ShowTownViewWindow(t->index);
2079 return true;
2080 }
2081 }
2082
2083 return false;
2084 }
2085
2086 static bool CheckClickOnStation(const ViewPort *vp, int x, int y)
2087 {
2088 if (!(HasBit(_display_opt, DO_SHOW_STATION_NAMES) || HasBit(_display_opt, DO_SHOW_WAYPOINT_NAMES)) || IsInvisibilitySet(TO_SIGNS)) return false;
2089
2090 const BaseStation *st;
2091 FOR_ALL_BASE_STATIONS(st) {
2092 /* Check whether the base station is a station or a waypoint */
2093 bool is_station = Station::IsExpected(st);
2094
2095 /* Don't check if the display options are disabled */
2096 if (!HasBit(_display_opt, is_station ? DO_SHOW_STATION_NAMES : DO_SHOW_WAYPOINT_NAMES)) continue;
2097
2098 /* Don't check if competitor signs are not shown and the sign isn't owned by the local company */
2099 if (!HasBit(_display_opt, DO_SHOW_COMPETITOR_SIGNS) && _local_company != st->owner && st->owner != OWNER_NONE) continue;
2100
2101 if (CheckClickOnViewportSign(vp, x, y, &st->sign)) {
2102 if (is_station) {
2103 ShowStationViewWindow(st->index);
2104 } else {
2105 ShowWaypointWindow(Waypoint::From(st));
2106 }
2107 return true;
2108 }
2109 }
2110
2111 return false;
2112 }
2113
2114
2115 static bool CheckClickOnSign(const ViewPort *vp, int x, int y)
2116 {
2117 /* Signs are turned off, or they are transparent and invisibility is ON, or company is a spectator */
2118 if (!HasBit(_display_opt, DO_SHOW_SIGNS) || IsInvisibilitySet(TO_SIGNS) || _local_company == COMPANY_SPECTATOR) return false;
2119
2120 const Sign *si;
2121 FOR_ALL_SIGNS(si) {
2122 /* If competitor signs are hidden, don't check signs that aren't owned by local company */
2123 if (!HasBit(_display_opt, DO_SHOW_COMPETITOR_SIGNS) && _local_company != si->owner && si->owner != OWNER_DEITY) continue;
2124 if (si->owner == OWNER_DEITY && _game_mode != GM_EDITOR) continue;
2125
2126 if (CheckClickOnViewportSign(vp, x, y, &si->sign)) {
2127 HandleClickOnSign(si);
2128 return true;
2129 }
2130 }
2131
2132 return false;
2133 }
2134
2135
2136 static bool CheckClickOnLandscape(const ViewPort *vp, int x, int y)
2137 {
2138 Point pt = TranslateXYToTileCoord(vp, x, y);
2139
2140 if (pt.x != -1) return ClickTile(TileVirtXY(pt.x, pt.y));
2141 return true;
2142 }
2143
2144 static void PlaceObject()
2145 {
2146 Point pt;
2147 Window *w;
2148
2149 pt = GetTileBelowCursor();
2150 if (pt.x == -1) return;
2151
2152 if ((_thd.place_mode & HT_DRAG_MASK) == HT_POINT) {
2153 pt.x += TILE_SIZE / 2;
2154 pt.y += TILE_SIZE / 2;
2155 }
2156
2157 _tile_fract_coords.x = pt.x & TILE_UNIT_MASK;
2158 _tile_fract_coords.y = pt.y & TILE_UNIT_MASK;
2159
2160 w = _thd.GetCallbackWnd();
2161 if (w != NULL) w->OnPlaceObject(pt, TileVirtXY(pt.x, pt.y));
2162 }
2163
2164
2165 bool HandleViewportClicked(const ViewPort *vp, int x, int y)
2166 {
2167 const Vehicle *v = CheckClickOnVehicle(vp, x, y);
2168
2169 if (_thd.place_mode & HT_VEHICLE) {
2170 if (v != NULL && VehicleClicked(v)) return true;
2171 }
2172
2173 /* Vehicle placement mode already handled above. */
2174 if ((_thd.place_mode & HT_DRAG_MASK) != HT_NONE) {
2175 PlaceObject();
2176 return true;
2177 }
2178
2179 if (CheckClickOnTown(vp, x, y)) return true;
2180 if (CheckClickOnStation(vp, x, y)) return true;
2181 if (CheckClickOnSign(vp, x, y)) return true;
2182 bool result = CheckClickOnLandscape(vp, x, y);
2183
2184 if (v != NULL) {
2185 DEBUG(misc, 2, "Vehicle %d (index %d) at %p", v->unitnumber, v->index, v);
2186 if (IsCompanyBuildableVehicleType(v)) {
2187 v = v->First();
2188 if (_ctrl_pressed && v->owner == _local_company) {
2189 StartStopVehicle(v, true);
2190 } else {
2191 ShowVehicleViewWindow(v);
2192 }
2193 }
2194 return true;
2195 }
2196 return result;
2197 }
2198
2199 void RebuildViewportOverlay(Window *w)
2200 {
2201 if (w->viewport->overlay != NULL &&
2202 w->viewport->overlay->GetCompanyMask() != 0 &&
2203 w->viewport->overlay->GetCargoMask() != 0) {
2204 w->viewport->overlay->RebuildCache();
2205 w->SetDirty();
2206 }
2207 }
2208
2218 bool ScrollWindowTo(int x, int y, int z, Window *w, bool instant)
2219 {
2220 /* The slope cannot be acquired outside of the map, so make sure we are always within the map. */
2221 if (z == -1) {
2222 if ( x >= 0 && x <= (int)MapSizeX() * (int)TILE_SIZE - 1
2223 && y >= 0 && y <= (int)MapSizeY() * (int)TILE_SIZE - 1) {
2224 z = GetSlopePixelZ(x, y);
2225 } else {
2226 z = TileHeightOutsideMap(x / (int)TILE_SIZE, y / (int)TILE_SIZE);
2227 }
2228 }
2229
2230 Point pt = MapXYZToViewport(w->viewport, x, y, z);
2231 w->viewport->follow_vehicle = INVALID_VEHICLE;
2232
2233 if (w->viewport->dest_scrollpos_x == pt.x && w->viewport->dest_scrollpos_y == pt.y) return false;
2234
2235 if (instant) {
2236 w->viewport->scrollpos_x = pt.x;
2237 w->viewport->scrollpos_y = pt.y;
2238 RebuildViewportOverlay(w);
2239 }
2240
2241 w->viewport->dest_scrollpos_x = pt.x;
2242 w->viewport->dest_scrollpos_y = pt.y;
2243 return true;
2244 }
2245
2253 bool ScrollWindowToTile(TileIndex tile, Window *w, bool instant)
2254 {
2255 return ScrollWindowTo(TileX(tile) * TILE_SIZE, TileY(tile) * TILE_SIZE, -1, w, instant);
2256 }
2257
2264 bool ScrollMainWindowToTile(TileIndex tile, bool instant)
2265 {
2266 return ScrollMainWindowTo(TileX(tile) * TILE_SIZE + TILE_SIZE / 2, TileY(tile) * TILE_SIZE + TILE_SIZE / 2, -1, instant);
2267 }
2268
2273 void SetRedErrorSquare(TileIndex tile)
2274 {
2275 TileIndex old;
2276
2277 old = _thd.redsq;
2278 _thd.redsq = tile;
2279
2280 if (tile != old) {
2281 if (tile != INVALID_TILE) MarkTileDirtyByTile(tile);
2282 if (old != INVALID_TILE) MarkTileDirtyByTile(old);
2283 }
2284 }
2285
2291 void SetTileSelectSize(int w, int h)
2292 {
2293 _thd.new_size.x = w * TILE_SIZE;
2294 _thd.new_size.y = h * TILE_SIZE;
2295 _thd.new_outersize.x = 0;
2296 _thd.new_outersize.y = 0;
2297 }
2298
2299 void SetTileSelectBigSize(int ox, int oy, int sx, int sy)
2300 {
2301 _thd.offs.x = ox * TILE_SIZE;
2302 _thd.offs.y = oy * TILE_SIZE;
2303 _thd.new_outersize.x = sx * TILE_SIZE;
2304 _thd.new_outersize.y = sy * TILE_SIZE;
2305 }
2306
2308 static HighLightStyle GetAutorailHT(int x, int y)
2309 {
2310 return HT_RAIL | _autorail_piece[x & TILE_UNIT_MASK][y & TILE_UNIT_MASK];
2311 }
2312
2316 void TileHighlightData::Reset()
2317 {
2318 this->pos.x = 0;
2319 this->pos.y = 0;
2320 this->new_pos.x = 0;
2321 this->new_pos.y = 0;
2322 }
2323
2328 bool TileHighlightData::IsDraggingDiagonal()
2329 {
2330 return (this->place_mode & HT_DIAGONAL) != 0 && _ctrl_pressed && _left_button_down;
2331 }
2332
2337 Window *TileHighlightData::GetCallbackWnd()
2338 {
2339 return FindWindowById(this->window_class, this->window_number);
2340 }
2341
2342
2343
2351 void UpdateTileSelection()
2352 {
2353 int x1;
2354 int y1;
2355
2356 HighLightStyle new_drawstyle = HT_NONE;
2357 bool new_diagonal = false;
2358
2359 if ((_thd.place_mode & HT_DRAG_MASK) == HT_SPECIAL) {
2360 x1 = _thd.selend.x;
2361 y1 = _thd.selend.y;
2362 if (x1 != -1) {
2363 int x2 = _thd.selstart.x & ~TILE_UNIT_MASK;
2364 int y2 = _thd.selstart.y & ~TILE_UNIT_MASK;
2365 x1 &= ~TILE_UNIT_MASK;
2366 y1 &= ~TILE_UNIT_MASK;
2367
2368 if (_thd.IsDraggingDiagonal()) {
2369 new_diagonal = true;
2370 } else {
2371 if (x1 >= x2) Swap(x1, x2);
2372 if (y1 >= y2) Swap(y1, y2);
2373 }
2374 _thd.new_pos.x = x1;
2375 _thd.new_pos.y = y1;
2376 _thd.new_size.x = x2 - x1;
2377 _thd.new_size.y = y2 - y1;
2378 if (!new_diagonal) {
2379 _thd.new_size.x += TILE_SIZE;
2380 _thd.new_size.y += TILE_SIZE;
2381 }
2382 new_drawstyle = _thd.next_drawstyle;
2383 }
2384 } else if ((_thd.place_mode & HT_DRAG_MASK) != HT_NONE) {
2385 Point pt = GetTileBelowCursor();
2386 x1 = pt.x;
2387 y1 = pt.y;
2388 if (x1 != -1) {
2389 switch (_thd.place_mode & HT_DRAG_MASK) {
2390 case HT_RECT:
2391 new_drawstyle = HT_RECT;
2392 break;
2393 case HT_POINT:
2394 new_drawstyle = HT_POINT;
2395 x1 += TILE_SIZE / 2;
2396 y1 += TILE_SIZE / 2;
2397 break;
2398 case HT_RAIL:
2399 /* Draw one highlighted tile in any direction */
2400 new_drawstyle = GetAutorailHT(pt.x, pt.y);
2401 break;
2402 case HT_LINE:
2403 switch (_thd.place_mode & HT_DIR_MASK) {
2404 case HT_DIR_X: new_drawstyle = HT_LINE | HT_DIR_X; break;
2405 case HT_DIR_Y: new_drawstyle = HT_LINE | HT_DIR_Y; break;
2406
2407 case HT_DIR_HU:
2408 case HT_DIR_HL:
2409 new_drawstyle = (pt.x & TILE_UNIT_MASK) + (pt.y & TILE_UNIT_MASK) <= TILE_SIZE ? HT_LINE | HT_DIR_HU : HT_LINE | HT_DIR_HL;
2410 break;
2411
2412 case HT_DIR_VL:
2413 case HT_DIR_VR:
2414 new_drawstyle = (pt.x & TILE_UNIT_MASK) > (pt.y & TILE_UNIT_MASK) ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR;
2415 break;
2416
2417 default: NOT_REACHED();
2418 }
2419 _thd.selstart.x = x1 & ~TILE_UNIT_MASK;
2420 _thd.selstart.y = y1 & ~TILE_UNIT_MASK;
2421 break;
2422 default:
2423 NOT_REACHED();
2424 break;
2425 }
2426 _thd.new_pos.x = x1 & ~TILE_UNIT_MASK;
2427 _thd.new_pos.y = y1 & ~TILE_UNIT_MASK;
2428 }
2429 }
2430
2431 /* redraw selection */
2432 if (_thd.drawstyle != new_drawstyle ||
2433 _thd.pos.x != _thd.new_pos.x || _thd.pos.y != _thd.new_pos.y ||
2434 _thd.size.x != _thd.new_size.x || _thd.size.y != _thd.new_size.y ||
2435 _thd.outersize.x != _thd.new_outersize.x ||
2436 _thd.outersize.y != _thd.new_outersize.y ||
2437 _thd.diagonal != new_diagonal) {
2438 /* Clear the old tile selection? */
2439 if ((_thd.drawstyle & HT_DRAG_MASK) != HT_NONE) SetSelectionTilesDirty();
2440
2441 _thd.drawstyle = new_drawstyle;
2442 _thd.pos = _thd.new_pos;
2443 _thd.size = _thd.new_size;
2444 _thd.outersize = _thd.new_outersize;
2445 _thd.diagonal = new_diagonal;
2446 _thd.dirty = 0xff;
2447
2448 /* Draw the new tile selection? */
2449 if ((new_drawstyle & HT_DRAG_MASK) != HT_NONE) SetSelectionTilesDirty();
2450 }
2451 }
2452
2460 static inline void ShowMeasurementTooltips(StringID str, uint paramcount, const uint64 params[], TooltipCloseCondition close_cond = TCC_LEFT_CLICK)
2461 {
2462 if (!_settings_client.gui.measure_tooltip) return;
2463 GuiShowTooltips(_thd.GetCallbackWnd(), str, paramcount, params, close_cond);
2464 }
2465
2467 void VpStartPlaceSizing(TileIndex tile, ViewportPlaceMethod method, ViewportDragDropSelectionProcess process)
2468 {
2469 _thd.select_method = method;
2470 _thd.select_proc = process;
2471 _thd.selend.x = TileX(tile) * TILE_SIZE;
2472 _thd.selstart.x = TileX(tile) * TILE_SIZE;
2473 _thd.selend.y = TileY(tile) * TILE_SIZE;
2474 _thd.selstart.y = TileY(tile) * TILE_SIZE;
2475
2476 /* Needed so several things (road, autoroad, bridges, ...) are placed correctly.
2477 * In effect, placement starts from the centre of a tile
2478 */
2479 if (method == VPM_X_OR_Y || method == VPM_FIX_X || method == VPM_FIX_Y) {
2480 _thd.selend.x += TILE_SIZE / 2;
2481 _thd.selend.y += TILE_SIZE / 2;
2482 _thd.selstart.x += TILE_SIZE / 2;
2483 _thd.selstart.y += TILE_SIZE / 2;
2484 }
2485
2486 HighLightStyle others = _thd.place_mode & ~(HT_DRAG_MASK | HT_DIR_MASK);
2487 if ((_thd.place_mode & HT_DRAG_MASK) == HT_RECT) {
2488 _thd.place_mode = HT_SPECIAL | others;
2489 _thd.next_drawstyle = HT_RECT | others;
2490 } else if (_thd.place_mode & (HT_RAIL | HT_LINE)) {
2491 _thd.place_mode = HT_SPECIAL | others;
2492 _thd.next_drawstyle = _thd.drawstyle | others;
2493 } else {
2494 _thd.place_mode = HT_SPECIAL | others;
2495 _thd.next_drawstyle = HT_POINT | others;
2496 }
2497 _special_mouse_mode = WSM_SIZING;
2498 }
2499
2500 void VpSetPlaceSizingLimit(int limit)
2501 {
2502 _thd.sizelimit = limit;
2503 }
2504
2510 void VpSetPresizeRange(TileIndex from, TileIndex to)
2511 {
2512 uint64 distance = DistanceManhattan(from, to) + 1;
2513
2514 _thd.selend.x = TileX(to) * TILE_SIZE;
2515 _thd.selend.y = TileY(to) * TILE_SIZE;
2516 _thd.selstart.x = TileX(from) * TILE_SIZE;
2517 _thd.selstart.y = TileY(from) * TILE_SIZE;
2518 _thd.next_drawstyle = HT_RECT;
2519
2520 /* show measurement only if there is any length to speak of */
2521 if (distance > 1) ShowMeasurementTooltips(STR_MEASURE_LENGTH, 1, &distance, TCC_HOVER);
2522 }
2523
2524 static void VpStartPreSizing()
2525 {
2526 _thd.selend.x = -1;
2527 _special_mouse_mode = WSM_PRESIZE;
2528 }
2529
2534 static HighLightStyle Check2x1AutoRail(int mode)
2535 {
2536 int fxpy = _tile_fract_coords.x + _tile_fract_coords.y;
2537 int sxpy = (_thd.selend.x & TILE_UNIT_MASK) + (_thd.selend.y & TILE_UNIT_MASK);
2538 int fxmy = _tile_fract_coords.x - _tile_fract_coords.y;
2539 int sxmy = (_thd.selend.x & TILE_UNIT_MASK) - (_thd.selend.y & TILE_UNIT_MASK);
2540
2541 switch (mode) {
2542 default: NOT_REACHED();
2543 case 0: // end piece is lower right
2544 if (fxpy >= 20 && sxpy <= 12) return HT_DIR_HL;
2545 if (fxmy < -3 && sxmy > 3) return HT_DIR_VR;
2546 return HT_DIR_Y;
2547
2548 case 1:
2549 if (fxmy > 3 && sxmy < -3) return HT_DIR_VL;
2550 if (fxpy <= 12 && sxpy >= 20) return HT_DIR_HU;
2551 return HT_DIR_Y;
2552
2553 case 2:
2554 if (fxmy > 3 && sxmy < -3) return HT_DIR_VL;
2555 if (fxpy >= 20 && sxpy <= 12) return HT_DIR_HL;
2556 return HT_DIR_X;
2557
2558 case 3:
2559 if (fxmy < -3 && sxmy > 3) return HT_DIR_VR;
2560 if (fxpy <= 12 && sxpy >= 20) return HT_DIR_HU;
2561 return HT_DIR_X;
2562 }
2563 }
2564
2578 static bool SwapDirection(HighLightStyle style, TileIndex start_tile, TileIndex end_tile)
2579 {
2580 uint start_x = TileX(start_tile);
2581 uint start_y = TileY(start_tile);
2582 uint end_x = TileX(end_tile);
2583 uint end_y = TileY(end_tile);
2584
2585 switch (style & HT_DRAG_MASK) {
2586 case HT_RAIL:
2587 case HT_LINE: return (end_x > start_x || (end_x == start_x && end_y > start_y));
2588
2589 case HT_RECT:
2590 case HT_POINT: return (end_x != start_x && end_y < start_y);
2591 default: NOT_REACHED();
2592 }
2593
2594 return false;
2595 }
2596
2612 static int CalcHeightdiff(HighLightStyle style, uint distance, TileIndex start_tile, TileIndex end_tile)
2613 {
2614 bool swap = SwapDirection(style, start_tile, end_tile);
2615 uint h0, h1; // Start height and end height.
2616
2617 if (start_tile == end_tile) return 0;
2618 if (swap) Swap(start_tile, end_tile);
2619
2620 switch (style & HT_DRAG_MASK) {
2621 case HT_RECT: {
2622 static const TileIndexDiffC heightdiff_area_by_dir[] = {
2623 /* Start */ {1, 0}, /* Dragging east */ {0, 0}, // Dragging south
2624 /* End */ {0, 1}, /* Dragging east */ {1, 1} // Dragging south
2625 };
2626
2627 /* In the case of an area we can determine whether we were dragging south or
2628 * east by checking the X-coordinates of the tiles */
2629 byte style_t = (byte)(TileX(end_tile) > TileX(start_tile));
2630 start_tile = TILE_ADD(start_tile, ToTileIndexDiff(heightdiff_area_by_dir[style_t]));
2631 end_tile = TILE_ADD(end_tile, ToTileIndexDiff(heightdiff_area_by_dir[2 + style_t]));
2632 /* FALL THROUGH */
2633 }
2634
2635 case HT_POINT:
2636 h0 = TileHeight(start_tile);
2637 h1 = TileHeight(end_tile);
2638 break;
2639 default: { // All other types, this is mostly only line/autorail
2640 static const HighLightStyle flip_style_direction[] = {
2641 HT_DIR_X, HT_DIR_Y, HT_DIR_HL, HT_DIR_HU, HT_DIR_VR, HT_DIR_VL
2642 };
2643 static const TileIndexDiffC heightdiff_line_by_dir[] = {
2644 /* Start */ {1, 0}, {1, 1}, /* HT_DIR_X */ {0, 1}, {1, 1}, // HT_DIR_Y
2645 /* Start */ {1, 0}, {0, 0}, /* HT_DIR_HU */ {1, 0}, {1, 1}, // HT_DIR_HL
2646 /* Start */ {1, 0}, {1, 1}, /* HT_DIR_VL */ {0, 1}, {1, 1}, // HT_DIR_VR
2647
2648 /* Start */ {0, 1}, {0, 0}, /* HT_DIR_X */ {1, 0}, {0, 0}, // HT_DIR_Y
2649 /* End */ {0, 1}, {0, 0}, /* HT_DIR_HU */ {1, 1}, {0, 1}, // HT_DIR_HL
2650 /* End */ {1, 0}, {0, 0}, /* HT_DIR_VL */ {0, 0}, {0, 1}, // HT_DIR_VR
2651 };
2652
2653 distance %= 2; // we're only interested if the distance is even or uneven
2654 style &= HT_DIR_MASK;
2655
2656 /* To handle autorail, we do some magic to be able to use a lookup table.
2657 * Firstly if we drag the other way around, we switch start&end, and if needed
2658 * also flip the drag-position. Eg if it was on the left, and the distance is even
2659 * that means the end, which is now the start is on the right */
2660 if (swap && distance == 0) style = flip_style_direction[style];
2661
2662 /* Use lookup table for start-tile based on HighLightStyle direction */
2663 byte style_t = style * 2;
2664 assert(style_t < lengthof(heightdiff_line_by_dir) - 13);
2665 h0 = TileHeight(TILE_ADD(start_tile, ToTileIndexDiff(heightdiff_line_by_dir[style_t])));
2666 uint ht = TileHeight(TILE_ADD(start_tile, ToTileIndexDiff(heightdiff_line_by_dir[style_t + 1])));
2667 h0 = max(h0, ht);
2668
2669 /* Use lookup table for end-tile based on HighLightStyle direction
2670 * flip around side (lower/upper, left/right) based on distance */
2671 if (distance == 0) style_t = flip_style_direction[style] * 2;
2672 assert(style_t < lengthof(heightdiff_line_by_dir) - 13);
2673 h1 = TileHeight(TILE_ADD(end_tile, ToTileIndexDiff(heightdiff_line_by_dir[12 + style_t])));
2674 ht = TileHeight(TILE_ADD(end_tile, ToTileIndexDiff(heightdiff_line_by_dir[12 + style_t + 1])));
2675 h1 = max(h1, ht);
2676 break;
2677 }
2678 }
2679
2680 if (swap) Swap(h0, h1);
2681 return (int)(h1 - h0) * TILE_HEIGHT_STEP;
2682 }
2683
2684 static const StringID measure_strings_length[] = {STR_NULL, STR_MEASURE_LENGTH, STR_MEASURE_LENGTH_HEIGHTDIFF};
2685
2692 static void CheckUnderflow(int &test, int &other, int mult)
2693 {
2694 if (test >= 0) return;
2695
2696 other += mult * test;
2697 test = 0;
2698 }
2699
2707 static void CheckOverflow(int &test, int &other, int max, int mult)
2708 {
2709 if (test <= max) return;
2710
2711 other += mult * (test - max);
2712 test = max;
2713 }
2714
2716 static void CalcRaildirsDrawstyle(int x, int y, int method)
2717 {
2718 HighLightStyle b;
2719
2720 int dx = _thd.selstart.x - (_thd.selend.x & ~TILE_UNIT_MASK);
2721 int dy = _thd.selstart.y - (_thd.selend.y & ~TILE_UNIT_MASK);
2722 uint w = abs(dx) + TILE_SIZE;
2723 uint h = abs(dy) + TILE_SIZE;
2724
2725 if (method & ~(VPM_RAILDIRS | VPM_SIGNALDIRS)) {
2726 /* We 'force' a selection direction; first four rail buttons. */
2727 method &= ~(VPM_RAILDIRS | VPM_SIGNALDIRS);
2728 int raw_dx = _thd.selstart.x - _thd.selend.x;
2729 int raw_dy = _thd.selstart.y - _thd.selend.y;
2730 switch (method) {
2731 case VPM_FIX_X:
2732 b = HT_LINE | HT_DIR_Y;
2733 x = _thd.selstart.x;
2734 break;
2735
2736 case VPM_FIX_Y:
2737 b = HT_LINE | HT_DIR_X;
2738 y = _thd.selstart.y;
2739 break;
2740
2741 case VPM_FIX_HORIZONTAL:
2742 if (dx == -dy) {
2743 /* We are on a straight horizontal line. Determine the 'rail'
2744 * to build based the sub tile location. */
2745 b = (x & TILE_UNIT_MASK) + (y & TILE_UNIT_MASK) >= TILE_SIZE ? HT_LINE | HT_DIR_HL : HT_LINE | HT_DIR_HU;
2746 } else {
2747 /* We are not on a straight line. Determine the rail to build
2748 * based on whether we are above or below it. */
2749 b = dx + dy >= (int)TILE_SIZE ? HT_LINE | HT_DIR_HU : HT_LINE | HT_DIR_HL;
2750
2751 /* Calculate where a horizontal line through the start point and
2752 * a vertical line from the selected end point intersect and
2753 * use that point as the end point. */
2754 int offset = (raw_dx - raw_dy) / 2;
2755 x = _thd.selstart.x - (offset & ~TILE_UNIT_MASK);
2756 y = _thd.selstart.y + (offset & ~TILE_UNIT_MASK);
2757
2758 /* 'Build' the last half rail tile if needed */
2759 if ((offset & TILE_UNIT_MASK) > (TILE_SIZE / 2)) {
2760 if (dx + dy >= (int)TILE_SIZE) {
2761 x += (dx + dy < 0) ? (int)TILE_SIZE : -(int)TILE_SIZE;
2762 } else {
2763 y += (dx + dy < 0) ? (int)TILE_SIZE : -(int)TILE_SIZE;
2764 }
2765 }
2766
2767 /* Make sure we do not overflow the map! */
2768 CheckUnderflow(x, y, 1);
2769 CheckUnderflow(y, x, 1);
2770 CheckOverflow(x, y, (MapMaxX() - 1) * TILE_SIZE, 1);
2771 CheckOverflow(y, x, (MapMaxY() - 1) * TILE_SIZE, 1);
2772 assert(x >= 0 && y >= 0 && x <= (int)(MapMaxX() * TILE_SIZE) && y <= (int)(MapMaxY() * TILE_SIZE));
2773 }
2774 break;
2775
2776 case VPM_FIX_VERTICAL:
2777 if (dx == dy) {
2778 /* We are on a straight vertical line. Determine the 'rail'
2779 * to build based the sub tile location. */
2780 b = (x & TILE_UNIT_MASK) > (y & TILE_UNIT_MASK) ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR;
2781 } else {
2782 /* We are not on a straight line. Determine the rail to build
2783 * based on whether we are left or right from it. */
2784 b = dx < dy ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR;
2785
2786 /* Calculate where a vertical line through the start point and
2787 * a horizontal line from the selected end point intersect and
2788 * use that point as the end point. */
2789 int offset = (raw_dx + raw_dy + (int)TILE_SIZE) / 2;
2790 x = _thd.selstart.x - (offset & ~TILE_UNIT_MASK);
2791 y = _thd.selstart.y - (offset & ~TILE_UNIT_MASK);
2792
2793 /* 'Build' the last half rail tile if needed */
2794 if ((offset & TILE_UNIT_MASK) > (TILE_SIZE / 2)) {
2795 if (dx - dy < 0) {
2796 y += (dx > dy) ? (int)TILE_SIZE : -(int)TILE_SIZE;
2797 } else {
2798 x += (dx < dy) ? (int)TILE_SIZE : -(int)TILE_SIZE;
2799 }
2800 }
2801
2802 /* Make sure we do not overflow the map! */
2803 CheckUnderflow(x, y, -1);
2804 CheckUnderflow(y, x, -1);
2805 CheckOverflow(x, y, (MapMaxX() - 1) * TILE_SIZE, -1);
2806 CheckOverflow(y, x, (MapMaxY() - 1) * TILE_SIZE, -1);
2807 assert(x >= 0 && y >= 0 && x <= (int)(MapMaxX() * TILE_SIZE) && y <= (int)(MapMaxY() * TILE_SIZE));
2808 }
2809 break;
2810
2811 default:
2812 NOT_REACHED();
2813 }
2814 } else if (TileVirtXY(_thd.selstart.x, _thd.selstart.y) == TileVirtXY(x, y)) { // check if we're only within one tile
2815 if (method & VPM_RAILDIRS) {
2816 b = GetAutorailHT(x, y);
2817 } else { // rect for autosignals on one tile
2818 b = HT_RECT;
2819 }
2820 } else if (h == TILE_SIZE) { // Is this in X direction?
2821 if (dx == (int)TILE_SIZE) { // 2x1 special handling
2822 b = (Check2x1AutoRail(3)) | HT_LINE;
2823 } else if (dx == -(int)TILE_SIZE) {
2824 b = (Check2x1AutoRail(2)) | HT_LINE;
2825 } else {
2826 b = HT_LINE | HT_DIR_X;
2827 }
2828 y = _thd.selstart.y;
2829 } else if (w == TILE_SIZE) { // Or Y direction?
2830 if (dy == (int)TILE_SIZE) { // 2x1 special handling
2831 b = (Check2x1AutoRail(1)) | HT_LINE;
2832 } else if (dy == -(int)TILE_SIZE) { // 2x1 other direction
2833 b = (Check2x1AutoRail(0)) | HT_LINE;
2834 } else {
2835 b = HT_LINE | HT_DIR_Y;
2836 }
2837 x = _thd.selstart.x;
2838 } else if (w > h * 2) { // still count as x dir?
2839 b = HT_LINE | HT_DIR_X;
2840 y = _thd.selstart.y;
2841 } else if (h > w * 2) { // still count as y dir?
2842 b = HT_LINE | HT_DIR_Y;
2843 x = _thd.selstart.x;
2844 } else { // complicated direction
2845 int d = w - h;
2846 _thd.selend.x = _thd.selend.x & ~TILE_UNIT_MASK;
2847 _thd.selend.y = _thd.selend.y & ~TILE_UNIT_MASK;
2848
2849 /* four cases. */
2850 if (x > _thd.selstart.x) {
2851 if (y > _thd.selstart.y) {
2852 /* south */
2853 if (d == 0) {
2854 b = (x & TILE_UNIT_MASK) > (y & TILE_UNIT_MASK) ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR;
2855 } else if (d >= 0) {
2856 x = _thd.selstart.x + h;
2857 b = HT_LINE | HT_DIR_VL;
2858 } else {
2859 y = _thd.selstart.y + w;
2860 b = HT_LINE | HT_DIR_VR;
2861 }
2862 } else {
2863 /* west */
2864 if (d == 0) {
2865 b = (x & TILE_UNIT_MASK) + (y & TILE_UNIT_MASK) >= TILE_SIZE ? HT_LINE | HT_DIR_HL : HT_LINE | HT_DIR_HU;
2866 } else if (d >= 0) {
2867 x = _thd.selstart.x + h;
2868 b = HT_LINE | HT_DIR_HL;
2869 } else {
2870 y = _thd.selstart.y - w;
2871 b = HT_LINE | HT_DIR_HU;
2872 }
2873 }
2874 } else {
2875 if (y > _thd.selstart.y) {
2876 /* east */
2877 if (d == 0) {
2878 b = (x & TILE_UNIT_MASK) + (y & TILE_UNIT_MASK) >= TILE_SIZE ? HT_LINE | HT_DIR_HL : HT_LINE | HT_DIR_HU;
2879 } else if (d >= 0) {
2880 x = _thd.selstart.x - h;
2881 b = HT_LINE | HT_DIR_HU;
2882 } else {
2883 y = _thd.selstart.y + w;
2884 b = HT_LINE | HT_DIR_HL;
2885 }
2886 } else {
2887 /* north */
2888 if (d == 0) {
2889 b = (x & TILE_UNIT_MASK) > (y & TILE_UNIT_MASK) ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR;
2890 } else if (d >= 0) {
2891 x = _thd.selstart.x - h;
2892 b = HT_LINE | HT_DIR_VR;
2893 } else {
2894 y = _thd.selstart.y - w;
2895 b = HT_LINE | HT_DIR_VL;
2896 }
2897 }
2898 }
2899 }
2900
2901 if (_settings_client.gui.measure_tooltip) {
2902 TileIndex t0 = TileVirtXY(_thd.selstart.x, _thd.selstart.y);
2903 TileIndex t1 = TileVirtXY(x, y);
2904 uint distance = DistanceManhattan(t0, t1) + 1;
2905 byte index = 0;
2906 uint64 params[2];
2907
2908 if (distance != 1) {
2909 int heightdiff = CalcHeightdiff(b, distance, t0, t1);
2910 /* If we are showing a tooltip for horizontal or vertical drags,
2911 * 2 tiles have a length of 1. To bias towards the ceiling we add
2912 * one before division. It feels more natural to count 3 lengths as 2 */
2913 if ((b & HT_DIR_MASK) != HT_DIR_X && (b & HT_DIR_MASK) != HT_DIR_Y) {
2914 distance = CeilDiv(distance, 2);
2915 }
2916
2917 params[index++] = distance;
2918 if (heightdiff != 0) params[index++] = heightdiff;
2919 }
2920
2921 ShowMeasurementTooltips(measure_strings_length[index], index, params);
2922 }
2923
2924 _thd.selend.x = x;
2925 _thd.selend.y = y;
2926 _thd.next_drawstyle = b;
2927 }
2928
2936 void VpSelectTilesWithMethod(int x, int y, ViewportPlaceMethod method)
2937 {
2938 int sx, sy;
2939 HighLightStyle style;
2940
2941 if (x == -1) {
2942 _thd.selend.x = -1;
2943 return;
2944 }
2945
2946 /* Special handling of drag in any (8-way) direction */
2947 if (method & (VPM_RAILDIRS | VPM_SIGNALDIRS)) {
2948 _thd.selend.x = x;
2949 _thd.selend.y = y;
2950 CalcRaildirsDrawstyle(x, y, method);
2951 return;
2952 }
2953
2954 /* Needed so level-land is placed correctly */
2955 if ((_thd.next_drawstyle & HT_DRAG_MASK) == HT_POINT) {
2956 x += TILE_SIZE / 2;
2957 y += TILE_SIZE / 2;
2958 }
2959
2960 sx = _thd.selstart.x;
2961 sy = _thd.selstart.y;
2962
2963 int limit = 0;
2964
2965 switch (method) {
2966 case VPM_X_OR_Y: // drag in X or Y direction
2967 if (abs(sy - y) < abs(sx - x)) {
2968 y = sy;
2969 style = HT_DIR_X;
2970 } else {
2971 x = sx;
2972 style = HT_DIR_Y;
2973 }
2974 goto calc_heightdiff_single_direction;
2975
2976 case VPM_X_LIMITED: // Drag in X direction (limited size).
2977 limit = (_thd.sizelimit - 1) * TILE_SIZE;
2978 /* FALL THROUGH */
2979
2980 case VPM_FIX_X: // drag in Y direction
2981 x = sx;
2982 style = HT_DIR_Y;
2983 goto calc_heightdiff_single_direction;
2984
2985 case VPM_Y_LIMITED: // Drag in Y direction (limited size).
2986 limit = (_thd.sizelimit - 1) * TILE_SIZE;
2987 /* FALL THROUGH */
2988
2989 case VPM_FIX_Y: // drag in X direction
2990 y = sy;
2991 style = HT_DIR_X;
2992
2993 calc_heightdiff_single_direction:;
2994 if (limit > 0) {
2995 x = sx + Clamp(x - sx, -limit, limit);
2996 y = sy + Clamp(y - sy, -limit, limit);
2997 }
2998 if (_settings_client.gui.measure_tooltip) {
2999 TileIndex t0 = TileVirtXY(sx, sy);
3000 TileIndex t1 = TileVirtXY(x, y);
3001 uint distance = DistanceManhattan(t0, t1) + 1;
3002 byte index = 0;
3003 uint64 params[2];
3004
3005 if (distance != 1) {
3006 /* With current code passing a HT_LINE style to calculate the height
3007 * difference is enough. However if/when a point-tool is created
3008 * with this method, function should be called with new_style (below)
3009 * instead of HT_LINE | style case HT_POINT is handled specially
3010 * new_style := (_thd.next_drawstyle & HT_RECT) ? HT_LINE | style : _thd.next_drawstyle; */
3011 int heightdiff = CalcHeightdiff(HT_LINE | style, 0, t0, t1);
3012
3013 params[index++] = distance;
3014 if (heightdiff != 0) params[index++] = heightdiff;
3015 }
3016
3017 ShowMeasurementTooltips(measure_strings_length[index], index, params);
3018 }
3019 break;
3020
3021 case VPM_X_AND_Y_LIMITED: // Drag an X by Y constrained rect area.
3022 limit = (_thd.sizelimit - 1) * TILE_SIZE;
3023 x = sx + Clamp(x - sx, -limit, limit);
3024 y = sy + Clamp(y - sy, -limit, limit);
3025 /* FALL THROUGH */
3026
3027 case VPM_X_AND_Y: // drag an X by Y area
3028 if (_settings_client.gui.measure_tooltip) {
3029 static const StringID measure_strings_area[] = {
3030 STR_NULL, STR_NULL, STR_MEASURE_AREA, STR_MEASURE_AREA_HEIGHTDIFF
3031 };
3032
3033 TileIndex t0 = TileVirtXY(sx, sy);
3034 TileIndex t1 = TileVirtXY(x, y);
3035 uint dx = Delta(TileX(t0), TileX(t1)) + 1;
3036 uint dy = Delta(TileY(t0), TileY(t1)) + 1;
3037 byte index = 0;
3038 uint64 params[3];
3039
3040 /* If dragging an area (eg dynamite tool) and it is actually a single
3041 * row/column, change the type to 'line' to get proper calculation for height */
3042 style = (HighLightStyle)_thd.next_drawstyle;
3043 if (_thd.IsDraggingDiagonal()) {
3044 /* Determine the "area" of the diagonal dragged selection.
3045 * We assume the area is the number of tiles along the X
3046 * edge and the number of tiles along the Y edge. However,
3047 * multiplying these two numbers does not give the exact
3048 * number of tiles; basically we are counting the black
3049 * squares on a chess board and ignore the white ones to
3050 * make the tile counts at the edges match up. There is no
3051 * other way to make a proper count though.
3052 *
3053 * First convert to the rotated coordinate system. */
3054 int dist_x = TileX(t0) - TileX(t1);
3055 int dist_y = TileY(t0) - TileY(t1);
3056 int a_max = dist_x + dist_y;
3057 int b_max = dist_y - dist_x;
3058
3059 /* Now determine the size along the edge, but due to the
3060 * chess board principle this counts double. */
3061 a_max = abs(a_max + (a_max > 0 ? 2 : -2)) / 2;
3062 b_max = abs(b_max + (b_max > 0 ? 2 : -2)) / 2;
3063
3064 /* We get a 1x1 on normal 2x1 rectangles, due to it being
3065 * a seen as two sides. As the result for actual building
3066 * will be the same as non-diagonal dragging revert to that
3067 * behaviour to give it a more normally looking size. */
3068 if (a_max != 1 || b_max != 1) {
3069 dx = a_max;
3070 dy = b_max;
3071 }
3072 } else if (style & HT_RECT) {
3073 if (dx == 1) {
3074 style = HT_LINE | HT_DIR_Y;
3075 } else if (dy == 1) {
3076 style = HT_LINE | HT_DIR_X;
3077 }
3078 }
3079
3080 if (dx != 1 || dy != 1) {
3081 int heightdiff = CalcHeightdiff(style, 0, t0, t1);
3082
3083 params[index++] = dx - (style & HT_POINT ? 1 : 0);
3084 params[index++] = dy - (style & HT_POINT ? 1 : 0);
3085 if (heightdiff != 0) params[index++] = heightdiff;
3086 }
3087
3088 ShowMeasurementTooltips(measure_strings_area[index], index, params);
3089 }
3090 break;
3091
3092 default: NOT_REACHED();
3093 }
3094
3095 _thd.selend.x = x;
3096 _thd.selend.y = y;
3097 }
3098
3103 EventState VpHandlePlaceSizingDrag()
3104 {
3105 if (_special_mouse_mode != WSM_SIZING) return ES_NOT_HANDLED;
3106
3107 /* stop drag mode if the window has been closed */
3108 Window *w = _thd.GetCallbackWnd();
3109 if (w == NULL) {
3110 ResetObjectToPlace();
3111 return ES_HANDLED;
3112 }
3113
3114 /* while dragging execute the drag procedure of the corresponding window (mostly VpSelectTilesWithMethod() ) */
3115 if (_left_button_down) {
3116 w->OnPlaceDrag(_thd.select_method, _thd.select_proc, GetTileBelowCursor());
3117 return ES_HANDLED;
3118 }
3119
3120 /* mouse button released..
3121 * keep the selected tool, but reset it to the original mode. */
3122 _special_mouse_mode = WSM_NONE;
3123 HighLightStyle others = _thd.place_mode & ~(HT_DRAG_MASK | HT_DIR_MASK);
3124 if ((_thd.next_drawstyle & HT_DRAG_MASK) == HT_RECT) {
3125 _thd.place_mode = HT_RECT | others;
3126 } else if (_thd.select_method & VPM_SIGNALDIRS) {
3127 _thd.place_mode = HT_RECT | others;
3128 } else if (_thd.select_method & VPM_RAILDIRS) {
3129 _thd.place_mode = (_thd.select_method & ~VPM_RAILDIRS) ? _thd.next_drawstyle : (HT_RAIL | others);
3130 } else {
3131 _thd.place_mode = HT_POINT | others;
3132 }
3133 SetTileSelectSize(1, 1);
3134
3135 w->OnPlaceMouseUp(_thd.select_method, _thd.select_proc, _thd.selend, TileVirtXY(_thd.selstart.x, _thd.selstart.y), TileVirtXY(_thd.selend.x, _thd.selend.y));
3136
3137 return ES_HANDLED;
3138 }
3139
3140 void SetObjectToPlaceWnd(CursorID icon, PaletteID pal, HighLightStyle mode, Window *w)
3141 {
3142 SetObjectToPlace(icon, pal, mode, w->window_class, w->window_number);
3143 }
3144
3145 #include "table/animcursors.h"
3146
3147 void SetObjectToPlace(CursorID icon, PaletteID pal, HighLightStyle mode, WindowClass window_class, WindowNumber window_num)
3148 {
3149 if (_thd.window_class != WC_INVALID) {
3150 /* Undo clicking on button and drag & drop */
3151 Window *w = _thd.GetCallbackWnd();
3152 /* Call the abort function, but set the window class to something
3153 * that will never be used to avoid infinite loops. Setting it to
3154 * the 'next' window class must not be done because recursion into
3155 * this function might in some cases reset the newly set object to
3156 * place or not properly reset the original selection. */
3157 _thd.window_class = WC_INVALID;
3158 if (w != NULL) w->OnPlaceObjectAbort();
3159 }
3160
3161 /* Mark the old selection dirty, in case the selection shape or colour changes */
3162 if ((_thd.drawstyle & HT_DRAG_MASK) != HT_NONE) SetSelectionTilesDirty();
3163
3164 SetTileSelectSize(1, 1);
3165
3166 _thd.make_square_red = false;
3167
3168 if (mode == HT_DRAG) { // HT_DRAG is for dragdropping trains in the depot window
3169 mode = HT_NONE;
3170 _special_mouse_mode = WSM_DRAGDROP;
3171 } else {
3172 _special_mouse_mode = WSM_NONE;
3173 }
3174
3175 _thd.place_mode = mode;
3176 _thd.window_class = window_class;
3177 _thd.window_number = window_num;
3178
3179 if ((mode & HT_DRAG_MASK) == HT_SPECIAL) { // special tools, like tunnels or docks start with presizing mode
3180 VpStartPreSizing();
3181 }
3182
3183 if ((icon & ANIMCURSOR_FLAG) != 0) {
3184 SetAnimatedMouseCursor(_animcursors[icon & ~ANIMCURSOR_FLAG]);
3185 } else {
3186 SetMouseCursor(icon, pal);
3187 }
3188
3189 }
3190
3191 void ResetObjectToPlace()
3192 {
3193 SetObjectToPlace(SPR_CURSOR_MOUSE, PAL_NONE, HT_NONE, WC_MAIN_WINDOW, 0);
3194 }
3195
3196 Point GetViewportStationMiddle(const ViewPort *vp, const Station *st)
3197 {
3198 int x = TileX(st->xy) * TILE_SIZE;
3199 int y = TileY(st->xy) * TILE_SIZE;
3200 int z = GetSlopePixelZ(Clamp(x, 0, MapSizeX() * TILE_SIZE - 1), Clamp(y, 0, MapSizeY() * TILE_SIZE - 1));
3201
3202 Point p = RemapCoords(x, y, z);
3203 p.x = UnScaleByZoom(p.x - vp->virtual_left, vp->zoom) + vp->left;
3204 p.y = UnScaleByZoom(p.y - vp->virtual_top, vp->zoom) + vp->top;
3205 return p;
3206 }
3207
3209 struct ViewportSSCSS {
3210 VpSorterChecker fct_checker;
3211 VpSpriteSorter fct_sorter;
3212 };
3213
3215 static ViewportSSCSS _vp_sprite_sorters[] = {
3216 #ifdef WITH_SSE
3217 { &ViewportSortParentSpritesSSE41Checker, &ViewportSortParentSpritesSSE41 },
3218 #endif
3219 { &ViewportSortParentSpritesChecker, &ViewportSortParentSprites }
3220 };
3221
3223 void InitializeSpriteSorter()
3224 {
3225 for (uint i = 0; i < lengthof(_vp_sprite_sorters); i++) {
3226 if (_vp_sprite_sorters[i].fct_checker()) {
3227 _vp_sprite_sorter = _vp_sprite_sorters[i].fct_sorter;
3228 break;
3229 }
3230 }
3231 assert(_vp_sprite_sorter != NULL);
3232 }