Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 49100b21488ca9d08719aeac91005483469e47bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
   client/mapwidget.cc
   This file is part of the Osirion project and is distributed under
   the terms of the GNU General Public License version 2
*/

#include "client/mapwidget.h"
#include "client/targets.h"
#include "core/application.h"
#include "core/entity.h"
#include "core/entityglobe.h"
#include "ui/paint.h"
#include "render/render.h"
#include "render/gl.h"
#include "client/targets.h"
#include "ui/ui.h"

namespace client {

MapWidget::MapWidget(ui::Widget *parent) : ui::Widget(parent)
{
	set_label("mapwidget");
	mapwidget_zone = 0;
	mapwidget_target = 0;
	mapwidget_hover_id = 0;
	mapwidget_zoom = 1.0f;
	
	// make sure the map has a tooltip
	set_tooltip();
}

MapWidget::~MapWidget()
{
	
}

void MapWidget::set_zoom(const float zoom)
{
	mapwidget_zoom = zoom;
}
void MapWidget::set_target(const core::Entity *entity)
{
	mapwidget_target = entity;	
}

void MapWidget::set_zone(core::Zone *zone)
{
	mapwidget_zone = zone;

	// verify current target
	if (mapwidget_target && (mapwidget_zone != mapwidget_target->zone()))
	{
		mapwidget_target = nullptr;
	}
}

bool MapWidget::on_mousepress(const unsigned int button)
{
	if (button == SDL_BUTTON_LEFT)
	{
		if (mapwidget_zone && hover())
		{
			core::Entity *target = mapwidget_zone->find_entity(hover());
			
			if (targets::is_valid_map_target(target))
			{
				set_target(target);
				emit(ui::Widget::EventClicked);
			}
		}
		return true;
	}

	return false;

}

bool MapWidget::on_mouserelease(const unsigned int button)
{
	if (button == SDL_BUTTON_LEFT)
	{
		return true;
	}
	
	return false;
}

void MapWidget::draw()
{
	if (!mapwidget_zone)
		return;
	
	// size and global location of the map
	const float map_size = math::min(width(), height());
	math::Vector2f map_location(global_location());
	
	if (map_size < width())
	{
		map_location[0] += (width() - map_size) * 0.5f;
	}
	if (map_size < height())
	{
		map_location[1] += (height() - map_size) * 0.5f;
	}

	// draw background
	math::Vector2f background_size(map_size, map_size);
	ui::Paint::draw_material(map_location, background_size, "ui/background");
	
	// number of squares in one grid segment
	const float grid_size = 16.0f;
	
	// draw the grid itself
	gl::color(0, 0, 0.8f);
	gl::begin(gl::Lines);
	
	for (float i = 0; i <= grid_size; i += 1.0f) {
		gl::vertex(map_location.x(), map_location.y() + map_size / grid_size * i);
		gl::vertex(map_location.x() + map_size, map_location.y() + map_size / grid_size * i);

		gl::vertex(map_location.x() + map_size / grid_size * i, map_location.y());
		gl::vertex(map_location.x() + map_size / grid_size * i, map_location.y() + map_size);
	}
	gl::end();

	// request required textures
	const size_t texture_entity = render::Textures::load("bitmaps/icons/entity_default");
	const size_t texture_globe = render::Textures::load("bitmaps/icons/entity_globe");
	const size_t texture_bright = render::Textures::load("bitmaps/icons/entity_bright");
	
	size_t texture_current = render::Textures::bind(texture_entity);

	// global mouse cursor location
	const math::Vector2f cursor(ui::root()->global_mouse_coords());
	mapwidget_hover_id = 0;
	set_tooltip();
	
	// map center
	math::Vector2f map_center(map_location[0] + map_size / 2.0f,  map_location[1] + map_size / 2.0f);
	math::Vector2f icon_location;
	const float r = 12.0f; 		// radius of map icons	
	const float rt = 24.0f;		// radius of target icons
	float scale = 4096.0f; 		// map size in game units
	math::Color color;

	// draw map icons
	gl::enable(GL_TEXTURE_2D);
	gl::begin(gl::Quads);


	for (core::Zone::Content::iterator it = mapwidget_zone->content().begin(); it != mapwidget_zone->content().end(); it++)
	{
		core::Entity *entity = (*it);

		bool draw_icon = false;
		
		icon_location.assign(map_center);
		
		if (targets::is_valid_map_target(entity))
		{
			draw_icon = true;
			icon_location[0] -= map_size / scale * entity->location().y();
			icon_location[1] -= map_size / scale * entity->location().x();
			
			if (math::distancesquared(cursor, icon_location) < (r*r))
			{
				mapwidget_hover_id = entity->id();
				set_tooltip(entity->name());
			}
			
			if (entity == mapwidget_target)
			{
				if (core::application()->time() - floorf(core::application()->time()) < 0.5f)
				{
					draw_icon = false;
				}
			}
		}

		if (draw_icon)
		{
			color.assign(entity->color());
			color.a = 1.0f;
			
			if (entity->type() == core::Entity::Globe)
			{
				// FIXME this is copy paste from the renderer and can use a cleanup
				core::EntityGlobe *globe = static_cast<core::EntityGlobe *>(entity);

				if (globe->has_flag(core::Entity::Bright))
				{
					
					gl::end();
					
					// load globe corona  texture
					if (!globe->corona_id() && globe->coronaname().size())
					{
						
						globe->set_corona_id(render::Textures::load("textures/" + globe->coronaname()));
						if (!globe->corona_id())
						{
							globe->set_coronaname("");
						}
					}
					
					// draw corona
					if (globe->corona_id())
					{
						texture_current = render::Textures::bind(globe->corona_id());
						
						gl::begin(gl::Quads);
						gl::color(color);
						glTexCoord2f(0.0f, 0.0f);
						gl::vertex(icon_location.x() - r * 2.0f, icon_location.y() - r * 2.0f);

						glTexCoord2f(1.0f, 0.0f);
						gl::vertex(icon_location.x() + r * 2.0f, icon_location.y() - r * 2.0f);

						glTexCoord2f(1.0f, 1.0f);
						gl::vertex(icon_location.x() + r * 2.0f, icon_location.y() + r * 2.0f);

						glTexCoord2f(0.0f, 1.0f);
						gl::vertex(icon_location.x() - r * 2.0f, icon_location.y() + r * 2.0f);
						gl::end();
					}
					
					// bind bright globe icon texture
					if (texture_current != texture_bright)
					{
						texture_current = render::Textures::bind(texture_bright);
					}
					
					gl::begin(gl::Quads);

				}
				else
				{
					// bind  globe icon texture
					if (texture_current != texture_globe)
					{
						gl::end();
						texture_current = render::Textures::bind(texture_globe);
						gl::begin(gl::Quads);
					}
				}
			}
			else
			{
				if (texture_current != texture_entity)
				{
					gl::end();
					texture_current = render::Textures::bind(texture_entity);
					gl::begin(gl::Quads);
				}
			}

			if (entity == core::localplayer()->mission_target())
			{
				color.assign(palette()->mission());

			}

			gl::color(color);
			glTexCoord2f(0.0f, 0.0f);
			gl::vertex(icon_location.x() - r, icon_location.y() - r);

			glTexCoord2f(1.0f, 0.0f);
			gl::vertex(icon_location.x() + r, icon_location.y() - r);

			glTexCoord2f(1.0f, 1.0f);
			gl::vertex(icon_location.x() + r, icon_location.y() + r);

			glTexCoord2f(0.0f, 1.0f);
			gl::vertex(icon_location.x() - r, icon_location.y() + r);
		}

	}
	gl::end();
	
	if (mapwidget_target)
	{
		// draw target icon
		size_t texture_target = 0;
 		if (mapwidget_target->has_flag(core::Entity::Dockable))
		{
			texture_target = render::Textures::load("bitmaps/icons/map_dockable");
		} else {
			texture_target = render::Textures::load("bitmaps/icons/map_target");
		}
		
		icon_location.assign(map_center);
		icon_location[0] -= map_size / scale * mapwidget_target->location().y();
		icon_location[1] -= map_size / scale * mapwidget_target->location().x();
		
		texture_current = render::Textures::bind(texture_target);
		
		gl::begin(gl::Quads);
	
		math::Color color(1.0);
		gl::color(color);
		
		glTexCoord2f(0.0f, 0.0f);
		gl::vertex(icon_location.x() - rt, icon_location.y() - rt);

		glTexCoord2f(1.0f, 0.0f);
		gl::vertex(icon_location.x() + rt, icon_location.y() - rt);

		glTexCoord2f(1.0f, 1.0f);
		gl::vertex(icon_location.x() + rt, icon_location.y() + rt);

		glTexCoord2f(0.0f, 1.0f);
		gl::vertex(icon_location.x() - rt, icon_location.y() + rt);
		
		gl::end();
	}

	// draw localcontrol icon
	// TODO draw a ship icon
	if (core::localcontrol()->zone() == mapwidget_zone)
	{
		core::EntityControlable *controlable = core::localcontrol();

		//if (core::localcontrol()->state() != core::Entity::Docked) {
		icon_location.assign(map_center);
		icon_location[0] -= map_size / scale * controlable->location().y();
		icon_location[1] -= map_size / scale * controlable->location().x();
		
		if (core::application()->time() - floorf(core::application()->time()) < 0.5f)
		{
			texture_current = render::Textures::bind(texture_entity);
			gl::begin(gl::Quads);
		
			math::Color color(controlable->color());
			color.a = 1.0f;
			gl::color(color);
			glTexCoord2f(0.0f, 0.0f);
			gl::vertex(icon_location.x() - r, icon_location.y() - r);

			glTexCoord2f(1.0f, 0.0f);
			gl::vertex(icon_location.x() + r, icon_location.y() - r);

			glTexCoord2f(1.0f, 1.0f);
			gl::vertex(icon_location.x() + r, icon_location.y() + r);

			glTexCoord2f(0.0f, 1.0f);
			gl::vertex(icon_location.x() - r, icon_location.y() + r);
			
			gl::end();
		}
	}
	
	gl::disable(GL_TEXTURE_2D);
	
// 	if (has_mouse_focus()) {
// 		if (mapwidget_hover_id)
// 			ui::root()->set_pointer("action", ui::Palette::Highlight);
// 	}
}

} // namespace client