From d2e9d7532f58f07c0ede3fad2dbf39a92130670c Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sun, 23 Dec 2012 17:10:54 +0000 Subject: Split HUD components into seperate widgets, added health bar,added temporary target health bar. --- src/client/Makefile.am | 6 +++ src/client/hud.cc | 91 +++++++++++++++++++++++++++++++++------- src/client/hud.h | 4 ++ src/client/hudenginestatus.cc | 86 ++++++++++++++++++++++++++++++++++++++ src/client/hudenginestatus.h | 36 ++++++++++++++++ src/client/hudplayerstatus.cc | 53 +++++++++++++++++++++++ src/client/hudplayerstatus.h | 46 ++++++++++++++++++++ src/client/hudtargetstatus.cc | 97 +++++++++++++++++++++++++++++++++++++++++++ src/client/hudtargetstatus.h | 46 ++++++++++++++++++++ 9 files changed, 450 insertions(+), 15 deletions(-) create mode 100644 src/client/hudenginestatus.cc create mode 100644 src/client/hudenginestatus.h create mode 100644 src/client/hudplayerstatus.cc create mode 100644 src/client/hudplayerstatus.h create mode 100644 src/client/hudtargetstatus.cc create mode 100644 src/client/hudtargetstatus.h (limited to 'src') diff --git a/src/client/Makefile.am b/src/client/Makefile.am index 1baefd8..cd87d63 100644 --- a/src/client/Makefile.am +++ b/src/client/Makefile.am @@ -20,6 +20,9 @@ noinst_HEADERS = \ galaxymapwidget.h \ gamewindow.h \ hud.h \ + hudenginestatus.h \ + hudplayerstatus.h \ + hudtargetstatus.h \ infowidget.h \ input.h \ inventorywindow.h \ @@ -52,6 +55,9 @@ libclient_la_SOURCES = \ galaxymapwidget.cc \ gamewindow.cc \ hud.cc \ + hudenginestatus.cc \ + hudplayerstatus.cc \ + hudtargetstatus.cc \ infowidget.cc \ input.cc \ inventorylistview.cc \ diff --git a/src/client/hud.cc b/src/client/hud.cc index f7fc0d7..7db0398 100644 --- a/src/client/hud.cc +++ b/src/client/hud.cc @@ -8,6 +8,9 @@ #include "core/core.h" #include "client/client.h" #include "client/hud.h" +#include "client/hudenginestatus.h" +#include "client/hudplayerstatus.h" +#include "client/hudtargetstatus.h" #include "client/input.h" #include "client/targets.h" #include "render/render.h" @@ -23,14 +26,17 @@ HUD::HUD(ui::Widget *parent) : Widget(parent) set_border(false); set_background(false); - /*hud_toolbar = new ui::Toolbar(this); - - hud_toolbar->add_button("", "Menu", "ui_menu"); - hud_toolbar->add_button("", "Chat", "ui_chat"); - hud_toolbar->add_button("", "Map", "ui_map"); - */ hud_center = new ui::Bitmap(this, "bitmaps/pointers/center"); hud_center->set_color(palette()->pointer()); + + // player status + hud_playerstatus = new HUDPlayerStatus(this); + + // engine status + hud_enginestatus = new HUDEngineStatus(this); + + // target status + hud_targetstatus = new HUDTargetStatus(this); } @@ -38,8 +44,21 @@ void HUD::resize() { //hud_toolbar->set_geometry(0.0f, 0.0f, width(), font()->height() *2 ); + const float padding = ui::root()->font_large()->height(); + hud_center->set_size(ui::pointer_size, ui::pointer_size); hud_center->set_location((size() - hud_center->size()) * 0.5f); + + const float w = (width() - 4 * padding) / 3.0f; + hud_playerstatus->set_size(w,ui::UI::elementsize.height() * 3.0f); + hud_playerstatus->set_location(padding, height() - hud_playerstatus->height() - padding); + + hud_enginestatus->set_size(w,ui::UI::elementsize.height()); + hud_enginestatus->set_location(hud_playerstatus->right() + padding, height() - hud_enginestatus->height() - padding); + + hud_targetstatus->set_size(w,ui::UI::elementsize.height() * 3.0f); + hud_targetstatus->set_location(hud_enginestatus->right() + padding, height() - hud_targetstatus->height() - padding); + } void HUD::draw_offscreen_target(core::Entity *entity, bool is_active_target) @@ -154,10 +173,12 @@ void HUD::draw_target(core::Entity *entity, bool is_active_target) if (entity == core::localplayer()->mission_target()) { gl::color(palette()->mission()); + } else if (entity->type() == core::Entity::Controlable) { gl::color(0, 1, 0, 1); // FIXME faction color + } else { - gl::color(1, 1, 1, 1); // FIXME neutral color + gl::color(palette()->foreground()); } // outer square0 @@ -176,13 +197,41 @@ void HUD::draw_target(core::Entity *entity, bool is_active_target) gl::vertex(cx, cy - (r*0.25f)); gl::end(); } - - gl::enable(GL_TEXTURE_2D); + if (is_active_target) { + gl::color(palette()->foreground()); + // owner name if (entity->type() == core::Entity::Controlable) { const core::EntityControlable *ec = static_cast(entity); + + if (ec->health() > 0) { + // on-screen health indicator + const float f = 2.0f * r * ec->health() / 100.0f; + + // health bar + gl::color(0, 1, 0, 1); + gl::begin(gl::Quads); + gl::vertex(cx - r, cy + r - 4 - render::Text::fontheight() * 0.5f); + gl::vertex(cx - r + f, cy + r - 4 - render::Text::fontheight() * 0.5f); + gl::vertex(cx - r + f, cy + r - 4); + gl::vertex(cx - r, cy + r - 4); + gl::end(); + + // frame + gl::color(palette()->foreground()); + + gl::begin(gl::LineLoop); + gl::vertex(cx - r, cy + r - 4 - render::Text::fontheight() * 0.5f); + gl::vertex(cx + r, cy + r - 4 - render::Text::fontheight() * 0.5f); + gl::vertex(cx + r, cy + r - 4); + gl::vertex(cx - r, cy + r - 4); + gl::end(); + } + + gl::enable(GL_TEXTURE_2D); + if (ec->owner()) { std::ostringstream strtoplabel; strtoplabel << "^B" << ec->owner()->name() << "^B\n"; @@ -191,15 +240,20 @@ void HUD::draw_target(core::Entity *entity, bool is_active_target) cy - r - 4 - render::Text::fontheight(), strtoplabel.str() ); } + + } else { + gl::enable(GL_TEXTURE_2D); } - // entity name + gl::color(palette()->foreground()); + + // on-screen entity name render::Text::draw( cx - aux::text_length(entity->name()) * render::Text::fontwidth() * 0.5f, cy + r + 4, entity->name() ); - // distance + // on-screen distance std::ostringstream strdistance; float d = math::distance(core::localcontrol()->location(), entity->location()) - entity->radius() - core::localcontrol()->radius(); if (d > 0) { @@ -247,7 +301,7 @@ void HUD::draw() gl::enable(GL_TEXTURE_2D); Text::setfont("gui", 12, 18); Text::setcolor('N'); //set normal color - + core::Zone *zone = core::localcontrol()->zone(); // draw HUD targets @@ -265,7 +319,9 @@ void HUD::draw() } } + gl::disable(GL_TEXTURE_2D); + /* // --------------------------------------------------------- // player info pane @@ -275,7 +331,9 @@ void HUD::draw() playerinfostr << "^Ncredits: " << core::localplayer()->credits(); Text::draw(width() - 4 - Text::fontwidth() * 52, height() - Text::fontheight() * 3 - 4, playerinfostr.str()); - + */ + + /* // --------------------------------------------------------- // target info pane @@ -323,6 +381,10 @@ void HUD::draw() Text::draw(width() - 4 - Text::fontwidth() * 30, height() - Text::fontheight() * 3 - 4, strtarget.str()); } + */ + + /* + //---------------------------------------------------------- // engine info pane @@ -397,8 +459,7 @@ void HUD::draw() Text::setfont("gui", 12, 18); Text::setcolor('N'); //set normal color - - gl::disable(GL_TEXTURE_2D); + */ if (has_mouse_focus()) { diff --git a/src/client/hud.h b/src/client/hud.h index bda6ea1..5c318f6 100644 --- a/src/client/hud.h +++ b/src/client/hud.h @@ -45,6 +45,10 @@ private: ui::Bitmap *hud_center; ui::Toolbar *hud_toolbar; + + ui::Widget *hud_playerstatus; + ui::Widget *hud_enginestatus; + ui::Widget *hud_targetstatus; }; } diff --git a/src/client/hudenginestatus.cc b/src/client/hudenginestatus.cc new file mode 100644 index 0000000..04b6f63 --- /dev/null +++ b/src/client/hudenginestatus.cc @@ -0,0 +1,86 @@ +/* + client/hudenginestatus.cc + This file is part of the Osirion project and is distributed under + the terms and conditions of the GNU General Public License version 2 +*/ + +#include "client/hudenginestatus.h" +#include "core/core.h" +#include "core/application.h" +#include "ui/ui.h" +#include "ui/paint.h" +#include "render/gl.h" + +namespace client +{ + +HUDEngineStatus::HUDEngineStatus(ui::Widget *parent) : ui::Widget(parent) +{ + set_border(false); + set_background(false); +} + +void HUDEngineStatus::draw() +{ + const float padding = font()->width() * 0.25f; + + math::Vector2f pos(global_location()); + pos[0] += padding; + pos[1] += padding; + + math::Vector2f s(size()); + s[0] -= 2.0f * padding; + s[1] -= 2.0f * padding; + + // thrust + std::ostringstream thruststr; + if (core::localcontrol()->state() == core::Entity::Normal) { + thruststr << "^B" << roundf(core::localcontrol()->thrust() * 100.0f) << "%"; + } else { + thruststr << "^B--"; + } + + // speed + std::ostringstream speedstr; + speedstr << "^B" << roundf(core::localcontrol()->speed() * 100.0f); + + ui::Paint::set_color(palette()->foreground()); + ui::Paint::draw_label(pos, s, ui::root()->font_large(), thruststr.str(), ui::AlignLeft | ui::AlignVCenter); + ui::Paint::draw_label(pos, s, ui::root()->font_large(), speedstr.str(), ui::AlignRight | ui::AlignVCenter); + + // health bar size + const float hb_width = width() - 12 * ui::root()->font_large()->width() - 2.0f * padding; + const float hb_height = ui::root()->font_tiny()->width(); + const float hb_f = hb_width * core::localcontrol()->health() / 100.0f; + + pos.assign(global_location()); + pos[0] += (width() - hb_width) * 0.5f; + pos[1] += (height() - hb_height) * 0.5f; + + // health bar + gl::color(0, 1, 0, 1); + gl::begin(gl::Quads); + gl::vertex(pos[0], pos[1]); + gl::vertex(pos[0] + hb_f, pos[1]); + gl::vertex(pos[0] + hb_f, pos[1] + hb_height); + gl::vertex(pos[0], pos[1] + hb_height); + gl::end(); + + // health bar frame + gl::color(palette()->foreground()); + + gl::begin(gl::LineLoop); + gl::vertex(pos[0], pos[1]); + gl::vertex(pos[0] + hb_width, pos[1]); + gl::vertex(pos[0] + hb_width, pos[1] + hb_height); + gl::vertex(pos[0], pos[1] + hb_height); + gl::end(); +} + +bool HUDEngineStatus::on_keypress(const int key, const unsigned int modifier) +{ + return false; +} + +} // namespace client + diff --git a/src/client/hudenginestatus.h b/src/client/hudenginestatus.h new file mode 100644 index 0000000..ff1c813 --- /dev/null +++ b/src/client/hudenginestatus.h @@ -0,0 +1,36 @@ +/* + client/hudenginestatus.h + This file is part of the Osirion project and is distributed under + the terms and conditions of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_CLIENT_HUDENGINESTATUS_H__ +#define __INCLUDED_CLIENT_HUDENGINESTATUS_H__ + +#include "ui/widget.h" + +namespace client +{ + +/** + * @brief A Wdiget used by the HUD to draw the engine status + * */ + +class HUDEngineStatus : public ui::Widget +{ +public: + /// create a new HUD widget + HUDEngineStatus(ui::Widget *parent = 0); + +protected: + /// draw hud elements + virtual void draw(); + + /// receive keyboard events + virtual bool on_keypress(const int key, const unsigned int modifier); +}; + +} + +#endif // __INCLUDED_CLIENT_HUDENGINESTATUS_H__ + diff --git a/src/client/hudplayerstatus.cc b/src/client/hudplayerstatus.cc new file mode 100644 index 0000000..e1f921a --- /dev/null +++ b/src/client/hudplayerstatus.cc @@ -0,0 +1,53 @@ +/* + client/hudplayerstatus.cc + This file is part of the Osirion project and is distributed under + the terms and conditions of the GNU General Public License version 2 +*/ + +#include "client/hudplayerstatus.h" +#include "core/core.h" +#include "core/application.h" +#include "ui/ui.h" +#include "ui/paint.h" + +namespace client +{ + +HUDPlayerStatus::HUDPlayerStatus(ui::Widget *parent) : ui::Widget(parent) +{ + set_border(true); + set_background(true); + set_font(ui::root()->font_tiny()); +} + +void HUDPlayerStatus::draw_background() +{ + ui::Paint::draw_material(global_location(), size(), "ui/window"); +} + +void HUDPlayerStatus::draw() +{ + const float padding = font()->width() * 0.25f; + + math::Vector2f pos(global_location()); + pos[0] += padding; + pos[1] += padding; + + math::Vector2f s(width() - 2.0f * padding, height() - 2.0f * padding); + + std::ostringstream playerinfostr; + playerinfostr << "^B" << core::localplayer()->name() << '\n'; + playerinfostr << "^B" << core::localcontrol()->name() << '\n'; + playerinfostr << "^NCredits: " << core::localplayer()->credits(); + + ui::Paint::set_color(palette()->foreground()); + ui::Paint::draw_label(pos, s, font(), playerinfostr.str(), ui::AlignLeft | ui::AlignTop); +} + +bool HUDPlayerStatus::on_keypress(const int key, const unsigned int modifier) +{ + return false; +} + +} // namespace client + diff --git a/src/client/hudplayerstatus.h b/src/client/hudplayerstatus.h new file mode 100644 index 0000000..87cc086 --- /dev/null +++ b/src/client/hudplayerstatus.h @@ -0,0 +1,46 @@ +/* + client/hydplayerstatus.h + This file is part of the Osirion project and is distributed under + the terms and conditions of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_CLIENT_HUDPLAYERSTATUS_H__ +#define __INCLUDED_CLIENT_HUDPLAYERSTATUS_H__ + +#include "ui/widget.h" + +namespace client +{ + +/** + * @brief A Wdiget used by the HUD to draw the player's status + * */ + +class HUDPlayerStatus : public ui::Widget +{ +public: + /// create a new HUD widget + HUDPlayerStatus(ui::Widget *parent = 0); + +protected: + /** + * @brief draw window background + * */ + virtual void draw_background(); + + /** + * @brief draw player status + * */ + virtual void draw(); + + /** + * @brief receive keyboard events + * This implementation ignores any incoming key events + * */ + virtual bool on_keypress(const int key, const unsigned int modifier); +}; + +} + +#endif // __INCLUDED_CLIENT_HUDPLAYERSTATUS_H__ + diff --git a/src/client/hudtargetstatus.cc b/src/client/hudtargetstatus.cc new file mode 100644 index 0000000..1c910d3 --- /dev/null +++ b/src/client/hudtargetstatus.cc @@ -0,0 +1,97 @@ +/* + client/hudtargetstatus.cc + This file is part of the Osirion project and is distributed under + the terms and conditions of the GNU General Public License version 2 +*/ + +#include "client/hudtargetstatus.h" +#include "client/targets.h" +#include "client/client.h" +#include "ui/ui.h" +#include "ui/paint.h" + +namespace client +{ + +HUDTargetStatus::HUDTargetStatus(ui::Widget *parent) : ui::Widget(parent) +{ + set_border(true); + set_background(true); + set_font(ui::root()->font_tiny()); +} + +void HUDTargetStatus::draw_background() +{ + ui::Paint::draw_material(global_location(), size(), "ui/window"); +} + +void HUDTargetStatus::draw() +{ + const core::Entity *target = targets::current(); + + if (target) { + + const float padding = font()->width() * 0.25f; + + math::Vector2f pos(global_location()); + pos[0] += padding; + pos[1] += padding; + + math::Vector2f s(width() - 2.0f * padding, height() - 2.0f * padding); + + std::ostringstream targetinfostr; + + if (target->type() == core::Entity::Controlable) { + const core::EntityControlable *target_controlable = static_cast(target); + if (target_controlable->owner()) { + targetinfostr << "^B" << target_controlable->owner()->name() << "\n"; + targetinfostr << "^B" << target->name() << "\n"; + } else { + targetinfostr << "^B" << target->name() << "\n"; + targetinfostr << "\n"; + } + } else { + targetinfostr << "^B" << target->name() << "\n"; + targetinfostr << "\n"; + } + + float d = math::distance(core::localcontrol()->location(), target->location()) + - target->radius() - core::localcontrol()->radius(); + + if (d > 0) { + targetinfostr << "^NDist:^B "; + if (d > 100.0f) { + targetinfostr << roundf(d * 0.1f) << "km"; + } else { + targetinfostr << roundf(d * 100.0f) << "m"; + } + + if (core::localcontrol()->speed() > 0.1f) { + targetinfostr << "^N ETA:^B "; + float eta = floorf(d / core::localcontrol()->speed()); + if (eta > 60.0f) { + float etamin = floorf(eta / 60.0f); + targetinfostr << etamin << "min "; + eta -= etamin * 60; + } + targetinfostr << eta << "sec"; + } + } else if (target->has_flag(core::Entity::Dockable)) { + targetinfostr << "^FDock"; + } else { + targetinfostr << "^B--"; + } + + ui::Paint::set_color(palette()->foreground()); + ui::Paint::draw_label(pos, s, font() , targetinfostr.str(), ui::AlignLeft | ui::AlignTop); + + } +} + +bool HUDTargetStatus::on_keypress(const int key, const unsigned int modifier) +{ + return false; +} + +} // namespace client + diff --git a/src/client/hudtargetstatus.h b/src/client/hudtargetstatus.h new file mode 100644 index 0000000..52dd3bd --- /dev/null +++ b/src/client/hudtargetstatus.h @@ -0,0 +1,46 @@ +/* + client/hudtargetstatus.h + This file is part of the Osirion project and is distributed under + the terms and conditions of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_CLIENT_HUDTARGETSTATUS_H__ +#define __INCLUDED_CLIENT_HUDTARGETSTATUS_H__ + +#include "ui/widget.h" + +namespace client +{ + +/** + * @brief A Wdiget used by the HUD to draw the current target status + * */ + +class HUDTargetStatus : public ui::Widget +{ +public: + /// create a new HUD widget + HUDTargetStatus(ui::Widget *parent = 0); + +protected: + /** + * @brief draw window background + * */ + virtual void draw_background(); + + /** + * @brief draw current target status + * */ + virtual void draw(); + + /** + * @brief receive keyboard events + * This implementation ignores any incoming key events + * */ + virtual bool on_keypress(const int key, const unsigned int modifier); +}; + +} + +#endif // __INCLUDED_CLIENT_HUDTARGETSTATUS_H__ + -- cgit v1.2.3