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 |
|
|
|
33 |
|
|
TextManager *textManager = nullptr; |
34 |
|
|
|
35 |
|
6 |
TextManager::TextManager() : |
36 |
|
12 |
mTextList() |
37 |
|
|
{ |
38 |
|
6 |
} |
39 |
|
|
|
40 |
|
9 |
void TextManager::addText(Text *const text) |
41 |
|
|
{ |
42 |
✓✗ |
9 |
if (text == nullptr) |
43 |
|
|
return; |
44 |
|
9 |
place(text, nullptr, text->mX, text->mY, text->mHeight); |
45 |
|
9 |
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 |
|
9 |
void TextManager::removeText(const Text *const text) |
59 |
|
|
{ |
60 |
✓✗ |
27 |
FOR_EACH (TextList::iterator, ptr, mTextList) |
61 |
|
|
{ |
62 |
✓✗ |
9 |
if (*ptr == text) |
63 |
|
|
{ |
64 |
|
18 |
mTextList.erase(ptr); |
65 |
|
9 |
return; |
66 |
|
|
} |
67 |
|
|
} |
68 |
|
|
} |
69 |
|
|
|
70 |
|
12 |
TextManager::~TextManager() |
71 |
|
|
{ |
72 |
|
6 |
} |
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 |
|
9 |
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 |
✓✗ |
9 |
if (textObj == nullptr) |
90 |
|
9 |
return; |
91 |
|
9 |
const int xLeft = textObj->mX; |
92 |
|
9 |
const int xRight1 = xLeft + textObj->mWidth; |
93 |
|
9 |
const int TEST = 50; // Number of lines to test for text |
94 |
|
9 |
const int nBeings = 30; |
95 |
|
|
bool occupied[TEST]; // is some other text obscuring this line? |
96 |
|
9 |
std::memset(&occupied, 0, sizeof(occupied)); // set all to false |
97 |
|
9 |
const int wantedTop = (TEST - h) / 2; // Entry in occupied at top of text |
98 |
|
9 |
const int occupiedTop = y - wantedTop; // Line in map representing |
99 |
|
|
// to of occupied |
100 |
|
9 |
int cnt = 0; |
101 |
|
|
|
102 |
✓✓ |
35 |
for (TextList::const_iterator ptr = mTextList.begin(), |
103 |
|
18 |
pfEnd = mTextList.end(); |
104 |
✓✓✗✓
|
13 |
ptr != pfEnd && cnt < nBeings; |
105 |
|
|
++ptr, cnt ++) |
106 |
|
|
{ |
107 |
|
4 |
const Text *const text = *ptr; |
108 |
|
|
|
109 |
✓✗✓✗
|
4 |
if (text != omit && text->mX + 1 <= xRight1 |
110 |
✓✗ |
4 |
&& text->mX + text->mWidth > xLeft) |
111 |
|
|
{ |
112 |
|
4 |
int from = text->mY - occupiedTop; |
113 |
|
4 |
int to = from + text->mHeight - 1; |
114 |
✓✗ |
4 |
if (to < 0 || from >= TEST) // out of range considered |
115 |
|
|
continue; |
116 |
✗✓ |
4 |
if (from < 0) |
117 |
|
|
from = 0; |
118 |
✗✓ |
4 |
if (to >= TEST) |
119 |
|
|
to = TEST - 1; |
120 |
✓✓ |
80 |
for (int i = from; i <= to; ++i) |
121 |
|
76 |
occupied[i] = true; |
122 |
|
|
} |
123 |
|
|
} |
124 |
|
9 |
bool ok = true; |
125 |
|
9 |
const int sz = wantedTop + h; |
126 |
✓✓ |
123 |
for (int i = wantedTop; i < sz; i ++) |
127 |
|
|
{ |
128 |
✓✓ |
117 |
if (occupied[i]) |
129 |
|
|
{ |
130 |
|
|
ok = false; |
131 |
|
|
break; |
132 |
|
|
} |
133 |
|
|
} |
134 |
|
|
|
135 |
✓✓ |
9 |
if (ok) |
136 |
|
|
return; |
137 |
|
|
|
138 |
|
|
// Have to move it up or down, so find nearest spaces either side |
139 |
|
3 |
int consec = 0; |
140 |
|
3 |
int upSlot = -1; // means not found |
141 |
|
3 |
const int sz2 = wantedTop + h - 2; |
142 |
✓✓ |
102 |
for (int seek = sz2; seek >= 0; --seek) |
143 |
|
|
{ |
144 |
✓✓ |
99 |
if (occupied[seek]) |
145 |
|
|
{ |
146 |
|
|
consec = 0; |
147 |
|
|
} |
148 |
|
|
else |
149 |
|
|
{ |
150 |
✓✗ |
45 |
if (++consec == h) |
151 |
|
|
{ |
152 |
|
|
upSlot = seek; |
153 |
|
|
break; |
154 |
|
|
} |
155 |
|
|
} |
156 |
|
|
} |
157 |
|
3 |
int downSlot = -1; |
158 |
|
3 |
consec = 0; |
159 |
✓✓ |
105 |
for (int seek = wantedTop + 1; seek < TEST; ++seek) |
160 |
|
|
{ |
161 |
✓✓ |
102 |
if (occupied[seek]) |
162 |
|
|
{ |
163 |
|
|
consec = 0; |
164 |
|
|
} |
165 |
|
|
else |
166 |
|
|
{ |
167 |
✗✓ |
48 |
if (++consec == h) |
168 |
|
|
{ |
169 |
|
|
downSlot = seek - h + 1; |
170 |
|
|
break; |
171 |
|
|
} |
172 |
|
|
} |
173 |
|
|
} |
174 |
✗✓ |
3 |
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 |
|
|
} |