Complete.Org:
Mailing Lists:
Archives:
freeciv-dev:
September 2003: [Freeciv-Dev] (PR#6272) scroll positions for the mapview |
![]() |
[Freeciv-Dev] (PR#6272) scroll positions for the mapview[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
This patch introduces "scroll positions" that can be used in scrolling and clipping the mapview window. The main advantage is that it hides the logic of how scrolling works behind a set of 3 functions. Thus changing the scrolling logic can be done without any updates to the GUI needed. This is useful both for gen-topologies and when trying to fix iso-view scrolling. Tested for gui-gtk and gui-xaw only. Untested support for gui-gtk-2.0 and gui-win32 is included. jason Index: client/mapview_common.c =================================================================== RCS file: /home/freeciv/CVS/freeciv/client/mapview_common.c,v retrieving revision 1.60 diff -u -r1.60 mapview_common.c --- client/mapview_common.c 2003/09/23 16:18:05 1.60 +++ client/mapview_common.c 2003/09/24 20:30:17 @@ -288,19 +288,49 @@ return normalize_map_pos(map_x, map_y); } -/************************************************************************** - Return the range of values that the mapview origin can take. Useful - for scrollbars or when manually clipping the window. -**************************************************************************/ -void get_mapview_clipping_window(int *xmin, int *ymin, - int *xmax, int *ymax, - int *xsize, int *ysize) +/**************************************************************************** + Return the range of values that the mapview origin can take, in scroll + positins. Useful for scrollbars or when manually clipping the window. +****************************************************************************/ +void get_mapview_scroll_window(int *xmin, int *ymin, int *xmax, int *ymax, + int *xsize, int *ysize) { *xmin = *ymin = 0; *xmax = map.xsize; *ymax = map.ysize + EXTRA_BOTTOM_ROW; *xsize = mapview_canvas.tile_width; *ysize = mapview_canvas.tile_height; +} + +/**************************************************************************** + Find the current scroll position (origin) of the mapview. +****************************************************************************/ +void get_mapview_scroll_pos(int *scroll_x, int *scroll_y) +{ + map_to_native_pos(scroll_x, scroll_y, + mapview_canvas.map_x0, mapview_canvas.map_y0); +} + +/**************************************************************************** + Set the scroll position (origin) of the mapview, and update the GUI. +****************************************************************************/ +void set_mapview_scroll_pos(int scroll_x, int scroll_y) +{ + int xmin, ymin, xmax, ymax, xsize, ysize, x0, y0; + + get_mapview_scroll_window(&xmin, &ymin, &xmax, &ymax, &xsize, &ysize); + native_to_map_pos(&x0, &y0, scroll_x, scroll_y); + + x0 = CLIP(xmin, x0, xmax - xsize); + y0 = CLIP(ymin, y0, ymax - ysize); + + if (x0 != mapview_canvas.map_x0 || y0 != mapview_canvas.map_y0) { + mapview_canvas.map_x0 = x0; + mapview_canvas.map_y0 = y0; + + update_map_canvas_visible(); + refresh_overview_viewrect(); + } } /************************************************************************** Index: client/mapview_common.h =================================================================== RCS file: /home/freeciv/CVS/freeciv/client/mapview_common.h,v retrieving revision 1.37 diff -u -r1.37 mapview_common.h --- client/mapview_common.h 2003/07/25 18:59:45 1.37 +++ client/mapview_common.h 2003/09/24 20:30:17 @@ -134,9 +134,11 @@ bool map_to_canvas_pos(int *canvas_x, int *canvas_y, int map_x, int map_y); bool canvas_to_map_pos(int *map_x, int *map_y, int canvas_x, int canvas_y); -void get_mapview_clipping_window(int *xmin, int *ymin, - int *xmax, int *ymax, - int *xsize, int *ysize); +void get_mapview_scroll_window(int *xmin, int *ymin, + int *xmax, int *ymax, + int *xsize, int *ysize); +void get_mapview_scroll_pos(int *scroll_x, int *scroll_y); +void set_mapview_scroll_pos(int scroll_x, int scroll_y); void get_center_tile_mapcanvas(int *map_x, int *map_y); void center_tile_mapcanvas(int map_x, int map_y); Index: client/gui-gtk/mapview.c =================================================================== RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk/mapview.c,v retrieving revision 1.180 diff -u -r1.180 mapview.c --- client/gui-gtk/mapview.c 2003/08/12 18:54:37 1.180 +++ client/gui-gtk/mapview.c 2003/09/24 20:30:17 @@ -1153,8 +1153,11 @@ **************************************************************************/ void update_map_canvas_scrollbars(void) { - gtk_adjustment_set_value(GTK_ADJUSTMENT(map_hadj), map_view_x0); - gtk_adjustment_set_value(GTK_ADJUSTMENT(map_vadj), map_view_y0); + int scroll_x, scroll_y; + + get_mapview_scroll_pos(&scroll_x, &scroll_y); + gtk_adjustment_set_value(GTK_ADJUSTMENT(map_hadj), scroll_x); + gtk_adjustment_set_value(GTK_ADJUSTMENT(map_vadj), scroll_y); } /************************************************************************** @@ -1164,7 +1167,7 @@ { int xmin, ymin, xmax, ymax, xsize, ysize; - get_mapview_clipping_window(&xmin, &ymin, &xmax, &ymax, &xsize, &ysize); + get_mapview_scroll_window(&xmin, &ymin, &xmax, &ymax, &xsize, &ysize); map_hadj = gtk_adjustment_new(-1, xmin, xmax, 1, xsize, xsize); map_vadj = gtk_adjustment_new(-1, ymin, ymax, 1, ysize, ysize); @@ -1187,28 +1190,21 @@ **************************************************************************/ void scrollbar_jump_callback(GtkAdjustment *adj, gpointer hscrollbar) { - int last_map_view_x0; - int last_map_view_y0; - - gfloat percent=adj->value; + int scroll_x, scroll_y; if (!can_client_change_view()) { return; } - last_map_view_x0=map_view_x0; - last_map_view_y0=map_view_y0; + get_mapview_scroll_pos(&scroll_x, &scroll_y); - if(hscrollbar) - map_view_x0=percent; - else { - map_view_y0=percent; + if (hscrollbar) { + scroll_x = adj->value; + } else { + scroll_y = adj->value; } - if (last_map_view_x0!=map_view_x0 || last_map_view_y0!=map_view_y0) { - update_map_canvas_visible(); - refresh_overview_viewrect(); - } + set_mapview_scroll_pos(scroll_x, scroll_y); } Index: client/gui-gtk-2.0/mapview.c =================================================================== RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk-2.0/mapview.c,v retrieving revision 1.74 diff -u -r1.74 mapview.c --- client/gui-gtk-2.0/mapview.c 2003/08/12 18:54:37 1.74 +++ client/gui-gtk-2.0/mapview.c 2003/09/24 20:30:17 @@ -1236,8 +1236,11 @@ **************************************************************************/ void update_map_canvas_scrollbars(void) { - gtk_adjustment_set_value(GTK_ADJUSTMENT(map_hadj), map_view_x0); - gtk_adjustment_set_value(GTK_ADJUSTMENT(map_vadj), map_view_y0); + int scroll_x, scroll_y; + + get_mapview_scroll_pos(&scroll_x, &scroll_y); + gtk_adjustment_set_value(GTK_ADJUSTMENT(map_hadj), scroll_x); + gtk_adjustment_set_value(GTK_ADJUSTMENT(map_vadj), scroll_y); } /************************************************************************** @@ -1247,7 +1250,7 @@ { int xmin, ymin, xmax, ymax, xsize, ysize; - get_mapview_clipping_window(&xmin, &ymin, &xmax, &ymax, &xsize, &ysize); + get_mapview_scroll_window(&xmin, &ymin, &xmax, &ymax, &xsize, &ysize); map_hadj = gtk_adjustment_new(-1, xmin, xmax, 1, xsize, xsize); map_vadj = gtk_adjustment_new(-1, ymin, ymax, 1, ysize, ysize); @@ -1270,28 +1273,21 @@ **************************************************************************/ void scrollbar_jump_callback(GtkAdjustment *adj, gpointer hscrollbar) { - int last_map_view_x0; - int last_map_view_y0; - - gfloat percent=adj->value; + int scroll_x, scroll_y; if (!can_client_change_view()) { return; } - last_map_view_x0=map_view_x0; - last_map_view_y0=map_view_y0; + get_mapview_scroll_pos(&scroll_x, &scroll_y); - if(hscrollbar) - map_view_x0=percent; - else { - map_view_y0=percent; + if (hscrollbar) { + scroll_x = adj->value; + } else { + scroll_y = adj->value; } - if (last_map_view_x0!=map_view_x0 || last_map_view_y0!=map_view_y0) { - update_map_canvas_visible(); - refresh_overview_viewrect(); - } + set_mapview_scroll_pos(scroll_x, scroll_y); } Index: client/gui-win32/mapview.c =================================================================== RCS file: /home/freeciv/CVS/freeciv/client/gui-win32/mapview.c,v retrieving revision 1.78 diff -u -r1.78 mapview.c --- client/gui-win32/mapview.c 2003/08/06 16:10:08 1.78 +++ client/gui-win32/mapview.c 2003/09/24 20:30:17 @@ -590,8 +590,7 @@ { int xmin, ymin, xmax, ymax, xsize, ysize; - get_mapview_clipping_window(&xmin, &ymin, &xmax, &ymax, &xsize, &ysize); - + get_mapview_scroll_window(&xmin, &ymin, &xmax, &ymax, &xsize, &ysize); ScrollBar_SetRange(map_scroll_h, xmin, xmax, TRUE); ScrollBar_SetRange(map_scroll_v, ymin, ymax, TRUE); } @@ -602,8 +601,11 @@ void update_map_canvas_scrollbars(void) { - ScrollBar_SetPos(map_scroll_h,map_view_x,TRUE); - ScrollBar_SetPos(map_scroll_v,map_view_y,TRUE); + int scroll_x, scroll_y; + + get_mapview_scroll_pos(&scroll_x, &scroll_y); + ScrollBar_SetPos(map_scroll_h, scroll_x, TRUE); + ScrollBar_SetPos(map_scroll_v, scroll_y, TRUE); } /************************************************************************** @@ -1010,17 +1012,14 @@ **************************************************************************/ void map_handle_hscroll(int pos) { - int xmin, ymin, xmax, ymax, xsize, ysize; + int scroll_x, scroll_y; if (!can_client_change_view()) { return; } - get_mapview_clipping_window(&xmin, &ymin, &xmax, &ymax, &xsize, &ysize); - map_view_x = CLIP(xmin, pos, xmax - xsize); - - update_map_canvas_visible(); - refresh_overview_viewrect(); + set_mapview_scroll_pos(&scroll_x, &scroll_y); + set_mapview_scroll_pos(pos, scroll_y); } /************************************************************************** @@ -1028,17 +1027,14 @@ **************************************************************************/ void map_handle_vscroll(int pos) { - int xmin, ymin, xmax, ymax, xsize, ysize; + int scroll_x, scroll_y; if (!can_client_change_view()) { return; } - - get_mapview_clipping_window(&xmin, &ymin, &xmax, &ymax, &xsize, &ysize); - map_view_y = CLIP(ymin, pos, ymax - ysize); - update_map_canvas_visible(); - refresh_overview_viewrect(); + set_mapview_scroll_pos(&scroll_x, &scroll_y); + set_mapview_scroll_pos(scroll_x, pos); } /************************************************************************** Index: client/gui-xaw/mapview.c =================================================================== RCS file: /home/freeciv/CVS/freeciv/client/gui-xaw/mapview.c,v retrieving revision 1.147 diff -u -r1.147 mapview.c --- client/gui-xaw/mapview.c 2003/08/06 07:34:38 1.147 +++ client/gui-xaw/mapview.c 2003/09/24 20:30:18 @@ -744,11 +744,13 @@ { float shown_h, top_h, shown_v, top_v; int xmin, ymin, xmax, ymax, xsize, ysize; + int scroll_x, scroll_y; - get_mapview_clipping_window(&xmin, &ymin, &xmax, &ymax, &xsize, &ysize); + get_mapview_scroll_window(&xmin, &ymin, &xmax, &ymax, &xsize, &ysize); + get_mapview_scroll_pos(&scroll_x, &scroll_y); - top_h = (float)(map_view_x0 - xmin) / (float)(xmax - xmin); - top_v = (float)(map_view_y0 - ymin) / (float)(ymax - ymin); + top_h = (float)(scroll_x - xmin) / (float)(xmax - xmin); + top_v = (float)(scroll_y - ymin) / (float)(ymax - ymin); shown_h = (float)xsize / (float)(xmax - xmin); shown_v = (float)ysize / (float)(ymax - ymin); @@ -1027,25 +1029,22 @@ { float percent=*(float*)percent_ptr; int xmin, ymin, xmax, ymax, xsize, ysize; + int scroll_x, scroll_y; - get_mapview_clipping_window(&xmin, &ymin, &xmax, &ymax, &xsize, &ysize); - if (!can_client_change_view()) { return; } + get_mapview_scroll_window(&xmin, &ymin, &xmax, &ymax, &xsize, &ysize); + get_mapview_scroll_pos(&scroll_x, &scroll_y); + if(w==map_horizontal_scrollbar) { - map_view_x0 = xmin + (percent * (xmax - xmin)); - map_view_x0 = CLIP(xmin, map_view_x0, xmax - xsize); + scroll_x = xmin + (percent * (xmax - xmin)); } else { - map_view_y0 = ymin + (percent * (ymax - ymin)); - map_view_y0 = CLIP(ymin, map_view_y0, ymax - ysize); + scroll_y = ymin + (percent * (ymax - ymin)); } - update_map_canvas_visible(); - /* The scrollbar tracks by itself, while calling the jumpProc, - so there's no need to call update_map_canvas_scrollbars() here. */ - refresh_overview_viewrect(); + set_mapview_scroll_pos(scroll_x, scroll_y); } @@ -1056,9 +1055,9 @@ XtPointer position_val) { int position = XTPOINTER_TO_INT(position_val); - int xmin, ymin, xmax, ymax, xsize, ysize; + int scroll_x, scroll_y; - get_mapview_clipping_window(&xmin, &ymin, &xmax, &ymax, &xsize, &ysize); + get_mapview_scroll_pos(&scroll_x, &scroll_y); if (!can_client_change_view()) { return; @@ -1066,24 +1065,21 @@ if(w==map_horizontal_scrollbar) { if (position > 0) { - map_view_x0++; + scroll_x++; } else { - map_view_x0--; + scroll_x--; } - map_view_x0 = CLIP(xmin, map_view_x0, xmax - xsize); } else { if (position > 0) { - map_view_y0++; + scroll_y++; } else { - map_view_y0--; + scroll_y--; } - map_view_y0 = CLIP(ymin, map_view_y0, ymax - ysize); } - update_map_canvas_visible(); + set_mapview_scroll_pos(scroll_x, scroll_y); update_map_canvas_scrollbars(); - refresh_overview_viewrect(); } /**************************************************************************
|