Mana
Loading...
Searching...
No Matches
textmanager.cpp
Go to the documentation of this file.
1/*
2 * Support for non-overlapping floating text
3 * Copyright (C) 2008 Douglas Boffey <DougABoffey@netscape.net>
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include "textmanager.h"
23
24#include "text.h"
25
26#include <cstring>
27
29
33
35{
36 place(text, nullptr, text->mX, text->mY, text->mHeight);
37 mTextList.push_back(text);
38}
39
40void TextManager::moveText(Text *text, int x, int y)
41{
42 text->mX = x;
43 text->mY = y;
44 place(text, text, text->mX, text->mY, text->mHeight);
45}
46
48{
49 for (auto ptr = mTextList.begin(),
50 pEnd = mTextList.end(); ptr != pEnd; ++ptr)
51 {
52 if (*ptr == text)
53 {
54 mTextList.erase(ptr);
55 return;
56 }
57 }
58}
59
61
62void TextManager::draw(gcn::Graphics *graphics, int xOff, int yOff)
63{
64 for (auto text : mTextList)
65 {
66 text->draw(graphics, xOff, yOff);
67 }
68}
69
70void TextManager::place(const Text *textObj, const Text *omit,
71 int &x, int &y, int h)
72{
73 int xLeft = textObj->mX;
74 int xRight = xLeft + textObj->mWidth - 1;
75 const int TEST = 100; // Number of lines to test for text
76 bool occupied[TEST]; // is some other text obscuring this line?
77 std::memset(&occupied, 0, sizeof(occupied)); // set all to false
78 int wantedTop = (TEST - h) / 2; // Entry in occupied at top of text
79 int occupiedTop = y - wantedTop; // Line in map representing to of occupied
80
81 for (auto text : mTextList)
82 {
83 if (text != omit &&
84 text->mX <= xRight &&
85 text->mX + text->mWidth > xLeft)
86 {
87 int from = text->mY - occupiedTop;
88 int to = from + text->mHeight - 1;
89 if (to < 0 || from >= TEST) // out of range considered
90 continue;
91 if (from < 0)
92 from = 0;
93 if (to >= TEST)
94 to = TEST - 1;
95 for (int i = from; i <= to; ++i)
96 occupied[i] = true;
97 }
98 }
99 bool ok = true;
100 for (int i = wantedTop; i < wantedTop + h; ++i)
101 {
102 ok = ok && !occupied[i];
103 }
104
105 if (ok)
106 return;
107
108 // Have to move it up or down, so find nearest spaces either side
109 int consec = 0;
110 int upSlot = -1; // means not found
111 for (int seek = wantedTop + h - 2; seek >= 0; --seek)
112 {
113 if (occupied[seek])
114 {
115 consec = 0;
116 }
117 else
118 {
119 if (++consec == h)
120 {
121 upSlot = seek;
122 break;
123 }
124 }
125 }
126 int downSlot = -1;
127 consec = 0;
128 for (int seek = wantedTop + 1; seek < TEST; ++seek)
129 {
130 if (occupied[seek])
131 {
132 consec = 0;
133 }
134 else
135 {
136 if (++consec == h)
137 {
138 downSlot = seek - h + 1;
139 break;
140 }
141 }
142 }
143 if (upSlot == -1 && downSlot == -1) // no good solution, so leave as is
144 {
145 return;
146 }
147 if (upSlot == -1) // must go down
148 {
149 y += downSlot - wantedTop;
150 return;
151 }
152 if (downSlot == -1) // must go up
153 {
154 y -= wantedTop - upSlot;
155 return;
156 }
157 if (wantedTop - upSlot > downSlot - wantedTop) // down is better
158 {
159 y += downSlot - wantedTop;
160 }
161 else
162 {
163 y -= wantedTop - upSlot;
164 }
165}
void addText(Text *text)
Add text to the manager.
void moveText(Text *text, int x, int y)
Move the text around the screen.
void place(const Text *textObj, const Text *omit, int &x, int &y, int h)
Position the text so as to avoid conflict.
~TextManager()
Destroy the manager.
void draw(gcn::Graphics *graphics, int xOff, int yOff)
Draw the text.
void removeText(const Text *text)
Remove the text from the manager.
std::list< Text * > mTextList
Definition textmanager.h:67
Definition text.h:34
int mX
Actual x-value of left of text written.
Definition text.h:67
int mWidth
The width of the text.
Definition text.h:69
int mHeight
The height of the text.
Definition text.h:70
int mY
Actual y-value of top of text written.
Definition text.h:68
Graphics * graphics
Definition client.cpp:104
TextManager * textManager