2
* Copyright (C) 2002-2004, 2006-2008 by the Widelands Development Team
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.
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.
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.
22
#include "graphic/graphic.h"
23
#include "interactive_base.h"
24
#include "interactive_player.h"
26
#include "mapviewpixelfunctions.h"
27
#include "graphic/overlay_manager.h"
29
#include "graphic/rendertarget.h"
30
#include "wlapplication.h"
36
int32_t x, int32_t y, uint32_t w, uint32_t h,
37
Interactive_Base & player)
39
UI::Panel (parent, x, y, w, h),
41
m_viewpoint (Point(0, 0)),
43
m_complete_redraw_needed(true)
47
/// Moves the mouse cursor so that it is directly above the given node
48
void Map_View::warp_mouse_to_node(Widelands::Coords const c) {
49
Widelands::Map const & map = intbase().egbase().map();
51
MapviewPixelFunctions::get_save_pix(map, c, p.x, p.y);
54
// If the user has scrolled the field outside the viewable area, he most
55
// surely doesn't want to jump there.
56
if (p.x < get_w() and p.y < get_h()) {
58
warp_mouse_to_node(Widelands::Coords(c.x + map.get_width (), c.y));
60
warp_mouse_to_node(Widelands::Coords(c.x, c.y + map.get_height()));
71
This is the guts!! this function draws the whole
72
map the user can see. we spend a lot of time
76
void Map_View::draw(RenderTarget & dst)
78
Widelands::Editor_Game_Base & egbase = intbase().egbase();
80
if (upcast(Widelands::Game, game, &egbase)) {
81
// Bail out if the game isn't actually loaded.
82
// This fixes a crash with displaying an error dialog during loading.
83
if (!game->is_loaded())
86
// Check if the view has changed in a game
87
if (intbase().get_player() && intbase().get_player()->has_view_changed())
88
m_complete_redraw_needed = true;
91
egbase.map().overlay_manager().load_graphics();
93
if (upcast(Interactive_Player const, interactive_player, &intbase()))
94
dst.rendermap(egbase, interactive_player->player(), m_viewpoint);
96
dst.rendermap(egbase, m_viewpoint);
98
m_complete_redraw_needed = false;
104
Set the viewpoint to the given pixel coordinates
107
void Map_View::set_viewpoint(Point vp)
109
if (vp == m_viewpoint)
112
MapviewPixelFunctions::normalize_pix(intbase().egbase().map(), vp);
115
warpview.call(m_viewpoint.x, m_viewpoint.y);
117
m_complete_redraw_needed = true;
121
void Map_View::stop_dragging() {
122
WLApplication::get()->set_mouse_lock(false);
128
* Mousepressess and -releases on the map:
129
* Right-press: enable dragging
130
* Right-release: disable dragging
131
* Left-press: field action window
133
bool Map_View::handle_mousepress
134
(Uint8 const btn, int32_t const x, int32_t const y)
137
// SDL does on Mac hardcoded middle mouse button emulation (alt+left).
138
// This interferes with the editor, which is using alt+left click for third
139
// tool. So just handle middle mouse button like left one.
140
// TODO This should be handled in a more general way someplace else. What
141
// TODO kind of stupid idea is it to hardcode something like that in SDL?
142
// TODO Sometimes, people are funny....
143
if (btn == SDL_BUTTON_MIDDLE || btn == SDL_BUTTON_LEFT)
145
if (btn == SDL_BUTTON_LEFT)
148
track_sel(Point(x, y));
151
} else if (btn == SDL_BUTTON_RIGHT) {
154
WLApplication::get()->set_mouse_lock(true);
158
bool Map_View::handle_mouserelease(const Uint8 btn, int32_t, int32_t)
159
{if (btn == SDL_BUTTON_RIGHT and m_dragging) stop_dragging(); return true;}
164
Map_View::handle_mousemove
166
Scroll the view according to mouse movement.
169
bool Map_View::handle_mousemove
170
(Uint8 const state, int32_t x, int32_t y, int32_t xdiff, int32_t ydiff)
174
if (state & SDL_BUTTON(SDL_BUTTON_RIGHT))
175
set_rel_viewpoint(Point(xdiff, ydiff));
176
else stop_dragging();
179
if (not intbase().get_sel_freeze()) track_sel(Point(x, y));
181
g_gr->update_fullscreen();
188
Map_View::track_sel(int32_t mx, int32_t my)
190
Move the sel to the given mouse position.
191
Does not honour sel freeze.
194
void Map_View::track_sel(Point m) {
196
m_intbase.set_sel_pos
197
(MapviewPixelFunctions::calc_node_and_triangle
198
(intbase().egbase().map(), m.x, m.y));