~ubuntu-branches/ubuntu/precise/widelands/precise-backports

« back to all changes in this revision

Viewing changes to src/mapview.cc

  • Committer: Bazaar Package Importer
  • Author(s): Martin Quinson
  • Date: 2005-02-14 10:41:12 UTC
  • Revision ID: james.westby@ubuntu.com-20050214104112-6v08iux9fptxpva9
Tags: upstream-build9
Import upstream version build9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2002-2004 by The Widelands Development Team
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU General Public License
 
6
 * as published by the Free Software Foundation; either version 2
 
7
 * of the License, or (at your option) any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
17
 *
 
18
 */
 
19
 
 
20
#include "interactive_base.h"
 
21
#include "map.h"
 
22
#include "mapview.h"
 
23
#include "rendertarget.h"
 
24
#include "system.h"
 
25
 
 
26
/*
 
27
===============
 
28
Map_View::Map_View
 
29
 
 
30
Initialize
 
31
===============
 
32
*/
 
33
Map_View::Map_View(UIPanel *parent, int x, int y, uint w, uint h, Interactive_Base *player)
 
34
        : UIPanel(parent, x, y, w, h)
 
35
{
 
36
        m_intbase = player;
 
37
        m_viewpoint.x = m_viewpoint.y = 0;
 
38
        m_dragging = false;
 
39
}
 
40
 
 
41
 
 
42
/*
 
43
===============
 
44
Map_View::~Map_View
 
45
 
 
46
Cleanups
 
47
===============
 
48
*/
 
49
Map_View::~Map_View(void)
 
50
{
 
51
}
 
52
 
 
53
 
 
54
/*
 
55
===============
 
56
Map_View::warp_mouse_to_field
 
57
 
 
58
Moves the mouse cursor so that it is directly above the given field
 
59
===============
 
60
*/
 
61
void Map_View::warp_mouse_to_field(Coords c)
 
62
{
 
63
        int x, y;
 
64
 
 
65
        m_intbase->get_map()->get_save_pix(c, &x, &y);
 
66
        x -= m_viewpoint.x;
 
67
        y -= m_viewpoint.y;
 
68
 
 
69
        if(x>=get_w() || y>=get_h()) {
 
70
      // The user has scrolled the field outside
 
71
      // the viewable area, he most surely doesn't
 
72
      // want to jump there
 
73
      return;
 
74
   }
 
75
 
 
76
   if(x<=0) { warp_mouse_to_field(Coords(c.x+m_intbase->get_map()->get_width(),c.y)); return; }
 
77
   if(y<=0) { warp_mouse_to_field(Coords(c.x, c.y+m_intbase->get_map()->get_height())); return; }
 
78
 
 
79
   set_mouse_pos(x, y);
 
80
}
 
81
 
 
82
 
 
83
/*
 
84
===============
 
85
Map_View::draw
 
86
 
 
87
This is the guts!! this function draws the whole
 
88
map the user can see. we spend a lot of time
 
89
in this function
 
90
===============
 
91
*/
 
92
void Map_View::draw(RenderTarget* dst)
 
93
{
 
94
        dst->rendermap(m_intbase->get_egbase(), m_intbase->get_visibility(), m_viewpoint);
 
95
}
 
96
 
 
97
 
 
98
/*
 
99
===============
 
100
Map_View::set_viewpoint
 
101
 
 
102
Set the viewpoint to the given pixel coordinates
 
103
===============
 
104
*/
 
105
void Map_View::set_viewpoint(Point vp)
 
106
{
 
107
        if (vp == m_viewpoint)
 
108
                return;
 
109
 
 
110
        m_intbase->get_map()->normalize_pix(&vp);
 
111
        m_viewpoint = vp;
 
112
 
 
113
        warpview.call(m_viewpoint.x, m_viewpoint.y);
 
114
}
 
115
 
 
116
/** Map_View::handle_mouseclick(uint btn, bool down, int x, int y)
 
117
 *
 
118
 * Mouseclicks on the map:
 
119
 * Right-click: enable/disable dragging
 
120
 * Left-click: field action window
 
121
 */
 
122
bool Map_View::handle_mouseclick(uint btn, bool down, int x, int y)
 
123
{
 
124
        if (btn == MOUSE_LEFT)
 
125
        {
 
126
                if (down) {
 
127
         track_fsel(x, y);
 
128
 
 
129
                        fieldclicked.call();
 
130
                }
 
131
        }
 
132
        else if (btn == MOUSE_RIGHT)
 
133
        {
 
134
                if (down) {
 
135
                        m_dragging = true;
 
136
                        grab_mouse(true);
 
137
                        Sys_MouseLock(true);
 
138
                } else if (m_dragging) {
 
139
                        Sys_MouseLock(false);
 
140
                        grab_mouse(false);
 
141
                        m_dragging = false;
 
142
                }
 
143
        }
 
144
 
 
145
        return true;
 
146
}
 
147
 
 
148
 
 
149
/*
 
150
===============
 
151
Map_View::handle_mousemove
 
152
 
 
153
Scroll the view according to mouse movement.
 
154
===============
 
155
*/
 
156
void Map_View::handle_mousemove(int x, int y, int xdiff, int ydiff, uint btns)
 
157
{
 
158
   if (!(btns & (1<<MOUSE_RIGHT)))
 
159
                m_dragging = false;
 
160
 
 
161
        if (m_dragging)
 
162
        {
 
163
                set_rel_viewpoint(Point(xdiff, ydiff));
 
164
        }
 
165
 
 
166
        if (!m_intbase->get_fieldsel_freeze())
 
167
                track_fsel(x, y);
 
168
 
 
169
        g_gr->update_fullscreen();
 
170
}
 
171
 
 
172
 
 
173
/*
 
174
===============
 
175
Map_View::track_fsel(int mx, int my)
 
176
 
 
177
Move the fsel to the given mouse position.
 
178
Does not honour fieldsel freeze.
 
179
===============
 
180
*/
 
181
void Map_View::track_fsel(int mx, int my)
 
182
{
 
183
        FCoords fsel;
 
184
 
 
185
        fsel = m_intbase->get_map()->calc_coords(Point(m_viewpoint.x + mx, m_viewpoint.y + my));
 
186
 
 
187
        // Apply the new fieldsel
 
188
        m_intbase->set_fieldsel_pos(fsel);
 
189
}