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/core/events.h"
26 #include "ultima/nuvie/gui/gui.h"
27 #include "ultima/nuvie/gui/gui_button.h"
28 #include "ultima/nuvie/core/u6_objects.h"
29 #include "ultima/nuvie/core/party.h"
30 #include "ultima/nuvie/actors/actor.h"
31 #include "ultima/nuvie/fonts/font.h"
32 #include "ultima/nuvie/views/view_manager.h"
33 #include "ultima/nuvie/fonts/font_manager.h"
34 #include "ultima/nuvie/views/container_widget_gump.h"
35 #include "ultima/nuvie/views/container_view_gump.h"
36 #include "ultima/nuvie/keybinding/keys.h"
37 
38 namespace Ultima {
39 namespace Nuvie {
40 
41 #define CONTAINER_WIDGET_OFFSET 29
42 #define CHECK_X 0
43 
ContainerViewGump(Configuration * cfg)44 ContainerViewGump::ContainerViewGump(Configuration *cfg) : DraggableView(cfg) {
45 	bg_image = NULL;
46 	gump_button = NULL;
47 	up_arrow_button = NULL;
48 	down_arrow_button = NULL;
49 	doll_button = NULL;
50 	left_arrow_button = NULL;
51 	right_arrow_button = NULL;
52 	container_widget = NULL;
53 	font = NULL;
54 	actor = NULL;
55 	container_obj = NULL;
56 }
57 
~ContainerViewGump()58 ContainerViewGump::~ContainerViewGump() {
59 }
60 
init(Screen * tmp_screen,void * view_manager,uint16 x,uint16 y,Font * f,Party * p,TileManager * tm,ObjManager * om,Obj * container_obj_type)61 bool ContainerViewGump::init(Screen *tmp_screen, void *view_manager, uint16 x, uint16 y, Font *f, Party *p, TileManager *tm, ObjManager *om, Obj *container_obj_type) {
62 	View::init(x, y, f, p, tm, om);
63 
64 	//actor = p->get_actor(p->get_leader()); don't have party leader as default, get owner of container obj or leave NULL (moved to init_container_type)
65 
66 	Std::string datadir = GUI::get_gui()->get_data_dir();
67 	Std::string imagefile;
68 	Std::string path;
69 
70 	build_path(datadir, "images", path);
71 	datadir = path;
72 	build_path(datadir, "gumps", path);
73 	datadir = path;
74 
75 	init_container_type(datadir, container_obj_type);
76 
77 	set_bg_color_key(0, 0x70, 0xfc);
78 
79 	//font = new GUI_Font(GUI_FONT_GUMP);
80 	//font->setColoring( 0x08, 0x08, 0x08, 0x80, 0x58, 0x30, 0x00, 0x00, 0x00);
81 	font = f;
82 
83 	return true;
84 }
85 
init_container_type(Std::string datadir,Obj * obj_type)86 void ContainerViewGump::init_container_type(Std::string datadir, Obj *obj_type) {
87 
88 
89 	if (obj_type != NULL) {
90 		if (obj_type->is_in_inventory())
91 			actor = obj_type->get_actor_holding_obj();
92 		if (Game::get_game()->get_game_type() == NUVIE_GAME_U6) {
93 			if (obj_type->obj_n == OBJ_U6_CHEST)
94 				return init_chest(datadir);
95 			else if (obj_type->obj_n == OBJ_U6_CRATE)
96 				return init_crate(datadir);
97 			else if (obj_type->obj_n == OBJ_U6_BARREL)
98 				return init_barrel(datadir);
99 			else if (obj_type->obj_n == OBJ_U6_DEAD_GARGOYLE)
100 				return init_corpse(datadir, "corpse_gargoyle_bg.bmp");
101 			else if (obj_type->obj_n == OBJ_U6_DEAD_BODY
102 			         || obj_type->obj_n == OBJ_U6_GRAVE || obj_type->obj_n == OBJ_U6_REMAINS)
103 				return init_corpse(datadir, "corpse_body_bg.bmp");
104 			else if (obj_type->obj_n == OBJ_U6_DEAD_CYCLOPS)
105 				return init_corpse(datadir, "corpse_cyclops_bg.bmp");
106 			else if (obj_type->obj_n == OBJ_U6_DEAD_ANIMAL || obj_type->obj_n == OBJ_U6_MOUSE
107 			         || obj_type->obj_n == OBJ_U6_MONGBAT || obj_type->obj_n == OBJ_U6_DRAKE
108 			         || obj_type->obj_n == OBJ_U6_REAPER)
109 				return init_corpse(datadir, "corpse_animal_bg.bmp");
110 		}
111 	}
112 
113 	return init_backpack(datadir, obj_type ? !obj_type->is_in_inventory() : true);
114 }
115 
init_backpack(Std::string datadir,bool extend_area_w)116 void ContainerViewGump::init_backpack(Std::string datadir, bool extend_area_w) {
117 	Std::string imagefile, path;
118 	uint8 check_y = 27;
119 	gump_button = loadButton(datadir, "gump", CHECK_X, check_y);
120 
121 	build_path(datadir, "container", path);
122 	datadir = path;
123 
124 	up_arrow_button = loadButton(datadir, "cont_up", 83, 35);
125 	down_arrow_button = loadButton(datadir, "cont_down", 83, 66);
126 
127 	build_path(datadir, "backpack_bg.bmp", imagefile);
128 
129 	bg_image = SDL_LoadBMP(imagefile.c_str());
130 
131 	doll_button = loadButton(datadir, "cont_doll", area.left + 18, area.top + bg_image->h);
132 	left_arrow_button = loadButton(datadir, "cont_left", area.left + 18 + 11, area.top + bg_image->h);
133 	right_arrow_button = loadButton(datadir, "cont_right", area.left + 18 + 22, area.top + bg_image->h);
134 
135 	SetRect(area.left, area.top, bg_image->w, bg_image->h + 16); //111, 101);
136 
137 	container_widget = new ContainerWidgetGump(config, this);
138 	container_widget_y_offset = CONTAINER_WIDGET_OFFSET;
139 	container_widget->init(actor, 21, container_widget_y_offset, 4, 3, tile_manager, obj_manager, font, CHECK_X, check_y);
140 
141 	AddWidget(container_widget);
142 	if (extend_area_w) // text extends beyond the gump
143 		area.right += 4;
144 }
145 
init_chest(Std::string datadir)146 void ContainerViewGump::init_chest(Std::string datadir) {
147 	Std::string imagefile, path;
148 	uint8 check_y = 56;
149 	gump_button = loadButton(datadir, "gump", CHECK_X, check_y);
150 
151 	build_path(datadir, "container", path);
152 	datadir = path;
153 
154 	up_arrow_button = loadButton(datadir, "cont_up", 85, 31);
155 	down_arrow_button = loadButton(datadir, "cont_down", 85, 47);
156 
157 	build_path(datadir, "chest_bg.bmp", imagefile);
158 
159 	bg_image = SDL_LoadBMP(imagefile.c_str());
160 
161 	SetRect(area.left, area.top, bg_image->w, bg_image->h + 16); //111, 101);
162 
163 	container_widget = new ContainerWidgetGump(config, this);
164 	container_widget_y_offset = CONTAINER_WIDGET_OFFSET - 1;
165 	container_widget->init(actor, 21, container_widget_y_offset, 4, 2, tile_manager, obj_manager, font, CHECK_X, check_y);
166 
167 	AddWidget(container_widget);
168 }
169 
init_crate(Std::string datadir)170 void ContainerViewGump::init_crate(Std::string datadir) {
171 	Std::string imagefile, path;
172 	uint8 check_y = 63;
173 	gump_button = loadButton(datadir, "gump", CHECK_X, check_y);
174 
175 	build_path(datadir, "container", path);
176 	datadir = path;
177 
178 	up_arrow_button = loadButton(datadir, "cont_up", 100, 15);
179 	down_arrow_button = loadButton(datadir, "cont_down", 100, 46);
180 
181 	build_path(datadir, "crate_bg.bmp", imagefile);
182 
183 	bg_image = SDL_LoadBMP(imagefile.c_str());
184 
185 	SetRect(area.left, area.top, bg_image->w, bg_image->h);
186 
187 	container_widget = new ContainerWidgetGump(config, this);
188 	container_widget_y_offset = 10;
189 	container_widget->init(actor, 21, container_widget_y_offset, 5, 3, tile_manager, obj_manager, font, CHECK_X, check_y);
190 
191 	AddWidget(container_widget);
192 }
193 
init_barrel(Std::string datadir)194 void ContainerViewGump::init_barrel(Std::string datadir) {
195 	Std::string imagefile, path;
196 	uint8 check_y = 55;
197 	gump_button = loadButton(datadir, "gump", CHECK_X, check_y);
198 
199 	build_path(datadir, "container", path);
200 	datadir = path;
201 
202 	up_arrow_button = loadButton(datadir, "cont_up", 102, 28);
203 	down_arrow_button = loadButton(datadir, "cont_down", 102, 42);
204 
205 	build_path(datadir, "barrel_bg.bmp", imagefile);
206 
207 	bg_image = SDL_LoadBMP(imagefile.c_str());
208 
209 	SetRect(area.left, area.top, bg_image->w, bg_image->h);
210 
211 	container_widget = new ContainerWidgetGump(config, this);
212 	container_widget_y_offset = 24;
213 	container_widget->init(actor, 38, container_widget_y_offset, 4, 2, tile_manager, obj_manager, font, CHECK_X, check_y);
214 
215 	AddWidget(container_widget);
216 }
217 
init_corpse(Std::string datadir,Std::string bg_filename)218 void ContainerViewGump::init_corpse(Std::string datadir, Std::string bg_filename) {
219 	Std::string imagefile, path;
220 	uint8 check_y = 25;
221 	gump_button = loadButton(datadir, "gump", CHECK_X, check_y);
222 
223 	build_path(datadir, "container", path);
224 	datadir = path;
225 
226 	up_arrow_button = loadButton(datadir, "cont_up", 67, 28);
227 	down_arrow_button = loadButton(datadir, "cont_down", 67, 78);
228 
229 	build_path(datadir, bg_filename, imagefile);
230 
231 	bg_image = SDL_LoadBMP(imagefile.c_str());
232 
233 	SetRect(area.left, area.top, bg_image->w, bg_image->h);
234 
235 	container_widget = new ContainerWidgetGump(config, this);
236 	container_widget_y_offset = 26;
237 	container_widget->init(actor, 20, container_widget_y_offset, 3, 4, tile_manager, obj_manager, font, CHECK_X, check_y);
238 
239 	AddWidget(container_widget);
240 }
set_actor(Actor * a)241 void ContainerViewGump::set_actor(Actor *a) {
242 	actor = a;
243 	container_obj = NULL;
244 	container_widget->set_actor(a);
245 	if (doll_button)
246 		doll_button->Show();
247 	if (party->get_member_num(a) >= 0) {
248 		if (left_arrow_button)
249 			left_arrow_button->Show();
250 		if (right_arrow_button)
251 			right_arrow_button->Show();
252 	} else {
253 		if (left_arrow_button)
254 			left_arrow_button->Hide();
255 		if (right_arrow_button)
256 			right_arrow_button->Hide();
257 	}
258 }
259 
set_container_obj(Obj * o)260 void ContainerViewGump::set_container_obj(Obj *o) {
261 	container_obj = o;
262 	container_widget->set_container(container_obj);
263 	if (doll_button)
264 		doll_button->Hide();
265 	if (left_arrow_button)
266 		left_arrow_button->Hide();
267 	if (right_arrow_button)
268 		right_arrow_button->Hide();
269 }
270 
Display(bool full_redraw)271 void ContainerViewGump::Display(bool full_redraw) {
272 //display_level_text();
273 //display_spell_list_text();
274 	Common::Rect dst;
275 	dst = area;
276 	SDL_BlitSurface(bg_image, NULL, surface, &dst);
277 
278 	DisplayChildren(full_redraw);
279 
280 	if (actor) {
281 		font->drawString(screen, actor->get_name(), area.left + 18, area.top + 2, 15, 15);
282 		display_inventory_weight();
283 	}
284 	update_display = false;
285 	screen->update(area.left, area.top, area.width(), area.height());
286 
287 	return;
288 }
289 
display_inventory_weight()290 void ContainerViewGump::display_inventory_weight() {
291 	uint8 strength = actor->get_strength();
292 	unsigned int equip_weight = Game::get_game()->get_view_manager()->get_display_weight(actor->get_inventory_weight());
293 	char string[11]; //I:nnn/nnns\0
294 
295 	snprintf(string, 10, "I:%u/%us", equip_weight, strength * 2);
296 	font->drawString(screen, string, area.left + (container_obj ? 18 : 18 + 34), area.top + bg_image->h + 2, 15, 15);
297 }
298 
left_arrow()299 void ContainerViewGump::left_arrow() {
300 	sint8 party_mem_num = party->get_member_num(actor);
301 	if (party_mem_num >= 0) {
302 		if (party_mem_num > 0)
303 			party_mem_num--;
304 		else
305 			party_mem_num = party->get_party_size() - 1;
306 
307 		set_actor(party->get_actor(party_mem_num));
308 		force_full_redraw_if_needed();
309 	}
310 }
311 
right_arrow()312 void ContainerViewGump::right_arrow() {
313 	set_actor(party->get_actor((party->get_member_num(actor) + 1) % party->get_party_size()));
314 	force_full_redraw_if_needed();
315 }
316 
callback(uint16 msg,GUI_CallBack * caller,void * data)317 GUI_status ContainerViewGump::callback(uint16 msg, GUI_CallBack *caller, void *data) {
318 	//close gump and return control to Magic class for clean up.
319 	if (caller == gump_button) {
320 		Game::get_game()->get_view_manager()->close_gump(this);
321 		return GUI_YUM;
322 	} else if (caller == down_arrow_button) {
323 		container_widget->down_arrow();
324 		return GUI_YUM;
325 	} else if (caller == up_arrow_button) {
326 		container_widget->up_arrow();
327 		return GUI_YUM;
328 	} else if (doll_button && caller == doll_button) {
329 		Game::get_game()->get_view_manager()->open_doll_view(actor);
330 		return GUI_YUM;
331 	} else if (left_arrow_button && caller == left_arrow_button) {
332 		left_arrow();
333 		return GUI_YUM;
334 	} else if (right_arrow_button && caller == right_arrow_button) {
335 		right_arrow();
336 		return GUI_YUM;
337 	}
338 
339 	return GUI_PASS;
340 }
341 
KeyDown(const Common::KeyState & key)342 GUI_status ContainerViewGump::KeyDown(const Common::KeyState &key) {
343 	if (left_arrow_button && left_arrow_button->Status() == WIDGET_VISIBLE) { // okay to change member number
344 		KeyBinder *keybinder = Game::get_game()->get_keybinder();
345 		ActionType a = keybinder->get_ActionType(key);
346 
347 		switch (keybinder->GetActionKeyType(a)) {
348 		case NEXT_PARTY_MEMBER_KEY:
349 			right_arrow();
350 			return GUI_YUM;
351 		case PREVIOUS_PARTY_MEMBER_KEY:
352 			left_arrow();
353 			return GUI_YUM;
354 		case HOME_KEY:
355 			set_actor(party->get_actor(0));
356 			force_full_redraw_if_needed();
357 			return GUI_YUM;
358 		case END_KEY:
359 			set_actor(party->get_actor(party->get_party_size() - 1));
360 			force_full_redraw_if_needed();
361 			return GUI_YUM;
362 		default:
363 			break;
364 		}
365 	}
366 	/* moved into container widget
367 	switch(key.keycode)
368 	    {
369 	        case Common::KEYCODE_RETURN:
370 	        case Common::KEYCODE_KP_ENTER:
371 
372 	            return GUI_YUM;
373 	        default:
374 	            break;
375 	    }
376 	*/
377 	return container_widget->KeyDown(key);
378 }
379 
MouseWheel(sint32 x,sint32 y)380 GUI_status ContainerViewGump::MouseWheel(sint32 x, sint32 y) {
381 	int xpos, ypos;
382 	screen->get_mouse_location(&xpos, &ypos);
383 	ypos -= area.top;
384 
385 	if (ypos >= container_widget_y_offset && ypos < container_widget_y_offset + container_widget->H()) {
386 		if (y > 0) {
387 			container_widget->up_arrow();
388 		} else if (y < 0) {
389 			container_widget->down_arrow();
390 		}
391 	} else {
392 		if (is_actor_container() && party->get_member_num(actor) >= 0) {
393 			if (y > 0) {
394 				left_arrow();
395 			} else if (y < 0) {
396 				right_arrow();
397 			}
398 		}
399 	}
400 	return GUI_YUM;
401 }
402 
MouseDown(int x,int y,Shared::MouseButton button)403 GUI_status ContainerViewGump::MouseDown(int x, int y, Shared::MouseButton button) {
404 	return DraggableView::MouseDown(x, y, button);
405 }
406 
MouseUp(int x,int y,Shared::MouseButton button)407 GUI_status ContainerViewGump::MouseUp(int x, int y, Shared::MouseButton button) {
408 	return DraggableView::MouseUp(x, y, button);
409 }
410 
411 } // End of namespace Nuvie
412 } // End of namespace Ultima
413