-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer_text.cpp
91 lines (73 loc) · 2.95 KB
/
buffer_text.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include "glviewer.h"
#include "buffer_text.h"
using namespace std;
using namespace MaxLib;
namespace GLViewer {
// Global counter for buffer
static uint m_Counter_TextID = 0;
// Text at 3D (World) Position, text is aligned centre by default (0, 0 if top left)
RenderableText::RenderableText(Viewer* viewer, const std::string& text, const glm::vec3& pos3d, const glm::vec4& colour, const glm::vec2& textAlign)
: m_Label(text), m_Colour(colour)
{
// Get 2d screen position of 3d point (pair<bool, glm::vec2>)
if(std::optional<glm::vec2> pos2d = viewer->WorldToScreenCoords(pos3d)) {
ImVec2 textSize = ImGui::CalcTextSize(m_Label.c_str());
// Align the text
m_Position = *pos2d - textAlign * glm::vec2(textSize.x, textSize.y);
m_Valid = true;
}
}
// Text at 2D (Window) Position, text is aligned centre by default
RenderableText::RenderableText(Viewer* viewer, const std::string& text, const glm::vec2& pos2d, const glm::vec4& colour, const glm::vec2& textAlign)
: m_Label(text), m_Colour(colour), m_Valid(true)
{
(void)viewer;
// Send text to imgui draw list (first align the text)
ImVec2 textSize = ImGui::CalcTextSize(m_Label.c_str());
m_Position = pos2d - textAlign * glm::vec2(textSize.x, textSize.y);
}
// TODO: Viewer in render only
void RenderableText::Render()
{
if(!m_Valid) { return; }
// convert colour
ImU32 colour = ImGui::GetColorU32(ImVec4(m_Colour.r, m_Colour.g, m_Colour.b, m_Colour.a));
// Send text to imgui draw list
ImGui::GetBackgroundDrawList()->AddText(ImVec2(m_Position.x, m_Position.y), colour, m_Label.c_str());
}
TextBuffer::TextBuffer(Viewer* viewer)
: m_Parent(viewer), m_ID(++m_Counter_TextID)
{}
void TextBuffer::Add2DText(const std::string& label, const glm::vec2& position, const glm::vec4& colour, const glm::vec2& textAlign)
{
m_Texts.emplace_back(m_Parent, label, position, colour, textAlign);
}
void TextBuffer::Add3DText(const std::string& label, const glm::vec3& position, const glm::vec4& colour, const glm::vec2& textAlign)
{
m_Texts.emplace_back(m_Parent, label, position, colour, textAlign);
}
void TextBuffer::Add3DAxisLabels(const Transform& transform, const glm::vec4& colour, const glm::vec2& textAlign)
{
Add3DText("X", transform.TransformVertex(glm::vec3(1.0f, 0.0f, 0.0f)), colour, textAlign);
Add3DText("Y", transform.TransformVertex(glm::vec3(0.0f, 1.0f, 0.0f)), colour, textAlign);
Add3DText("Z", transform.TransformVertex(glm::vec3(0.0f, 0.0f, 1.0f)), colour, textAlign);
}
void TextBuffer::ClearAll()
{
m_Texts.erase(m_Texts.begin(), m_Texts.end());
}
void TextBuffer::SetData(std::function<void(TextBuffer&)> cb_AddData)
{ // Clear old data
ClearAll();
// Add vertices via callback
cb_AddData(*this);
}
void TextBuffer::Render()
{
// add each text to imgui draw list
for(RenderableText& text : m_Texts) {
text.Render();
}
}
} // End namespace GLViewer