00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "textmanager.h"
00023
00024 #include <cstring>
00025
00026 #include "text.h"
00027
00028 TextManager *textManager = 0;
00029
00030 TextManager::TextManager()
00031 {
00032 }
00033
00034 void TextManager::addText(Text *text)
00035 {
00036 place(text, 0, text->mX, text->mY, text->mHeight);
00037 mTextList.push_back(text);
00038 }
00039
00040 void TextManager::moveText(Text *text, int x, int y)
00041 {
00042 text->mX = x;
00043 text->mY = y;
00044 place(text, text, text->mX, text->mY, text->mHeight);
00045 }
00046
00047 void TextManager::removeText(const Text *text)
00048 {
00049 for (TextList::iterator ptr = mTextList.begin(),
00050 pEnd = mTextList.end(); ptr != pEnd; ++ptr)
00051 {
00052 if (*ptr == text)
00053 {
00054 mTextList.erase(ptr);
00055 return;
00056 }
00057 }
00058 }
00059
00060 TextManager::~TextManager()
00061 {
00062 }
00063
00064 void TextManager::draw(gcn::Graphics *graphics, int xOff, int yOff)
00065 {
00066 for (TextList::iterator bPtr = mTextList.begin(), ePtr = mTextList.end();
00067 bPtr != ePtr; ++bPtr)
00068 {
00069 (*bPtr)->draw(graphics, xOff, yOff);
00070 }
00071 }
00072
00073 void TextManager::place(const Text *textObj, const Text *omit,
00074 int &x, int &y, int h)
00075 {
00076 int xLeft = textObj->mX;
00077 int xRight = xLeft + textObj->mWidth - 1;
00078 const int TEST = 100;
00079 bool occupied[TEST];
00080 std::memset(&occupied, 0, sizeof(occupied));
00081 int wantedTop = (TEST - h) / 2;
00082 int occupiedTop = y - wantedTop;
00083
00084 for (TextList::const_iterator ptr = mTextList.begin(),
00085 pEnd = mTextList.end(); ptr != pEnd; ++ptr)
00086 {
00087 if (*ptr != omit &&
00088 (*ptr)->mX <= xRight &&
00089 (*ptr)->mX + (*ptr)->mWidth > xLeft)
00090 {
00091 int from = (*ptr)->mY - occupiedTop;
00092 int to = from + (*ptr)->mHeight - 1;
00093 if (to < 0 || from >= TEST)
00094 continue;
00095 if (from < 0)
00096 from = 0;
00097 if (to >= TEST)
00098 to = TEST - 1;
00099 for (int i = from; i <= to; ++i)
00100 occupied[i] = true;
00101 }
00102 }
00103 bool ok = true;
00104 for (int i = wantedTop; i < wantedTop + h; ++i)
00105 {
00106 ok = ok && !occupied[i];
00107 }
00108
00109 if (ok)
00110 return;
00111
00112
00113 int consec = 0;
00114 int upSlot = -1;
00115 for (int seek = wantedTop + h - 2; seek >= 0; --seek)
00116 {
00117 if (occupied[seek])
00118 {
00119 consec = 0;
00120 }
00121 else
00122 {
00123 if (++consec == h)
00124 {
00125 upSlot = seek;
00126 break;
00127 }
00128 }
00129 }
00130 int downSlot = -1;
00131 consec = 0;
00132 for (int seek = wantedTop + 1; seek < TEST; ++seek)
00133 {
00134 if (occupied[seek])
00135 {
00136 consec = 0;
00137 }
00138 else
00139 {
00140 if (++consec == h)
00141 {
00142 downSlot = seek - h + 1;
00143 break;
00144 }
00145 }
00146 }
00147 if (upSlot == -1 && downSlot == -1)
00148 {
00149 return;
00150 }
00151 if (upSlot == -1)
00152 {
00153 y += downSlot - wantedTop;
00154 return;
00155 }
00156 if (downSlot == -1)
00157 {
00158 y -= wantedTop - upSlot;
00159 return;
00160 }
00161 if (wantedTop - upSlot > downSlot - wantedTop)
00162 {
00163 y += downSlot - wantedTop;
00164 }
00165 else
00166 {
00167 y -= wantedTop - upSlot;
00168 }
00169 }