]> git.vomp.tv Git - vompclient-marten.git/blob - box.cc
New gui code
[vompclient-marten.git] / box.cc
1 /*
2     Copyright 2004-2005 Chris Tallon
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "box.h"
22
23 char Box::numBoxes = 0;
24
25 Box::Box()
26 {
27   height = 0;
28   width = 0;
29
30   screenX = 0;
31   screenY = 0;
32   offsetX = 0;
33   offsetY = 0;
34
35   numBoxes++;
36   Log::getInstance()->log("Box", Log::DEBUG, "Construct, now %u", numBoxes);
37
38   surface = NULL;
39 }
40
41 Box::~Box()
42 {
43   numBoxes--;
44   Log::getInstance()->log("Box", Log::DEBUG, "Destruct, now %u", numBoxes);
45 }
46
47 void Box::draw()
48 {
49 }
50
51 void Box::setScreenPos(int x, int y)
52 {
53   screenX = x;
54   screenY = y;
55 }
56
57 void Box::setSurfaceOffset(UINT x, UINT y)
58 {
59   offsetX = x;
60   offsetY = y;
61 }
62
63 void Box::show(int noBufferToScreen)
64 {
65   Log::getInstance()->log("Box", Log::DEBUG, "Show data %p %u %u %u %u %u", surface, screenX, screenY, width, height, noBufferToScreen);
66   surface->updateToBuffer(screenX, screenY, width, height);
67   if (!noBufferToScreen) Surface::bufferToScreen(screenX, screenY, width, height);
68 }
69
70 int Box::getScreenX()
71 {
72   return screenX;
73 }
74
75 int Box::getScreenY()
76 {
77   return screenY;
78 }
79
80 int Box::getWidth()
81 {
82   return width;
83 }
84
85 int Box::getHeight()
86 {
87   return height;
88 }
89
90 // Level 1 drawing functions
91
92 void Box::fillColour(Colour& colour)
93 {
94   rectangle(0, 0, width, height, colour);
95 }
96
97 void Box::drawPara(char* text, int x, int y, Colour& colour)
98 {
99   char line[256];
100   int lineHeight = surface->getFontHeight() + 6;
101
102   int lineWidth;
103   int thisCharWidth;
104   int textPos;
105   int linePos;
106   int ypos;
107   int printLine;
108
109   textPos = 0;
110   ypos = y;
111
112   while(1)
113   {
114     linePos = 0;
115     lineWidth = 0;
116     while(1)
117     {
118       printLine = 0;
119
120       if (text[textPos] == '\0') break;
121
122       if (text[textPos] == '\n')
123       {
124         textPos++; // ignore the \n
125         printLine = 1;
126         break;
127       }
128
129       thisCharWidth = surface->getCharWidth(text[textPos]);
130       if ((lineWidth + thisCharWidth) > (int)(width - (2 * paraMargin)))
131       {
132         // this character would break the right margin
133         if (text[textPos] == ' ')
134         {
135           // this char is a space, ignore and break
136           textPos++;
137           break;
138         }
139         else
140         {
141           // Need to go back to the last space in the line
142           while ((text[textPos] != ' ') && (linePos >= 0))
143           {
144             textPos--;
145             linePos--;
146           }
147           // Now take the space we just found
148           textPos++;
149           break;
150         }
151       }
152       line[linePos++] = text[textPos];
153       lineWidth += thisCharWidth;
154       textPos++;
155     }
156
157     line[linePos++] = '\0';
158     if (printLine || (linePos > 1)) // if some text was put in line
159     {
160       drawText(line, x, ypos, colour);
161       ypos += lineHeight;
162       if (ypos > (int)(height - lineHeight)) break;
163     }
164     else
165     {
166       break;
167     }
168   }
169 }
170
171 // Level 0 drawing functions
172
173 void Box::rectangle(int x1, int y1, int w, int h, Colour& colour)
174 {
175   surface->fillblt(offsetX + x1, offsetY + y1, w, h, surface->rgba(colour.red, colour.green, colour.blue, colour.alpha));
176 }
177
178 void Box::drawText(char* text, int x, int y, Colour& colour)
179 {
180   surface->drawText(text, offsetX + x, offsetY + y, colour.red, colour.green, colour.blue);
181 }
182
183 void Box::drawTextRJ(char* text, int x, int y, Colour& colour)
184 {
185   surface->drawTextRJ(text, offsetX + x, offsetY + y, colour.red, colour.green, colour.blue);
186 }
187
188 void Box::drawPixel(UINT x, UINT y, Colour& colour)
189 {
190   int c = (  (0xFF000000        )
191              | (colour.red  << 16)
192              | (colour.green  <<  8)
193              | (colour.blue     ) );
194
195
196   surface->drawPixel(offsetX + x, offsetY + y, c);
197 }