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
|
/*
client/galaxymapwidget.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/galaxymapwidget.h"
#include "core/application.h"
#include "ui/paint.h"
#include "render/render.h"
#include "render/gl.h"
#include "ui/ui.h"
namespace client {
GalaxyMapWidget::GalaxyMapWidget(ui::Widget *parent) : ui::Widget(parent)
{
set_label("galaxymapwidget");
galaxymapwidget_zone = 0;
galaxymapwidget_hover_id = 0;
galaxymapwidget_zoom = 1.0f;
// make sure the map has a tooltip
set_tooltip();
}
GalaxyMapWidget::~GalaxyMapWidget()
{
}
void GalaxyMapWidget::set_zoom(const float zoom)
{
galaxymapwidget_zoom = zoom;
}
void GalaxyMapWidget::set_zone(core::Zone *zone)
{
galaxymapwidget_zone = zone;
}
bool GalaxyMapWidget::on_mousepress(const unsigned int button)
{
if (button == SDL_BUTTON_LEFT)
{
if (hover())
{
if (zone() && (zone()->id() == hover()))
{
emit(ui::Widget::EventDoubleClicked);
}
else
{
set_zone(core::Zone::find(hover()));
emit(ui::Widget::EventClicked);
}
}
return true;
}
return false;
}
bool GalaxyMapWidget::on_mouserelease(const unsigned int button)
{
if (button == SDL_BUTTON_LEFT)
{
return true;
}
return false;
}
void GalaxyMapWidget::draw()
{
// 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 widget background
math::Vector2f background_size(map_size, map_size);
ui::Paint::draw_bitmap(map_location, background_size, math::Color(), "bitmaps/ui/map_background");
// number of squares in one grid segment
const float grid_size = 16.0f;
// draw the grid itself
gl::color(0, 0, 0.25f);
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_zone = render::Textures::load("bitmaps/icons/entity_default");
render::Textures::bind(texture_zone);
// global mouse cursor location
const math::Vector2f cursor(ui::root()->global_mouse_coords());
galaxymapwidget_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 = 1024.0f; // galaxy size (in zone location units)
math::Color color(1.0f, 1.0f, 1.0f, 1.0f);
const core::Zone *zone = 0;
// draw map icons
/*
* Note: the galactic coordinate system differs from the zone coordinate system:
* positive X-axis runs left-to-right on the screen and the positive Y-axis runs bottom-to-top on the screen.
*
* This is because I'm lazy and I just copy the coordinates from the SVG starsystem roadmap as shown in Inkscape.
*/
gl::enable(GL_TEXTURE_2D);
gl::begin(gl::Quads);
for (core::Zone::Registry::iterator it = core::Zone::registry().begin(); it != core::Zone::registry().end(); it++) {
zone = (*it).second;
if (zone->has_flag(core::Zone::Hidden))
{
continue;
}
bool draw_icon = true;
icon_location.assign(map_location);
icon_location[0] += map_size / scale * zone->location().x();
icon_location[1] += map_size - map_size / scale * zone->location().y(); // flip vertically
if (math::distancesquared(cursor, icon_location) < (r*r))
{
galaxymapwidget_hover_id = zone->id();
set_tooltip(zone->name());
}
if (zone == galaxymapwidget_zone)
{
if (core::application()->time() - floorf(core::application()->time()) < 0.5f)
{
draw_icon = false;
}
}
// TODO draw a ship icon for current location
if (draw_icon) {
color.assign(zone->color());
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 (galaxymapwidget_zone)
{
icon_location.assign(map_location);
icon_location[0] += map_size / scale * galaxymapwidget_zone->location().x();
icon_location[1] += map_size - map_size / scale * galaxymapwidget_zone->location().y(); // flip vertically
// draw target icon
const size_t texture_target = render::Textures::load("bitmaps/icons/map_target");
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();
}
gl::disable(GL_TEXTURE_2D);
// if (has_mouse_focus()) {
// if (galaxymapwidget_hover_id)
// ui::root()->set_pointer("action", ui::Palette::Highlight);
// }
}
} // namespace client
|