1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
|
use ui;
use core:io;
use core:geometry;
use graphics;
/**
* An elements that simply fills its assigned rectangle with a brush.
*/
class SolidFill extends Element {
// The brush we're using to fill the screen.
Brush fill;
// Brush used to border the solid box, if any.
Brush? border;
// Create.
init(Brush fill) {
init() { fill = fill; }
}
// Create, select a solid color.
init(Color color) {
init() { fill = SolidBrush(color); }
}
// Set the border.
void border(Color c) {
border = SolidBrush(c);
}
// Minimum size.
Size minSize() : override { Size(); }
// Draw.
void draw(Graphics g) : override {
if (border) {
g.draw(pos, border);
}
g.fill(pos, fill);
}
}
/**
* An element that displays text. Useful for headings etc.
*
* Text is not re-flowed to fit the container, and the text will be centered horizontally in the
* given space.
*/
class Heading extends Element {
// Pre-formatted text.
Text text;
// The brush used to fill the text.
Brush brush;
// Create.
init(FormatStr text, Font f, Brush brush) {
init() {
text = text.text(f);
brush = brush;
}
}
// Create, specifying a solid color.
init(FormatStr text, Font f, Color color) {
init() {
text = text.text(f);
brush = SolidBrush(color);
}
}
// Create from the supplied text style.
init(FormatStr text, TextStyle style) {
init() {
text = text.text(style.font);
brush = style.fill;
}
}
// Minimum size.
Size minSize() { text.size; }
// Draw.
void draw(Graphics g) {
var p = pos;
Point center((p.size.w - text.size.w) / 2, 0);
g.draw(text, brush, p.p0 + center);
}
}
Heading heading(Presentation p, FormatStr text) on Render { Heading(text, p.headingStyle); }
Heading subHeading(Presentation p, FormatStr text) on Render { Heading(text, p.contentStyle); }
/**
* A paragraph of text. Fits the text to the given rectangle.
*
* Use 'par' for easy creation of paragraphs.
*/
class Paragraph extends Element {
// Pre-formatted text.
Text text;
// Brush used to fill the text.
Brush brush;
// Set color.
void fill(Color color) {
brush = SolidBrush(color);
}
// Create.
init(FormatStr text, Font f, Brush brush) {
init() {
text = text.text(f);
brush = brush;
}
}
// Minimum size (TODO: Can we do better somehow?)
Size minSize() { text.size; }
// Set size.
assign pos(Rect r) : override {
super:pos(r);
text.layoutBorder = r.size;
}
// Draw.
void draw(Graphics g) {
g.draw(text, brush, pos.p0);
}
}
// Create a paragraph, specifying a solid color.
Paragraph par(FormatStr t, Font f, Color c) on Render { Paragraph(t, f, SolidBrush(c)); }
// Create a paragraph with a pre-defined style.
Paragraph par(FormatStr t, TextStyle style) on Render { Paragraph(t, style.font, style.fill); }
// Create a paragraph with the default style.
Paragraph par(Presentation p, FormatStr t) on Render { par(t, p.contentStyle); }
/**
* A list of strings, each with a bullet point to the left.
*/
class List extends Element {
// Text elements.
private Text[] elements;
// Height of the font.
private Float fontHeight;
// Brush used to fill the text.
Brush brush;
// Size of the bullets.
Float bulletSize;
// Space from the left margin of this element to the start of the text.
Float margin;
// Space between the text and the bullet or the number.
Float padding;
// Space between lines.
Float lineSpace;
// Pre-formatted text snippnets used if the list is numbered.
private Text[] numbers;
// Cached base-lines of all elements. Used to format bullets properly.
private Float[] baselines;
// Create.
init(Str[] text, Font f, Float lineSpace, Brush brush) {
FormatStr[] fmt;
for (t in text)
fmt << FormatStr(t);
self(fmt, f, lineSpace, brush);
}
init(FormatStr[] text, Font f, Float lineSpace, Brush brush) {
init() {
brush = brush;
fontHeight = f.pxHeight;
margin = f.pxHeight / 2;
padding = f.pxHeight / 3;
bulletSize = f.pxHeight / 4;
lineSpace = lineSpace;
}
for (x in text) {
Text t = x.text(f);
elements << t;
baselines << t.lineInfo[0].baseline;
}
}
// Create numbers.
void ordered() {
numbers.clear();
if (elements.empty)
return;
Font f = elements.first.font;
for (Nat i = 0; i < elements.count; i++) {
Text t("${i + 1}.", f);
numbers << t;
margin = max(margin, t.size.w + padding);
}
}
// Create alphabetical letters.
void letters() {
numbers.clear();
if (elements.empty)
return;
Font f = elements.first.font;
for (Nat i = 0; i < elements.count; i++) {
Text t(Char(65 + i).toS + ":", f);
numbers << t;
margin = max(margin, t.size.w + padding);
}
}
// Minimum size
Size minSize() {
Size sz;
for (e in elements) {
Size s = e.size;
sz.w = max(sz.w, s.w);
sz.h += s.h + lineSpace;
}
sz.h -= lineSpace;
sz.w += margin;
sz;
}
// Set size.
assign pos(Rect r) : override {
super:pos(r);
Size s = r.size;
s.w -= margin;
for (e in elements) {
e.layoutBorder = s;
}
}
// Draw.
void draw(Graphics g) {
Point at = pos.p0;
for (i, e in elements) {
if (i >= numbers.count) {
// Point delta(margin - padding - bulletSize, (fontHeight - bulletSize) * 2 / 3);
Point delta(margin - padding - bulletSize, baselines[i]*0.65 - bulletSize*0.5);
Rect ball(at + delta, Size(bulletSize));
g.fillOval(ball, brush);
} else {
Size text = numbers[i].size;
g.draw(numbers[i], brush, at + Point(margin - padding - text.w, 0));
}
g.draw(e, brush, at + Point(margin, 0));
at.y += e.size.h + lineSpace;
}
}
}
// Create a list, specifying a solid color.
List list(Str[] t, Font f, Color c) on Render { List(t, f, 0, SolidBrush(c)); }
List list(FormatStr[] t, Font f, Color c) on Render { List(t, f, 0, SolidBrush(c)); }
// Create a list with a pre-defined style.
List list(Str[] t, TextStyle style) on Render { List(t, style.font, style.space, style.fill); }
List list(FormatStr[] t, TextStyle style) on Render { List(t, style.font, style.space, style.fill); }
// Create a list with the default style.
List list(Presentation p, Str[] t) on Render { list(t, p.contentStyle); }
List list(Presentation p, FormatStr[] t) on Render { list(t, p.contentStyle); }
/**
* An image.
*/
class Image extends Element {
// The bitmap we're painting.
private Bitmap image;
// Fill mode: 0 = scale according to scale factor, 1 = fit to container, 2 = fill container (no borders)
private Nat mode;
// Scale. Only relevant in mode 0.
private Float fixScale;
// Create.
init(Bitmap image) {
init() {
image = image;
mode = 1;
fixScale = 1.0;
}
}
// Set an explicit scale.
void scale(Float scale) {
fixScale = scale;
mode = 0;
}
// Fit to the container without clipping (default).
void fit() {
mode = 1;
}
// Fill the container (maybe clips).
void fill() {
mode = 2;
}
// Minimum size.
Size minSize() {
image.size * fixScale;
}
// Draw.
void draw(Graphics g) {
Size imageSz = image.size;
Size size = pos.size;
Point center = pos.center;
Float scale = fixScale;
if (mode == 1) {
scale = min(size.w / imageSz.w, size.h / imageSz.h);
} else if (mode == 2) {
scale = max(size.w / imageSz.w, size.h / imageSz.h);
}
g.push();
g.transform = core:geometry:scale(scale) * translate(center);
g.draw(image, -Point(imageSz/2));
g.pop();
}
}
// Create an image.
Image image(Bitmap image) on Render { Image(image); }
// Create from an Image instance.
Image image(graphics:Image image) on Render { Image(Bitmap(image)); }
// Create from an Url. TODO: Perhaps we want to de-duplicate these!
Image image(Url url) on Render { Image(Bitmap(url.loadImage)); }
/**
* Page numbering.
*/
class PageNumber extends Element {
// Font.
private Font font;
// Fill.
private Brush fill;
// Margin, to make this element easy to position inside an anchor element.
Size border;
void border(Float w, Float h) {
border = Size(w, h);
}
// Offset.
Nat offset;
// Maximum size.
private Size maxSize;
// Cache of text objects to draw.
private Nat->Text cache;
// Create.
init(TextStyle style) {
init() {
font = style.font;
fill = style.fill;
offset = 1;
border = Size(20, 15);
}
maxSize = Text("99", font).size;
}
Size minSize() : override {
maxSize + border*2;
}
// We need access to 'step'.
void draw(Graphics g, Nat step, Duration time) : override {
if (before(g, step, time)) {
Text t = find(step);
Point at = pos.p1 - t.size - border;
g.draw(t, fill, at);
}
after(g, step, time);
}
void draw(Graphics g) : override {}
private Text find(Nat id) {
id += offset;
if (cached = cache.at(id))
return cached;
Text t(id.toS, font);
cache.put(id, t);
t;
}
}
// Create page numbering conveniently.
PageNumber pageNumber(Presentation p) {
PageNumber(p.contentStyle);
}
/**
* Big arrow.
*/
class BigArrow extends Element {
// Fill color.
Brush brush;
void fill(Color color) { brush = SolidBrush(color); }
// Angle.
Angle angle;
// Size.
Float size;
// Arrow path.
private Path arrow;
// Initialize, specify direction.
init(Angle angle) {
init() {
angle = angle;
size = 40;
brush = SolidBrush(black);
}
arrow.start(Point(0, -0.8));
arrow.line(Point(1, 0.1));
arrow.line(Point(0.4, 0.0));
arrow.line(Point(0.4, 0.7));
arrow.line(Point(0, 0.5));
arrow.line(Point(-0.4, 0.7));
arrow.line(Point(-0.4, 0.0));
arrow.line(Point(-1, 0.1));
arrow.close();
}
// Minimum size.
Size minSize() : override {
Size(size);
}
// Draw.
void draw(Graphics g) : override {
g.push();
g.transform = scale(size/2) * rotate(angle) * translate(pos.center);
g.fill(arrow, brush);
g.pop();
}
}
/**
* Line, good for using together with Grid to make tables.
*/
class HLine extends Element {
init(Brush draw) {
init() { draw = draw; width = 1.0; }
}
init(Color draw) {
init() { draw = SolidBrush(draw); width = 1.0; }
}
// Color.
Brush draw;
// Line width.
Float width;
// Minimum length of the line.
Float minLength;
// Minimum size.
Size minSize() : override {
Size(minLength, width);
}
// Draw.
void draw(Graphics g) : override {
Rect p = pos;
g.push();
g.lineWidth = width;
g.line(p.p0, p.p0 + Size(p.size.w, 0), draw);
g.pop();
}
}
/**
* Line, good for using together with Grid to make tables.
*/
class VLine extends Element {
init(Brush draw) {
init() { draw = draw; width = 1.0; }
}
init(Color draw) {
init() { draw = SolidBrush(draw); width = 1.0; }
}
// Color.
Brush draw;
// Line width.
Float width;
// Minimum length of the line.
Float minLength;
// Minimum size.
Size minSize() : override {
Size(width, minLength);
}
// Draw.
void draw(Graphics g) : override {
Rect p = pos;
g.push();
g.lineWidth = width;
g.line(p.p0, p.p0 + Size(0, p.size.h), draw);
g.pop();
}
}
|