iMSTK
Interactive Medical Simulation Toolkit
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
imstkVTKTextRenderDelegate.cpp
1 /*
2 ** This file is part of the Interactive Medical Simulation Toolkit (iMSTK)
3 ** iMSTK is distributed under the Apache License, Version 2.0.
4 ** See accompanying NOTICE for details.
5 */
6 
7 #include <vtkTextActor.h>
8 #include <vtkTextProperty.h>
9 #include <vtkTextMapper.h>
10 
11 #include "imstkVTKTextRenderDelegate.h"
12 #include "imstkTextVisualModel.h"
13 
14 namespace imstk
15 {
16 void
17 VTKTextRenderDelegate::init()
18 {
19  auto textVisualModel = std::dynamic_pointer_cast<TextVisualModel>(m_visualModel);
20 
21  m_textMapper = vtkSmartPointer<vtkTextMapper>::New();
22  m_textMapper->SetInput(textVisualModel->getText().c_str());
23 
24  // Pull properties from textVisualModel
25  vtkTextProperty* txtprop = m_textMapper->GetTextProperty();
26  txtprop->SetFontSize(textVisualModel->getFontSize());
27  txtprop->SetFontFamilyToArial();
28  txtprop->SetBackgroundColor(180, 180, 180);
29  txtprop->SetBackgroundOpacity(0.75);
30 
31  const Color& fontColor = textVisualModel->getTextColor();
32  txtprop->SetColor(fontColor.r, fontColor.g, fontColor.b);
33  txtprop->SetJustificationToCentered();
34  txtprop->SetVerticalJustificationToCentered();
35 
36  // Setup Text Actor
37  m_textActor = vtkSmartPointer<vtkActor2D>::New();
38  m_textActor->SetMapper(m_textMapper);
39  m_textActor->GetPositionCoordinate()->SetCoordinateSystemToNormalizedDisplay();
40  m_textActor->GetPositionCoordinate()->SetValue(0.5, 0.5);
41 
42  // Set text position
43  TextVisualModel::DisplayPosition position = textVisualModel->getPosition();
44  switch (position)
45  {
46  case TextVisualModel::DisplayPosition::CenterCenter:
47  // Use the default value
48  break;
49 
50  case TextVisualModel::DisplayPosition::UpperLeft:
51  txtprop->SetJustificationToLeft();
52  txtprop->SetVerticalJustificationToTop();
53  m_textActor->GetPositionCoordinate()->SetValue(0.01, 0.95);
54  break;
55 
56  case TextVisualModel::DisplayPosition::UpperRight:
57  txtprop->SetJustificationToRight();
58  txtprop->SetVerticalJustificationToTop();
59  m_textActor->GetPositionCoordinate()->SetValue(0.95, 0.95);
60  break;
61 
62  case TextVisualModel::DisplayPosition::LowerRight:
63  txtprop->SetJustificationToRight();
64  txtprop->SetVerticalJustificationToBottom();
65  m_textActor->GetPositionCoordinate()->SetValue(0.95, 0.05);
66  break;
67 
68  case TextVisualModel::DisplayPosition::LowerLeft:
69  txtprop->SetJustificationToLeft();
70  txtprop->SetVerticalJustificationToBottom();
71  m_textActor->GetPositionCoordinate()->SetValue(0.05, 0.05);
72  break;
73  }
74 
75  m_mapper = m_textMapper;
76  m_actor = m_textActor;
77 
78  processEvents();
79 }
80 
81 void
83 {
84  // Update text if changed
85  auto textVisualModel = std::dynamic_pointer_cast<TextVisualModel>(m_visualModel);
86 
87  if (textVisualModel->getText() != std::string(m_textMapper->GetInput()))
88  {
89  m_textMapper->SetInput(textVisualModel->getText().c_str());
90  }
91 
92  m_textActor->SetVisibility(textVisualModel->getVisibility());
93 }
94 
95 void
97 {
98 }
99 } // namespace imstk
void updateRenderProperties() override
Updates the actor and mapper properties from the currently set VisualModel.
std::shared_ptr< VisualModel > m_visualModel
imstk visual model (contains data (geometry) and render specification (render material)) ...
Compound Geometry.
void processEvents() override
Plot text and track updates to TextVisualModel.
Renders text to the screen.