ManaPlus
textmanager.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2008 Douglas Boffey <[email protected]>
4  * Copyright (C) 2011-2019 The ManaPlus Developers
5  * Copyright (C) 2019-2021 Andrei Karas
6  *
7  * This file is part of The ManaPlus Client.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #include "textmanager.h"
24 
25 #include "text.h"
26 
27 #include "utils/foreach.h"
28 
29 #include <cstring>
30 
31 #include "debug.h"
32 
34 
36  mTextList()
37 {
38 }
39 
40 void TextManager::addText(Text *const text)
41 {
42  if (text == nullptr)
43  return;
44  place(text, nullptr, text->mX, text->mY, text->mHeight);
45  mTextList.push_back(text);
46 }
47 
48 void TextManager::moveText(Text *const text,
49  const int x, const int y) const
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 }
57 
58 void TextManager::removeText(const Text *const text)
59 {
60  FOR_EACH (TextList::iterator, ptr, mTextList)
61  {
62  if (*ptr == text)
63  {
64  mTextList.erase(ptr);
65  return;
66  }
67  }
68 }
69 
71 {
72 }
73 
74 void TextManager::draw(Graphics *const graphics,
75  const int xOff, const int yOff)
76 {
77  BLOCK_START("TextManager::draw")
78  FOR_EACH (TextList::const_iterator, bPtr, mTextList)
79  (*bPtr)->draw(graphics, xOff, yOff);
80  BLOCK_END("TextManager::draw")
81 }
82 
83 void TextManager::place(const Text *const textObj,
84  const Text *const omit,
85  const int &x A_UNUSED,
86  int &y,
87  const int h) const
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 }
void removeText(const Text *const text)
Definition: textmanager.cpp:58
void moveText(Text *const text, const int x, const int y) const
Definition: textmanager.cpp:48
TextList mTextList
Definition: textmanager.h:78
void addText(Text *const text)
Definition: textmanager.cpp:40
void place(const Text *const textObj, const Text *const omit, const int &x, int &y, const int h) const
Definition: textmanager.cpp:83
void draw(Graphics *const graphics, const int xOff, const int yOff)
Definition: textmanager.cpp:74
Definition: text.h:40
int mX
Definition: text.h:91
int mWidth
Definition: text.h:93
int mHeight
Definition: text.h:94
int mY
Definition: text.h:92
#define FOR_EACH(type, iter, array)
Definition: foreach.h:25
#define A_UNUSED
Definition: localconsts.h:160
bool ok(InputEvent &event)
Definition: actions.cpp:34
int seek(File *const file, const uint64_t pos)
Definition: fs.cpp:847
#define BLOCK_END(name)
Definition: perfomance.h:80
#define BLOCK_START(name)
Definition: perfomance.h:79
TextManager * textManager
Definition: textmanager.cpp:33