13 public enum GradientDirection { vertical, horizontal };
15 public void PaletteGradient(ColorAlpha * palette, int numColors, ColorKey * keys, int numKeys, float smoothness)
17 ColorKey * key = keys, * nextKey = keys;
18 int keyNum = 0, nextKeyNum = 0;
19 float inc = 1.0f/(numColors-1);
25 for(c = start = 0; c<numColors; c++)
27 ColorAlpha newColor = 0;
29 while(nextKey && percent > nextKey->percent)
31 key = nextKey; keyNum = nextKeyNum;
33 if(keyNum < numKeys - 1)
36 nextKeyNum = keyNum + 1;
42 if(nextKey && nextKey->percent != key->percent)
44 float scale = ease((percent - key->percent) / (nextKey->percent - key->percent),
45 smoothness, smoothness);
46 int cr = key->color.color.r;
47 int cg = key->color.color.g;
48 int cb = key->color.color.b;
49 int nr = nextKey->color.color.r;
50 int ng = nextKey->color.color.g;
51 int nb = nextKey->color.color.b;
52 int r = (int)(cr + (nr - cr) * scale);
53 int g = (int)(cg + (ng - cg) * scale);
54 int b = (int)(cb + (nb - cb) * scale);
56 r = Max(Min(r, 255),0);
57 g = Max(Min(g, 255),0);
58 b = Max(Min(b, 255),0);
60 newColor = Color { (byte)r, (byte)g, (byte)b };
63 newColor = key ? key->color : 0;
65 if(c == 0 || newColor != color)
70 for(i = start; i<c; i++)
82 for(i = start; i<c; i++)
87 float ease(float t, float a, float b)
92 if (s == 0.0f) return t;
99 if (t < a) return (k/a)*t*t;
100 if (t < 1.0f - b) return k*(2.0f * t - a);
102 return 1.0f - (k/b)*t*t;
105 public enum AlphaWriteMode
117 Box box, unclippedBox;
121 subclass(DisplayDriver) driver;
122 DisplaySystem displaySystem;
128 ColorAlpha foreground, background;
130 AlphaWriteMode alphaWrite;
134 ColorAlpha outlineColor;
141 outlineColor = black;
146 driver.ReleaseSurface(display, this);
150 property AlphaWriteMode alphaWrite
152 set { alphaWrite = value; }
153 get { return alphaWrite; }
157 set { blend = value; }
158 get { return blend; }
160 property Bitmap bitmap
164 return ((LFBSurface)driverData).bitmap;
167 property ColorAlpha outlineColor
169 set { outlineColor = value; }
170 get { return outlineColor; }
173 ColorAlpha GetPixel(int x, int y)
175 return driver.GetPixel(display, this, x,y);
178 void PutPixel(int x, int y)
180 driver.PutPixel(display, this, x,y);
183 void DrawLine(int x1, int y1, int x2, int y2)
185 driver.DrawLine(display, this, x1,y1,x2,y2);
188 void VLine(int y1, int y2, int x)
190 driver.DrawLine(display, this, x,y1,x,y2);
193 void HLine(int x1, int x2, int y)
195 driver.DrawLine(display, this, x1,y,x2,y);
198 void Rectangle(int x1, int y1, int x2, int y2)
200 driver.Rectangle(display, this, x1,y1,x2,y2);
203 void Area(int x1, int y1, int x2, int y2)
205 driver.Area(display, this, x1,y1,x2,y2);
208 void Clear(ClearType type)
210 driver.Clear(display, this, type);
213 void Blit(Bitmap src, int dx, int dy, int sx, int sy, int w, int h)
215 if(src.driver == driver)
216 driver.Blit(display, this, src, dx,dy, sx, sy,w,h);
217 else if(!src.driver || src.driver == class(LFBDisplayDriver))
218 driver.BlitDI(display, this, src, dx,dy, sx, sy,w,h);
221 void Stretch(Bitmap src, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
223 if(src.driver == driver)
224 driver.Stretch(display, this, src, dx,dy, sx,sy, w,h, sw, sh);
225 else if(!src.driver || src.driver == class(LFBDisplayDriver))
226 driver.StretchDI(display, this, src, dx,dy, sx,sy, w,h, sw, sh);
229 void Filter(Bitmap src, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
231 if(src.driver == driver)
232 driver.Filter(display, this, src, dx,dy, sx,sy, w,h, sw, sh);
233 else if(!src.driver || src.driver == class(LFBDisplayDriver))
234 driver.FilterDI(display, this, src, dx,dy, sx,sy, w,h, sw, sh);
237 void Tile(Bitmap src, int dx, int dy, int w, int h)
239 if(src && src.width && src.height)
242 for(x = 0,sx = dx; x < w; x += src.width, sx += src.width)
243 for(y = 0, sy = dy; y < h; y += src.height,sy += src.height)
244 Blit(src, sx, sy, 0,0, Min(src.width, w - x), Min(src.height, h - y));
248 void HTile(Bitmap src, int dx, int dy, int w, int h)
250 if(src && src.width && src.height)
253 for(x = 0, sx = dx; x < w; x += src.width, sx += src.width)
254 Stretch(src, sx, dy, 0, 0, Min(src.width, w - x), h, Min(src.width, w - x), src.height);
258 void VTile(Bitmap src, int dx, int dy, int w, int h)
260 if(src && src.width && src.height)
263 for(y = 0, sy = dy; y < h; y += src.height, sy += src.height)
264 Stretch(src, dx, sy, 0,0, w, Min(src.height, h - y), src.width, Min(src.height, h - y));
268 void FilterHTile(Bitmap src, int dx, int dy, int w, int h)
270 if(src && src.width && src.height)
273 for(x = 0, sx = dx; x < w; x += src.width, sx += src.width)
274 Filter(src, sx, dy, 0, 0, Min(src.width, w - x), h, Min(src.width, w - x), src.height);
278 void FilterVTile(Bitmap src, int dx, int dy, int w, int h)
280 if(src && src.width && src.height)
283 for(y = 0, sy = dy; y < h; y += src.height, sy += src.height)
284 Filter(src, dx, sy, 0,0, w, Min(src.height, h - y), src.width, Min(src.height, h - y));
288 void WriteText(int x, int y, const char * text, int len)
291 driver.WriteText(display, this, x,y, text, len, 0, null); //, null);
294 void WriteText2(int x, int y, const char * text, int len, int prevGlyph, int * rPrevGlyph)
297 driver.WriteText(display, this, x,y, text, len, prevGlyph, rPrevGlyph);
300 void TextExtent(const char * text, int len, int * width, int * height)
303 driver.TextExtent(display, this, text, len, width, height, 0, null, &advance);
308 void TextExtent2(const char * text, int len, int * width, int * height, int prevGlyph, int * rPrevGlyph, int * overHang)
310 driver.TextExtent(display, this, text, len, width, height, prevGlyph, rPrevGlyph, overHang);
313 void WriteTextf(int x, int y, const char * format, ...)
317 char text[MAX_F_STRING];
319 va_start(args, format);
320 vsnprintf(text, sizeof(text), format, args);
321 text[sizeof(text)-1] = 0;
323 driver.WriteText(display, this, x,y, text, strlen(text), 0, null);
328 void CenterTextf(int x, int y, const char * format, ...)
332 char text[MAX_F_STRING];
336 va_start(args, format);
337 vsnprintf(text, sizeof(text), format, args);
338 text[sizeof(text)-1] = 0;
341 driver.TextExtent(display, this, text, len, &w, &h, 0, null, &oh);
343 driver.WriteText(display, this, x - w/2, y, text, len, 0, null);
348 void WriteTextDots(Alignment alignment, int x, int y, int width, const char * text, int len)
352 TextExtent(text, len, &w, &h);
355 if(alignment == right)
357 else if(alignment == center)
358 x += (width - w) / 2;
359 WriteText(x, y, text, len);
370 TextExtent(".", 1, &dw, &dh);
372 #define UTF8_NUM_BYTES(x) (__extension__({ byte b = x; (b & 0x80 && b & 0x40) ? ((b & 0x20) ? ((b & 0x10) ? 4 : 3) : 2) : 1; }))
373 for(c = 0; (ch = text[c]); c += nb)
375 nb = UTF8_NUM_BYTES(ch);
376 TextExtent(text+c, nb, &w, &h);
382 WriteText(x, y, text, c);
383 //TextExtent(text, c, &totalW, &h);
385 WriteText(x, y, "...", 3);
389 void WriteTextDotsf(Alignment alignment, int x, int y, int width, const char * format, ...)
393 char text[MAX_F_STRING];
395 va_start(args, format);
396 vsnprintf(text, sizeof(text), format, args);
397 text[sizeof(text)-1] = 0;
398 WriteTextDots(alignment, x,y, width, text, strlen(text));
403 void Bevel(bool inner, int x, int y, int w, int h)
405 ColorAlpha foreground = this.foreground;
407 SetForeground(inner ? Color { 128,128,128 } : formColor);
408 HLine(x, x+w - 2, y);
409 VLine(y+1, y+h - 2, x);
411 SetForeground(inner ? Color { 64,64,64 } : white);
412 HLine(x+1, x+w-3, y+1);
413 VLine(y+2, y+h-3, x+1);
415 SetForeground(inner ? formColor : Color { 128,128,128 } );
416 HLine(x+1, x+w-2, y + h -2);
417 VLine(y+1, y+h-3, x + w - 2);
419 SetForeground(inner ? white : Color { 64,64,64 });
420 HLine(x, x+w-1, y + h - 1);
421 VLine(y, y+h-2, x + w - 1);
423 SetForeground(foreground);
426 void ThinBevel(bool inner, int x, int y, int w, int h)
428 SetForeground(inner ? Color { 128,128,128 } : white);
429 HLine(x, x+w - 2, y);
430 VLine(y+1, y+h - 2, x);
431 SetForeground(inner ? white : Color { 128,128,128 });
432 HLine(x, x+w-1, y + h - 1);
433 VLine(y, y+h-2, x + w - 1);
436 void Gradient(ColorKey * keys, int numKeys, float smoothness, GradientDirection direction, int x1, int y1, int x2, int y2)
438 if(x2 >= box.left && x1 <= box.right && y2 >= box.top && y1 <= box.bottom)
440 ColorKey * key = keys, * nextKey = keys;
441 int keyNum = 0, nextKeyNum = 0;
442 int height = (direction == horizontal) ? ((x2 - x1) + 1) : ((y2 - y1) + 1);
443 float inc = 1.0f/(height-1);
446 ColorAlpha color = 0;
447 int firstPixel = (direction == horizontal) ? x1 : y1;
448 int lastPixel = (direction == horizontal) ? x2 : y2;
449 int boxLeft = (direction == horizontal) ? box.left : box.top;
450 int boxRight = (direction == horizontal) ? box.right : box.bottom;
454 if(boxLeft > firstPixel)
456 percent = (boxLeft - firstPixel) * inc;
457 firstPixel = boxLeft;
459 if(boxRight < lastPixel)
460 lastPixel = boxRight;
462 for(c = start = firstPixel; c<=lastPixel; c++)
466 while(nextKey && percent > nextKey->percent)
468 key = nextKey; keyNum = nextKeyNum;
470 if(keyNum < numKeys - 1)
473 nextKeyNum = keyNum + 1;
479 if(nextKey && nextKey->percent != key->percent)
481 float scale = ease((percent - key->percent) / (nextKey->percent - key->percent),
482 smoothness, smoothness);
483 int cr = key->color.color.r;
484 int cg = key->color.color.g;
485 int cb = key->color.color.b;
486 int nr = nextKey->color.color.r;
487 int ng = nextKey->color.color.g;
488 int nb = nextKey->color.color.b;
489 int r = (int)(cr + (nr - cr) * scale);
490 int g = (int)(cg + (ng - cg) * scale);
491 int b = (int)(cb + (nb - cb) * scale);
493 r = Max(Min(r, 255),0);
494 g = Max(Min(g, 255),0);
495 b = Max(Min(b, 255),0);
497 newColor = Color { (byte)r, (byte)g, (byte)b };
500 newColor = key ? key->color : 0;
502 if(c == firstPixel || newColor != color)
506 SetBackground(color);
508 if(direction == horizontal)
509 Area(start,y1,c-1,y2);
511 Area(x1, start,x2, c-1);
519 SetBackground(color);
520 if(direction == horizontal)
521 Area(start,y1,c-1,y2);
523 Area(x1, start,x2, c-1);
528 property ColorAlpha foreground
533 driver.SetForeground(display, this, value);
535 get { return foreground; }
538 property ColorAlpha background
543 driver.SetBackground(display, this, value);
545 get { return background; }
548 property ColorAlpha blitTint
553 driver.SetBlitTint(display, this, value);
555 get { return blitTint; }
558 property uint lineStipple
562 driver.LineStipple(display, this, value);
568 get { value = { width, height }; }
576 if(display && display.flags.text)
578 value.left *= textCellW;
579 value.top *= textCellH;
580 value.right *= textCellW;
581 value.bottom *= textCellH;
586 property Display display
588 get { return display; }
595 if(value && font != value)
597 driver.TextFont(display, this, value);
605 property bool textOpacity
610 driver.TextOpacity(display, this, value);
613 get { return textOpacity; }
616 property byte drawingChar
618 set { driver.DrawingChar(display, this, value); }
621 property Box clipping
623 set { driver.Clip(display, this, value); }
626 // TODO: Make these functions obsolete
627 void SetForeground(ColorAlpha value)
630 driver.SetForeground(display, this, value);
633 void SetBackground(ColorAlpha value)
636 driver.SetBackground(display, this, value);
639 Color GetForeground(void)
644 Color GetBackground(void)
649 void LineStipple(uint value)
651 driver.LineStipple(display, this, value);
654 void GetSize(int * w, int * h)
660 void GetBox(Box value)
663 if(display.flags.text)
665 box.left *= textCellW;
666 box.top *= textCellH;
667 box.right *= textCellW;
668 box.bottom *= textCellH;
672 Display GetDisplay(void)
677 void TextFont(Font value)
679 if(value && font != value)
681 driver.TextFont(display, this, value);
691 bool GetTextOpacity(void)
696 void TextOpacity(bool value)
699 driver.TextOpacity(display, this, value);
702 void DrawingChar(unsigned char value)
704 driver.DrawingChar(display, this, value);
709 driver.Clip(display, this, box);