tron@2186: /* $Id$ */ tron@2186: truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" tron@1299: #include "debug.h" tron@2163: #include "functions.h" tron@2662: #include "gui.h" tron@1349: #include "spritecache.h" tron@1309: #include "strings.h" tron@1363: #include "table/sprites.h" tron@507: #include "table/strings.h" tron@679: #include "map.h" truelight@0: #include "viewport.h" truelight@0: #include "window.h" truelight@0: #include "vehicle.h" truelight@0: #include "station.h" truelight@0: #include "gfx.h" truelight@0: #include "town.h" truelight@988: #include "signs.h" truelight@1542: #include "waypoint.h" tron@2159: #include "variables.h" bjarni@2676: #include "train.h" truelight@0: truelight@133: #define VIEWPORT_DRAW_MEM (65536 * 2) truelight@0: truelight@0: static bool _added_tile_sprite; truelight@0: static bool _offset_ground_sprites; truelight@0: truelight@0: typedef struct StringSpriteToDraw { truelight@0: uint16 string; truelight@0: uint16 color; truelight@0: struct StringSpriteToDraw *next; tron@849: int32 x; tron@849: int32 y; truelight@835: uint32 params[3]; truelight@0: uint16 width; truelight@0: } StringSpriteToDraw; truelight@0: truelight@0: typedef struct TileSpriteToDraw { truelight@0: uint32 image; truelight@0: struct TileSpriteToDraw *next; tron@849: int32 x; tron@849: int32 y; truelight@0: byte z; truelight@0: } TileSpriteToDraw; truelight@0: truelight@0: typedef struct ChildScreenSpriteToDraw { truelight@0: uint32 image; tron@849: int32 x; tron@849: int32 y; truelight@0: struct ChildScreenSpriteToDraw *next; truelight@0: } ChildScreenSpriteToDraw; truelight@0: truelight@0: typedef struct ParentSpriteToDraw { truelight@0: uint32 image; tron@849: int32 left; tron@849: int32 top; tron@849: int32 right; tron@849: int32 bottom; tron@849: int32 tile_x; tron@849: int32 tile_y; tron@849: int32 tile_right; tron@849: int32 tile_bottom; truelight@0: ChildScreenSpriteToDraw *child; truelight@0: byte unk16; truelight@0: byte tile_z; truelight@0: byte tile_z_bottom; truelight@0: } ParentSpriteToDraw; truelight@0: ludde@2109: // Quick hack to know how much memory to reserve when allocating from the spritelist ludde@2109: // to prevent a buffer overflow. ludde@2109: #define LARGEST_SPRITELIST_STRUCT ParentSpriteToDraw ludde@2109: truelight@0: typedef struct ViewportDrawer { truelight@0: DrawPixelInfo dpi; truelight@193: tron@2116: byte *spritelist_mem; tron@2116: const byte *eof_spritelist_mem; truelight@193: truelight@0: StringSpriteToDraw **last_string, *first_string; truelight@0: TileSpriteToDraw **last_tile, *first_tile; truelight@0: truelight@0: ChildScreenSpriteToDraw **last_child; truelight@0: truelight@0: ParentSpriteToDraw **parent_list; tron@2116: ParentSpriteToDraw * const *eof_parent_list; truelight@0: truelight@0: byte combine_sprites; truelight@0: truelight@0: int offs_x, offs_y; // used when drawing ground sprites relative truelight@0: bool ground_child; truelight@0: } ViewportDrawer; truelight@0: truelight@0: static ViewportDrawer *_cur_vd; truelight@0: tron@1863: TileHighlightData _thd; truelight@0: static TileInfo *_cur_ti; truelight@0: truelight@0: tron@2116: static Point MapXYZToViewport(const ViewPort *vp, uint x, uint y, uint z) truelight@0: { truelight@0: Point p = RemapCoords(x, y, z); tron@2116: p.x -= vp->virtual_width / 2; tron@2116: p.y -= vp->virtual_height / 2; truelight@0: return p; truelight@0: } truelight@0: truelight@193: void AssignWindowViewport(Window *w, int x, int y, truelight@0: int width, int height, uint32 follow_flags, byte zoom) truelight@0: { truelight@0: ViewPort *vp; truelight@0: Point pt; truelight@0: uint32 bit; truelight@0: tron@2116: for (vp = _viewports, bit = 1; ; vp++, bit <<= 1) { tron@2116: assert(vp != endof(_viewports)); tron@2116: if (vp->width == 0) break; truelight@0: } truelight@0: _active_viewports |= bit; truelight@0: truelight@0: vp->left = x + w->left; truelight@0: vp->top = y + w->top; truelight@0: vp->width = width; truelight@0: vp->height = height; truelight@193: truelight@0: vp->zoom = zoom; truelight@0: truelight@0: vp->virtual_width = width << zoom; truelight@0: vp->virtual_height = height << zoom; truelight@0: truelight@0: if (follow_flags & 0x80000000) { tron@2116: const Vehicle *veh; tron@2116: tron@2116: WP(w, vp_d).follow_vehicle = (VehicleID)(follow_flags & 0xFFFF); tron@2116: veh = GetVehicle(WP(w, vp_d).follow_vehicle); truelight@0: pt = MapXYZToViewport(vp, veh->x_pos, veh->y_pos, veh->z_pos); truelight@0: } else { tron@2116: uint x = TileX(follow_flags) * 16; tron@2116: uint y = TileY(follow_flags) * 16; tron@2116: tron@2116: WP(w, vp_d).follow_vehicle = INVALID_VEHICLE; tron@2116: pt = MapXYZToViewport(vp, x, y, GetSlopeZ(x, y)); truelight@0: } truelight@0: tron@2116: WP(w, vp_d).scrollpos_x = pt.x; tron@2116: WP(w, vp_d).scrollpos_y = pt.y; truelight@0: w->viewport = vp; truelight@0: vp->virtual_left = 0;//pt.x; truelight@0: vp->virtual_top = 0;//pt.y; truelight@0: } truelight@0: truelight@0: static Point _vp_move_offs; truelight@0: truelight@0: static void DoSetViewportPosition(Window *w, int left, int top, int width, int height) truelight@0: { truelight@0: for (; w < _last_window; w++) { truelight@0: if (left + width > w->left && tron@2116: w->left + w->width > left && truelight@0: top + height > w->top && tron@2116: w->top + w->height > top) { truelight@193: truelight@0: if (left < w->left) { truelight@0: DoSetViewportPosition(w, left, top, w->left - left, height); truelight@0: DoSetViewportPosition(w, left + (w->left - left), top, width - (w->left - left), height); truelight@0: return; truelight@0: } truelight@0: truelight@0: if (left + width > w->left + w->width) { truelight@0: DoSetViewportPosition(w, left, top, (w->left + w->width - left), height); truelight@0: DoSetViewportPosition(w, left + (w->left + w->width - left), top, width - (w->left + w->width - left) , height); truelight@0: return; truelight@0: } truelight@0: truelight@0: if (top < w->top) { truelight@0: DoSetViewportPosition(w, left, top, width, (w->top - top)); truelight@0: DoSetViewportPosition(w, left, top + (w->top - top), width, height - (w->top - top)); truelight@0: return; truelight@0: } truelight@0: truelight@0: if (top + height > w->top + w->height) { truelight@0: DoSetViewportPosition(w, left, top, width, (w->top + w->height - top)); truelight@0: DoSetViewportPosition(w, left, top + (w->top + w->height - top), width , height - (w->top + w->height - top)); truelight@0: return; truelight@0: } truelight@0: truelight@0: return; truelight@0: } truelight@0: } truelight@0: truelight@0: { truelight@0: int xo = _vp_move_offs.x; truelight@0: int yo = _vp_move_offs.y; truelight@0: truelight@0: if (abs(xo) >= width || abs(yo) >= height) { truelight@0: /* fully_outside */ tron@2116: RedrawScreenRect(left, top, left + width, top + height); truelight@0: return; truelight@0: } truelight@0: truelight@0: GfxScroll(left, top, width, height, xo, yo); truelight@0: truelight@0: if (xo > 0) { truelight@0: RedrawScreenRect(left, top, xo + left, top + height); truelight@0: left += xo; truelight@0: width -= xo; truelight@0: } else if (xo < 0) { truelight@0: RedrawScreenRect(left+width+xo, top, left+width, top + height); truelight@0: width += xo; truelight@0: } truelight@0: truelight@0: if (yo > 0) { truelight@0: RedrawScreenRect(left, top, width+left, top + yo); truelight@0: } else if (yo < 0) { truelight@0: RedrawScreenRect(left, top + height + yo, width+left, top + height); truelight@0: } truelight@0: } truelight@0: } truelight@0: tron@2817: static void SetViewportPosition(Window* w, int x, int y) truelight@0: { truelight@0: ViewPort *vp = w->viewport; truelight@0: int old_left = vp->virtual_left; truelight@0: int old_top = vp->virtual_top; truelight@0: int i; truelight@0: int left, top, width, height; truelight@0: truelight@0: vp->virtual_left = x; truelight@0: vp->virtual_top = y; truelight@0: truelight@0: old_left >>= vp->zoom; truelight@0: old_top >>= vp->zoom; truelight@0: x >>= vp->zoom; truelight@0: y >>= vp->zoom; truelight@0: truelight@0: old_left -= x; truelight@0: old_top -= y; truelight@0: tron@2116: if (old_top == 0 && old_left == 0) return; truelight@0: truelight@0: _vp_move_offs.x = old_left; truelight@0: _vp_move_offs.y = old_top; truelight@0: truelight@0: left = vp->left; truelight@0: top = vp->top; truelight@0: width = vp->width; truelight@0: height = vp->height; truelight@0: truelight@0: if (left < 0) { truelight@0: width += left; truelight@0: left = 0; truelight@0: } truelight@0: tron@2116: i = left + width - _screen.width; tron@2116: if (i >= 0) width -= i; truelight@0: truelight@0: if (width > 0) { truelight@0: if (top < 0) { truelight@0: height += top; truelight@0: top = 0; truelight@0: } truelight@193: tron@2116: i = top + height - _screen.height; tron@2116: if (i >= 0) height -= i; truelight@0: tron@2116: if (height > 0) DoSetViewportPosition(w + 1, left, top, width, height); truelight@0: } truelight@0: } truelight@0: truelight@0: tron@2116: ViewPort *IsPtInWindowViewport(const Window *w, int x, int y) truelight@0: { truelight@0: ViewPort *vp = w->viewport; tron@2116: truelight@0: if (vp != NULL && truelight@0: IS_INT_INSIDE(x, vp->left, vp->left + vp->width) && truelight@0: IS_INT_INSIDE(y, vp->top, vp->top + vp->height)) truelight@0: return vp; truelight@0: truelight@0: return NULL; truelight@0: } truelight@0: tron@2116: static Point TranslateXYToTileCoord(const ViewPort *vp, int x, int y) tron@1095: { truelight@0: int z; truelight@0: Point pt; truelight@0: int a,b; truelight@0: truelight@0: if ( (uint)(x -= vp->left) >= (uint)vp->width || truelight@0: (uint)(y -= vp->top) >= (uint)vp->height) { truelight@0: Point pt = {-1, -1}; truelight@0: return pt; truelight@0: } truelight@0: truelight@0: x = ((x << vp->zoom) + vp->virtual_left) >> 2; truelight@0: y = ((y << vp->zoom) + vp->virtual_top) >> 1; truelight@0: truelight@0: #if !defined(NEW_ROTATION) truelight@0: a = y-x; truelight@0: b = y+x; truelight@0: #else truelight@0: a = x+y; truelight@0: b = x-y; truelight@0: #endif truelight@0: z = GetSlopeZ(a, b) >> 1; truelight@0: z = GetSlopeZ(a+z, b+z) >> 1; truelight@0: z = GetSlopeZ(a+z, b+z) >> 1; truelight@0: z = GetSlopeZ(a+z, b+z) >> 1; truelight@0: z = GetSlopeZ(a+z, b+z) >> 1; truelight@0: truelight@0: pt.x = a+z; truelight@0: pt.y = b+z; truelight@193: tron@856: if ((uint)pt.x >= MapMaxX() * 16 || (uint)pt.y >= MapMaxY() * 16) { truelight@0: pt.x = pt.y = -1; truelight@0: } truelight@0: truelight@0: return pt; truelight@0: } truelight@0: darkvater@981: /* When used for zooming, check area below current coordinates (x,y) darkvater@981: * and return the tile of the zoomed out/in position (zoom_x, zoom_y) darkvater@981: * when you just want the tile, make x = zoom_x and y = zoom_y */ darkvater@981: static Point GetTileFromScreenXY(int x, int y, int zoom_x, int zoom_y) truelight@193: { truelight@0: Window *w; truelight@0: ViewPort *vp; truelight@0: Point pt; truelight@193: truelight@193: if ( (w = FindWindowFromPt(x, y)) != NULL && truelight@0: (vp = IsPtInWindowViewport(w, x, y)) != NULL) darkvater@981: return TranslateXYToTileCoord(vp, zoom_x, zoom_y); truelight@0: truelight@0: pt.y = pt.x = -1; truelight@0: return pt; truelight@0: } truelight@0: tron@1093: Point GetTileBelowCursor(void) truelight@0: { darkvater@981: return GetTileFromScreenXY(_cursor.pos.x, _cursor.pos.y, _cursor.pos.x, _cursor.pos.y); truelight@0: } truelight@0: darkvater@152: darkvater@152: Point GetTileZoomCenterWindow(bool in, Window * w) truelight@0: { truelight@0: int x, y; darkvater@152: ViewPort * vp; darkvater@152: darkvater@152: vp = w->viewport; darkvater@152: tron@2026: if (in) { tron@2116: x = ((_cursor.pos.x - vp->left) >> 1) + (vp->width >> 2); tron@2116: y = ((_cursor.pos.y - vp->top) >> 1) + (vp->height >> 2); tron@2116: } else { darkvater@152: x = vp->width - (_cursor.pos.x - vp->left); darkvater@152: y = vp->height - (_cursor.pos.y - vp->top); truelight@0: } darkvater@981: /* Get the tile below the cursor and center on the zoomed-out center */ darkvater@981: return GetTileFromScreenXY(_cursor.pos.x, _cursor.pos.y, x + vp->left, y + vp->top); truelight@0: } truelight@0: tron@849: void DrawGroundSpriteAt(uint32 image, int32 x, int32 y, byte z) truelight@0: { truelight@0: ViewportDrawer *vd = _cur_vd; truelight@0: TileSpriteToDraw *ts; truelight@193: celestar@2187: assert((image & SPRITE_MASK) < MAX_SPRITES); truelight@0: tron@2116: if (vd->spritelist_mem >= vd->eof_spritelist_mem) { celestar@2013: DEBUG(misc, 0) ("Out of sprite mem"); truelight@0: return; truelight@0: } tron@2116: ts = (TileSpriteToDraw*)vd->spritelist_mem; truelight@0: truelight@0: vd->spritelist_mem += sizeof(TileSpriteToDraw); truelight@193: truelight@0: ts->image = image; truelight@0: ts->next = NULL; truelight@0: ts->x = x; truelight@0: ts->y = y; truelight@0: ts->z = z; truelight@0: *vd->last_tile = ts; truelight@193: vd->last_tile = &ts->next; truelight@0: } truelight@0: truelight@0: void DrawGroundSprite(uint32 image) truelight@0: { truelight@0: if (_offset_ground_sprites) { truelight@0: // offset ground sprite because of foundation? truelight@0: AddChildSpriteScreen(image, _cur_vd->offs_x, _cur_vd->offs_y); truelight@0: } else { dominik@1083: _added_tile_sprite = true; truelight@0: DrawGroundSpriteAt(image, _cur_ti->x, _cur_ti->y, _cur_ti->z); truelight@0: } truelight@0: } truelight@0: truelight@0: truelight@0: void OffsetGroundSprite(int x, int y) truelight@0: { truelight@0: _cur_vd->offs_x = x; truelight@0: _cur_vd->offs_y = y; truelight@0: _offset_ground_sprites = true; truelight@0: } truelight@0: truelight@0: static void AddCombinedSprite(uint32 image, int x, int y, byte z) truelight@0: { tron@2116: const ViewportDrawer *vd = _cur_vd; truelight@0: Point pt = RemapCoords(x, y, z); tron@2319: const Sprite* spr = GetSprite(image & SPRITE_MASK); truelight@0: tron@2319: if (pt.x + spr->x_offs >= vd->dpi.left + vd->dpi.width || tron@2319: pt.x + spr->x_offs + spr->width <= vd->dpi.left || tron@2319: pt.y + spr->y_offs >= vd->dpi.top + vd->dpi.height || tron@2319: pt.y + spr->y_offs + spr->height <= vd->dpi.top) truelight@0: return; truelight@0: tron@2116: AddChildSpriteScreen(image, pt.x - vd->parent_list[-1]->left, pt.y - vd->parent_list[-1]->top); truelight@0: } truelight@0: truelight@0: truelight@0: void AddSortableSpriteToDraw(uint32 image, int x, int y, int w, int h, byte dz, byte z) truelight@0: { truelight@0: ViewportDrawer *vd = _cur_vd; truelight@0: ParentSpriteToDraw *ps; tron@2319: const Sprite* spr; truelight@0: Point pt; truelight@0: celestar@2187: assert((image & SPRITE_MASK) < MAX_SPRITES); truelight@0: truelight@0: if (vd->combine_sprites == 2) { truelight@0: AddCombinedSprite(image, x, y, z); truelight@0: return; truelight@0: } truelight@0: truelight@0: vd->last_child = NULL; truelight@0: tron@2116: if (vd->spritelist_mem >= vd->eof_spritelist_mem) { celestar@2013: DEBUG(misc, 0) ("Out of sprite mem"); truelight@0: return; truelight@0: } tron@2116: ps = (ParentSpriteToDraw*)vd->spritelist_mem; tron@2116: truelight@133: if (vd->parent_list >= vd->eof_parent_list) { truelight@133: // This can happen rarely, mostly when you zoom out completely truelight@193: // and have a lot of stuff that moves (and is added to the truelight@133: // sort-list, this function). To solve it, increase truelight@133: // parent_list somewhere below to a higher number. truelight@133: // This can not really hurt you, it just gives some black truelight@133: // spots on the screen ;) celestar@2013: DEBUG(misc, 0) ("Out of sprite mem (parent_list)"); truelight@133: return; truelight@193: } truelight@193: truelight@193: vd->spritelist_mem += sizeof(ParentSpriteToDraw); truelight@0: truelight@0: ps->image = image; truelight@0: ps->tile_x = x; truelight@0: ps->tile_right = x + w - 1; truelight@0: truelight@0: ps->tile_y = y; truelight@0: ps->tile_bottom = y + h - 1; truelight@0: truelight@0: ps->tile_z = z; truelight@0: ps->tile_z_bottom = z + dz - 1; truelight@0: truelight@0: pt = RemapCoords(x, y, z); truelight@0: tron@2319: spr = GetSprite(image & SPRITE_MASK); tron@2319: if ((ps->left = (pt.x += spr->x_offs)) >= vd->dpi.left + vd->dpi.width || tron@2319: (ps->right = (pt.x + spr->width )) <= vd->dpi.left || tron@2319: (ps->top = (pt.y += spr->y_offs)) >= vd->dpi.top + vd->dpi.height || tron@2319: (ps->bottom = (pt.y + spr->height)) <= vd->dpi.top) { truelight@0: return; truelight@0: } truelight@0: truelight@0: ps->unk16 = 0; truelight@0: ps->child = NULL; truelight@0: vd->last_child = &ps->child; truelight@0: truelight@0: *vd->parent_list++ = ps; truelight@0: tron@2116: if (vd->combine_sprites == 1) vd->combine_sprites = 2; truelight@0: } truelight@0: tron@1093: void StartSpriteCombine(void) truelight@0: { truelight@0: _cur_vd->combine_sprites = 1; truelight@0: } truelight@0: tron@1093: void EndSpriteCombine(void) truelight@0: { truelight@0: _cur_vd->combine_sprites = 0; truelight@0: } truelight@0: truelight@0: void AddChildSpriteScreen(uint32 image, int x, int y) truelight@0: { truelight@0: ViewportDrawer *vd = _cur_vd; truelight@0: ChildScreenSpriteToDraw *cs; truelight@193: celestar@2187: assert((image & SPRITE_MASK) < MAX_SPRITES); truelight@0: tron@2116: if (vd->spritelist_mem >= vd->eof_spritelist_mem) { celestar@2013: DEBUG(misc, 0) ("Out of sprite mem"); truelight@0: return; truelight@0: } tron@2116: cs = (ChildScreenSpriteToDraw*)vd->spritelist_mem; truelight@0: tron@2116: if (vd->last_child == NULL) return; truelight@193: truelight@193: vd->spritelist_mem += sizeof(ChildScreenSpriteToDraw); truelight@0: truelight@0: *vd->last_child = cs; truelight@0: vd->last_child = &cs->next; truelight@0: truelight@0: cs->image = image; truelight@0: cs->x = x; truelight@0: cs->y = y; truelight@0: cs->next = NULL; truelight@0: } truelight@0: truelight@0: /* Returns a StringSpriteToDraw */ truelight@835: void *AddStringToDraw(int x, int y, StringID string, uint32 params_1, uint32 params_2, uint32 params_3) truelight@0: { truelight@0: ViewportDrawer *vd = _cur_vd; truelight@0: StringSpriteToDraw *ss; truelight@0: tron@2116: if (vd->spritelist_mem >= vd->eof_spritelist_mem) { celestar@2013: DEBUG(misc, 0) ("Out of sprite mem"); truelight@0: return NULL; truelight@0: } tron@2116: ss = (StringSpriteToDraw*)vd->spritelist_mem; truelight@0: truelight@0: vd->spritelist_mem += sizeof(StringSpriteToDraw); truelight@0: truelight@0: ss->string = string; truelight@0: ss->next = NULL; truelight@0: ss->x = x; truelight@0: ss->y = y; truelight@0: ss->params[0] = params_1; truelight@0: ss->params[1] = params_2; truelight@835: ss->params[2] = params_3; truelight@0: ss->width = 0; truelight@0: truelight@0: *vd->last_string = ss; truelight@0: vd->last_string = &ss->next; truelight@0: truelight@0: return ss; truelight@0: } truelight@0: truelight@0: ludde@2125: #ifdef DEBUG_HILIGHT_MARKED_TILES ludde@2125: ludde@2125: static void DrawHighlighedTile(const TileInfo *ti) truelight@0: { ludde@2125: if (_m[ti->tile].extra & 0x80) { ludde@2125: DrawSelectionSprite(PALETTE_TILE_RED_PULSATING | (SPR_SELECT_TILE + _tileh_to_sprite[ti->tileh]), ti); ludde@2125: } truelight@0: } truelight@0: ludde@2125: int _debug_marked_tiles, _debug_red_tiles; ludde@2125: ludde@2125: // Helper functions that allow you mark a tile as red. ludde@2125: void DebugMarkTile(TileIndex tile) { ludde@2125: _debug_marked_tiles++; ludde@2125: if (_m[tile].extra & 0x80) ludde@2125: return; ludde@2125: _debug_red_tiles++; ludde@2125: MarkTileDirtyByTile(tile); ludde@2125: _m[tile].extra = (_m[tile].extra & ~0xE0) | 0x80; truelight@0: } truelight@0: ludde@2125: void DebugClearMarkedTiles() truelight@0: { ludde@2125: uint size = MapSize(), i; ludde@2125: for(i=0; i!=size; i++) { ludde@2125: if (_m[i].extra & 0x80) { ludde@2125: _m[i].extra &= ~0x80; ludde@2125: MarkTileDirtyByTile(i); truelight@0: } tron@2116: } ludde@2125: _debug_red_tiles = 0; ludde@2125: _debug_red_tiles = 0; truelight@0: } truelight@0: ludde@2125: truelight@0: #endif truelight@0: truelight@0: static void DrawSelectionSprite(uint32 image, const TileInfo *ti) truelight@0: { tron@1863: if (_added_tile_sprite && !(_thd.drawstyle & HT_LINE)) { // draw on real ground dominik@1083: DrawGroundSpriteAt(image, ti->x, ti->y, ti->z + 7); dominik@1083: } else { // draw on top of foundation dominik@1083: AddSortableSpriteToDraw(image, ti->x, ti->y, 0x10, 0x10, 1, ti->z + 7); dominik@1083: } truelight@0: } truelight@0: truelight@0: static bool IsPartOfAutoLine(int px, int py) truelight@0: { tron@1863: px -= _thd.selstart.x; tron@1863: py -= _thd.selstart.y; truelight@0: tron@1863: switch(_thd.drawstyle) { dominik@1070: case HT_LINE | HT_DIR_X: return py == 0; // x direction dominik@1070: case HT_LINE | HT_DIR_Y: return px == 0; // y direction dominik@1070: case HT_LINE | HT_DIR_HU: return px == -py || px == -py - 16; // horizontal upper dominik@1070: case HT_LINE | HT_DIR_HL: return px == -py || px == -py + 16; // horizontal lower dominik@1070: case HT_LINE | HT_DIR_VL: return px == py || px == py + 16; // vertival left dominik@1070: case HT_LINE | HT_DIR_VR: return px == py || px == py - 16; // vertical right truelight@0: default: truelight@0: NOT_REACHED(); truelight@0: } truelight@193: truelight@0: /* useless, but avoids compiler warning this way */ truelight@0: return 0; truelight@0: } truelight@0: dominik@1070: // [direction][side] Darkvater@2710: static const int _AutorailType[6][2] = { dominik@1070: { HT_DIR_X, HT_DIR_X }, dominik@1070: { HT_DIR_Y, HT_DIR_Y }, dominik@1070: { HT_DIR_HU, HT_DIR_HL }, dominik@1070: { HT_DIR_HL, HT_DIR_HU }, dominik@1070: { HT_DIR_VL, HT_DIR_VR }, dominik@1070: { HT_DIR_VR, HT_DIR_VL } dominik@1070: }; dominik@1070: dominik@1070: #include "table/autorail.h" dominik@1070: truelight@0: static void DrawTileSelection(const TileInfo *ti) truelight@0: { truelight@0: uint32 image; truelight@0: ludde@2125: #ifdef DEBUG_HILIGHT_MARKED_TILES ludde@2125: DrawHighlighedTile(ti); truelight@0: #endif truelight@193: truelight@0: // Draw a red error square? tron@1863: if (_thd.redsq != 0 && _thd.redsq == ti->tile) { tron@1864: DrawSelectionSprite(PALETTE_TILE_RED_PULSATING | (SPR_SELECT_TILE + _tileh_to_sprite[ti->tileh]), ti); truelight@0: return; truelight@0: } truelight@0: truelight@0: // no selection active? tron@2116: if (_thd.drawstyle == 0) return; truelight@0: truelight@0: // Inside the inner area? tron@2116: if (IS_INSIDE_1D(ti->x, _thd.pos.x, _thd.size.x) && tron@2116: IS_INSIDE_1D(ti->y, _thd.pos.y, _thd.size.y)) { tron@1863: if (_thd.drawstyle & HT_RECT) { tron@1864: image = SPR_SELECT_TILE + _tileh_to_sprite[ti->tileh]; tron@1864: if (_thd.make_square_red) image |= PALETTE_SEL_TILE_RED; truelight@0: DrawSelectionSprite(image, ti); tron@1863: } else if (_thd.drawstyle & HT_POINT) { truelight@0: // Figure out the Z coordinate for the single dot. truelight@0: byte z = ti->z; truelight@0: if (ti->tileh & 8) { truelight@193: z += 8; tron@2116: if (!(ti->tileh & 2) && (IsSteepTileh(ti->tileh))) z += 8; truelight@0: } tron@1864: DrawGroundSpriteAt(_cur_dpi->zoom != 2 ? SPR_DOT : SPR_DOT_SMALL, ti->x, ti->y, z); tron@2116: } else if (_thd.drawstyle & HT_RAIL /*&& _thd.place_mode == VHM_RAIL*/) { tron@2116: // autorail highlight piece under cursor tron@2116: uint type = _thd.drawstyle & 0xF; tron@2116: assert(type <= 5); Darkvater@2710: image = SPR_AUTORAIL_BASE + _AutorailTilehSprite[ti->tileh][_AutorailType[type][0]]; dominik@1070: tron@1864: if (_thd.make_square_red) image |= PALETTE_SEL_TILE_RED; dominik@1070: DrawSelectionSprite(image, ti); dominik@1070: tron@2116: } else if (IsPartOfAutoLine(ti->x, ti->y)) { tron@2116: // autorail highlighting long line tron@2116: int dir = _thd.drawstyle & ~0xF0; tron@2116: uint side; dominik@1070: tron@2116: if (dir < 2) { tron@2116: side = 0; tron@2116: } else { tron@2116: TileIndex start = TileVirtXY(_thd.selstart.x, _thd.selstart.y); tron@2116: int diffx = myabs(TileX(start) - TileX(ti->tile)); tron@2116: int diffy = myabs(TileY(start) - TileY(ti->tile)); tron@2116: side = myabs(diffx - diffy); tron@2116: } tron@1109: Darkvater@2710: image = SPR_AUTORAIL_BASE + _AutorailTilehSprite[ti->tileh][_AutorailType[dir][side]]; dominik@1070: tron@2116: if (_thd.make_square_red) image |= PALETTE_SEL_TILE_RED; tron@2116: DrawSelectionSprite(image, ti); tron@2116: } truelight@0: return; truelight@0: } truelight@0: truelight@0: // Check if it's inside the outer area? tron@1863: if (_thd.outersize.x && tron@1863: _thd.size.x < _thd.size.x + _thd.outersize.x && tron@1863: IS_INSIDE_1D(ti->x, _thd.pos.x + _thd.offs.x, _thd.size.x + _thd.outersize.x) && tron@1863: IS_INSIDE_1D(ti->y, _thd.pos.y + _thd.offs.y, _thd.size.y + _thd.outersize.y)) { truelight@0: // Draw a blue rect. tron@1864: DrawSelectionSprite(PALETTE_SEL_TILE_BLUE | (SPR_SELECT_TILE + _tileh_to_sprite[ti->tileh]), ti); truelight@0: return; truelight@0: } truelight@0: } truelight@0: tron@1093: static void ViewportAddLandscape(void) truelight@0: { truelight@0: ViewportDrawer *vd = _cur_vd; truelight@0: int x, y, width, height; truelight@0: TileInfo ti; truelight@0: bool direction; truelight@0: truelight@0: _cur_ti = &ti; truelight@0: truelight@0: // Transform into tile coordinates and round to closest full tile truelight@0: #if !defined(NEW_ROTATION) truelight@0: x = ((vd->dpi.top >> 1) - (vd->dpi.left >> 2)) & ~0xF; truelight@0: y = ((vd->dpi.top >> 1) + (vd->dpi.left >> 2) - 0x10) & ~0xF; truelight@0: #else truelight@0: x = ((vd->dpi.top >> 1) + (vd->dpi.left >> 2) - 0x10) & ~0xF; truelight@0: y = ((vd->dpi.left >> 2) - (vd->dpi.top >> 1)) & ~0xF; truelight@0: #endif truelight@0: // determine size of area truelight@0: { truelight@0: Point pt = RemapCoords(x, y, 241); truelight@0: width = (vd->dpi.left + vd->dpi.width - pt.x + 95) >> 6; truelight@0: height = (vd->dpi.top + vd->dpi.height - pt.y) >> 5 << 1; truelight@0: } truelight@0: truelight@0: assert(width > 0); truelight@0: assert(height > 0); truelight@193: truelight@0: direction = false; truelight@0: truelight@0: do { truelight@0: int width_cur = width; truelight@0: int x_cur = x; truelight@0: int y_cur = y; truelight@193: truelight@0: do { truelight@0: FindLandscapeHeight(&ti, x_cur, y_cur); truelight@0: #if !defined(NEW_ROTATION) truelight@0: y_cur += 0x10; truelight@0: x_cur -= 0x10; truelight@0: #else truelight@0: y_cur += 0x10; truelight@0: x_cur += 0x10; truelight@0: #endif truelight@0: _added_tile_sprite = false; truelight@0: _offset_ground_sprites = false; truelight@193: truelight@0: DrawTile(&ti); truelight@0: DrawTileSelection(&ti); truelight@0: } while (--width_cur); truelight@193: truelight@0: #if !defined(NEW_ROTATION) truelight@0: if ( (direction^=1) != 0) truelight@0: y += 0x10; truelight@0: else truelight@0: x += 0x10; truelight@0: #else truelight@0: if ( (direction^=1) != 0) truelight@0: x += 0x10; truelight@0: else truelight@0: y -= 0x10; truelight@0: #endif truelight@193: } while (--height); truelight@0: } truelight@0: truelight@0: tron@410: static void ViewportAddTownNames(DrawPixelInfo *dpi) truelight@0: { truelight@0: Town *t; truelight@0: int left, top, right, bottom; truelight@0: truelight@0: if (!(_display_opt & DO_SHOW_TOWN_NAMES) || _game_mode == GM_MENU) truelight@0: return; truelight@0: truelight@0: left = dpi->left; truelight@0: top = dpi->top; truelight@0: right = left + dpi->width; truelight@0: bottom = top + dpi->height; truelight@0: truelight@0: if (dpi->zoom < 1) { truelight@0: FOR_ALL_TOWNS(t) { truelight@0: if (t->xy && truelight@0: bottom > t->sign.top && truelight@0: top < t->sign.top + 12 && truelight@0: right > t->sign.left && truelight@0: left < t->sign.left + t->sign.width_1) { truelight@193: ludde@2070: AddStringToDraw(t->sign.left + 1, t->sign.top + 1, ludde@2070: _patches.population_in_label ? STR_TOWN_LABEL_POP : STR_TOWN_LABEL, ludde@2070: t->index, t->population, 0); truelight@0: } truelight@0: } truelight@0: } else if (dpi->zoom == 1) { truelight@0: right += 2; truelight@0: bottom += 2; truelight@0: truelight@0: FOR_ALL_TOWNS(t) { truelight@0: if (t->xy && truelight@0: bottom > t->sign.top && truelight@0: top < t->sign.top + 24 && truelight@0: right > t->sign.left && truelight@0: left < t->sign.left + t->sign.width_1*2) { truelight@193: ludde@2070: AddStringToDraw(t->sign.left + 1, t->sign.top + 1, ludde@2070: _patches.population_in_label ? STR_TOWN_LABEL_POP : STR_TOWN_LABEL, ludde@2070: t->index, t->population, 0); truelight@193: } truelight@0: } truelight@0: } else { truelight@0: right += 4; truelight@0: bottom += 5; truelight@0: truelight@0: assert(dpi->zoom == 2); truelight@0: FOR_ALL_TOWNS(t) { truelight@0: if (t->xy && truelight@0: bottom > t->sign.top && truelight@0: top < t->sign.top + 24 && truelight@0: right > t->sign.left && truelight@0: left < t->sign.left + t->sign.width_2*4) { truelight@193: ludde@2070: AddStringToDraw(t->sign.left + 5, t->sign.top + 1, STR_TOWN_LABEL_TINY_BLACK, t->index, 0, 0); ludde@2070: AddStringToDraw(t->sign.left + 1, t->sign.top - 3, STR_TOWN_LABEL_TINY_WHITE, t->index, 0, 0); truelight@193: } truelight@0: } truelight@0: } truelight@0: } truelight@0: tron@410: static void ViewportAddStationNames(DrawPixelInfo *dpi) truelight@0: { truelight@0: int left, top, right, bottom; truelight@0: Station *st; truelight@0: StringSpriteToDraw *sstd; truelight@0: truelight@0: if (!(_display_opt & DO_SHOW_STATION_NAMES) || _game_mode == GM_MENU) truelight@0: return; truelight@0: truelight@0: left = dpi->left; truelight@0: top = dpi->top; truelight@0: right = left + dpi->width; truelight@0: bottom = top + dpi->height; truelight@0: truelight@0: if (dpi->zoom < 1) { truelight@0: FOR_ALL_STATIONS(st) { truelight@0: if (st->xy && truelight@0: bottom > st->sign.top && truelight@0: top < st->sign.top + 12 && truelight@0: right > st->sign.left && truelight@0: left < st->sign.left + st->sign.width_1) { truelight@193: truelight@835: sstd=AddStringToDraw(st->sign.left + 1, st->sign.top + 1, STR_305C_0, st->index, st->facilities, 0); truelight@0: if (sstd != NULL) { truelight@0: sstd->color = (st->owner == OWNER_NONE || !st->facilities) ? 0xE : _player_colors[st->owner]; truelight@0: sstd->width = st->sign.width_1; truelight@0: } truelight@0: } truelight@0: } truelight@0: } else if (dpi->zoom == 1) { truelight@0: right += 2; truelight@0: bottom += 2; truelight@0: truelight@0: FOR_ALL_STATIONS(st) { truelight@0: if (st->xy && truelight@0: bottom > st->sign.top && truelight@0: top < st->sign.top + 24 && truelight@0: right > st->sign.left && truelight@0: left < st->sign.left + st->sign.width_1*2) { truelight@193: truelight@835: sstd=AddStringToDraw(st->sign.left + 1, st->sign.top + 1, STR_305C_0, st->index, st->facilities, 0); truelight@0: if (sstd != NULL) { truelight@0: sstd->color = (st->owner == OWNER_NONE || !st->facilities) ? 0xE : _player_colors[st->owner]; truelight@0: sstd->width = st->sign.width_1; truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: } else { truelight@0: assert(dpi->zoom == 2); truelight@0: truelight@0: right += 4; truelight@0: bottom += 5; truelight@0: truelight@0: FOR_ALL_STATIONS(st) { truelight@0: if (st->xy && truelight@0: bottom > st->sign.top && truelight@0: top < st->sign.top + 24 && truelight@0: right > st->sign.left && truelight@0: left < st->sign.left + st->sign.width_2*4) { truelight@193: hackykid@1964: sstd=AddStringToDraw(st->sign.left + 1, st->sign.top + 1, STR_STATION_SIGN_TINY, st->index, st->facilities, 0); truelight@0: if (sstd != NULL) { truelight@0: sstd->color = (st->owner == OWNER_NONE || !st->facilities) ? 0xE : _player_colors[st->owner]; truelight@0: sstd->width = st->sign.width_2 | 0x8000; truelight@0: } truelight@0: } truelight@0: } truelight@0: } truelight@0: } truelight@0: tron@410: static void ViewportAddSigns(DrawPixelInfo *dpi) truelight@0: { truelight@0: SignStruct *ss; truelight@0: int left, top, right, bottom; truelight@0: StringSpriteToDraw *sstd; truelight@0: truelight@0: if (!(_display_opt & DO_SHOW_SIGNS)) truelight@0: return; truelight@0: truelight@0: left = dpi->left; truelight@0: top = dpi->top; truelight@0: right = left + dpi->width; truelight@0: bottom = top + dpi->height; truelight@0: truelight@0: if (dpi->zoom < 1) { truelight@988: FOR_ALL_SIGNS(ss) { truelight@0: if (ss->str && truelight@0: bottom > ss->sign.top && truelight@0: top < ss->sign.top + 12 && truelight@0: right > ss->sign.left && truelight@0: left < ss->sign.left + ss->sign.width_1) { truelight@193: truelight@835: sstd=AddStringToDraw(ss->sign.left + 1, ss->sign.top + 1, STR_2806, ss->str, 0, 0); truelight@0: if (sstd != NULL) { truelight@0: sstd->width = ss->sign.width_1; dominik@1165: sstd->color = (ss->owner==OWNER_NONE)?14:_player_colors[ss->owner]; truelight@0: } truelight@0: } truelight@193: } truelight@0: } else if (dpi->zoom == 1) { truelight@0: right += 2; truelight@0: bottom += 2; truelight@988: FOR_ALL_SIGNS(ss) { truelight@0: if (ss->str && truelight@0: bottom > ss->sign.top && truelight@0: top < ss->sign.top + 24 && truelight@0: right > ss->sign.left && truelight@0: left < ss->sign.left + ss->sign.width_1*2) { truelight@193: truelight@835: sstd=AddStringToDraw(ss->sign.left + 1, ss->sign.top + 1, STR_2806, ss->str, 0, 0); truelight@0: if (sstd != NULL) { truelight@0: sstd->width = ss->sign.width_1; dominik@1172: sstd->color = (ss->owner==OWNER_NONE)?14:_player_colors[ss->owner]; truelight@0: } truelight@0: } truelight@193: } truelight@0: } else { truelight@0: right += 4; truelight@0: bottom += 5; truelight@0: truelight@988: FOR_ALL_SIGNS(ss) { truelight@0: if (ss->str && truelight@0: bottom > ss->sign.top && truelight@0: top < ss->sign.top + 24 && truelight@0: right > ss->sign.left && truelight@0: left < ss->sign.left + ss->sign.width_2*4) { truelight@193: dominik@1172: sstd=AddStringToDraw(ss->sign.left + 1, ss->sign.top + 1, STR_2002, ss->str, 0, 0); truelight@0: if (sstd != NULL) { truelight@0: sstd->width = ss->sign.width_2 | 0x8000; dominik@1172: sstd->color = (ss->owner==OWNER_NONE)?14:_player_colors[ss->owner]; truelight@0: } truelight@0: } truelight@193: } truelight@0: } truelight@0: } truelight@0: tron@410: static void ViewportAddWaypoints(DrawPixelInfo *dpi) truelight@0: { truelight@1542: Waypoint *wp; truelight@0: truelight@0: int left, top, right, bottom; truelight@0: StringSpriteToDraw *sstd; truelight@0: darkvater@395: if (!(_display_opt & DO_WAYPOINTS)) truelight@0: return; truelight@0: truelight@0: left = dpi->left; truelight@0: top = dpi->top; truelight@0: right = left + dpi->width; truelight@0: bottom = top + dpi->height; truelight@0: truelight@0: if (dpi->zoom < 1) { truelight@1542: FOR_ALL_WAYPOINTS(wp) { truelight@1542: if (wp->xy && truelight@1542: bottom > wp->sign.top && truelight@1542: top < wp->sign.top + 12 && truelight@1542: right > wp->sign.left && truelight@1542: left < wp->sign.left + wp->sign.width_1) { truelight@193: truelight@1542: sstd = AddStringToDraw(wp->sign.left + 1, wp->sign.top + 1, STR_WAYPOINT_VIEWPORT, wp->index, 0, 0); truelight@0: if (sstd != NULL) { truelight@1542: sstd->width = wp->sign.width_1; truelight@1542: sstd->color = (wp->deleted ? 0xE : 11); truelight@0: } truelight@0: } truelight@193: } truelight@0: } else if (dpi->zoom == 1) { truelight@0: right += 2; truelight@0: bottom += 2; truelight@1542: FOR_ALL_WAYPOINTS(wp) { truelight@1542: if (wp->xy && truelight@1542: bottom > wp->sign.top && truelight@1542: top < wp->sign.top + 24 && truelight@1542: right > wp->sign.left && truelight@1542: left < wp->sign.left + wp->sign.width_1*2) { truelight@193: truelight@1542: sstd = AddStringToDraw(wp->sign.left + 1, wp->sign.top + 1, STR_WAYPOINT_VIEWPORT, wp->index, 0, 0); truelight@0: if (sstd != NULL) { truelight@1542: sstd->width = wp->sign.width_1; truelight@1542: sstd->color = (wp->deleted ? 0xE : 11); truelight@0: } truelight@0: } truelight@193: } truelight@0: } else { truelight@0: right += 4; truelight@0: bottom += 5; truelight@0: truelight@1542: FOR_ALL_WAYPOINTS(wp) { truelight@1542: if (wp->xy && truelight@1542: bottom > wp->sign.top && truelight@1542: top < wp->sign.top + 24 && truelight@1542: right > wp->sign.left && truelight@1542: left < wp->sign.left + wp->sign.width_2*4) { truelight@193: truelight@1542: sstd = AddStringToDraw(wp->sign.left + 1, wp->sign.top + 1, STR_WAYPOINT_VIEWPORT_TINY, wp->index, 0, 0); truelight@0: if (sstd != NULL) { truelight@1542: sstd->width = wp->sign.width_2 | 0x8000; truelight@1542: sstd->color = (wp->deleted ? 0xE : 11); truelight@0: } truelight@0: } truelight@193: } truelight@0: } truelight@0: } truelight@0: truelight@0: void UpdateViewportSignPos(ViewportSign *sign, int left, int top, StringID str) truelight@0: { truelight@0: char buffer[128]; tron@2116: uint w; truelight@0: truelight@0: sign->top = top; truelight@0: truelight@0: GetString(buffer, str); truelight@0: w = GetStringWidth(buffer) + 3; truelight@0: sign->width_1 = w; tron@2116: sign->left = left - w / 2; truelight@0: Darkvater@1390: // zoomed out version truelight@0: _stringwidth_base = 0xE0; Darkvater@1390: w = GetStringWidth(buffer) + 3; truelight@0: _stringwidth_base = 0; Darkvater@1390: sign->width_2 = w; truelight@0: } truelight@0: truelight@0: tron@410: static void ViewportDrawTileSprites(TileSpriteToDraw *ts) truelight@193: { truelight@0: do { truelight@0: Point pt = RemapCoords(ts->x, ts->y, ts->z); truelight@0: DrawSprite(ts->image, pt.x, pt.y); tron@2116: ts = ts->next; tron@2116: } while (ts != NULL); truelight@0: } truelight@0: tron@2116: static void ViewportSortParentSprites(ParentSpriteToDraw* psd[]) truelight@0: { tron@2116: while (*psd != NULL) { tron@2116: ParentSpriteToDraw* ps = *psd; truelight@0: truelight@0: if (!(ps->unk16 & 1)) { tron@2116: ParentSpriteToDraw** psd2 = psd; tron@2116: truelight@0: ps->unk16 |= 1; truelight@193: tron@2116: while (*++psd2 != NULL) { tron@2116: ParentSpriteToDraw* ps2 = *psd2; hackykid@1934: bool mustswap = false; hackykid@1934: tron@2116: if (ps2->unk16 & 1) continue; truelight@0: hackykid@1934: // Decide which sort order algorithm to use, based on whether the sprites have some overlapping area. hackykid@1934: if (((ps2->tile_x > ps->tile_x && ps2->tile_x < ps->tile_right) || hackykid@1934: (ps2->tile_right > ps->tile_x && ps2->tile_x < ps->tile_right)) && // overlap in X hackykid@1934: ((ps2->tile_y > ps->tile_y && ps2->tile_y < ps->tile_bottom) || hackykid@1934: (ps2->tile_bottom > ps->tile_y && ps2->tile_y < ps->tile_bottom)) && // overlap in Y hackykid@1934: ((ps2->tile_z > ps->tile_z && ps2->tile_z < ps->tile_z_bottom) || hackykid@1934: (ps2->tile_z_bottom > ps->tile_z && ps2->tile_z < ps->tile_z_bottom)) ) { // overlap in Z hackykid@1934: // Sprites overlap. hackykid@1934: // Use X+Y+Z as the sorting order, so sprites nearer the bottom of the screen, hackykid@1934: // and with higher Z elevation, draw in front. hackykid@1934: // Here X,Y,Z are the coordinates of the "center of mass" of the sprite, hackykid@1934: // i.e. X=(left+right)/2, etc. hackykid@1934: // However, since we only care about order, don't actually calculate the division / 2. hackykid@1934: mustswap = ps->tile_x + ps->tile_right + ps->tile_y + ps->tile_bottom + ps->tile_z + ps->tile_z_bottom > hackykid@1934: ps2->tile_x + ps2->tile_right + ps2->tile_y + ps2->tile_bottom + ps2->tile_z + ps2->tile_z_bottom; hackykid@1934: } else { hackykid@1934: // No overlap; use the original TTD sort algorithm. hackykid@1934: mustswap = (ps->tile_right >= ps2->tile_x && hackykid@1934: ps->tile_bottom >= ps2->tile_y && hackykid@1934: ps->tile_z_bottom >= ps2->tile_z && hackykid@1934: (ps->tile_x >= ps2->tile_right || hackykid@1934: ps->tile_y >= ps2->tile_bottom || hackykid@1934: ps->tile_z >= ps2->tile_z_bottom)); hackykid@1934: } hackykid@1934: if (mustswap) { hackykid@1934: // Swap the two sprites ps and ps2 using bubble-sort algorithm. tron@2116: ParentSpriteToDraw** psd3 = psd; tron@2116: truelight@0: do { tron@2116: ParentSpriteToDraw* temp = *psd3; truelight@0: *psd3 = ps2; tron@2116: ps2 = temp; truelight@0: truelight@0: psd3++; truelight@0: } while (psd3 <= psd2); truelight@0: } truelight@0: } truelight@0: } else { truelight@0: psd++; truelight@0: } truelight@0: } truelight@0: } truelight@0: tron@2116: static void ViewportDrawParentSprites(ParentSpriteToDraw *psd[]) truelight@0: { tron@2116: for (; *psd != NULL; psd++) { tron@2116: const ParentSpriteToDraw* ps = *psd; tron@2116: Point pt = RemapCoords(ps->tile_x, ps->tile_y, ps->tile_z); tron@2116: const ChildScreenSpriteToDraw* cs; truelight@0: truelight@0: DrawSprite(ps->image, pt.x, pt.y); truelight@0: tron@2116: for (cs = ps->child; cs != NULL; cs = cs->next) { truelight@0: DrawSprite(cs->image, ps->left + cs->x, ps->top + cs->y); truelight@0: } truelight@0: } truelight@0: } truelight@0: tron@2116: static void ViewportDrawStrings(DrawPixelInfo *dpi, const StringSpriteToDraw *ss) truelight@0: { truelight@0: DrawPixelInfo dp; truelight@0: byte zoom; truelight@193: truelight@0: _cur_dpi = &dp; truelight@0: dp = *dpi; truelight@0: tron@2116: zoom = dp.zoom; truelight@0: dp.zoom = 0; truelight@0: truelight@0: dp.left >>= zoom; truelight@0: dp.top >>= zoom; truelight@0: dp.width >>= zoom; truelight@0: dp.height >>= zoom; truelight@0: truelight@0: do { truelight@0: if (ss->width != 0) { tron@2116: int x = (ss->x >> zoom) - 1; tron@2116: int y = (ss->y >> zoom) - 1; tron@2116: int bottom = y + 11; tron@2116: int w = ss->width; truelight@193: truelight@0: if (w & 0x8000) { truelight@0: w &= ~0x8000; truelight@0: y--; truelight@0: bottom -= 6; truelight@0: w -= 3; truelight@0: } truelight@0: tron@2116: /* Draw the rectangle if 'tranparent station signs' is off, tron@2116: * or if we are drawing a general text sign (STR_2806) */ tron@2116: if (!(_display_opt & DO_TRANS_SIGNS) || ss->string == STR_2806) tron@2116: DrawFrameRect( tron@2116: x, y, x + w, bottom, ss->color, tron@2116: (_display_opt & DO_TRANS_BUILDINGS) ? FR_TRANSPARENT | FR_NOBORDER : 0 tron@2116: ); truelight@0: } truelight@0: tron@534: SetDParam(0, ss->params[0]); tron@534: SetDParam(1, ss->params[1]); truelight@835: SetDParam(2, ss->params[2]); tron@2116: /* if we didn't draw a rectangle, or if transparant building is on, tron@2116: * draw the text in the color the rectangle would have */ tron@2116: if (( tron@2116: (_display_opt & DO_TRANS_BUILDINGS) || tron@2116: (_display_opt & DO_TRANS_SIGNS && ss->string != STR_2806) tron@2116: ) && ss->width != 0) { tron@2116: /* Real colors need the IS_PALETTE_COLOR flag tron@2116: * otherwise colors from _string_colormap are assumed. */ tron@2116: DrawString( tron@2116: ss->x >> zoom, (ss->y >> zoom) - (ss->width & 0x8000 ? 2 : 0), tron@2116: ss->string, (_color_list[ss->color].window_color_bgb | IS_PALETTE_COLOR) tron@2116: ); truelight@0: } else { tron@2116: DrawString( tron@2116: ss->x >> zoom, (ss->y >> zoom) - (ss->width & 0x8000 ? 2 : 0), tron@2116: ss->string, 16 tron@2116: ); truelight@0: } tron@2116: tron@2116: ss = ss->next; tron@2116: } while (ss != NULL); truelight@0: truelight@0: _cur_dpi = dpi; truelight@0: } truelight@0: tron@430: void ViewportDoDraw(const ViewPort *vp, int left, int top, int right, int bottom) truelight@0: { truelight@0: ViewportDrawer vd; truelight@0: int mask; tron@2116: int x; tron@2116: int y; truelight@0: DrawPixelInfo *old_dpi; truelight@0: truelight@0: byte mem[VIEWPORT_DRAW_MEM]; truelight@137: ParentSpriteToDraw *parent_list[6144]; truelight@0: truelight@0: _cur_vd = &vd; truelight@0: truelight@0: old_dpi = _cur_dpi; truelight@0: _cur_dpi = &vd.dpi; truelight@0: truelight@0: vd.dpi.zoom = vp->zoom; truelight@0: mask = (-1) << vp->zoom; truelight@0: truelight@0: vd.combine_sprites = 0; truelight@0: vd.ground_child = 0; truelight@0: truelight@0: vd.dpi.width = (right - left) & mask; truelight@0: vd.dpi.height = (bottom - top) & mask; truelight@0: vd.dpi.left = left & mask; truelight@0: vd.dpi.top = top & mask; truelight@0: vd.dpi.pitch = old_dpi->pitch; truelight@0: truelight@0: x = ((vd.dpi.left - (vp->virtual_left&mask)) >> vp->zoom) + vp->left; truelight@0: y = ((vd.dpi.top - (vp->virtual_top&mask)) >> vp->zoom) + vp->top; truelight@0: truelight@0: vd.dpi.dst_ptr = old_dpi->dst_ptr + x - old_dpi->left + (y - old_dpi->top) * old_dpi->pitch; truelight@0: truelight@0: vd.parent_list = parent_list; tron@2116: vd.eof_parent_list = endof(parent_list); truelight@0: vd.spritelist_mem = mem; tron@2116: vd.eof_spritelist_mem = endof(mem) - sizeof(LARGEST_SPRITELIST_STRUCT); truelight@0: vd.last_string = &vd.first_string; truelight@0: vd.first_string = NULL; truelight@0: vd.last_tile = &vd.first_tile; truelight@0: vd.first_tile = NULL; truelight@193: truelight@0: ViewportAddLandscape(); truelight@0: #if !defined(NEW_ROTATION) truelight@0: ViewportAddVehicles(&vd.dpi); truelight@0: DrawTextEffects(&vd.dpi); truelight@0: truelight@0: ViewportAddTownNames(&vd.dpi); truelight@0: ViewportAddStationNames(&vd.dpi); truelight@0: ViewportAddSigns(&vd.dpi); darkvater@395: ViewportAddWaypoints(&vd.dpi); truelight@0: #endif truelight@0: truelight@133: // This assert should never happen (because the length of the parent_list truelight@133: // is checked) tron@979: assert(vd.parent_list <= endof(parent_list)); truelight@133: tron@2116: if (vd.first_tile != NULL) ViewportDrawTileSprites(vd.first_tile); truelight@0: truelight@0: /* null terminate parent sprite list */ truelight@0: *vd.parent_list = NULL; truelight@0: truelight@0: ViewportSortParentSprites(parent_list); truelight@0: ViewportDrawParentSprites(parent_list); truelight@193: tron@2116: if (vd.first_string != NULL) ViewportDrawStrings(&vd.dpi, vd.first_string); truelight@193: truelight@0: _cur_dpi = old_dpi; truelight@0: } truelight@0: truelight@0: // Make sure we don't draw a too big area at a time. truelight@0: // If we do, the sprite memory will overflow. truelight@0: static void ViewportDrawChk(ViewPort *vp, int left, int top, int right, int bottom) truelight@0: { truelight@0: if (((bottom - top) * (right - left) << vp->zoom) > 180000) { truelight@0: if ((bottom - top) > (right - left)) { truelight@0: int t = (top + bottom) >> 1; truelight@0: ViewportDrawChk(vp, left, top, right, t); truelight@0: ViewportDrawChk(vp, left, t, right, bottom); truelight@0: } else { truelight@0: int t = (left + right) >> 1; truelight@0: ViewportDrawChk(vp, left, top, t, bottom); truelight@0: ViewportDrawChk(vp, t, top, right, bottom); truelight@0: } truelight@0: } else { truelight@193: ViewportDoDraw(vp, truelight@0: ((left - vp->left) << vp->zoom) + vp->virtual_left, truelight@0: ((top - vp->top) << vp->zoom) + vp->virtual_top, truelight@0: ((right - vp->left) << vp->zoom) + vp->virtual_left, truelight@0: ((bottom - vp->top) << vp->zoom) + vp->virtual_top truelight@0: ); truelight@0: } truelight@0: } truelight@0: tron@500: static inline void ViewportDraw(ViewPort *vp, int left, int top, int right, int bottom) truelight@0: { tron@2116: if (right <= vp->left || bottom <= vp->top) return; truelight@0: tron@2116: if (left >= vp->left + vp->width) return; truelight@0: truelight@0: if (left < vp->left) left = vp->left; tron@2116: if (right > vp->left + vp->width) right = vp->left + vp->width; truelight@0: tron@2116: if (top >= vp->top + vp->height) return; truelight@0: truelight@0: if (top < vp->top) top = vp->top; tron@2116: if (bottom > vp->top + vp->height) bottom = vp->top + vp->height; truelight@0: truelight@0: ViewportDrawChk(vp, left, top, right, bottom); truelight@0: } truelight@0: tron@2116: void DrawWindowViewport(Window *w) tron@2116: { truelight@0: DrawPixelInfo *dpi = _cur_dpi; truelight@0: truelight@0: dpi->left += w->left; truelight@0: dpi->top += w->top; truelight@0: truelight@0: ViewportDraw(w->viewport, dpi->left, dpi->top, dpi->left + dpi->width, dpi->top + dpi->height); truelight@0: truelight@0: dpi->left -= w->left; truelight@0: dpi->top -= w->top; truelight@0: } truelight@0: truelight@0: void UpdateViewportPosition(Window *w) truelight@0: { tron@2116: const ViewPort *vp = w->viewport; truelight@0: tron@2116: if (WP(w, vp_d).follow_vehicle != INVALID_VEHICLE) { tron@2116: const Vehicle* veh = GetVehicle(WP(w,vp_d).follow_vehicle); tron@2116: Point pt = MapXYZToViewport(vp, veh->x_pos, veh->y_pos, veh->z_pos); truelight@0: truelight@0: SetViewportPosition(w, pt.x, pt.y); truelight@0: } else { tron@632: #if !defined(NEW_ROTATION) tron@632: int x; tron@632: int y; tron@632: int vx; tron@632: int vy; tron@632: tron@632: // Center of the viewport is hot spot tron@632: x = WP(w,vp_d).scrollpos_x + vp->virtual_width / 2; tron@632: y = WP(w,vp_d).scrollpos_y + vp->virtual_height / 2; tron@632: // Convert viewport coordinates to map coordinates tron@632: // Calculation is scaled by 4 to avoid rounding errors tron@632: vx = -x + y * 2; tron@632: vy = x + y * 2; tron@632: // clamp to size of map tron@856: vx = clamp(vx, 0 * 4, MapMaxX() * 16 * 4); tron@856: vy = clamp(vy, 0 * 4, MapMaxY() * 16 * 4); tron@632: // Convert map coordinates to viewport coordinates tron@632: x = (-vx + vy) / 2; tron@632: y = ( vx + vy) / 4; tron@632: // Set position tron@2116: WP(w, vp_d).scrollpos_x = x - vp->virtual_width / 2; tron@2116: WP(w, vp_d).scrollpos_y = y - vp->virtual_height / 2; tron@632: #else truelight@0: int x,y,t; truelight@0: int err; truelight@0: truelight@0: x = WP(w,vp_d).scrollpos_x >> 2; truelight@0: y = WP(w,vp_d).scrollpos_y >> 1; truelight@0: truelight@0: t = x; truelight@0: x = x + y; truelight@0: y = x - y; truelight@0: err= 0; truelight@0: truelight@0: if (err != 0) { truelight@0: /* coordinate remap */ truelight@0: Point pt = RemapCoords(x, y, 0); truelight@193: t = (-1) << vp->zoom; truelight@0: WP(w,vp_d).scrollpos_x = pt.x & t; truelight@0: WP(w,vp_d).scrollpos_y = pt.y & t; truelight@0: } tron@632: #endif tron@632: tron@2116: SetViewportPosition(w, WP(w, vp_d).scrollpos_x, WP(w, vp_d).scrollpos_y); truelight@0: } truelight@0: } truelight@0: tron@2116: static void MarkViewportDirty(const ViewPort *vp, int left, int top, int right, int bottom) truelight@0: { tron@2116: right -= vp->virtual_left; tron@2116: if (right <= 0) return; truelight@0: tron@2116: bottom -= vp->virtual_top; tron@2116: if (bottom <= 0) return; truelight@0: tron@2116: left = max(0, left - vp->virtual_left); truelight@0: tron@2116: if (left >= vp->virtual_width) return; truelight@0: tron@2116: top = max(0, top - vp->virtual_top); tron@2116: tron@2116: if (top >= vp->virtual_height) return; truelight@0: truelight@0: SetDirtyBlocks( truelight@0: (left >> vp->zoom) + vp->left, truelight@0: (top >> vp->zoom) + vp->top, truelight@0: (right >> vp->zoom) + vp->left, truelight@0: (bottom >> vp->zoom) + vp->top truelight@0: ); truelight@0: } truelight@0: truelight@0: void MarkAllViewportsDirty(int left, int top, int right, int bottom) truelight@0: { tron@2116: const ViewPort *vp = _viewports; truelight@0: uint32 act = _active_viewports; truelight@0: do { truelight@0: if (act & 1) { truelight@0: assert(vp->width != 0); truelight@0: MarkViewportDirty(vp, left, top, right, bottom); truelight@0: } truelight@0: } while (vp++,act>>=1); truelight@0: } truelight@0: tron@2116: void MarkTileDirtyByTile(TileIndex tile) tron@2116: { tron@926: Point pt = RemapCoords(TileX(tile) * 16, TileY(tile) * 16, GetTileZ(tile)); truelight@0: MarkAllViewportsDirty( truelight@0: pt.x - 31, truelight@0: pt.y - 122, truelight@0: pt.x - 31 + 67, truelight@0: pt.y - 122 + 154 truelight@0: ); truelight@0: } truelight@0: truelight@0: void MarkTileDirty(int x, int y) truelight@0: { tron@2116: uint z = 0; truelight@0: Point pt; tron@2116: tron@863: if (IS_INT_INSIDE(x, 0, MapSizeX() * 16) && tron@863: IS_INT_INSIDE(y, 0, MapSizeY() * 16)) tron@1980: z = GetTileZ(TileVirtXY(x, y)); truelight@0: pt = RemapCoords(x, y, z); truelight@0: truelight@0: MarkAllViewportsDirty( truelight@0: pt.x - 31, truelight@0: pt.y - 122, truelight@0: pt.x - 31 + 67, truelight@0: pt.y - 122 + 154 truelight@0: ); truelight@193: } truelight@0: tron@1093: static void SetSelectionTilesDirty(void) truelight@0: { truelight@0: int y_size, x_size; tron@1863: int x = _thd.pos.x; tron@1863: int y = _thd.pos.y; truelight@0: tron@1863: x_size = _thd.size.x; tron@1863: y_size = _thd.size.y; truelight@0: tron@1863: if (_thd.outersize.x) { tron@1863: x_size += _thd.outersize.x; tron@1863: x += _thd.offs.x; tron@1863: y_size += _thd.outersize.y; tron@1863: y += _thd.offs.y; truelight@0: } truelight@0: truelight@0: assert(x_size > 0); truelight@0: assert(y_size > 0); truelight@0: truelight@0: x_size += x; truelight@0: y_size += y; truelight@0: truelight@0: do { truelight@0: int y_bk = y; truelight@0: do { truelight@0: MarkTileDirty(x, y); truelight@0: } while ( (y+=16) != y_size); truelight@0: y = y_bk; truelight@0: } while ( (x+=16) != x_size); truelight@0: } truelight@0: truelight@0: tron@1990: void SetSelectionRed(bool b) tron@1990: { tron@1990: _thd.make_square_red = b; tron@1990: SetSelectionTilesDirty(); tron@1990: } tron@1990: tron@1990: tron@2116: static bool CheckClickOnTown(const ViewPort *vp, int x, int y) truelight@0: { tron@2116: const Town *t; truelight@0: tron@2116: if (!(_display_opt & DO_SHOW_TOWN_NAMES)) return false; truelight@0: truelight@0: if (vp->zoom < 1) { truelight@0: x = x - vp->left + vp->virtual_left; truelight@193: y = y - vp->top + vp->virtual_top; truelight@193: truelight@0: FOR_ALL_TOWNS(t) { truelight@0: if (t->xy && truelight@0: y >= t->sign.top && truelight@0: y < t->sign.top + 12 && truelight@0: x >= t->sign.left && truelight@0: x < t->sign.left + t->sign.width_1) { truelight@0: ShowTownViewWindow(t->index); truelight@0: return true; truelight@0: } truelight@0: } truelight@0: } else if (vp->zoom == 1) { truelight@0: x = (x - vp->left + 1) * 2 + vp->virtual_left; truelight@193: y = (y - vp->top + 1) * 2 + vp->virtual_top; truelight@0: FOR_ALL_TOWNS(t) { truelight@0: if (t->xy && truelight@0: y >= t->sign.top && truelight@0: y < t->sign.top + 24 && truelight@0: x >= t->sign.left && truelight@0: x < t->sign.left + t->sign.width_1 * 2) { truelight@0: ShowTownViewWindow(t->index); truelight@0: return true; truelight@0: } truelight@0: } truelight@0: } else { truelight@0: x = (x - vp->left + 3) * 4 + vp->virtual_left; truelight@193: y = (y - vp->top + 3) * 4 + vp->virtual_top; truelight@0: FOR_ALL_TOWNS(t) { truelight@0: if (t->xy && truelight@0: y >= t->sign.top && truelight@0: y < t->sign.top + 24 && truelight@0: x >= t->sign.left && truelight@0: x < t->sign.left + t->sign.width_2 * 4) { truelight@0: ShowTownViewWindow(t->index); truelight@0: return true; truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: return false; truelight@0: } truelight@0: tron@2116: static bool CheckClickOnStation(const ViewPort *vp, int x, int y) truelight@0: { tron@2116: const Station *st; truelight@0: tron@2116: if (!(_display_opt & DO_SHOW_STATION_NAMES)) return false; truelight@0: truelight@0: if (vp->zoom < 1) { truelight@0: x = x - vp->left + vp->virtual_left; truelight@193: y = y - vp->top + vp->virtual_top; truelight@0: truelight@0: FOR_ALL_STATIONS(st) { truelight@0: if (st->xy && truelight@0: y >= st->sign.top && truelight@0: y < st->sign.top + 12 && truelight@0: x >= st->sign.left && truelight@0: x < st->sign.left + st->sign.width_1) { truelight@0: ShowStationViewWindow(st->index); truelight@0: return true; truelight@0: } truelight@0: } truelight@0: } else if (vp->zoom == 1) { truelight@0: x = (x - vp->left + 1) * 2 + vp->virtual_left; truelight@193: y = (y - vp->top + 1) * 2 + vp->virtual_top; truelight@0: FOR_ALL_STATIONS(st) { truelight@0: if (st->xy && truelight@0: y >= st->sign.top && truelight@0: y < st->sign.top + 24 && truelight@0: x >= st->sign.left && truelight@0: x < st->sign.left + st->sign.width_1 * 2) { truelight@0: ShowStationViewWindow(st->index); truelight@0: return true; truelight@0: } truelight@0: } truelight@0: } else { truelight@0: x = (x - vp->left + 3) * 4 + vp->virtual_left; truelight@193: y = (y - vp->top + 3) * 4 + vp->virtual_top; truelight@0: FOR_ALL_STATIONS(st) { truelight@0: if (st->xy && truelight@0: y >= st->sign.top && truelight@0: y < st->sign.top + 24 && truelight@0: x >= st->sign.left && truelight@0: x < st->sign.left + st->sign.width_2 * 4) { truelight@0: ShowStationViewWindow(st->index); truelight@0: return true; truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: return false; truelight@0: } truelight@0: tron@2116: static bool CheckClickOnSign(const ViewPort *vp, int x, int y) truelight@0: { tron@2116: const SignStruct *ss; truelight@0: tron@2116: if (!(_display_opt & DO_SHOW_SIGNS)) return false; truelight@0: truelight@0: if (vp->zoom < 1) { truelight@0: x = x - vp->left + vp->virtual_left; truelight@193: y = y - vp->top + vp->virtual_top; truelight@0: truelight@988: FOR_ALL_SIGNS(ss) { truelight@0: if (ss->str && truelight@0: y >= ss->sign.top && truelight@0: y < ss->sign.top + 12 && truelight@0: x >= ss->sign.left && truelight@0: x < ss->sign.left + ss->sign.width_1) { truelight@0: ShowRenameSignWindow(ss); truelight@0: return true; truelight@0: } truelight@0: } truelight@0: } else if (vp->zoom == 1) { truelight@0: x = (x - vp->left + 1) * 2 + vp->virtual_left; truelight@193: y = (y - vp->top + 1) * 2 + vp->virtual_top; truelight@988: FOR_ALL_SIGNS(ss) { truelight@0: if (ss->str && truelight@0: y >= ss->sign.top && truelight@0: y < ss->sign.top + 24 && truelight@0: x >= ss->sign.left && truelight@0: x < ss->sign.left + ss->sign.width_1 * 2) { truelight@0: ShowRenameSignWindow(ss); truelight@0: return true; truelight@0: } truelight@0: } truelight@0: } else { truelight@0: x = (x - vp->left + 3) * 4 + vp->virtual_left; truelight@193: y = (y - vp->top + 3) * 4 + vp->virtual_top; truelight@988: FOR_ALL_SIGNS(ss) { truelight@0: if (ss->str && truelight@0: y >= ss->sign.top && truelight@0: y < ss->sign.top + 24 && truelight@0: x >= ss->sign.left && truelight@0: x < ss->sign.left + ss->sign.width_2 * 4) { truelight@0: ShowRenameSignWindow(ss); truelight@0: return true; truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: return false; truelight@0: } truelight@0: tron@2116: static bool CheckClickOnWaypoint(const ViewPort *vp, int x, int y) truelight@0: { tron@2116: const Waypoint *wp; truelight@0: tron@2116: if (!(_display_opt & DO_WAYPOINTS)) return false; truelight@0: truelight@0: if (vp->zoom < 1) { truelight@0: x = x - vp->left + vp->virtual_left; truelight@193: y = y - vp->top + vp->virtual_top; truelight@0: truelight@1542: FOR_ALL_WAYPOINTS(wp) { truelight@1542: if (wp->xy && truelight@1542: y >= wp->sign.top && truelight@1542: y < wp->sign.top + 12 && truelight@1542: x >= wp->sign.left && truelight@1542: x < wp->sign.left + wp->sign.width_1) { truelight@1542: ShowRenameWaypointWindow(wp); truelight@0: return true; truelight@0: } truelight@0: } truelight@0: } else if (vp->zoom == 1) { truelight@0: x = (x - vp->left + 1) * 2 + vp->virtual_left; truelight@193: y = (y - vp->top + 1) * 2 + vp->virtual_top; truelight@1542: FOR_ALL_WAYPOINTS(wp) { truelight@1542: if (wp->xy && truelight@1542: y >= wp->sign.top && truelight@1542: y < wp->sign.top + 24 && truelight@1542: x >= wp->sign.left && truelight@1542: x < wp->sign.left + wp->sign.width_1 * 2) { truelight@1542: ShowRenameWaypointWindow(wp); truelight@0: return true; truelight@0: } truelight@0: } truelight@0: } else { truelight@0: x = (x - vp->left + 3) * 4 + vp->virtual_left; truelight@193: y = (y - vp->top + 3) * 4 + vp->virtual_top; truelight@1542: FOR_ALL_WAYPOINTS(wp) { truelight@1542: if (wp->xy && truelight@1542: y >= wp->sign.top && truelight@1542: y < wp->sign.top + 24 && truelight@1542: x >= wp->sign.left && truelight@1542: x < wp->sign.left + wp->sign.width_2 * 4) { truelight@1542: ShowRenameWaypointWindow(wp); truelight@0: return true; truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: return false; truelight@0: } truelight@0: truelight@0: tron@2116: static void CheckClickOnLandscape(const ViewPort *vp, int x, int y) truelight@0: { tron@2116: Point pt = TranslateXYToTileCoord(vp, x, y); tron@1977: tron@1980: if (pt.x != -1) ClickTile(TileVirtXY(pt.x, pt.y)); truelight@0: } truelight@0: tron@2662: tron@2662: static void SafeShowTrainViewWindow(const Vehicle* v) tron@2662: { bjarni@2676: if (!IsFrontEngine(v)) v = GetFirstVehicleInChain(v); tron@2662: ShowTrainViewWindow(v); tron@2662: } tron@2662: tron@2662: static void Nop(const Vehicle* v) {} tron@2662: tron@2662: typedef void OnVehicleClickProc(const Vehicle* v); tron@2662: static OnVehicleClickProc* const _on_vehicle_click_proc[] = { tron@2662: SafeShowTrainViewWindow, tron@2662: ShowRoadVehViewWindow, tron@2662: ShowShipViewWindow, tron@2662: ShowAircraftViewWindow, tron@2662: Nop, // Special vehicles tron@2662: Nop // Disaster vehicles truelight@0: }; truelight@0: tron@2116: void HandleViewportClicked(const ViewPort *vp, int x, int y) truelight@0: { tron@2662: const Vehicle* v; truelight@0: tron@2116: if (CheckClickOnTown(vp, x, y)) return; tron@2116: if (CheckClickOnStation(vp, x, y)) return; tron@2116: if (CheckClickOnSign(vp, x, y)) return; tron@2116: if (CheckClickOnWaypoint(vp, x, y)) return; truelight@0: CheckClickOnLandscape(vp, x, y); truelight@0: tron@2116: v = CheckClickOnVehicle(vp, x, y); tron@2116: if (v != NULL) _on_vehicle_click_proc[v->type - 0x10](v); truelight@0: } truelight@0: tron@1093: Vehicle *CheckMouseOverVehicle(void) truelight@0: { tron@2116: const Window* w; tron@2116: const ViewPort* vp; truelight@0: truelight@0: int x = _cursor.pos.x; truelight@0: int y = _cursor.pos.y; truelight@0: truelight@0: w = FindWindowFromPt(x, y); tron@2116: if (w == NULL) return NULL; truelight@0: truelight@0: vp = IsPtInWindowViewport(w, x, y); tron@2116: return (vp != NULL) ? CheckClickOnVehicle(vp, x, y) : NULL; truelight@0: } truelight@0: truelight@0: truelight@0: tron@1093: void PlaceObject(void) truelight@0: { truelight@0: Point pt; truelight@0: Window *w; truelight@193: truelight@0: pt = GetTileBelowCursor(); tron@2116: if (pt.x == -1) return; truelight@0: dominik@1070: if (_thd.place_mode == VHM_POINT) { truelight@0: pt.x += 8; truelight@0: pt.y += 8; truelight@0: } truelight@0: truelight@0: _tile_fract_coords.x = pt.x & 0xF; truelight@0: _tile_fract_coords.y = pt.y & 0xF; truelight@0: tron@2116: w = GetCallbackWnd(); tron@2116: if (w != NULL) { tron@2116: WindowEvent e; tron@2116: truelight@0: e.event = WE_PLACE_OBJ; truelight@0: e.place.pt = pt; tron@1980: e.place.tile = TileVirtXY(pt.x, pt.y); truelight@0: w->wndproc(w, &e); truelight@0: } truelight@0: } truelight@0: darkvater@152: darkvater@152: /* scrolls the viewport in a window to a given location */ tron@2116: bool ScrollWindowTo(int x , int y, Window* w) darkvater@152: { darkvater@152: Point pt; darkvater@152: darkvater@152: pt = MapXYZToViewport(w->viewport, x, y, GetSlopeZ(x, y)); tron@2116: WP(w, vp_d).follow_vehicle = INVALID_VEHICLE; darkvater@152: tron@2116: if (WP(w, vp_d).scrollpos_x == pt.x && WP(w, vp_d).scrollpos_y == pt.y) tron@2116: return false; darkvater@152: tron@2116: WP(w, vp_d).scrollpos_x = pt.x; tron@2116: WP(w, vp_d).scrollpos_y = pt.y; darkvater@152: return true; darkvater@152: } darkvater@152: darkvater@152: truelight@0: bool ScrollMainWindowTo(int x, int y) truelight@0: { tron@2116: return ScrollWindowTo(x, y, FindWindowById(WC_MAIN_WINDOW, 0)); truelight@0: } truelight@0: truelight@0: truelight@0: bool ScrollMainWindowToTile(TileIndex tile) truelight@0: { tron@926: return ScrollMainWindowTo(TileX(tile) * 16 + 8, TileY(tile) * 16 + 8); truelight@0: } truelight@0: truelight@0: void SetRedErrorSquare(TileIndex tile) truelight@0: { truelight@0: TileIndex old; truelight@0: truelight@0: old = _thd.redsq; truelight@0: _thd.redsq = tile; truelight@0: truelight@0: if (tile != old) { truelight@0: if (tile != 0) MarkTileDirtyByTile(tile); truelight@0: if (old != 0) MarkTileDirtyByTile(old); truelight@0: } truelight@0: } truelight@0: truelight@0: void SetTileSelectSize(int w, int h) truelight@0: { tron@1863: _thd.new_size.x = w * 16; tron@1863: _thd.new_size.y = h * 16; tron@1863: _thd.new_outersize.x = 0; tron@1863: _thd.new_outersize.y = 0; truelight@0: } truelight@0: tron@2116: void SetTileSelectBigSize(int ox, int oy, int sx, int sy) tron@2116: { tron@1863: _thd.offs.x = ox * 16; tron@1863: _thd.offs.y = oy * 16; tron@1863: _thd.new_outersize.x = sx * 16; tron@1863: _thd.new_outersize.y = sy * 16; truelight@0: } truelight@0: dominik@1070: /* returns the best autorail highlight type from map coordinates */ dominik@1070: static byte GetAutorailHT(int x, int y) dominik@1070: { Darkvater@2710: return HT_RAIL | _AutorailPiece[x & 0xF][y & 0xF]; dominik@1070: } truelight@0: dominik@1070: // called regular to update tile highlighting in all cases tron@1093: void UpdateTileSelection(void) truelight@0: { tron@2116: int x1; tron@2116: int y1; truelight@0: tron@1863: _thd.new_drawstyle = 0; truelight@0: tron@1863: if (_thd.place_mode == VHM_SPECIAL) { tron@1863: x1 = _thd.selend.x; tron@1863: y1 = _thd.selend.y; truelight@0: if (x1 != -1) { tron@1863: int x2 = _thd.selstart.x; tron@1863: int y2 = _thd.selstart.y; truelight@0: x1 &= ~0xF; truelight@0: y1 &= ~0xF; truelight@193: truelight@0: if (x1 >= x2) intswap(x1,x2); truelight@0: if (y1 >= y2) intswap(y1,y2); tron@1863: _thd.new_pos.x = x1; tron@1863: _thd.new_pos.y = y1; tron@1863: _thd.new_size.x = x2 - x1 + 16; tron@1863: _thd.new_size.y = y2 - y1 + 16; tron@1863: _thd.new_drawstyle = _thd.next_drawstyle; truelight@0: } tron@1863: } else if (_thd.place_mode != VHM_NONE) { tron@2116: Point pt = GetTileBelowCursor(); truelight@0: x1 = pt.x; truelight@0: y1 = pt.y; truelight@0: if (x1 != -1) { tron@1863: switch (_thd.place_mode) { dominik@1070: case VHM_RECT: tron@1863: _thd.new_drawstyle = HT_RECT; dominik@1070: break; dominik@1070: case VHM_POINT: tron@1863: _thd.new_drawstyle = HT_POINT; dominik@1070: x1 += 8; dominik@1070: y1 += 8; dominik@1070: break; dominik@1070: case VHM_RAIL: tron@1863: _thd.new_drawstyle = GetAutorailHT(pt.x, pt.y); // draw one highlighted tile truelight@0: } tron@1863: _thd.new_pos.x = x1 & ~0xF; tron@1863: _thd.new_pos.y = y1 & ~0xF; truelight@0: } truelight@0: } truelight@0: dominik@1070: // redraw selection tron@1863: if (_thd.drawstyle != _thd.new_drawstyle || tron@1863: _thd.pos.x != _thd.new_pos.x || _thd.pos.y != _thd.new_pos.y || tron@1863: _thd.size.x != _thd.new_size.x || _thd.size.y != _thd.new_size.y) { truelight@0: // clear the old selection? tron@1863: if (_thd.drawstyle) SetSelectionTilesDirty(); truelight@0: tron@1863: _thd.drawstyle = _thd.new_drawstyle; tron@1863: _thd.pos = _thd.new_pos; tron@1863: _thd.size = _thd.new_size; tron@1863: _thd.outersize = _thd.new_outersize; tron@1863: _thd.dirty = 0xff; truelight@0: truelight@0: // draw the new selection? tron@1863: if (_thd.new_drawstyle) SetSelectionTilesDirty(); truelight@0: } truelight@0: } truelight@0: dominik@1070: // highlighting tiles while only going over them with the mouse tron@1977: void VpStartPlaceSizing(TileIndex tile, int user) truelight@0: { tron@1863: _thd.userdata = user; tron@1863: _thd.selend.x = TileX(tile) * 16; tron@1863: _thd.selstart.x = TileX(tile) * 16; tron@1863: _thd.selend.y = TileY(tile) * 16; tron@1863: _thd.selstart.y = TileY(tile) * 16; tron@1863: if (_thd.place_mode == VHM_RECT) { tron@1863: _thd.place_mode = VHM_SPECIAL; tron@1863: _thd.next_drawstyle = HT_RECT; tron@1863: } else if (_thd.place_mode == VHM_RAIL) { // autorail one piece tron@1863: _thd.place_mode = VHM_SPECIAL; tron@1863: _thd.next_drawstyle = _thd.drawstyle; truelight@0: } else { tron@1863: _thd.place_mode = VHM_SPECIAL; tron@1863: _thd.next_drawstyle = HT_POINT; truelight@0: } truelight@0: _special_mouse_mode = WSM_SIZING; truelight@0: } truelight@0: truelight@0: void VpSetPlaceSizingLimit(int limit) truelight@0: { truelight@0: _thd.sizelimit = limit; truelight@0: } truelight@0: truelight@0: void VpSetPresizeRange(uint from, uint to) truelight@0: { tron@1863: _thd.selend.x = TileX(to) * 16; tron@1863: _thd.selend.y = TileY(to) * 16; tron@1863: _thd.selstart.x = TileX(from) * 16; tron@1863: _thd.selstart.y = TileY(from) * 16; tron@1863: _thd.next_drawstyle = HT_RECT; truelight@0: } truelight@0: tron@2817: static void VpStartPreSizing(void) truelight@0: { truelight@0: _thd.selend.x = -1; truelight@0: _special_mouse_mode = WSM_PRESIZE; truelight@0: } truelight@0: tron@1109: /* returns information about the 2x1 piece to be build. dominik@1070: * The lower bits (0-3) are the track type. */ dominik@1070: static byte Check2x1AutoRail(int mode) dominik@1070: { dominik@1070: int fxpy = _tile_fract_coords.x + _tile_fract_coords.y; tron@1863: int sxpy = (_thd.selend.x & 0xF) + (_thd.selend.y & 0xF); dominik@1070: int fxmy = _tile_fract_coords.x - _tile_fract_coords.y; tron@1863: int sxmy = (_thd.selend.x & 0xF) - (_thd.selend.y & 0xF); dominik@1070: dominik@1070: switch(mode) { dominik@1070: case 0: // end piece is lower right dominik@1070: if (fxpy >= 20 && sxpy <= 12) { /*SwapSelection(); DoRailroadTrack(0); */return 3; } dominik@1070: if (fxmy < -3 && sxmy > 3) {/* DoRailroadTrack(0); */return 5; } dominik@1070: return 1; dominik@1070: dominik@1070: case 1: dominik@1070: if (fxmy > 3 && sxmy < -3) { /*SwapSelection(); DoRailroadTrack(0); */return 4; } dominik@1070: if (fxpy <= 12 && sxpy >= 20) { /*DoRailroadTrack(0); */return 2; } dominik@1070: return 1; dominik@1070: dominik@1070: case 2: dominik@1070: if (fxmy > 3 && sxmy < -3) { /*DoRailroadTrack(3);*/ return 4; } dominik@1070: if (fxpy >= 20 && sxpy <= 12) { /*SwapSelection(); DoRailroadTrack(0); */return 3; } dominik@1070: return 0; dominik@1070: dominik@1070: case 3: dominik@1070: if (fxmy < -3 && sxmy > 3) { /*SwapSelection(); DoRailroadTrack(3);*/ return 5; } dominik@1070: if (fxpy <= 12 && sxpy >= 20) { /*DoRailroadTrack(0); */return 2; } dominik@1070: return 0; dominik@1070: } dominik@1070: dominik@1070: return 0; // avoids compiler warnings dominik@1070: } dominik@1070: dominik@1070: dominik@1070: // while dragging dominik@1070: static void CalcRaildirsDrawstyle(TileHighlightData *thd, int x, int y, int method) truelight@0: { truelight@0: int d; dominik@1070: byte b=6; truelight@0: uint w,h; truelight@193: dominik@1070: int dx = thd->selstart.x - (thd->selend.x&~0xF); dominik@1070: int dy = thd->selstart.y - (thd->selend.y&~0xF); dominik@1070: w = myabs(dx) + 16; dominik@1070: h = myabs(dy) + 16; truelight@0: tron@1980: if (TileVirtXY(thd->selstart.x, thd->selstart.y) == TileVirtXY(x, y)) { // check if we're only within one tile tron@2116: if (method == VPM_RAILDIRS) tron@2116: b = GetAutorailHT(x, y); tron@2116: else // rect for autosignals on one tile tron@2116: b = HT_RECT; dominik@1070: } else if (h == 16) { // Is this in X direction? tron@2116: if (dx == 16) // 2x1 special handling dominik@1070: b = (Check2x1AutoRail(3)) | HT_LINE; tron@2116: else if (dx == -16) dominik@1070: b = (Check2x1AutoRail(2)) | HT_LINE; tron@1109: else dominik@1070: b = HT_LINE | HT_DIR_X; dominik@1070: y = thd->selstart.y; dominik@1070: } else if (w == 16) { // Or Y direction? tron@2116: if (dy == 16) // 2x1 special handling dominik@1070: b = (Check2x1AutoRail(1)) | HT_LINE; tron@2116: else if (dy == -16) // 2x1 other direction dominik@1070: b = (Check2x1AutoRail(0)) | HT_LINE; tron@1109: else dominik@1070: b = HT_LINE | HT_DIR_Y; truelight@0: x = thd->selstart.x; dominik@1070: } else if (w > h * 2) { // still count as x dir? tron@2116: b = HT_LINE | HT_DIR_X; truelight@0: y = thd->selstart.y; dominik@1070: } else if (h > w * 2) { // still count as y dir? tron@2116: b = HT_LINE | HT_DIR_Y; dominik@1070: x = thd->selstart.x; dominik@1070: } else { // complicated direction truelight@0: d = w - h; tron@2116: thd->selend.x = thd->selend.x & ~0xF; tron@2116: thd->selend.y = thd->selend.y & ~0xF; truelight@0: truelight@0: // four cases. truelight@0: if (x > thd->selstart.x) { truelight@0: if (y > thd->selstart.y) { truelight@0: // south tron@2116: if (d == 0) { tron@2116: b = (x & 0xF) > (y & 0xF) ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR; tron@2116: } else if (d >= 0) { tron@2116: x = thd->selstart.x + h; tron@2116: b = HT_LINE | HT_DIR_VL; tron@2116: // return px == py || px == py + 16; tron@2116: } else { tron@2116: y = thd->selstart.y + w; tron@2116: b = HT_LINE | HT_DIR_VR; tron@2116: } // return px == py || px == py - 16; truelight@0: } else { truelight@0: // west tron@2116: if (d == 0) { tron@2116: b = (x & 0xF) + (y & 0xF) >= 0x10 ? HT_LINE | HT_DIR_HL : HT_LINE | HT_DIR_HU; tron@2116: } else if (d >= 0) { tron@2116: x = thd->selstart.x + h; tron@2116: b = HT_LINE | HT_DIR_HL; tron@2116: } else { tron@2116: y = thd->selstart.y - w; tron@2116: b = HT_LINE | HT_DIR_HU; tron@2116: } truelight@0: } truelight@0: } else { truelight@0: if (y > thd->selstart.y) { truelight@0: // east tron@2116: if (d == 0) { tron@2116: b = (x & 0xF) + (y & 0xF) >= 0x10 ? HT_LINE | HT_DIR_HL : HT_LINE | HT_DIR_HU; tron@2116: } else if (d >= 0) { tron@2116: x = thd->selstart.x - h; tron@2116: b = HT_LINE | HT_DIR_HU; tron@2116: // return px == -py || px == -py - 16; tron@2116: } else { tron@2116: y = thd->selstart.y + w; tron@2116: b = HT_LINE | HT_DIR_HL; tron@2116: } // return px == -py || px == -py + 16; truelight@0: } else { truelight@0: // north tron@2116: if (d == 0) { tron@2116: b = (x & 0xF) > (y & 0xF) ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR; tron@2116: } else if (d >= 0) { tron@2116: x = thd->selstart.x - h; tron@2116: b = HT_LINE | HT_DIR_VR; tron@2116: // return px == py || px == py - 16; tron@2116: } else { tron@2116: y = thd->selstart.y - w; tron@2116: b = HT_LINE | HT_DIR_VL; tron@2116: } //return px == py || px == py + 16; truelight@0: } truelight@0: } truelight@0: } truelight@0: thd->selend.x = x; truelight@0: thd->selend.y = y; truelight@0: thd->next_drawstyle = b; truelight@0: } truelight@0: dominik@1070: // while dragging truelight@0: void VpSelectTilesWithMethod(int x, int y, int method) truelight@0: { tron@2116: int sx; tron@2116: int sy; tron@1863: truelight@0: if (x == -1) { tron@1863: _thd.selend.x = -1; truelight@0: return; truelight@0: } truelight@0: truelight@0: // allow drag in any rail direction darkvater@58: if (method == VPM_RAILDIRS || method == VPM_SIGNALDIRS) { tron@1863: _thd.selend.x = x; tron@1863: _thd.selend.y = y; tron@1863: CalcRaildirsDrawstyle(&_thd, x, y, method); truelight@0: return; truelight@0: } truelight@0: tron@2116: if (_thd.next_drawstyle == HT_POINT) { tron@2116: x += 8; tron@2116: y += 8; tron@2116: } truelight@0: tron@1863: sx = _thd.selstart.x; tron@1863: sy = _thd.selstart.y; truelight@0: tron@2116: switch (method) { tron@2116: case VPM_FIX_X: tron@2116: x = sx; tron@2116: break; truelight@0: tron@2116: case VPM_FIX_Y: tron@2116: y = sy; tron@2116: break; truelight@0: tron@2116: case VPM_X_OR_Y: tron@2116: if (myabs(sy - y) < myabs(sx - x)) y = sy; else x = sx; tron@2116: break; tron@2116: tron@2116: case VPM_X_AND_Y: tron@2116: break; tron@2116: tron@2116: // limit the selected area to a 10x10 rect. tron@2116: case VPM_X_AND_Y_LIMITED: { tron@2116: int limit = (_thd.sizelimit - 1) * 16; tron@2116: x = sx + clamp(x - sx, -limit, limit); tron@2116: y = sy + clamp(y - sy, -limit, limit); tron@2116: break; tron@2116: } truelight@0: } truelight@0: tron@1863: _thd.selend.x = x; tron@1863: _thd.selend.y = y; truelight@0: } truelight@0: tron@1109: // while dragging tron@1093: bool VpHandlePlaceSizingDrag(void) truelight@0: { truelight@0: Window *w; truelight@0: WindowEvent e; truelight@193: tron@2116: if (_special_mouse_mode != WSM_SIZING) return true; truelight@0: truelight@0: e.place.userdata = _thd.userdata; truelight@0: dominik@1070: // stop drag mode if the window has been closed truelight@0: w = FindWindowById(_thd.window_class,_thd.window_number); truelight@0: if (w == NULL) { truelight@0: ResetObjectToPlace(); truelight@0: return false; truelight@0: } truelight@0: dominik@1070: // while dragging execute the drag procedure of the corresponding window (mostly VpSelectTilesWithMethod() ) truelight@0: if (_left_button_down) { truelight@0: e.event = WE_PLACE_DRAG; truelight@0: e.place.pt = GetTileBelowCursor(); truelight@0: w->wndproc(w, &e); truelight@0: return false; truelight@0: } truelight@0: truelight@0: // mouse button released.. truelight@0: // keep the selected tool, but reset it to the original mode. truelight@193: _special_mouse_mode = WSM_NONE; tron@2116: if (_thd.next_drawstyle == HT_RECT) { dominik@1070: _thd.place_mode = VHM_RECT; tron@2116: } else if ((e.place.userdata & 0xF) == VPM_SIGNALDIRS) { // some might call this a hack... -- Dominik tron@2116: _thd.place_mode = VHM_RECT; tron@2116: } else if (_thd.next_drawstyle & HT_LINE) { dominik@1070: _thd.place_mode = VHM_RAIL; tron@2116: } else if (_thd.next_drawstyle & HT_RAIL) { dominik@1070: _thd.place_mode = VHM_RAIL; tron@2116: } else { dominik@1070: _thd.place_mode = VHM_POINT; tron@2116: } truelight@0: SetTileSelectSize(1, 1); truelight@0: truelight@0: // and call the mouseup event. truelight@0: e.event = WE_PLACE_MOUSEUP; truelight@0: e.place.pt = _thd.selend; tron@1980: e.place.tile = TileVirtXY(e.place.pt.x, e.place.pt.y); tron@1980: e.place.starttile = TileVirtXY(_thd.selstart.x, _thd.selstart.y); truelight@0: w->wndproc(w, &e); truelight@193: truelight@0: return false; truelight@0: } truelight@0: Darkvater@1914: void SetObjectToPlaceWnd(CursorID icon, byte mode, Window *w) truelight@0: { Darkvater@1914: SetObjectToPlace(icon, mode, w->window_class, w->window_number); truelight@0: } truelight@0: truelight@0: #include "table/animcursors.h" truelight@0: Darkvater@1914: void SetObjectToPlace(CursorID icon, byte mode, WindowClass window_class, WindowNumber window_num) truelight@0: { truelight@0: Window *w; truelight@0: dominik@1070: // undo clicking on button tron@1863: if (_thd.place_mode != 0) { tron@1863: _thd.place_mode = 0; tron@1863: w = FindWindowById(_thd.window_class, _thd.window_number); tron@2116: if (w != NULL) CallWindowEventNP(w, WE_ABORT_PLACE_OBJ); truelight@0: } truelight@193: truelight@0: SetTileSelectSize(1, 1); truelight@193: tron@1863: _thd.make_square_red = false; truelight@0: dominik@1070: if (mode == VHM_DRAG) { // mode 4 is for dragdropping trains in the depot window truelight@0: mode = 0; truelight@0: _special_mouse_mode = WSM_DRAGDROP; truelight@0: } else { truelight@0: _special_mouse_mode = WSM_NONE; truelight@0: } truelight@0: tron@1863: _thd.place_mode = mode; tron@1863: _thd.window_class = window_class; tron@1863: _thd.window_number = window_num; truelight@0: dominik@1070: if (mode == VHM_SPECIAL) // special tools, like tunnels or docks start with presizing mode truelight@0: VpStartPreSizing(); truelight@193: truelight@0: if ( (int)icon < 0) tron@2484: SetAnimatedMouseCursor(_animcursors[~icon]); truelight@0: else truelight@0: SetMouseCursor(icon); truelight@0: } truelight@0: tron@1093: void ResetObjectToPlace(void) tron@1093: { Darkvater@1914: SetObjectToPlace(SPR_CURSOR_MOUSE, 0, 0, 0); truelight@0: }