1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "ultima/nuvie/core/nuvie_defs.h"
24 #include "ultima/nuvie/misc/u6_misc.h"
25 #include "ultima/nuvie/gui/gui.h"
26 #include "ultima/nuvie/gui/gui_button.h"
27 #include "ultima/nuvie/conf/configuration.h"
28 #include "ultima/nuvie/views/view_manager.h"
29 #include "ultima/nuvie/gui/widgets/msg_scroll.h"
30 #include "ultima/nuvie/views/draggable_view.h"
31 
32 namespace Ultima {
33 namespace Nuvie {
34 
DraggableView(Configuration * cfg)35 DraggableView::DraggableView(Configuration *cfg) : View(cfg) {
36 	drag = false;
37 	button_x = 0;
38 	button_y = 0;
39 	bg_image = NULL;
40 	bg_color_key = 0;
41 	Game *game = Game::get_game();
42 	if (game->is_orig_style() || game->is_original_plus_cutoff_map()) {
43 		need_full_redraw_when_moved = true;
44 		always_need_full_redraw_when_moved = true;
45 	} else if (game->get_game_width() < game->get_screen()->get_width()
46 	           || game->get_game_height() < game->get_screen()->get_height()) {
47 		need_full_redraw_when_moved = true;
48 		always_need_full_redraw_when_moved = false;
49 	} else // no need to set always_need_full_redraw_when_moved
50 		need_full_redraw_when_moved = false;
51 }
52 
~DraggableView()53 DraggableView::~DraggableView() {
54 	if (bg_image) {
55 		SDL_FreeSurface(bg_image);
56 		bg_image = NULL;
57 	}
58 }
59 
set_bg_color_key(uint8 r,uint8 g,uint8 b)60 void DraggableView::set_bg_color_key(uint8 r, uint8 g, uint8 b) {
61 	if (bg_image) {
62 		bg_color_key = SDL_MapRGB(bg_image->format, 0, 0x70, 0xfc);
63 		SDL_SetColorKey(bg_image, SDL_TRUE, bg_color_key);
64 	}
65 }
66 
MouseDown(int x,int y,Shared::MouseButton button)67 GUI_status DraggableView::MouseDown(int x, int y, Shared::MouseButton button) {
68 	if (bg_image && HitRect(x, y)) {
69 		uint32 pixel = sdl_getpixel(bg_image, x - area.left, y - area.top);
70 		if (pixel == bg_color_key) {
71 			return GUI_PASS;
72 		}
73 	}
74 	drag = true;
75 	button_x = x;
76 	button_y = y;
77 
78 	moveToFront();
79 	if (Game::get_game()->is_new_style()) {
80 		Game::get_game()->get_scroll()->moveToFront();
81 	}
82 	grab_focus();
83 
84 	return GUI_YUM;
85 }
86 
MouseUp(int x,int y,Shared::MouseButton button)87 GUI_status DraggableView::MouseUp(int x, int y, Shared::MouseButton button) {
88 	drag = false;
89 
90 	release_focus();
91 	if (button == Shared::BUTTON_RIGHT) {
92 		Game::get_game()->get_view_manager()->close_gump(this);
93 	}
94 	return GUI_YUM;
95 }
96 
MouseMotion(int x,int y,uint8 state)97 GUI_status DraggableView::MouseMotion(int x, int y, uint8 state) {
98 	int dx, dy;
99 
100 	if (!drag || state == 0) //state is 0 if no button pressed
101 		return GUI_PASS;
102 
103 	dx = x - button_x;
104 	dy = y - button_y;
105 
106 	button_x = x;
107 	button_y = y;
108 
109 	GUI::get_gui()->moveWidget(this, dx, dy);
110 // Redraw();
111 
112 	return (GUI_YUM);
113 }
114 
force_full_redraw_if_needed()115 void DraggableView::force_full_redraw_if_needed() {
116 	if (need_full_redraw_when_moved) {
117 		if (always_need_full_redraw_when_moved // or over background
118 		        || (area.right > Game::get_game()->get_game_width() + Game::get_game()->get_game_x_offset()
119 		            || area.left < Game::get_game()->get_game_x_offset() || area.top < Game::get_game()->get_game_y_offset()
120 		            || area.bottom > Game::get_game()->get_game_height() + Game::get_game()->get_game_y_offset()))
121 			GUI::get_gui()->force_full_redraw();
122 	}
123 }
124 
MoveRelative(int dx,int dy)125 void DraggableView::MoveRelative(int dx, int dy) {
126 	int new_x = area.left + dx;
127 
128 	if (new_x < 0) {
129 		dx = -area.left;
130 	} else if (new_x + area.width() > screen->get_width()) {
131 		dx = screen->get_width() - (area.left + area.width());
132 	}
133 
134 	int new_y = area.top + dy;
135 
136 	if (new_y < 0) {
137 		dy = -area.top;
138 	} else if (new_y + area.height() > screen->get_height()) {
139 		dy = screen->get_height() - (area.top + area.height());
140 	}
141 
142 	force_full_redraw_if_needed(); // needs to happen before the move
143 	GUI_Widget::MoveRelative(dx, dy);
144 
145 	return;
146 }
147 
148 } // End of namespace Nuvie
149 } // End of namespace Ultima
150