]> git.vomp.tv Git - vompclient.git/blob - box.cc
New GUI code
[vompclient.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   area.x = 0;
28   area.y = 0;
29   area.w = 0;
30   area.h = 0;
31
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   area.x = x;
54   area.y = y;
55 }
56
57 void Box::setSurfaceOffset(UINT x, UINT y)
58 {
59   offsetX = x;
60   offsetY = y;
61 }
62
63 void Box::show()
64 {
65   show(area);
66 }
67
68 void Box::show(Region& r)
69 {
70   Log::getInstance()->log("Box", Log::DEBUG, "Show region %p %u %u %u %u", surface, r.x, r.y, r.w, r.h);
71 //  surface->updateToScreen(area.x, area.y, area.w, area.h);
72
73   /* surface update to screen needs:
74   source x distance into this surface
75   source y distance into this surface
76   width of update
77   height of update
78   destination x on screen
79   destination y on screen
80   */
81
82   surface->updateToScreen(r.x - area.x, r.y - area.y, r.w, r.h, r.x, r.y);
83 }
84
85 int Box::getScreenX()
86 {
87   return area.x;
88 }
89
90 int Box::getScreenY()
91 {
92   return area.y;
93 }
94
95 int Box::getWidth()
96 {
97   return area.w;
98 }
99
100 int Box::getHeight()
101 {
102   return area.h;
103 }
104
105 // Level 1 drawing functions
106
107 void Box::fillColour(Colour& colour)
108 {
109   rectangle(0, 0, area.w, area.h, colour);
110 }
111
112 void Box::drawPara(char* text, int x, int y, Colour& colour)
113 {
114   char line[256];
115   int lineHeight = surface->getFontHeight() + 6;
116
117   int lineWidth;
118   int thisCharWidth;
119   int textPos;
120   int linePos;
121   int ypos;
122   int printLine;
123
124   textPos = 0;
125   ypos = y;
126
127   while(1)
128   {
129     linePos = 0;
130     lineWidth = 0;
131     while(1)
132     {
133       printLine = 0;
134
135       if (text[textPos] == '\0') break;
136
137       if (text[textPos] == '\n')
138       {
139         textPos++; // ignore the \n
140         printLine = 1;
141         break;
142       }
143
144       thisCharWidth = surface->getCharWidth(text[textPos]);
145       if ((lineWidth + thisCharWidth) > (int)(area.w - (2 * paraMargin)))
146       {
147         // this character would break the right margin
148         if (text[textPos] == ' ')
149         {
150           // this char is a space, ignore and break
151           textPos++;
152           break;
153         }
154         else
155         {
156           // Need to go back to the last space in the line
157           while ((text[textPos] != ' ') && (linePos >= 0))
158           {
159             textPos--;
160             linePos--;
161           }
162           // Now take the space we just found
163           textPos++;
164           break;
165         }
166       }
167       line[linePos++] = text[textPos];
168       lineWidth += thisCharWidth;
169       textPos++;
170     }
171
172     line[linePos++] = '\0';
173     if (printLine || (linePos > 1)) // if some text was put in line
174     {
175       drawText(line, x, ypos, colour);
176       ypos += lineHeight;
177       if (ypos > (int)(area.h - lineHeight)) break;
178     }
179     else
180     {
181       break;
182     }
183   }
184 }
185
186 // Level 0 drawing functions
187
188 void Box::rectangle(int x1, int y1, int w, int h, Colour& colour)
189 {
190   surface->fillblt(offsetX + x1, offsetY + y1, w, h, surface->rgba(colour.red, colour.green, colour.blue, colour.alpha));
191 }
192
193 void Box::drawText(char* text, int x, int y, Colour& colour)
194 {
195   surface->drawText(text, offsetX + x, offsetY + y, colour.red, colour.green, colour.blue);
196 }
197
198 void Box::drawTextRJ(char* text, int x, int y, Colour& colour)
199 {
200   surface->drawTextRJ(text, offsetX + x, offsetY + y, colour.red, colour.green, colour.blue);
201 }
202
203 void Box::drawPixel(UINT x, UINT y, Colour& colour)
204 {
205   int c = (  (0xFF000000        )
206              | (colour.red  << 16)
207              | (colour.green  <<  8)
208              | (colour.blue     ) );
209
210
211   surface->drawPixel(offsetX + x, offsetY + y, c);
212 }