Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "overlay_text_display.h"
00037 #include <OGRE/OgreMaterialManager.h>
00038 #include <rviz/uniform_string_stream.h>
00039 #include <OGRE/OgreTexture.h>
00040 #include <OGRE/OgreHardwarePixelBuffer.h>
00041 #include <QPainter>
00042
00043 namespace jsk_rviz_plugin
00044 {
00045 OverlayTextDisplay::OverlayTextDisplay() : Display(),
00046 texture_width_(0), texture_height_(0),
00047 text_size_(14),
00048 line_width_(2),
00049 text_(""), font_(""),
00050 bg_color_(0, 0, 0, 0),
00051 fg_color_(255, 255, 255, 255.0),
00052 require_update_texture_(false)
00053 {
00054 update_topic_property_ = new rviz::RosTopicProperty(
00055 "Topic", "",
00056 ros::message_traits::datatype<jsk_rviz_plugins::OverlayText>(),
00057 "jsk_rviz_plugins::OverlayText topic to subscribe to.",
00058 this, SLOT( updateTopic() ));
00059 }
00060
00061
00062 OverlayTextDisplay::~OverlayTextDisplay()
00063 {
00064 onDisable();
00065
00066 delete update_topic_property_;
00067 }
00068
00069 void OverlayTextDisplay::onEnable()
00070 {
00071 if (overlay_) {
00072 overlay_->show();
00073 }
00074 subscribe();
00075 }
00076
00077 void OverlayTextDisplay::onDisable()
00078 {
00079 if (overlay_) {
00080 overlay_->hide();
00081 }
00082 unsubscribe();
00083 }
00084
00085 void OverlayTextDisplay::unsubscribe()
00086 {
00087 sub_.shutdown();
00088 }
00089
00090 void OverlayTextDisplay::subscribe()
00091 {
00092 std::string topic_name = update_topic_property_->getTopicStd();
00093 if (topic_name.length() > 0 && topic_name != "/") {
00094 sub_ = ros::NodeHandle().subscribe(topic_name, 1, &OverlayTextDisplay::processMessage, this);
00095 }
00096 }
00097
00098 void OverlayTextDisplay::updateTopic()
00099 {
00100 unsubscribe();
00101 subscribe();
00102 }
00103
00104
00105 void OverlayTextDisplay::onInitialize()
00106 {
00107 onEnable();
00108 updateTopic();
00109 require_update_texture_ = true;
00110 }
00111
00112 void OverlayTextDisplay::update(float wall_dt, float ros_dt)
00113 {
00114 if (!require_update_texture_) {
00115 return;
00116 }
00117 if (!isEnabled()) {
00118 return;
00119 }
00120 if (!overlay_) {
00121 return;
00122 }
00123 overlay_->updateTextureSize(texture_width_, texture_height_);
00124 {
00125 ScopedPixelBuffer buffer = overlay_->getBuffer();
00126 QImage Hud = buffer.getQImage(*overlay_, bg_color_);
00127 QPainter painter( &Hud );
00128 painter.setRenderHint(QPainter::Antialiasing, true);
00129 painter.setPen(QPen(fg_color_, line_width_ || 1, Qt::SolidLine));
00130 uint16_t w = overlay_->getTextureWidth();
00131 uint16_t h = overlay_->getTextureHeight();
00132
00133
00134 if (text_size_ != 0) {
00135
00136 QFont font(font_.length() > 0 ? font_.c_str(): "Arial");
00137 font.setPointSize(text_size_);
00138 font.setBold(true);
00139 painter.setFont(font);
00140 }
00141 if (text_.length() > 0) {
00142
00143 painter.drawText(0, 0, w, h,
00144 Qt::TextWordWrap | Qt::AlignLeft | Qt::AlignTop,
00145 text_.c_str());
00146 }
00147 painter.end();
00148 }
00149 overlay_->setDimensions(overlay_->getTextureWidth(), overlay_->getTextureHeight());
00150 require_update_texture_ = false;
00151 }
00152
00153 void OverlayTextDisplay::processMessage
00154 (const jsk_rviz_plugins::OverlayText::ConstPtr& msg)
00155 {
00156 if (!isEnabled()) {
00157 return;
00158 }
00159 if (!overlay_) {
00160 static int count = 0;
00161 rviz::UniformStringStream ss;
00162 ss << "OverlayTextDisplayObject" << count++;
00163 overlay_.reset(new OverlayObject(ss.str()));
00164 overlay_->show();
00165 }
00166 if (overlay_) {
00167 if (msg->action == jsk_rviz_plugins::OverlayText::DELETE) {
00168 overlay_->hide();
00169 }
00170 else if (msg->action == jsk_rviz_plugins::OverlayText::ADD) {
00171 overlay_->show();
00172 }
00173 }
00174 texture_width_ = msg->width;
00175 texture_height_ = msg->height;
00176 if (overlay_) {
00177 overlay_->setPosition(msg->left, msg->top);
00178 }
00179
00180 text_ = msg->text;
00181 font_ = msg->font;
00182 bg_color_ = QColor(msg->bg_color.r * 255.0,
00183 msg->bg_color.g * 255.0,
00184 msg->bg_color.b * 255.0,
00185 msg->bg_color.a * 255.0);
00186 fg_color_ = QColor(msg->fg_color.r * 255.0,
00187 msg->fg_color.g * 255.0,
00188 msg->fg_color.b * 255.0,
00189 msg->fg_color.a * 255.0);
00190 text_size_ = msg->text_size;
00191 line_width_ = msg->line_width;
00192 require_update_texture_ = true;
00193 }
00194
00195 }
00196
00197 #include <pluginlib/class_list_macros.h>
00198 PLUGINLIB_EXPORT_CLASS( jsk_rviz_plugin::OverlayTextDisplay, rviz::Display )