| 1 |
/* +------------------------------------------------------------------------+ |
| 2 |
| Mobile Robot Programming Toolkit (MRPT) | |
| 3 |
| https://www.mrpt.org/ | |
| 4 |
| | |
| 5 |
| Copyright (c) 2005-2026, Individual contributors, see AUTHORS file | |
| 6 |
| See: https://www.mrpt.org/Authors - All rights reserved. | |
| 7 |
| Released under BSD License. See: https://www.mrpt.org/License | |
| 8 |
+------------------------------------------------------------------------+ */ |
| 9 |
|
| 10 |
#include "opengl-precomp.h" // Precompiled header |
| 11 |
// |
| 12 |
#include <mrpt/opengl/CTextMessageCapable.h> |
| 13 |
#include <mrpt/opengl/opengl_api.h> |
| 14 |
|
| 15 |
using namespace std; |
| 16 |
using namespace mrpt; |
| 17 |
using namespace mrpt::opengl; |
| 18 |
|
| 19 |
void CTextMessageCapable::TListTextMessages::regenerateGLobjects() const |
| 20 |
{ |
| 21 |
std::unique_lock<std::shared_mutex> lckWrite2DTexts(mtx.data); |
| 22 |
|
| 23 |
// (re)generate the opengl CText objects for each label: |
| 24 |
for (auto& kv : messages) |
| 25 |
{ |
| 26 |
const DataPerText& labelData = kv.second; |
| 27 |
if (labelData.gl_text && labelData.gl_text_outdated) continue; |
| 28 |
|
| 29 |
if (!labelData.gl_text) |
| 30 |
{ |
| 31 |
labelData.gl_text = mrpt::opengl::CText::Create(); |
| 32 |
} |
| 33 |
if (labelData.draw_shadow && !labelData.gl_text_shadow) |
| 34 |
labelData.gl_text_shadow = mrpt::opengl::CText::Create(); |
| 35 |
|
| 36 |
if (!labelData.draw_shadow && labelData.gl_text_shadow) labelData.gl_text_shadow.reset(); |
| 37 |
|
| 38 |
kv.second.gl_text_outdated = false; |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
void CTextMessageCapable::clearTextMessages() |
| 43 |
{ |
| 44 |
std::unique_lock<std::shared_mutex> lckWrite2DTexts(m_2D_texts.mtx.data); |
| 45 |
m_2D_texts.messages.clear(); |
| 46 |
} |
| 47 |
|
| 48 |
/** Just updates the text of a given text message, without touching the other |
| 49 |
* parameters. |
| 50 |
* \return false if given ID doesn't exist. |
| 51 |
*/ |
| 52 |
bool CTextMessageCapable::updateTextMessage(size_t unique_index, const std::string& text) |
| 53 |
{ |
| 54 |
std::unique_lock<std::shared_mutex> lckWrite2DTexts(m_2D_texts.mtx.data); |
| 55 |
|
| 56 |
auto it = m_2D_texts.messages.find(unique_index); |
| 57 |
if (it == m_2D_texts.messages.end()) |
| 58 |
return false; |
| 59 |
else |
| 60 |
{ |
| 61 |
it->second.text = text; |
| 62 |
it->second.gl_text_outdated = true; |
| 63 |
return true; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
/// overload with more font parameters - refer to |
| 68 |
/// mrpt::opengl::gl_utils::glDrawText() |
| 69 |
void CTextMessageCapable::addTextMessage( |
| 70 |
const double x_frac, |
| 71 |
const double y_frac, |
| 72 |
const std::string& text, |
| 73 |
size_t unique_index, |
| 74 |
const TFontParams& fontParams) |
| 75 |
{ |
| 76 |
DataPerText d; |
| 77 |
static_cast<TFontParams&>(d) = fontParams; |
| 78 |
d.text = text; |
| 79 |
d.x = x_frac; |
| 80 |
d.y = y_frac; |
| 81 |
|
| 82 |
std::unique_lock<std::shared_mutex> lckWrite2DTexts(m_2D_texts.mtx.data); |
| 83 |
m_2D_texts.messages[unique_index] = std::move(d); |
| 84 |
} |