RigsofRods
Soft-body Physics Simulation
ColoredTextAreaOverlayElement.cpp
Go to the documentation of this file.
1 /*
2  This source file is part of Rigs of Rods
3  Copyright 2005-2012 Pierre-Michel Ricordel
4  Copyright 2007-2012 Thomas Fischer
5 
6  For more information, see http://www.rigsofrods.org/
7 
8  Rigs of Rods is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License version 3, as
10  published by the Free Software Foundation.
11 
12  Rigs of Rods is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
22 
23 #include <OgreRoot.h>
24 
25 #define POS_TEX_BINDING 0
26 #define COLOUR_BINDING 1
27 
28 using namespace Ogre;
29 using namespace std;
30 
32  : TextAreaOverlayElement(name)
33  , m_ValueTop(1.0f)
34  , m_ValueBottom(0.8f)
35 {
36 }
37 
39 {
40 }
41 
43 {
44  m_ValueTop = Value;
45  mColoursChanged = true;
46 }
47 
49 {
50  m_ValueBottom = Value;
51  mColoursChanged = true;
52 }
53 
54 ColourValue ColoredTextAreaOverlayElement::GetColor(unsigned char ID, float Value)
55 {
56  switch (ID)
57  {
58  case 0:
59  return ColourValue(0, 0, 0); // Black
60  case 1:
61  return ColourValue(Value, 0, 0); // Red
62  case 2:
63  return ColourValue(0, Value, 0); // Green
64  case 3:
65  return ColourValue(Value, Value, 0); // Yellow
66  case 4:
67  return ColourValue(0, 0, Value); // Blue
68  case 5:
69  return ColourValue(0, Value, Value); // Cyan
70  case 6:
71  return ColourValue(Value, 0, Value); // Magenta
72  case 7:
73  return ColourValue(Value, Value, Value); // White
74  case 8:
75  return ColourValue(Value * 0.9, Value * 0.9, Value * 0.9); // Gray
76  case 9:
77  return ColourValue(0.5, 0.5, Value * 0.9); // dark blue
78  }
79  return ColourValue::Black;
80 }
81 
82 DisplayString ColoredTextAreaOverlayElement::StripColors(const Ogre::String& otext)
83 {
84  try
85  {
86  DisplayString text = DisplayString(otext.c_str());
87  DisplayString StrippedText;
88  int i;
89  for (i = 0; i < (int)text.size() - 1; ++i)
90  {
91  if (text[i] == '^' &&
92  text[i + 1] >= '0' && text[i + 1] <= '9') // This is a color code, ignore it
93  {
94  ++i;
95  }
96  else
97  {
98  StrippedText.append(1, text[i]);
99  }
100  }
101  // One last character to add because loop went to size()-1
102  if (i < (int)text.size())
103  StrippedText.append(1, text[i]);
104  return StrippedText;
105  }
106  catch (...)
107  {
108  }
109  return String("UTF8 error (String cannot be displayed with current font set)");
110 }
111 
112 void ColoredTextAreaOverlayElement::setCaption(const DisplayString& text)
113 {
114  m_Colors.clear();
115  m_Colors.resize(text.size(), 9);
116  bool noColor = true;
117  int i, iNumColorCodes = 0, iNumSpaces = 0;
118  for (i = 0; i < (int)text.size() - 1; ++i)
119  {
120  if (text[i] == ' ' || text[i] == '\n')
121  {
122  // Spaces and newlines are skipped when rendering and as such can't have a color
123  ++iNumSpaces;
124  }
125  else if (text[i] == '^' &&
126  text[i + 1] >= '0' && text[i + 1] <= '9') // This is a color code
127  {
128  // Fill the color array starting from this point to the end with the new color code
129  // adjustments need to made because color codes will be removed and spaces are not counted
130  fill(m_Colors.begin() + i - (2 * iNumColorCodes) - iNumSpaces, m_Colors.end(), text[i + 1] - '0');
131  ++i;
132  ++iNumColorCodes;
133  mColoursChanged = true;
134  noColor = false;
135  }
136  }
137  if (noColor)
138  mColoursChanged = true;
139  // Set the caption using the base class, but strip the color codes from it first
140  TextAreaOverlayElement::setCaption(StripColors(text));
141 }
142 
144 {
145  if (!mRenderOp.vertexData)
146  return;
147  // Convert to system-specific
148  RGBA topColour, bottomColour;
149  // Set default to white
150  Root::getSingleton().convertColourValue(ColourValue::White, &topColour);
151  Root::getSingleton().convertColourValue(ColourValue::White, &bottomColour);
152 
153  HardwareVertexBufferSharedPtr vbuf =
154  mRenderOp.vertexData->vertexBufferBinding->getBuffer(COLOUR_BINDING);
155 
156  //RGBA* pDest = static_cast<RGBA*>(
157  // vbuf->lock(HardwareBuffer::HBL_NORMAL) );
158  RGBA* pDest = (RGBA*)malloc(vbuf->getSizeInBytes());
159  RGBA* oDest = pDest;
160 
161  for (size_t i = 0; i < mAllocSize; ++i)
162  {
163  if (i < m_Colors.size())
164  {
165  Root::getSingleton().convertColourValue(GetColor(m_Colors[i], m_ValueTop), &topColour);
166  Root::getSingleton().convertColourValue(GetColor(m_Colors[i], m_ValueBottom), &bottomColour);
167  }
168 
169  // First tri (top, bottom, top)
170  *pDest++ = topColour;
171  *pDest++ = bottomColour;
172  *pDest++ = topColour;
173  // Second tri (top, bottom, bottom)
174  *pDest++ = topColour;
175  *pDest++ = bottomColour;
176  *pDest++ = bottomColour;
177  }
178  vbuf->writeData(0, vbuf->getSizeInBytes(), oDest, true);
179  free(oDest);
180  //vbuf->unlock();
181 }
ColoredTextAreaOverlayElement::~ColoredTextAreaOverlayElement
~ColoredTextAreaOverlayElement(void)
Definition: ColoredTextAreaOverlayElement.cpp:38
ColoredTextAreaOverlayElement::setCaption
void setCaption(const Ogre::DisplayString &text)
Definition: ColoredTextAreaOverlayElement.cpp:112
ColoredTextAreaOverlayElement::m_ValueBottom
float m_ValueBottom
Definition: ColoredTextAreaOverlayElement.h:45
ColoredTextAreaOverlayElement::ColoredTextAreaOverlayElement
ColoredTextAreaOverlayElement(const Ogre::String &name)
Definition: ColoredTextAreaOverlayElement.cpp:31
COLOUR_BINDING
#define COLOUR_BINDING
Definition: ColoredTextAreaOverlayElement.cpp:26
ColoredTextAreaOverlayElement::m_ValueTop
float m_ValueTop
Definition: ColoredTextAreaOverlayElement.h:44
ColoredTextAreaOverlayElement::m_Colors
std::vector< unsigned char > m_Colors
Definition: ColoredTextAreaOverlayElement.h:43
ColoredTextAreaOverlayElement::StripColors
static Ogre::DisplayString StripColors(const Ogre::String &text)
Definition: ColoredTextAreaOverlayElement.cpp:82
ColoredTextAreaOverlayElement::setValueBottom
void setValueBottom(float Value)
Definition: ColoredTextAreaOverlayElement.cpp:42
ColoredTextAreaOverlayElement::GetColor
static Ogre::ColourValue GetColor(unsigned char ID, float Value=1.0f)
Definition: ColoredTextAreaOverlayElement.cpp:54
ColoredTextAreaOverlayElement::updateColours
void updateColours(void)
Definition: ColoredTextAreaOverlayElement.cpp:143
Ogre
Definition: ExtinguishableFireAffector.cpp:35
ColoredTextAreaOverlayElement.h
ColoredTextAreaOverlayElement::setValueTop
void setValueTop(float Value)
Definition: ColoredTextAreaOverlayElement.cpp:48