2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* @file viewport.cpp Handling of all viewports.
|
2008-04-17 09:42:44 +00:00
|
|
|
*
|
|
|
|
* \verbatim
|
|
|
|
* The in-game coordinate system looks like this *
|
|
|
|
* *
|
|
|
|
* ^ Z *
|
|
|
|
* | *
|
|
|
|
* | *
|
|
|
|
* | *
|
|
|
|
* | *
|
|
|
|
* / \ *
|
|
|
|
* / \ *
|
|
|
|
* / \ *
|
|
|
|
* / \ *
|
|
|
|
* X < > Y *
|
|
|
|
* \endverbatim
|
|
|
|
*/
|
2007-02-23 01:48:53 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "stdafx.h"
|
2007-04-12 13:07:15 +00:00
|
|
|
#include "landscape.h"
|
2008-01-09 09:45:45 +00:00
|
|
|
#include "viewport_func.h"
|
2008-03-31 00:06:17 +00:00
|
|
|
#include "station_base.h"
|
2009-07-22 10:18:19 +00:00
|
|
|
#include "waypoint_base.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "town.h"
|
2008-03-31 07:25:49 +00:00
|
|
|
#include "signs_base.h"
|
|
|
|
#include "signs_func.h"
|
2009-01-31 20:16:06 +00:00
|
|
|
#include "vehicle_base.h"
|
2007-08-29 20:50:58 +00:00
|
|
|
#include "vehicle_gui.h"
|
2007-06-17 20:30:28 +00:00
|
|
|
#include "blitter/factory.hpp"
|
2007-12-21 19:49:27 +00:00
|
|
|
#include "strings_func.h"
|
2007-12-23 10:56:02 +00:00
|
|
|
#include "zoom_func.h"
|
2007-12-27 13:35:39 +00:00
|
|
|
#include "vehicle_func.h"
|
2008-09-30 20:51:04 +00:00
|
|
|
#include "company_func.h"
|
2009-07-22 10:18:19 +00:00
|
|
|
#include "waypoint_func.h"
|
2008-05-06 22:17:12 +00:00
|
|
|
#include "window_func.h"
|
2008-05-07 13:10:15 +00:00
|
|
|
#include "tilehighlight_func.h"
|
2008-05-17 13:01:30 +00:00
|
|
|
#include "window_gui.h"
|
2008-04-16 14:15:00 +00:00
|
|
|
|
2008-01-13 01:21:35 +00:00
|
|
|
#include "table/strings.h"
|
|
|
|
|
2008-01-09 09:45:45 +00:00
|
|
|
PlaceProc *_place_proc;
|
|
|
|
Point _tile_fract_coords;
|
2007-05-15 14:08:39 +00:00
|
|
|
|
2007-03-07 12:11:48 +00:00
|
|
|
struct StringSpriteToDraw {
|
2009-02-09 02:57:15 +00:00
|
|
|
StringID string;
|
2009-11-29 19:27:53 +00:00
|
|
|
Colours colour;
|
2005-01-03 08:50:44 +00:00
|
|
|
int32 x;
|
|
|
|
int32 y;
|
2007-06-21 19:08:47 +00:00
|
|
|
uint64 params[2];
|
2004-08-09 17:04:08 +00:00
|
|
|
uint16 width;
|
2007-03-07 12:11:48 +00:00
|
|
|
};
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-07 12:11:48 +00:00
|
|
|
struct TileSpriteToDraw {
|
2007-01-14 19:57:49 +00:00
|
|
|
SpriteID image;
|
2010-01-21 01:38:13 +00:00
|
|
|
PaletteID pal;
|
2007-10-05 21:49:15 +00:00
|
|
|
const SubSprite *sub; ///< only draw a rectangular part of the sprite
|
2009-06-07 13:25:21 +00:00
|
|
|
int32 x; ///< screen X coordinate of sprite
|
|
|
|
int32 y; ///< screen Y coordinate of sprite
|
2007-03-07 12:11:48 +00:00
|
|
|
};
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-07 12:11:48 +00:00
|
|
|
struct ChildScreenSpriteToDraw {
|
2007-01-14 19:57:49 +00:00
|
|
|
SpriteID image;
|
2010-01-21 01:38:13 +00:00
|
|
|
PaletteID pal;
|
2007-10-05 21:49:15 +00:00
|
|
|
const SubSprite *sub; ///< only draw a rectangular part of the sprite
|
2005-01-03 08:50:44 +00:00
|
|
|
int32 x;
|
|
|
|
int32 y;
|
2008-06-16 20:08:30 +00:00
|
|
|
int next; ///< next child to draw (-1 at the end)
|
2007-03-07 12:11:48 +00:00
|
|
|
};
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-10-13 03:26:48 +00:00
|
|
|
/** Parent sprite that should be drawn */
|
2007-03-07 12:11:48 +00:00
|
|
|
struct ParentSpriteToDraw {
|
2007-09-13 15:51:50 +00:00
|
|
|
SpriteID image; ///< sprite to draw
|
2010-01-21 01:38:13 +00:00
|
|
|
PaletteID pal; ///< palette to use
|
2007-10-05 21:49:15 +00:00
|
|
|
const SubSprite *sub; ///< only draw a rectangular part of the sprite
|
2007-09-13 15:51:50 +00:00
|
|
|
|
2007-09-14 21:54:57 +00:00
|
|
|
int32 x; ///< screen X coordinate of sprite
|
|
|
|
int32 y; ///< screen Y coordinate of sprite
|
|
|
|
|
2007-09-13 15:51:50 +00:00
|
|
|
int32 left; ///< minimal screen X coordinate of sprite (= x + sprite->x_offs), reference point for child sprites
|
|
|
|
int32 top; ///< minimal screen Y coordinate of sprite (= y + sprite->y_offs), reference point for child sprites
|
|
|
|
|
|
|
|
int32 xmin; ///< minimal world X coordinate of bounding box
|
|
|
|
int32 xmax; ///< maximal world X coordinate of bounding box
|
|
|
|
int32 ymin; ///< minimal world Y coordinate of bounding box
|
|
|
|
int32 ymax; ///< maximal world Y coordinate of bounding box
|
2007-09-14 21:54:57 +00:00
|
|
|
int zmin; ///< minimal world Z coordinate of bounding box
|
|
|
|
int zmax; ///< maximal world Z coordinate of bounding box
|
2007-09-13 15:51:50 +00:00
|
|
|
|
2008-04-16 20:39:35 +00:00
|
|
|
int first_child; ///< the first child to draw.
|
2007-09-14 21:54:57 +00:00
|
|
|
bool comparison_done; ///< Used during sprite sorting: true if sprite has been compared with all other sprites
|
2007-03-07 12:11:48 +00:00
|
|
|
};
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-10-13 03:26:48 +00:00
|
|
|
/** Enumeration of multi-part foundations */
|
2007-10-20 20:25:43 +00:00
|
|
|
enum FoundationPart {
|
|
|
|
FOUNDATION_PART_NONE = 0xFF, ///< Neither foundation nor groundsprite drawn yet.
|
|
|
|
FOUNDATION_PART_NORMAL = 0, ///< First part (normal foundation or no foundation)
|
|
|
|
FOUNDATION_PART_HALFTILE = 1, ///< Second part (halftile foundation)
|
|
|
|
FOUNDATION_PART_END
|
|
|
|
};
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Mode of "sprite combining"
|
2009-07-29 20:27:24 +00:00
|
|
|
* @see StartSpriteCombine
|
|
|
|
*/
|
|
|
|
enum SpriteCombineMode {
|
|
|
|
SPRITE_COMBINE_NONE, ///< Every #AddSortableSpriteToDraw start its own bounding box
|
|
|
|
SPRITE_COMBINE_PENDING, ///< %Sprite combining will start with the next unclipped sprite.
|
|
|
|
SPRITE_COMBINE_ACTIVE, ///< %Sprite combining is active. #AddSortableSpriteToDraw outputs child sprites.
|
|
|
|
};
|
|
|
|
|
2008-04-16 19:01:09 +00:00
|
|
|
typedef SmallVector<TileSpriteToDraw, 64> TileSpriteToDrawVector;
|
|
|
|
typedef SmallVector<StringSpriteToDraw, 4> StringSpriteToDrawVector;
|
2008-04-16 20:01:04 +00:00
|
|
|
typedef SmallVector<ParentSpriteToDraw, 64> ParentSpriteToDrawVector;
|
|
|
|
typedef SmallVector<ParentSpriteToDraw*, 64> ParentSpriteToSortVector;
|
2008-04-16 20:39:35 +00:00
|
|
|
typedef SmallVector<ChildScreenSpriteToDraw, 16> ChildScreenSpriteToDrawVector;
|
2008-04-16 14:15:00 +00:00
|
|
|
|
2008-10-13 03:26:48 +00:00
|
|
|
/** Data structure storing rendering information */
|
2007-03-07 12:11:48 +00:00
|
|
|
struct ViewportDrawer {
|
2004-08-09 17:04:08 +00:00
|
|
|
DrawPixelInfo dpi;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2008-04-16 14:15:00 +00:00
|
|
|
StringSpriteToDrawVector string_sprites_to_draw;
|
2008-04-16 14:18:15 +00:00
|
|
|
TileSpriteToDrawVector tile_sprites_to_draw;
|
2008-04-16 20:01:04 +00:00
|
|
|
ParentSpriteToDrawVector parent_sprites_to_draw;
|
2008-10-13 03:26:48 +00:00
|
|
|
ParentSpriteToSortVector parent_sprites_to_sort; ///< Parent sprite pointer array used for sorting
|
2008-04-16 20:39:35 +00:00
|
|
|
ChildScreenSpriteToDrawVector child_screen_sprites_to_draw;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-16 20:39:35 +00:00
|
|
|
int *last_child;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-07-29 20:27:24 +00:00
|
|
|
SpriteCombineMode combine_sprites; ///< Current mode of "sprite combining". @see StartSpriteCombine
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-16 20:39:35 +00:00
|
|
|
int foundation[FOUNDATION_PART_END]; ///< Foundation sprites (index into parent_sprites_to_draw).
|
|
|
|
FoundationPart foundation_part; ///< Currently active foundation for ground sprite drawing.
|
|
|
|
int *last_foundation_child[FOUNDATION_PART_END]; ///< Tail of ChildSprite list of the foundations. (index into child_screen_sprites_to_draw)
|
2010-05-30 13:05:36 +00:00
|
|
|
Point foundation_offset[FOUNDATION_PART_END]; ///< Pixel offset for ground sprites on the foundations.
|
2007-03-07 12:11:48 +00:00
|
|
|
};
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-16 21:06:21 +00:00
|
|
|
static ViewportDrawer _vd;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-05-27 15:05:54 +00:00
|
|
|
TileHighlightData _thd;
|
2004-08-09 17:04:08 +00:00
|
|
|
static TileInfo *_cur_ti;
|
2008-04-18 16:51:54 +00:00
|
|
|
bool _draw_bounding_boxes = false;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-03-15 15:25:18 +00:00
|
|
|
static Point MapXYZToViewport(const ViewPort *vp, int x, int y, int z)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Point p = RemapCoords(x, y, z);
|
2005-07-17 20:14:58 +00:00
|
|
|
p.x -= vp->virtual_width / 2;
|
|
|
|
p.y -= vp->virtual_height / 2;
|
2004-08-09 17:04:08 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2006-11-18 13:54:33 +00:00
|
|
|
void DeleteWindowViewport(Window *w)
|
|
|
|
{
|
2008-05-11 15:08:44 +00:00
|
|
|
free(w->viewport);
|
2006-11-18 13:54:33 +00:00
|
|
|
w->viewport = NULL;
|
|
|
|
}
|
|
|
|
|
2008-04-19 13:05:05 +00:00
|
|
|
/**
|
|
|
|
* Initialize viewport of the window for use.
|
|
|
|
* @param w Window to use/display the viewport in
|
|
|
|
* @param x Offset of left edge of viewport with respect to left edge window \a w
|
|
|
|
* @param y Offset of top edge of viewport with respect to top edge window \a w
|
|
|
|
* @param width Width of the viewport
|
|
|
|
* @param height Height of the viewport
|
|
|
|
* @param follow_flags Flags controlling the viewport.
|
|
|
|
* - If bit 31 is set, the lower 16 bits are the vehicle that the viewport should follow.
|
2010-10-23 18:12:38 +00:00
|
|
|
* - If bit 31 is clear, it is a #TileIndex.
|
2008-04-19 13:05:05 +00:00
|
|
|
* @param zoom Zoomlevel to display
|
|
|
|
*/
|
2008-04-19 13:17:19 +00:00
|
|
|
void InitializeWindowViewport(Window *w, int x, int y,
|
2007-05-15 14:08:39 +00:00
|
|
|
int width, int height, uint32 follow_flags, ZoomLevel zoom)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-04-17 09:42:44 +00:00
|
|
|
assert(w->viewport == NULL);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-05-11 15:08:44 +00:00
|
|
|
ViewportData *vp = CallocT<ViewportData>(1);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
vp->left = x + w->left;
|
|
|
|
vp->top = y + w->top;
|
|
|
|
vp->width = width;
|
|
|
|
vp->height = height;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
vp->zoom = zoom;
|
|
|
|
|
2007-05-19 23:52:04 +00:00
|
|
|
vp->virtual_width = ScaleByZoom(width, zoom);
|
|
|
|
vp->virtual_height = ScaleByZoom(height, zoom);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-17 09:42:44 +00:00
|
|
|
Point pt;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (follow_flags & 0x80000000) {
|
2005-07-17 20:14:58 +00:00
|
|
|
const Vehicle *veh;
|
|
|
|
|
2008-05-11 15:08:44 +00:00
|
|
|
vp->follow_vehicle = (VehicleID)(follow_flags & 0xFFFF);
|
2009-05-16 23:34:14 +00:00
|
|
|
veh = Vehicle::Get(vp->follow_vehicle);
|
2004-08-09 17:04:08 +00:00
|
|
|
pt = MapXYZToViewport(vp, veh->x_pos, veh->y_pos, veh->z_pos);
|
|
|
|
} else {
|
2006-04-03 05:32:11 +00:00
|
|
|
uint x = TileX(follow_flags) * TILE_SIZE;
|
|
|
|
uint y = TileY(follow_flags) * TILE_SIZE;
|
2005-07-17 20:14:58 +00:00
|
|
|
|
2008-05-11 15:08:44 +00:00
|
|
|
vp->follow_vehicle = INVALID_VEHICLE;
|
2005-07-17 20:14:58 +00:00
|
|
|
pt = MapXYZToViewport(vp, x, y, GetSlopeZ(x, y));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-05-11 15:08:44 +00:00
|
|
|
vp->scrollpos_x = pt.x;
|
|
|
|
vp->scrollpos_y = pt.y;
|
|
|
|
vp->dest_scrollpos_x = pt.x;
|
|
|
|
vp->dest_scrollpos_y = pt.y;
|
2007-05-28 16:46:16 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
w->viewport = vp;
|
|
|
|
vp->virtual_left = 0;//pt.x;
|
|
|
|
vp->virtual_top = 0;//pt.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Point _vp_move_offs;
|
|
|
|
|
2009-01-07 16:11:27 +00:00
|
|
|
static void DoSetViewportPosition(const Window *w, int left, int top, int width, int height)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-01-07 18:59:46 +00:00
|
|
|
FOR_ALL_WINDOWS_FROM_BACK_FROM(w, w) {
|
2004-08-09 17:04:08 +00:00
|
|
|
if (left + width > w->left &&
|
2005-07-17 20:14:58 +00:00
|
|
|
w->left + w->width > left &&
|
2004-08-09 17:04:08 +00:00
|
|
|
top + height > w->top &&
|
2005-07-17 20:14:58 +00:00
|
|
|
w->top + w->height > top) {
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (left < w->left) {
|
2009-01-07 16:11:27 +00:00
|
|
|
DoSetViewportPosition(w, left, top, w->left - left, height);
|
|
|
|
DoSetViewportPosition(w, left + (w->left - left), top, width - (w->left - left), height);
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (left + width > w->left + w->width) {
|
2009-01-07 16:11:27 +00:00
|
|
|
DoSetViewportPosition(w, left, top, (w->left + w->width - left), height);
|
2009-10-02 15:13:15 +00:00
|
|
|
DoSetViewportPosition(w, left + (w->left + w->width - left), top, width - (w->left + w->width - left), height);
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (top < w->top) {
|
2009-01-07 16:11:27 +00:00
|
|
|
DoSetViewportPosition(w, left, top, width, (w->top - top));
|
|
|
|
DoSetViewportPosition(w, left, top + (w->top - top), width, height - (w->top - top));
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (top + height > w->top + w->height) {
|
2009-01-07 16:11:27 +00:00
|
|
|
DoSetViewportPosition(w, left, top, width, (w->top + w->height - top));
|
2009-10-02 15:13:15 +00:00
|
|
|
DoSetViewportPosition(w, left, top + (w->top + w->height - top), width, height - (w->top + w->height - top));
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
int xo = _vp_move_offs.x;
|
|
|
|
int yo = _vp_move_offs.y;
|
|
|
|
|
|
|
|
if (abs(xo) >= width || abs(yo) >= height) {
|
|
|
|
/* fully_outside */
|
2005-07-17 20:14:58 +00:00
|
|
|
RedrawScreenRect(left, top, left + width, top + height);
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GfxScroll(left, top, width, height, xo, yo);
|
|
|
|
|
|
|
|
if (xo > 0) {
|
|
|
|
RedrawScreenRect(left, top, xo + left, top + height);
|
|
|
|
left += xo;
|
|
|
|
width -= xo;
|
|
|
|
} else if (xo < 0) {
|
2009-01-09 22:56:28 +00:00
|
|
|
RedrawScreenRect(left + width + xo, top, left + width, top + height);
|
2004-08-09 17:04:08 +00:00
|
|
|
width += xo;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (yo > 0) {
|
2009-01-09 22:56:28 +00:00
|
|
|
RedrawScreenRect(left, top, width + left, top + yo);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else if (yo < 0) {
|
2009-01-09 22:56:28 +00:00
|
|
|
RedrawScreenRect(left, top + height + yo, width + left, top + height);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-26 03:33:12 +00:00
|
|
|
static void SetViewportPosition(Window *w, int x, int y)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
ViewPort *vp = w->viewport;
|
|
|
|
int old_left = vp->virtual_left;
|
|
|
|
int old_top = vp->virtual_top;
|
|
|
|
int i;
|
|
|
|
int left, top, width, height;
|
|
|
|
|
|
|
|
vp->virtual_left = x;
|
|
|
|
vp->virtual_top = y;
|
|
|
|
|
2010-05-30 13:05:36 +00:00
|
|
|
/* Viewport is bound to its left top corner, so it must be rounded down (UnScaleByZoomLower)
|
2007-11-18 13:13:59 +00:00
|
|
|
* else glitch described in FS#1412 will happen (offset by 1 pixel with zoom level > NORMAL)
|
|
|
|
*/
|
|
|
|
old_left = UnScaleByZoomLower(old_left, vp->zoom);
|
|
|
|
old_top = UnScaleByZoomLower(old_top, vp->zoom);
|
|
|
|
x = UnScaleByZoomLower(x, vp->zoom);
|
|
|
|
y = UnScaleByZoomLower(y, vp->zoom);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
old_left -= x;
|
|
|
|
old_top -= y;
|
|
|
|
|
2005-07-17 20:14:58 +00:00
|
|
|
if (old_top == 0 && old_left == 0) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
_vp_move_offs.x = old_left;
|
|
|
|
_vp_move_offs.y = old_top;
|
|
|
|
|
|
|
|
left = vp->left;
|
|
|
|
top = vp->top;
|
|
|
|
width = vp->width;
|
|
|
|
height = vp->height;
|
|
|
|
|
|
|
|
if (left < 0) {
|
|
|
|
width += left;
|
|
|
|
left = 0;
|
|
|
|
}
|
|
|
|
|
2005-07-17 20:14:58 +00:00
|
|
|
i = left + width - _screen.width;
|
|
|
|
if (i >= 0) width -= i;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (width > 0) {
|
|
|
|
if (top < 0) {
|
|
|
|
height += top;
|
|
|
|
top = 0;
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-07-17 20:14:58 +00:00
|
|
|
i = top + height - _screen.height;
|
|
|
|
if (i >= 0) height -= i;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-01-07 16:11:27 +00:00
|
|
|
if (height > 0) DoSetViewportPosition(w->z_front, left, top, width, height);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-19 13:05:05 +00:00
|
|
|
/**
|
|
|
|
* Is a xy position inside the viewport of the window?
|
|
|
|
* @param w Window to examine its viewport
|
|
|
|
* @param x X coordinate of the xy position
|
|
|
|
* @param y Y coordinate of the xy position
|
|
|
|
* @return Pointer to the viewport if the xy position is in the viewport of the window,
|
|
|
|
* otherwise \c NULL is returned.
|
|
|
|
*/
|
2005-07-17 20:14:58 +00:00
|
|
|
ViewPort *IsPtInWindowViewport(const Window *w, int x, int y)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
ViewPort *vp = w->viewport;
|
2005-07-17 20:14:58 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (vp != NULL &&
|
2008-02-14 15:13:36 +00:00
|
|
|
IsInsideMM(x, vp->left, vp->left + vp->width) &&
|
2007-11-24 10:38:43 +00:00
|
|
|
IsInsideMM(y, vp->top, vp->top + vp->height))
|
2004-08-09 17:04:08 +00:00
|
|
|
return vp;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-10-13 03:26:48 +00:00
|
|
|
/**
|
|
|
|
* Translate screen coordinate in a viewport to a tile coordinate
|
|
|
|
* @param vp Viewport that contains the (\a x, \a y) screen coordinate
|
|
|
|
* @param x Screen x coordinate
|
|
|
|
* @param y Screen y coordinate
|
2010-08-01 19:44:49 +00:00
|
|
|
* @return Tile coordinate
|
|
|
|
*/
|
2005-07-17 20:14:58 +00:00
|
|
|
static Point TranslateXYToTileCoord(const ViewPort *vp, int x, int y)
|
2005-01-22 22:47:58 +00:00
|
|
|
{
|
2004-08-09 17:04:08 +00:00
|
|
|
Point pt;
|
2009-01-09 22:56:28 +00:00
|
|
|
int a, b;
|
2006-11-03 00:48:03 +00:00
|
|
|
uint z;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if ( (uint)(x -= vp->left) >= (uint)vp->width ||
|
|
|
|
(uint)(y -= vp->top) >= (uint)vp->height) {
|
|
|
|
Point pt = {-1, -1};
|
|
|
|
return pt;
|
|
|
|
}
|
|
|
|
|
2007-05-15 16:08:46 +00:00
|
|
|
x = (ScaleByZoom(x, vp->zoom) + vp->virtual_left) >> 2;
|
|
|
|
y = (ScaleByZoom(y, vp->zoom) + vp->virtual_top) >> 1;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-01-09 22:56:28 +00:00
|
|
|
a = y - x;
|
|
|
|
b = y + x;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-11-03 00:48:03 +00:00
|
|
|
/* we need to move variables in to the valid range, as the
|
|
|
|
* GetTileZoomCenterWindow() function can call here with invalid x and/or y,
|
|
|
|
* when the user tries to zoom out along the sides of the map */
|
2010-05-13 12:42:56 +00:00
|
|
|
a = Clamp(a, -4 * (int)TILE_SIZE, (int)(MapMaxX() * TILE_SIZE) - 1);
|
|
|
|
b = Clamp(b, -4 * (int)TILE_SIZE, (int)(MapMaxY() * TILE_SIZE) - 1);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-10-20 16:50:48 +00:00
|
|
|
/* (a, b) is the X/Y-world coordinate that belongs to (x,y) if the landscape would be completely flat on height 0.
|
|
|
|
* Now find the Z-world coordinate by fix point iteration.
|
|
|
|
* This is a bit tricky because the tile height is non-continuous at foundations.
|
|
|
|
* The clicked point should be approached from the back, otherwise there are regions that are not clickable.
|
|
|
|
* (FOUNDATION_HALFTILE_LOWER on SLOPE_STEEP_S hides north halftile completely)
|
|
|
|
* So give it a z-malus of 4 in the first iterations.
|
|
|
|
*/
|
|
|
|
z = 0;
|
2006-08-06 16:32:49 +00:00
|
|
|
|
2009-01-21 02:31:55 +00:00
|
|
|
int min_coord = _settings_game.construction.freeform_edges ? TILE_SIZE : 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < 5; i++) z = GetSlopeZ(Clamp(a + (int)max(z, 4u) - 4, min_coord, MapMaxX() * TILE_SIZE - 1), Clamp(b + (int)max(z, 4u) - 4, min_coord, MapMaxY() * TILE_SIZE - 1)) / 2;
|
|
|
|
for (uint malus = 3; malus > 0; malus--) z = GetSlopeZ(Clamp(a + (int)max(z, malus) - (int)malus, min_coord, MapMaxX() * TILE_SIZE - 1), Clamp(b + (int)max(z, malus) - (int)malus, min_coord, MapMaxY() * TILE_SIZE - 1)) / 2;
|
|
|
|
for (int i = 0; i < 5; i++) z = GetSlopeZ(Clamp(a + (int)z, min_coord, MapMaxX() * TILE_SIZE - 1), Clamp(b + (int)z, min_coord, MapMaxY() * TILE_SIZE - 1)) / 2;
|
|
|
|
|
|
|
|
pt.x = Clamp(a + (int)z, min_coord, MapMaxX() * TILE_SIZE - 1);
|
|
|
|
pt.y = Clamp(b + (int)z, min_coord, MapMaxY() * TILE_SIZE - 1);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
return pt;
|
|
|
|
}
|
|
|
|
|
2005-01-10 22:56:20 +00:00
|
|
|
/* When used for zooming, check area below current coordinates (x,y)
|
|
|
|
* and return the tile of the zoomed out/in position (zoom_x, zoom_y)
|
|
|
|
* when you just want the tile, make x = zoom_x and y = zoom_y */
|
|
|
|
static Point GetTileFromScreenXY(int x, int y, int zoom_x, int zoom_y)
|
2004-09-10 19:02:27 +00:00
|
|
|
{
|
2004-08-09 17:04:08 +00:00
|
|
|
Window *w;
|
|
|
|
ViewPort *vp;
|
|
|
|
Point pt;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
|
|
|
if ( (w = FindWindowFromPt(x, y)) != NULL &&
|
2004-08-09 17:04:08 +00:00
|
|
|
(vp = IsPtInWindowViewport(w, x, y)) != NULL)
|
2005-01-10 22:56:20 +00:00
|
|
|
return TranslateXYToTileCoord(vp, zoom_x, zoom_y);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
pt.y = pt.x = -1;
|
|
|
|
return pt;
|
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
Point GetTileBelowCursor()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-01-10 22:56:20 +00:00
|
|
|
return GetTileFromScreenXY(_cursor.pos.x, _cursor.pos.y, _cursor.pos.x, _cursor.pos.y);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2004-09-03 19:59:05 +00:00
|
|
|
|
|
|
|
Point GetTileZoomCenterWindow(bool in, Window * w)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
int x, y;
|
2008-05-11 15:08:44 +00:00
|
|
|
ViewPort *vp = w->viewport;
|
2004-09-03 19:59:05 +00:00
|
|
|
|
2005-07-08 22:25:24 +00:00
|
|
|
if (in) {
|
2005-07-17 20:14:58 +00:00
|
|
|
x = ((_cursor.pos.x - vp->left) >> 1) + (vp->width >> 2);
|
|
|
|
y = ((_cursor.pos.y - vp->top) >> 1) + (vp->height >> 2);
|
|
|
|
} else {
|
2004-09-03 19:59:05 +00:00
|
|
|
x = vp->width - (_cursor.pos.x - vp->left);
|
|
|
|
y = vp->height - (_cursor.pos.y - vp->top);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2005-01-10 22:56:20 +00:00
|
|
|
/* Get the tile below the cursor and center on the zoomed-out center */
|
|
|
|
return GetTileFromScreenXY(_cursor.pos.x, _cursor.pos.y, x + vp->left, y + vp->top);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Update the status of the zoom-buttons according to the zoom-level
|
2006-11-07 13:06:02 +00:00
|
|
|
* of the viewport. This will update their status and invalidate accordingly
|
2007-04-09 15:06:24 +00:00
|
|
|
* @param w Window pointer to the window that has the zoom buttons
|
2006-11-07 13:06:02 +00:00
|
|
|
* @param vp pointer to the viewport whose zoom-level the buttons represent
|
|
|
|
* @param widget_zoom_in widget index for window with zoom-in button
|
2010-08-01 19:44:49 +00:00
|
|
|
* @param widget_zoom_out widget index for window with zoom-out button
|
|
|
|
*/
|
2006-11-07 13:06:02 +00:00
|
|
|
void HandleZoomMessage(Window *w, const ViewPort *vp, byte widget_zoom_in, byte widget_zoom_out)
|
|
|
|
{
|
2007-12-02 14:29:48 +00:00
|
|
|
w->SetWidgetDisabledState(widget_zoom_in, vp->zoom == ZOOM_LVL_MIN);
|
2009-09-13 19:15:59 +00:00
|
|
|
w->SetWidgetDirty(widget_zoom_in);
|
2006-11-07 13:06:02 +00:00
|
|
|
|
2007-12-02 14:29:48 +00:00
|
|
|
w->SetWidgetDisabledState(widget_zoom_out, vp->zoom == ZOOM_LVL_MAX);
|
2009-09-13 19:15:59 +00:00
|
|
|
w->SetWidgetDirty(widget_zoom_out);
|
2006-11-07 13:06:02 +00:00
|
|
|
}
|
|
|
|
|
2007-10-05 21:49:15 +00:00
|
|
|
/**
|
2010-05-30 13:05:36 +00:00
|
|
|
* Schedules a tile sprite for drawing.
|
2007-10-05 21:49:15 +00:00
|
|
|
*
|
|
|
|
* @param image the image to draw.
|
|
|
|
* @param pal the provided palette.
|
2009-06-07 13:25:21 +00:00
|
|
|
* @param x position x (world coordinates) of the sprite.
|
|
|
|
* @param y position y (world coordinates) of the sprite.
|
|
|
|
* @param z position z (world coordinates) of the sprite.
|
2007-10-05 21:49:15 +00:00
|
|
|
* @param sub Only draw a part of the sprite.
|
2009-06-07 13:25:21 +00:00
|
|
|
* @param extra_offs_x Pixel X offset for the sprite position.
|
|
|
|
* @param extra_offs_y Pixel Y offset for the sprite position.
|
2007-10-05 21:49:15 +00:00
|
|
|
*/
|
2010-01-21 01:38:13 +00:00
|
|
|
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)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-07-24 15:56:31 +00:00
|
|
|
assert((image & SPRITE_MASK) < MAX_SPRITES);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-16 21:06:21 +00:00
|
|
|
TileSpriteToDraw *ts = _vd.tile_sprites_to_draw.Append();
|
2008-04-16 19:01:09 +00:00
|
|
|
ts->image = image;
|
|
|
|
ts->pal = pal;
|
|
|
|
ts->sub = sub;
|
2009-06-07 13:25:21 +00:00
|
|
|
Point pt = RemapCoords(x, y, z);
|
|
|
|
ts->x = pt.x + extra_offs_x;
|
|
|
|
ts->y = pt.y + extra_offs_y;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-10-14 19:57:15 +00:00
|
|
|
/**
|
|
|
|
* Adds a child sprite to the active foundation.
|
|
|
|
*
|
|
|
|
* The pixel offset of the sprite relative to the ParentSprite is the sum of the offset passed to OffsetGroundSprite() and extra_offs_?.
|
|
|
|
*
|
|
|
|
* @param image the image to draw.
|
|
|
|
* @param pal the provided palette.
|
|
|
|
* @param sub Only draw a part of the sprite.
|
2007-10-20 20:25:43 +00:00
|
|
|
* @param foundation_part Foundation part.
|
2007-10-14 19:57:15 +00:00
|
|
|
* @param extra_offs_x Pixel X offset for the sprite position.
|
|
|
|
* @param extra_offs_y Pixel Y offset for the sprite position.
|
|
|
|
*/
|
2010-01-21 01:38:13 +00:00
|
|
|
static void AddChildSpriteToFoundation(SpriteID image, PaletteID pal, const SubSprite *sub, FoundationPart foundation_part, int extra_offs_x, int extra_offs_y)
|
2007-10-14 19:57:15 +00:00
|
|
|
{
|
2007-11-24 10:38:43 +00:00
|
|
|
assert(IsInsideMM(foundation_part, 0, FOUNDATION_PART_END));
|
2008-04-16 21:06:21 +00:00
|
|
|
assert(_vd.foundation[foundation_part] != -1);
|
|
|
|
Point offs = _vd.foundation_offset[foundation_part];
|
2007-10-14 19:57:15 +00:00
|
|
|
|
|
|
|
/* Change the active ChildSprite list to the one of the foundation */
|
2008-04-16 21:06:21 +00:00
|
|
|
int *old_child = _vd.last_child;
|
|
|
|
_vd.last_child = _vd.last_foundation_child[foundation_part];
|
2007-10-14 19:57:15 +00:00
|
|
|
|
|
|
|
AddChildSpriteScreen(image, pal, offs.x + extra_offs_x, offs.y + extra_offs_y, false, sub);
|
|
|
|
|
|
|
|
/* Switch back to last ChildSprite list */
|
2008-04-16 21:06:21 +00:00
|
|
|
_vd.last_child = old_child;
|
2007-10-14 19:57:15 +00:00
|
|
|
}
|
|
|
|
|
2007-10-05 21:49:15 +00:00
|
|
|
/**
|
2010-01-03 20:55:00 +00:00
|
|
|
* Draws a ground sprite at a specific world-coordinate relative to the current tile.
|
2007-10-05 21:49:15 +00:00
|
|
|
* If the current tile is drawn on top of a foundation the sprite is added as child sprite to the "foundation"-ParentSprite.
|
|
|
|
*
|
|
|
|
* @param image the image to draw.
|
|
|
|
* @param pal the provided palette.
|
2010-01-03 20:55:00 +00:00
|
|
|
* @param x position x (world coordinates) of the sprite relative to current tile.
|
|
|
|
* @param y position y (world coordinates) of the sprite relative to current tile.
|
|
|
|
* @param z position z (world coordinates) of the sprite relative to current tile.
|
2007-10-05 21:49:15 +00:00
|
|
|
* @param sub Only draw a part of the sprite.
|
2009-06-07 13:25:21 +00:00
|
|
|
* @param extra_offs_x Pixel X offset for the sprite position.
|
|
|
|
* @param extra_offs_y Pixel Y offset for the sprite position.
|
2007-10-05 21:49:15 +00:00
|
|
|
*/
|
2010-01-21 01:38:13 +00:00
|
|
|
void DrawGroundSpriteAt(SpriteID image, PaletteID pal, int32 x, int32 y, int z, const SubSprite *sub, int extra_offs_x, int extra_offs_y)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-10-20 20:25:43 +00:00
|
|
|
/* Switch to first foundation part, if no foundation was drawn */
|
2008-04-16 21:06:21 +00:00
|
|
|
if (_vd.foundation_part == FOUNDATION_PART_NONE) _vd.foundation_part = FOUNDATION_PART_NORMAL;
|
2007-10-20 20:25:43 +00:00
|
|
|
|
2008-04-16 21:06:21 +00:00
|
|
|
if (_vd.foundation[_vd.foundation_part] != -1) {
|
2010-01-03 20:55:00 +00:00
|
|
|
Point pt = RemapCoords(x, y, z);
|
|
|
|
AddChildSpriteToFoundation(image, pal, sub, _vd.foundation_part, pt.x + extra_offs_x, pt.y + extra_offs_y);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2010-01-03 20:55:00 +00:00
|
|
|
AddTileSpriteToDraw(image, pal, _cur_ti->x + x, _cur_ti->y + y, _cur_ti->z + z, sub, extra_offs_x, extra_offs_y);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-03 20:55:00 +00:00
|
|
|
/**
|
|
|
|
* Draws a ground sprite for the current tile.
|
|
|
|
* If the current tile is drawn on top of a foundation the sprite is added as child sprite to the "foundation"-ParentSprite.
|
|
|
|
*
|
|
|
|
* @param image the image to draw.
|
|
|
|
* @param pal the provided palette.
|
|
|
|
* @param sub Only draw a part of the sprite.
|
|
|
|
* @param extra_offs_x Pixel X offset for the sprite position.
|
|
|
|
* @param extra_offs_y Pixel Y offset for the sprite position.
|
|
|
|
*/
|
2010-01-21 01:38:13 +00:00
|
|
|
void DrawGroundSprite(SpriteID image, PaletteID pal, const SubSprite *sub, int extra_offs_x, int extra_offs_y)
|
2010-01-03 20:55:00 +00:00
|
|
|
{
|
|
|
|
DrawGroundSpriteAt(image, pal, 0, 0, 0, sub, extra_offs_x, extra_offs_y);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-10-14 19:57:15 +00:00
|
|
|
/**
|
|
|
|
* Called when a foundation has been drawn for the current tile.
|
|
|
|
* Successive ground sprites for the current tile will be drawn as child sprites of the "foundation"-ParentSprite, not as TileSprites.
|
|
|
|
*
|
|
|
|
* @param x sprite x-offset (screen coordinates) of ground sprites relative to the "foundation"-ParentSprite.
|
|
|
|
* @param y sprite y-offset (screen coordinates) of ground sprites relative to the "foundation"-ParentSprite.
|
|
|
|
*/
|
2004-08-09 17:04:08 +00:00
|
|
|
void OffsetGroundSprite(int x, int y)
|
|
|
|
{
|
2007-10-20 20:25:43 +00:00
|
|
|
/* Switch to next foundation part */
|
2008-04-16 21:06:21 +00:00
|
|
|
switch (_vd.foundation_part) {
|
2007-10-20 20:25:43 +00:00
|
|
|
case FOUNDATION_PART_NONE:
|
2008-04-16 21:06:21 +00:00
|
|
|
_vd.foundation_part = FOUNDATION_PART_NORMAL;
|
2007-10-20 20:25:43 +00:00
|
|
|
break;
|
|
|
|
case FOUNDATION_PART_NORMAL:
|
2008-04-16 21:06:21 +00:00
|
|
|
_vd.foundation_part = FOUNDATION_PART_HALFTILE;
|
2007-10-20 20:25:43 +00:00
|
|
|
break;
|
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
2007-10-14 19:57:15 +00:00
|
|
|
|
2008-04-16 21:06:21 +00:00
|
|
|
/* _vd.last_child == NULL if foundation sprite was clipped by the viewport bounds */
|
2008-06-19 09:32:25 +00:00
|
|
|
if (_vd.last_child != NULL) _vd.foundation[_vd.foundation_part] = _vd.parent_sprites_to_draw.Length() - 1;
|
2007-10-14 19:57:15 +00:00
|
|
|
|
2008-04-16 21:06:21 +00:00
|
|
|
_vd.foundation_offset[_vd.foundation_part].x = x;
|
|
|
|
_vd.foundation_offset[_vd.foundation_part].y = y;
|
|
|
|
_vd.last_foundation_child[_vd.foundation_part] = _vd.last_child;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-10-05 21:49:15 +00:00
|
|
|
/**
|
|
|
|
* Adds a child sprite to a parent sprite.
|
|
|
|
* In contrast to "AddChildSpriteScreen()" the sprite position is in world coordinates
|
|
|
|
*
|
|
|
|
* @param image the image to draw.
|
|
|
|
* @param pal the provided palette.
|
|
|
|
* @param x position x of the sprite.
|
|
|
|
* @param y position y of the sprite.
|
|
|
|
* @param z position z of the sprite.
|
|
|
|
* @param sub Only draw a part of the sprite.
|
|
|
|
*/
|
2010-01-21 01:38:13 +00:00
|
|
|
static void AddCombinedSprite(SpriteID image, PaletteID pal, int x, int y, byte z, const SubSprite *sub)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Point pt = RemapCoords(x, y, z);
|
2009-01-10 00:31:47 +00:00
|
|
|
const Sprite *spr = GetSprite(image & SPRITE_MASK, ST_NORMAL);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-16 21:06:21 +00:00
|
|
|
if (pt.x + spr->x_offs >= _vd.dpi.left + _vd.dpi.width ||
|
|
|
|
pt.x + spr->x_offs + spr->width <= _vd.dpi.left ||
|
|
|
|
pt.y + spr->y_offs >= _vd.dpi.top + _vd.dpi.height ||
|
|
|
|
pt.y + spr->y_offs + spr->height <= _vd.dpi.top)
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
|
2008-04-16 21:06:21 +00:00
|
|
|
const ParentSpriteToDraw *pstd = _vd.parent_sprites_to_draw.End() - 1;
|
2008-04-16 20:01:04 +00:00
|
|
|
AddChildSpriteScreen(image, pal, pt.x - pstd->left, pt.y - pstd->top, false, sub);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Draw a (transparent) sprite at given coordinates with a given bounding box.
|
2007-09-14 21:54:57 +00:00
|
|
|
* The bounding box extends from (x + bb_offset_x, y + bb_offset_y, z + bb_offset_z) to (x + w - 1, y + h - 1, z + dz - 1), both corners included.
|
2007-09-23 09:37:25 +00:00
|
|
|
* Bounding boxes with bb_offset_x == w or bb_offset_y == h or bb_offset_z == dz are allowed and produce thin slices.
|
2007-09-14 21:54:57 +00:00
|
|
|
*
|
|
|
|
* @note Bounding boxes are normally specified with bb_offset_x = bb_offset_y = bb_offset_z = 0. The extent of the bounding box in negative direction is
|
|
|
|
* defined by the sprite offset in the grf file.
|
|
|
|
* However if modifying the sprite offsets is not suitable (e.g. when using existing graphics), the bounding box can be tuned by bb_offset.
|
|
|
|
*
|
2007-09-23 09:37:25 +00:00
|
|
|
* @pre w >= bb_offset_x, h >= bb_offset_y, dz >= bb_offset_z. Else w, h or dz are ignored.
|
2007-09-14 21:54:57 +00:00
|
|
|
*
|
2007-07-26 14:07:11 +00:00
|
|
|
* @param image the image to combine and draw,
|
|
|
|
* @param pal the provided palette,
|
2007-09-14 21:54:57 +00:00
|
|
|
* @param x position X (world) of the sprite,
|
|
|
|
* @param y position Y (world) of the sprite,
|
|
|
|
* @param w bounding box extent towards positive X (world),
|
|
|
|
* @param h bounding box extent towards positive Y (world),
|
|
|
|
* @param dz bounding box extent towards positive Z (world),
|
|
|
|
* @param z position Z (world) of the sprite,
|
|
|
|
* @param transparent if true, switch the palette between the provided palette and the transparent palette,
|
|
|
|
* @param bb_offset_x bounding box extent towards negative X (world),
|
|
|
|
* @param bb_offset_y bounding box extent towards negative Y (world),
|
|
|
|
* @param bb_offset_z bounding box extent towards negative Z (world)
|
2007-10-05 21:49:15 +00:00
|
|
|
* @param sub Only draw a part of the sprite.
|
2007-07-26 14:07:11 +00:00
|
|
|
*/
|
2010-01-21 01:38:13 +00:00
|
|
|
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)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-09-26 19:27:29 +00:00
|
|
|
int32 left, right, top, bottom;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-07-24 15:56:31 +00:00
|
|
|
assert((image & SPRITE_MASK) < MAX_SPRITES);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-07-26 14:07:11 +00:00
|
|
|
/* make the sprites transparent with the right palette */
|
|
|
|
if (transparent) {
|
2007-11-20 13:35:54 +00:00
|
|
|
SetBit(image, PALETTE_MODIFIER_TRANSPARENT);
|
2007-07-26 14:07:11 +00:00
|
|
|
pal = PALETTE_TO_TRANSPARENT;
|
|
|
|
}
|
|
|
|
|
2009-07-29 20:27:24 +00:00
|
|
|
if (_vd.combine_sprites == SPRITE_COMBINE_ACTIVE) {
|
2007-10-05 21:49:15 +00:00
|
|
|
AddCombinedSprite(image, pal, x, y, z, sub);
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-04-16 21:06:21 +00:00
|
|
|
_vd.last_child = NULL;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-16 20:01:04 +00:00
|
|
|
Point pt = RemapCoords(x, y, z);
|
|
|
|
int tmp_left, tmp_top, tmp_x = pt.x, tmp_y = pt.y;
|
2007-09-26 19:27:29 +00:00
|
|
|
|
|
|
|
/* Compute screen extents of sprite */
|
2007-09-19 16:36:42 +00:00
|
|
|
if (image == SPR_EMPTY_BOUNDING_BOX) {
|
2008-04-16 20:01:04 +00:00
|
|
|
left = tmp_left = RemapCoords(x + w , y + bb_offset_y, z + bb_offset_z).x;
|
2007-09-26 19:27:29 +00:00
|
|
|
right = RemapCoords(x + bb_offset_x, y + h , z + bb_offset_z).x + 1;
|
2008-04-16 20:01:04 +00:00
|
|
|
top = tmp_top = RemapCoords(x + bb_offset_x, y + bb_offset_y, z + dz ).y;
|
2007-09-26 19:27:29 +00:00
|
|
|
bottom = RemapCoords(x + w , y + h , z + bb_offset_z).y + 1;
|
2007-09-19 16:36:42 +00:00
|
|
|
} else {
|
2008-09-02 15:20:38 +00:00
|
|
|
const Sprite *spr = GetSprite(image & SPRITE_MASK, ST_NORMAL);
|
2008-04-16 20:01:04 +00:00
|
|
|
left = tmp_left = (pt.x += spr->x_offs);
|
2007-09-26 19:27:29 +00:00
|
|
|
right = (pt.x + spr->width );
|
2008-04-16 20:01:04 +00:00
|
|
|
top = tmp_top = (pt.y += spr->y_offs);
|
2007-09-26 19:27:29 +00:00
|
|
|
bottom = (pt.y + spr->height);
|
|
|
|
}
|
2008-02-14 15:13:36 +00:00
|
|
|
|
2007-09-26 19:27:29 +00:00
|
|
|
if (_draw_bounding_boxes && (image != SPR_EMPTY_BOUNDING_BOX)) {
|
2010-04-12 14:12:47 +00:00
|
|
|
/* Compute maximal extents of sprite and its bounding box */
|
2007-09-26 19:27:29 +00:00
|
|
|
left = min(left , RemapCoords(x + w , y + bb_offset_y, z + bb_offset_z).x);
|
|
|
|
right = max(right , RemapCoords(x + bb_offset_x, y + h , z + bb_offset_z).x + 1);
|
|
|
|
top = min(top , RemapCoords(x + bb_offset_x, y + bb_offset_y, z + dz ).y);
|
|
|
|
bottom = max(bottom, RemapCoords(x + w , y + h , z + bb_offset_z).y + 1);
|
2007-09-19 16:36:42 +00:00
|
|
|
}
|
2008-02-14 15:13:36 +00:00
|
|
|
|
2007-09-26 19:27:29 +00:00
|
|
|
/* Do not add the sprite to the viewport, if it is outside */
|
2008-04-16 21:06:21 +00:00
|
|
|
if (left >= _vd.dpi.left + _vd.dpi.width ||
|
|
|
|
right <= _vd.dpi.left ||
|
|
|
|
top >= _vd.dpi.top + _vd.dpi.height ||
|
|
|
|
bottom <= _vd.dpi.top) {
|
2006-07-29 13:06:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-04-16 21:06:21 +00:00
|
|
|
ParentSpriteToDraw *ps = _vd.parent_sprites_to_draw.Append();
|
2008-04-16 20:01:04 +00:00
|
|
|
ps->x = tmp_x;
|
|
|
|
ps->y = tmp_y;
|
|
|
|
|
|
|
|
ps->left = tmp_left;
|
|
|
|
ps->top = tmp_top;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
ps->image = image;
|
2007-01-14 19:57:49 +00:00
|
|
|
ps->pal = pal;
|
2007-10-05 21:49:15 +00:00
|
|
|
ps->sub = sub;
|
2007-09-14 21:54:57 +00:00
|
|
|
ps->xmin = x + bb_offset_x;
|
2007-09-23 09:37:25 +00:00
|
|
|
ps->xmax = x + max(bb_offset_x, w) - 1;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-09-14 21:54:57 +00:00
|
|
|
ps->ymin = y + bb_offset_y;
|
2007-09-23 09:37:25 +00:00
|
|
|
ps->ymax = y + max(bb_offset_y, h) - 1;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-09-14 21:54:57 +00:00
|
|
|
ps->zmin = z + bb_offset_z;
|
2007-09-23 09:37:25 +00:00
|
|
|
ps->zmax = z + max(bb_offset_z, dz) - 1;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-09-14 21:54:57 +00:00
|
|
|
ps->comparison_done = false;
|
2008-06-16 20:08:30 +00:00
|
|
|
ps->first_child = -1;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-06-16 20:08:30 +00:00
|
|
|
_vd.last_child = &ps->first_child;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-07-29 20:27:24 +00:00
|
|
|
if (_vd.combine_sprites == SPRITE_COMBINE_PENDING) _vd.combine_sprites = SPRITE_COMBINE_ACTIVE;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-07-29 20:27:24 +00:00
|
|
|
/**
|
|
|
|
* Starts a block of sprites, which are "combined" into a single bounding box.
|
|
|
|
*
|
|
|
|
* Subsequent calls to #AddSortableSpriteToDraw will be drawn into the same bounding box.
|
|
|
|
* That is: The first sprite that is not clipped by the viewport defines the bounding box, and
|
|
|
|
* the following sprites will be child sprites to that one.
|
|
|
|
*
|
|
|
|
* That implies:
|
|
|
|
* - The drawing order is definite. No other sprites will be sorted between those of the block.
|
|
|
|
* - You have to provide a valid bounding box for all sprites,
|
|
|
|
* as you won't know which one is the first non-clipped one.
|
|
|
|
* Preferable you use the same bounding box for all.
|
|
|
|
* - You cannot use #AddChildSpriteScreen inside the block, as its result will be indefinite.
|
|
|
|
*
|
|
|
|
* The block is terminated by #EndSpriteCombine.
|
|
|
|
*
|
|
|
|
* You cannot nest "combined" blocks.
|
|
|
|
*/
|
2007-03-07 11:47:46 +00:00
|
|
|
void StartSpriteCombine()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-07-29 20:27:24 +00:00
|
|
|
assert(_vd.combine_sprites == SPRITE_COMBINE_NONE);
|
|
|
|
_vd.combine_sprites = SPRITE_COMBINE_PENDING;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-07-29 20:27:24 +00:00
|
|
|
/**
|
|
|
|
* Terminates a block of sprites started by #StartSpriteCombine.
|
|
|
|
* Take a look there for details.
|
|
|
|
*/
|
2007-03-07 11:47:46 +00:00
|
|
|
void EndSpriteCombine()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-07-29 20:27:24 +00:00
|
|
|
assert(_vd.combine_sprites != SPRITE_COMBINE_NONE);
|
|
|
|
_vd.combine_sprites = SPRITE_COMBINE_NONE;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-10-05 21:49:15 +00:00
|
|
|
/**
|
|
|
|
* Add a child sprite to a parent sprite.
|
|
|
|
*
|
|
|
|
* @param image the image to draw.
|
|
|
|
* @param pal the provided palette.
|
|
|
|
* @param x sprite x-offset (screen coordinates) relative to parent sprite.
|
|
|
|
* @param y sprite y-offset (screen coordinates) relative to parent sprite.
|
|
|
|
* @param transparent if true, switch the palette between the provided palette and the transparent palette,
|
|
|
|
* @param sub Only draw a part of the sprite.
|
|
|
|
*/
|
2010-01-21 01:38:13 +00:00
|
|
|
void AddChildSpriteScreen(SpriteID image, PaletteID pal, int x, int y, bool transparent, const SubSprite *sub)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-07-24 15:56:31 +00:00
|
|
|
assert((image & SPRITE_MASK) < MAX_SPRITES);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-16 20:39:35 +00:00
|
|
|
/* If the ParentSprite was clipped by the viewport bounds, do not draw the ChildSprites either */
|
2008-04-16 21:06:21 +00:00
|
|
|
if (_vd.last_child == NULL) return;
|
2008-04-16 20:39:35 +00:00
|
|
|
|
2007-09-30 19:33:40 +00:00
|
|
|
/* make the sprites transparent with the right palette */
|
|
|
|
if (transparent) {
|
2007-11-20 13:35:54 +00:00
|
|
|
SetBit(image, PALETTE_MODIFIER_TRANSPARENT);
|
2007-09-30 19:33:40 +00:00
|
|
|
pal = PALETTE_TO_TRANSPARENT;
|
|
|
|
}
|
|
|
|
|
2008-06-19 09:32:25 +00:00
|
|
|
*_vd.last_child = _vd.child_screen_sprites_to_draw.Length();
|
2008-06-16 20:08:30 +00:00
|
|
|
|
2008-04-16 21:06:21 +00:00
|
|
|
ChildScreenSpriteToDraw *cs = _vd.child_screen_sprites_to_draw.Append();
|
2004-08-09 17:04:08 +00:00
|
|
|
cs->image = image;
|
2007-01-14 19:57:49 +00:00
|
|
|
cs->pal = pal;
|
2007-10-05 21:49:15 +00:00
|
|
|
cs->sub = sub;
|
2004-08-09 17:04:08 +00:00
|
|
|
cs->x = x;
|
|
|
|
cs->y = y;
|
2008-06-16 20:08:30 +00:00
|
|
|
cs->next = -1;
|
2008-04-16 20:39:35 +00:00
|
|
|
|
2008-06-16 20:08:30 +00:00
|
|
|
/* Append the sprite to the active ChildSprite list.
|
|
|
|
* If the active ParentSprite is a foundation, update last_foundation_child as well.
|
|
|
|
* Note: ChildSprites of foundations are NOT sequential in the vector, as selection sprites are added at last. */
|
|
|
|
if (_vd.last_foundation_child[0] == _vd.last_child) _vd.last_foundation_child[0] = &cs->next;
|
|
|
|
if (_vd.last_foundation_child[1] == _vd.last_child) _vd.last_foundation_child[1] = &cs->next;
|
|
|
|
_vd.last_child = &cs->next;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-12-22 12:50:41 +00:00
|
|
|
static void AddStringToDraw(int x, int y, StringID string, uint64 params_1, uint64 params_2, Colours colour, uint16 width)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-12-22 12:50:41 +00:00
|
|
|
assert(width != 0);
|
2008-04-16 21:06:21 +00:00
|
|
|
StringSpriteToDraw *ss = _vd.string_sprites_to_draw.Append();
|
2008-04-16 19:01:09 +00:00
|
|
|
ss->string = string;
|
|
|
|
ss->x = x;
|
|
|
|
ss->y = y;
|
|
|
|
ss->params[0] = params_1;
|
|
|
|
ss->params[1] = params_2;
|
|
|
|
ss->width = width;
|
2009-02-09 02:57:15 +00:00
|
|
|
ss->colour = colour;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-06-10 08:37:41 +00:00
|
|
|
|
2007-10-14 19:57:15 +00:00
|
|
|
/**
|
|
|
|
* Draws sprites between ground sprite and everything above.
|
|
|
|
*
|
|
|
|
* The sprite is either drawn as TileSprite or as ChildSprite of the active foundation.
|
|
|
|
*
|
|
|
|
* @param image the image to draw.
|
|
|
|
* @param pal the provided palette.
|
|
|
|
* @param ti TileInfo Tile that is being drawn
|
|
|
|
* @param z_offset Z offset relative to the groundsprite. Only used for the sprite position, not for sprite sorting.
|
2007-10-20 20:25:43 +00:00
|
|
|
* @param foundation_part Foundation part the sprite belongs to.
|
2007-10-14 19:57:15 +00:00
|
|
|
*/
|
2010-01-21 01:38:13 +00:00
|
|
|
static void DrawSelectionSprite(SpriteID image, PaletteID pal, const TileInfo *ti, int z_offset, FoundationPart foundation_part)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2010-10-30 17:51:07 +00:00
|
|
|
/* FIXME: This is not totally valid for some autorail highlights that extend over the edges of the tile. */
|
2008-04-16 21:06:21 +00:00
|
|
|
if (_vd.foundation[foundation_part] == -1) {
|
2007-10-14 19:57:15 +00:00
|
|
|
/* draw on real ground */
|
2010-01-03 20:55:00 +00:00
|
|
|
AddTileSpriteToDraw(image, pal, ti->x, ti->y, ti->z + z_offset);
|
2007-10-14 19:57:15 +00:00
|
|
|
} else {
|
|
|
|
/* draw on top of foundation */
|
2007-10-20 20:25:43 +00:00
|
|
|
AddChildSpriteToFoundation(image, pal, NULL, foundation_part, 0, -z_offset);
|
2005-01-22 09:17:58 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-10-05 19:57:20 +00:00
|
|
|
/**
|
|
|
|
* Draws a selection rectangle on a tile.
|
|
|
|
*
|
|
|
|
* @param ti TileInfo Tile that is being drawn
|
|
|
|
* @param pal Palette to apply.
|
|
|
|
*/
|
2010-01-21 01:38:13 +00:00
|
|
|
static void DrawTileSelectionRect(const TileInfo *ti, PaletteID pal)
|
2007-10-05 19:57:20 +00:00
|
|
|
{
|
2009-01-19 13:50:56 +00:00
|
|
|
if (!IsValidTile(ti->tile)) return;
|
|
|
|
|
2007-10-20 20:25:43 +00:00
|
|
|
SpriteID sel;
|
|
|
|
if (IsHalftileSlope(ti->tileh)) {
|
|
|
|
Corner halftile_corner = GetHalftileSlopeCorner(ti->tileh);
|
|
|
|
SpriteID sel2 = SPR_HALFTILE_SELECTION_FLAT + halftile_corner;
|
|
|
|
DrawSelectionSprite(sel2, pal, ti, 7 + TILE_HEIGHT, FOUNDATION_PART_HALFTILE);
|
|
|
|
|
|
|
|
Corner opposite_corner = OppositeCorner(halftile_corner);
|
|
|
|
if (IsSteepSlope(ti->tileh)) {
|
|
|
|
sel = SPR_HALFTILE_SELECTION_DOWN;
|
|
|
|
} else {
|
|
|
|
sel = ((ti->tileh & SlopeWithOneCornerRaised(opposite_corner)) != 0 ? SPR_HALFTILE_SELECTION_UP : SPR_HALFTILE_SELECTION_FLAT);
|
|
|
|
}
|
|
|
|
sel += opposite_corner;
|
|
|
|
} else {
|
2010-07-19 17:00:54 +00:00
|
|
|
sel = SPR_SELECT_TILE + SlopeToSpriteOffset(ti->tileh);
|
2007-10-20 20:25:43 +00:00
|
|
|
}
|
|
|
|
DrawSelectionSprite(sel, pal, ti, 7, FOUNDATION_PART_NORMAL);
|
2007-10-05 19:57:20 +00:00
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
static bool IsPartOfAutoLine(int px, int py)
|
|
|
|
{
|
2005-05-27 15:05:54 +00:00
|
|
|
px -= _thd.selstart.x;
|
|
|
|
py -= _thd.selstart.y;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-01-09 09:45:45 +00:00
|
|
|
if ((_thd.drawstyle & ~HT_DIR_MASK) != HT_LINE) return false;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2008-01-09 09:45:45 +00:00
|
|
|
switch (_thd.drawstyle & HT_DIR_MASK) {
|
|
|
|
case HT_DIR_X: return py == 0; // x direction
|
|
|
|
case HT_DIR_Y: return px == 0; // y direction
|
|
|
|
case HT_DIR_HU: return px == -py || px == -py - 16; // horizontal upper
|
|
|
|
case HT_DIR_HL: return px == -py || px == -py + 16; // horizontal lower
|
2010-05-30 13:05:36 +00:00
|
|
|
case HT_DIR_VL: return px == py || px == py + 16; // vertical left
|
2008-01-09 09:45:45 +00:00
|
|
|
case HT_DIR_VR: return px == py || px == py - 16; // vertical right
|
|
|
|
default:
|
|
|
|
NOT_REACHED();
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
/* [direction][side] */
|
2008-01-09 09:45:45 +00:00
|
|
|
static const HighLightStyle _autorail_type[6][2] = {
|
2005-01-19 20:55:23 +00:00
|
|
|
{ HT_DIR_X, HT_DIR_X },
|
|
|
|
{ HT_DIR_Y, HT_DIR_Y },
|
|
|
|
{ HT_DIR_HU, HT_DIR_HL },
|
|
|
|
{ HT_DIR_HL, HT_DIR_HU },
|
|
|
|
{ HT_DIR_VL, HT_DIR_VR },
|
|
|
|
{ HT_DIR_VR, HT_DIR_VL }
|
|
|
|
};
|
|
|
|
|
|
|
|
#include "table/autorail.h"
|
|
|
|
|
2007-06-26 16:58:40 +00:00
|
|
|
/**
|
2007-10-05 19:57:20 +00:00
|
|
|
* Draws autorail highlights.
|
|
|
|
*
|
2007-06-26 16:58:40 +00:00
|
|
|
* @param *ti TileInfo Tile that is being drawn
|
2007-10-05 19:57:20 +00:00
|
|
|
* @param autorail_type Offset into _AutorailTilehSprite[][]
|
2007-06-26 16:58:40 +00:00
|
|
|
*/
|
2007-10-05 19:57:20 +00:00
|
|
|
static void DrawAutorailSelection(const TileInfo *ti, uint autorail_type)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-01-14 19:57:49 +00:00
|
|
|
SpriteID image;
|
2010-01-21 01:38:13 +00:00
|
|
|
PaletteID pal;
|
2007-10-05 19:57:20 +00:00
|
|
|
int offset;
|
|
|
|
|
2007-10-20 20:25:43 +00:00
|
|
|
FoundationPart foundation_part = FOUNDATION_PART_NORMAL;
|
2008-01-25 15:47:58 +00:00
|
|
|
Slope autorail_tileh = RemoveHalftileSlope(ti->tileh);
|
2007-10-20 20:25:43 +00:00
|
|
|
if (IsHalftileSlope(ti->tileh)) {
|
|
|
|
static const uint _lower_rail[4] = { 5U, 2U, 4U, 3U };
|
|
|
|
Corner halftile_corner = GetHalftileSlopeCorner(ti->tileh);
|
|
|
|
if (autorail_type != _lower_rail[halftile_corner]) {
|
|
|
|
foundation_part = FOUNDATION_PART_HALFTILE;
|
|
|
|
/* Here we draw the highlights of the "three-corners-raised"-slope. That looks ok to me. */
|
|
|
|
autorail_tileh = SlopeWithThreeCornersRaised(OppositeCorner(halftile_corner));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
offset = _AutorailTilehSprite[autorail_tileh][autorail_type];
|
2007-10-05 19:57:20 +00:00
|
|
|
if (offset >= 0) {
|
|
|
|
image = SPR_AUTORAIL_BASE + offset;
|
|
|
|
pal = PAL_NONE;
|
|
|
|
} else {
|
|
|
|
image = SPR_AUTORAIL_BASE - offset;
|
|
|
|
pal = PALETTE_SEL_TILE_RED;
|
|
|
|
}
|
|
|
|
|
2007-10-20 20:25:43 +00:00
|
|
|
DrawSelectionSprite(image, _thd.make_square_red ? PALETTE_SEL_TILE_RED : pal, ti, 7, foundation_part);
|
2007-10-05 19:57:20 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-10-05 19:57:20 +00:00
|
|
|
/**
|
|
|
|
* Checks if the specified tile is selected and if so draws selection using correct selectionstyle.
|
|
|
|
* @param *ti TileInfo Tile that is being drawn
|
|
|
|
*/
|
|
|
|
static void DrawTileSelection(const TileInfo *ti)
|
|
|
|
{
|
2007-04-04 04:08:47 +00:00
|
|
|
/* Draw a red error square? */
|
2009-01-19 15:06:11 +00:00
|
|
|
bool is_redsq = _thd.redsq == ti->tile;
|
2008-03-27 14:10:09 +00:00
|
|
|
if (is_redsq) DrawTileSelectionRect(ti, PALETTE_TILE_RED_PULSATING);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-04 04:08:47 +00:00
|
|
|
/* no selection active? */
|
2005-07-17 20:14:58 +00:00
|
|
|
if (_thd.drawstyle == 0) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-04 04:08:47 +00:00
|
|
|
/* Inside the inner area? */
|
2007-11-24 10:38:43 +00:00
|
|
|
if (IsInsideBS(ti->x, _thd.pos.x, _thd.size.x) &&
|
|
|
|
IsInsideBS(ti->y, _thd.pos.y, _thd.size.y)) {
|
2005-05-27 15:05:54 +00:00
|
|
|
if (_thd.drawstyle & HT_RECT) {
|
2008-03-27 14:10:09 +00:00
|
|
|
if (!is_redsq) DrawTileSelectionRect(ti, _thd.make_square_red ? PALETTE_SEL_TILE_RED : PAL_NONE);
|
2005-05-27 15:05:54 +00:00
|
|
|
} else if (_thd.drawstyle & HT_POINT) {
|
2007-04-04 04:08:47 +00:00
|
|
|
/* Figure out the Z coordinate for the single dot. */
|
2007-10-14 19:57:15 +00:00
|
|
|
byte z = 0;
|
2007-10-20 20:25:43 +00:00
|
|
|
FoundationPart foundation_part = FOUNDATION_PART_NORMAL;
|
2006-04-23 13:48:16 +00:00
|
|
|
if (ti->tileh & SLOPE_N) {
|
2006-04-23 19:35:36 +00:00
|
|
|
z += TILE_HEIGHT;
|
2008-01-25 15:47:58 +00:00
|
|
|
if (RemoveHalftileSlope(ti->tileh) == SLOPE_STEEP_N) z += TILE_HEIGHT;
|
2007-10-20 20:25:43 +00:00
|
|
|
}
|
|
|
|
if (IsHalftileSlope(ti->tileh)) {
|
|
|
|
Corner halftile_corner = GetHalftileSlopeCorner(ti->tileh);
|
|
|
|
if ((halftile_corner == CORNER_W) || (halftile_corner == CORNER_E)) z += TILE_HEIGHT;
|
|
|
|
if (halftile_corner != CORNER_S) {
|
|
|
|
foundation_part = FOUNDATION_PART_HALFTILE;
|
|
|
|
if (IsSteepSlope(ti->tileh)) z -= TILE_HEIGHT;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2007-10-20 20:25:43 +00:00
|
|
|
DrawSelectionSprite(_cur_dpi->zoom <= ZOOM_LVL_DETAIL ? SPR_DOT : SPR_DOT_SMALL, PAL_NONE, ti, z, foundation_part);
|
2009-04-19 21:26:06 +00:00
|
|
|
} else if (_thd.drawstyle & HT_RAIL) {
|
2007-04-04 04:08:47 +00:00
|
|
|
/* autorail highlight piece under cursor */
|
2009-04-18 21:10:36 +00:00
|
|
|
HighLightStyle type = _thd.drawstyle & HT_DIR_MASK;
|
|
|
|
assert(type < HT_DIR_END);
|
2008-01-09 09:45:45 +00:00
|
|
|
DrawAutorailSelection(ti, _autorail_type[type][0]);
|
2005-07-17 20:14:58 +00:00
|
|
|
} else if (IsPartOfAutoLine(ti->x, ti->y)) {
|
2007-04-04 04:08:47 +00:00
|
|
|
/* autorail highlighting long line */
|
2009-04-18 21:10:36 +00:00
|
|
|
HighLightStyle dir = _thd.drawstyle & HT_DIR_MASK;
|
2005-07-17 20:14:58 +00:00
|
|
|
uint side;
|
2005-01-23 13:09:35 +00:00
|
|
|
|
2009-04-18 21:10:36 +00:00
|
|
|
if (dir == HT_DIR_X || dir == HT_DIR_Y) {
|
2005-07-17 20:14:58 +00:00
|
|
|
side = 0;
|
|
|
|
} else {
|
|
|
|
TileIndex start = TileVirtXY(_thd.selstart.x, _thd.selstart.y);
|
2007-11-26 16:01:29 +00:00
|
|
|
side = Delta(Delta(TileX(start), TileX(ti->tile)), Delta(TileY(start), TileY(ti->tile)));
|
2005-07-17 20:14:58 +00:00
|
|
|
}
|
2005-01-19 20:55:23 +00:00
|
|
|
|
2008-01-09 09:45:45 +00:00
|
|
|
DrawAutorailSelection(ti, _autorail_type[dir][side]);
|
2005-07-17 20:14:58 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-04-04 04:08:47 +00:00
|
|
|
/* Check if it's inside the outer area? */
|
2008-03-27 14:10:09 +00:00
|
|
|
if (!is_redsq && _thd.outersize.x &&
|
2005-05-27 15:05:54 +00:00
|
|
|
_thd.size.x < _thd.size.x + _thd.outersize.x &&
|
2007-11-24 10:38:43 +00:00
|
|
|
IsInsideBS(ti->x, _thd.pos.x + _thd.offs.x, _thd.size.x + _thd.outersize.x) &&
|
|
|
|
IsInsideBS(ti->y, _thd.pos.y + _thd.offs.y, _thd.size.y + _thd.outersize.y)) {
|
2007-04-04 04:08:47 +00:00
|
|
|
/* Draw a blue rect. */
|
2007-10-05 19:57:20 +00:00
|
|
|
DrawTileSelectionRect(ti, PALETTE_SEL_TILE_BLUE);
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static void ViewportAddLandscape()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
int x, y, width, height;
|
|
|
|
TileInfo ti;
|
|
|
|
bool direction;
|
|
|
|
|
|
|
|
_cur_ti = &ti;
|
|
|
|
|
2007-04-04 04:08:47 +00:00
|
|
|
/* Transform into tile coordinates and round to closest full tile */
|
2009-04-18 21:10:36 +00:00
|
|
|
x = ((_vd.dpi.top >> 1) - (_vd.dpi.left >> 2)) & ~TILE_UNIT_MASK;
|
|
|
|
y = ((_vd.dpi.top >> 1) + (_vd.dpi.left >> 2) - TILE_SIZE) & ~TILE_UNIT_MASK;
|
2007-01-03 09:45:07 +00:00
|
|
|
|
2007-04-04 04:08:47 +00:00
|
|
|
/* determine size of area */
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Point pt = RemapCoords(x, y, 241);
|
2008-04-16 21:06:21 +00:00
|
|
|
width = (_vd.dpi.left + _vd.dpi.width - pt.x + 95) >> 6;
|
|
|
|
height = (_vd.dpi.top + _vd.dpi.height - pt.y) >> 5 << 1;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(width > 0);
|
|
|
|
assert(height > 0);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
direction = false;
|
|
|
|
|
|
|
|
do {
|
|
|
|
int width_cur = width;
|
2010-05-13 11:19:30 +00:00
|
|
|
uint x_cur = x;
|
|
|
|
uint y_cur = y;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
do {
|
2009-01-21 02:31:55 +00:00
|
|
|
TileType tt = MP_VOID;
|
2006-08-07 17:32:29 +00:00
|
|
|
|
|
|
|
ti.x = x_cur;
|
|
|
|
ti.y = y_cur;
|
2009-01-21 02:31:55 +00:00
|
|
|
|
|
|
|
ti.z = 0;
|
|
|
|
|
|
|
|
ti.tileh = SLOPE_FLAT;
|
|
|
|
ti.tile = INVALID_TILE;
|
|
|
|
|
2010-05-13 11:19:30 +00:00
|
|
|
if (x_cur < MapMaxX() * TILE_SIZE &&
|
|
|
|
y_cur < MapMaxY() * TILE_SIZE) {
|
2006-08-07 17:32:29 +00:00
|
|
|
TileIndex tile = TileVirtXY(x_cur, y_cur);
|
|
|
|
|
2009-01-21 02:31:55 +00:00
|
|
|
if (!_settings_game.construction.freeform_edges || (TileX(tile) != 0 && TileY(tile) != 0)) {
|
|
|
|
if (x_cur == ((int)MapMaxX() - 1) * TILE_SIZE || y_cur == ((int)MapMaxY() - 1) * TILE_SIZE) {
|
|
|
|
uint maxh = max<uint>(TileHeight(tile), 1);
|
|
|
|
for (uint h = 0; h < maxh; h++) {
|
2010-01-03 20:55:00 +00:00
|
|
|
AddTileSpriteToDraw(SPR_SHADOW_CELL, PAL_NONE, ti.x, ti.y, h * TILE_HEIGHT);
|
2009-01-21 02:31:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ti.tile = tile;
|
|
|
|
ti.tileh = GetTileSlope(tile, &ti.z);
|
|
|
|
tt = GetTileType(tile);
|
|
|
|
}
|
2006-08-07 17:32:29 +00:00
|
|
|
}
|
|
|
|
|
2008-04-16 21:06:21 +00:00
|
|
|
_vd.foundation_part = FOUNDATION_PART_NONE;
|
|
|
|
_vd.foundation[0] = -1;
|
|
|
|
_vd.foundation[1] = -1;
|
|
|
|
_vd.last_foundation_child[0] = NULL;
|
|
|
|
_vd.last_foundation_child[1] = NULL;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2006-08-07 17:32:29 +00:00
|
|
|
_tile_type_procs[tt]->draw_tile_proc(&ti);
|
2009-01-21 02:31:55 +00:00
|
|
|
|
2009-01-21 22:35:07 +00:00
|
|
|
if ((x_cur == (int)MapMaxX() * TILE_SIZE && IsInsideMM(y_cur, 0, MapMaxY() * TILE_SIZE + 1)) ||
|
2010-07-24 10:14:39 +00:00
|
|
|
(y_cur == (int)MapMaxY() * TILE_SIZE && IsInsideMM(x_cur, 0, MapMaxX() * TILE_SIZE + 1))) {
|
2009-01-21 02:31:55 +00:00
|
|
|
TileIndex tile = TileVirtXY(x_cur, y_cur);
|
|
|
|
ti.tile = tile;
|
|
|
|
ti.tileh = GetTileSlope(tile, &ti.z);
|
|
|
|
tt = GetTileType(tile);
|
|
|
|
}
|
2009-01-19 13:50:56 +00:00
|
|
|
if (ti.tile != INVALID_TILE) DrawTileSelection(&ti);
|
2009-01-21 02:31:55 +00:00
|
|
|
|
|
|
|
y_cur += 0x10;
|
|
|
|
x_cur -= 0x10;
|
2004-08-09 17:04:08 +00:00
|
|
|
} while (--width_cur);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-01-03 09:45:07 +00:00
|
|
|
if ((direction ^= 1) != 0) {
|
2004-08-09 17:04:08 +00:00
|
|
|
y += 0x10;
|
2007-01-03 09:45:07 +00:00
|
|
|
} else {
|
2004-08-09 17:04:08 +00:00
|
|
|
x += 0x10;
|
2007-01-03 09:45:07 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
} while (--height);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-11-22 19:53:49 +00:00
|
|
|
/**
|
|
|
|
* Add a string to draw in the viewport
|
|
|
|
* @param dpi current viewport area
|
2009-12-22 12:50:41 +00:00
|
|
|
* @param small_from Zoomlevel from when the small font should be used
|
2009-11-22 19:53:49 +00:00
|
|
|
* @param sign sign position and dimension
|
|
|
|
* @param string_normal String for normal and 2x zoom level
|
|
|
|
* @param string_small String for 4x and 8x zoom level
|
2010-05-30 13:05:36 +00:00
|
|
|
* @param string_small_shadow Shadow string for 4x and 8x zoom level; or #STR_NULL if no shadow
|
2009-11-22 19:53:49 +00:00
|
|
|
* @param colour colour of the sign background; or 0 if transparent
|
|
|
|
*/
|
2009-12-22 12:50:41 +00:00
|
|
|
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)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-12-22 12:50:41 +00:00
|
|
|
bool small = dpi->zoom >= small_from;
|
|
|
|
|
|
|
|
int left = dpi->left;
|
|
|
|
int top = dpi->top;
|
|
|
|
int right = left + dpi->width;
|
2009-11-22 19:53:49 +00:00
|
|
|
int bottom = top + dpi->height;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-12-22 12:50:41 +00:00
|
|
|
int sign_height = ScaleByZoom(VPSM_TOP + FONT_HEIGHT_NORMAL + VPSM_BOTTOM, dpi->zoom);
|
|
|
|
int sign_half_width = ScaleByZoom((small ? sign->width_small : sign->width_normal) / 2, dpi->zoom);
|
2006-11-05 12:22:46 +00:00
|
|
|
|
2009-12-23 17:59:34 +00:00
|
|
|
if (bottom < sign->top ||
|
|
|
|
top > sign->top + sign_height ||
|
|
|
|
right < sign->center - sign_half_width ||
|
|
|
|
left > sign->center + sign_half_width) {
|
2009-12-22 12:50:41 +00:00
|
|
|
return;
|
|
|
|
}
|
2007-05-19 22:48:04 +00:00
|
|
|
|
2009-12-22 12:50:41 +00:00
|
|
|
if (!small) {
|
|
|
|
AddStringToDraw(sign->center - sign_half_width, sign->top, string_normal, params_1, params_2, colour, sign->width_normal);
|
|
|
|
} else {
|
|
|
|
int shadow_offset = 0;
|
|
|
|
if (string_small_shadow != STR_NULL) {
|
|
|
|
shadow_offset = 4;
|
|
|
|
AddStringToDraw(sign->center - sign_half_width + shadow_offset, sign->top, string_small_shadow, params_1, params_2, INVALID_COLOUR, sign->width_small);
|
|
|
|
}
|
|
|
|
AddStringToDraw(sign->center - sign_half_width, sign->top - shadow_offset, string_small, params_1, params_2,
|
|
|
|
colour, sign->width_small | 0x8000);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-22 19:53:49 +00:00
|
|
|
static void ViewportAddTownNames(DrawPixelInfo *dpi)
|
2006-11-05 11:17:33 +00:00
|
|
|
{
|
2009-11-22 19:53:49 +00:00
|
|
|
if (!HasBit(_display_opt, DO_SHOW_TOWN_NAMES) || _game_mode == GM_MENU) return;
|
|
|
|
|
|
|
|
const Town *t;
|
|
|
|
FOR_ALL_TOWNS(t) {
|
2009-12-22 12:50:41 +00:00
|
|
|
ViewportAddString(dpi, ZOOM_LVL_OUT_4X, &t->sign,
|
2009-11-22 19:53:49 +00:00
|
|
|
_settings_client.gui.population_in_label ? STR_VIEWPORT_TOWN_POP : STR_VIEWPORT_TOWN,
|
|
|
|
STR_VIEWPORT_TOWN_TINY_WHITE, STR_VIEWPORT_TOWN_TINY_BLACK,
|
|
|
|
t->index, t->population);
|
|
|
|
}
|
2006-11-05 11:17:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-11-14 19:44:06 +00:00
|
|
|
static void ViewportAddStationNames(DrawPixelInfo *dpi)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-11-22 19:53:49 +00:00
|
|
|
if (!(HasBit(_display_opt, DO_SHOW_STATION_NAMES) || HasBit(_display_opt, DO_SHOW_WAYPOINT_NAMES)) || _game_mode == GM_MENU) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-07-25 10:56:36 +00:00
|
|
|
const BaseStation *st;
|
2009-11-22 19:53:49 +00:00
|
|
|
FOR_ALL_BASE_STATIONS(st) {
|
|
|
|
/* Check whether the base station is a station or a waypoint */
|
|
|
|
bool is_station = Station::IsExpected(st);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-11-22 19:53:49 +00:00
|
|
|
/* Don't draw if the display options are disabled */
|
|
|
|
if (!HasBit(_display_opt, is_station ? DO_SHOW_STATION_NAMES : DO_SHOW_WAYPOINT_NAMES)) continue;
|
2007-05-19 22:48:04 +00:00
|
|
|
|
2009-12-22 12:50:41 +00:00
|
|
|
ViewportAddString(dpi, ZOOM_LVL_OUT_4X, &st->sign,
|
2009-11-22 19:53:49 +00:00
|
|
|
is_station ? STR_VIEWPORT_STATION : STR_VIEWPORT_WAYPOINT,
|
|
|
|
(is_station ? STR_VIEWPORT_STATION : STR_VIEWPORT_WAYPOINT) + 1, STR_NULL,
|
2009-11-29 19:27:53 +00:00
|
|
|
st->index, st->facilities, (st->owner == OWNER_NONE || !st->IsInUse()) ? COLOUR_GREY : _company_colours[st->owner]);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-11-05 11:17:33 +00:00
|
|
|
|
2004-11-14 19:44:06 +00:00
|
|
|
static void ViewportAddSigns(DrawPixelInfo *dpi)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-04-03 19:55:40 +00:00
|
|
|
/* Signs are turned off or are invisible */
|
|
|
|
if (!HasBit(_display_opt, DO_SHOW_SIGNS) || IsInvisibilitySet(TO_SIGNS)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-11-22 19:53:49 +00:00
|
|
|
const Sign *si;
|
|
|
|
FOR_ALL_SIGNS(si) {
|
2009-12-22 12:50:41 +00:00
|
|
|
ViewportAddString(dpi, ZOOM_LVL_OUT_4X, &si->sign,
|
2009-11-22 19:53:49 +00:00
|
|
|
STR_WHITE_SIGN,
|
|
|
|
IsTransparencySet(TO_SIGNS) ? STR_VIEWPORT_SIGN_SMALL_WHITE : STR_VIEWPORT_SIGN_SMALL_BLACK, STR_NULL,
|
2009-11-29 19:27:53 +00:00
|
|
|
si->index, 0, (si->owner == OWNER_NONE) ? COLOUR_GREY : _company_colours[si->owner]);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-07 16:51:20 +00:00
|
|
|
/**
|
|
|
|
* Update the position of the viewport sign.
|
|
|
|
* @param center the (preferred) center of the viewport sign
|
|
|
|
* @param top the new top of the sign
|
|
|
|
* @param str the string to show in the sign
|
|
|
|
*/
|
|
|
|
void ViewportSign::UpdatePosition(int center, int top, StringID str)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-07-13 23:15:13 +00:00
|
|
|
if (this->width_normal != 0) this->MarkDirty();
|
|
|
|
|
2009-07-07 16:51:20 +00:00
|
|
|
this->top = top;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-07-07 16:51:20 +00:00
|
|
|
char buffer[DRAW_STRING_BUFFER];
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-10-21 23:31:34 +00:00
|
|
|
GetString(buffer, str, lastof(buffer));
|
2009-12-22 12:50:41 +00:00
|
|
|
this->width_normal = VPSM_LEFT + Align(GetStringBoundingBox(buffer).width, 2) + VPSM_RIGHT;
|
|
|
|
this->center = center;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-09-16 13:20:14 +00:00
|
|
|
/* zoomed out version */
|
2010-05-30 15:32:37 +00:00
|
|
|
this->width_small = VPSM_LEFT + Align(GetStringBoundingBox(buffer, FS_SMALL).width, 2) + VPSM_RIGHT;
|
2009-07-13 23:15:13 +00:00
|
|
|
|
|
|
|
this->MarkDirty();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-07-08 08:30:35 +00:00
|
|
|
/**
|
|
|
|
* Mark the sign dirty in all viewports.
|
|
|
|
*
|
|
|
|
* @ingroup dirty
|
|
|
|
*/
|
|
|
|
void ViewportSign::MarkDirty() const
|
|
|
|
{
|
2010-01-01 18:45:40 +00:00
|
|
|
/* We use ZOOM_LVL_MAX here, as every viewport can have another zoom,
|
2009-07-08 08:30:35 +00:00
|
|
|
* and there is no way for us to know which is the biggest. So make the
|
2009-12-22 12:50:41 +00:00
|
|
|
* biggest area dirty, and we are safe for sure.
|
|
|
|
* We also add 1 to make sure the whole thing is redrawn. */
|
2009-07-08 08:30:35 +00:00
|
|
|
MarkAllViewportsDirty(
|
2009-12-22 12:50:41 +00:00
|
|
|
this->center - ScaleByZoom(this->width_normal / 2 + 1, ZOOM_LVL_MAX),
|
|
|
|
this->top - ScaleByZoom(1, ZOOM_LVL_MAX),
|
|
|
|
this->center + ScaleByZoom(this->width_normal / 2 + 1, ZOOM_LVL_MAX),
|
|
|
|
this->top + ScaleByZoom(VPSM_TOP + FONT_HEIGHT_NORMAL + VPSM_BOTTOM + 1, ZOOM_LVL_MAX));
|
2009-07-08 08:30:35 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-16 14:18:15 +00:00
|
|
|
static void ViewportDrawTileSprites(const TileSpriteToDrawVector *tstdv)
|
2004-09-10 19:02:27 +00:00
|
|
|
{
|
2008-04-16 19:01:09 +00:00
|
|
|
const TileSpriteToDraw *tsend = tstdv->End();
|
|
|
|
for (const TileSpriteToDraw *ts = tstdv->Begin(); ts != tsend; ++ts) {
|
2009-06-07 13:25:21 +00:00
|
|
|
DrawSprite(ts->image, ts->pal, ts->x, ts->y, ts->sub);
|
2008-04-16 14:18:15 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-10-13 03:26:48 +00:00
|
|
|
/** Sort parent sprites pointer array */
|
2008-04-16 20:01:04 +00:00
|
|
|
static void ViewportSortParentSprites(ParentSpriteToSortVector *psdv)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-04-16 20:01:04 +00:00
|
|
|
ParentSpriteToDraw **psdvend = psdv->End();
|
|
|
|
ParentSpriteToDraw **psd = psdv->Begin();
|
|
|
|
while (psd != psdvend) {
|
2008-04-16 18:28:05 +00:00
|
|
|
ParentSpriteToDraw *ps = *psd;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-16 19:36:30 +00:00
|
|
|
if (ps->comparison_done) {
|
|
|
|
psd++;
|
|
|
|
continue;
|
|
|
|
}
|
2005-07-17 20:14:58 +00:00
|
|
|
|
2008-04-16 18:28:05 +00:00
|
|
|
ps->comparison_done = true;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2008-04-16 20:01:04 +00:00
|
|
|
for (ParentSpriteToDraw **psd2 = psd + 1; psd2 != psdvend; psd2++) {
|
2008-04-16 18:28:05 +00:00
|
|
|
ParentSpriteToDraw *ps2 = *psd2;
|
2005-06-15 14:04:48 +00:00
|
|
|
|
2008-04-16 18:28:05 +00:00
|
|
|
if (ps2->comparison_done) continue;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-16 18:28:05 +00:00
|
|
|
/* Decide which comparator to use, based on whether the bounding
|
|
|
|
* boxes overlap
|
|
|
|
*/
|
|
|
|
if (ps->xmax >= ps2->xmin && ps->xmin <= ps2->xmax && // overlap in X?
|
|
|
|
ps->ymax >= ps2->ymin && ps->ymin <= ps2->ymax && // overlap in Y?
|
|
|
|
ps->zmax >= ps2->zmin && ps->zmin <= ps2->zmax) { // overlap in Z?
|
|
|
|
/* Use X+Y+Z as the sorting order, so sprites closer to the bottom of
|
|
|
|
* the screen and with higher Z elevation, are drawn in front.
|
|
|
|
* Here X,Y,Z are the coordinates of the "center of mass" of the sprite,
|
|
|
|
* i.e. X=(left+right)/2, etc.
|
|
|
|
* However, since we only care about order, don't actually divide / 2
|
|
|
|
*/
|
|
|
|
if (ps->xmin + ps->xmax + ps->ymin + ps->ymax + ps->zmin + ps->zmax <=
|
|
|
|
ps2->xmin + ps2->xmax + ps2->ymin + ps2->ymax + ps2->zmin + ps2->zmax) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* We only change the order, if it is definite.
|
|
|
|
* I.e. every single order of X, Y, Z says ps2 is behind ps or they overlap.
|
|
|
|
* That is: If one partial order says ps behind ps2, do not change the order.
|
2006-07-29 10:18:59 +00:00
|
|
|
*/
|
2008-04-16 18:28:05 +00:00
|
|
|
if (ps->xmax < ps2->xmin ||
|
|
|
|
ps->ymax < ps2->ymin ||
|
|
|
|
ps->zmax < ps2->zmin) {
|
|
|
|
continue;
|
2005-06-15 14:04:48 +00:00
|
|
|
}
|
2008-04-16 18:28:05 +00:00
|
|
|
}
|
2005-07-17 20:14:58 +00:00
|
|
|
|
2008-09-28 12:38:56 +00:00
|
|
|
/* Move ps2 in front of ps */
|
|
|
|
ParentSpriteToDraw *temp = ps2;
|
|
|
|
for (ParentSpriteToDraw **psd3 = psd2; psd3 > psd; psd3--) {
|
|
|
|
*psd3 = *(psd3 - 1);
|
|
|
|
}
|
|
|
|
*psd = temp;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-16 20:39:35 +00:00
|
|
|
static void ViewportDrawParentSprites(const ParentSpriteToSortVector *psd, const ChildScreenSpriteToDrawVector *csstdv)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-04-16 20:01:04 +00:00
|
|
|
const ParentSpriteToDraw * const *psd_end = psd->End();
|
|
|
|
for (const ParentSpriteToDraw * const *it = psd->Begin(); it != psd_end; it++) {
|
|
|
|
const ParentSpriteToDraw *ps = *it;
|
2007-10-05 21:49:15 +00:00
|
|
|
if (ps->image != SPR_EMPTY_BOUNDING_BOX) DrawSprite(ps->image, ps->pal, ps->x, ps->y, ps->sub);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-06-16 20:08:30 +00:00
|
|
|
int child_idx = ps->first_child;
|
|
|
|
while (child_idx >= 0) {
|
|
|
|
const ChildScreenSpriteToDraw *cs = csstdv->Get(child_idx);
|
|
|
|
child_idx = cs->next;
|
2007-10-05 21:49:15 +00:00
|
|
|
DrawSprite(cs->image, cs->pal, ps->left + cs->x, ps->top + cs->y, cs->sub);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-26 19:27:29 +00:00
|
|
|
/**
|
|
|
|
* Draws the bounding boxes of all ParentSprites
|
|
|
|
* @param psd Array of ParentSprites
|
|
|
|
*/
|
2008-04-16 20:01:04 +00:00
|
|
|
static void ViewportDrawBoundingBoxes(const ParentSpriteToSortVector *psd)
|
2007-09-26 19:27:29 +00:00
|
|
|
{
|
2008-04-16 20:01:04 +00:00
|
|
|
const ParentSpriteToDraw * const *psd_end = psd->End();
|
|
|
|
for (const ParentSpriteToDraw * const *it = psd->Begin(); it != psd_end; it++) {
|
|
|
|
const ParentSpriteToDraw *ps = *it;
|
2007-09-26 19:27:29 +00:00
|
|
|
Point pt1 = RemapCoords(ps->xmax + 1, ps->ymax + 1, ps->zmax + 1); // top front corner
|
|
|
|
Point pt2 = RemapCoords(ps->xmin , ps->ymax + 1, ps->zmax + 1); // top left corner
|
|
|
|
Point pt3 = RemapCoords(ps->xmax + 1, ps->ymin , ps->zmax + 1); // top right corner
|
|
|
|
Point pt4 = RemapCoords(ps->xmax + 1, ps->ymax + 1, ps->zmin ); // bottom front corner
|
|
|
|
|
|
|
|
DrawBox( pt1.x, pt1.y,
|
|
|
|
pt2.x - pt1.x, pt2.y - pt1.y,
|
|
|
|
pt3.x - pt1.x, pt3.y - pt1.y,
|
|
|
|
pt4.x - pt1.x, pt4.y - pt1.y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-16 14:15:00 +00:00
|
|
|
static void ViewportDrawStrings(DrawPixelInfo *dpi, const StringSpriteToDrawVector *sstdv)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
DrawPixelInfo dp;
|
2007-05-15 14:08:39 +00:00
|
|
|
ZoomLevel zoom;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
_cur_dpi = &dp;
|
|
|
|
dp = *dpi;
|
|
|
|
|
2005-07-17 20:14:58 +00:00
|
|
|
zoom = dp.zoom;
|
2007-05-15 14:08:39 +00:00
|
|
|
dp.zoom = ZOOM_LVL_NORMAL;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-05-19 23:52:04 +00:00
|
|
|
dp.left = UnScaleByZoom(dp.left, zoom);
|
|
|
|
dp.top = UnScaleByZoom(dp.top, zoom);
|
|
|
|
dp.width = UnScaleByZoom(dp.width, zoom);
|
|
|
|
dp.height = UnScaleByZoom(dp.height, zoom);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-16 19:01:09 +00:00
|
|
|
const StringSpriteToDraw *ssend = sstdv->End();
|
|
|
|
for (const StringSpriteToDraw *ss = sstdv->Begin(); ss != ssend; ++ss) {
|
2009-09-11 22:21:54 +00:00
|
|
|
TextColour colour = TC_BLACK;
|
|
|
|
bool small = HasBit(ss->width, 15);
|
|
|
|
int w = GB(ss->width, 0, 15);
|
|
|
|
int x = UnScaleByZoom(ss->x, zoom);
|
|
|
|
int y = UnScaleByZoom(ss->y, zoom);
|
2009-12-22 12:50:41 +00:00
|
|
|
int h = VPSM_TOP + (small ? FONT_HEIGHT_SMALL : FONT_HEIGHT_NORMAL) + VPSM_BOTTOM;
|
2009-09-11 22:21:54 +00:00
|
|
|
|
|
|
|
SetDParam(0, ss->params[0]);
|
|
|
|
SetDParam(1, ss->params[1]);
|
2006-11-05 11:17:33 +00:00
|
|
|
|
2009-12-22 12:50:41 +00:00
|
|
|
if (ss->colour != INVALID_COLOUR) {
|
2008-04-03 19:55:40 +00:00
|
|
|
/* Do not draw signs nor station names if they are set invisible */
|
2009-08-05 17:59:21 +00:00
|
|
|
if (IsInvisibilitySet(TO_SIGNS) && ss->string != STR_WHITE_SIGN) continue;
|
2008-04-03 19:55:40 +00:00
|
|
|
|
2009-09-11 22:21:54 +00:00
|
|
|
/* if we didn't draw a rectangle, or if transparant building is on,
|
2009-11-03 17:30:08 +00:00
|
|
|
* draw the text in the colour the rectangle would have */
|
2009-09-11 22:21:54 +00:00
|
|
|
if (IsTransparencySet(TO_SIGNS) && ss->string != STR_WHITE_SIGN) {
|
|
|
|
/* Real colours need the IS_PALETTE_COLOUR flag
|
2009-11-03 17:30:08 +00:00
|
|
|
* otherwise colours from _string_colourmap are assumed. */
|
2009-09-11 22:21:54 +00:00
|
|
|
colour = (TextColour)_colour_gradient[ss->colour][6] | IS_PALETTE_COLOUR;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-09-11 22:21:54 +00:00
|
|
|
/* Draw the rectangle if 'tranparent station signs' is off,
|
|
|
|
* or if we are drawing a general text sign (STR_WHITE_SIGN) */
|
2009-08-05 17:59:21 +00:00
|
|
|
if (!IsTransparencySet(TO_SIGNS) || ss->string == STR_WHITE_SIGN) {
|
2005-07-17 20:14:58 +00:00
|
|
|
DrawFrameRect(
|
2009-12-22 12:50:41 +00:00
|
|
|
x, y, x + w, y + h, ss->colour,
|
2007-11-10 01:17:15 +00:00
|
|
|
IsTransparencySet(TO_SIGNS) ? FR_TRANSPARENT : FR_NONE
|
2005-07-17 20:14:58 +00:00
|
|
|
);
|
2007-04-05 07:49:04 +00:00
|
|
|
}
|
2009-03-22 23:54:36 +00:00
|
|
|
}
|
2009-03-22 09:43:00 +00:00
|
|
|
|
2010-07-02 13:53:05 +00:00
|
|
|
DrawString(x + VPSM_LEFT, x + w - 1 - VPSM_RIGHT, y + VPSM_TOP, ss->string, colour, SA_HOR_CENTER);
|
2008-04-16 14:18:15 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2004-11-15 19:25:59 +00:00
|
|
|
void ViewportDoDraw(const ViewPort *vp, int left, int top, int right, int bottom)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-04-16 21:06:21 +00:00
|
|
|
DrawPixelInfo *old_dpi = _cur_dpi;
|
|
|
|
_cur_dpi = &_vd.dpi;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-16 21:06:21 +00:00
|
|
|
_vd.dpi.zoom = vp->zoom;
|
|
|
|
int mask = ScaleByZoom(-1, vp->zoom);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-07-29 20:27:24 +00:00
|
|
|
_vd.combine_sprites = SPRITE_COMBINE_NONE;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-16 21:06:21 +00:00
|
|
|
_vd.dpi.width = (right - left) & mask;
|
|
|
|
_vd.dpi.height = (bottom - top) & mask;
|
|
|
|
_vd.dpi.left = left & mask;
|
|
|
|
_vd.dpi.top = top & mask;
|
|
|
|
_vd.dpi.pitch = old_dpi->pitch;
|
|
|
|
_vd.last_child = NULL;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-16 21:06:21 +00:00
|
|
|
int x = UnScaleByZoom(_vd.dpi.left - (vp->virtual_left & mask), vp->zoom) + vp->left;
|
|
|
|
int y = UnScaleByZoom(_vd.dpi.top - (vp->virtual_top & mask), vp->zoom) + vp->top;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-16 21:06:21 +00:00
|
|
|
_vd.dpi.dst_ptr = BlitterFactoryBase::GetCurrentBlitter()->MoveTo(old_dpi->dst_ptr, x - old_dpi->left, y - old_dpi->top);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
ViewportAddLandscape();
|
2008-04-16 21:06:21 +00:00
|
|
|
ViewportAddVehicles(&_vd.dpi);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-16 21:06:21 +00:00
|
|
|
ViewportAddTownNames(&_vd.dpi);
|
|
|
|
ViewportAddStationNames(&_vd.dpi);
|
|
|
|
ViewportAddSigns(&_vd.dpi);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-08-04 14:40:50 +00:00
|
|
|
DrawTextEffects(&_vd.dpi);
|
|
|
|
|
2008-06-19 09:32:25 +00:00
|
|
|
if (_vd.tile_sprites_to_draw.Length() != 0) ViewportDrawTileSprites(&_vd.tile_sprites_to_draw);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-16 21:06:21 +00:00
|
|
|
ParentSpriteToDraw *psd_end = _vd.parent_sprites_to_draw.End();
|
|
|
|
for (ParentSpriteToDraw *it = _vd.parent_sprites_to_draw.Begin(); it != psd_end; it++) {
|
|
|
|
*_vd.parent_sprites_to_sort.Append() = it;
|
2008-04-16 20:01:04 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-16 21:06:21 +00:00
|
|
|
ViewportSortParentSprites(&_vd.parent_sprites_to_sort);
|
|
|
|
ViewportDrawParentSprites(&_vd.parent_sprites_to_sort, &_vd.child_screen_sprites_to_draw);
|
2008-02-14 15:13:36 +00:00
|
|
|
|
2008-04-16 21:06:21 +00:00
|
|
|
if (_draw_bounding_boxes) ViewportDrawBoundingBoxes(&_vd.parent_sprites_to_sort);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2008-06-19 09:32:25 +00:00
|
|
|
if (_vd.string_sprites_to_draw.Length() != 0) ViewportDrawStrings(&_vd.dpi, &_vd.string_sprites_to_draw);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
_cur_dpi = old_dpi;
|
2008-04-16 21:06:21 +00:00
|
|
|
|
2008-06-19 09:32:25 +00:00
|
|
|
_vd.string_sprites_to_draw.Clear();
|
|
|
|
_vd.tile_sprites_to_draw.Clear();
|
|
|
|
_vd.parent_sprites_to_draw.Clear();
|
|
|
|
_vd.parent_sprites_to_sort.Clear();
|
|
|
|
_vd.child_screen_sprites_to_draw.Clear();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Make sure we don't draw a too big area at a time.
|
2010-08-01 19:44:49 +00:00
|
|
|
* If we do, the sprite memory will overflow.
|
|
|
|
*/
|
2006-11-18 00:14:43 +00:00
|
|
|
static void ViewportDrawChk(const ViewPort *vp, int left, int top, int right, int bottom)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-05-15 16:08:46 +00:00
|
|
|
if (ScaleByZoom(bottom - top, vp->zoom) * ScaleByZoom(right - left, vp->zoom) > 180000) {
|
2004-08-09 17:04:08 +00:00
|
|
|
if ((bottom - top) > (right - left)) {
|
|
|
|
int t = (top + bottom) >> 1;
|
|
|
|
ViewportDrawChk(vp, left, top, right, t);
|
|
|
|
ViewportDrawChk(vp, left, t, right, bottom);
|
|
|
|
} else {
|
|
|
|
int t = (left + right) >> 1;
|
|
|
|
ViewportDrawChk(vp, left, top, t, bottom);
|
|
|
|
ViewportDrawChk(vp, t, top, right, bottom);
|
|
|
|
}
|
|
|
|
} else {
|
2004-09-10 19:02:27 +00:00
|
|
|
ViewportDoDraw(vp,
|
2007-05-15 16:08:46 +00:00
|
|
|
ScaleByZoom(left - vp->left, vp->zoom) + vp->virtual_left,
|
|
|
|
ScaleByZoom(top - vp->top, vp->zoom) + vp->virtual_top,
|
|
|
|
ScaleByZoom(right - vp->left, vp->zoom) + vp->virtual_left,
|
|
|
|
ScaleByZoom(bottom - vp->top, vp->zoom) + vp->virtual_top
|
2004-08-09 17:04:08 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-11-18 00:14:43 +00:00
|
|
|
static inline void ViewportDraw(const ViewPort *vp, int left, int top, int right, int bottom)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-07-17 20:14:58 +00:00
|
|
|
if (right <= vp->left || bottom <= vp->top) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-07-17 20:14:58 +00:00
|
|
|
if (left >= vp->left + vp->width) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (left < vp->left) left = vp->left;
|
2005-07-17 20:14:58 +00:00
|
|
|
if (right > vp->left + vp->width) right = vp->left + vp->width;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-07-17 20:14:58 +00:00
|
|
|
if (top >= vp->top + vp->height) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (top < vp->top) top = vp->top;
|
2005-07-17 20:14:58 +00:00
|
|
|
if (bottom > vp->top + vp->height) bottom = vp->top + vp->height;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
ViewportDrawChk(vp, left, top, right, bottom);
|
|
|
|
}
|
|
|
|
|
2008-10-14 19:27:08 +00:00
|
|
|
/**
|
|
|
|
* Draw the viewport of this window.
|
|
|
|
*/
|
2008-05-17 12:48:06 +00:00
|
|
|
void Window::DrawViewport() const
|
2005-07-17 20:14:58 +00:00
|
|
|
{
|
2004-08-09 17:04:08 +00:00
|
|
|
DrawPixelInfo *dpi = _cur_dpi;
|
|
|
|
|
2008-05-17 12:48:06 +00:00
|
|
|
dpi->left += this->left;
|
|
|
|
dpi->top += this->top;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-05-17 12:48:06 +00:00
|
|
|
ViewportDraw(this->viewport, dpi->left, dpi->top, dpi->left + dpi->width, dpi->top + dpi->height);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-05-17 12:48:06 +00:00
|
|
|
dpi->left -= this->left;
|
|
|
|
dpi->top -= this->top;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-06-25 21:50:36 +00:00
|
|
|
static inline void ClampViewportToMap(const ViewPort *vp, int &x, int &y)
|
|
|
|
{
|
|
|
|
/* Centre of the viewport is hot spot */
|
|
|
|
x += vp->virtual_width / 2;
|
|
|
|
y += vp->virtual_height / 2;
|
|
|
|
|
|
|
|
/* Convert viewport coordinates to map coordinates
|
|
|
|
* Calculation is scaled by 4 to avoid rounding errors */
|
|
|
|
int vx = -x + y * 2;
|
|
|
|
int vy = x + y * 2;
|
|
|
|
|
|
|
|
/* clamp to size of map */
|
2007-11-19 18:38:10 +00:00
|
|
|
vx = Clamp(vx, 0, MapMaxX() * TILE_SIZE * 4);
|
|
|
|
vy = Clamp(vy, 0, MapMaxY() * TILE_SIZE * 4);
|
2007-06-25 21:50:36 +00:00
|
|
|
|
|
|
|
/* Convert map coordinates to viewport coordinates */
|
|
|
|
x = (-vx + vy) / 2;
|
|
|
|
y = ( vx + vy) / 4;
|
|
|
|
|
2010-05-30 13:05:36 +00:00
|
|
|
/* Remove centering */
|
2007-06-25 21:50:36 +00:00
|
|
|
x -= vp->virtual_width / 2;
|
|
|
|
y -= vp->virtual_height / 2;
|
|
|
|
}
|
|
|
|
|
2009-07-26 12:49:26 +00:00
|
|
|
/**
|
|
|
|
* Update the viewport position being displayed.
|
|
|
|
* @param w %Window owning the viewport.
|
|
|
|
*/
|
2004-08-09 17:04:08 +00:00
|
|
|
void UpdateViewportPosition(Window *w)
|
|
|
|
{
|
2005-07-17 20:14:58 +00:00
|
|
|
const ViewPort *vp = w->viewport;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-05-11 15:08:44 +00:00
|
|
|
if (w->viewport->follow_vehicle != INVALID_VEHICLE) {
|
2009-05-16 23:34:14 +00:00
|
|
|
const Vehicle *veh = Vehicle::Get(w->viewport->follow_vehicle);
|
2005-07-17 20:14:58 +00:00
|
|
|
Point pt = MapXYZToViewport(vp, veh->x_pos, veh->y_pos, veh->z_pos);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-01-09 23:30:02 +00:00
|
|
|
w->viewport->scrollpos_x = pt.x;
|
|
|
|
w->viewport->scrollpos_y = pt.y;
|
2004-08-09 17:04:08 +00:00
|
|
|
SetViewportPosition(w, pt.x, pt.y);
|
|
|
|
} else {
|
2007-06-25 21:50:36 +00:00
|
|
|
/* Ensure the destination location is within the map */
|
2008-05-11 15:08:44 +00:00
|
|
|
ClampViewportToMap(vp, w->viewport->dest_scrollpos_x, w->viewport->dest_scrollpos_y);
|
2004-12-13 18:33:47 +00:00
|
|
|
|
2008-05-11 15:08:44 +00:00
|
|
|
int delta_x = w->viewport->dest_scrollpos_x - w->viewport->scrollpos_x;
|
|
|
|
int delta_y = w->viewport->dest_scrollpos_y - w->viewport->scrollpos_y;
|
2007-05-28 16:46:16 +00:00
|
|
|
|
|
|
|
if (delta_x != 0 || delta_y != 0) {
|
2008-05-29 15:13:28 +00:00
|
|
|
if (_settings_client.gui.smooth_scroll) {
|
2007-05-28 17:07:19 +00:00
|
|
|
int max_scroll = ScaleByMapSize1D(512);
|
2010-05-30 13:05:36 +00:00
|
|
|
/* Not at our desired position yet... */
|
2008-05-11 15:08:44 +00:00
|
|
|
w->viewport->scrollpos_x += Clamp(delta_x / 4, -max_scroll, max_scroll);
|
|
|
|
w->viewport->scrollpos_y += Clamp(delta_y / 4, -max_scroll, max_scroll);
|
2007-05-28 17:07:19 +00:00
|
|
|
} else {
|
2008-05-11 15:08:44 +00:00
|
|
|
w->viewport->scrollpos_x = w->viewport->dest_scrollpos_x;
|
|
|
|
w->viewport->scrollpos_y = w->viewport->dest_scrollpos_y;
|
2007-05-28 17:07:19 +00:00
|
|
|
}
|
2007-05-28 16:46:16 +00:00
|
|
|
}
|
|
|
|
|
2008-05-11 15:08:44 +00:00
|
|
|
ClampViewportToMap(vp, w->viewport->scrollpos_x, w->viewport->scrollpos_y);
|
2008-02-14 15:13:36 +00:00
|
|
|
|
2008-05-11 15:08:44 +00:00
|
|
|
SetViewportPosition(w, w->viewport->scrollpos_x, w->viewport->scrollpos_y);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-09 10:13:17 +00:00
|
|
|
/**
|
2008-04-19 13:05:05 +00:00
|
|
|
* Marks a viewport as dirty for repaint if it displays (a part of) the area the needs to be repainted.
|
|
|
|
* @param vp The viewport to mark as dirty
|
|
|
|
* @param left Left edge of area to repaint
|
|
|
|
* @param top Top edge of area to repaint
|
|
|
|
* @param right Right edge of area to repaint
|
|
|
|
* @param bottom Bottom edge of area to repaint
|
2007-09-09 10:13:17 +00:00
|
|
|
* @ingroup dirty
|
|
|
|
*/
|
2005-07-17 20:14:58 +00:00
|
|
|
static void MarkViewportDirty(const ViewPort *vp, int left, int top, int right, int bottom)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-07-17 20:14:58 +00:00
|
|
|
right -= vp->virtual_left;
|
|
|
|
if (right <= 0) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-07-17 20:14:58 +00:00
|
|
|
bottom -= vp->virtual_top;
|
|
|
|
if (bottom <= 0) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-07-17 20:14:58 +00:00
|
|
|
left = max(0, left - vp->virtual_left);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-07-17 20:14:58 +00:00
|
|
|
if (left >= vp->virtual_width) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-07-17 20:14:58 +00:00
|
|
|
top = max(0, top - vp->virtual_top);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-07-17 20:14:58 +00:00
|
|
|
if (top >= vp->virtual_height) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
SetDirtyBlocks(
|
2009-01-03 15:03:28 +00:00
|
|
|
UnScaleByZoomLower(left, vp->zoom) + vp->left,
|
|
|
|
UnScaleByZoomLower(top, vp->zoom) + vp->top,
|
|
|
|
UnScaleByZoom(right, vp->zoom) + vp->left + 1,
|
|
|
|
UnScaleByZoom(bottom, vp->zoom) + vp->top + 1
|
2004-08-09 17:04:08 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2008-04-19 13:05:05 +00:00
|
|
|
/**
|
|
|
|
* Mark all viewports that display an area as dirty (in need of repaint).
|
|
|
|
* @param left Left edge of area to repaint
|
|
|
|
* @param top Top edge of area to repaint
|
|
|
|
* @param right Right edge of area to repaint
|
|
|
|
* @param bottom Bottom edge of area to repaint
|
|
|
|
* @ingroup dirty
|
|
|
|
*/
|
2004-08-09 17:04:08 +00:00
|
|
|
void MarkAllViewportsDirty(int left, int top, int right, int bottom)
|
|
|
|
{
|
2009-01-06 22:37:42 +00:00
|
|
|
Window *w;
|
|
|
|
FOR_ALL_WINDOWS_FROM_BACK(w) {
|
|
|
|
ViewPort *vp = w->viewport;
|
2008-04-17 09:42:44 +00:00
|
|
|
if (vp != NULL) {
|
2004-08-09 17:04:08 +00:00
|
|
|
assert(vp->width != 0);
|
|
|
|
MarkViewportDirty(vp, left, top, right, bottom);
|
|
|
|
}
|
2008-04-17 09:42:44 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-07-17 20:14:58 +00:00
|
|
|
void MarkTileDirtyByTile(TileIndex tile)
|
|
|
|
{
|
2006-04-03 05:32:11 +00:00
|
|
|
Point pt = RemapCoords(TileX(tile) * TILE_SIZE, TileY(tile) * TILE_SIZE, GetTileZ(tile));
|
2004-08-09 17:04:08 +00:00
|
|
|
MarkAllViewportsDirty(
|
|
|
|
pt.x - 31,
|
|
|
|
pt.y - 122,
|
|
|
|
pt.x - 31 + 67,
|
|
|
|
pt.y - 122 + 154
|
|
|
|
);
|
2004-09-10 19:02:27 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-09-09 10:13:17 +00:00
|
|
|
/**
|
|
|
|
* Marks the selected tiles as dirty.
|
|
|
|
*
|
|
|
|
* This function marks the selected tiles as dirty for repaint
|
|
|
|
*
|
|
|
|
* @ingroup dirty
|
|
|
|
*/
|
2007-03-07 11:47:46 +00:00
|
|
|
static void SetSelectionTilesDirty()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-04-25 22:04:13 +00:00
|
|
|
int x_start = _thd.pos.x;
|
|
|
|
int y_start = _thd.pos.y;
|
|
|
|
|
|
|
|
int x_size = _thd.size.x;
|
|
|
|
int y_size = _thd.size.y;
|
|
|
|
|
|
|
|
if (_thd.outersize.x != 0) {
|
|
|
|
x_size += _thd.outersize.x;
|
|
|
|
x_start += _thd.offs.x;
|
|
|
|
y_size += _thd.outersize.y;
|
|
|
|
y_start += _thd.offs.y;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-04-25 22:04:13 +00:00
|
|
|
x_size -= TILE_SIZE;
|
|
|
|
y_size -= TILE_SIZE;
|
|
|
|
|
|
|
|
assert(x_size >= 0);
|
|
|
|
assert(y_size >= 0);
|
|
|
|
|
|
|
|
int x_end = Clamp(x_start + x_size, 0, MapSizeX() * TILE_SIZE - TILE_SIZE);
|
|
|
|
int y_end = Clamp(y_start + y_size, 0, MapSizeY() * TILE_SIZE - TILE_SIZE);
|
|
|
|
|
|
|
|
x_start = Clamp(x_start, 0, MapSizeX() * TILE_SIZE - TILE_SIZE);
|
|
|
|
y_start = Clamp(y_start, 0, MapSizeY() * TILE_SIZE - TILE_SIZE);
|
|
|
|
|
|
|
|
/* make sure everything is multiple of TILE_SIZE */
|
|
|
|
assert((x_end | y_end | x_start | y_start) % TILE_SIZE == 0);
|
|
|
|
|
|
|
|
/* How it works:
|
|
|
|
* Suppose we have to mark dirty rectangle of 3x4 tiles:
|
|
|
|
* x
|
|
|
|
* xxx
|
|
|
|
* xxxxx
|
|
|
|
* xxxxx
|
|
|
|
* xxx
|
|
|
|
* x
|
|
|
|
* This algorithm marks dirty columns of tiles, so it is done in 3+4-1 steps:
|
|
|
|
* 1) x 2) x
|
|
|
|
* xxx Oxx
|
|
|
|
* Oxxxx xOxxx
|
|
|
|
* xxxxx Oxxxx
|
|
|
|
* xxx xxx
|
|
|
|
* x x
|
|
|
|
* And so forth...
|
|
|
|
*/
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-04-25 22:04:13 +00:00
|
|
|
int top_x = x_end; // coordinates of top dirty tile
|
|
|
|
int top_y = y_start;
|
|
|
|
int bot_x = top_x; // coordinates of bottom dirty tile
|
|
|
|
int bot_y = top_y;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
do {
|
2009-04-25 22:04:13 +00:00
|
|
|
Point top = RemapCoords2(top_x, top_y); // topmost dirty point
|
|
|
|
Point bot = RemapCoords2(bot_x + TILE_SIZE - 1, bot_y + TILE_SIZE - 1); // bottommost point
|
|
|
|
|
|
|
|
/* the 'x' coordinate of 'top' and 'bot' is the same (and always in the same distance from tile middle),
|
|
|
|
* tile height/slope affects only the 'y' on-screen coordinate! */
|
|
|
|
|
|
|
|
int l = top.x - (TILE_PIXELS - 2); // 'x' coordinate of left side of dirty rectangle
|
|
|
|
int t = top.y; // 'y' coordinate of top side -//-
|
|
|
|
int r = top.x + (TILE_PIXELS - 2); // right side of dirty rectangle
|
|
|
|
int b = bot.y; // bottom -//-
|
|
|
|
|
|
|
|
static const int OVERLAY_WIDTH = 4; // part of selection sprites is drawn outside the selected area
|
|
|
|
|
2009-06-08 15:44:11 +00:00
|
|
|
/* For halftile foundations on SLOPE_STEEP_S the sprite extents some more towards the top */
|
2009-06-08 15:42:35 +00:00
|
|
|
MarkAllViewportsDirty(l - OVERLAY_WIDTH, t - OVERLAY_WIDTH - TILE_HEIGHT, r + OVERLAY_WIDTH, b + OVERLAY_WIDTH);
|
2009-04-25 22:04:13 +00:00
|
|
|
|
|
|
|
/* haven't we reached the topmost tile yet? */
|
|
|
|
if (top_x != x_start) {
|
|
|
|
top_x -= TILE_SIZE;
|
|
|
|
} else {
|
|
|
|
top_y += TILE_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* the way the bottom tile changes is different when we reach the bottommost tile */
|
|
|
|
if (bot_y != y_end) {
|
|
|
|
bot_y += TILE_SIZE;
|
|
|
|
} else {
|
|
|
|
bot_x -= TILE_SIZE;
|
|
|
|
}
|
|
|
|
} while (bot_x >= top_x);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-29 08:14:09 +00:00
|
|
|
void SetSelectionRed(bool b)
|
|
|
|
{
|
|
|
|
_thd.make_square_red = b;
|
|
|
|
SetSelectionTilesDirty();
|
|
|
|
}
|
|
|
|
|
2009-11-22 20:38:06 +00:00
|
|
|
/**
|
|
|
|
* Test whether a sign is below the mouse
|
|
|
|
* @param vp the clicked viewport
|
|
|
|
* @param x X position of click
|
|
|
|
* @param y Y position of click
|
|
|
|
* @param sign the sign to check
|
|
|
|
* @return true if the sign was hit
|
|
|
|
*/
|
|
|
|
static bool CheckClickOnViewportSign(const ViewPort *vp, int x, int y, const ViewportSign *sign)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-12-22 12:50:41 +00:00
|
|
|
bool small = (vp->zoom >= ZOOM_LVL_OUT_4X);
|
|
|
|
int sign_half_width = ScaleByZoom((small ? sign->width_small : sign->width_normal) / 2, vp->zoom);
|
|
|
|
int sign_height = ScaleByZoom(VPSM_TOP + (small ? FONT_HEIGHT_SMALL : FONT_HEIGHT_NORMAL) + VPSM_BOTTOM, vp->zoom);
|
|
|
|
|
|
|
|
x = ScaleByZoom(x - vp->left, vp->zoom) + vp->virtual_left;
|
|
|
|
y = ScaleByZoom(y - vp->top, vp->zoom) + vp->virtual_top;
|
|
|
|
|
2010-11-20 15:44:24 +00:00
|
|
|
return y >= sign->top && y < sign->top + sign_height &&
|
|
|
|
x >= sign->center - sign_half_width && x < sign->center + sign_half_width;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-11-22 20:38:06 +00:00
|
|
|
static bool CheckClickOnTown(const ViewPort *vp, int x, int y)
|
2009-07-25 10:56:36 +00:00
|
|
|
{
|
2009-11-22 20:38:06 +00:00
|
|
|
if (!HasBit(_display_opt, DO_SHOW_TOWN_NAMES)) return false;
|
2009-07-25 10:56:36 +00:00
|
|
|
|
2009-11-22 20:38:06 +00:00
|
|
|
const Town *t;
|
|
|
|
FOR_ALL_TOWNS(t) {
|
|
|
|
if (CheckClickOnViewportSign(vp, x, y, &t->sign)) {
|
|
|
|
ShowTownViewWindow(t->index);
|
|
|
|
return true;
|
|
|
|
}
|
2009-07-25 10:56:36 +00:00
|
|
|
}
|
2009-11-22 20:38:06 +00:00
|
|
|
|
|
|
|
return false;
|
2009-07-25 10:56:36 +00:00
|
|
|
}
|
2006-08-31 07:49:34 +00:00
|
|
|
|
2005-07-17 20:14:58 +00:00
|
|
|
static bool CheckClickOnStation(const ViewPort *vp, int x, int y)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-11-22 20:38:06 +00:00
|
|
|
if (!(HasBit(_display_opt, DO_SHOW_STATION_NAMES) || HasBit(_display_opt, DO_SHOW_WAYPOINT_NAMES)) || IsInvisibilitySet(TO_SIGNS)) return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-07-25 10:56:36 +00:00
|
|
|
const BaseStation *st;
|
2009-11-22 20:38:06 +00:00
|
|
|
FOR_ALL_BASE_STATIONS(st) {
|
|
|
|
/* Check whether the base station is a station or a waypoint */
|
|
|
|
bool is_station = Station::IsExpected(st);
|
2006-08-31 07:49:34 +00:00
|
|
|
|
2009-11-22 20:38:06 +00:00
|
|
|
/* Don't check if the display options are disabled */
|
|
|
|
if (!HasBit(_display_opt, is_station ? DO_SHOW_STATION_NAMES : DO_SHOW_WAYPOINT_NAMES)) continue;
|
2007-05-19 22:48:04 +00:00
|
|
|
|
2009-11-22 20:38:06 +00:00
|
|
|
if (CheckClickOnViewportSign(vp, x, y, &st->sign)) {
|
|
|
|
if (is_station) {
|
|
|
|
ShowStationViewWindow(st->index);
|
|
|
|
} else {
|
|
|
|
ShowWaypointWindow(Waypoint::From(st));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2009-11-22 20:38:06 +00:00
|
|
|
return true;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-11-22 20:38:06 +00:00
|
|
|
return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-08-31 07:49:34 +00:00
|
|
|
|
2005-07-17 20:14:58 +00:00
|
|
|
static bool CheckClickOnSign(const ViewPort *vp, int x, int y)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-09-30 20:39:50 +00:00
|
|
|
/* Signs are turned off, or they are transparent and invisibility is ON, or company is a spectator */
|
2010-05-01 14:20:56 +00:00
|
|
|
if (!HasBit(_display_opt, DO_SHOW_SIGNS) || IsInvisibilitySet(TO_SIGNS) || _local_company == COMPANY_SPECTATOR) return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-11-22 20:38:06 +00:00
|
|
|
const Sign *si;
|
|
|
|
FOR_ALL_SIGNS(si) {
|
|
|
|
if (CheckClickOnViewportSign(vp, x, y, &si->sign)) {
|
|
|
|
HandleClickOnSign(si);
|
|
|
|
return true;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-08-31 07:49:34 +00:00
|
|
|
|
2009-01-02 22:42:05 +00:00
|
|
|
static bool CheckClickOnLandscape(const ViewPort *vp, int x, int y)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-07-17 20:14:58 +00:00
|
|
|
Point pt = TranslateXYToTileCoord(vp, x, y);
|
2005-06-24 12:38:35 +00:00
|
|
|
|
2009-01-02 22:42:05 +00:00
|
|
|
if (pt.x != -1) return ClickTile(TileVirtXY(pt.x, pt.y));
|
|
|
|
return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-09-07 11:05:22 +00:00
|
|
|
static void PlaceObject()
|
|
|
|
{
|
|
|
|
Point pt;
|
|
|
|
Window *w;
|
|
|
|
|
|
|
|
pt = GetTileBelowCursor();
|
|
|
|
if (pt.x == -1) return;
|
|
|
|
|
|
|
|
if (_thd.place_mode == HT_POINT) {
|
|
|
|
pt.x += 8;
|
|
|
|
pt.y += 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
_tile_fract_coords.x = pt.x & TILE_UNIT_MASK;
|
|
|
|
_tile_fract_coords.y = pt.y & TILE_UNIT_MASK;
|
|
|
|
|
|
|
|
w = GetCallbackWnd();
|
|
|
|
if (w != NULL) w->OnPlaceObject(pt, TileVirtXY(pt.x, pt.y));
|
|
|
|
}
|
|
|
|
|
2005-11-16 12:52:01 +00:00
|
|
|
|
2009-01-02 22:42:05 +00:00
|
|
|
bool HandleViewportClicked(const ViewPort *vp, int x, int y)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2010-09-06 14:14:09 +00:00
|
|
|
const Vehicle *v = CheckClickOnVehicle(vp, x, y);
|
|
|
|
|
|
|
|
if (_thd.place_mode & HT_VEHICLE) {
|
|
|
|
if (v != NULL && VehicleClicked(v)) return true;
|
|
|
|
}
|
|
|
|
|
2010-09-07 11:00:42 +00:00
|
|
|
if (_thd.place_mode & HT_DRAG_MASK) {
|
|
|
|
PlaceObject();
|
|
|
|
return true;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-01-02 22:42:05 +00:00
|
|
|
if (CheckClickOnTown(vp, x, y)) return true;
|
|
|
|
if (CheckClickOnStation(vp, x, y)) return true;
|
|
|
|
if (CheckClickOnSign(vp, x, y)) return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
CheckClickOnLandscape(vp, x, y);
|
|
|
|
|
2006-05-11 05:32:26 +00:00
|
|
|
if (v != NULL) {
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(misc, 2, "Vehicle %d (index %d) at %p", v->unitnumber, v->index, v);
|
2010-04-24 20:55:51 +00:00
|
|
|
if (IsCompanyBuildableVehicleType(v)) {
|
|
|
|
v = v->First();
|
|
|
|
if (_ctrl_pressed && v->owner == _local_company) {
|
|
|
|
StartStopVehicle(v, true);
|
|
|
|
} else {
|
|
|
|
ShowVehicleViewWindow(v);
|
|
|
|
}
|
|
|
|
}
|
2009-01-02 22:42:05 +00:00
|
|
|
return true;
|
2006-05-11 05:32:26 +00:00
|
|
|
}
|
2009-01-02 22:42:05 +00:00
|
|
|
return CheckClickOnLandscape(vp, x, y);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2004-09-03 19:59:05 +00:00
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Scrolls the viewport in a window to a given location.
|
2009-07-26 12:49:26 +00:00
|
|
|
* @param x Desired x location of the map to scroll to (world coordinate).
|
|
|
|
* @param y Desired y location of the map to scroll to (world coordinate).
|
|
|
|
* @param z Desired z location of the map to scroll to (world coordinate). Use \c -1 to scroll to the height of the map at the \a x, \a y location.
|
|
|
|
* @param w %Window containing the viewport.
|
|
|
|
* @param instant Jump to the location instead of slowly moving to it.
|
|
|
|
* @return Destination of the viewport was changed (to activate other actions when the viewport is already at the desired position).
|
|
|
|
*/
|
2009-03-15 15:25:18 +00:00
|
|
|
bool ScrollWindowTo(int x, int y, int z, Window *w, bool instant)
|
2004-09-03 19:59:05 +00:00
|
|
|
{
|
2008-02-02 21:09:05 +00:00
|
|
|
/* The slope cannot be acquired outside of the map, so make sure we are always within the map. */
|
2009-03-15 15:25:18 +00:00
|
|
|
if (z == -1) z = GetSlopeZ(Clamp(x, 0, MapSizeX() * TILE_SIZE - 1), Clamp(y, 0, MapSizeY() * TILE_SIZE - 1));
|
|
|
|
|
|
|
|
Point pt = MapXYZToViewport(w->viewport, x, y, z);
|
2008-05-11 15:08:44 +00:00
|
|
|
w->viewport->follow_vehicle = INVALID_VEHICLE;
|
2004-09-03 19:59:05 +00:00
|
|
|
|
2010-07-24 10:14:39 +00:00
|
|
|
if (w->viewport->dest_scrollpos_x == pt.x && w->viewport->dest_scrollpos_y == pt.y) return false;
|
2004-09-03 19:59:05 +00:00
|
|
|
|
2007-05-28 17:07:19 +00:00
|
|
|
if (instant) {
|
2008-05-11 15:08:44 +00:00
|
|
|
w->viewport->scrollpos_x = pt.x;
|
|
|
|
w->viewport->scrollpos_y = pt.y;
|
2007-05-28 16:46:16 +00:00
|
|
|
}
|
|
|
|
|
2008-05-11 15:08:44 +00:00
|
|
|
w->viewport->dest_scrollpos_x = pt.x;
|
|
|
|
w->viewport->dest_scrollpos_y = pt.y;
|
2004-09-03 19:59:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-10-23 20:39:21 +00:00
|
|
|
/**
|
|
|
|
* Scrolls the viewport in a window to a given location.
|
|
|
|
* @param tile Desired tile to center on.
|
|
|
|
* @param w %Window containing the viewport.
|
|
|
|
* @param instant Jump to the location instead of slowly moving to it.
|
|
|
|
* @return Destination of the viewport was changed (to activate other actions when the viewport is already at the desired position).
|
|
|
|
*/
|
|
|
|
bool ScrollWindowToTile(TileIndex tile, Window *w, bool instant)
|
|
|
|
{
|
|
|
|
return ScrollWindowTo(TileX(tile) * TILE_SIZE, TileY(tile) * TILE_SIZE, -1, w, instant);
|
|
|
|
}
|
|
|
|
|
2010-10-23 18:28:20 +00:00
|
|
|
/**
|
|
|
|
* Scrolls the viewport of the main window to a given location.
|
|
|
|
* @param tile Desired tile to center on.
|
|
|
|
* @param instant Jump to the location instead of slowly moving to it.
|
|
|
|
* @return Destination of the viewport was changed (to activate other actions when the viewport is already at the desired position).
|
|
|
|
*/
|
2007-05-28 16:46:16 +00:00
|
|
|
bool ScrollMainWindowToTile(TileIndex tile, bool instant)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-03-15 15:25:18 +00:00
|
|
|
return ScrollMainWindowTo(TileX(tile) * TILE_SIZE + TILE_SIZE / 2, TileY(tile) * TILE_SIZE + TILE_SIZE / 2, -1, instant);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-10-23 18:28:20 +00:00
|
|
|
/**
|
|
|
|
* Set a tile to display a red error square.
|
|
|
|
* @param tile Tile that should show the red error square.
|
|
|
|
*/
|
2004-08-09 17:04:08 +00:00
|
|
|
void SetRedErrorSquare(TileIndex tile)
|
|
|
|
{
|
|
|
|
TileIndex old;
|
|
|
|
|
|
|
|
old = _thd.redsq;
|
|
|
|
_thd.redsq = tile;
|
|
|
|
|
|
|
|
if (tile != old) {
|
2009-01-19 15:06:11 +00:00
|
|
|
if (tile != INVALID_TILE) MarkTileDirtyByTile(tile);
|
|
|
|
if (old != INVALID_TILE) MarkTileDirtyByTile(old);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Highlight \a w by \a h tiles at the cursor.
|
2009-08-14 18:41:03 +00:00
|
|
|
* @param w Width of the highlighted tiles rectangle.
|
|
|
|
* @param h Height of the highlighted tiles rectangle.
|
|
|
|
*/
|
2004-08-09 17:04:08 +00:00
|
|
|
void SetTileSelectSize(int w, int h)
|
|
|
|
{
|
2006-04-03 05:32:11 +00:00
|
|
|
_thd.new_size.x = w * TILE_SIZE;
|
|
|
|
_thd.new_size.y = h * TILE_SIZE;
|
2005-05-27 15:05:54 +00:00
|
|
|
_thd.new_outersize.x = 0;
|
|
|
|
_thd.new_outersize.y = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-07-17 20:14:58 +00:00
|
|
|
void SetTileSelectBigSize(int ox, int oy, int sx, int sy)
|
|
|
|
{
|
2006-04-03 05:32:11 +00:00
|
|
|
_thd.offs.x = ox * TILE_SIZE;
|
|
|
|
_thd.offs.y = oy * TILE_SIZE;
|
|
|
|
_thd.new_outersize.x = sx * TILE_SIZE;
|
|
|
|
_thd.new_outersize.y = sy * TILE_SIZE;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-04-04 04:08:47 +00:00
|
|
|
/** returns the best autorail highlight type from map coordinates */
|
2008-01-09 09:45:45 +00:00
|
|
|
static HighLightStyle GetAutorailHT(int x, int y)
|
2005-01-19 20:55:23 +00:00
|
|
|
{
|
2009-04-18 21:10:36 +00:00
|
|
|
return HT_RAIL | _autorail_piece[x & TILE_UNIT_MASK][y & TILE_UNIT_MASK];
|
2005-01-19 20:55:23 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-06-26 16:58:40 +00:00
|
|
|
/**
|
|
|
|
* Updates tile highlighting for all cases.
|
|
|
|
* Uses _thd.selstart and _thd.selend and _thd.place_mode (set elsewhere) to determine _thd.pos and _thd.size
|
|
|
|
* Also drawstyle is determined. Uses _thd.new.* as a buffer and calls SetSelectionTilesDirty() twice,
|
|
|
|
* Once for the old and once for the new selection.
|
|
|
|
* _thd is TileHighlightData, found in viewport.h
|
|
|
|
*/
|
2007-03-07 11:47:46 +00:00
|
|
|
void UpdateTileSelection()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-07-17 20:14:58 +00:00
|
|
|
int x1;
|
|
|
|
int y1;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-04-18 21:10:36 +00:00
|
|
|
_thd.new_drawstyle = HT_NONE;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-04-19 10:31:30 +00:00
|
|
|
if (_thd.place_mode == HT_SPECIAL) {
|
2005-05-27 15:05:54 +00:00
|
|
|
x1 = _thd.selend.x;
|
|
|
|
y1 = _thd.selend.y;
|
2004-08-09 17:04:08 +00:00
|
|
|
if (x1 != -1) {
|
2009-04-18 21:10:36 +00:00
|
|
|
int x2 = _thd.selstart.x & ~TILE_UNIT_MASK;
|
|
|
|
int y2 = _thd.selstart.y & ~TILE_UNIT_MASK;
|
|
|
|
x1 &= ~TILE_UNIT_MASK;
|
|
|
|
y1 &= ~TILE_UNIT_MASK;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-02-22 08:43:02 +00:00
|
|
|
if (x1 >= x2) Swap(x1, x2);
|
|
|
|
if (y1 >= y2) Swap(y1, y2);
|
2005-05-27 15:05:54 +00:00
|
|
|
_thd.new_pos.x = x1;
|
|
|
|
_thd.new_pos.y = y1;
|
2006-04-03 05:32:11 +00:00
|
|
|
_thd.new_size.x = x2 - x1 + TILE_SIZE;
|
|
|
|
_thd.new_size.y = y2 - y1 + TILE_SIZE;
|
2005-05-27 15:05:54 +00:00
|
|
|
_thd.new_drawstyle = _thd.next_drawstyle;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2010-09-06 14:14:09 +00:00
|
|
|
} else if ((_thd.place_mode & HT_DRAG_MASK) != HT_NONE) {
|
2005-07-17 20:14:58 +00:00
|
|
|
Point pt = GetTileBelowCursor();
|
2004-08-09 17:04:08 +00:00
|
|
|
x1 = pt.x;
|
|
|
|
y1 = pt.y;
|
|
|
|
if (x1 != -1) {
|
2009-04-19 21:26:06 +00:00
|
|
|
switch (_thd.place_mode & HT_DRAG_MASK) {
|
2009-04-19 10:31:30 +00:00
|
|
|
case HT_RECT:
|
2005-05-27 15:05:54 +00:00
|
|
|
_thd.new_drawstyle = HT_RECT;
|
2005-01-19 20:55:23 +00:00
|
|
|
break;
|
2009-04-19 10:31:30 +00:00
|
|
|
case HT_POINT:
|
2005-05-27 15:05:54 +00:00
|
|
|
_thd.new_drawstyle = HT_POINT;
|
2009-04-18 21:10:36 +00:00
|
|
|
x1 += TILE_SIZE / 2;
|
|
|
|
y1 += TILE_SIZE / 2;
|
2005-01-19 20:55:23 +00:00
|
|
|
break;
|
2009-04-19 10:31:30 +00:00
|
|
|
case HT_RAIL:
|
2009-04-19 21:26:06 +00:00
|
|
|
/* Draw one highlighted tile in any direction */
|
|
|
|
_thd.new_drawstyle = GetAutorailHT(pt.x, pt.y);
|
|
|
|
break;
|
|
|
|
case HT_LINE:
|
|
|
|
switch (_thd.place_mode & HT_DIR_MASK) {
|
|
|
|
case HT_DIR_X: _thd.new_drawstyle = HT_LINE | HT_DIR_X; break;
|
|
|
|
case HT_DIR_Y: _thd.new_drawstyle = HT_LINE | HT_DIR_Y; break;
|
|
|
|
|
|
|
|
case HT_DIR_HU:
|
|
|
|
case HT_DIR_HL:
|
|
|
|
_thd.new_drawstyle = (pt.x & TILE_UNIT_MASK) + (pt.y & TILE_UNIT_MASK) <= TILE_SIZE ? HT_LINE | HT_DIR_HU : HT_LINE | HT_DIR_HL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HT_DIR_VL:
|
|
|
|
case HT_DIR_VR:
|
|
|
|
_thd.new_drawstyle = (pt.x & TILE_UNIT_MASK) > (pt.y & TILE_UNIT_MASK) ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
|
|
|
_thd.selstart.x = x1 & ~TILE_UNIT_MASK;
|
|
|
|
_thd.selstart.y = y1 & ~TILE_UNIT_MASK;
|
2007-11-18 23:13:53 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NOT_REACHED();
|
|
|
|
break;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2009-04-18 21:10:36 +00:00
|
|
|
_thd.new_pos.x = x1 & ~TILE_UNIT_MASK;
|
|
|
|
_thd.new_pos.y = y1 & ~TILE_UNIT_MASK;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-04 04:08:47 +00:00
|
|
|
/* redraw selection */
|
2005-05-27 15:05:54 +00:00
|
|
|
if (_thd.drawstyle != _thd.new_drawstyle ||
|
|
|
|
_thd.pos.x != _thd.new_pos.x || _thd.pos.y != _thd.new_pos.y ||
|
2006-09-03 22:39:02 +00:00
|
|
|
_thd.size.x != _thd.new_size.x || _thd.size.y != _thd.new_size.y ||
|
2007-06-26 16:58:40 +00:00
|
|
|
_thd.outersize.x != _thd.new_outersize.x ||
|
|
|
|
_thd.outersize.y != _thd.new_outersize.y) {
|
2007-04-04 04:08:47 +00:00
|
|
|
/* clear the old selection? */
|
2005-05-27 15:05:54 +00:00
|
|
|
if (_thd.drawstyle) SetSelectionTilesDirty();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-02-08 15:45:34 +00:00
|
|
|
_thd.drawstyle = _thd.new_drawstyle;
|
|
|
|
_thd.pos = _thd.new_pos;
|
|
|
|
_thd.size = _thd.new_size;
|
2005-05-27 15:05:54 +00:00
|
|
|
_thd.outersize = _thd.new_outersize;
|
2009-02-08 15:45:34 +00:00
|
|
|
_thd.dirty = 0xff;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-04 04:08:47 +00:00
|
|
|
/* draw the new selection? */
|
2005-05-27 15:05:54 +00:00
|
|
|
if (_thd.new_drawstyle) SetSelectionTilesDirty();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Displays the measurement tooltips when selecting multiple tiles
|
2008-08-02 11:26:25 +00:00
|
|
|
* @param str String to be displayed
|
|
|
|
* @param paramcount number of params to deal with
|
|
|
|
* @param params (optional) up to 5 pieces of additional information that may be added to a tooltip
|
2010-10-03 12:20:50 +00:00
|
|
|
* @param close_cond Condition for closing this tooltip.
|
2008-08-02 11:26:25 +00:00
|
|
|
*/
|
2010-10-03 12:20:50 +00:00
|
|
|
static inline void ShowMeasurementTooltips(StringID str, uint paramcount, const uint64 params[], TooltipCloseCondition close_cond = TCC_LEFT_CLICK)
|
2008-08-02 11:26:25 +00:00
|
|
|
{
|
|
|
|
if (!_settings_client.gui.measure_tooltip) return;
|
2010-12-12 14:14:26 +00:00
|
|
|
GuiShowTooltips(FindWindowById(_thd.window_class, _thd.window_number), str, paramcount, params, close_cond);
|
2008-08-02 11:26:25 +00:00
|
|
|
}
|
|
|
|
|
2007-04-04 04:08:47 +00:00
|
|
|
/** highlighting tiles while only going over them with the mouse */
|
2008-05-08 13:21:55 +00:00
|
|
|
void VpStartPlaceSizing(TileIndex tile, ViewportPlaceMethod method, ViewportDragDropSelectionProcess process)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-05-23 12:45:56 +00:00
|
|
|
_thd.select_method = method;
|
|
|
|
_thd.select_proc = process;
|
2006-04-03 05:32:11 +00:00
|
|
|
_thd.selend.x = TileX(tile) * TILE_SIZE;
|
|
|
|
_thd.selstart.x = TileX(tile) * TILE_SIZE;
|
|
|
|
_thd.selend.y = TileY(tile) * TILE_SIZE;
|
|
|
|
_thd.selstart.y = TileY(tile) * TILE_SIZE;
|
2007-11-18 20:05:44 +00:00
|
|
|
|
|
|
|
/* Needed so several things (road, autoroad, bridges, ...) are placed correctly.
|
|
|
|
* In effect, placement starts from the centre of a tile
|
|
|
|
*/
|
|
|
|
if (method == VPM_X_OR_Y || method == VPM_FIX_X || method == VPM_FIX_Y) {
|
|
|
|
_thd.selend.x += TILE_SIZE / 2;
|
|
|
|
_thd.selend.y += TILE_SIZE / 2;
|
|
|
|
_thd.selstart.x += TILE_SIZE / 2;
|
|
|
|
_thd.selstart.y += TILE_SIZE / 2;
|
|
|
|
}
|
|
|
|
|
2009-04-19 10:31:30 +00:00
|
|
|
if (_thd.place_mode == HT_RECT) {
|
|
|
|
_thd.place_mode = HT_SPECIAL;
|
2005-05-27 15:05:54 +00:00
|
|
|
_thd.next_drawstyle = HT_RECT;
|
2009-04-24 17:23:32 +00:00
|
|
|
} else if (_thd.place_mode & (HT_RAIL | HT_LINE)) {
|
2009-04-19 10:31:30 +00:00
|
|
|
_thd.place_mode = HT_SPECIAL;
|
2005-05-27 15:05:54 +00:00
|
|
|
_thd.next_drawstyle = _thd.drawstyle;
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2009-04-19 10:31:30 +00:00
|
|
|
_thd.place_mode = HT_SPECIAL;
|
2005-05-27 15:05:54 +00:00
|
|
|
_thd.next_drawstyle = HT_POINT;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
_special_mouse_mode = WSM_SIZING;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VpSetPlaceSizingLimit(int limit)
|
|
|
|
{
|
|
|
|
_thd.sizelimit = limit;
|
|
|
|
}
|
|
|
|
|
2006-10-12 15:13:40 +00:00
|
|
|
/**
|
2009-03-14 18:16:29 +00:00
|
|
|
* Highlights all tiles between a set of two tiles. Used in dock and tunnel placement
|
|
|
|
* @param from TileIndex of the first tile to highlight
|
2010-08-01 19:44:49 +00:00
|
|
|
* @param to TileIndex of the last tile to highlight
|
|
|
|
*/
|
2006-10-12 15:13:40 +00:00
|
|
|
void VpSetPresizeRange(TileIndex from, TileIndex to)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-06-21 19:08:47 +00:00
|
|
|
uint64 distance = DistanceManhattan(from, to) + 1;
|
2006-10-12 15:13:40 +00:00
|
|
|
|
2006-04-03 05:32:11 +00:00
|
|
|
_thd.selend.x = TileX(to) * TILE_SIZE;
|
|
|
|
_thd.selend.y = TileY(to) * TILE_SIZE;
|
|
|
|
_thd.selstart.x = TileX(from) * TILE_SIZE;
|
|
|
|
_thd.selstart.y = TileY(from) * TILE_SIZE;
|
2005-05-27 15:05:54 +00:00
|
|
|
_thd.next_drawstyle = HT_RECT;
|
2006-10-12 15:13:40 +00:00
|
|
|
|
|
|
|
/* show measurement only if there is any length to speak of */
|
2010-10-03 12:20:50 +00:00
|
|
|
if (distance > 1) ShowMeasurementTooltips(STR_MEASURE_LENGTH, 1, &distance, TCC_HOVER);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static void VpStartPreSizing()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
_thd.selend.x = -1;
|
|
|
|
_special_mouse_mode = WSM_PRESIZE;
|
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* returns information about the 2x1 piece to be build.
|
2010-08-01 19:44:49 +00:00
|
|
|
* The lower bits (0-3) are the track type.
|
|
|
|
*/
|
2008-01-09 09:45:45 +00:00
|
|
|
static HighLightStyle Check2x1AutoRail(int mode)
|
2005-01-19 20:55:23 +00:00
|
|
|
{
|
|
|
|
int fxpy = _tile_fract_coords.x + _tile_fract_coords.y;
|
2009-04-18 21:10:36 +00:00
|
|
|
int sxpy = (_thd.selend.x & TILE_UNIT_MASK) + (_thd.selend.y & TILE_UNIT_MASK);
|
2005-01-19 20:55:23 +00:00
|
|
|
int fxmy = _tile_fract_coords.x - _tile_fract_coords.y;
|
2009-04-18 21:10:36 +00:00
|
|
|
int sxmy = (_thd.selend.x & TILE_UNIT_MASK) - (_thd.selend.y & TILE_UNIT_MASK);
|
2005-01-19 20:55:23 +00:00
|
|
|
|
2006-02-01 07:36:15 +00:00
|
|
|
switch (mode) {
|
2008-01-09 09:45:45 +00:00
|
|
|
default: NOT_REACHED();
|
|
|
|
case 0: // end piece is lower right
|
2009-04-18 21:10:36 +00:00
|
|
|
if (fxpy >= 20 && sxpy <= 12) return HT_DIR_HL;
|
|
|
|
if (fxmy < -3 && sxmy > 3) return HT_DIR_VR;
|
2008-01-09 09:45:45 +00:00
|
|
|
return HT_DIR_Y;
|
2005-01-19 20:55:23 +00:00
|
|
|
|
2008-01-09 09:45:45 +00:00
|
|
|
case 1:
|
2009-04-18 21:10:36 +00:00
|
|
|
if (fxmy > 3 && sxmy < -3) return HT_DIR_VL;
|
|
|
|
if (fxpy <= 12 && sxpy >= 20) return HT_DIR_HU;
|
2008-01-09 09:45:45 +00:00
|
|
|
return HT_DIR_Y;
|
2005-01-19 20:55:23 +00:00
|
|
|
|
2008-01-09 09:45:45 +00:00
|
|
|
case 2:
|
2009-04-18 21:10:36 +00:00
|
|
|
if (fxmy > 3 && sxmy < -3) return HT_DIR_VL;
|
|
|
|
if (fxpy >= 20 && sxpy <= 12) return HT_DIR_HL;
|
2008-01-09 09:45:45 +00:00
|
|
|
return HT_DIR_X;
|
2005-01-19 20:55:23 +00:00
|
|
|
|
2008-01-09 09:45:45 +00:00
|
|
|
case 3:
|
2009-04-18 21:10:36 +00:00
|
|
|
if (fxmy < -3 && sxmy > 3) return HT_DIR_VR;
|
|
|
|
if (fxpy <= 12 && sxpy >= 20) return HT_DIR_HU;
|
2008-01-09 09:45:45 +00:00
|
|
|
return HT_DIR_X;
|
2005-01-19 20:55:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Check if the direction of start and end tile should be swapped based on
|
2006-10-12 15:13:40 +00:00
|
|
|
* the dragging-style. Default directions are:
|
|
|
|
* in the case of a line (HT_RAIL, HT_LINE): DIR_NE, DIR_NW, DIR_N, DIR_E
|
|
|
|
* in the case of a rect (HT_RECT, HT_POINT): DIR_S, DIR_E
|
|
|
|
* For example dragging a rectangle area from south to north should be swapped to
|
|
|
|
* north-south (DIR_S) to obtain the same results with less code. This is what
|
|
|
|
* the return value signifies.
|
|
|
|
* @param style HighLightStyle dragging style
|
2007-04-18 00:41:09 +00:00
|
|
|
* @param start_tile start tile of drag
|
|
|
|
* @param end_tile end tile of drag
|
2010-08-01 19:44:49 +00:00
|
|
|
* @return boolean value which when true means start/end should be swapped
|
|
|
|
*/
|
2006-10-12 15:13:40 +00:00
|
|
|
static bool SwapDirection(HighLightStyle style, TileIndex start_tile, TileIndex end_tile)
|
|
|
|
{
|
|
|
|
uint start_x = TileX(start_tile);
|
|
|
|
uint start_y = TileY(start_tile);
|
|
|
|
uint end_x = TileX(end_tile);
|
|
|
|
uint end_y = TileY(end_tile);
|
|
|
|
|
|
|
|
switch (style & HT_DRAG_MASK) {
|
|
|
|
case HT_RAIL:
|
|
|
|
case HT_LINE: return (end_x > start_x || (end_x == start_x && end_y > start_y));
|
|
|
|
|
|
|
|
case HT_RECT:
|
|
|
|
case HT_POINT: return (end_x != start_x && end_y < start_y);
|
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Calculates height difference between one tile and another.
|
2010-05-07 20:39:00 +00:00
|
|
|
* Multiplies the result to suit the standard given by #TILE_HEIGHT_STEP.
|
|
|
|
*
|
2009-03-14 18:16:29 +00:00
|
|
|
* To correctly get the height difference we need the direction we are dragging
|
|
|
|
* in, as well as with what kind of tool we are dragging. For example a horizontal
|
|
|
|
* autorail tool that starts in bottom and ends at the top of a tile will need the
|
|
|
|
* maximum of SW, S and SE, N corners respectively. This is handled by the lookup table below
|
2010-05-07 20:20:03 +00:00
|
|
|
* See #_tileoffs_by_dir in map.cpp for the direction enums if you can't figure out the values yourself.
|
|
|
|
* @param style Highlighting style of the drag. This includes direction and style (autorail, rect, etc.)
|
|
|
|
* @param distance Number of tiles dragged, important for horizontal/vertical drags, ignored for others.
|
|
|
|
* @param start_tile Start tile of the drag operation.
|
|
|
|
* @param end_tile End tile of the drag operation.
|
|
|
|
* @return Height difference between two tiles. The tile measurement tool utilizes this value in its tooltip.
|
|
|
|
*/
|
2006-10-12 15:13:40 +00:00
|
|
|
static int CalcHeightdiff(HighLightStyle style, uint distance, TileIndex start_tile, TileIndex end_tile)
|
|
|
|
{
|
|
|
|
bool swap = SwapDirection(style, start_tile, end_tile);
|
2010-05-07 20:20:03 +00:00
|
|
|
uint h0, h1; // Start height and end height.
|
2006-10-12 15:13:40 +00:00
|
|
|
|
|
|
|
if (start_tile == end_tile) return 0;
|
2007-01-19 11:47:48 +00:00
|
|
|
if (swap) Swap(start_tile, end_tile);
|
2006-10-12 15:13:40 +00:00
|
|
|
|
|
|
|
switch (style & HT_DRAG_MASK) {
|
|
|
|
case HT_RECT: {
|
|
|
|
static const TileIndexDiffC heightdiff_area_by_dir[] = {
|
2009-03-15 00:32:18 +00:00
|
|
|
/* Start */ {1, 0}, /* Dragging east */ {0, 0}, // Dragging south
|
|
|
|
/* End */ {0, 1}, /* Dragging east */ {1, 1} // Dragging south
|
2006-10-12 15:13:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* In the case of an area we can determine whether we were dragging south or
|
|
|
|
* east by checking the X-coordinates of the tiles */
|
2010-05-07 20:20:03 +00:00
|
|
|
byte style_t = (byte)(TileX(end_tile) > TileX(start_tile));
|
2006-10-12 15:13:40 +00:00
|
|
|
start_tile = TILE_ADD(start_tile, ToTileIndexDiff(heightdiff_area_by_dir[style_t]));
|
|
|
|
end_tile = TILE_ADD(end_tile, ToTileIndexDiff(heightdiff_area_by_dir[2 + style_t]));
|
2010-08-01 20:52:11 +00:00
|
|
|
/* FALL THROUGH */
|
2006-10-12 15:13:40 +00:00
|
|
|
}
|
2010-08-01 20:52:11 +00:00
|
|
|
|
2006-10-12 15:13:40 +00:00
|
|
|
case HT_POINT:
|
|
|
|
h0 = TileHeight(start_tile);
|
|
|
|
h1 = TileHeight(end_tile);
|
|
|
|
break;
|
2009-03-15 00:32:18 +00:00
|
|
|
default: { // All other types, this is mostly only line/autorail
|
2006-10-12 15:13:40 +00:00
|
|
|
static const HighLightStyle flip_style_direction[] = {
|
|
|
|
HT_DIR_X, HT_DIR_Y, HT_DIR_HL, HT_DIR_HU, HT_DIR_VR, HT_DIR_VL
|
|
|
|
};
|
|
|
|
static const TileIndexDiffC heightdiff_line_by_dir[] = {
|
2009-03-15 00:32:18 +00:00
|
|
|
/* Start */ {1, 0}, {1, 1}, /* HT_DIR_X */ {0, 1}, {1, 1}, // HT_DIR_Y
|
|
|
|
/* Start */ {1, 0}, {0, 0}, /* HT_DIR_HU */ {1, 0}, {1, 1}, // HT_DIR_HL
|
|
|
|
/* Start */ {1, 0}, {1, 1}, /* HT_DIR_VL */ {0, 1}, {1, 1}, // HT_DIR_VR
|
2006-10-12 15:13:40 +00:00
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
/* Start */ {0, 1}, {0, 0}, /* HT_DIR_X */ {1, 0}, {0, 0}, // HT_DIR_Y
|
|
|
|
/* End */ {0, 1}, {0, 0}, /* HT_DIR_HU */ {1, 1}, {0, 1}, // HT_DIR_HL
|
|
|
|
/* End */ {1, 0}, {0, 0}, /* HT_DIR_VL */ {0, 0}, {0, 1}, // HT_DIR_VR
|
2006-10-12 15:13:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
distance %= 2; // we're only interested if the distance is even or uneven
|
|
|
|
style &= HT_DIR_MASK;
|
|
|
|
|
|
|
|
/* To handle autorail, we do some magic to be able to use a lookup table.
|
|
|
|
* Firstly if we drag the other way around, we switch start&end, and if needed
|
|
|
|
* also flip the drag-position. Eg if it was on the left, and the distance is even
|
|
|
|
* that means the end, which is now the start is on the right */
|
|
|
|
if (swap && distance == 0) style = flip_style_direction[style];
|
|
|
|
|
|
|
|
/* Use lookup table for start-tile based on HighLightStyle direction */
|
2010-05-07 20:20:03 +00:00
|
|
|
byte style_t = style * 2;
|
2006-10-12 15:13:40 +00:00
|
|
|
assert(style_t < lengthof(heightdiff_line_by_dir) - 13);
|
|
|
|
h0 = TileHeight(TILE_ADD(start_tile, ToTileIndexDiff(heightdiff_line_by_dir[style_t])));
|
2010-05-07 20:20:03 +00:00
|
|
|
uint ht = TileHeight(TILE_ADD(start_tile, ToTileIndexDiff(heightdiff_line_by_dir[style_t + 1])));
|
2007-01-11 11:05:01 +00:00
|
|
|
h0 = max(h0, ht);
|
2006-10-12 15:13:40 +00:00
|
|
|
|
|
|
|
/* Use lookup table for end-tile based on HighLightStyle direction
|
|
|
|
* flip around side (lower/upper, left/right) based on distance */
|
|
|
|
if (distance == 0) style_t = flip_style_direction[style] * 2;
|
|
|
|
assert(style_t < lengthof(heightdiff_line_by_dir) - 13);
|
|
|
|
h1 = TileHeight(TILE_ADD(end_tile, ToTileIndexDiff(heightdiff_line_by_dir[12 + style_t])));
|
|
|
|
ht = TileHeight(TILE_ADD(end_tile, ToTileIndexDiff(heightdiff_line_by_dir[12 + style_t + 1])));
|
2007-01-11 11:05:01 +00:00
|
|
|
h1 = max(h1, ht);
|
2010-08-01 18:53:30 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-10-12 15:13:40 +00:00
|
|
|
}
|
|
|
|
|
2007-01-19 11:47:48 +00:00
|
|
|
if (swap) Swap(h0, h1);
|
2010-05-07 20:39:00 +00:00
|
|
|
return (int)(h1 - h0) * TILE_HEIGHT_STEP;
|
2006-10-12 15:13:40 +00:00
|
|
|
}
|
2005-01-19 20:55:23 +00:00
|
|
|
|
2006-10-18 14:42:19 +00:00
|
|
|
static const StringID measure_strings_length[] = {STR_NULL, STR_MEASURE_LENGTH, STR_MEASURE_LENGTH_HEIGHTDIFF};
|
2006-10-18 14:20:10 +00:00
|
|
|
|
2009-04-19 21:26:06 +00:00
|
|
|
/**
|
|
|
|
* Check for underflowing the map.
|
|
|
|
* @param test the variable to test for underflowing
|
|
|
|
* @param other the other variable to update to keep the line
|
|
|
|
* @param mult the constant to multiply the difference by for \c other
|
|
|
|
*/
|
|
|
|
static void CheckUnderflow(int &test, int &other, int mult)
|
|
|
|
{
|
|
|
|
if (test >= 0) return;
|
|
|
|
|
|
|
|
other += mult * test;
|
|
|
|
test = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check for overflowing the map.
|
|
|
|
* @param test the variable to test for overflowing
|
|
|
|
* @param other the other variable to update to keep the line
|
|
|
|
* @param max the maximum value for the \c test variable
|
|
|
|
* @param mult the constant to multiply the difference by for \c other
|
|
|
|
*/
|
|
|
|
static void CheckOverflow(int &test, int &other, int max, int mult)
|
|
|
|
{
|
|
|
|
if (test <= max) return;
|
|
|
|
|
|
|
|
other += mult * (test - max);
|
|
|
|
test = max;
|
|
|
|
}
|
|
|
|
|
2007-04-04 04:08:47 +00:00
|
|
|
/** while dragging */
|
2005-01-19 20:55:23 +00:00
|
|
|
static void CalcRaildirsDrawstyle(TileHighlightData *thd, int x, int y, int method)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-10-10 14:05:39 +00:00
|
|
|
HighLightStyle b;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2009-04-18 21:10:36 +00:00
|
|
|
int dx = thd->selstart.x - (thd->selend.x & ~TILE_UNIT_MASK);
|
|
|
|
int dy = thd->selstart.y - (thd->selend.y & ~TILE_UNIT_MASK);
|
|
|
|
uint w = abs(dx) + TILE_SIZE;
|
|
|
|
uint h = abs(dy) + TILE_SIZE;
|
2005-01-19 20:55:23 +00:00
|
|
|
|
2009-04-19 21:26:06 +00:00
|
|
|
if (method & ~(VPM_RAILDIRS | VPM_SIGNALDIRS)) {
|
|
|
|
/* We 'force' a selection direction; first four rail buttons. */
|
|
|
|
method &= ~(VPM_RAILDIRS | VPM_SIGNALDIRS);
|
|
|
|
int raw_dx = thd->selstart.x - thd->selend.x;
|
|
|
|
int raw_dy = thd->selstart.y - thd->selend.y;
|
|
|
|
switch (method) {
|
|
|
|
case VPM_FIX_X:
|
|
|
|
b = HT_LINE | HT_DIR_Y;
|
|
|
|
x = thd->selstart.x;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VPM_FIX_Y:
|
|
|
|
b = HT_LINE | HT_DIR_X;
|
|
|
|
y = thd->selstart.y;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VPM_FIX_HORIZONTAL:
|
|
|
|
if (dx == -dy) {
|
|
|
|
/* We are on a straight horizontal line. Determine the 'rail'
|
|
|
|
* to build based the sub tile location. */
|
|
|
|
b = (x & TILE_UNIT_MASK) + (y & TILE_UNIT_MASK) >= TILE_SIZE ? HT_LINE | HT_DIR_HL : HT_LINE | HT_DIR_HU;
|
|
|
|
} else {
|
|
|
|
/* We are not on a straight line. Determine the rail to build
|
|
|
|
* based on whether we are above or below it. */
|
2010-05-13 11:19:30 +00:00
|
|
|
b = dx + dy >= (int)TILE_SIZE ? HT_LINE | HT_DIR_HU : HT_LINE | HT_DIR_HL;
|
2009-04-19 21:26:06 +00:00
|
|
|
|
|
|
|
/* Calculate where a horizontal line through the start point and
|
|
|
|
* a vertical line from the selected end point intersect and
|
|
|
|
* use that point as the end point. */
|
|
|
|
int offset = (raw_dx - raw_dy) / 2;
|
|
|
|
x = thd->selstart.x - (offset & ~TILE_UNIT_MASK);
|
|
|
|
y = thd->selstart.y + (offset & ~TILE_UNIT_MASK);
|
|
|
|
|
|
|
|
/* 'Build' the last half rail tile if needed */
|
|
|
|
if ((offset & TILE_UNIT_MASK) > (TILE_SIZE / 2)) {
|
2010-05-13 11:19:30 +00:00
|
|
|
if (dx + dy >= (int)TILE_SIZE) {
|
2010-05-21 05:42:41 +00:00
|
|
|
x += (dx + dy < 0) ? (int)TILE_SIZE : -(int)TILE_SIZE;
|
2009-04-19 21:26:06 +00:00
|
|
|
} else {
|
2010-05-21 05:42:41 +00:00
|
|
|
y += (dx + dy < 0) ? (int)TILE_SIZE : -(int)TILE_SIZE;
|
2009-04-19 21:26:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure we do not overflow the map! */
|
|
|
|
CheckUnderflow(x, y, 1);
|
|
|
|
CheckUnderflow(y, x, 1);
|
|
|
|
CheckOverflow(x, y, (MapMaxX() - 1) * TILE_SIZE, 1);
|
|
|
|
CheckOverflow(y, x, (MapMaxY() - 1) * TILE_SIZE, 1);
|
2010-05-13 11:19:30 +00:00
|
|
|
assert(x >= 0 && y >= 0 && x <= (int)(MapMaxX() * TILE_SIZE) && y <= (int)(MapMaxY() * TILE_SIZE));
|
2009-04-19 21:26:06 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VPM_FIX_VERTICAL:
|
|
|
|
if (dx == dy) {
|
|
|
|
/* We are on a straight vertical line. Determine the 'rail'
|
|
|
|
* to build based the sub tile location. */
|
|
|
|
b = (x & TILE_UNIT_MASK) > (y & TILE_UNIT_MASK) ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR;
|
|
|
|
} else {
|
|
|
|
/* We are not on a straight line. Determine the rail to build
|
|
|
|
* based on whether we are left or right from it. */
|
|
|
|
b = dx < dy ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR;
|
|
|
|
|
|
|
|
/* Calculate where a vertical line through the start point and
|
|
|
|
* a horizontal line from the selected end point intersect and
|
|
|
|
* use that point as the end point. */
|
2010-06-07 19:43:40 +00:00
|
|
|
int offset = (raw_dx + raw_dy + (int)TILE_SIZE) / 2;
|
2009-04-19 21:26:06 +00:00
|
|
|
x = thd->selstart.x - (offset & ~TILE_UNIT_MASK);
|
|
|
|
y = thd->selstart.y - (offset & ~TILE_UNIT_MASK);
|
|
|
|
|
|
|
|
/* 'Build' the last half rail tile if needed */
|
|
|
|
if ((offset & TILE_UNIT_MASK) > (TILE_SIZE / 2)) {
|
|
|
|
if (dx - dy < 0) {
|
2010-05-21 05:42:41 +00:00
|
|
|
y += (dx > dy) ? (int)TILE_SIZE : -(int)TILE_SIZE;
|
2009-04-19 21:26:06 +00:00
|
|
|
} else {
|
2010-05-21 05:42:41 +00:00
|
|
|
x += (dx < dy) ? (int)TILE_SIZE : -(int)TILE_SIZE;
|
2009-04-19 21:26:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure we do not overflow the map! */
|
|
|
|
CheckUnderflow(x, y, -1);
|
|
|
|
CheckUnderflow(y, x, -1);
|
|
|
|
CheckOverflow(x, y, (MapMaxX() - 1) * TILE_SIZE, -1);
|
|
|
|
CheckOverflow(y, x, (MapMaxY() - 1) * TILE_SIZE, -1);
|
2010-05-13 11:19:30 +00:00
|
|
|
assert(x >= 0 && y >= 0 && x <= (int)(MapMaxX() * TILE_SIZE) && y <= (int)(MapMaxY() * TILE_SIZE));
|
2009-04-19 21:26:06 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
NOT_REACHED();
|
|
|
|
}
|
|
|
|
} else if (TileVirtXY(thd->selstart.x, thd->selstart.y) == TileVirtXY(x, y)) { // check if we're only within one tile
|
|
|
|
if (method & VPM_RAILDIRS) {
|
2005-07-17 20:14:58 +00:00
|
|
|
b = GetAutorailHT(x, y);
|
2006-06-27 21:25:53 +00:00
|
|
|
} else { // rect for autosignals on one tile
|
2005-07-17 20:14:58 +00:00
|
|
|
b = HT_RECT;
|
2006-06-27 21:25:53 +00:00
|
|
|
}
|
2009-04-18 21:10:36 +00:00
|
|
|
} else if (h == TILE_SIZE) { // Is this in X direction?
|
2010-05-13 11:19:30 +00:00
|
|
|
if (dx == (int)TILE_SIZE) { // 2x1 special handling
|
2005-01-19 20:55:23 +00:00
|
|
|
b = (Check2x1AutoRail(3)) | HT_LINE;
|
2010-05-13 11:19:30 +00:00
|
|
|
} else if (dx == -(int)TILE_SIZE) {
|
2005-01-19 20:55:23 +00:00
|
|
|
b = (Check2x1AutoRail(2)) | HT_LINE;
|
2006-06-27 21:25:53 +00:00
|
|
|
} else {
|
2005-01-19 20:55:23 +00:00
|
|
|
b = HT_LINE | HT_DIR_X;
|
2006-06-27 21:25:53 +00:00
|
|
|
}
|
2005-01-19 20:55:23 +00:00
|
|
|
y = thd->selstart.y;
|
2009-04-18 21:10:36 +00:00
|
|
|
} else if (w == TILE_SIZE) { // Or Y direction?
|
2010-05-13 11:19:30 +00:00
|
|
|
if (dy == (int)TILE_SIZE) { // 2x1 special handling
|
2005-01-19 20:55:23 +00:00
|
|
|
b = (Check2x1AutoRail(1)) | HT_LINE;
|
2010-05-13 11:19:30 +00:00
|
|
|
} else if (dy == -(int)TILE_SIZE) { // 2x1 other direction
|
2005-01-19 20:55:23 +00:00
|
|
|
b = (Check2x1AutoRail(0)) | HT_LINE;
|
2006-06-27 21:25:53 +00:00
|
|
|
} else {
|
2005-01-19 20:55:23 +00:00
|
|
|
b = HT_LINE | HT_DIR_Y;
|
2006-06-27 21:25:53 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
x = thd->selstart.x;
|
2005-01-19 20:55:23 +00:00
|
|
|
} else if (w > h * 2) { // still count as x dir?
|
2005-07-17 20:14:58 +00:00
|
|
|
b = HT_LINE | HT_DIR_X;
|
2004-08-09 17:04:08 +00:00
|
|
|
y = thd->selstart.y;
|
2005-01-19 20:55:23 +00:00
|
|
|
} else if (h > w * 2) { // still count as y dir?
|
2005-07-17 20:14:58 +00:00
|
|
|
b = HT_LINE | HT_DIR_Y;
|
2005-01-19 20:55:23 +00:00
|
|
|
x = thd->selstart.x;
|
|
|
|
} else { // complicated direction
|
2006-10-10 14:05:39 +00:00
|
|
|
int d = w - h;
|
2009-04-18 21:10:36 +00:00
|
|
|
thd->selend.x = thd->selend.x & ~TILE_UNIT_MASK;
|
|
|
|
thd->selend.y = thd->selend.y & ~TILE_UNIT_MASK;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
/* four cases. */
|
2004-08-09 17:04:08 +00:00
|
|
|
if (x > thd->selstart.x) {
|
|
|
|
if (y > thd->selstart.y) {
|
2009-03-15 00:32:18 +00:00
|
|
|
/* south */
|
2005-07-17 20:14:58 +00:00
|
|
|
if (d == 0) {
|
2009-04-18 21:10:36 +00:00
|
|
|
b = (x & TILE_UNIT_MASK) > (y & TILE_UNIT_MASK) ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR;
|
2005-07-17 20:14:58 +00:00
|
|
|
} else if (d >= 0) {
|
|
|
|
x = thd->selstart.x + h;
|
|
|
|
b = HT_LINE | HT_DIR_VL;
|
|
|
|
} else {
|
|
|
|
y = thd->selstart.y + w;
|
|
|
|
b = HT_LINE | HT_DIR_VR;
|
2009-04-18 21:10:36 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2009-03-15 00:32:18 +00:00
|
|
|
/* west */
|
2005-07-17 20:14:58 +00:00
|
|
|
if (d == 0) {
|
2009-04-18 21:10:36 +00:00
|
|
|
b = (x & TILE_UNIT_MASK) + (y & TILE_UNIT_MASK) >= TILE_SIZE ? HT_LINE | HT_DIR_HL : HT_LINE | HT_DIR_HU;
|
2005-07-17 20:14:58 +00:00
|
|
|
} else if (d >= 0) {
|
|
|
|
x = thd->selstart.x + h;
|
|
|
|
b = HT_LINE | HT_DIR_HL;
|
|
|
|
} else {
|
|
|
|
y = thd->selstart.y - w;
|
|
|
|
b = HT_LINE | HT_DIR_HU;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (y > thd->selstart.y) {
|
2009-03-15 00:32:18 +00:00
|
|
|
/* east */
|
2005-07-17 20:14:58 +00:00
|
|
|
if (d == 0) {
|
2009-04-18 21:10:36 +00:00
|
|
|
b = (x & TILE_UNIT_MASK) + (y & TILE_UNIT_MASK) >= TILE_SIZE ? HT_LINE | HT_DIR_HL : HT_LINE | HT_DIR_HU;
|
2005-07-17 20:14:58 +00:00
|
|
|
} else if (d >= 0) {
|
|
|
|
x = thd->selstart.x - h;
|
|
|
|
b = HT_LINE | HT_DIR_HU;
|
|
|
|
} else {
|
|
|
|
y = thd->selstart.y + w;
|
|
|
|
b = HT_LINE | HT_DIR_HL;
|
2009-04-18 21:10:36 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2009-03-15 00:32:18 +00:00
|
|
|
/* north */
|
2005-07-17 20:14:58 +00:00
|
|
|
if (d == 0) {
|
2009-04-18 21:10:36 +00:00
|
|
|
b = (x & TILE_UNIT_MASK) > (y & TILE_UNIT_MASK) ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR;
|
2005-07-17 20:14:58 +00:00
|
|
|
} else if (d >= 0) {
|
|
|
|
x = thd->selstart.x - h;
|
|
|
|
b = HT_LINE | HT_DIR_VR;
|
|
|
|
} else {
|
|
|
|
y = thd->selstart.y - w;
|
|
|
|
b = HT_LINE | HT_DIR_VL;
|
2009-04-18 21:10:36 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-10-12 15:13:40 +00:00
|
|
|
|
2008-05-29 15:13:28 +00:00
|
|
|
if (_settings_client.gui.measure_tooltip) {
|
2006-10-12 15:13:40 +00:00
|
|
|
TileIndex t0 = TileVirtXY(thd->selstart.x, thd->selstart.y);
|
|
|
|
TileIndex t1 = TileVirtXY(x, y);
|
|
|
|
uint distance = DistanceManhattan(t0, t1) + 1;
|
2006-10-18 14:20:10 +00:00
|
|
|
byte index = 0;
|
2007-06-21 19:08:47 +00:00
|
|
|
uint64 params[2];
|
2006-10-12 15:13:40 +00:00
|
|
|
|
2006-10-18 14:20:10 +00:00
|
|
|
if (distance != 1) {
|
|
|
|
int heightdiff = CalcHeightdiff(b, distance, t0, t1);
|
|
|
|
/* If we are showing a tooltip for horizontal or vertical drags,
|
|
|
|
* 2 tiles have a length of 1. To bias towards the ceiling we add
|
|
|
|
* one before division. It feels more natural to count 3 lengths as 2 */
|
|
|
|
if ((b & HT_DIR_MASK) != HT_DIR_X && (b & HT_DIR_MASK) != HT_DIR_Y) {
|
2010-04-18 14:56:05 +00:00
|
|
|
distance = CeilDiv(distance, 2);
|
2006-10-18 14:20:10 +00:00
|
|
|
}
|
2006-10-12 15:13:40 +00:00
|
|
|
|
2006-10-18 14:20:10 +00:00
|
|
|
params[index++] = distance;
|
|
|
|
if (heightdiff != 0) params[index++] = heightdiff;
|
2006-10-12 15:13:40 +00:00
|
|
|
}
|
2006-10-18 14:20:10 +00:00
|
|
|
|
2008-08-02 11:26:25 +00:00
|
|
|
ShowMeasurementTooltips(measure_strings_length[index], index, params);
|
2006-10-12 15:13:40 +00:00
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
thd->selend.x = x;
|
|
|
|
thd->selend.y = y;
|
|
|
|
thd->next_drawstyle = b;
|
|
|
|
}
|
|
|
|
|
2006-10-10 14:05:39 +00:00
|
|
|
/**
|
|
|
|
* Selects tiles while dragging
|
|
|
|
* @param x X coordinate of end of selection
|
|
|
|
* @param y Y coordinate of end of selection
|
|
|
|
* @param method modifies the way tiles are selected. Possible
|
2010-08-01 19:44:49 +00:00
|
|
|
* methods are VPM_* in viewport.h
|
|
|
|
*/
|
2007-11-15 18:21:59 +00:00
|
|
|
void VpSelectTilesWithMethod(int x, int y, ViewportPlaceMethod method)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-10-10 14:05:39 +00:00
|
|
|
int sx, sy;
|
2006-10-12 15:13:40 +00:00
|
|
|
HighLightStyle style;
|
2005-05-27 15:05:54 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (x == -1) {
|
2005-05-27 15:05:54 +00:00
|
|
|
_thd.selend.x = -1;
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-10-10 14:05:39 +00:00
|
|
|
/* Special handling of drag in any (8-way) direction */
|
2009-04-19 21:26:06 +00:00
|
|
|
if (method & (VPM_RAILDIRS | VPM_SIGNALDIRS)) {
|
2005-05-27 15:05:54 +00:00
|
|
|
_thd.selend.x = x;
|
|
|
|
_thd.selend.y = y;
|
|
|
|
CalcRaildirsDrawstyle(&_thd, x, y, method);
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-11-18 20:05:44 +00:00
|
|
|
/* Needed so level-land is placed correctly */
|
2005-07-17 20:14:58 +00:00
|
|
|
if (_thd.next_drawstyle == HT_POINT) {
|
2006-10-10 14:05:39 +00:00
|
|
|
x += TILE_SIZE / 2;
|
|
|
|
y += TILE_SIZE / 2;
|
2005-07-17 20:14:58 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-05-27 15:05:54 +00:00
|
|
|
sx = _thd.selstart.x;
|
|
|
|
sy = _thd.selstart.y;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-02-01 23:13:15 +00:00
|
|
|
int limit = 0;
|
|
|
|
|
2005-07-17 20:14:58 +00:00
|
|
|
switch (method) {
|
2009-03-15 00:32:18 +00:00
|
|
|
case VPM_X_OR_Y: // drag in X or Y direction
|
2007-11-19 18:58:04 +00:00
|
|
|
if (abs(sy - y) < abs(sx - x)) {
|
2006-06-27 21:25:53 +00:00
|
|
|
y = sy;
|
2006-10-12 15:13:40 +00:00
|
|
|
style = HT_DIR_X;
|
2006-06-27 21:25:53 +00:00
|
|
|
} else {
|
|
|
|
x = sx;
|
2006-10-12 15:13:40 +00:00
|
|
|
style = HT_DIR_Y;
|
2006-06-27 21:25:53 +00:00
|
|
|
}
|
2006-10-12 15:13:40 +00:00
|
|
|
goto calc_heightdiff_single_direction;
|
2010-02-01 23:13:15 +00:00
|
|
|
|
|
|
|
case VPM_X_LIMITED: // Drag in X direction (limited size).
|
|
|
|
limit = (_thd.sizelimit - 1) * TILE_SIZE;
|
2010-07-29 14:26:28 +00:00
|
|
|
/* FALL THROUGH */
|
2010-02-01 23:13:15 +00:00
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
case VPM_FIX_X: // drag in Y direction
|
2006-10-12 15:13:40 +00:00
|
|
|
x = sx;
|
|
|
|
style = HT_DIR_Y;
|
|
|
|
goto calc_heightdiff_single_direction;
|
2010-02-01 23:13:15 +00:00
|
|
|
|
|
|
|
case VPM_Y_LIMITED: // Drag in Y direction (limited size).
|
|
|
|
limit = (_thd.sizelimit - 1) * TILE_SIZE;
|
2010-07-29 14:26:28 +00:00
|
|
|
/* FALL THROUGH */
|
2010-02-01 23:13:15 +00:00
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
case VPM_FIX_Y: // drag in X direction
|
2006-10-12 15:13:40 +00:00
|
|
|
y = sy;
|
|
|
|
style = HT_DIR_X;
|
|
|
|
|
|
|
|
calc_heightdiff_single_direction:;
|
2010-02-01 23:13:15 +00:00
|
|
|
if (limit > 0) {
|
|
|
|
x = sx + Clamp(x - sx, -limit, limit);
|
|
|
|
y = sy + Clamp(y - sy, -limit, limit);
|
|
|
|
}
|
2008-05-29 15:13:28 +00:00
|
|
|
if (_settings_client.gui.measure_tooltip) {
|
2006-10-12 15:13:40 +00:00
|
|
|
TileIndex t0 = TileVirtXY(sx, sy);
|
|
|
|
TileIndex t1 = TileVirtXY(x, y);
|
|
|
|
uint distance = DistanceManhattan(t0, t1) + 1;
|
2006-10-18 14:20:10 +00:00
|
|
|
byte index = 0;
|
2007-06-21 19:08:47 +00:00
|
|
|
uint64 params[2];
|
2006-10-12 15:13:40 +00:00
|
|
|
|
2006-10-18 14:20:10 +00:00
|
|
|
if (distance != 1) {
|
|
|
|
/* With current code passing a HT_LINE style to calculate the height
|
|
|
|
* difference is enough. However if/when a point-tool is created
|
|
|
|
* with this method, function should be called with new_style (below)
|
|
|
|
* instead of HT_LINE | style case HT_POINT is handled specially
|
|
|
|
* new_style := (_thd.next_drawstyle & HT_RECT) ? HT_LINE | style : _thd.next_drawstyle; */
|
|
|
|
int heightdiff = CalcHeightdiff(HT_LINE | style, 0, t0, t1);
|
|
|
|
|
|
|
|
params[index++] = distance;
|
|
|
|
if (heightdiff != 0) params[index++] = heightdiff;
|
2006-10-12 15:13:40 +00:00
|
|
|
}
|
2006-10-18 14:20:10 +00:00
|
|
|
|
2008-08-02 11:26:25 +00:00
|
|
|
ShowMeasurementTooltips(measure_strings_length[index], index, params);
|
2010-08-01 18:53:30 +00:00
|
|
|
}
|
2010-08-04 07:31:29 +00:00
|
|
|
break;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-02-01 23:13:15 +00:00
|
|
|
case VPM_X_AND_Y_LIMITED: // Drag an X by Y constrained rect area.
|
|
|
|
limit = (_thd.sizelimit - 1) * TILE_SIZE;
|
2007-11-19 18:38:10 +00:00
|
|
|
x = sx + Clamp(x - sx, -limit, limit);
|
|
|
|
y = sy + Clamp(y - sy, -limit, limit);
|
2010-07-29 14:26:28 +00:00
|
|
|
/* FALL THROUGH */
|
2010-02-01 23:13:15 +00:00
|
|
|
|
2010-08-04 07:31:29 +00:00
|
|
|
case VPM_X_AND_Y: // drag an X by Y area
|
2009-02-08 15:45:34 +00:00
|
|
|
if (_settings_client.gui.measure_tooltip) {
|
2006-10-18 14:20:10 +00:00
|
|
|
static const StringID measure_strings_area[] = {
|
|
|
|
STR_NULL, STR_NULL, STR_MEASURE_AREA, STR_MEASURE_AREA_HEIGHTDIFF
|
|
|
|
};
|
|
|
|
|
2006-10-12 15:13:40 +00:00
|
|
|
TileIndex t0 = TileVirtXY(sx, sy);
|
|
|
|
TileIndex t1 = TileVirtXY(x, y);
|
2007-11-26 16:01:29 +00:00
|
|
|
uint dx = Delta(TileX(t0), TileX(t1)) + 1;
|
|
|
|
uint dy = Delta(TileY(t0), TileY(t1)) + 1;
|
2006-10-18 14:20:10 +00:00
|
|
|
byte index = 0;
|
2007-06-21 19:08:47 +00:00
|
|
|
uint64 params[3];
|
2006-10-12 15:13:40 +00:00
|
|
|
|
|
|
|
/* If dragging an area (eg dynamite tool) and it is actually a single
|
|
|
|
* row/column, change the type to 'line' to get proper calculation for height */
|
2008-01-09 09:45:45 +00:00
|
|
|
style = (HighLightStyle)_thd.next_drawstyle;
|
2006-10-12 15:13:40 +00:00
|
|
|
if (style & HT_RECT) {
|
|
|
|
if (dx == 1) {
|
|
|
|
style = HT_LINE | HT_DIR_Y;
|
|
|
|
} else if (dy == 1) {
|
|
|
|
style = HT_LINE | HT_DIR_X;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-18 14:20:10 +00:00
|
|
|
if (dx != 1 || dy != 1) {
|
|
|
|
int heightdiff = CalcHeightdiff(style, 0, t0, t1);
|
2006-10-12 15:13:40 +00:00
|
|
|
|
2006-10-18 14:20:10 +00:00
|
|
|
params[index++] = dx;
|
|
|
|
params[index++] = dy;
|
|
|
|
if (heightdiff != 0) params[index++] = heightdiff;
|
2006-10-12 15:13:40 +00:00
|
|
|
}
|
2006-10-18 14:20:10 +00:00
|
|
|
|
2008-08-02 11:26:25 +00:00
|
|
|
ShowMeasurementTooltips(measure_strings_area[index], index, params);
|
2006-10-12 15:13:40 +00:00
|
|
|
}
|
2010-08-04 07:31:29 +00:00
|
|
|
break;
|
2006-10-12 15:13:40 +00:00
|
|
|
|
|
|
|
default: NOT_REACHED();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-05-27 15:05:54 +00:00
|
|
|
_thd.selend.x = x;
|
|
|
|
_thd.selend.y = y;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-05-19 09:24:03 +00:00
|
|
|
/**
|
|
|
|
* Handle the mouse while dragging for placement/resizing.
|
2010-05-30 12:15:28 +00:00
|
|
|
* @return State of handling the event.
|
2008-05-19 09:24:03 +00:00
|
|
|
*/
|
2010-05-30 12:15:28 +00:00
|
|
|
EventState VpHandlePlaceSizingDrag()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2010-05-30 12:15:28 +00:00
|
|
|
if (_special_mouse_mode != WSM_SIZING) return ES_NOT_HANDLED;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-04 04:08:47 +00:00
|
|
|
/* stop drag mode if the window has been closed */
|
2008-05-10 13:46:36 +00:00
|
|
|
Window *w = FindWindowById(_thd.window_class, _thd.window_number);
|
2004-08-09 17:04:08 +00:00
|
|
|
if (w == NULL) {
|
|
|
|
ResetObjectToPlace();
|
2010-05-30 12:15:28 +00:00
|
|
|
return ES_HANDLED;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-04-04 04:08:47 +00:00
|
|
|
/* while dragging execute the drag procedure of the corresponding window (mostly VpSelectTilesWithMethod() ) */
|
2004-08-09 17:04:08 +00:00
|
|
|
if (_left_button_down) {
|
2008-05-10 13:46:36 +00:00
|
|
|
w->OnPlaceDrag(_thd.select_method, _thd.select_proc, GetTileBelowCursor());
|
2010-05-30 12:15:28 +00:00
|
|
|
return ES_HANDLED;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-04-04 04:08:47 +00:00
|
|
|
/* mouse button released..
|
|
|
|
* keep the selected tool, but reset it to the original mode. */
|
2004-09-10 19:02:27 +00:00
|
|
|
_special_mouse_mode = WSM_NONE;
|
2005-07-17 20:14:58 +00:00
|
|
|
if (_thd.next_drawstyle == HT_RECT) {
|
2009-04-19 10:31:30 +00:00
|
|
|
_thd.place_mode = HT_RECT;
|
2009-04-19 21:26:06 +00:00
|
|
|
} else if (_thd.select_method & VPM_SIGNALDIRS) {
|
2009-04-19 10:31:30 +00:00
|
|
|
_thd.place_mode = HT_RECT;
|
2009-04-19 21:26:06 +00:00
|
|
|
} else if (_thd.select_method & VPM_RAILDIRS) {
|
|
|
|
_thd.place_mode = (_thd.select_method & ~VPM_RAILDIRS) ? _thd.next_drawstyle : HT_RAIL;
|
2005-07-17 20:14:58 +00:00
|
|
|
} else {
|
2009-04-19 10:31:30 +00:00
|
|
|
_thd.place_mode = HT_POINT;
|
2005-07-17 20:14:58 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
SetTileSelectSize(1, 1);
|
|
|
|
|
2008-05-10 13:46:36 +00:00
|
|
|
w->OnPlaceMouseUp(_thd.select_method, _thd.select_proc, _thd.selend, TileVirtXY(_thd.selstart.x, _thd.selstart.y), TileVirtXY(_thd.selend.x, _thd.selend.y));
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2010-05-30 12:15:28 +00:00
|
|
|
return ES_HANDLED;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-01-21 01:38:13 +00:00
|
|
|
void SetObjectToPlaceWnd(CursorID icon, PaletteID pal, HighLightStyle mode, Window *w)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-01-14 19:57:49 +00:00
|
|
|
SetObjectToPlace(icon, pal, mode, w->window_class, w->window_number);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#include "table/animcursors.h"
|
|
|
|
|
2010-01-21 01:38:13 +00:00
|
|
|
void SetObjectToPlace(CursorID icon, PaletteID pal, HighLightStyle mode, WindowClass window_class, WindowNumber window_num)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-02-17 12:21:05 +00:00
|
|
|
/* undo clicking on button and drag & drop */
|
2009-04-19 10:31:30 +00:00
|
|
|
if (_thd.place_mode != HT_NONE || _special_mouse_mode == WSM_DRAGDROP) {
|
2008-05-15 20:52:28 +00:00
|
|
|
Window *w = FindWindowById(_thd.window_class, _thd.window_number);
|
|
|
|
if (w != NULL) {
|
|
|
|
/* Call the abort function, but set the window class to something
|
|
|
|
* that will never be used to avoid infinite loops. Setting it to
|
|
|
|
* the 'next' window class must not be done because recursion into
|
|
|
|
* this function might in some cases reset the newly set object to
|
|
|
|
* place or not properly reset the original selection. */
|
|
|
|
_thd.window_class = WC_INVALID;
|
|
|
|
w->OnPlaceObjectAbort();
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
SetTileSelectSize(1, 1);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-05-27 15:05:54 +00:00
|
|
|
_thd.make_square_red = false;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-04-19 10:31:30 +00:00
|
|
|
if (mode == HT_DRAG) { // HT_DRAG is for dragdropping trains in the depot window
|
|
|
|
mode = HT_NONE;
|
2004-08-09 17:04:08 +00:00
|
|
|
_special_mouse_mode = WSM_DRAGDROP;
|
|
|
|
} else {
|
|
|
|
_special_mouse_mode = WSM_NONE;
|
|
|
|
}
|
|
|
|
|
2005-05-27 15:05:54 +00:00
|
|
|
_thd.place_mode = mode;
|
|
|
|
_thd.window_class = window_class;
|
|
|
|
_thd.window_number = window_num;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-07-24 10:14:39 +00:00
|
|
|
if (mode == HT_SPECIAL) { // special tools, like tunnels or docks start with presizing mode
|
2004-08-09 17:04:08 +00:00
|
|
|
VpStartPreSizing();
|
2010-07-24 10:14:39 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2010-02-04 15:42:38 +00:00
|
|
|
if ((icon & ANIMCURSOR_FLAG) != 0) {
|
|
|
|
SetAnimatedMouseCursor(_animcursors[icon & ~ANIMCURSOR_FLAG]);
|
2008-05-15 14:41:56 +00:00
|
|
|
} else {
|
2007-01-14 19:57:49 +00:00
|
|
|
SetMouseCursor(icon, pal);
|
2008-05-15 14:41:56 +00:00
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void ResetObjectToPlace()
|
2005-01-22 20:23:18 +00:00
|
|
|
{
|
2009-04-19 10:31:30 +00:00
|
|
|
SetObjectToPlace(SPR_CURSOR_MOUSE, PAL_NONE, HT_NONE, WC_MAIN_WINDOW, 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|