[Freeciv-Dev] Re: PATCH: cityview unification (PR#1085)
[Top] [All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Vasco Alexandre Da Silva Costa wrote:
On Mon, 3 Dec 2001 jdorje@xxxxxxxxxxxxxxxxxxxxx wrote:
The attached patch unifies city_get_canvas_xy/city_get_map_xy from all
GUI's (except xaw) into one set of common functions. I did this just
because I've grown tired of seeing the identical code in so many places.
Some issues:
- These functions should rightfully go into a file cityview_common.[ch].
However, no such file currently exists so I've placed them into
mapview_common.[ch]. I'll fix this if desired (creating the new file).
That would be a better idea, yes.
Done.
- I don't clean up the functions any; I just copy them as-is. I would
like to clean them up, though (at the same time that I clean up the
corresponding mapview functions).
- I've changed the names from city_get_[canvas|map]_xy to
canvas_pos_to_city_pos/city_pos_to_canvas_pos. This results in a lot of
extra bloat to the diff file, since these name changes were made in many
places. If desired, I'll undo this - but the new name seems much more
logical and matches the corresponding mapview functions.
Hmm. What is it with you people and long function names? :-)
I would name it canvas_to_city_pos() and city_to_canvas_pos() but i don't
really care that much about that.
I blame it all on Raimar for accepting (or proposing?) the
map_pos_to_canvas_pos/canvas_pos_to_map_pos names. :-)
Aside from general code cleanup, this isn't a crucial change. It does
make the codebase smaller and IMO more maintainable. It will make it
easier to supply isometric-view to the xaw client (since the xaw client
can now use these functions). And it will make it easier for me to
clean up these functions to match the mapview ones (it will be necessary
to clean up the mapview ones for the general-topologies change, and
would be very bad IMO to not clean up this code at the same time).
Excellent! Please proceed!
New patch attached. This is identical except new files,
citydlg_common.[ch] have been created (I know I said cityview_common
before, but it turns out there's no cityview.[ch] at all, it was all
citydlg). Everything's been moved out of mapview_common.
It passes a minimal testing with the GTK client. The code's really not
different enough to think that anything might not work, though (so long
as everything compiles).
(Note: the patch must be applied with -p1, not -p0.)
jason
diff -Naur freeciv-cvs-pristine/client/Makefile.am
freeciv-cityview-unif/client/Makefile.am
--- freeciv-cvs-pristine/client/Makefile.am Thu Nov 1 11:37:29 2001
+++ freeciv-cityview-unif/client/Makefile.am Tue Dec 4 21:36:57 2001
@@ -106,6 +106,8 @@
civclient_SOURCES = \
attribute.h \
attribute.c \
+ citydlg_common.c \
+ citydlg_common.h \
cityrepdata.c \
cityrepdata.h \
civclient.c \
diff -Naur freeciv-cvs-pristine/client/citydlg_common.c
freeciv-cityview-unif/client/citydlg_common.c
--- freeciv-cvs-pristine/client/citydlg_common.c Wed Dec 31 19:00:00 1969
+++ freeciv-cityview-unif/client/citydlg_common.c Tue Dec 4 21:37:06 2001
@@ -0,0 +1,79 @@
+/**********************************************************************
+ Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
+ This program 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; either version 2, or (at your option)
+ any later version.
+
+ This program 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.
+***********************************************************************/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "log.h"
+
+#include "tilespec.h" /* for is_isometric */
+
+/**************************************************************************
+This converts a city coordinate position to citymap canvas coordinates
+(either isometric or overhead). It should be in cityview.c instead.
+**************************************************************************/
+void city_pos_to_canvas_pos(int city_x, int city_y, int *canvas_x, int
*canvas_y)
+{
+ if (is_isometric) {
+ int diff_xy;
+
+ /* The line at y=0 isometric has constant x+y=1(tiles) */
+ diff_xy = (city_x + city_y) - (1);
+ *canvas_y = diff_xy/2 * NORMAL_TILE_HEIGHT + (diff_xy%2) *
(NORMAL_TILE_HEIGHT/2);
+
+ /* The line at x=0 isometric has constant x-y=-3(tiles) */
+ diff_xy = city_x - city_y;
+ *canvas_x = (diff_xy + 3) * NORMAL_TILE_WIDTH/2;
+ } else {
+ *canvas_x = city_x * NORMAL_TILE_WIDTH;
+ *canvas_y = city_y * NORMAL_TILE_HEIGHT;
+ }
+}
+
+/**************************************************************************
+This converts a citymap canvas position to a city coordinate position
+(either isometric or overhead). It should be in cityview.c instead.
+**************************************************************************/
+void canvas_pos_to_city_pos(int canvas_x, int canvas_y, int *map_x, int *map_y)
+{
+ if (is_isometric) {
+ *map_x = -2;
+ *map_y = 2;
+
+ /* first find an equivalent position on the left side of the screen. */
+ *map_x += canvas_x / NORMAL_TILE_WIDTH;
+ *map_y -= canvas_x / NORMAL_TILE_WIDTH;
+ canvas_x %= NORMAL_TILE_WIDTH;
+
+ /* Then move op to the top corner. */
+ *map_x += canvas_y / NORMAL_TILE_HEIGHT;
+ *map_y += canvas_y / NORMAL_TILE_HEIGHT;
+ canvas_y %= NORMAL_TILE_HEIGHT;
+
+ assert(NORMAL_TILE_WIDTH == 2 * NORMAL_TILE_HEIGHT);
+ canvas_y *= 2; /* now we have a square. */
+ if (canvas_x + canvas_y > NORMAL_TILE_WIDTH / 2)
+ (*map_x)++;
+ if (canvas_x + canvas_y > 3 * NORMAL_TILE_WIDTH / 2)
+ (*map_x)++;
+ if (canvas_x - canvas_y > NORMAL_TILE_WIDTH / 2)
+ (*map_y)--;
+ if (canvas_y - canvas_x > NORMAL_TILE_WIDTH / 2)
+ (*map_y)++;
+ } else {
+ *map_x = canvas_x / NORMAL_TILE_WIDTH;
+ *map_y = canvas_y / NORMAL_TILE_HEIGHT;
+ }
+ freelog(LOG_DEBUG, "canvas_pos_to_city_pos(pos=(%d,%d))=(%d,%d)", canvas_x,
canvas_y, *map_x, *map_y);
+}
diff -Naur freeciv-cvs-pristine/client/citydlg_common.h
freeciv-cityview-unif/client/citydlg_common.h
--- freeciv-cvs-pristine/client/citydlg_common.h Wed Dec 31 19:00:00 1969
+++ freeciv-cityview-unif/client/citydlg_common.h Tue Dec 4 21:37:08 2001
@@ -0,0 +1,20 @@
+/**********************************************************************
+ Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
+ This program 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; either version 2, or (at your option)
+ any later version.
+
+ This program 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.
+***********************************************************************/
+
+#ifndef FC__CITYDLG_COMMON_H
+#define FC__CITYDLG_COMMON_H
+
+void city_pos_to_canvas_pos(int city_x, int city_y, int *canvas_x, int
*canvas_y);
+void canvas_pos_to_city_pos(int canvas_x, int canvas_y, int *map_x, int
*map_y);
+
+#endif /* FC__CITYDLG_COMMON_H */
diff -Naur freeciv-cvs-pristine/client/gui-gtk/citydlg.c
freeciv-cityview-unif/client/gui-gtk/citydlg.c
--- freeciv-cvs-pristine/client/gui-gtk/citydlg.c Tue Dec 4 20:28:15 2001
+++ freeciv-cityview-unif/client/gui-gtk/citydlg.c Tue Dec 4 21:36:57 2001
@@ -266,10 +266,6 @@
static void draw_map_canvas(struct city_dialog *pdialog);
static gint city_map_canvas_expose(GtkWidget * w, GdkEventExpose * ev,
gpointer data);
-static void city_get_canvas_xy(int map_x, int map_y, int *canvas_x,
- int *canvas_y);
-static void city_get_map_xy(int canvas_x, int canvas_y, int *map_x,
- int *map_y);
static void buy_callback(GtkWidget * w, gpointer data);
static gint buy_callback_delete(GtkWidget * w, GdkEvent * ev,
@@ -1778,7 +1774,7 @@
&& city_map_to_map(&map_x, &map_y, pcity, city_x, city_y)) {
if (tile_is_known(map_x, map_y)) {
int canvas_x, canvas_y;
- city_get_canvas_xy(city_x, city_y, &canvas_x, &canvas_y);
+ city_pos_to_canvas_pos(city_x, city_y, &canvas_x, &canvas_y);
put_one_tile_full(pdialog->map_canvas_store, map_x, map_y,
canvas_x, canvas_y, 1);
}
@@ -1789,7 +1785,7 @@
city_map_checked_iterate(pcity->x, pcity->y, x, y, map_x, map_y) {
if (tile_is_known(map_x, map_y)) {
int canvas_x, canvas_y;
- city_get_canvas_xy(x, y, &canvas_x, &canvas_y);
+ city_pos_to_canvas_pos(x, y, &canvas_x, &canvas_y);
if (pcity->city_map[x][y] == C_TILE_WORKER) {
put_city_tile_output(pdialog->map_canvas_store,
canvas_x, canvas_y,
@@ -1808,7 +1804,7 @@
city_map_checked_iterate(pcity->x, pcity->y, x, y, map_x, map_y) {
if (tile_is_known(map_x, map_y)) {
int canvas_x, canvas_y;
- city_get_canvas_xy(x, y, &canvas_x, &canvas_y);
+ city_pos_to_canvas_pos(x, y, &canvas_x, &canvas_y);
if (pcity->city_map[x][y] == C_TILE_UNAVAILABLE) {
pixmap_frame_tile_red(pdialog->map_canvas_store,
canvas_x, canvas_y);
@@ -2858,7 +2854,7 @@
int xtile, ytile;
struct packet_city_request packet;
- city_get_map_xy(ev->x, ev->y, &xtile, &ytile);
+ canvas_pos_to_city_pos(ev->x, ev->y, &xtile, &ytile);
packet.city_id = pcity->id;
packet.worker_x = xtile;
packet.worker_y = ytile;
@@ -2915,70 +2911,6 @@
return TRUE;
}
-/**************************************************************************
-...
-**************************************************************************/
-static void city_get_canvas_xy(int map_x, int map_y, int *canvas_x,
- int *canvas_y)
-{
- if (is_isometric) {
- int diff_xy;
-
- /* The line at y=0 isometric has constant x+y=1(tiles) */
- diff_xy = (map_x + map_y) - (1);
- *canvas_y =
- diff_xy / 2 * NORMAL_TILE_HEIGHT +
- (diff_xy % 2) * (NORMAL_TILE_HEIGHT / 2);
-
- /* The line at x=0 isometric has constant x-y=-3(tiles) */
- diff_xy = map_x - map_y;
- *canvas_x = (diff_xy + 3) * NORMAL_TILE_WIDTH / 2;
- } else {
- *canvas_x = map_x * NORMAL_TILE_WIDTH;
- *canvas_y = map_y * NORMAL_TILE_HEIGHT;
- }
- freelog(LOG_DEBUG, "city_get_canvas_xy(pos=(%d,%d))=(%d,%d)", map_x,
- map_y, *canvas_x, *canvas_y);
-}
-
-/**************************************************************************
-...
-**************************************************************************/
-static void city_get_map_xy(int canvas_x, int canvas_y, int *map_x,
- int *map_y)
-{
- if (is_isometric) {
- *map_x = -2;
- *map_y = 2;
-
- /* first find an equivalent position on the left side of the screen. */
- *map_x += canvas_x / NORMAL_TILE_WIDTH;
- *map_y -= canvas_x / NORMAL_TILE_WIDTH;
- canvas_x %= NORMAL_TILE_WIDTH;
-
- /* Then move op to the top corner. */
- *map_x += canvas_y / NORMAL_TILE_HEIGHT;
- *map_y += canvas_y / NORMAL_TILE_HEIGHT;
- canvas_y %= NORMAL_TILE_HEIGHT;
-
- assert(NORMAL_TILE_WIDTH == 2 * NORMAL_TILE_HEIGHT);
- canvas_y *= 2; /* now we have a square. */
- if (canvas_x + canvas_y > NORMAL_TILE_WIDTH / 2)
- (*map_x)++;
- if (canvas_x + canvas_y > 3 * NORMAL_TILE_WIDTH / 2)
- (*map_x)++;
- if (canvas_x - canvas_y > NORMAL_TILE_WIDTH / 2)
- (*map_y)--;
- if (canvas_y - canvas_x > NORMAL_TILE_WIDTH / 2)
- (*map_y)++;
- } else {
- *map_x = canvas_x / NORMAL_TILE_WIDTH;
- *map_y = canvas_y / NORMAL_TILE_HEIGHT;
- }
- freelog(LOG_DEBUG, "city_get_map_xy(pos=(%d,%d))=(%d,%d)", canvas_x,
- canvas_y, *map_x, *map_y);
-}
-
/********* Callbacks for Buy, Change, Sell, Worklist ************/
/****************************************************************
...
diff -Naur freeciv-cvs-pristine/client/gui-mui/mapclass.c
freeciv-cityview-unif/client/gui-mui/mapclass.c
--- freeciv-cvs-pristine/client/gui-mui/mapclass.c Sat Oct 27 00:04:16 2001
+++ freeciv-cityview-unif/client/gui-mui/mapclass.c Tue Dec 4 21:36:57 2001
@@ -2150,59 +2150,6 @@
CityMap Custom Class
*****************************************************************/
-/**************************************************************************
-...
-**************************************************************************/
-static void city_get_canvas_xy(int map_x, int map_y, int *canvas_x, int
*canvas_y)
-{
- if (is_isometric) {
- int diff_xy;
-
- /* The line at y=0 isometric has constant x+y=1(tiles) */
- diff_xy = (map_x + map_y) - (1);
- *canvas_y = diff_xy/2 * NORMAL_TILE_HEIGHT + (diff_xy%2) *
(NORMAL_TILE_HEIGHT/2);
-
- /* The line at x=0 isometric has constant x-y=-3(tiles) */
- diff_xy = map_x - map_y;
- *canvas_x = (diff_xy + 3) * NORMAL_TILE_WIDTH/2;
- } else {
- *canvas_x = map_x * NORMAL_TILE_WIDTH;
- *canvas_y = map_y * NORMAL_TILE_HEIGHT;
- }
-}
-
-/**************************************************************************
-...
-**************************************************************************/
-static void city_get_map_xy(int canvas_x, int canvas_y, int *map_x, int *map_y)
-{
- if (is_isometric) {
- *map_x = -2;
- *map_y = 2;
-
- /* first find an equivalent position on the left side of the screen. */
- *map_x += canvas_x/NORMAL_TILE_WIDTH;
- *map_y -= canvas_x/NORMAL_TILE_WIDTH;
- canvas_x %= NORMAL_TILE_WIDTH;
-
- /* Then move op to the top corner. */
- *map_x += canvas_y/NORMAL_TILE_HEIGHT;
- *map_y += canvas_y/NORMAL_TILE_HEIGHT;
- canvas_y %= NORMAL_TILE_HEIGHT;
-
- assert(NORMAL_TILE_WIDTH == 2*NORMAL_TILE_HEIGHT);
- canvas_y *= 2; /* now we have a square. */
- if (canvas_x + canvas_y > NORMAL_TILE_WIDTH/2) (*map_x)++;
- if (canvas_x + canvas_y > 3 * NORMAL_TILE_WIDTH/2) (*map_x)++;
- if (canvas_x - canvas_y > NORMAL_TILE_WIDTH/2) (*map_y)--;
- if (canvas_y - canvas_x > NORMAL_TILE_WIDTH/2) (*map_y)++;
- } else {
- *map_x = canvas_x/NORMAL_TILE_WIDTH;
- *map_y = canvas_y/NORMAL_TILE_HEIGHT;
- }
-}
-
-
struct MUI_CustomClass *CL_CityMap;
Object *MakeCityMap(struct city *pcity)
@@ -2355,7 +2302,7 @@
city_map_checked_iterate(pcity->x, pcity->y, x, y, map_x, map_y) {
if (tile_is_known(map_x, map_y)) {
int canvas_x, canvas_y;
- city_get_canvas_xy(x, y, &canvas_x, &canvas_y);
+ city_pos_to_canvas_pos(x, y, &canvas_x, &canvas_y);
put_one_tile_full(_rp(o), map_x, map_y, canvas_x + _mleft(o),
canvas_y + _mtop(o), 1);
}
} city_map_checked_iterate_end;
@@ -2364,7 +2311,7 @@
city_map_checked_iterate(pcity->x, pcity->y, x, y, map_x, map_y) {
if (tile_is_known(map_x, map_y)) {
int canvas_x, canvas_y;
- city_get_canvas_xy(x, y, &canvas_x, &canvas_y);
+ city_pos_to_canvas_pos(x, y, &canvas_x, &canvas_y);
if (pcity->city_map[x][y]==C_TILE_WORKER) {
put_city_output_tile(_rp(o),
city_get_food_tile(x, y, pcity),
@@ -2383,7 +2330,7 @@
if (tile_is_known(map_x, map_y))
{
int canvas_x, canvas_y;
- city_get_canvas_xy(x, y, &canvas_x, &canvas_y);
+ city_pos_to_canvas_pos(x, y, &canvas_x, &canvas_y);
canvas_x += _mleft(o);
canvas_y += _mtop(o);
@@ -2470,7 +2417,7 @@
if (_isinobject(msg->imsg->MouseX, msg->imsg->MouseY))
{
int x,y;
- city_get_map_xy(msg->imsg->MouseX - _mleft(o), msg->imsg->MouseY -
_mtop(o), &x, &y);
+ canvas_pos_to_city_pos(msg->imsg->MouseX - _mleft(o),
msg->imsg->MouseY - _mtop(o), &x, &y);
data->click.x = x;
data->click.y = y;
set(o, MUIA_CityMap_Click, &data->click);
diff -Naur freeciv-cvs-pristine/client/gui-win32/citydlg.c
freeciv-cityview-unif/client/gui-win32/citydlg.c
--- freeciv-cvs-pristine/client/gui-win32/citydlg.c Tue Dec 4 20:28:15 2001
+++ freeciv-cityview-unif/client/gui-win32/citydlg.c Tue Dec 4 21:36:57 2001
@@ -449,59 +449,6 @@
/* FIXME Worklists */
}
-/**************************************************************************
-...
-**************************************************************************/
-static void city_get_canvas_xy(int map_x, int map_y, int *canvas_x, int
*canvas_y)
-{
- if (is_isometric) {
- int diff_xy;
-
- /* The line at y=0 isometric has constant x+y=1(tiles) */
- diff_xy = (map_x + map_y) - (1);
- *canvas_y = diff_xy/2 * NORMAL_TILE_HEIGHT + (diff_xy%2) *
(NORMAL_TILE_HEIGHT/2);
-
- /* The line at x=0 isometric has constant x-y=-3(tiles) */
- diff_xy = map_x - map_y;
- *canvas_x = (diff_xy + 3) * NORMAL_TILE_WIDTH/2;
- } else {
- *canvas_x = map_x * NORMAL_TILE_WIDTH;
- *canvas_y = map_y * NORMAL_TILE_HEIGHT;
- }
-}
-
-/**************************************************************************
-...
-**************************************************************************/
-static void city_get_map_xy(int canvas_x, int canvas_y, int *map_x, int *map_y)
-{
- if (is_isometric) {
- *map_x = -2;
- *map_y = 2;
-
- /* first find an equivalent position on the left side of the screen. */
- *map_x += canvas_x/NORMAL_TILE_WIDTH;
- *map_y -= canvas_x/NORMAL_TILE_WIDTH;
- canvas_x %= NORMAL_TILE_WIDTH;
-
- /* Then move op to the top corner. */
- *map_x += canvas_y/NORMAL_TILE_HEIGHT;
- *map_y += canvas_y/NORMAL_TILE_HEIGHT;
- canvas_y %= NORMAL_TILE_HEIGHT;
-
- assert(NORMAL_TILE_WIDTH == 2*NORMAL_TILE_HEIGHT);
- canvas_y *= 2; /* now we have a square. */
- if (canvas_x + canvas_y > NORMAL_TILE_WIDTH/2) (*map_x)++;
- if (canvas_x + canvas_y > 3 * NORMAL_TILE_WIDTH/2) (*map_x)++;
- if (canvas_x - canvas_y > NORMAL_TILE_WIDTH/2) (*map_y)--;
- if (canvas_y - canvas_x > NORMAL_TILE_WIDTH/2) (*map_y)++;
- } else {
- *map_x = canvas_x/NORMAL_TILE_WIDTH;
- *map_y = canvas_y/NORMAL_TILE_HEIGHT;
- }
-}
-
-
/****************************************************************
Isometric.
*****************************************************************/
@@ -519,7 +466,7 @@
&& city_map_to_map(&map_x, &map_y, pcity, city_x, city_y)) {
if (tile_is_known(map_x, map_y)) {
int canvas_x, canvas_y;
- city_get_canvas_xy(city_x, city_y, &canvas_x, &canvas_y);
+ city_pos_to_canvas_pos(city_x, city_y, &canvas_x, &canvas_y);
put_one_tile_full(hdc, map_x, map_y,
canvas_x, canvas_y, 1);
}
@@ -530,7 +477,7 @@
city_map_checked_iterate(pcity->x, pcity->y, x, y, map_x, map_y) {
if (tile_is_known(map_x, map_y)) {
int canvas_x, canvas_y;
- city_get_canvas_xy(x, y, &canvas_x, &canvas_y);
+ city_pos_to_canvas_pos(x, y, &canvas_x, &canvas_y);
if (pcity->city_map[x][y]==C_TILE_WORKER) {
put_city_tile_output(hdc,
canvas_x, canvas_y,
@@ -548,7 +495,7 @@
city_map_checked_iterate(pcity->x, pcity->y, x, y, map_x, map_y) {
if (tile_is_known(map_x, map_y)) {
int canvas_x, canvas_y;
- city_get_canvas_xy(x, y, &canvas_x, &canvas_y);
+ city_pos_to_canvas_pos(x, y, &canvas_x, &canvas_y);
if (pcity->city_map[x][y]==C_TILE_UNAVAILABLE) {
pixmap_frame_tile_red(hdc,
canvas_x, canvas_y);
@@ -1784,7 +1731,7 @@
int tile_x,tile_y;
xr=x-pdialog->map_x;
yr=y-pdialog->map_y;
- city_get_map_xy(xr,yr,&tile_x,&tile_y);
+ canvas_pos_to_city_pos(xr,yr,&tile_x,&tile_y);
city_dlg_click_map(pdialog,tile_x,tile_y);
}
}
diff -Naur freeciv-cvs-pristine/client/include/citydlg_g.h
freeciv-cityview-unif/client/include/citydlg_g.h
--- freeciv-cvs-pristine/client/include/citydlg_g.h Mon Jun 25 14:15:03 2001
+++ freeciv-cityview-unif/client/include/citydlg_g.h Tue Dec 4 21:36:57 2001
@@ -13,6 +13,8 @@
#ifndef FC__CITYDLG_G_H
#define FC__CITYDLG_G_H
+#include "citydlg_common.h"
+
struct unit;
struct city;
|
|