ManaPlus
Public Member Functions | Private Types | Private Member Functions | Private Attributes
TextManager Class Reference

#include <textmanager.h>

Public Member Functions

 TextManager ()
 
 ~TextManager ()
 
void addText (Text *const text)
 
void moveText (Text *const text, const int x, const int y) const
 
void removeText (const Text *const text)
 
void draw (Graphics *const graphics, const int xOff, const int yOff)
 

Private Types

typedef std::list< Text * > TextList
 

Private Member Functions

void place (const Text *const textObj, const Text *const omit, const int &x, int &y, const int h) const
 

Private Attributes

TextList mTextList
 

Detailed Description

Definition at line 33 of file textmanager.h.

Member Typedef Documentation

◆ TextList

typedef std::list<Text *> TextManager::TextList
private

The container type

Definition at line 77 of file textmanager.h.

Constructor & Destructor Documentation

◆ TextManager()

TextManager::TextManager ( )

Constructor

Definition at line 35 of file textmanager.cpp.

35  :
36  mTextList()
37 {
38 }
TextList mTextList
Definition: textmanager.h:78

◆ ~TextManager()

TextManager::~TextManager ( )

Destroy the manager

Definition at line 70 of file textmanager.cpp.

71 {
72 }

Member Function Documentation

◆ addText()

void TextManager::addText ( Text *const  text)

Add text to the manager

Definition at line 40 of file textmanager.cpp.

41 {
42  if (text == nullptr)
43  return;
44  place(text, nullptr, text->mX, text->mY, text->mHeight);
45  mTextList.push_back(text);
46 }
void place(const Text *const textObj, const Text *const omit, const int &x, int &y, const int h) const
Definition: textmanager.cpp:83
int mX
Definition: text.h:91
int mHeight
Definition: text.h:94
int mY
Definition: text.h:92

References Text::mHeight, mTextList, Text::mX, Text::mY, and place().

Referenced by Text::Text().

◆ draw()

void TextManager::draw ( Graphics *const  graphics,
const int  xOff,
const int  yOff 
)

Draw the text

Definition at line 74 of file textmanager.cpp.

76 {
77  BLOCK_START("TextManager::draw")
78  FOR_EACH (TextList::const_iterator, bPtr, mTextList)
79  (*bPtr)->draw(graphics, xOff, yOff);
81 }
std::list< Text * > TextList
Definition: textmanager.h:77
void draw(Graphics *const graphics, const int xOff, const int yOff)
Definition: textmanager.cpp:74
#define FOR_EACH(type, iter, array)
Definition: foreach.h:25
#define BLOCK_END(name)
Definition: perfomance.h:80
#define BLOCK_START(name)
Definition: perfomance.h:79

References BLOCK_END, BLOCK_START, FOR_EACH, and mTextList.

◆ moveText()

void TextManager::moveText ( Text *const  text,
const int  x,
const int  y 
) const

Move the text around the screen

Definition at line 48 of file textmanager.cpp.

50 {
51  if (text == nullptr)
52  return;
53  text->mX = x;
54  text->mY = y;
55  place(text, text, text->mX, text->mY, text->mHeight);
56 }

References Text::mHeight, Text::mX, Text::mY, place(), x, and y.

Referenced by Text::adviseXY().

◆ place()

void TextManager::place ( const Text *const  textObj,
const Text *const  omit,
const int &  x,
int &  y,
const int  h 
) const
private

Position the text so as to avoid conflict

Definition at line 83 of file textmanager.cpp.

88 {
89  if (textObj == nullptr)
90  return;
91  const int xLeft = textObj->mX;
92  const int xRight1 = xLeft + textObj->mWidth;
93  const int TEST = 50; // Number of lines to test for text
94  const int nBeings = 30;
95  bool occupied[TEST]; // is some other text obscuring this line?
96  std::memset(&occupied, 0, sizeof(occupied)); // set all to false
97  const int wantedTop = (TEST - h) / 2; // Entry in occupied at top of text
98  const int occupiedTop = y - wantedTop; // Line in map representing
99  // to of occupied
100  int cnt = 0;
101 
102  for (TextList::const_iterator ptr = mTextList.begin(),
103  pfEnd = mTextList.end();
104  ptr != pfEnd && cnt < nBeings;
105  ++ptr, cnt ++)
106  {
107  const Text *const text = *ptr;
108 
109  if (text != omit && text->mX + 1 <= xRight1
110  && text->mX + text->mWidth > xLeft)
111  {
112  int from = text->mY - occupiedTop;
113  int to = from + text->mHeight - 1;
114  if (to < 0 || from >= TEST) // out of range considered
115  continue;
116  if (from < 0)
117  from = 0;
118  if (to >= TEST)
119  to = TEST - 1;
120  for (int i = from; i <= to; ++i)
121  occupied[i] = true;
122  }
123  }
124  bool ok = true;
125  const int sz = wantedTop + h;
126  for (int i = wantedTop; i < sz; i ++)
127  {
128  if (occupied[i])
129  {
130  ok = false;
131  break;
132  }
133  }
134 
135  if (ok)
136  return;
137 
138  // Have to move it up or down, so find nearest spaces either side
139  int consec = 0;
140  int upSlot = -1; // means not found
141  const int sz2 = wantedTop + h - 2;
142  for (int seek = sz2; seek >= 0; --seek)
143  {
144  if (occupied[seek])
145  {
146  consec = 0;
147  }
148  else
149  {
150  if (++consec == h)
151  {
152  upSlot = seek;
153  break;
154  }
155  }
156  }
157  int downSlot = -1;
158  consec = 0;
159  for (int seek = wantedTop + 1; seek < TEST; ++seek)
160  {
161  if (occupied[seek])
162  {
163  consec = 0;
164  }
165  else
166  {
167  if (++consec == h)
168  {
169  downSlot = seek - h + 1;
170  break;
171  }
172  }
173  }
174  if (upSlot == -1 && downSlot == -1) // no good solution, so leave as is
175  {
176  return;
177  }
178  if (upSlot == -1) // must go down
179  {
180  y += downSlot - wantedTop;
181  return;
182  }
183  if (downSlot == -1) // must go up
184  {
185  y -= wantedTop - upSlot;
186  return;
187  }
188  if (wantedTop - upSlot > downSlot - wantedTop) // down is better
189  {
190  y += downSlot - wantedTop;
191  }
192  else
193  {
194  y -= wantedTop - upSlot;
195  }
196 }
Definition: text.h:40
int mWidth
Definition: text.h:93
bool ok(InputEvent &event)
Definition: actions.cpp:34
int seek(File *const file, const uint64_t pos)
Definition: fs.cpp:847

References Text::mHeight, mTextList, Text::mWidth, Text::mX, Text::mY, Actions::ok(), VirtFs::seek(), and y.

Referenced by addText(), and moveText().

◆ removeText()

void TextManager::removeText ( const Text *const  text)

Remove the text from the manager

Definition at line 58 of file textmanager.cpp.

59 {
60  FOR_EACH (TextList::iterator, ptr, mTextList)
61  {
62  if (*ptr == text)
63  {
64  mTextList.erase(ptr);
65  return;
66  }
67  }
68 }

References FOR_EACH, and mTextList.

Referenced by Text::~Text().

Field Documentation

◆ mTextList

TextList TextManager::mTextList
private

The container

Definition at line 78 of file textmanager.h.

Referenced by addText(), draw(), place(), and removeText().


The documentation for this class was generated from the following files: