From 597b3e0921dd24fa5e224377da4754da93a782c1 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Mon, 26 Jan 2009 20:18:22 +0000 Subject: new widgets to replace the "View" class --- src/client/infowidget.cc | 179 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 179 insertions(+) create mode 100644 src/client/infowidget.cc (limited to 'src/client/infowidget.cc') diff --git a/src/client/infowidget.cc b/src/client/infowidget.cc new file mode 100644 index 0000000..433ee64 --- /dev/null +++ b/src/client/infowidget.cc @@ -0,0 +1,179 @@ +/* + client/infowidget.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 +#include +#include +#include + +#include "auxiliary/functions.h" +#include "client/infowidget.h" +#include "client/client.h" +#include "client/input.h" +#include "client/targets.h" +#include "render/render.h" +#include "core/stats.h" +#include "math/mathlib.h" +#include "sys/sys.h" +#include "ui/paint.h" +#include "ui/ui.h" +#include "ui/widget.h" + +namespace client +{ + +void time_to_stream(std::stringstream &str, float time) +{ + int minutes = (int) floorf(time / 60.0f); + int seconds = (int) floorf( time - (float) minutes* 60.0f); + str << std::setfill(' ') << std::setw(4) << minutes << ":" << std::setfill('0') << std::setw(2) << seconds; +} + +/* -- DevInfoWidget ------------------------------------------------ */ + +DevInfoWidget::DevInfoWidget(ui::Widget *parent) : ui::Widget(parent) +{ + set_label("devinfo"); + set_border(false); + set_background(false); +} + +void DevInfoWidget::draw() +{ + std::stringstream textstream; + core::Entity *target = targets::current(); + float d = 0; + + textstream << "^Ncore ^B"; + time_to_stream(textstream, core::application()->time()); + textstream << '\n'; + if (core::game()) { + textstream << "^Ntime ^B"; + time_to_stream(textstream, core::game()->time()); + } + textstream << '\n'; + + if (core::localcontrol()) { + textstream << std::fixed << std::setprecision(2) + << "^Nx ^B" << core::localcontrol()->location().x << " " + << "^Ny ^B" << core::localcontrol()->location().y << " " + << "^Nz ^B" << core::localcontrol()->location().z << '\n'; + + textstream << "^Nthurst ^B" << core::localcontrol()->thrust() << " " + << "^Nspeed ^B" << core::localcontrol()->speed() << '\n'; + + if (target) { + d = math::distance(core::localcontrol()->location(), target->location()) - target->radius() - core::localcontrol()->radius(); + textstream << "^Ndist ^B" << d << '\n'; + } + } + + ui::paint::color(palette()->foreground()); + ui::paint::text(global_location(), size(), font(), textstream); +} + +/* -- StatsInfoWidget ---------------------------------------------- */ + +StatsInfoWidget::StatsInfoWidget(ui::Widget *parent) : ui::Widget(parent) +{ + set_label("stats"); + set_border(false); + set_background(false); + + // clear counters + for (size_t i =0; i < fps_counter_size; i++) + fps_counter_time[i] = 0.0f; + + for (size_t i = 0; i < net_counter_size; i++) + net_counter_traffic[i] = 0; + + net_counter_index = 0; + fps_counter_index = 0; +} + +void StatsInfoWidget::draw() +{ + // average fps + fps_counter_time[fps_counter_index] = core::application()->time(); + fps_counter_index = (fps_counter_index + 1 ) % fps_counter_size; + float min_time = core::application()->time(); + for (size_t i=0; i < fps_counter_size; i++) + if (fps_counter_time[i] < min_time) + min_time = fps_counter_time[i]; + float fps = 0.0f; + float t = (core::application()->time() - min_time); + if (t > 0) { + fps = roundf(((float) fps_counter_size - 1.0f) / t); + } + + std::stringstream textstream; + + if (core::game()) { + textstream << "^Ntime ^B"; + time_to_stream(textstream, core::game()->time()); + } + + textstream << std::setfill(' ') << "\n"; + textstream << "^Nfps ^B" << std::setw(6) << fps << "\n"; + + if (core::application()->connected()) { + textstream << "^Ntris ^B" << std::setw(5) << render::Stats::tris << "\n"; + textstream << "^Nquads ^B" << std::setw(5) << render::Stats::quads << "\n"; + + if (core::Stats::network_bytes_sent + core::Stats::network_bytes_received) { + net_counter_traffic[net_counter_index] = core::Stats::network_bytes_sent + core::Stats::network_bytes_received; + net_counter_time[net_counter_index] = core::application()->time(); + size_t index_max = net_counter_index; + + net_counter_index = (net_counter_index + 1) % net_counter_size; + size_t index_min = net_counter_index; + + float d = net_counter_time[index_max] - net_counter_time[index_min]; + if (d > 0) { + float traffic = net_counter_traffic[index_max] - net_counter_traffic[index_min]; + textstream << "^Nnet ^B" << std::setw(6) << roundf( (float) traffic / d ) << "\n"; + } + } + } + + ui::paint::color(palette()->foreground()); + ui::paint::text(global_location(), size(), font(), textstream); +} + +/* -- KeyInfoWidget ------------------------------------------------ */ + +KeyInfoWidget::KeyInfoWidget(ui::Widget *parent) : Widget(parent) +{ + set_label("keypress"); + set_border(false); + set_background(false); +} + +void KeyInfoWidget::draw() +{ + std::string label; + ui::paint::color(palette()->highlight()); + + Key::Modifier mod = input::modifier(); + if (mod != Key::None) { + if (mod == Key::Shift) + label.assign("shift+"); + else if (mod == Key::Ctrl) + label.assign("ctrl+"); + else if (mod == Key::Alt) + label.assign("alt+"); + } + + if(input::last_key_pressed()) { + label.append(input::last_key_pressed()->name()); + } + + if (label.size()) + ui::paint::label(global_location(), size(), font(), label , ui::AlignCenter); +} + +} + -- cgit v1.2.3