diff options
Diffstat (limited to 'src/client/mapwindow.cc')
-rw-r--r-- | src/client/mapwindow.cc | 374 |
1 files changed, 374 insertions, 0 deletions
diff --git a/src/client/mapwindow.cc b/src/client/mapwindow.cc new file mode 100644 index 0000000..d6e45bc --- /dev/null +++ b/src/client/mapwindow.cc @@ -0,0 +1,374 @@ + +/* + client/mapwindow.cc + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#include "audio/audio.h" +#include "core/application.h" +#include "client/mapwindow.h" +#include "client/targets.h" +#include "ui/ui.h" +#include "ui/paint.h" +#include "render/gl.h" +#include "render/textures.h" +#include "render/text.h" + +namespace client +{ + +MapWindow::MapWindow(ui::Widget *parent) : ui::Window(parent) +{ + set_label("map"); + set_border(true); + set_background(true); + set_font(ui::root()->font_small()); + + // window title + mapwindow_titlelabel = new ui::Label(this); + mapwindow_titlelabel->set_label("title"); + mapwindow_titlelabel->set_background(false); + mapwindow_titlelabel->set_border(false); + mapwindow_titlelabel->set_font(ui::root()->font_large()); + mapwindow_titlelabel->set_alignment(ui::AlignCenter); + mapwindow_titlelabel->set_text("STAR CHART"); + + // map widget + mapwindow_mapwidget = new MapWidget(this); + mapwindow_mapwidget->set_background(false); + mapwindow_mapwidget->set_border(false); + + // map label (map widget child) + mapwindow_maplabel = new ui::Label(mapwindow_mapwidget); + mapwindow_maplabel->set_label("maplabel"); + mapwindow_maplabel->set_background(false); + mapwindow_maplabel->set_border(false); + mapwindow_maplabel->set_alignment(ui::AlignCenter); + + // modelview + mapwindow_modelview = new ui::ModelView(this); + mapwindow_modelview->set_label("modelview"); + mapwindow_modelview->set_background(false); + mapwindow_modelview->set_border(false); + + // target title (modelview child) + mapwindow_targetlabel = new ui::Label(mapwindow_modelview); + mapwindow_targetlabel->set_label("targetlabel"); + mapwindow_targetlabel->set_background(false); + mapwindow_targetlabel->set_border(false); + mapwindow_targetlabel->set_alignment(ui::AlignCenter); + + // target text + mapwindow_scrollpane = new ui::ScrollPane(this, mapwindow_infotext); + mapwindow_scrollpane->set_background(false); + mapwindow_scrollpane->set_border(false); + mapwindow_scrollpane->set_alignment(ui::AlignTop); + + set_target(0); + hide(); +} + +MapWindow::~MapWindow() +{ +} + +void MapWindow::hide() +{ + ui::Window::hide(); + mapwindow_target = 0; +} + +void MapWindow::show() +{ + ui::Window::show(); + if (core::localplayer()->view()) { + set_target(core::localplayer()->view()); + } else { + set_target(targets::current()); + } +} + +void MapWindow::toggle() +{ + if (visible()) + hide(); + else + show(); +} + +void MapWindow::resize() +{ + const float padding = ui::root()->font_large()->height(); + + // resize title label + mapwindow_titlelabel->set_size(width() - padding * 2.0f, mapwindow_titlelabel->font()->height()); + mapwindow_titlelabel->set_location(padding, padding); + + // resize map widget + mapwindow_mapwidget->set_size((width() - padding * 3.0f) * 0.5f, height() - mapwindow_titlelabel->bottom() - padding * 2.0f ); + mapwindow_mapwidget->set_location(padding, mapwindow_titlelabel->bottom() + padding); + + // resize map label (map widget child) + mapwindow_maplabel->set_size(mapwindow_mapwidget->width(), mapwindow_maplabel->font()->height()); + mapwindow_maplabel->set_location(0, 0); + + // resize target modelview + mapwindow_modelview->set_size(width() - mapwindow_mapwidget->right() - padding * 2.0f, (height() - mapwindow_titlelabel->bottom() - padding * 3.0f) * 0.5f); + mapwindow_modelview->set_location(mapwindow_mapwidget->right() + padding, mapwindow_titlelabel->bottom() + padding); + + // resize target label (modelview child) + mapwindow_targetlabel->set_size(mapwindow_modelview->width(), mapwindow_targetlabel->font()->height()); + mapwindow_targetlabel->set_location(0, 0); + + // resize target infopane text + mapwindow_scrollpane->set_size(width() - mapwindow_mapwidget->right() - padding * 2.0f, height() - mapwindow_modelview->bottom() - padding * 2.0f); + mapwindow_scrollpane->set_location(mapwindow_mapwidget->right() + padding, mapwindow_modelview->bottom() + padding); + +} + +void MapWindow::draw() +{ + ui::Window::draw(); + + // make sure the target still exists + mapwindow_target = core::localplayer()->zone()->find_entity(mapwindow_target); + + mapwindow_mapwidget->set_zone(core::localplayer()->zone()); + mapwindow_mapwidget->set_target(mapwindow_target); + mapwindow_maplabel->set_text(core::localplayer()->zone()->name()); + + /* + const float fontmargin = mapwindow_targetlabel->font()->height(); + const float s = ui::UI::elementsize.width() * 1.5f; + + const float blue = 0.8f; + const float gridsize = 16; + + const core::Entity *entity; + const core::Entity *current_target = mapwindow_target; + mapwindow_target = 0; + + math::Color color; + + math::Vector2f v(global_location()); + math::Vector2f l; + + v[0] += fontmargin; + v[1] += fontmargin + (height() - s) * 0.5f; + mapwindow_hover = 0; + + gl::color(0, 0, blue); + + gl::begin(gl::Lines); + for (int i = 0; i <= gridsize; i++) { + gl::vertex(v.x(), v.y() + s / gridsize * i); + gl::vertex(v.x() + s, v.y() + s / gridsize * i); + + gl::vertex(v.x() + s / gridsize * i, v.y()); + gl::vertex(v.x() + s / gridsize * i, v.y() + s); + } + gl::end(); + + 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); + + v[0] += s * 0.5f; + v[1] += s * 0.5f; + + core::Zone *zone = core::localplayer()->zone(); + + mapwindow_maplabel->set_text(zone->name()); + + const math::Vector2f cursor(input::mouse_position_x(), input::mouse_position_y()); + + const float r = 12.0f; + float scale = 2048.0f; + scale *= 2; + + gl::enable(GL_TEXTURE_2D); + gl::begin(gl::Quads); + + // draw map icons + for (core::Zone::Content::iterator it = zone->content().begin(); it != zone->content().end(); it++) { + entity = (*it); + + bool draw_icon = false; + l.assign(v); + + if (targets::is_valid_map_target(entity)) { + draw_icon = true; + l[0] -= s / scale * entity->location().y(); + l[1] -= s / scale * entity->location().x(); + + if (math::distancesquared(cursor, l) < (r*r)) { + mapwindow_hover = entity->id(); + } + + if (entity == current_target) { + mapwindow_target = entity; + if (core::application()->time() - floorf(core::application()->time()) < 0.5f) { + draw_icon = false; + } + } + } + + if (draw_icon) { + if (entity->type() == core::Entity::Globe) { + if (entity->flag_is_set(core::Entity::Bright)) { + if (texture_current != texture_bright) { + gl::end(); + texture_current = render::Textures::bind(texture_bright); + gl::begin(gl::Quads); + } + } else { + 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()); + } else { + color.assign(entity->color()); + } + color.a = 1.0f; + + gl::color(color); + glTexCoord2f(0.0f, 0.0f); + gl::vertex(l.x() - r, l.y() - r); + + glTexCoord2f(1.0f, 0.0f); + gl::vertex(l.x() + r, l.y() - r); + + glTexCoord2f(1.0f, 1.0f); + gl::vertex(l.x() + r, l.y() + r); + + glTexCoord2f(0.0f, 1.0f); + gl::vertex(l.x() - r, l.y() + r); + } + + } + + // draw localcontrol icon + entity = core::localcontrol(); + + //if (core::localcontrol()->state() != core::Entity::Docked) { + l.assign(v); + l[0] -= s / scale * entity->location().y(); + l[1] -= s / scale * entity->location().x(); + if (core::application()->time() - floorf(core::application()->time()) < 0.5f) { + if (texture_current != texture_entity) { + gl::end(); + texture_current = render::Textures::bind(texture_entity); + gl::begin(gl::Quads); + } + + math::Color color(entity->color()); + color.a = 1.0f; + gl::color(color); + glTexCoord2f(0.0f, 0.0f); + gl::vertex(l.x() - r, l.y() - r); + + glTexCoord2f(1.0f, 0.0f); + gl::vertex(l.x() + r, l.y() - r); + + glTexCoord2f(1.0f, 1.0f); + gl::vertex(l.x() + r, l.y() + r); + + glTexCoord2f(0.0f, 1.0f); + gl::vertex(l.x() - r, l.y() + r); + } + //} + + gl::end(); + gl::disable(GL_TEXTURE_2D); + + if (mapwindow_target != current_target ) { + // this makes sure the map target exists + set_target(current_target); + + } else if (mapwindow_inforecord && (mapwindow_infotimestamp != mapwindow_inforecord->timestamp())) { + set_target(mapwindow_target); + } + */ +} + +void MapWindow::set_target(const core::Entity *entity) { + + mapwindow_target = entity; + mapwindow_infotimestamp = 0; + mapwindow_inforecord = 0; + mapwindow_infotext.clear(); + + if (mapwindow_target) { + // set title label to target name + mapwindow_targetlabel->set_text(mapwindow_target->name()); + + if (mapwindow_target->info()) + mapwindow_inforecord = core::game()->request_info(mapwindow_target->info()->id()); + else + mapwindow_inforecord = 0; + + if (mapwindow_inforecord) { + for (core::Info::Text::const_iterator it = mapwindow_inforecord->text().begin(); it != mapwindow_inforecord->text().end(); it++) { + mapwindow_infotext.push_back((*it)); + } + mapwindow_infotimestamp = mapwindow_inforecord->timestamp(); + } else { + mapwindow_infotext.push_back("Information is not available"); + mapwindow_infotimestamp = 0; + } + + if (mapwindow_target->model()) { + mapwindow_modelview->set_modelname(mapwindow_target->model()->name()); + mapwindow_modelview->set_colors(mapwindow_target->color(), mapwindow_target->color_second()); + } else { + mapwindow_modelview->set_modelname(0); + } + + mapwindow_modelview->show(); + mapwindow_scrollpane->show(); + + } else { + + mapwindow_modelview->hide(); + mapwindow_scrollpane->hide(); + } +} + +bool MapWindow::on_keypress(const int key, const unsigned int modifier) +{ + if (key == 512 + SDL_BUTTON_LEFT) { + if (mapwindow_mapwidget->has_mouse_focus() && mapwindow_mapwidget->hover()) { + core::Entity *target = core::localplayer()->zone()->find_entity(mapwindow_mapwidget->hover()); + if (targets::is_valid_map_target(target)) { + set_target(target); + targets::set_target(mapwindow_target); + } + } + return true; + + } else if (key == SDLK_ESCAPE) { + if (visible()) { + hide(); + return true; + } + } + + return false; +} + +} |