Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ImageBox.cpp
Go to the documentation of this file.
1 /* Project nGenEx
2  Destroyer Studios LLC
3  Copyright © 1997-2004. All Rights Reserved.
4 
5  SUBSYSTEM: nGenEx.lib
6  FILE: ImageBox.cpp
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  ImageBox class
13 */
14 
15 #include "MemDebug.h"
16 #include "ImageBox.h"
17 #include "Video.h"
18 #include "Bitmap.h"
19 #include "Font.h"
20 #include "DataLoader.h"
21 
22 // +--------------------------------------------------------------------+
23 
24 ImageBox::ImageBox(ActiveWindow* p, int ax, int ay, int aw, int ah, DWORD aid)
25 : ActiveWindow(p->GetScreen(), ax, ay, aw, ah, aid, 0, p), blend_mode(Video::BLEND_ALPHA)
26 {
27  picture_loc = 1;
28  text_align = DT_CENTER;
29 
30  char buf[32];
31  sprintf_s(buf, "ImageBox %d", id);
32  desc = buf;
33 }
34 
35 ImageBox::ImageBox(Screen* s, int ax, int ay, int aw, int ah, DWORD aid)
36 : ActiveWindow(s, ax, ay, aw, ah, aid, 0, 0), blend_mode(Video::BLEND_ALPHA)
37 {
38  picture_loc = 1;
39  text_align = DT_CENTER;
40 
41  char buf[32];
42  sprintf_s(buf, "ImageBox %d", id);
43  desc = buf;
44 }
45 
46 // +--------------------------------------------------------------------+
47 
49 { }
50 
51 // +--------------------------------------------------------------------+
52 
53 void
55 {
56  if (!shown)
57  return;
58 
59  int x = 0;
60  int y = 0;
61  int w = rect.w;
62  int h = rect.h;
63  int img_w = picture.Width();
64  int img_h = picture.Height();
65 
66  Rect box_rect(x,y,w,h);
67 
68  // draw the picture (if any)
69  if (picture.Width()) {
70  Rect irect = CalcPictureRect();
71  DrawBitmap(irect.x,
72  irect.y,
73  irect.x + irect.w,
74  irect.y + irect.h,
75  &picture,
76  blend_mode);
77  }
78 
79  // draw the border:
80  DrawStyleRect(0, 0, w, h, style);
81 
82  // draw text here:
83  if (font && text.length()) {
84  int border_size = 4;
85 
87  border_size = 8;
88 
89  Rect label_rect = CalcLabelRect(img_w,img_h);
90  int vert_space = label_rect.h;
91  int horz_space = label_rect.w;
92 
93  DrawText(text.data(), 0, label_rect, DT_CALCRECT | DT_WORDBREAK | DT_CENTER);
94  vert_space = (vert_space - label_rect.h)/2;
95 
96  label_rect.w = horz_space;
97 
98  if (vert_space > 0)
99  label_rect.y += vert_space;
100 
101  Color fore = ShadeColor(fore_color, 1);
102  font->SetColor(fore);
103  DrawText(text.data(), 0, label_rect, DT_WORDBREAK | DT_CENTER);
104  }
105 }
106 
107 void
109 {
110  if (!shown)
111  return;
112 
113  int x = 0;
114  int y = 0;
115  int w = rect.w;
116  int h = rect.h;
117  int img_w = picture.Width();
118  int img_h = picture.Height();
119 
120  Rect box_rect(x,y,w,h);
121 
122  // draw the picture (if any)
123  if (picture.Width()) {
124  Rect irect = CalcPictureRect();
125  DrawBitmap(irect.x,
126  irect.y,
127  irect.x + irect.w,
128  irect.y + irect.h,
129  &picture,
131  }
132 
134 }
135 
136 Rect ImageBox::CalcLabelRect(int img_w, int img_h)
137 {
138  // fit the text in the bevel:
139  Rect label_rect;
140  label_rect.x = 0;
141  label_rect.y = 0;
142  label_rect.w = rect.w;
143  label_rect.h = rect.h;
144 
145  label_rect.Deflate(2,2);
146 
147  // and around the picture, if any:
148  if (img_h != 0) {
149  switch (picture_loc) {
150  default:
151  case 0: // the four corner positions
152  case 2: // and the center position
153  case 4: // don't affect the text position
154  case 6:
155  case 8:
156  break;
157 
158  case 1: // north
159  label_rect.y += img_h;
160  label_rect.h -= img_h;
161  break;
162 
163  case 3: // west
164  label_rect.x += img_w;
165  label_rect.w -= img_w;
166  break;
167 
168  case 5: // east
169  label_rect.w -= img_w;
170  break;
171 
172  case 7: // south
173  label_rect.h -= img_h;
174  break;
175  }
176  }
177 
178  return label_rect;
179 }
180 
181 // +--------------------------------------------------------------------+
182 
183 Rect
185 {
186  if (target_rect.w > 0 && target_rect.h > 0)
187  return target_rect;
188 
189  int w = rect.w;
190  int h = rect.h;
191  int img_w = picture.Width();
192  int img_h = picture.Height();
193 
194  if (img_h > h) img_h = h-2;
195  if (img_w > w) img_w = w-2;
196 
197  int img_x_offset = 0;
198  int img_y_offset = 0;
199 
200  switch (picture_loc) {
201  default:
202  // TOP ROW:
203  case 0: break;
204 
205  case 1: img_x_offset = (w/2-img_w/2);
206  break;
207 
208  case 2: img_x_offset = w - img_w;
209  break;
210 
211  // MIDDLE ROW:
212  case 3: img_y_offset = (h/2-img_h/2);
213  break;
214  case 4: img_x_offset = (w/2-img_w/2);
215  img_y_offset = (h/2-img_h/2);
216  break;
217  case 5: img_x_offset = w - img_w;
218  img_y_offset = (h/2-img_h/2);
219  break;
220 
221  // BOTTOM ROW:
222  case 6:
223  img_y_offset = h - img_h;
224  break;
225  case 7: img_x_offset = (w/2-img_w/2);
226  img_y_offset = h - img_h;
227  break;
228  case 8: img_x_offset = w - img_w;
229  img_y_offset = h - img_h;
230  break;
231  }
232 
233  Rect img_rect;
234  img_rect.x = img_x_offset;
235  img_rect.y = img_y_offset;
236  img_rect.w = img_w;
237  img_rect.h = img_h;
238 
239  return img_rect;
240 }
241 
242 // +--------------------------------------------------------------------+
243 
244 int ImageBox::OnMouseMove(int x, int y)
245 {
246  return ActiveWindow::OnMouseMove(x,y);
247 }
248 
249 int ImageBox::OnLButtonDown(int x, int y)
250 {
251  return ActiveWindow::OnLButtonDown(x,y);
252 }
253 
254 int ImageBox::OnLButtonUp(int x, int y)
255 {
256  return ActiveWindow::OnLButtonUp(x,y);
257 }
258 
260 {
261  return ActiveWindow::OnClick();
262 }
263 
264 int ImageBox::OnMouseEnter(int mx, int my)
265 {
266  return ActiveWindow::OnMouseEnter(mx, my);
267 }
268 
269 int ImageBox::OnMouseExit(int mx, int my)
270 {
271  return ActiveWindow::OnMouseExit(mx, my);
272 }
273 
274 // +--------------------------------------------------------------------+
275 
276 void ImageBox::GetPicture(Bitmap& img) const
277 {
278  img.CopyBitmap(picture);
279 }
280 
281 void ImageBox::SetPicture(const Bitmap& img)
282 {
283  picture.CopyBitmap(img);
284  picture.AutoMask();
286 }
287 
289 {
290  return picture_loc;
291 }
292 
294 {
295  if (picture_loc != n && n >= 0 && n <= 8) {
296  picture_loc = n;
297  }
298 }