1 #include <stdlib.h>
2 #include <string.h>
3 #include <SDL.h>
4 #include "mapwin.h"
5 #include "asc.h"
6 #include "books.h"
7 #include "console.h"
8 #include "consolewin.h"
9 #include "chat.h"
10 #include "draw_scene.h"
11 #include "elwindows.h"
12 #include "events.h"
13 #include "gamewin.h"
14 #include "gl_init.h"
15 #include "hud.h"
16 #include "init.h"
17 #include "interface.h"
18 #include "map.h"
19 #include "missiles.h"
20 #include "new_character.h"
21 #include "pathfinder.h"
22 #include "textures.h"
23 #include "eye_candy_wrapper.h"
24 #include "special_effects.h"
25 
26 
27 static int showing_continent = 0;
28 #ifdef DEBUG_MAP_SOUND
29 extern int cur_tab_map;
30 #endif // DEBUG_MAP_SOUND
31 
32 int adding_mark = 0;
33 int mark_x , mark_y;
34 int max_mark = 0;
35 marking marks[MAX_MARKINGS];
36 
37 static int mouse_over_minimap = 0;
38 
39 static int reload_tab_map = 0;
40 
41 int temp_tile_map_size_x = 0;
42 int temp_tile_map_size_y = 0;
43 int max_temp_mark = 0;
44 marking temp_marks[MAX_USER_MARKS];
45 
46 #define MARK_FILTER_MAX_LEN 40
47 int mark_filter_active = 0;
48 char mark_filter_text[MARK_FILTER_MAX_LEN] = "";
49 
50 int curmark_r=0,curmark_g=255,curmark_b=0;
51 
click_map_handler(window_info * win,int mx,int my,Uint32 flags)52 static int click_map_handler (window_info *win, int mx, int my, Uint32 flags)
53 {
54 	Uint32 ctrl_on = flags & KMOD_CTRL;
55 	Uint32 left_click = flags & ELW_LEFT_MOUSE;
56 	Uint32 right_click = flags & ELW_RIGHT_MOUSE;
57 
58 	if (hud_click(win, mx, my, flags))
59 		return 1;
60 
61 	if (left_click && mx > small_map_screen_x_left && mx < small_map_screen_x_right
62 		&& my > small_map_screen_y_top && my < small_map_screen_y_bottom)
63 	{
64 		showing_continent = !showing_continent;
65 		inspect_map_text = 0;
66 	}
67 	else if (!showing_continent && inspect_map_text == 0)
68 	{
69 		if (left_click)
70 		{
71 			pf_move_to_mouse_position ();
72 		}
73 		else if (right_click)
74 		{
75 			if (!ctrl_on)
76 				put_mark_on_map_on_mouse_position ();
77 			else
78 				delete_mark_on_map_on_mouse_position ();
79 		}
80 	}
81 	else if(showing_continent)
82 	{
83 		if (left_click)
84 		{
85 			int screen_map_width = main_map_screen_x_right - main_map_screen_x_left;
86 			int screen_map_height = main_map_screen_y_bottom - main_map_screen_y_top;
87 
88 			int i;
89 			/* Convert mouse coordinates to map coordinates (stolen from pf_get_mouse_position()) */
90 			int m_px = ((mx-main_map_screen_x_left) * 512) / screen_map_width;
91 			int m_py = 512 - ((my-main_map_screen_y_top) * 512) / screen_map_height;
92 
93 			/* Check if we clicked on a map */
94 			for(i = 0; continent_maps[i].name != NULL; i++) {
95 				if(continent_maps[i].cont == continent_maps[cur_map].cont) {
96 					if(m_px > continent_maps[i].x_start && m_px < continent_maps[i].x_end
97 						&& m_py > continent_maps[i].y_start && m_py < continent_maps[i].y_end)
98 					{
99 						/* Load this map's bmp */
100 						if(cur_map != i) {
101 							inspect_map_text = load_texture_cached(continent_maps[i].name, tt_image);
102 						}
103 #ifdef DEBUG_MAP_SOUND
104 						cur_tab_map = i;
105 #endif // DEBUG_MAP_SOUND
106 						load_marks_to_buffer(continent_maps[i].name, temp_marks, &max_temp_mark);
107 						get_tile_map_sizes(continent_maps[i].name, &temp_tile_map_size_x, &temp_tile_map_size_y);
108 						showing_continent = !showing_continent;
109 						break;
110 					}
111 				}
112 			}
113 		}
114 	}
115 
116 	return 1;
117 }
118 
display_map_handler(window_info * win)119 static int display_map_handler (window_info * win)
120 {
121 	draw_hud_interface (win);
122 	Leave2DMode ();
123 	if(reload_tab_map && windows_list.window[win->window_id].displayed)
124 		switch_to_game_map(); //need to reload the BMP
125 	draw_game_map (!showing_continent, mouse_over_minimap);
126 	Enter2DMode ();
127 	CHECK_GL_ERRORS ();
128 	reload_tab_map = 0;
129 
130 	display_handling_common(win);
131 
132 	return 1;
133 }
134 
mouseover_map_handler(window_info * win,int mx,int my)135 static int mouseover_map_handler (window_info *win, int mx, int my)
136 {
137 	if (hud_mouse_over(win, mx, my))
138 		return 1;
139 
140 	mouse_over_minimap = (mx > small_map_screen_x_left && mx < small_map_screen_x_right
141 		&& my > small_map_screen_y_top && my < small_map_screen_y_bottom);
142 	return mouse_over_minimap;
143 }
144 
keypress_map_handler(window_info * win,int mx,int my,SDL_Keycode key_code,Uint32 key_unicode,Uint16 key_mod)145 static int keypress_map_handler (window_info *win, int mx, int my, SDL_Keycode key_code, Uint32 key_unicode, Uint16 key_mod)
146 {
147 	if ((key_code == SDLK_RETURN || key_code == SDLK_KP_ENTER) && adding_mark && input_text_line.len > 0)
148 	{
149 		int i;
150 
151 		// if text wrapping just keep the text until the wrap.
152 		for (i = 0; i < input_text_line.len; i++)
153 		{
154 			if (input_text_line.data[i] == '\n')
155 			{
156 				input_text_line.data[i] = '\0';
157 				break;
158 			}
159 		}
160 
161 		put_mark_on_position(mark_x, mark_y, input_text_line.data);
162 		adding_mark = 0;
163 		clear_input_line ();
164 	}
165 	// does the user want to cancel a mapmark?
166 	else if (key_code == SDLK_ESCAPE && adding_mark)
167 	{
168 		adding_mark = 0;
169 		clear_input_line ();
170 	}
171 	// enable, disable or reset the mark filter
172 	else if (KEY_DEF_CMP(K_MARKFILTER, key_code, key_mod) || (mark_filter_active && (key_code == SDLK_ESCAPE)))
173 	{
174 		if (!mark_filter_active || (key_code == SDLK_ESCAPE))
175 			mark_filter_active ^= 1;
176 		memset(mark_filter_text, 0, sizeof(char)*MARK_FILTER_MAX_LEN);
177 	}
178 	// now try the keypress handler for all root windows
179 	else if ( keypress_root_common (key_code, key_unicode, key_mod) )
180 	{
181 		return 1;
182 	}
183 	else if (KEY_DEF_CMP(K_MAP, key_code, key_mod))
184 	{
185 		return_to_gamewin_common();
186 	}
187 	else if (mark_filter_active && !adding_mark)
188 	{
189 		return string_input(mark_filter_text, MARK_FILTER_MAX_LEN, key_code, key_unicode, key_mod);
190 	}
191 	else
192 	{
193 		if (key_unicode == '`' || KEY_DEF_CMP(K_CONSOLE, key_code, key_mod))
194 		{
195 			hide_window (win->window_id);
196 			show_window_MW(MW_CONSOLE);
197 		}
198 		else if ( !text_input_handler (key_code, key_unicode, key_mod) )
199 		{
200 			// nothing we can handle
201 			return 0;
202 		}
203 	}
204 	// we handled it, return 1 to let the window manager know
205 	return 1;
206 }
207 
show_map_handler(window_info * win)208 static int show_map_handler (window_info *win)
209 {
210 	close_book_window();
211 	hide_window(tab_bar_win);
212 	return 1;
213 }
214 
resize_map_root_handler(window_info * win,int width,int height)215 static int resize_map_root_handler(window_info *win, int width, int height)
216 {
217 	if (get_show_window(win->window_id))
218 		init_hud_interface (HUD_INTERFACE_GAME);
219 	reload_tab_map = 1;
220 	return 1;
221 }
222 
hide_map_handler(window_info * win)223 static int hide_map_handler (window_info * win)
224 {
225 	show_console_input();
226 	return 1;
227 }
228 
create_map_root_window(int width,int height)229 void create_map_root_window (int width, int height)
230 {
231 	int map_root_win = get_id_MW(MW_TABMAP);
232 	if (map_root_win < 0)
233 	{
234 		map_root_win = create_window ("Map", -1, -1, 0, 0, width, height, ELW_USE_UISCALE|ELW_TITLE_NONE|ELW_SHOW_LAST);
235 		set_id_MW(MW_TABMAP, map_root_win);
236 
237 		set_window_handler (map_root_win, ELW_HANDLER_DISPLAY, &display_map_handler);
238 		set_window_handler (map_root_win, ELW_HANDLER_KEYPRESS, (int (*)())&keypress_map_handler);
239 		set_window_handler (map_root_win, ELW_HANDLER_CLICK, &click_map_handler);
240 		set_window_handler (map_root_win, ELW_HANDLER_MOUSEOVER, &mouseover_map_handler);
241 		set_window_handler (map_root_win, ELW_HANDLER_SHOW, &show_map_handler);
242 		set_window_handler (map_root_win, ELW_HANDLER_HIDE, &hide_map_handler);
243 		set_window_handler (map_root_win, ELW_HANDLER_RESIZE, &resize_map_root_handler);
244 	}
245 }
246 
247 
248 
249