File: champlain-map.c

package info (click to toggle)
libchamplain 0.4.6-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 4,680 kB
  • ctags: 2,295
  • sloc: ansic: 15,212; sh: 10,205; perl: 2,324; makefile: 647; python: 450; xml: 18; cs: 16
file content (126 lines) | stat: -rw-r--r-- 3,134 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * Copyright (C) 2008-2009 Pierre-Luc Beaudoin <pierre-luc@pierlux.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "champlain-map.h"

#define DEBUG_FLAG CHAMPLAIN_DEBUG_LOADING
#include "champlain-debug.h"
#include "champlain-zoom-level.h"

#include <math.h>

Map*
map_new (void)
{
  Map *map = g_new0 (Map, 1);

  map->previous_level = NULL;
  map->current_level = NULL;

  return map;
}

void
map_load_level (Map *map,
    ChamplainMapSource *map_source,
    gint zoom_level)
{
  if (map->previous_level != NULL)
    {
      if (champlain_zoom_level_get_zoom_level (map->previous_level) == zoom_level)
        {
          /* Swap current and previous, instead of reloading it */
          ChamplainZoomLevel *tmp = map->previous_level;

          map->previous_level = map->current_level;
          map->current_level = tmp;

          return;
        }
      else
        g_object_unref (map->previous_level);
    }

  map->previous_level = map->current_level;

  guint row_count = champlain_map_source_get_row_count (map_source, zoom_level);
  guint column_count = champlain_map_source_get_column_count (map_source, zoom_level);

  map->current_level = champlain_zoom_level_new ();
  g_object_set (G_OBJECT (map->current_level),
      "zoom-level", zoom_level,
      "width", row_count,
      "height", column_count,
      NULL);
}

gboolean
map_zoom_in (Map *map,
    ChamplainMapSource *source)
{
  guint new_level = champlain_zoom_level_get_zoom_level (map->current_level) + 1;

  if(new_level <= champlain_map_source_get_max_zoom_level (source))
    {
      map_load_level (map, source, new_level);
      return TRUE;
    }

  return FALSE;
}

gboolean
map_zoom_out (Map *map,
    ChamplainMapSource *source)
{
  gint new_level = champlain_zoom_level_get_zoom_level (map->current_level) - 1;

  if(new_level >= champlain_map_source_get_min_zoom_level (source))
    {
      map_load_level (map, source, new_level);
      return TRUE;
    }

  return FALSE;
}

void
map_free (Map *map)
{
  if (map->previous_level != NULL)
    g_object_unref (map->previous_level);

  if (map->current_level != NULL)
    g_object_unref (map->current_level);

  g_free (map);
}

gboolean
map_zoom_to (Map *map,
    ChamplainMapSource *source,
    guint zoomLevel)
{
  if (zoomLevel <= champlain_map_source_get_max_zoom_level (source))
    {
      map_load_level (map, source, zoomLevel);
      return TRUE;
    }

  return FALSE;
}