00001
00002
00003
00004
00005
00006
00007
00008
00009
00029 #include "stdafx.h"
00030 #include "landscape.h"
00031 #include "viewport_func.h"
00032 #include "station_base.h"
00033 #include "waypoint_base.h"
00034 #include "town.h"
00035 #include "signs_base.h"
00036 #include "signs_func.h"
00037 #include "vehicle_base.h"
00038 #include "vehicle_gui.h"
00039 #include "blitter/factory.hpp"
00040 #include "strings_func.h"
00041 #include "zoom_func.h"
00042 #include "vehicle_func.h"
00043 #include "company_func.h"
00044 #include "waypoint_func.h"
00045 #include "window_func.h"
00046 #include "tilehighlight_func.h"
00047 #include "window_gui.h"
00048 #include "linkgraph/linkgraph_gui.h"
00049 #include "viewport_sprite_sorter.h"
00050
00051 #include "table/strings.h"
00052 #include "table/palettes.h"
00053
00054 Point _tile_fract_coords;
00055
00056 struct StringSpriteToDraw {
00057 StringID string;
00058 Colours colour;
00059 int32 x;
00060 int32 y;
00061 uint64 params[2];
00062 uint16 width;
00063 };
00064
00065 struct TileSpriteToDraw {
00066 SpriteID image;
00067 PaletteID pal;
00068 const SubSprite *sub;
00069 int32 x;
00070 int32 y;
00071 };
00072
00073 struct ChildScreenSpriteToDraw {
00074 SpriteID image;
00075 PaletteID pal;
00076 const SubSprite *sub;
00077 int32 x;
00078 int32 y;
00079 int next;
00080 };
00081
00083 enum FoundationPart {
00084 FOUNDATION_PART_NONE = 0xFF,
00085 FOUNDATION_PART_NORMAL = 0,
00086 FOUNDATION_PART_HALFTILE = 1,
00087 FOUNDATION_PART_END
00088 };
00089
00094 enum SpriteCombineMode {
00095 SPRITE_COMBINE_NONE,
00096 SPRITE_COMBINE_PENDING,
00097 SPRITE_COMBINE_ACTIVE,
00098 };
00099
00100 typedef SmallVector<TileSpriteToDraw, 64> TileSpriteToDrawVector;
00101 typedef SmallVector<StringSpriteToDraw, 4> StringSpriteToDrawVector;
00102 typedef SmallVector<ParentSpriteToDraw, 64> ParentSpriteToDrawVector;
00103 typedef SmallVector<ChildScreenSpriteToDraw, 16> ChildScreenSpriteToDrawVector;
00104
00106 struct ViewportDrawer {
00107 DrawPixelInfo dpi;
00108
00109 StringSpriteToDrawVector string_sprites_to_draw;
00110 TileSpriteToDrawVector tile_sprites_to_draw;
00111 ParentSpriteToDrawVector parent_sprites_to_draw;
00112 ParentSpriteToSortVector parent_sprites_to_sort;
00113 ChildScreenSpriteToDrawVector child_screen_sprites_to_draw;
00114
00115 int *last_child;
00116
00117 SpriteCombineMode combine_sprites;
00118
00119 int foundation[FOUNDATION_PART_END];
00120 FoundationPart foundation_part;
00121 int *last_foundation_child[FOUNDATION_PART_END];
00122 Point foundation_offset[FOUNDATION_PART_END];
00123 };
00124
00125 static void MarkViewportDirty(const ViewPort *vp, int left, int top, int right, int bottom);
00126
00127 static ViewportDrawer _vd;
00128
00129 TileHighlightData _thd;
00130 static TileInfo *_cur_ti;
00131 bool _draw_bounding_boxes = false;
00132 bool _draw_dirty_blocks = false;
00133 uint _dirty_block_colour = 0;
00134 static VpSpriteSorter _vp_sprite_sorter = NULL;
00135
00136 static Point MapXYZToViewport(const ViewPort *vp, int x, int y, int z)
00137 {
00138 Point p = RemapCoords(x, y, z);
00139 p.x -= vp->virtual_width / 2;
00140 p.y -= vp->virtual_height / 2;
00141 return p;
00142 }
00143
00144 void DeleteWindowViewport(Window *w)
00145 {
00146 if (w->viewport == NULL) return;
00147
00148 delete w->viewport->overlay;
00149 free(w->viewport);
00150 w->viewport = NULL;
00151 }
00152
00165 void InitializeWindowViewport(Window *w, int x, int y,
00166 int width, int height, uint32 follow_flags, ZoomLevel zoom)
00167 {
00168 assert(w->viewport == NULL);
00169
00170 ViewportData *vp = CallocT<ViewportData>(1);
00171
00172 vp->left = x + w->left;
00173 vp->top = y + w->top;
00174 vp->width = width;
00175 vp->height = height;
00176
00177 vp->zoom = static_cast<ZoomLevel>(Clamp(zoom, _settings_client.gui.zoom_min, _settings_client.gui.zoom_max));
00178
00179 vp->virtual_width = ScaleByZoom(width, zoom);
00180 vp->virtual_height = ScaleByZoom(height, zoom);
00181
00182 Point pt;
00183
00184 if (follow_flags & 0x80000000) {
00185 const Vehicle *veh;
00186
00187 vp->follow_vehicle = (VehicleID)(follow_flags & 0xFFFFF);
00188 veh = Vehicle::Get(vp->follow_vehicle);
00189 pt = MapXYZToViewport(vp, veh->x_pos, veh->y_pos, veh->z_pos);
00190 } else {
00191 uint x = TileX(follow_flags) * TILE_SIZE;
00192 uint y = TileY(follow_flags) * TILE_SIZE;
00193
00194 vp->follow_vehicle = INVALID_VEHICLE;
00195 pt = MapXYZToViewport(vp, x, y, GetSlopePixelZ(x, y));
00196 }
00197
00198 vp->scrollpos_x = pt.x;
00199 vp->scrollpos_y = pt.y;
00200 vp->dest_scrollpos_x = pt.x;
00201 vp->dest_scrollpos_y = pt.y;
00202
00203 vp->overlay = NULL;
00204
00205 w->viewport = vp;
00206 vp->virtual_left = 0;
00207 vp->virtual_top = 0;
00208 }
00209
00210 static Point _vp_move_offs;
00211
00212 static void DoSetViewportPosition(const Window *w, int left, int top, int width, int height)
00213 {
00214 FOR_ALL_WINDOWS_FROM_BACK_FROM(w, w) {
00215 if (left + width > w->left &&
00216 w->left + w->width > left &&
00217 top + height > w->top &&
00218 w->top + w->height > top) {
00219
00220 if (left < w->left) {
00221 DoSetViewportPosition(w, left, top, w->left - left, height);
00222 DoSetViewportPosition(w, left + (w->left - left), top, width - (w->left - left), height);
00223 return;
00224 }
00225
00226 if (left + width > w->left + w->width) {
00227 DoSetViewportPosition(w, left, top, (w->left + w->width - left), height);
00228 DoSetViewportPosition(w, left + (w->left + w->width - left), top, width - (w->left + w->width - left), height);
00229 return;
00230 }
00231
00232 if (top < w->top) {
00233 DoSetViewportPosition(w, left, top, width, (w->top - top));
00234 DoSetViewportPosition(w, left, top + (w->top - top), width, height - (w->top - top));
00235 return;
00236 }
00237
00238 if (top + height > w->top + w->height) {
00239 DoSetViewportPosition(w, left, top, width, (w->top + w->height - top));
00240 DoSetViewportPosition(w, left, top + (w->top + w->height - top), width, height - (w->top + w->height - top));
00241 return;
00242 }
00243
00244 return;
00245 }
00246 }
00247
00248 {
00249 int xo = _vp_move_offs.x;
00250 int yo = _vp_move_offs.y;
00251
00252 if (abs(xo) >= width || abs(yo) >= height) {
00253
00254 RedrawScreenRect(left, top, left + width, top + height);
00255 return;
00256 }
00257
00258 GfxScroll(left, top, width, height, xo, yo);
00259
00260 if (xo > 0) {
00261 RedrawScreenRect(left, top, xo + left, top + height);
00262 left += xo;
00263 width -= xo;
00264 } else if (xo < 0) {
00265 RedrawScreenRect(left + width + xo, top, left + width, top + height);
00266 width += xo;
00267 }
00268
00269 if (yo > 0) {
00270 RedrawScreenRect(left, top, width + left, top + yo);
00271 } else if (yo < 0) {
00272 RedrawScreenRect(left, top + height + yo, width + left, top + height);
00273 }
00274 }
00275 }
00276
00277 static void SetViewportPosition(Window *w, int x, int y)
00278 {
00279 ViewPort *vp = w->viewport;
00280 int old_left = vp->virtual_left;
00281 int old_top = vp->virtual_top;
00282 int i;
00283 int left, top, width, height;
00284
00285 vp->virtual_left = x;
00286 vp->virtual_top = y;
00287
00288
00289
00290
00291 old_left = UnScaleByZoomLower(old_left, vp->zoom);
00292 old_top = UnScaleByZoomLower(old_top, vp->zoom);
00293 x = UnScaleByZoomLower(x, vp->zoom);
00294 y = UnScaleByZoomLower(y, vp->zoom);
00295
00296 old_left -= x;
00297 old_top -= y;
00298
00299 if (old_top == 0 && old_left == 0) return;
00300
00301 _vp_move_offs.x = old_left;
00302 _vp_move_offs.y = old_top;
00303
00304 left = vp->left;
00305 top = vp->top;
00306 width = vp->width;
00307 height = vp->height;
00308
00309 if (left < 0) {
00310 width += left;
00311 left = 0;
00312 }
00313
00314 i = left + width - _screen.width;
00315 if (i >= 0) width -= i;
00316
00317 if (width > 0) {
00318 if (top < 0) {
00319 height += top;
00320 top = 0;
00321 }
00322
00323 i = top + height - _screen.height;
00324 if (i >= 0) height -= i;
00325
00326 if (height > 0) DoSetViewportPosition(w->z_front, left, top, width, height);
00327 }
00328 }
00329
00338 ViewPort *IsPtInWindowViewport(const Window *w, int x, int y)
00339 {
00340 ViewPort *vp = w->viewport;
00341
00342 if (vp != NULL &&
00343 IsInsideMM(x, vp->left, vp->left + vp->width) &&
00344 IsInsideMM(y, vp->top, vp->top + vp->height))
00345 return vp;
00346
00347 return NULL;
00348 }
00349
00357 static Point TranslateXYToTileCoord(const ViewPort *vp, int x, int y)
00358 {
00359 Point pt;
00360 int a, b;
00361 int z;
00362
00363 if ( (uint)(x -= vp->left) >= (uint)vp->width ||
00364 (uint)(y -= vp->top) >= (uint)vp->height) {
00365 Point pt = {-1, -1};
00366 return pt;
00367 }
00368
00369 x = (ScaleByZoom(x, vp->zoom) + vp->virtual_left) >> (2 + ZOOM_LVL_SHIFT);
00370 y = (ScaleByZoom(y, vp->zoom) + vp->virtual_top) >> (1 + ZOOM_LVL_SHIFT);
00371
00372 a = y - x;
00373 b = y + x;
00374
00375
00376
00377
00378 a = Clamp(a, -4 * (int)TILE_SIZE, (int)(MapMaxX() * TILE_SIZE) - 1);
00379 b = Clamp(b, -4 * (int)TILE_SIZE, (int)(MapMaxY() * TILE_SIZE) - 1);
00380
00381
00382
00383
00384
00385
00386
00387
00388 z = 0;
00389
00390 int min_coord = _settings_game.construction.freeform_edges ? TILE_SIZE : 0;
00391
00392 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;
00393 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;
00394 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;
00395
00396 pt.x = Clamp(a + z, min_coord, MapMaxX() * TILE_SIZE - 1);
00397 pt.y = Clamp(b + z, min_coord, MapMaxY() * TILE_SIZE - 1);
00398
00399 return pt;
00400 }
00401
00402
00403
00404
00405 static Point GetTileFromScreenXY(int x, int y, int zoom_x, int zoom_y)
00406 {
00407 Window *w;
00408 ViewPort *vp;
00409 Point pt;
00410
00411 if ( (w = FindWindowFromPt(x, y)) != NULL &&
00412 (vp = IsPtInWindowViewport(w, x, y)) != NULL)
00413 return TranslateXYToTileCoord(vp, zoom_x, zoom_y);
00414
00415 pt.y = pt.x = -1;
00416 return pt;
00417 }
00418
00419 Point GetTileBelowCursor()
00420 {
00421 return GetTileFromScreenXY(_cursor.pos.x, _cursor.pos.y, _cursor.pos.x, _cursor.pos.y);
00422 }
00423
00424
00425 Point GetTileZoomCenterWindow(bool in, Window * w)
00426 {
00427 int x, y;
00428 ViewPort *vp = w->viewport;
00429
00430 if (in) {
00431 x = ((_cursor.pos.x - vp->left) >> 1) + (vp->width >> 2);
00432 y = ((_cursor.pos.y - vp->top) >> 1) + (vp->height >> 2);
00433 } else {
00434 x = vp->width - (_cursor.pos.x - vp->left);
00435 y = vp->height - (_cursor.pos.y - vp->top);
00436 }
00437
00438 return GetTileFromScreenXY(_cursor.pos.x, _cursor.pos.y, x + vp->left, y + vp->top);
00439 }
00440
00449 void HandleZoomMessage(Window *w, const ViewPort *vp, byte widget_zoom_in, byte widget_zoom_out)
00450 {
00451 w->SetWidgetDisabledState(widget_zoom_in, vp->zoom <= _settings_client.gui.zoom_min);
00452 w->SetWidgetDirty(widget_zoom_in);
00453
00454 w->SetWidgetDisabledState(widget_zoom_out, vp->zoom >= _settings_client.gui.zoom_max);
00455 w->SetWidgetDirty(widget_zoom_out);
00456 }
00457
00470 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)
00471 {
00472 assert((image & SPRITE_MASK) < MAX_SPRITES);
00473
00474 TileSpriteToDraw *ts = _vd.tile_sprites_to_draw.Append();
00475 ts->image = image;
00476 ts->pal = pal;
00477 ts->sub = sub;
00478 Point pt = RemapCoords(x, y, z);
00479 ts->x = pt.x + extra_offs_x;
00480 ts->y = pt.y + extra_offs_y;
00481 }
00482
00495 static void AddChildSpriteToFoundation(SpriteID image, PaletteID pal, const SubSprite *sub, FoundationPart foundation_part, int extra_offs_x, int extra_offs_y)
00496 {
00497 assert(IsInsideMM(foundation_part, 0, FOUNDATION_PART_END));
00498 assert(_vd.foundation[foundation_part] != -1);
00499 Point offs = _vd.foundation_offset[foundation_part];
00500
00501
00502 int *old_child = _vd.last_child;
00503 _vd.last_child = _vd.last_foundation_child[foundation_part];
00504
00505 AddChildSpriteScreen(image, pal, offs.x + extra_offs_x, offs.y + extra_offs_y, false, sub, false);
00506
00507
00508 _vd.last_child = old_child;
00509 }
00510
00524 void DrawGroundSpriteAt(SpriteID image, PaletteID pal, int32 x, int32 y, int z, const SubSprite *sub, int extra_offs_x, int extra_offs_y)
00525 {
00526
00527 if (_vd.foundation_part == FOUNDATION_PART_NONE) _vd.foundation_part = FOUNDATION_PART_NORMAL;
00528
00529 if (_vd.foundation[_vd.foundation_part] != -1) {
00530 Point pt = RemapCoords(x, y, z);
00531 AddChildSpriteToFoundation(image, pal, sub, _vd.foundation_part, pt.x + extra_offs_x * ZOOM_LVL_BASE, pt.y + extra_offs_y * ZOOM_LVL_BASE);
00532 } else {
00533 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);
00534 }
00535 }
00536
00547 void DrawGroundSprite(SpriteID image, PaletteID pal, const SubSprite *sub, int extra_offs_x, int extra_offs_y)
00548 {
00549 DrawGroundSpriteAt(image, pal, 0, 0, 0, sub, extra_offs_x, extra_offs_y);
00550 }
00551
00559 void OffsetGroundSprite(int x, int y)
00560 {
00561
00562 switch (_vd.foundation_part) {
00563 case FOUNDATION_PART_NONE:
00564 _vd.foundation_part = FOUNDATION_PART_NORMAL;
00565 break;
00566 case FOUNDATION_PART_NORMAL:
00567 _vd.foundation_part = FOUNDATION_PART_HALFTILE;
00568 break;
00569 default: NOT_REACHED();
00570 }
00571
00572
00573 if (_vd.last_child != NULL) _vd.foundation[_vd.foundation_part] = _vd.parent_sprites_to_draw.Length() - 1;
00574
00575 _vd.foundation_offset[_vd.foundation_part].x = x * ZOOM_LVL_BASE;
00576 _vd.foundation_offset[_vd.foundation_part].y = y * ZOOM_LVL_BASE;
00577 _vd.last_foundation_child[_vd.foundation_part] = _vd.last_child;
00578 }
00579
00591 static void AddCombinedSprite(SpriteID image, PaletteID pal, int x, int y, int z, const SubSprite *sub)
00592 {
00593 Point pt = RemapCoords(x, y, z);
00594 const Sprite *spr = GetSprite(image & SPRITE_MASK, ST_NORMAL);
00595
00596 if (pt.x + spr->x_offs >= _vd.dpi.left + _vd.dpi.width ||
00597 pt.x + spr->x_offs + spr->width <= _vd.dpi.left ||
00598 pt.y + spr->y_offs >= _vd.dpi.top + _vd.dpi.height ||
00599 pt.y + spr->y_offs + spr->height <= _vd.dpi.top)
00600 return;
00601
00602 const ParentSpriteToDraw *pstd = _vd.parent_sprites_to_draw.End() - 1;
00603 AddChildSpriteScreen(image, pal, pt.x - pstd->left, pt.y - pstd->top, false, sub, false);
00604 }
00605
00631 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)
00632 {
00633 int32 left, right, top, bottom;
00634
00635 assert((image & SPRITE_MASK) < MAX_SPRITES);
00636
00637
00638 if (transparent) {
00639 SetBit(image, PALETTE_MODIFIER_TRANSPARENT);
00640 pal = PALETTE_TO_TRANSPARENT;
00641 }
00642
00643 if (_vd.combine_sprites == SPRITE_COMBINE_ACTIVE) {
00644 AddCombinedSprite(image, pal, x, y, z, sub);
00645 return;
00646 }
00647
00648 _vd.last_child = NULL;
00649
00650 Point pt = RemapCoords(x, y, z);
00651 int tmp_left, tmp_top, tmp_x = pt.x, tmp_y = pt.y;
00652
00653
00654 if (image == SPR_EMPTY_BOUNDING_BOX) {
00655 left = tmp_left = RemapCoords(x + w , y + bb_offset_y, z + bb_offset_z).x;
00656 right = RemapCoords(x + bb_offset_x, y + h , z + bb_offset_z).x + 1;
00657 top = tmp_top = RemapCoords(x + bb_offset_x, y + bb_offset_y, z + dz ).y;
00658 bottom = RemapCoords(x + w , y + h , z + bb_offset_z).y + 1;
00659 } else {
00660 const Sprite *spr = GetSprite(image & SPRITE_MASK, ST_NORMAL);
00661 left = tmp_left = (pt.x += spr->x_offs);
00662 right = (pt.x + spr->width );
00663 top = tmp_top = (pt.y += spr->y_offs);
00664 bottom = (pt.y + spr->height);
00665 }
00666
00667 if (_draw_bounding_boxes && (image != SPR_EMPTY_BOUNDING_BOX)) {
00668
00669 left = min(left , RemapCoords(x + w , y + bb_offset_y, z + bb_offset_z).x);
00670 right = max(right , RemapCoords(x + bb_offset_x, y + h , z + bb_offset_z).x + 1);
00671 top = min(top , RemapCoords(x + bb_offset_x, y + bb_offset_y, z + dz ).y);
00672 bottom = max(bottom, RemapCoords(x + w , y + h , z + bb_offset_z).y + 1);
00673 }
00674
00675
00676 if (left >= _vd.dpi.left + _vd.dpi.width ||
00677 right <= _vd.dpi.left ||
00678 top >= _vd.dpi.top + _vd.dpi.height ||
00679 bottom <= _vd.dpi.top) {
00680 return;
00681 }
00682
00683 ParentSpriteToDraw *ps = _vd.parent_sprites_to_draw.Append();
00684 ps->x = tmp_x;
00685 ps->y = tmp_y;
00686
00687 ps->left = tmp_left;
00688 ps->top = tmp_top;
00689
00690 ps->image = image;
00691 ps->pal = pal;
00692 ps->sub = sub;
00693 ps->xmin = x + bb_offset_x;
00694 ps->xmax = x + max(bb_offset_x, w) - 1;
00695
00696 ps->ymin = y + bb_offset_y;
00697 ps->ymax = y + max(bb_offset_y, h) - 1;
00698
00699 ps->zmin = z + bb_offset_z;
00700 ps->zmax = z + max(bb_offset_z, dz) - 1;
00701
00702 ps->comparison_done = false;
00703 ps->first_child = -1;
00704
00705 _vd.last_child = &ps->first_child;
00706
00707 if (_vd.combine_sprites == SPRITE_COMBINE_PENDING) _vd.combine_sprites = SPRITE_COMBINE_ACTIVE;
00708 }
00709
00728 void StartSpriteCombine()
00729 {
00730 assert(_vd.combine_sprites == SPRITE_COMBINE_NONE);
00731 _vd.combine_sprites = SPRITE_COMBINE_PENDING;
00732 }
00733
00738 void EndSpriteCombine()
00739 {
00740 assert(_vd.combine_sprites != SPRITE_COMBINE_NONE);
00741 _vd.combine_sprites = SPRITE_COMBINE_NONE;
00742 }
00743
00753 static bool IsInRangeInclusive(int begin, int end, int check)
00754 {
00755 if (begin > end) Swap(begin, end);
00756 return begin <= check && check <= end;
00757 }
00758
00765 bool IsInsideRotatedRectangle(int x, int y)
00766 {
00767 int dist_a = (_thd.size.x + _thd.size.y);
00768 int dist_b = (_thd.size.x - _thd.size.y);
00769 int a = ((x - _thd.pos.x) + (y - _thd.pos.y));
00770 int b = ((x - _thd.pos.x) - (y - _thd.pos.y));
00771
00772
00773 return IsInRangeInclusive(dist_a, 0, a) && IsInRangeInclusive(dist_b, 0, b);
00774 }
00775
00786 void AddChildSpriteScreen(SpriteID image, PaletteID pal, int x, int y, bool transparent, const SubSprite *sub, bool scale)
00787 {
00788 assert((image & SPRITE_MASK) < MAX_SPRITES);
00789
00790
00791 if (_vd.last_child == NULL) return;
00792
00793
00794 if (transparent) {
00795 SetBit(image, PALETTE_MODIFIER_TRANSPARENT);
00796 pal = PALETTE_TO_TRANSPARENT;
00797 }
00798
00799 *_vd.last_child = _vd.child_screen_sprites_to_draw.Length();
00800
00801 ChildScreenSpriteToDraw *cs = _vd.child_screen_sprites_to_draw.Append();
00802 cs->image = image;
00803 cs->pal = pal;
00804 cs->sub = sub;
00805 cs->x = scale ? x * ZOOM_LVL_BASE : x;
00806 cs->y = scale ? y * ZOOM_LVL_BASE : y;
00807 cs->next = -1;
00808
00809
00810
00811
00812 if (_vd.last_foundation_child[0] == _vd.last_child) _vd.last_foundation_child[0] = &cs->next;
00813 if (_vd.last_foundation_child[1] == _vd.last_child) _vd.last_foundation_child[1] = &cs->next;
00814 _vd.last_child = &cs->next;
00815 }
00816
00817 static void AddStringToDraw(int x, int y, StringID string, uint64 params_1, uint64 params_2, Colours colour, uint16 width)
00818 {
00819 assert(width != 0);
00820 StringSpriteToDraw *ss = _vd.string_sprites_to_draw.Append();
00821 ss->string = string;
00822 ss->x = x;
00823 ss->y = y;
00824 ss->params[0] = params_1;
00825 ss->params[1] = params_2;
00826 ss->width = width;
00827 ss->colour = colour;
00828 }
00829
00830
00842 static void DrawSelectionSprite(SpriteID image, PaletteID pal, const TileInfo *ti, int z_offset, FoundationPart foundation_part)
00843 {
00844
00845 if (_vd.foundation[foundation_part] == -1) {
00846
00847 AddTileSpriteToDraw(image, pal, ti->x, ti->y, ti->z + z_offset);
00848 } else {
00849
00850 AddChildSpriteToFoundation(image, pal, NULL, foundation_part, 0, -z_offset * ZOOM_LVL_BASE);
00851 }
00852 }
00853
00860 static void DrawTileSelectionRect(const TileInfo *ti, PaletteID pal)
00861 {
00862 if (!IsValidTile(ti->tile)) return;
00863
00864 SpriteID sel;
00865 if (IsHalftileSlope(ti->tileh)) {
00866 Corner halftile_corner = GetHalftileSlopeCorner(ti->tileh);
00867 SpriteID sel2 = SPR_HALFTILE_SELECTION_FLAT + halftile_corner;
00868 DrawSelectionSprite(sel2, pal, ti, 7 + TILE_HEIGHT, FOUNDATION_PART_HALFTILE);
00869
00870 Corner opposite_corner = OppositeCorner(halftile_corner);
00871 if (IsSteepSlope(ti->tileh)) {
00872 sel = SPR_HALFTILE_SELECTION_DOWN;
00873 } else {
00874 sel = ((ti->tileh & SlopeWithOneCornerRaised(opposite_corner)) != 0 ? SPR_HALFTILE_SELECTION_UP : SPR_HALFTILE_SELECTION_FLAT);
00875 }
00876 sel += opposite_corner;
00877 } else {
00878 sel = SPR_SELECT_TILE + SlopeToSpriteOffset(ti->tileh);
00879 }
00880 DrawSelectionSprite(sel, pal, ti, 7, FOUNDATION_PART_NORMAL);
00881 }
00882
00883 static bool IsPartOfAutoLine(int px, int py)
00884 {
00885 px -= _thd.selstart.x;
00886 py -= _thd.selstart.y;
00887
00888 if ((_thd.drawstyle & HT_DRAG_MASK) != HT_LINE) return false;
00889
00890 switch (_thd.drawstyle & HT_DIR_MASK) {
00891 case HT_DIR_X: return py == 0;
00892 case HT_DIR_Y: return px == 0;
00893 case HT_DIR_HU: return px == -py || px == -py - 16;
00894 case HT_DIR_HL: return px == -py || px == -py + 16;
00895 case HT_DIR_VL: return px == py || px == py + 16;
00896 case HT_DIR_VR: return px == py || px == py - 16;
00897 default:
00898 NOT_REACHED();
00899 }
00900 }
00901
00902
00903 static const HighLightStyle _autorail_type[6][2] = {
00904 { HT_DIR_X, HT_DIR_X },
00905 { HT_DIR_Y, HT_DIR_Y },
00906 { HT_DIR_HU, HT_DIR_HL },
00907 { HT_DIR_HL, HT_DIR_HU },
00908 { HT_DIR_VL, HT_DIR_VR },
00909 { HT_DIR_VR, HT_DIR_VL }
00910 };
00911
00912 #include "table/autorail.h"
00913
00920 static void DrawAutorailSelection(const TileInfo *ti, uint autorail_type)
00921 {
00922 SpriteID image;
00923 PaletteID pal;
00924 int offset;
00925
00926 FoundationPart foundation_part = FOUNDATION_PART_NORMAL;
00927 Slope autorail_tileh = RemoveHalftileSlope(ti->tileh);
00928 if (IsHalftileSlope(ti->tileh)) {
00929 static const uint _lower_rail[4] = { 5U, 2U, 4U, 3U };
00930 Corner halftile_corner = GetHalftileSlopeCorner(ti->tileh);
00931 if (autorail_type != _lower_rail[halftile_corner]) {
00932 foundation_part = FOUNDATION_PART_HALFTILE;
00933
00934 autorail_tileh = SlopeWithThreeCornersRaised(OppositeCorner(halftile_corner));
00935 }
00936 }
00937
00938 offset = _AutorailTilehSprite[autorail_tileh][autorail_type];
00939 if (offset >= 0) {
00940 image = SPR_AUTORAIL_BASE + offset;
00941 pal = PAL_NONE;
00942 } else {
00943 image = SPR_AUTORAIL_BASE - offset;
00944 pal = PALETTE_SEL_TILE_RED;
00945 }
00946
00947 DrawSelectionSprite(image, _thd.make_square_red ? PALETTE_SEL_TILE_RED : pal, ti, 7, foundation_part);
00948 }
00949
00954 static void DrawTileSelection(const TileInfo *ti)
00955 {
00956
00957 bool is_redsq = _thd.redsq == ti->tile;
00958 if (is_redsq) DrawTileSelectionRect(ti, PALETTE_TILE_RED_PULSATING);
00959
00960
00961 if ((_thd.drawstyle & HT_DRAG_MASK) == HT_NONE) return;
00962
00963 if (_thd.diagonal) {
00964 if (IsInsideRotatedRectangle((int)ti->x, (int)ti->y)) goto draw_inner;
00965 return;
00966 }
00967
00968
00969 if (IsInsideBS(ti->x, _thd.pos.x, _thd.size.x) &&
00970 IsInsideBS(ti->y, _thd.pos.y, _thd.size.y)) {
00971 draw_inner:
00972 if (_thd.drawstyle & HT_RECT) {
00973 if (!is_redsq) DrawTileSelectionRect(ti, _thd.make_square_red ? PALETTE_SEL_TILE_RED : PAL_NONE);
00974 } else if (_thd.drawstyle & HT_POINT) {
00975
00976 int z = 0;
00977 FoundationPart foundation_part = FOUNDATION_PART_NORMAL;
00978 if (ti->tileh & SLOPE_N) {
00979 z += TILE_HEIGHT;
00980 if (RemoveHalftileSlope(ti->tileh) == SLOPE_STEEP_N) z += TILE_HEIGHT;
00981 }
00982 if (IsHalftileSlope(ti->tileh)) {
00983 Corner halftile_corner = GetHalftileSlopeCorner(ti->tileh);
00984 if ((halftile_corner == CORNER_W) || (halftile_corner == CORNER_E)) z += TILE_HEIGHT;
00985 if (halftile_corner != CORNER_S) {
00986 foundation_part = FOUNDATION_PART_HALFTILE;
00987 if (IsSteepSlope(ti->tileh)) z -= TILE_HEIGHT;
00988 }
00989 }
00990 DrawSelectionSprite(_cur_dpi->zoom <= ZOOM_LVL_DETAIL ? SPR_DOT : SPR_DOT_SMALL, PAL_NONE, ti, z, foundation_part);
00991 } else if (_thd.drawstyle & HT_RAIL) {
00992
00993 HighLightStyle type = _thd.drawstyle & HT_DIR_MASK;
00994 assert(type < HT_DIR_END);
00995 DrawAutorailSelection(ti, _autorail_type[type][0]);
00996 } else if (IsPartOfAutoLine(ti->x, ti->y)) {
00997
00998 HighLightStyle dir = _thd.drawstyle & HT_DIR_MASK;
00999 uint side;
01000
01001 if (dir == HT_DIR_X || dir == HT_DIR_Y) {
01002 side = 0;
01003 } else {
01004 TileIndex start = TileVirtXY(_thd.selstart.x, _thd.selstart.y);
01005 side = Delta(Delta(TileX(start), TileX(ti->tile)), Delta(TileY(start), TileY(ti->tile)));
01006 }
01007
01008 DrawAutorailSelection(ti, _autorail_type[dir][side]);
01009 }
01010 return;
01011 }
01012
01013
01014 if (!is_redsq && _thd.outersize.x > 0 &&
01015 IsInsideBS(ti->x, _thd.pos.x + _thd.offs.x, _thd.size.x + _thd.outersize.x) &&
01016 IsInsideBS(ti->y, _thd.pos.y + _thd.offs.y, _thd.size.y + _thd.outersize.y)) {
01017
01018 DrawTileSelectionRect(ti, PALETTE_SEL_TILE_BLUE);
01019 return;
01020 }
01021 }
01022
01023 static void ViewportAddLandscape()
01024 {
01025 int x, y, width, height;
01026 TileInfo ti;
01027 bool direction;
01028
01029 _cur_ti = &ti;
01030
01031
01032 x = ((_vd.dpi.top >> (1 + ZOOM_LVL_SHIFT)) - (_vd.dpi.left >> (2 + ZOOM_LVL_SHIFT))) & ~TILE_UNIT_MASK;
01033 y = ((_vd.dpi.top >> (1 + ZOOM_LVL_SHIFT)) + (_vd.dpi.left >> (2 + ZOOM_LVL_SHIFT)) - TILE_SIZE) & ~TILE_UNIT_MASK;
01034
01035
01036 {
01037 Point pt = RemapCoords(x, y, 241);
01038 width = (_vd.dpi.left + _vd.dpi.width - pt.x + 96 * ZOOM_LVL_BASE - 1) >> (6 + ZOOM_LVL_SHIFT);
01039 height = (_vd.dpi.top + _vd.dpi.height - pt.y) >> (5 + ZOOM_LVL_SHIFT) << 1;
01040 }
01041
01042 assert(width > 0);
01043 assert(height > 0);
01044
01045 direction = false;
01046
01047 do {
01048 int width_cur = width;
01049 uint x_cur = x;
01050 uint y_cur = y;
01051
01052 do {
01053 TileType tt = MP_VOID;
01054
01055 ti.x = x_cur;
01056 ti.y = y_cur;
01057
01058 ti.z = 0;
01059
01060 ti.tileh = SLOPE_FLAT;
01061 ti.tile = INVALID_TILE;
01062
01063 if (x_cur < MapMaxX() * TILE_SIZE &&
01064 y_cur < MapMaxY() * TILE_SIZE) {
01065 TileIndex tile = TileVirtXY(x_cur, y_cur);
01066
01067 if (!_settings_game.construction.freeform_edges || (TileX(tile) != 0 && TileY(tile) != 0)) {
01068 if (x_cur == ((int)MapMaxX() - 1) * TILE_SIZE || y_cur == ((int)MapMaxY() - 1) * TILE_SIZE) {
01069 uint maxh = max<uint>(TileHeight(tile), 1);
01070 for (uint h = 0; h < maxh; h++) {
01071 AddTileSpriteToDraw(SPR_SHADOW_CELL, PAL_NONE, ti.x, ti.y, h * TILE_HEIGHT);
01072 }
01073 }
01074
01075 ti.tile = tile;
01076 ti.tileh = GetTilePixelSlope(tile, &ti.z);
01077 tt = GetTileType(tile);
01078 }
01079 }
01080
01081 _vd.foundation_part = FOUNDATION_PART_NONE;
01082 _vd.foundation[0] = -1;
01083 _vd.foundation[1] = -1;
01084 _vd.last_foundation_child[0] = NULL;
01085 _vd.last_foundation_child[1] = NULL;
01086
01087 _tile_type_procs[tt]->draw_tile_proc(&ti);
01088
01089 if ((x_cur == (int)MapMaxX() * TILE_SIZE && IsInsideMM(y_cur, 0, MapMaxY() * TILE_SIZE + 1)) ||
01090 (y_cur == (int)MapMaxY() * TILE_SIZE && IsInsideMM(x_cur, 0, MapMaxX() * TILE_SIZE + 1))) {
01091 TileIndex tile = TileVirtXY(x_cur, y_cur);
01092 ti.tile = tile;
01093 ti.tileh = GetTilePixelSlope(tile, &ti.z);
01094 tt = GetTileType(tile);
01095 }
01096 if (ti.tile != INVALID_TILE) DrawTileSelection(&ti);
01097
01098 y_cur += 0x10;
01099 x_cur -= 0x10;
01100 } while (--width_cur);
01101
01102 if ((direction ^= 1) != 0) {
01103 y += 0x10;
01104 } else {
01105 x += 0x10;
01106 }
01107 } while (--height);
01108 }
01109
01120 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)
01121 {
01122 bool small = dpi->zoom >= small_from;
01123
01124 int left = dpi->left;
01125 int top = dpi->top;
01126 int right = left + dpi->width;
01127 int bottom = top + dpi->height;
01128
01129 int sign_height = ScaleByZoom(VPSM_TOP + FONT_HEIGHT_NORMAL + VPSM_BOTTOM, dpi->zoom);
01130 int sign_half_width = ScaleByZoom((small ? sign->width_small : sign->width_normal) / 2, dpi->zoom);
01131
01132 if (bottom < sign->top ||
01133 top > sign->top + sign_height ||
01134 right < sign->center - sign_half_width ||
01135 left > sign->center + sign_half_width) {
01136 return;
01137 }
01138
01139 if (!small) {
01140 AddStringToDraw(sign->center - sign_half_width, sign->top, string_normal, params_1, params_2, colour, sign->width_normal);
01141 } else {
01142 int shadow_offset = 0;
01143 if (string_small_shadow != STR_NULL) {
01144 shadow_offset = 4;
01145 AddStringToDraw(sign->center - sign_half_width + shadow_offset, sign->top, string_small_shadow, params_1, params_2, INVALID_COLOUR, sign->width_small);
01146 }
01147 AddStringToDraw(sign->center - sign_half_width, sign->top - shadow_offset, string_small, params_1, params_2,
01148 colour, sign->width_small | 0x8000);
01149 }
01150 }
01151
01152 static void ViewportAddTownNames(DrawPixelInfo *dpi)
01153 {
01154 if (!HasBit(_display_opt, DO_SHOW_TOWN_NAMES) || _game_mode == GM_MENU) return;
01155
01156 const Town *t;
01157 FOR_ALL_TOWNS(t) {
01158 ViewportAddString(dpi, ZOOM_LVL_OUT_16X, &t->cache.sign,
01159 _settings_client.gui.population_in_label ? STR_VIEWPORT_TOWN_POP : STR_VIEWPORT_TOWN,
01160 STR_VIEWPORT_TOWN_TINY_WHITE, STR_VIEWPORT_TOWN_TINY_BLACK,
01161 t->index, t->cache.population);
01162 }
01163 }
01164
01165
01166 static void ViewportAddStationNames(DrawPixelInfo *dpi)
01167 {
01168 if (!(HasBit(_display_opt, DO_SHOW_STATION_NAMES) || HasBit(_display_opt, DO_SHOW_WAYPOINT_NAMES)) || _game_mode == GM_MENU) return;
01169
01170 const BaseStation *st;
01171 FOR_ALL_BASE_STATIONS(st) {
01172
01173 bool is_station = Station::IsExpected(st);
01174
01175
01176 if (!HasBit(_display_opt, is_station ? DO_SHOW_STATION_NAMES : DO_SHOW_WAYPOINT_NAMES)) continue;
01177
01178
01179 if (!HasBit(_display_opt, DO_SHOW_COMPETITOR_SIGNS) && _local_company != st->owner && st->owner != OWNER_NONE) continue;
01180
01181 ViewportAddString(dpi, ZOOM_LVL_OUT_16X, &st->sign,
01182 is_station ? STR_VIEWPORT_STATION : STR_VIEWPORT_WAYPOINT,
01183 (is_station ? STR_VIEWPORT_STATION : STR_VIEWPORT_WAYPOINT) + 1, STR_NULL,
01184 st->index, st->facilities, (st->owner == OWNER_NONE || !st->IsInUse()) ? COLOUR_GREY : _company_colours[st->owner]);
01185 }
01186 }
01187
01188
01189 static void ViewportAddSigns(DrawPixelInfo *dpi)
01190 {
01191
01192 if (!HasBit(_display_opt, DO_SHOW_SIGNS) || IsInvisibilitySet(TO_SIGNS)) return;
01193
01194 const Sign *si;
01195 FOR_ALL_SIGNS(si) {
01196
01197
01198
01199 if (!HasBit(_display_opt, DO_SHOW_COMPETITOR_SIGNS) && _local_company != si->owner && si->owner != OWNER_DEITY) continue;
01200
01201 ViewportAddString(dpi, ZOOM_LVL_OUT_16X, &si->sign,
01202 STR_WHITE_SIGN,
01203 (IsTransparencySet(TO_SIGNS) || si->owner == OWNER_DEITY) ? STR_VIEWPORT_SIGN_SMALL_WHITE : STR_VIEWPORT_SIGN_SMALL_BLACK, STR_NULL,
01204 si->index, 0, (si->owner == OWNER_NONE) ? COLOUR_GREY : (si->owner == OWNER_DEITY ? INVALID_COLOUR : _company_colours[si->owner]));
01205 }
01206 }
01207
01214 void ViewportSign::UpdatePosition(int center, int top, StringID str)
01215 {
01216 if (this->width_normal != 0) this->MarkDirty();
01217
01218 this->top = top;
01219
01220 char buffer[DRAW_STRING_BUFFER];
01221
01222 GetString(buffer, str, lastof(buffer));
01223 this->width_normal = VPSM_LEFT + Align(GetStringBoundingBox(buffer).width, 2) + VPSM_RIGHT;
01224 this->center = center;
01225
01226
01227 this->width_small = VPSM_LEFT + Align(GetStringBoundingBox(buffer, FS_SMALL).width, 2) + VPSM_RIGHT;
01228
01229 this->MarkDirty();
01230 }
01231
01238 void ViewportSign::MarkDirty(ZoomLevel maxzoom) const
01239 {
01240 Rect zoomlevels[ZOOM_LVL_COUNT];
01241
01242 for (ZoomLevel zoom = ZOOM_LVL_BEGIN; zoom != ZOOM_LVL_END; zoom++) {
01243
01244 zoomlevels[zoom].left = this->center - ScaleByZoom(this->width_normal / 2 + 1, zoom);
01245 zoomlevels[zoom].top = this->top - ScaleByZoom(1, zoom);
01246 zoomlevels[zoom].right = this->center + ScaleByZoom(this->width_normal / 2 + 1, zoom);
01247 zoomlevels[zoom].bottom = this->top + ScaleByZoom(VPSM_TOP + FONT_HEIGHT_NORMAL + VPSM_BOTTOM + 1, zoom);
01248 }
01249
01250 Window *w;
01251 FOR_ALL_WINDOWS_FROM_BACK(w) {
01252 ViewPort *vp = w->viewport;
01253 if (vp != NULL && vp->zoom <= maxzoom) {
01254 assert(vp->width != 0);
01255 Rect &zl = zoomlevels[vp->zoom];
01256 MarkViewportDirty(vp, zl.left, zl.top, zl.right, zl.bottom);
01257 }
01258 }
01259 }
01260
01261 static void ViewportDrawTileSprites(const TileSpriteToDrawVector *tstdv)
01262 {
01263 const TileSpriteToDraw *tsend = tstdv->End();
01264 for (const TileSpriteToDraw *ts = tstdv->Begin(); ts != tsend; ++ts) {
01265 DrawSpriteViewport(ts->image, ts->pal, ts->x, ts->y, ts->sub);
01266 }
01267 }
01268
01270 static bool ViewportSortParentSpritesChecker()
01271 {
01272 return true;
01273 }
01274
01276 static void ViewportSortParentSprites(ParentSpriteToSortVector *psdv)
01277 {
01278 ParentSpriteToDraw **psdvend = psdv->End();
01279 ParentSpriteToDraw **psd = psdv->Begin();
01280 while (psd != psdvend) {
01281 ParentSpriteToDraw *ps = *psd;
01282
01283 if (ps->comparison_done) {
01284 psd++;
01285 continue;
01286 }
01287
01288 ps->comparison_done = true;
01289
01290 for (ParentSpriteToDraw **psd2 = psd + 1; psd2 != psdvend; psd2++) {
01291 ParentSpriteToDraw *ps2 = *psd2;
01292
01293 if (ps2->comparison_done) continue;
01294
01295
01296
01297
01298 if (ps->xmax >= ps2->xmin && ps->xmin <= ps2->xmax &&
01299 ps->ymax >= ps2->ymin && ps->ymin <= ps2->ymax &&
01300 ps->zmax >= ps2->zmin && ps->zmin <= ps2->zmax) {
01301
01302
01303
01304
01305
01306
01307 if (ps->xmin + ps->xmax + ps->ymin + ps->ymax + ps->zmin + ps->zmax <=
01308 ps2->xmin + ps2->xmax + ps2->ymin + ps2->ymax + ps2->zmin + ps2->zmax) {
01309 continue;
01310 }
01311 } else {
01312
01313
01314
01315
01316 if (ps->xmax < ps2->xmin ||
01317 ps->ymax < ps2->ymin ||
01318 ps->zmax < ps2->zmin) {
01319 continue;
01320 }
01321 }
01322
01323
01324 ParentSpriteToDraw *temp = ps2;
01325 for (ParentSpriteToDraw **psd3 = psd2; psd3 > psd; psd3--) {
01326 *psd3 = *(psd3 - 1);
01327 }
01328 *psd = temp;
01329 }
01330 }
01331 }
01332
01333 static void ViewportDrawParentSprites(const ParentSpriteToSortVector *psd, const ChildScreenSpriteToDrawVector *csstdv)
01334 {
01335 const ParentSpriteToDraw * const *psd_end = psd->End();
01336 for (const ParentSpriteToDraw * const *it = psd->Begin(); it != psd_end; it++) {
01337 const ParentSpriteToDraw *ps = *it;
01338 if (ps->image != SPR_EMPTY_BOUNDING_BOX) DrawSpriteViewport(ps->image, ps->pal, ps->x, ps->y, ps->sub);
01339
01340 int child_idx = ps->first_child;
01341 while (child_idx >= 0) {
01342 const ChildScreenSpriteToDraw *cs = csstdv->Get(child_idx);
01343 child_idx = cs->next;
01344 DrawSpriteViewport(cs->image, cs->pal, ps->left + cs->x, ps->top + cs->y, cs->sub);
01345 }
01346 }
01347 }
01348
01353 static void ViewportDrawBoundingBoxes(const ParentSpriteToSortVector *psd)
01354 {
01355 const ParentSpriteToDraw * const *psd_end = psd->End();
01356 for (const ParentSpriteToDraw * const *it = psd->Begin(); it != psd_end; it++) {
01357 const ParentSpriteToDraw *ps = *it;
01358 Point pt1 = RemapCoords(ps->xmax + 1, ps->ymax + 1, ps->zmax + 1);
01359 Point pt2 = RemapCoords(ps->xmin , ps->ymax + 1, ps->zmax + 1);
01360 Point pt3 = RemapCoords(ps->xmax + 1, ps->ymin , ps->zmax + 1);
01361 Point pt4 = RemapCoords(ps->xmax + 1, ps->ymax + 1, ps->zmin );
01362
01363 DrawBox( pt1.x, pt1.y,
01364 pt2.x - pt1.x, pt2.y - pt1.y,
01365 pt3.x - pt1.x, pt3.y - pt1.y,
01366 pt4.x - pt1.x, pt4.y - pt1.y);
01367 }
01368 }
01369
01373 static void ViewportDrawDirtyBlocks()
01374 {
01375 Blitter *blitter = BlitterFactory::GetCurrentBlitter();
01376 const DrawPixelInfo *dpi = _cur_dpi;
01377 void *dst;
01378 int right = UnScaleByZoom(dpi->width, dpi->zoom);
01379 int bottom = UnScaleByZoom(dpi->height, dpi->zoom);
01380
01381 int colour = _string_colourmap[_dirty_block_colour & 0xF];
01382
01383 dst = dpi->dst_ptr;
01384
01385 byte bo = UnScaleByZoom(dpi->left + dpi->top, dpi->zoom) & 1;
01386 do {
01387 for (int i = (bo ^= 1); i < right; i += 2) blitter->SetPixel(dst, i, 0, (uint8)colour);
01388 dst = blitter->MoveTo(dst, 0, 1);
01389 } while (--bottom > 0);
01390 }
01391
01392 static void ViewportDrawStrings(ZoomLevel zoom, const StringSpriteToDrawVector *sstdv)
01393 {
01394 const StringSpriteToDraw *ssend = sstdv->End();
01395 for (const StringSpriteToDraw *ss = sstdv->Begin(); ss != ssend; ++ss) {
01396 TextColour colour = TC_BLACK;
01397 bool small = HasBit(ss->width, 15);
01398 int w = GB(ss->width, 0, 15);
01399 int x = UnScaleByZoom(ss->x, zoom);
01400 int y = UnScaleByZoom(ss->y, zoom);
01401 int h = VPSM_TOP + (small ? FONT_HEIGHT_SMALL : FONT_HEIGHT_NORMAL) + VPSM_BOTTOM;
01402
01403 SetDParam(0, ss->params[0]);
01404 SetDParam(1, ss->params[1]);
01405
01406 if (ss->colour != INVALID_COLOUR) {
01407
01408 if (IsInvisibilitySet(TO_SIGNS) && ss->string != STR_WHITE_SIGN) continue;
01409
01410 if (IsTransparencySet(TO_SIGNS) && ss->string != STR_WHITE_SIGN) {
01411
01412
01413
01414 colour = (TextColour)_colour_gradient[ss->colour][6] | TC_IS_PALETTE_COLOUR;
01415 } else {
01416
01417
01418 DrawFrameRect(
01419 x, y, x + w, y + h, ss->colour,
01420 IsTransparencySet(TO_SIGNS) ? FR_TRANSPARENT : FR_NONE
01421 );
01422 }
01423 }
01424
01425 DrawString(x + VPSM_LEFT, x + w - 1 - VPSM_RIGHT, y + VPSM_TOP, ss->string, colour, SA_HOR_CENTER);
01426 }
01427 }
01428
01429 void ViewportDoDraw(const ViewPort *vp, int left, int top, int right, int bottom)
01430 {
01431 DrawPixelInfo *old_dpi = _cur_dpi;
01432 _cur_dpi = &_vd.dpi;
01433
01434 _vd.dpi.zoom = vp->zoom;
01435 int mask = ScaleByZoom(-1, vp->zoom);
01436
01437 _vd.combine_sprites = SPRITE_COMBINE_NONE;
01438
01439 _vd.dpi.width = (right - left) & mask;
01440 _vd.dpi.height = (bottom - top) & mask;
01441 _vd.dpi.left = left & mask;
01442 _vd.dpi.top = top & mask;
01443 _vd.dpi.pitch = old_dpi->pitch;
01444 _vd.last_child = NULL;
01445
01446 int x = UnScaleByZoom(_vd.dpi.left - (vp->virtual_left & mask), vp->zoom) + vp->left;
01447 int y = UnScaleByZoom(_vd.dpi.top - (vp->virtual_top & mask), vp->zoom) + vp->top;
01448
01449 _vd.dpi.dst_ptr = BlitterFactory::GetCurrentBlitter()->MoveTo(old_dpi->dst_ptr, x - old_dpi->left, y - old_dpi->top);
01450
01451 ViewportAddLandscape();
01452 ViewportAddVehicles(&_vd.dpi);
01453
01454 ViewportAddTownNames(&_vd.dpi);
01455 ViewportAddStationNames(&_vd.dpi);
01456 ViewportAddSigns(&_vd.dpi);
01457
01458 DrawTextEffects(&_vd.dpi);
01459
01460 if (_vd.tile_sprites_to_draw.Length() != 0) ViewportDrawTileSprites(&_vd.tile_sprites_to_draw);
01461
01462 ParentSpriteToDraw *psd_end = _vd.parent_sprites_to_draw.End();
01463 for (ParentSpriteToDraw *it = _vd.parent_sprites_to_draw.Begin(); it != psd_end; it++) {
01464 *_vd.parent_sprites_to_sort.Append() = it;
01465 }
01466
01467 _vp_sprite_sorter(&_vd.parent_sprites_to_sort);
01468 ViewportDrawParentSprites(&_vd.parent_sprites_to_sort, &_vd.child_screen_sprites_to_draw);
01469
01470 if (_draw_bounding_boxes) ViewportDrawBoundingBoxes(&_vd.parent_sprites_to_sort);
01471 if (_draw_dirty_blocks) ViewportDrawDirtyBlocks();
01472
01473 DrawPixelInfo dp = _vd.dpi;
01474 ZoomLevel zoom = _vd.dpi.zoom;
01475 dp.zoom = ZOOM_LVL_NORMAL;
01476 dp.width = UnScaleByZoom(dp.width, zoom);
01477 dp.height = UnScaleByZoom(dp.height, zoom);
01478 _cur_dpi = &dp;
01479
01480
01481 dp.left = x;
01482 dp.top = y;
01483
01484 if (vp->overlay != NULL) vp->overlay->Draw(&dp);
01485
01486
01487 dp.left = UnScaleByZoom(_vd.dpi.left, zoom);
01488 dp.top = UnScaleByZoom(_vd.dpi.top, zoom);
01489
01490 if (_vd.string_sprites_to_draw.Length() != 0) ViewportDrawStrings(zoom, &_vd.string_sprites_to_draw);
01491
01492 _cur_dpi = old_dpi;
01493
01494 _vd.string_sprites_to_draw.Clear();
01495 _vd.tile_sprites_to_draw.Clear();
01496 _vd.parent_sprites_to_draw.Clear();
01497 _vd.parent_sprites_to_sort.Clear();
01498 _vd.child_screen_sprites_to_draw.Clear();
01499 }
01500
01505 static void ViewportDrawChk(const ViewPort *vp, int left, int top, int right, int bottom)
01506 {
01507 if (ScaleByZoom(bottom - top, vp->zoom) * ScaleByZoom(right - left, vp->zoom) > 180000 * ZOOM_LVL_BASE * ZOOM_LVL_BASE) {
01508 if ((bottom - top) > (right - left)) {
01509 int t = (top + bottom) >> 1;
01510 ViewportDrawChk(vp, left, top, right, t);
01511 ViewportDrawChk(vp, left, t, right, bottom);
01512 } else {
01513 int t = (left + right) >> 1;
01514 ViewportDrawChk(vp, left, top, t, bottom);
01515 ViewportDrawChk(vp, t, top, right, bottom);
01516 }
01517 } else {
01518 ViewportDoDraw(vp,
01519 ScaleByZoom(left - vp->left, vp->zoom) + vp->virtual_left,
01520 ScaleByZoom(top - vp->top, vp->zoom) + vp->virtual_top,
01521 ScaleByZoom(right - vp->left, vp->zoom) + vp->virtual_left,
01522 ScaleByZoom(bottom - vp->top, vp->zoom) + vp->virtual_top
01523 );
01524 }
01525 }
01526
01527 static inline void ViewportDraw(const ViewPort *vp, int left, int top, int right, int bottom)
01528 {
01529 if (right <= vp->left || bottom <= vp->top) return;
01530
01531 if (left >= vp->left + vp->width) return;
01532
01533 if (left < vp->left) left = vp->left;
01534 if (right > vp->left + vp->width) right = vp->left + vp->width;
01535
01536 if (top >= vp->top + vp->height) return;
01537
01538 if (top < vp->top) top = vp->top;
01539 if (bottom > vp->top + vp->height) bottom = vp->top + vp->height;
01540
01541 ViewportDrawChk(vp, left, top, right, bottom);
01542 }
01543
01547 void Window::DrawViewport() const
01548 {
01549 DrawPixelInfo *dpi = _cur_dpi;
01550
01551 dpi->left += this->left;
01552 dpi->top += this->top;
01553
01554 ViewportDraw(this->viewport, dpi->left, dpi->top, dpi->left + dpi->width, dpi->top + dpi->height);
01555
01556 dpi->left -= this->left;
01557 dpi->top -= this->top;
01558 }
01559
01560 static inline void ClampViewportToMap(const ViewPort *vp, int &x, int &y)
01561 {
01562
01563 x += vp->virtual_width / 2;
01564 y += vp->virtual_height / 2;
01565
01566
01567
01568 int vx = -x + y * 2;
01569 int vy = x + y * 2;
01570
01571
01572 vx = Clamp(vx, 0, MapMaxX() * TILE_SIZE * 4 * ZOOM_LVL_BASE);
01573 vy = Clamp(vy, 0, MapMaxY() * TILE_SIZE * 4 * ZOOM_LVL_BASE);
01574
01575
01576 x = (-vx + vy) / 2;
01577 y = ( vx + vy) / 4;
01578
01579
01580 x -= vp->virtual_width / 2;
01581 y -= vp->virtual_height / 2;
01582 }
01583
01588 void UpdateViewportPosition(Window *w)
01589 {
01590 const ViewPort *vp = w->viewport;
01591
01592 if (w->viewport->follow_vehicle != INVALID_VEHICLE) {
01593 const Vehicle *veh = Vehicle::Get(w->viewport->follow_vehicle);
01594 Point pt = MapXYZToViewport(vp, veh->x_pos, veh->y_pos, veh->z_pos);
01595
01596 w->viewport->scrollpos_x = pt.x;
01597 w->viewport->scrollpos_y = pt.y;
01598 SetViewportPosition(w, pt.x, pt.y);
01599 } else {
01600
01601 ClampViewportToMap(vp, w->viewport->dest_scrollpos_x, w->viewport->dest_scrollpos_y);
01602
01603 int delta_x = w->viewport->dest_scrollpos_x - w->viewport->scrollpos_x;
01604 int delta_y = w->viewport->dest_scrollpos_y - w->viewport->scrollpos_y;
01605
01606 bool update_overlay = false;
01607 if (delta_x != 0 || delta_y != 0) {
01608 if (_settings_client.gui.smooth_scroll) {
01609 int max_scroll = ScaleByMapSize1D(512 * ZOOM_LVL_BASE);
01610
01611 w->viewport->scrollpos_x += Clamp(delta_x / 4, -max_scroll, max_scroll);
01612 w->viewport->scrollpos_y += Clamp(delta_y / 4, -max_scroll, max_scroll);
01613 } else {
01614 w->viewport->scrollpos_x = w->viewport->dest_scrollpos_x;
01615 w->viewport->scrollpos_y = w->viewport->dest_scrollpos_y;
01616 }
01617 update_overlay = (w->viewport->scrollpos_x == w->viewport->dest_scrollpos_x &&
01618 w->viewport->scrollpos_y == w->viewport->dest_scrollpos_y);
01619 }
01620
01621 ClampViewportToMap(vp, w->viewport->scrollpos_x, w->viewport->scrollpos_y);
01622
01623 SetViewportPosition(w, w->viewport->scrollpos_x, w->viewport->scrollpos_y);
01624 if (update_overlay) RebuildViewportOverlay(w);
01625 }
01626 }
01627
01637 static void MarkViewportDirty(const ViewPort *vp, int left, int top, int right, int bottom)
01638 {
01639 right -= vp->virtual_left;
01640 if (right <= 0) return;
01641
01642 bottom -= vp->virtual_top;
01643 if (bottom <= 0) return;
01644
01645 left = max(0, left - vp->virtual_left);
01646
01647 if (left >= vp->virtual_width) return;
01648
01649 top = max(0, top - vp->virtual_top);
01650
01651 if (top >= vp->virtual_height) return;
01652
01653 SetDirtyBlocks(
01654 UnScaleByZoomLower(left, vp->zoom) + vp->left,
01655 UnScaleByZoomLower(top, vp->zoom) + vp->top,
01656 UnScaleByZoom(right, vp->zoom) + vp->left + 1,
01657 UnScaleByZoom(bottom, vp->zoom) + vp->top + 1
01658 );
01659 }
01660
01669 void MarkAllViewportsDirty(int left, int top, int right, int bottom)
01670 {
01671 Window *w;
01672 FOR_ALL_WINDOWS_FROM_BACK(w) {
01673 ViewPort *vp = w->viewport;
01674 if (vp != NULL) {
01675 assert(vp->width != 0);
01676 MarkViewportDirty(vp, left, top, right, bottom);
01677 }
01678 }
01679 }
01680
01681 void ConstrainAllViewportsZoom()
01682 {
01683 Window *w;
01684 FOR_ALL_WINDOWS_FROM_FRONT(w) {
01685 if (w->viewport == NULL) continue;
01686
01687 ZoomLevel zoom = static_cast<ZoomLevel>(Clamp(w->viewport->zoom, _settings_client.gui.zoom_min, _settings_client.gui.zoom_max));
01688 if (zoom != w->viewport->zoom) {
01689 while (w->viewport->zoom < zoom) DoZoomInOutWindow(ZOOM_OUT, w);
01690 while (w->viewport->zoom > zoom) DoZoomInOutWindow(ZOOM_IN, w);
01691 }
01692 }
01693 }
01694
01700 void MarkTileDirtyByTile(TileIndex tile)
01701 {
01702 Point pt = RemapCoords(TileX(tile) * TILE_SIZE, TileY(tile) * TILE_SIZE, GetTilePixelZ(tile));
01703 MarkAllViewportsDirty(
01704 pt.x - 31 * ZOOM_LVL_BASE,
01705 pt.y - 122 * ZOOM_LVL_BASE,
01706 pt.x - 31 * ZOOM_LVL_BASE + 67 * ZOOM_LVL_BASE,
01707 pt.y - 122 * ZOOM_LVL_BASE + 154 * ZOOM_LVL_BASE
01708 );
01709 }
01710
01718 static void SetSelectionTilesDirty()
01719 {
01720 int x_size = _thd.size.x;
01721 int y_size = _thd.size.y;
01722
01723 if (!_thd.diagonal) {
01724 int x_start = _thd.pos.x;
01725 int y_start = _thd.pos.y;
01726
01727 if (_thd.outersize.x != 0) {
01728 x_size += _thd.outersize.x;
01729 x_start += _thd.offs.x;
01730 y_size += _thd.outersize.y;
01731 y_start += _thd.offs.y;
01732 }
01733
01734 x_size -= TILE_SIZE;
01735 y_size -= TILE_SIZE;
01736
01737 assert(x_size >= 0);
01738 assert(y_size >= 0);
01739
01740 int x_end = Clamp(x_start + x_size, 0, MapSizeX() * TILE_SIZE - TILE_SIZE);
01741 int y_end = Clamp(y_start + y_size, 0, MapSizeY() * TILE_SIZE - TILE_SIZE);
01742
01743 x_start = Clamp(x_start, 0, MapSizeX() * TILE_SIZE - TILE_SIZE);
01744 y_start = Clamp(y_start, 0, MapSizeY() * TILE_SIZE - TILE_SIZE);
01745
01746
01747 assert((x_end | y_end | x_start | y_start) % TILE_SIZE == 0);
01748
01749
01750
01751
01752
01753
01754
01755
01756
01757
01758
01759
01760
01761
01762
01763
01764
01765
01766
01767 int top_x = x_end;
01768 int top_y = y_start;
01769 int bot_x = top_x;
01770 int bot_y = top_y;
01771
01772 do {
01773
01774 TileIndex top_tile = TileVirtXY(top_x, top_y);
01775 Point top = RemapCoords(top_x, top_y, GetTileMaxPixelZ(top_tile));
01776
01777
01778 TileIndex bottom_tile = TileVirtXY(bot_x, bot_y);
01779 Point bot = RemapCoords(bot_x + TILE_SIZE, bot_y + TILE_SIZE, GetTilePixelZ(bottom_tile));
01780
01781
01782
01783
01784 int l = top.x - (TILE_PIXELS - 2) * ZOOM_LVL_BASE;
01785 int t = top.y;
01786 int r = top.x + (TILE_PIXELS - 2) * ZOOM_LVL_BASE;
01787 int b = bot.y;
01788
01789 static const int OVERLAY_WIDTH = 4 * ZOOM_LVL_BASE;
01790
01791
01792 MarkAllViewportsDirty(l - OVERLAY_WIDTH, t - OVERLAY_WIDTH - TILE_HEIGHT * ZOOM_LVL_BASE, r + OVERLAY_WIDTH, b + OVERLAY_WIDTH * ZOOM_LVL_BASE);
01793
01794
01795 if (top_x != x_start) {
01796 top_x -= TILE_SIZE;
01797 } else {
01798 top_y += TILE_SIZE;
01799 }
01800
01801
01802 if (bot_y != y_end) {
01803 bot_y += TILE_SIZE;
01804 } else {
01805 bot_x -= TILE_SIZE;
01806 }
01807 } while (bot_x >= top_x);
01808 } else {
01809
01810 int a_size = x_size + y_size, b_size = x_size - y_size;
01811
01812 int interval_a = a_size < 0 ? -(int)TILE_SIZE : (int)TILE_SIZE;
01813 int interval_b = b_size < 0 ? -(int)TILE_SIZE : (int)TILE_SIZE;
01814
01815 for (int a = -interval_a; a != a_size + interval_a; a += interval_a) {
01816 for (int b = -interval_b; b != b_size + interval_b; b += interval_b) {
01817 uint x = (_thd.pos.x + (a + b) / 2) / TILE_SIZE;
01818 uint y = (_thd.pos.y + (a - b) / 2) / TILE_SIZE;
01819
01820 if (x < MapMaxX() && y < MapMaxY()) {
01821 MarkTileDirtyByTile(TileXY(x, y));
01822 }
01823 }
01824 }
01825 }
01826 }
01827
01828
01829 void SetSelectionRed(bool b)
01830 {
01831 _thd.make_square_red = b;
01832 SetSelectionTilesDirty();
01833 }
01834
01843 static bool CheckClickOnViewportSign(const ViewPort *vp, int x, int y, const ViewportSign *sign)
01844 {
01845 bool small = (vp->zoom >= ZOOM_LVL_OUT_16X);
01846 int sign_half_width = ScaleByZoom((small ? sign->width_small : sign->width_normal) / 2, vp->zoom);
01847 int sign_height = ScaleByZoom(VPSM_TOP + (small ? FONT_HEIGHT_SMALL : FONT_HEIGHT_NORMAL) + VPSM_BOTTOM, vp->zoom);
01848
01849 x = ScaleByZoom(x - vp->left, vp->zoom) + vp->virtual_left;
01850 y = ScaleByZoom(y - vp->top, vp->zoom) + vp->virtual_top;
01851
01852 return y >= sign->top && y < sign->top + sign_height &&
01853 x >= sign->center - sign_half_width && x < sign->center + sign_half_width;
01854 }
01855
01856 static bool CheckClickOnTown(const ViewPort *vp, int x, int y)
01857 {
01858 if (!HasBit(_display_opt, DO_SHOW_TOWN_NAMES)) return false;
01859
01860 const Town *t;
01861 FOR_ALL_TOWNS(t) {
01862 if (CheckClickOnViewportSign(vp, x, y, &t->cache.sign)) {
01863 ShowTownViewWindow(t->index);
01864 return true;
01865 }
01866 }
01867
01868 return false;
01869 }
01870
01871 static bool CheckClickOnStation(const ViewPort *vp, int x, int y)
01872 {
01873 if (!(HasBit(_display_opt, DO_SHOW_STATION_NAMES) || HasBit(_display_opt, DO_SHOW_WAYPOINT_NAMES)) || IsInvisibilitySet(TO_SIGNS)) return false;
01874
01875 const BaseStation *st;
01876 FOR_ALL_BASE_STATIONS(st) {
01877
01878 bool is_station = Station::IsExpected(st);
01879
01880
01881 if (!HasBit(_display_opt, is_station ? DO_SHOW_STATION_NAMES : DO_SHOW_WAYPOINT_NAMES)) continue;
01882
01883
01884 if (!HasBit(_display_opt, DO_SHOW_COMPETITOR_SIGNS) && _local_company != st->owner && st->owner != OWNER_NONE) continue;
01885
01886 if (CheckClickOnViewportSign(vp, x, y, &st->sign)) {
01887 if (is_station) {
01888 ShowStationViewWindow(st->index);
01889 } else {
01890 ShowWaypointWindow(Waypoint::From(st));
01891 }
01892 return true;
01893 }
01894 }
01895
01896 return false;
01897 }
01898
01899
01900 static bool CheckClickOnSign(const ViewPort *vp, int x, int y)
01901 {
01902
01903 if (!HasBit(_display_opt, DO_SHOW_SIGNS) || IsInvisibilitySet(TO_SIGNS) || _local_company == COMPANY_SPECTATOR) return false;
01904
01905 const Sign *si;
01906 FOR_ALL_SIGNS(si) {
01907
01908 if (!HasBit(_display_opt, DO_SHOW_COMPETITOR_SIGNS) && _local_company != si->owner && si->owner != OWNER_DEITY) continue;
01909 if (si->owner == OWNER_DEITY && _game_mode != GM_EDITOR) continue;
01910
01911 if (CheckClickOnViewportSign(vp, x, y, &si->sign)) {
01912 HandleClickOnSign(si);
01913 return true;
01914 }
01915 }
01916
01917 return false;
01918 }
01919
01920
01921 static bool CheckClickOnLandscape(const ViewPort *vp, int x, int y)
01922 {
01923 Point pt = TranslateXYToTileCoord(vp, x, y);
01924
01925 if (pt.x != -1) return ClickTile(TileVirtXY(pt.x, pt.y));
01926 return true;
01927 }
01928
01929 static void PlaceObject()
01930 {
01931 Point pt;
01932 Window *w;
01933
01934 pt = GetTileBelowCursor();
01935 if (pt.x == -1) return;
01936
01937 if ((_thd.place_mode & HT_DRAG_MASK) == HT_POINT) {
01938 pt.x += TILE_SIZE / 2;
01939 pt.y += TILE_SIZE / 2;
01940 }
01941
01942 _tile_fract_coords.x = pt.x & TILE_UNIT_MASK;
01943 _tile_fract_coords.y = pt.y & TILE_UNIT_MASK;
01944
01945 w = _thd.GetCallbackWnd();
01946 if (w != NULL) w->OnPlaceObject(pt, TileVirtXY(pt.x, pt.y));
01947 }
01948
01949
01950 bool HandleViewportClicked(const ViewPort *vp, int x, int y)
01951 {
01952 const Vehicle *v = CheckClickOnVehicle(vp, x, y);
01953
01954 if (_thd.place_mode & HT_VEHICLE) {
01955 if (v != NULL && VehicleClicked(v)) return true;
01956 }
01957
01958
01959 if ((_thd.place_mode & HT_DRAG_MASK) != HT_NONE) {
01960 PlaceObject();
01961 return true;
01962 }
01963
01964 if (CheckClickOnTown(vp, x, y)) return true;
01965 if (CheckClickOnStation(vp, x, y)) return true;
01966 if (CheckClickOnSign(vp, x, y)) return true;
01967 bool result = CheckClickOnLandscape(vp, x, y);
01968
01969 if (v != NULL) {
01970 DEBUG(misc, 2, "Vehicle %d (index %d) at %p", v->unitnumber, v->index, v);
01971 if (IsCompanyBuildableVehicleType(v)) {
01972 v = v->First();
01973 if (_ctrl_pressed && v->owner == _local_company) {
01974 StartStopVehicle(v, true);
01975 } else {
01976 ShowVehicleViewWindow(v);
01977 }
01978 }
01979 return true;
01980 }
01981 return result;
01982 }
01983
01984 void RebuildViewportOverlay(Window *w)
01985 {
01986 if (w->viewport->overlay != NULL &&
01987 w->viewport->overlay->GetCompanyMask() != 0 &&
01988 w->viewport->overlay->GetCargoMask() != 0) {
01989 w->viewport->overlay->RebuildCache();
01990 w->SetDirty();
01991 }
01992 }
01993
02003 bool ScrollWindowTo(int x, int y, int z, Window *w, bool instant)
02004 {
02005
02006 if (z == -1) z = GetSlopePixelZ(Clamp(x, 0, MapSizeX() * TILE_SIZE - 1), Clamp(y, 0, MapSizeY() * TILE_SIZE - 1));
02007
02008 Point pt = MapXYZToViewport(w->viewport, x, y, z);
02009 w->viewport->follow_vehicle = INVALID_VEHICLE;
02010
02011 if (w->viewport->dest_scrollpos_x == pt.x && w->viewport->dest_scrollpos_y == pt.y) return false;
02012
02013 if (instant) {
02014 w->viewport->scrollpos_x = pt.x;
02015 w->viewport->scrollpos_y = pt.y;
02016 RebuildViewportOverlay(w);
02017 }
02018
02019 w->viewport->dest_scrollpos_x = pt.x;
02020 w->viewport->dest_scrollpos_y = pt.y;
02021 return true;
02022 }
02023
02031 bool ScrollWindowToTile(TileIndex tile, Window *w, bool instant)
02032 {
02033 return ScrollWindowTo(TileX(tile) * TILE_SIZE, TileY(tile) * TILE_SIZE, -1, w, instant);
02034 }
02035
02042 bool ScrollMainWindowToTile(TileIndex tile, bool instant)
02043 {
02044 return ScrollMainWindowTo(TileX(tile) * TILE_SIZE + TILE_SIZE / 2, TileY(tile) * TILE_SIZE + TILE_SIZE / 2, -1, instant);
02045 }
02046
02051 void SetRedErrorSquare(TileIndex tile)
02052 {
02053 TileIndex old;
02054
02055 old = _thd.redsq;
02056 _thd.redsq = tile;
02057
02058 if (tile != old) {
02059 if (tile != INVALID_TILE) MarkTileDirtyByTile(tile);
02060 if (old != INVALID_TILE) MarkTileDirtyByTile(old);
02061 }
02062 }
02063
02069 void SetTileSelectSize(int w, int h)
02070 {
02071 _thd.new_size.x = w * TILE_SIZE;
02072 _thd.new_size.y = h * TILE_SIZE;
02073 _thd.new_outersize.x = 0;
02074 _thd.new_outersize.y = 0;
02075 }
02076
02077 void SetTileSelectBigSize(int ox, int oy, int sx, int sy)
02078 {
02079 _thd.offs.x = ox * TILE_SIZE;
02080 _thd.offs.y = oy * TILE_SIZE;
02081 _thd.new_outersize.x = sx * TILE_SIZE;
02082 _thd.new_outersize.y = sy * TILE_SIZE;
02083 }
02084
02086 static HighLightStyle GetAutorailHT(int x, int y)
02087 {
02088 return HT_RAIL | _autorail_piece[x & TILE_UNIT_MASK][y & TILE_UNIT_MASK];
02089 }
02090
02094 void TileHighlightData::Reset()
02095 {
02096 this->pos.x = 0;
02097 this->pos.y = 0;
02098 this->new_pos.x = 0;
02099 this->new_pos.y = 0;
02100 }
02101
02106 bool TileHighlightData::IsDraggingDiagonal()
02107 {
02108 return (this->place_mode & HT_DIAGONAL) != 0 && _ctrl_pressed && _left_button_down;
02109 }
02110
02115 Window *TileHighlightData::GetCallbackWnd()
02116 {
02117 return FindWindowById(this->window_class, this->window_number);
02118 }
02119
02120
02121
02129 void UpdateTileSelection()
02130 {
02131 int x1;
02132 int y1;
02133
02134 HighLightStyle new_drawstyle = HT_NONE;
02135 bool new_diagonal = false;
02136
02137 if ((_thd.place_mode & HT_DRAG_MASK) == HT_SPECIAL) {
02138 x1 = _thd.selend.x;
02139 y1 = _thd.selend.y;
02140 if (x1 != -1) {
02141 int x2 = _thd.selstart.x & ~TILE_UNIT_MASK;
02142 int y2 = _thd.selstart.y & ~TILE_UNIT_MASK;
02143 x1 &= ~TILE_UNIT_MASK;
02144 y1 &= ~TILE_UNIT_MASK;
02145
02146 if (_thd.IsDraggingDiagonal()) {
02147 new_diagonal = true;
02148 } else {
02149 if (x1 >= x2) Swap(x1, x2);
02150 if (y1 >= y2) Swap(y1, y2);
02151 }
02152 _thd.new_pos.x = x1;
02153 _thd.new_pos.y = y1;
02154 _thd.new_size.x = x2 - x1;
02155 _thd.new_size.y = y2 - y1;
02156 if (!new_diagonal) {
02157 _thd.new_size.x += TILE_SIZE;
02158 _thd.new_size.y += TILE_SIZE;
02159 }
02160 new_drawstyle = _thd.next_drawstyle;
02161 }
02162 } else if ((_thd.place_mode & HT_DRAG_MASK) != HT_NONE) {
02163 Point pt = GetTileBelowCursor();
02164 x1 = pt.x;
02165 y1 = pt.y;
02166 if (x1 != -1) {
02167 switch (_thd.place_mode & HT_DRAG_MASK) {
02168 case HT_RECT:
02169 new_drawstyle = HT_RECT;
02170 break;
02171 case HT_POINT:
02172 new_drawstyle = HT_POINT;
02173 x1 += TILE_SIZE / 2;
02174 y1 += TILE_SIZE / 2;
02175 break;
02176 case HT_RAIL:
02177
02178 new_drawstyle = GetAutorailHT(pt.x, pt.y);
02179 break;
02180 case HT_LINE:
02181 switch (_thd.place_mode & HT_DIR_MASK) {
02182 case HT_DIR_X: new_drawstyle = HT_LINE | HT_DIR_X; break;
02183 case HT_DIR_Y: new_drawstyle = HT_LINE | HT_DIR_Y; break;
02184
02185 case HT_DIR_HU:
02186 case HT_DIR_HL:
02187 new_drawstyle = (pt.x & TILE_UNIT_MASK) + (pt.y & TILE_UNIT_MASK) <= TILE_SIZE ? HT_LINE | HT_DIR_HU : HT_LINE | HT_DIR_HL;
02188 break;
02189
02190 case HT_DIR_VL:
02191 case HT_DIR_VR:
02192 new_drawstyle = (pt.x & TILE_UNIT_MASK) > (pt.y & TILE_UNIT_MASK) ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR;
02193 break;
02194
02195 default: NOT_REACHED();
02196 }
02197 _thd.selstart.x = x1 & ~TILE_UNIT_MASK;
02198 _thd.selstart.y = y1 & ~TILE_UNIT_MASK;
02199 break;
02200 default:
02201 NOT_REACHED();
02202 break;
02203 }
02204 _thd.new_pos.x = x1 & ~TILE_UNIT_MASK;
02205 _thd.new_pos.y = y1 & ~TILE_UNIT_MASK;
02206 }
02207 }
02208
02209
02210 if (_thd.drawstyle != new_drawstyle ||
02211 _thd.pos.x != _thd.new_pos.x || _thd.pos.y != _thd.new_pos.y ||
02212 _thd.size.x != _thd.new_size.x || _thd.size.y != _thd.new_size.y ||
02213 _thd.outersize.x != _thd.new_outersize.x ||
02214 _thd.outersize.y != _thd.new_outersize.y ||
02215 _thd.diagonal != new_diagonal) {
02216
02217 if ((_thd.drawstyle & HT_DRAG_MASK) != HT_NONE) SetSelectionTilesDirty();
02218
02219 _thd.drawstyle = new_drawstyle;
02220 _thd.pos = _thd.new_pos;
02221 _thd.size = _thd.new_size;
02222 _thd.outersize = _thd.new_outersize;
02223 _thd.diagonal = new_diagonal;
02224 _thd.dirty = 0xff;
02225
02226
02227 if ((new_drawstyle & HT_DRAG_MASK) != HT_NONE) SetSelectionTilesDirty();
02228 }
02229 }
02230
02238 static inline void ShowMeasurementTooltips(StringID str, uint paramcount, const uint64 params[], TooltipCloseCondition close_cond = TCC_LEFT_CLICK)
02239 {
02240 if (!_settings_client.gui.measure_tooltip) return;
02241 GuiShowTooltips(_thd.GetCallbackWnd(), str, paramcount, params, close_cond);
02242 }
02243
02245 void VpStartPlaceSizing(TileIndex tile, ViewportPlaceMethod method, ViewportDragDropSelectionProcess process)
02246 {
02247 _thd.select_method = method;
02248 _thd.select_proc = process;
02249 _thd.selend.x = TileX(tile) * TILE_SIZE;
02250 _thd.selstart.x = TileX(tile) * TILE_SIZE;
02251 _thd.selend.y = TileY(tile) * TILE_SIZE;
02252 _thd.selstart.y = TileY(tile) * TILE_SIZE;
02253
02254
02255
02256
02257 if (method == VPM_X_OR_Y || method == VPM_FIX_X || method == VPM_FIX_Y) {
02258 _thd.selend.x += TILE_SIZE / 2;
02259 _thd.selend.y += TILE_SIZE / 2;
02260 _thd.selstart.x += TILE_SIZE / 2;
02261 _thd.selstart.y += TILE_SIZE / 2;
02262 }
02263
02264 HighLightStyle others = _thd.place_mode & ~(HT_DRAG_MASK | HT_DIR_MASK);
02265 if ((_thd.place_mode & HT_DRAG_MASK) == HT_RECT) {
02266 _thd.place_mode = HT_SPECIAL | others;
02267 _thd.next_drawstyle = HT_RECT | others;
02268 } else if (_thd.place_mode & (HT_RAIL | HT_LINE)) {
02269 _thd.place_mode = HT_SPECIAL | others;
02270 _thd.next_drawstyle = _thd.drawstyle | others;
02271 } else {
02272 _thd.place_mode = HT_SPECIAL | others;
02273 _thd.next_drawstyle = HT_POINT | others;
02274 }
02275 _special_mouse_mode = WSM_SIZING;
02276 }
02277
02278 void VpSetPlaceSizingLimit(int limit)
02279 {
02280 _thd.sizelimit = limit;
02281 }
02282
02288 void VpSetPresizeRange(TileIndex from, TileIndex to)
02289 {
02290 uint64 distance = DistanceManhattan(from, to) + 1;
02291
02292 _thd.selend.x = TileX(to) * TILE_SIZE;
02293 _thd.selend.y = TileY(to) * TILE_SIZE;
02294 _thd.selstart.x = TileX(from) * TILE_SIZE;
02295 _thd.selstart.y = TileY(from) * TILE_SIZE;
02296 _thd.next_drawstyle = HT_RECT;
02297
02298
02299 if (distance > 1) ShowMeasurementTooltips(STR_MEASURE_LENGTH, 1, &distance, TCC_HOVER);
02300 }
02301
02302 static void VpStartPreSizing()
02303 {
02304 _thd.selend.x = -1;
02305 _special_mouse_mode = WSM_PRESIZE;
02306 }
02307
02312 static HighLightStyle Check2x1AutoRail(int mode)
02313 {
02314 int fxpy = _tile_fract_coords.x + _tile_fract_coords.y;
02315 int sxpy = (_thd.selend.x & TILE_UNIT_MASK) + (_thd.selend.y & TILE_UNIT_MASK);
02316 int fxmy = _tile_fract_coords.x - _tile_fract_coords.y;
02317 int sxmy = (_thd.selend.x & TILE_UNIT_MASK) - (_thd.selend.y & TILE_UNIT_MASK);
02318
02319 switch (mode) {
02320 default: NOT_REACHED();
02321 case 0:
02322 if (fxpy >= 20 && sxpy <= 12) return HT_DIR_HL;
02323 if (fxmy < -3 && sxmy > 3) return HT_DIR_VR;
02324 return HT_DIR_Y;
02325
02326 case 1:
02327 if (fxmy > 3 && sxmy < -3) return HT_DIR_VL;
02328 if (fxpy <= 12 && sxpy >= 20) return HT_DIR_HU;
02329 return HT_DIR_Y;
02330
02331 case 2:
02332 if (fxmy > 3 && sxmy < -3) return HT_DIR_VL;
02333 if (fxpy >= 20 && sxpy <= 12) return HT_DIR_HL;
02334 return HT_DIR_X;
02335
02336 case 3:
02337 if (fxmy < -3 && sxmy > 3) return HT_DIR_VR;
02338 if (fxpy <= 12 && sxpy >= 20) return HT_DIR_HU;
02339 return HT_DIR_X;
02340 }
02341 }
02342
02356 static bool SwapDirection(HighLightStyle style, TileIndex start_tile, TileIndex end_tile)
02357 {
02358 uint start_x = TileX(start_tile);
02359 uint start_y = TileY(start_tile);
02360 uint end_x = TileX(end_tile);
02361 uint end_y = TileY(end_tile);
02362
02363 switch (style & HT_DRAG_MASK) {
02364 case HT_RAIL:
02365 case HT_LINE: return (end_x > start_x || (end_x == start_x && end_y > start_y));
02366
02367 case HT_RECT:
02368 case HT_POINT: return (end_x != start_x && end_y < start_y);
02369 default: NOT_REACHED();
02370 }
02371
02372 return false;
02373 }
02374
02390 static int CalcHeightdiff(HighLightStyle style, uint distance, TileIndex start_tile, TileIndex end_tile)
02391 {
02392 bool swap = SwapDirection(style, start_tile, end_tile);
02393 uint h0, h1;
02394
02395 if (start_tile == end_tile) return 0;
02396 if (swap) Swap(start_tile, end_tile);
02397
02398 switch (style & HT_DRAG_MASK) {
02399 case HT_RECT: {
02400 static const TileIndexDiffC heightdiff_area_by_dir[] = {
02401 {1, 0}, {0, 0},
02402 {0, 1}, {1, 1}
02403 };
02404
02405
02406
02407 byte style_t = (byte)(TileX(end_tile) > TileX(start_tile));
02408 start_tile = TILE_ADD(start_tile, ToTileIndexDiff(heightdiff_area_by_dir[style_t]));
02409 end_tile = TILE_ADD(end_tile, ToTileIndexDiff(heightdiff_area_by_dir[2 + style_t]));
02410
02411 }
02412
02413 case HT_POINT:
02414 h0 = TileHeight(start_tile);
02415 h1 = TileHeight(end_tile);
02416 break;
02417 default: {
02418 static const HighLightStyle flip_style_direction[] = {
02419 HT_DIR_X, HT_DIR_Y, HT_DIR_HL, HT_DIR_HU, HT_DIR_VR, HT_DIR_VL
02420 };
02421 static const TileIndexDiffC heightdiff_line_by_dir[] = {
02422 {1, 0}, {1, 1}, {0, 1}, {1, 1},
02423 {1, 0}, {0, 0}, {1, 0}, {1, 1},
02424 {1, 0}, {1, 1}, {0, 1}, {1, 1},
02425
02426 {0, 1}, {0, 0}, {1, 0}, {0, 0},
02427 {0, 1}, {0, 0}, {1, 1}, {0, 1},
02428 {1, 0}, {0, 0}, {0, 0}, {0, 1},
02429 };
02430
02431 distance %= 2;
02432 style &= HT_DIR_MASK;
02433
02434
02435
02436
02437
02438 if (swap && distance == 0) style = flip_style_direction[style];
02439
02440
02441 byte style_t = style * 2;
02442 assert(style_t < lengthof(heightdiff_line_by_dir) - 13);
02443 h0 = TileHeight(TILE_ADD(start_tile, ToTileIndexDiff(heightdiff_line_by_dir[style_t])));
02444 uint ht = TileHeight(TILE_ADD(start_tile, ToTileIndexDiff(heightdiff_line_by_dir[style_t + 1])));
02445 h0 = max(h0, ht);
02446
02447
02448
02449 if (distance == 0) style_t = flip_style_direction[style] * 2;
02450 assert(style_t < lengthof(heightdiff_line_by_dir) - 13);
02451 h1 = TileHeight(TILE_ADD(end_tile, ToTileIndexDiff(heightdiff_line_by_dir[12 + style_t])));
02452 ht = TileHeight(TILE_ADD(end_tile, ToTileIndexDiff(heightdiff_line_by_dir[12 + style_t + 1])));
02453 h1 = max(h1, ht);
02454 break;
02455 }
02456 }
02457
02458 if (swap) Swap(h0, h1);
02459 return (int)(h1 - h0) * TILE_HEIGHT_STEP;
02460 }
02461
02462 static const StringID measure_strings_length[] = {STR_NULL, STR_MEASURE_LENGTH, STR_MEASURE_LENGTH_HEIGHTDIFF};
02463
02470 static void CheckUnderflow(int &test, int &other, int mult)
02471 {
02472 if (test >= 0) return;
02473
02474 other += mult * test;
02475 test = 0;
02476 }
02477
02485 static void CheckOverflow(int &test, int &other, int max, int mult)
02486 {
02487 if (test <= max) return;
02488
02489 other += mult * (test - max);
02490 test = max;
02491 }
02492
02494 static void CalcRaildirsDrawstyle(int x, int y, int method)
02495 {
02496 HighLightStyle b;
02497
02498 int dx = _thd.selstart.x - (_thd.selend.x & ~TILE_UNIT_MASK);
02499 int dy = _thd.selstart.y - (_thd.selend.y & ~TILE_UNIT_MASK);
02500 uint w = abs(dx) + TILE_SIZE;
02501 uint h = abs(dy) + TILE_SIZE;
02502
02503 if (method & ~(VPM_RAILDIRS | VPM_SIGNALDIRS)) {
02504
02505 method &= ~(VPM_RAILDIRS | VPM_SIGNALDIRS);
02506 int raw_dx = _thd.selstart.x - _thd.selend.x;
02507 int raw_dy = _thd.selstart.y - _thd.selend.y;
02508 switch (method) {
02509 case VPM_FIX_X:
02510 b = HT_LINE | HT_DIR_Y;
02511 x = _thd.selstart.x;
02512 break;
02513
02514 case VPM_FIX_Y:
02515 b = HT_LINE | HT_DIR_X;
02516 y = _thd.selstart.y;
02517 break;
02518
02519 case VPM_FIX_HORIZONTAL:
02520 if (dx == -dy) {
02521
02522
02523 b = (x & TILE_UNIT_MASK) + (y & TILE_UNIT_MASK) >= TILE_SIZE ? HT_LINE | HT_DIR_HL : HT_LINE | HT_DIR_HU;
02524 } else {
02525
02526
02527 b = dx + dy >= (int)TILE_SIZE ? HT_LINE | HT_DIR_HU : HT_LINE | HT_DIR_HL;
02528
02529
02530
02531
02532 int offset = (raw_dx - raw_dy) / 2;
02533 x = _thd.selstart.x - (offset & ~TILE_UNIT_MASK);
02534 y = _thd.selstart.y + (offset & ~TILE_UNIT_MASK);
02535
02536
02537 if ((offset & TILE_UNIT_MASK) > (TILE_SIZE / 2)) {
02538 if (dx + dy >= (int)TILE_SIZE) {
02539 x += (dx + dy < 0) ? (int)TILE_SIZE : -(int)TILE_SIZE;
02540 } else {
02541 y += (dx + dy < 0) ? (int)TILE_SIZE : -(int)TILE_SIZE;
02542 }
02543 }
02544
02545
02546 CheckUnderflow(x, y, 1);
02547 CheckUnderflow(y, x, 1);
02548 CheckOverflow(x, y, (MapMaxX() - 1) * TILE_SIZE, 1);
02549 CheckOverflow(y, x, (MapMaxY() - 1) * TILE_SIZE, 1);
02550 assert(x >= 0 && y >= 0 && x <= (int)(MapMaxX() * TILE_SIZE) && y <= (int)(MapMaxY() * TILE_SIZE));
02551 }
02552 break;
02553
02554 case VPM_FIX_VERTICAL:
02555 if (dx == dy) {
02556
02557
02558 b = (x & TILE_UNIT_MASK) > (y & TILE_UNIT_MASK) ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR;
02559 } else {
02560
02561
02562 b = dx < dy ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR;
02563
02564
02565
02566
02567 int offset = (raw_dx + raw_dy + (int)TILE_SIZE) / 2;
02568 x = _thd.selstart.x - (offset & ~TILE_UNIT_MASK);
02569 y = _thd.selstart.y - (offset & ~TILE_UNIT_MASK);
02570
02571
02572 if ((offset & TILE_UNIT_MASK) > (TILE_SIZE / 2)) {
02573 if (dx - dy < 0) {
02574 y += (dx > dy) ? (int)TILE_SIZE : -(int)TILE_SIZE;
02575 } else {
02576 x += (dx < dy) ? (int)TILE_SIZE : -(int)TILE_SIZE;
02577 }
02578 }
02579
02580
02581 CheckUnderflow(x, y, -1);
02582 CheckUnderflow(y, x, -1);
02583 CheckOverflow(x, y, (MapMaxX() - 1) * TILE_SIZE, -1);
02584 CheckOverflow(y, x, (MapMaxY() - 1) * TILE_SIZE, -1);
02585 assert(x >= 0 && y >= 0 && x <= (int)(MapMaxX() * TILE_SIZE) && y <= (int)(MapMaxY() * TILE_SIZE));
02586 }
02587 break;
02588
02589 default:
02590 NOT_REACHED();
02591 }
02592 } else if (TileVirtXY(_thd.selstart.x, _thd.selstart.y) == TileVirtXY(x, y)) {
02593 if (method & VPM_RAILDIRS) {
02594 b = GetAutorailHT(x, y);
02595 } else {
02596 b = HT_RECT;
02597 }
02598 } else if (h == TILE_SIZE) {
02599 if (dx == (int)TILE_SIZE) {
02600 b = (Check2x1AutoRail(3)) | HT_LINE;
02601 } else if (dx == -(int)TILE_SIZE) {
02602 b = (Check2x1AutoRail(2)) | HT_LINE;
02603 } else {
02604 b = HT_LINE | HT_DIR_X;
02605 }
02606 y = _thd.selstart.y;
02607 } else if (w == TILE_SIZE) {
02608 if (dy == (int)TILE_SIZE) {
02609 b = (Check2x1AutoRail(1)) | HT_LINE;
02610 } else if (dy == -(int)TILE_SIZE) {
02611 b = (Check2x1AutoRail(0)) | HT_LINE;
02612 } else {
02613 b = HT_LINE | HT_DIR_Y;
02614 }
02615 x = _thd.selstart.x;
02616 } else if (w > h * 2) {
02617 b = HT_LINE | HT_DIR_X;
02618 y = _thd.selstart.y;
02619 } else if (h > w * 2) {
02620 b = HT_LINE | HT_DIR_Y;
02621 x = _thd.selstart.x;
02622 } else {
02623 int d = w - h;
02624 _thd.selend.x = _thd.selend.x & ~TILE_UNIT_MASK;
02625 _thd.selend.y = _thd.selend.y & ~TILE_UNIT_MASK;
02626
02627
02628 if (x > _thd.selstart.x) {
02629 if (y > _thd.selstart.y) {
02630
02631 if (d == 0) {
02632 b = (x & TILE_UNIT_MASK) > (y & TILE_UNIT_MASK) ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR;
02633 } else if (d >= 0) {
02634 x = _thd.selstart.x + h;
02635 b = HT_LINE | HT_DIR_VL;
02636 } else {
02637 y = _thd.selstart.y + w;
02638 b = HT_LINE | HT_DIR_VR;
02639 }
02640 } else {
02641
02642 if (d == 0) {
02643 b = (x & TILE_UNIT_MASK) + (y & TILE_UNIT_MASK) >= TILE_SIZE ? HT_LINE | HT_DIR_HL : HT_LINE | HT_DIR_HU;
02644 } else if (d >= 0) {
02645 x = _thd.selstart.x + h;
02646 b = HT_LINE | HT_DIR_HL;
02647 } else {
02648 y = _thd.selstart.y - w;
02649 b = HT_LINE | HT_DIR_HU;
02650 }
02651 }
02652 } else {
02653 if (y > _thd.selstart.y) {
02654
02655 if (d == 0) {
02656 b = (x & TILE_UNIT_MASK) + (y & TILE_UNIT_MASK) >= TILE_SIZE ? HT_LINE | HT_DIR_HL : HT_LINE | HT_DIR_HU;
02657 } else if (d >= 0) {
02658 x = _thd.selstart.x - h;
02659 b = HT_LINE | HT_DIR_HU;
02660 } else {
02661 y = _thd.selstart.y + w;
02662 b = HT_LINE | HT_DIR_HL;
02663 }
02664 } else {
02665
02666 if (d == 0) {
02667 b = (x & TILE_UNIT_MASK) > (y & TILE_UNIT_MASK) ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR;
02668 } else if (d >= 0) {
02669 x = _thd.selstart.x - h;
02670 b = HT_LINE | HT_DIR_VR;
02671 } else {
02672 y = _thd.selstart.y - w;
02673 b = HT_LINE | HT_DIR_VL;
02674 }
02675 }
02676 }
02677 }
02678
02679 if (_settings_client.gui.measure_tooltip) {
02680 TileIndex t0 = TileVirtXY(_thd.selstart.x, _thd.selstart.y);
02681 TileIndex t1 = TileVirtXY(x, y);
02682 uint distance = DistanceManhattan(t0, t1) + 1;
02683 byte index = 0;
02684 uint64 params[2];
02685
02686 if (distance != 1) {
02687 int heightdiff = CalcHeightdiff(b, distance, t0, t1);
02688
02689
02690
02691 if ((b & HT_DIR_MASK) != HT_DIR_X && (b & HT_DIR_MASK) != HT_DIR_Y) {
02692 distance = CeilDiv(distance, 2);
02693 }
02694
02695 params[index++] = distance;
02696 if (heightdiff != 0) params[index++] = heightdiff;
02697 }
02698
02699 ShowMeasurementTooltips(measure_strings_length[index], index, params);
02700 }
02701
02702 _thd.selend.x = x;
02703 _thd.selend.y = y;
02704 _thd.next_drawstyle = b;
02705 }
02706
02714 void VpSelectTilesWithMethod(int x, int y, ViewportPlaceMethod method)
02715 {
02716 int sx, sy;
02717 HighLightStyle style;
02718
02719 if (x == -1) {
02720 _thd.selend.x = -1;
02721 return;
02722 }
02723
02724
02725 if (method & (VPM_RAILDIRS | VPM_SIGNALDIRS)) {
02726 _thd.selend.x = x;
02727 _thd.selend.y = y;
02728 CalcRaildirsDrawstyle(x, y, method);
02729 return;
02730 }
02731
02732
02733 if ((_thd.next_drawstyle & HT_DRAG_MASK) == HT_POINT) {
02734 x += TILE_SIZE / 2;
02735 y += TILE_SIZE / 2;
02736 }
02737
02738 sx = _thd.selstart.x;
02739 sy = _thd.selstart.y;
02740
02741 int limit = 0;
02742
02743 switch (method) {
02744 case VPM_X_OR_Y:
02745 if (abs(sy - y) < abs(sx - x)) {
02746 y = sy;
02747 style = HT_DIR_X;
02748 } else {
02749 x = sx;
02750 style = HT_DIR_Y;
02751 }
02752 goto calc_heightdiff_single_direction;
02753
02754 case VPM_X_LIMITED:
02755 limit = (_thd.sizelimit - 1) * TILE_SIZE;
02756
02757
02758 case VPM_FIX_X:
02759 x = sx;
02760 style = HT_DIR_Y;
02761 goto calc_heightdiff_single_direction;
02762
02763 case VPM_Y_LIMITED:
02764 limit = (_thd.sizelimit - 1) * TILE_SIZE;
02765
02766
02767 case VPM_FIX_Y:
02768 y = sy;
02769 style = HT_DIR_X;
02770
02771 calc_heightdiff_single_direction:;
02772 if (limit > 0) {
02773 x = sx + Clamp(x - sx, -limit, limit);
02774 y = sy + Clamp(y - sy, -limit, limit);
02775 }
02776 if (_settings_client.gui.measure_tooltip) {
02777 TileIndex t0 = TileVirtXY(sx, sy);
02778 TileIndex t1 = TileVirtXY(x, y);
02779 uint distance = DistanceManhattan(t0, t1) + 1;
02780 byte index = 0;
02781 uint64 params[2];
02782
02783 if (distance != 1) {
02784
02785
02786
02787
02788
02789 int heightdiff = CalcHeightdiff(HT_LINE | style, 0, t0, t1);
02790
02791 params[index++] = distance;
02792 if (heightdiff != 0) params[index++] = heightdiff;
02793 }
02794
02795 ShowMeasurementTooltips(measure_strings_length[index], index, params);
02796 }
02797 break;
02798
02799 case VPM_X_AND_Y_LIMITED:
02800 limit = (_thd.sizelimit - 1) * TILE_SIZE;
02801 x = sx + Clamp(x - sx, -limit, limit);
02802 y = sy + Clamp(y - sy, -limit, limit);
02803
02804
02805 case VPM_X_AND_Y:
02806 if (_settings_client.gui.measure_tooltip) {
02807 static const StringID measure_strings_area[] = {
02808 STR_NULL, STR_NULL, STR_MEASURE_AREA, STR_MEASURE_AREA_HEIGHTDIFF
02809 };
02810
02811 TileIndex t0 = TileVirtXY(sx, sy);
02812 TileIndex t1 = TileVirtXY(x, y);
02813 uint dx = Delta(TileX(t0), TileX(t1)) + 1;
02814 uint dy = Delta(TileY(t0), TileY(t1)) + 1;
02815 byte index = 0;
02816 uint64 params[3];
02817
02818
02819
02820 style = (HighLightStyle)_thd.next_drawstyle;
02821 if (_thd.IsDraggingDiagonal()) {
02822
02823
02824
02825
02826
02827
02828
02829
02830
02831
02832 int dist_x = TileX(t0) - TileX(t1);
02833 int dist_y = TileY(t0) - TileY(t1);
02834 int a_max = dist_x + dist_y;
02835 int b_max = dist_y - dist_x;
02836
02837
02838
02839 a_max = abs(a_max + (a_max > 0 ? 2 : -2)) / 2;
02840 b_max = abs(b_max + (b_max > 0 ? 2 : -2)) / 2;
02841
02842
02843
02844
02845
02846 if (a_max != 1 || b_max != 1) {
02847 dx = a_max;
02848 dy = b_max;
02849 }
02850 } else if (style & HT_RECT) {
02851 if (dx == 1) {
02852 style = HT_LINE | HT_DIR_Y;
02853 } else if (dy == 1) {
02854 style = HT_LINE | HT_DIR_X;
02855 }
02856 }
02857
02858 if (dx != 1 || dy != 1) {
02859 int heightdiff = CalcHeightdiff(style, 0, t0, t1);
02860
02861 params[index++] = dx - (style & HT_POINT ? 1 : 0);
02862 params[index++] = dy - (style & HT_POINT ? 1 : 0);
02863 if (heightdiff != 0) params[index++] = heightdiff;
02864 }
02865
02866 ShowMeasurementTooltips(measure_strings_area[index], index, params);
02867 }
02868 break;
02869
02870 default: NOT_REACHED();
02871 }
02872
02873 _thd.selend.x = x;
02874 _thd.selend.y = y;
02875 }
02876
02881 EventState VpHandlePlaceSizingDrag()
02882 {
02883 if (_special_mouse_mode != WSM_SIZING) return ES_NOT_HANDLED;
02884
02885
02886 Window *w = _thd.GetCallbackWnd();
02887 if (w == NULL) {
02888 ResetObjectToPlace();
02889 return ES_HANDLED;
02890 }
02891
02892
02893 if (_left_button_down) {
02894 w->OnPlaceDrag(_thd.select_method, _thd.select_proc, GetTileBelowCursor());
02895 return ES_HANDLED;
02896 }
02897
02898
02899
02900 _special_mouse_mode = WSM_NONE;
02901 HighLightStyle others = _thd.place_mode & ~(HT_DRAG_MASK | HT_DIR_MASK);
02902 if ((_thd.next_drawstyle & HT_DRAG_MASK) == HT_RECT) {
02903 _thd.place_mode = HT_RECT | others;
02904 } else if (_thd.select_method & VPM_SIGNALDIRS) {
02905 _thd.place_mode = HT_RECT | others;
02906 } else if (_thd.select_method & VPM_RAILDIRS) {
02907 _thd.place_mode = (_thd.select_method & ~VPM_RAILDIRS) ? _thd.next_drawstyle : (HT_RAIL | others);
02908 } else {
02909 _thd.place_mode = HT_POINT | others;
02910 }
02911 SetTileSelectSize(1, 1);
02912
02913 w->OnPlaceMouseUp(_thd.select_method, _thd.select_proc, _thd.selend, TileVirtXY(_thd.selstart.x, _thd.selstart.y), TileVirtXY(_thd.selend.x, _thd.selend.y));
02914
02915 return ES_HANDLED;
02916 }
02917
02918 void SetObjectToPlaceWnd(CursorID icon, PaletteID pal, HighLightStyle mode, Window *w)
02919 {
02920 SetObjectToPlace(icon, pal, mode, w->window_class, w->window_number);
02921 }
02922
02923 #include "table/animcursors.h"
02924
02925 void SetObjectToPlace(CursorID icon, PaletteID pal, HighLightStyle mode, WindowClass window_class, WindowNumber window_num)
02926 {
02927 if (_thd.window_class != WC_INVALID) {
02928
02929 Window *w = _thd.GetCallbackWnd();
02930
02931
02932
02933
02934
02935 _thd.window_class = WC_INVALID;
02936 if (w != NULL) w->OnPlaceObjectAbort();
02937 }
02938
02939
02940 if ((_thd.drawstyle & HT_DRAG_MASK) != HT_NONE) SetSelectionTilesDirty();
02941
02942 SetTileSelectSize(1, 1);
02943
02944 _thd.make_square_red = false;
02945
02946 if (mode == HT_DRAG) {
02947 mode = HT_NONE;
02948 _special_mouse_mode = WSM_DRAGDROP;
02949 } else {
02950 _special_mouse_mode = WSM_NONE;
02951 }
02952
02953 _thd.place_mode = mode;
02954 _thd.window_class = window_class;
02955 _thd.window_number = window_num;
02956
02957 if ((mode & HT_DRAG_MASK) == HT_SPECIAL) {
02958 VpStartPreSizing();
02959 }
02960
02961 if ((icon & ANIMCURSOR_FLAG) != 0) {
02962 SetAnimatedMouseCursor(_animcursors[icon & ~ANIMCURSOR_FLAG]);
02963 } else {
02964 SetMouseCursor(icon, pal);
02965 }
02966
02967 }
02968
02969 void ResetObjectToPlace()
02970 {
02971 SetObjectToPlace(SPR_CURSOR_MOUSE, PAL_NONE, HT_NONE, WC_MAIN_WINDOW, 0);
02972 }
02973
02974 Point GetViewportStationMiddle(const ViewPort *vp, const Station *st)
02975 {
02976 int x = TileX(st->xy) * TILE_SIZE;
02977 int y = TileY(st->xy) * TILE_SIZE;
02978 int z = GetSlopePixelZ(Clamp(x, 0, MapSizeX() * TILE_SIZE - 1), Clamp(y, 0, MapSizeY() * TILE_SIZE - 1));
02979
02980 Point p = RemapCoords(x, y, z);
02981 p.x = UnScaleByZoom(p.x - vp->virtual_left, vp->zoom) + vp->left;
02982 p.y = UnScaleByZoom(p.y - vp->virtual_top, vp->zoom) + vp->top;
02983 return p;
02984 }
02985
02987 struct ViewportSSCSS {
02988 VpSorterChecker fct_checker;
02989 VpSpriteSorter fct_sorter;
02990 };
02991
02993 static ViewportSSCSS _vp_sprite_sorters[] = {
02994 #ifdef WITH_SSE
02995 { &ViewportSortParentSpritesSSE41Checker, &ViewportSortParentSpritesSSE41 },
02996 #endif
02997 { &ViewportSortParentSpritesChecker, &ViewportSortParentSprites }
02998 };
02999
03001 void InitializeSpriteSorter()
03002 {
03003 for (uint i = 0; i < lengthof(_vp_sprite_sorters); i++) {
03004 if (_vp_sprite_sorters[i].fct_checker()) {
03005 _vp_sprite_sorter = _vp_sprite_sorters[i].fct_sorter;
03006 break;
03007 }
03008 }
03009 assert(_vp_sprite_sorter != NULL);
03010 }