Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define placeholders for text #101

Merged
merged 19 commits into from
Oct 3, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/examples/cameras.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class MyApp : public robot_dart::gui::magnum::GlfwApplication {
// we synchronize by default if we have the graphics activated
simu->scheduler().set_sync(true);
// enable summary text when graphics activated
simu->enable_summary_text(true);
simu->enable_text_panel(true);
simu->enable_status_bar(true);
}

protected:
Expand Down
2 changes: 2 additions & 0 deletions src/examples/talos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ int main()
Eigen::VectorXd commands = (init_positions + delta_pos) - robot->positions(dofs);
robot->set_commands(commands, dofs);
}
simu.set_text_panel(robot->model_filename() + "\n"
+ simu.default_text_panel());
costashatz marked this conversation as resolved.
Show resolved Hide resolved
simu.step_world();
}

Expand Down
5 changes: 4 additions & 1 deletion src/python/simu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,11 @@ namespace robot_dart {
.def("remove_robot", (void (RobotDARTSimu::*)(size_t)) & RobotDARTSimu::remove_robot)
.def("clear_robots", &RobotDARTSimu::clear_robots)

.def("enable_summary_text", &RobotDARTSimu::enable_summary_text,
.def("enable_text_panel", &RobotDARTSimu::enable_text_panel,
py::arg("enable") = true)
.def("enable_status_bar", &RobotDARTSimu::enable_status_bar,
py::arg("enable") = true)


.def("add_text", &RobotDARTSimu::add_text, py::return_value_policy::reference,
py::arg("text"),
Expand Down
2 changes: 1 addition & 1 deletion src/robot_dart/gui/magnum/base_application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ namespace robot_dart {
_font->fillGlyphCache(*_glyph_cache,
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789:-+,.!° ");
"0123456789:-+*,.!° /|[]()_");

/* Initialize dynamic text */
_dynamic_text.reset(new Magnum::Text::Renderer2D(*_font, *_glyph_cache, 32.0f, Magnum::Text::Alignment::TopLeft));
Expand Down
3 changes: 2 additions & 1 deletion src/robot_dart/gui/magnum/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ namespace robot_dart {
// we synchronize by default if we have the graphics activated
simu->scheduler().set_sync(true);
// enable summary text when graphics activated
simu->enable_summary_text(true);
simu->enable_text_panel(true);
simu->enable_status_bar(true);
}
} // namespace magnum
} // namespace gui
Expand Down
3 changes: 2 additions & 1 deletion src/robot_dart/gui/magnum/windowless_graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ namespace robot_dart {
// we should not synchronize by default if we want windowless graphics (usually used only for sensors)
simu->scheduler().set_sync(false);
// disable summary text when windowless graphics activated
simu->enable_summary_text(false);
simu->enable_text_panel(false);
simu->enable_status_bar(false);
}
} // namespace magnum
} // namespace gui
Expand Down
77 changes: 57 additions & 20 deletions src/robot_dart/robot_dart_simu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,28 +147,35 @@ namespace robot_dart {
}

if (_scheduler(_graphics_freq)) {
if (_text_panel) {
if (_text_panel->text == "")
_text_panel->text = default_text_panel();
Eigen::Affine2d tf = Eigen::Affine2d::Identity();
tf.translate(Eigen::Vector2d(-static_cast<double>(_graphics->width()) / 2., _graphics->height() / 2.));
_text_panel->transformation = tf;
}
if (_status_bar){
if (_status_bar->text == "")
_status_bar->text = default_status_bar();
Eigen::Affine2d tf = Eigen::Affine2d::Identity();
tf.translate(Eigen::Vector2d(-static_cast<double>(_graphics->width()) / 2., 50 -_graphics->height() / 2.));
_status_bar->transformation = tf;
}

_graphics->refresh();

for (auto& robot : _robots) {
_gui_data->update_robot(robot);
}

if (_text_panel)
_text_panel->text = "";
if (_status_bar)
_status_bar->text = "";
}

_old_index++;
double rt = _scheduler.step();
if (_summary_text) {
std::ostringstream out;
out.precision(3);
out << std::fixed << "Sim. Time: " << _world->getTime() << "s" << std::endl
<< "Time: " << rt << "s";

_summary_text->text = out.str();

Eigen::Affine2d tf = Eigen::Affine2d::Identity();
tf.translate(Eigen::Vector2d(-static_cast<double>(_graphics->width()) / 2., _graphics->height() / 2.));

_summary_text->transformation = tf;
}
_scheduler.step();

return _break;
}
Expand Down Expand Up @@ -407,18 +414,48 @@ namespace robot_dart {

simu::GUIData* RobotDARTSimu::gui_data() { return &(*_gui_data); }

void RobotDARTSimu::enable_summary_text(bool enable)

void RobotDARTSimu::enable_text_panel(bool enable) { _enable(_text_panel, enable); }
void RobotDARTSimu::enable_status_bar(bool enable) { _enable(_status_bar, enable); }
void RobotDARTSimu::_enable(std::shared_ptr<simu::TextData>& text, bool enable)
{
if (!_summary_text && enable) {
_summary_text = _gui_data->add_text("Sim. Time: 0s");
if (!text && enable) {
text = _gui_data->add_text("");
}
else if (!enable) {
if (_summary_text)
_gui_data->remove_text(_summary_text);
_summary_text = nullptr;
if (text)
_gui_data->remove_text(text);
text = nullptr;
}
}

void RobotDARTSimu::set_text_panel(const std::string& str) {
if (_text_panel)
_text_panel->text = str;
}

void RobotDARTSimu::set_status_bar(const std::string& str) {
if (_status_bar)
_status_bar->text = str;
}

std::string RobotDARTSimu::default_text_panel() const {
std::ostringstream out;
out.precision(3);
double rt = _scheduler.current_time();
costashatz marked this conversation as resolved.
Show resolved Hide resolved

out << std::fixed << "Sim. Time: " << _world->getTime() << "s" << std::endl
<< "Time: " << rt << "s";

return out.str();
}

std::string RobotDARTSimu::default_status_bar() const {
if (_robots.empty())
return "";
return _robots[0]->model_filename();
}

std::shared_ptr<simu::TextData> RobotDARTSimu::add_text(const std::string& text, const Eigen::Affine2d& tf, Eigen::Vector4d color)
{
return _gui_data->add_text(text, tf, color);
Expand Down
13 changes: 11 additions & 2 deletions src/robot_dart/robot_dart_simu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,12 @@ namespace robot_dart {
void clear_robots();

simu::GUIData* gui_data();
void enable_summary_text(bool enable = true);
void enable_text_panel(bool enable = true);
std::string default_text_panel() const;
void set_text_panel(const std::string& str);
void enable_status_bar(bool enable = true);
std::string default_status_bar() const;
void set_status_bar(const std::string& str);
std::shared_ptr<simu::TextData> add_text(const std::string& text, const Eigen::Affine2d& tf = Eigen::Affine2d::Identity(), Eigen::Vector4d color = Eigen::Vector4d(1, 1, 1, 1));

void add_floor(double floor_width = 10.0, double floor_height = 0.1, const Eigen::Vector6d& pose = Eigen::Vector6d::Zero(), const std::string& floor_name = "floor");
Expand All @@ -138,6 +143,8 @@ namespace robot_dart {
void remove_all_collision_masks();

protected:
void _enable(std::shared_ptr<simu::TextData>& text, bool enable);

dart::simulation::WorldPtr _world;
size_t _old_index;
bool _break;
Expand All @@ -147,7 +154,9 @@ namespace robot_dart {
std::vector<robot_t> _robots;
std::shared_ptr<gui::Base> _graphics;
std::unique_ptr<simu::GUIData> _gui_data;
std::shared_ptr<simu::TextData> _summary_text = nullptr;
std::shared_ptr<simu::TextData> _text_panel = nullptr;
std::shared_ptr<simu::TextData> _status_bar = nullptr;

Scheduler _scheduler;
int _physics_freq = -1, _control_freq = -1, _graphics_freq = 40;
};
Expand Down