Skip to content

Commit

Permalink
Fix text issues
Browse files Browse the repository at this point in the history
  • Loading branch information
costashatz committed Apr 26, 2024
1 parent 10e41fe commit f60c49b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
17 changes: 10 additions & 7 deletions src/robot_dart/gui/magnum/gs/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,26 +327,29 @@ namespace robot_dart {
auto viewport = Magnum::Vector2{_camera->viewport()};
auto sc = Magnum::Vector2{viewport.max() / 1024.f};
auto text_scaling = Magnum::Matrix3::scaling(sc);
auto extra_tr = Magnum::Matrix3(Magnum::Math::IdentityInit);
if ((text->alignment & Magnum::Text::Implementation::AlignmentVertical) == Magnum::Text::Implementation::AlignmentLine) // if line (bottom) alignment, push the text a bit above
extra_tr = Magnum::Matrix3::translation({0.f, sc[1] * 0.25f * rectangle.sizeY()});

auto text_tr = Magnum::Matrix3(Magnum::Matrix3d(text->transformation));

if (text->draw_background) {
auto bg_scaling = Magnum::Matrix3::scaling(Magnum::Vector2{viewport[0], rectangle.sizeY() * sc[1]});
auto extra_tr = Magnum::Matrix3(Magnum::Math::IdentityInit);
if ((text->alignment & Magnum::Text::Implementation::AlignmentVertical) == Magnum::Text::Implementation::AlignmentBottom) // if bottom alignment, push the bg a bit above
extra_tr = Magnum::Matrix3::translation({0.f, sc[1] * 0.5f * rectangle.sizeY()});
if ((text->alignment & Magnum::Text::Implementation::AlignmentVertical) == Magnum::Text::Implementation::AlignmentTop) // if top alignment, push the bg a bit down
extra_tr = Magnum::Matrix3::translation({0.f, -sc[1] * 0.5f * rectangle.sizeY()});

auto bg_tr = text_tr * extra_tr; // * Magnum::Matrix3::translation({0.f, rectangle.sizeY() * sc[1] / 2.f});
auto bg_scaling = Magnum::Matrix3::scaling(Magnum::Vector2{viewport[0], rectangle.sizeY() * sc[1] / 2.f});

// draw the background
(*debug_data.background_shader)
.setTransformationProjectionMatrix(Magnum::Matrix3::projection(viewport) * text_tr * bg_scaling)
.setTransformationProjectionMatrix(Magnum::Matrix3::projection(viewport) * bg_tr * bg_scaling)
.setColor(Magnum::Vector4(Magnum::Vector4d(text->background_color)))
.draw(*debug_data.background_mesh);
}

(*debug_data.text_shader)
.bindVectorTexture(debug_data.cache->texture())
.setTransformationProjectionMatrix(Magnum::Matrix3::projection(viewport) * text_tr * extra_tr * text_scaling)
// .setTransformationProjectionMatrix(Magnum::Matrix3::projection(Magnum::Vector2{_camera->viewport()}) * Magnum::Matrix3::translation(Magnum::Vector2{-text_renderer->rectangle().sizeX() / 2.f, -text_renderer->rectangle().sizeY() / 2.f}) * Magnum::Matrix3(Magnum::Matrix3d(text.transformation)))
.setTransformationProjectionMatrix(Magnum::Matrix3::projection(viewport) * text_tr * text_scaling)
.setColor(Magnum::Vector4(Magnum::Vector4d(text->color)))
.setOutlineRange(0.4f, 0.45f)
.setSmoothness(0.075f)
Expand Down
2 changes: 1 addition & 1 deletion src/robot_dart/gui_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace robot_dart {
std::vector<std::shared_ptr<simu::TextData>> text_drawings;

public:
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), std::uint8_t alignment = (1 | 3 << 3), bool draw_bg = false, Eigen::Vector4d bg_color = Eigen::Vector4d(0, 0, 0, 0.75), double font_size = 28)
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), std::uint8_t alignment = 2 << 2, bool draw_bg = false, Eigen::Vector4d bg_color = Eigen::Vector4d(0, 0, 0, 0.75), double font_size = 28)
{
text_drawings.emplace_back(new TextData{text, tf, color, alignment, draw_bg, bg_color, font_size});

Expand Down
16 changes: 13 additions & 3 deletions src/robot_dart/robot_dart_simu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,14 @@ namespace robot_dart {
if (_text_panel) { // Need to re-transform as the size of the window might have changed
Eigen::Affine2d tf = Eigen::Affine2d::Identity();
tf.translate(Eigen::Vector2d(-static_cast<double>(_graphics->width()) / 2., _graphics->height() / 2.));
// tf.translate(Eigen::Vector2d(-static_cast<double>(_graphics->width()) / 2., 0.));
_text_panel->transformation = tf;
}
if (_status_bar) {
_status_bar->text = status_bar_text(); // this is dynamic text (timings)
Eigen::Affine2d tf = Eigen::Affine2d::Identity();
tf.translate(Eigen::Vector2d(-static_cast<double>(_graphics->width()) / 2., -static_cast<double>(_graphics->height() / 2.)));
// tf.translate(Eigen::Vector2d(-static_cast<double>(_graphics->width()) / 2., 0.));
_status_bar->transformation = tf;
}

Expand Down Expand Up @@ -385,13 +387,21 @@ namespace robot_dart {

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

void RobotDARTSimu::enable_text_panel(bool enable, double font_size) { _enable(_text_panel, enable, font_size); }
void RobotDARTSimu::enable_text_panel(bool enable, double font_size)
{
_enable(_text_panel, enable, font_size);
if (enable) {
_text_panel->alignment = 3 << 2; // alignment of status bar should be LineTop; TO-DO: Check how to get types automatically from Magnum?
// _text_panel->draw_background = true; // we want to draw a background
// _text_panel->background_color = Eigen::Vector4d(0, 0, 0, 0.75); // black-transparent bar
}
}

void RobotDARTSimu::enable_status_bar(bool enable, double font_size)
{
_enable(_status_bar, enable, font_size);
if (enable) {
_status_bar->alignment = (1 | 1 << 3); // alignment of status bar should be LineLeft
_status_bar->alignment = 1 << 2; // alignment of status bar should be LineBottom; TO-DO: Check how to get types automatically from Magnum?
_status_bar->draw_background = true; // we want to draw a background
_status_bar->background_color = Eigen::Vector4d(0, 0, 0, 0.75); // black-transparent bar
}
Expand All @@ -400,7 +410,7 @@ namespace robot_dart {
void RobotDARTSimu::_enable(std::shared_ptr<simu::TextData>& text, bool enable, double font_size)
{
if (!text && enable) {
text = _gui_data->add_text("");
text = add_text("");
}
else if (!enable) {
if (text)
Expand Down
2 changes: 1 addition & 1 deletion src/robot_dart/robot_dart_simu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ namespace robot_dart {
void enable_status_bar(bool enable = true, double font_size = -1);
std::string status_bar_text() const;

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), std::uint8_t alignment = (1 | 3 << 3), bool draw_bg = false, Eigen::Vector4d bg_color = Eigen::Vector4d(0, 0, 0, 0.75), double font_size = 28);
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), std::uint8_t alignment = 2 << 2, bool draw_bg = false, Eigen::Vector4d bg_color = Eigen::Vector4d(0, 0, 0, 0.75), double font_size = 28);

std::shared_ptr<Robot> add_floor(double floor_width = 10.0, double floor_height = 0.1, const Eigen::Isometry3d& tf = Eigen::Isometry3d::Identity(), const std::string& floor_name = "floor");
std::shared_ptr<Robot> add_checkerboard_floor(double floor_width = 10.0, double floor_height = 0.1, double size = 1., const Eigen::Isometry3d& tf = Eigen::Isometry3d::Identity(), const std::string& floor_name = "checkerboard_floor", const Eigen::Vector4d& first_color = dart::Color::White(1.), const Eigen::Vector4d& second_color = dart::Color::Gray(1.));
Expand Down

0 comments on commit f60c49b

Please sign in to comment.