src/viewport.cpp
branchnoai
changeset 9629 66dde6412125
parent 9628 b5c2449616b5
child 9694 e72987579514
--- a/src/viewport.cpp	Sun Jun 17 21:31:00 2007 +0000
+++ b/src/viewport.cpp	Tue Jun 26 23:40:58 2007 +0000
@@ -58,7 +58,7 @@
 	StringSpriteToDraw *next;
 	int32 x;
 	int32 y;
-	uint32 params[2];
+	uint64 params[2];
 	uint16 width;
 };
 
@@ -580,7 +580,7 @@
 }
 
 /* Returns a StringSpriteToDraw */
-void *AddStringToDraw(int x, int y, StringID string, uint32 params_1, uint32 params_2)
+void *AddStringToDraw(int x, int y, StringID string, uint64 params_1, uint64 params_2)
 {
 	ViewportDrawer *vd = _cur_vd;
 	StringSpriteToDraw *ss;
@@ -649,6 +649,10 @@
 
 #include "table/autorail.h"
 
+/**
+ * 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)
 {
 	SpriteID image;
@@ -939,7 +943,7 @@
 {
 	StringSpriteToDraw *sstd;
 
-	sstd = (StringSpriteToDraw*)AddStringToDraw(si->sign.left + 1, si->sign.top + 1, str, si->str, 0);
+	sstd = (StringSpriteToDraw*)AddStringToDraw(si->sign.left + 1, si->sign.top + 1, str, si->index, 0);
 	if (sstd != NULL) {
 		sstd->color = (si->owner == OWNER_NONE) ? 14 : _player_colors[si->owner];
 		sstd->width = width;
@@ -1361,6 +1365,30 @@
 	dpi->top -= w->top;
 }
 
+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 */
+	vx = clamp(vx, 0, MapMaxX() * TILE_SIZE * 4);
+	vy = clamp(vy, 0, MapMaxY() * TILE_SIZE * 4);
+
+	/* Convert map coordinates to viewport coordinates */
+	x = (-vx + vy) / 2;
+	y = ( vx + vy) / 4;
+
+	/* Remove centreing */
+	x -= vp->virtual_width / 2;
+	y -= vp->virtual_height / 2;
+}
+
 void UpdateViewportPosition(Window *w)
 {
 	const ViewPort *vp = w->viewport;
@@ -1371,47 +1399,25 @@
 
 		SetViewportPosition(w, pt.x, pt.y);
 	} else {
-		int x;
-		int y;
-		int vx;
-		int vy;
-
-		/* Center of the viewport is hot spot */
-		x = WP(w,vp_d).scrollpos_x + vp->virtual_width / 2;
-		y = WP(w,vp_d).scrollpos_y + vp->virtual_height / 2;
-
-		int dest_x = WP(w,vp_d).dest_scrollpos_x + vp->virtual_width / 2;
-		int dest_y = WP(w,vp_d).dest_scrollpos_y + vp->virtual_height / 2;
-
-		int delta_x = dest_x - x;
-		int delta_y = dest_y - y;
+		/* Ensure the destination location is within the map */
+		ClampViewportToMap(vp, WP(w, vp_d).dest_scrollpos_x, WP(w, vp_d).dest_scrollpos_y);
+
+		int delta_x = WP(w, vp_d).dest_scrollpos_x - WP(w, vp_d).scrollpos_x;
+		int delta_y = WP(w, vp_d).dest_scrollpos_y - WP(w, vp_d).scrollpos_y;
 
 		if (delta_x != 0 || delta_y != 0) {
 			if (_patches.smooth_scroll) {
 				int max_scroll = ScaleByMapSize1D(512);
 				/* Not at our desired positon yet... */
-				x += clamp(delta_x / 8, -max_scroll, max_scroll);
-				y += clamp(delta_y / 8, -max_scroll, max_scroll);
+				WP(w, vp_d).scrollpos_x += clamp(delta_x / 4, -max_scroll, max_scroll);
+				WP(w, vp_d).scrollpos_y += clamp(delta_y / 4, -max_scroll, max_scroll);
 			} else {
-				x = dest_x;
-				y = dest_y;
+				WP(w, vp_d).scrollpos_x = WP(w, vp_d).dest_scrollpos_x;
+				WP(w, vp_d).scrollpos_y = WP(w, vp_d).dest_scrollpos_y;
 			}
 		}
 
-		/* Convert viewport coordinates to map coordinates
-		 * Calculation is scaled by 4 to avoid rounding errors */
-		vx = -x + y * 2;
-		vy =  x + y * 2;
-		/* clamp to size of map */
-		vx = clamp(vx, 0 * 4, MapMaxX() * TILE_SIZE * 4);
-		vy = clamp(vy, 0 * 4, MapMaxY() * TILE_SIZE * 4);
-		/* Convert map coordinates to viewport coordinates */
-		x = (-vx + vy) / 2;
-		y = ( vx + vy) / 4;
-		/* Set position */
-		WP(w, vp_d).scrollpos_x = x - vp->virtual_width / 2;
-		WP(w, vp_d).scrollpos_y = y - vp->virtual_height / 2;
-
+		ClampViewportToMap(vp, WP(w, vp_d).scrollpos_x, WP(w, vp_d).scrollpos_y);
 		SetViewportPosition(w, WP(w, vp_d).scrollpos_x, WP(w, vp_d).scrollpos_y);
 	}
 }
@@ -1928,7 +1934,14 @@
 	return HT_RAIL | _AutorailPiece[x & 0xF][y & 0xF];
 }
 
-/** called regular to update tile highlighting in all cases */
+/**
+ * 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
+ * Called by MouseLoop() in windows.cpp
+ */
 void UpdateTileSelection()
 {
 	int x1;
@@ -1979,8 +1992,8 @@
 	if (_thd.drawstyle != _thd.new_drawstyle ||
 			_thd.pos.x != _thd.new_pos.x || _thd.pos.y != _thd.new_pos.y ||
 			_thd.size.x != _thd.new_size.x || _thd.size.y != _thd.new_size.y ||
-	    _thd.outersize.x != _thd.new_outersize.x ||
-	    _thd.outersize.y != _thd.new_outersize.y) {
+			_thd.outersize.x != _thd.new_outersize.x ||
+			_thd.outersize.y != _thd.new_outersize.y) {
 		/* clear the old selection? */
 		if (_thd.drawstyle) SetSelectionTilesDirty();
 
@@ -2028,7 +2041,7 @@
 * @param to TileIndex of the last tile to highlight */
 void VpSetPresizeRange(TileIndex from, TileIndex to)
 {
-	uint distance = DistanceManhattan(from, to) + 1;
+	uint64 distance = DistanceManhattan(from, to) + 1;
 
 	_thd.selend.x = TileX(to) * TILE_SIZE;
 	_thd.selend.y = TileY(to) * TILE_SIZE;
@@ -2304,7 +2317,7 @@
 		TileIndex t1 = TileVirtXY(x, y);
 		uint distance = DistanceManhattan(t0, t1) + 1;
 		byte index = 0;
-		uint params[2];
+		uint64 params[2];
 
 		if (distance != 1) {
 			int heightdiff = CalcHeightdiff(b, distance, t0, t1);
@@ -2383,7 +2396,7 @@
 				TileIndex t1 = TileVirtXY(x, y);
 				uint distance = DistanceManhattan(t0, t1) + 1;
 				byte index = 0;
-				uint params[2];
+				uint64 params[2];
 
 				if (distance != 1) {
 					/* With current code passing a HT_LINE style to calculate the height
@@ -2416,7 +2429,7 @@
 				uint dx = delta(TileX(t0), TileX(t1)) + 1;
 				uint dy = delta(TileY(t0), TileY(t1)) + 1;
 				byte index = 0;
-				uint params[3];
+				uint64 params[3];
 
 				/* 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 */