Author: cazfi
Date: Sat Mar 22 23:40:41 2014
New Revision: 24711

URL: http://svn.gna.org/viewcvs/freeciv?rev=24711&view=rev
Log:
Added initial version of set_map_zoom() function.

See patch #4619

Modified:
    trunk/client/mapview_common.c
    trunk/client/mapview_common.h

Modified: trunk/client/mapview_common.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/client/mapview_common.c?rev=24711&r1=24710&r2=24711&view=diff
==============================================================================
--- trunk/client/mapview_common.c       (original)
+++ trunk/client/mapview_common.c       Sat Mar 22 23:40:41 2014
@@ -74,6 +74,7 @@
 bool can_slide = TRUE;
 
 float map_zoom = 1.0;
+struct tile *center_tile = NULL;
 
 static void base_canvas_to_map_pos(int *map_x, int *map_y,
                                   int canvas_x, int canvas_y);
@@ -586,28 +587,41 @@
 }
 
 /****************************************************************************
+  Adjust mapview origin values. Returns TRUE iff values are different from
+  current mapview.
+****************************************************************************/
+static bool calc_mapview_origin(int *gui_x0, int *gui_y0)
+{
+  int xmin, xmax, ymin, ymax, xsize, ysize;
+
+  /* Normalize (wrap) the mapview origin. */
+  normalize_gui_pos(tileset, gui_x0, gui_y0);
+
+  /* First wrap/clip the position.  Wrapping is done in native positions
+   * while clipping is done in scroll (native) positions. */
+  get_mapview_scroll_window(&xmin, &ymin, &xmax, &ymax, &xsize, &ysize);
+
+  if (!current_topo_has_flag(TF_WRAPX)) {
+    *gui_x0 = CLIP(xmin, *gui_x0, xmax - xsize);
+  }
+
+  if (!current_topo_has_flag(TF_WRAPY)) {
+    *gui_y0 = CLIP(ymin, *gui_y0, ymax - ysize);
+  }
+
+  if (mapview.gui_x0 == *gui_x0 && mapview.gui_y0 == *gui_y0) {
+    return FALSE;
+  }
+
+  return TRUE;
+}
+
+/****************************************************************************
   Change the mapview origin, clip it, and update everything.
 ****************************************************************************/
 void set_mapview_origin(int gui_x0, int gui_y0)
 {
-  int xmin, xmax, ymin, ymax, xsize, ysize;
-
-  /* Normalize (wrap) the mapview origin. */
-  normalize_gui_pos(tileset, &gui_x0, &gui_y0);
-
-  /* First wrap/clip the position.  Wrapping is done in native positions
-   * while clipping is done in scroll (native) positions. */
-  get_mapview_scroll_window(&xmin, &ymin, &xmax, &ymax, &xsize, &ysize);
-
-  if (!current_topo_has_flag(TF_WRAPX)) {
-    gui_x0 = CLIP(xmin, gui_x0, xmax - xsize);
-  }
-
-  if (!current_topo_has_flag(TF_WRAPY)) {
-    gui_y0 = CLIP(ymin, gui_y0, ymax - ysize);
-  }
-
-  if (mapview.gui_x0 == gui_x0 && mapview.gui_y0 == gui_y0) {
+  if (!calc_mapview_origin(&gui_x0, &gui_y0)) {
     return;
   }
 
@@ -821,7 +835,7 @@
 struct tile *get_center_tile_mapcanvas(void)
 {
   return canvas_pos_to_nearest_tile(mapview.width / 2,
-                                   mapview.height / 2);
+                                    mapview.height / 2);
 }
 
 /**************************************************************************
@@ -845,6 +859,8 @@
   gui_y -= (mapview.height - tileset_tile_height(tileset) * map_zoom) / 2;
 
   set_mapview_origin(gui_x, gui_y);
+
+  center_tile = ptile;
 }
 
 /**************************************************************************
@@ -3093,8 +3109,8 @@
     mapview.store = canvas_create(full_width, full_height);
     canvas_set_zoom(mapview.store, map_zoom);
     canvas_put_rectangle(mapview.store,
-                        get_color(tileset, COLOR_MAPVIEW_UNKNOWN),
-                        0, 0, full_width, full_height);
+                         get_color(tileset, COLOR_MAPVIEW_UNKNOWN),
+                         0, 0, full_width, full_height);
 
     mapview.tmp_store = canvas_create(full_width, full_height);
     canvas_set_zoom(mapview.tmp_store, map_zoom);
@@ -3102,6 +3118,21 @@
 
   if (map_exists() && can_client_change_view()) {
     if (tile_size_changed) {
+      if (center_tile != NULL) {
+        int x0, y0;
+        int gui_x, gui_y;
+
+        index_to_map_pos(&x0, &y0, tile_index(center_tile));
+        map_to_gui_pos(tileset, &gui_x, &gui_y, x0, y0);
+
+        /* Put the center pixel of the tile at the exact center of the 
mapview. */
+        gui_x -= (mapview.width - tileset_tile_width(tileset) * map_zoom) / 2;
+        gui_y -= (mapview.height - tileset_tile_height(tileset) * map_zoom) / 
2;
+
+        calc_mapview_origin(&gui_x, &gui_y);
+        mapview.gui_x0 = gui_x;
+        mapview.gui_y0 = gui_y;
+      }
       update_map_canvas_visible();
       center_tile_overviewcanvas();
       unqueue_mapview_updates(TRUE);
@@ -3119,6 +3150,16 @@
   mapview.can_do_cached_drawing = can_do_cached_drawing();
 
   return redrawn;
+}
+
+/**************************************************************************
+  Set map zoom level.
+**************************************************************************/
+void set_map_zoom(float new_zoom)
+{
+  map_zoom = new_zoom;
+
+  map_canvas_resized(mapview.width, mapview.height);
 }
 
 /**************************************************************************

Modified: trunk/client/mapview_common.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/client/mapview_common.h?rev=24711&r1=24710&r2=24711&view=diff
==============================================================================
--- trunk/client/mapview_common.h       (original)
+++ trunk/client/mapview_common.h       Sat Mar 22 23:40:41 2014
@@ -355,6 +355,8 @@
 void link_mark_add_new(enum text_link_type type, int id);
 void link_mark_restore(enum text_link_type type, int id);
 
+void set_map_zoom(float new_zoom);
+
 bool tileset_map_topo_compatible(int topology_id, struct tileset *tset);
 
 #ifdef __cplusplus


_______________________________________________
Freeciv-commits mailing list
Freeciv-commits@gna.org
https://mail.gna.org/listinfo/freeciv-commits

Reply via email to