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 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
|
use layout;
use core:geometry;
use graphics;
use ui;
/**
* Custom layouts that are useful when creating presentations.
*/
/**
* Custom root element. Adds a border, but a different size than layout:Border, and allows different
* size on different edges.
*/
class SlideBorder extends layout:Border {
init(Presentation p) {
init() {}
topLeft = p.topLeftBorder;
bottomRight = p.bottomRightBorder;
}
// The one and only component inside here.
private Component? child;
// Size of the border.
Size topLeft;
Size bottomRight;
// Set border size.
void border(Float w, Float h) {
topLeft = bottomRight = Size(w, h);
}
// Add/set the child.
Layout:Info add(Component c) {
child = c;
Layout:Info();
}
// Compute the minimum size.
Size minSize() {
Size s;
if (c = child)
s = c.minSize;
s + topLeft + bottomRight;
}
// Re-compute the layout.
void layout() {
unless (c = child)
return;
Rect p = pos;
p.p0 += topLeft;
p.p1 -= bottomRight;
c.pos = p;
}
// Traverse.
void findAll(fn(Component)->void fn) : override {
super:findAll(fn);
if (child)
child.findAll(fn);
}
}
/**
* A simple layout that handles a caption and content.
*/
class CaptionLayout extends layout:Layout {
// First component, the caption.
private Component? caption;
// Second component, the content.
private Component? content;
// Alignment of the caption.
Cardinal align;
// Space below the caption.
Float space;
// Additional margin of the content.
Float contentLeftMargin;
Float contentRightMargin;
// Create.
init() {
init() {
// Default spacing.
space = 10;
align = center;
contentLeftMargin = 0;
contentRightMargin = 0;
}
}
// Add a component.
Layout:Info add(Component c) : override {
unless (caption) {
this.caption = c;
return Layout:Info();
}
content = c;
Layout:Info();
}
// Compute the minimum size.
Size minSize() : override {
Size sz;
if (caption) {
sz = caption.minSize;
sz.h += space;
}
if (content) {
Size s = content.minSize;
s.w += contentLeftMargin + contentRightMargin;
sz.w = max(sz.w, s.w);
sz.h += s.h;
}
sz;
}
// Re-compute the layout.
void layout() : override {
Rect p = pos;
if (caption) {
Size s = caption.minSize();
caption.pos = align.align(Rect(p.p0.x, p.p0.y, p.p1.x, p.p0.y + s.h), s);
p.p0.y += s.h + space;
}
if (content) {
p.p0.x += contentLeftMargin;
p.p1.x -= contentRightMargin;
content.pos = p;
}
}
// Traverse.
void findAll(fn(Component)->void fn) : override {
super:findAll(fn);
if (caption)
caption.findAll(fn);
if (content)
content.findAll(fn);
}
}
/**
* Put things in columns. The columns will always be the same size, regardless of their content.
*/
class ColumnLayout extends layout:Layout {
private Component[] components;
// Space between columns.
Float padding;
// Create.
init() {
init() { padding = 10; }
}
// Add a component.
Layout:Info add(Component c) : override {
components << c;
Layout:Info();
}
// Minimum size.
Size minSize() : override {
Size m(-padding, 0);
for (c in components) {
var s = c.minSize();
m.w += padding + s.w;
m.h = max(m.h, s.h);
}
m;
}
// Re-compute the layout.
void layout() : override {
Rect p = pos;
Float step = (p.size.w + padding) / components.count.int.float;
Size partSize(step - padding, p.size.h);
for (i, c in components) {
Point p0 = p.p0;
p0.x += step * i.int.float;
c.pos = Rect(p0, partSize);
}
}
// Traverse.
void findAll(fn(Component)->void fn) : override {
super:findAll(fn);
for (c in components)
c.findAll(fn);
}
}
/**
* A layout for putting each element below the previous one. Does not attempt to fill the remaining
* space below all elements. A simpler alternative to the grid layout, possibly combined with anchor
* elements.
*/
class RowLayout extends layout:Layout {
private Component[] components;
private Cardinal anchor;
// Space between rows.
Float padding;
// Create.
init() {
init() {
padding = 10;
anchor = north;
}
}
// Align elements to the left.
void left() {
anchor = northWest;
}
// Align elements to the right.
void right() {
anchor = northEast;
}
// Align elements to the center (default).
void center() {
anchor = north;
}
// Add a component.
Layout:Info add(Component c) : override {
components << c;
Layout:Info();
}
// Minimum size.
Size minSize() : override {
Size m(0, -padding);
for (c in components) {
var s = c.minSize();
m.h += padding + s.h;
m.w = max(m.w, s.w);
}
m;
}
// Re-compute the layout.
void layout() : override {
Rect p = pos;
Float y = p.p0.y;
for (c in components) {
var s = c.minSize();
s.w = min(s.w, p.size.w);
s.h = min(s.h, p.p1.y - y);
Rect box(Point(p.p0.x, y), Point(p.p1.x, y + s.h));
c.pos = anchor.align(box, s);
y += s.h + padding;
}
}
// Traverse.
void findAll(fn(Component)->void fn) : override {
super:findAll(fn);
for (c in components)
c.findAll(fn);
}
}
/**
* Legacy alias for the anchor layout.
*/
class AnchorLayout extends layout:Anchor {
// Create.
init(Cardinal anchor) {
init(anchor) {}
}
}
// Create anchor layout easily.
AnchorLayout anchor(Cardinal c) {
AnchorLayout(c);
}
/**
* Group an entire sub-layout into one element, so that animations can be applied to the entire
* group rather than individual elements.
*
* Extending this class allows modifiers, such as 'WithBackground' below, to be implemented conveniently.
*/
class Group extends layout:Layout {
// Components managed inside this layout.
protected Component[] children;
// The "real" element we show to the presentation system.
private Elem elem;
// Add an element.
Layout:Info add(Component c) : override {
children << c;
Layout:Info();
}
// Get our minimum size.
Size minSize() : override {
Size s;
for (c in children)
s = max(s, c.minSize);
s;
}
// Do layout. Override and call 'layout(Rect)' to modify the layout if desired.
void layout() : override {
layout(pos);
}
// Call from derived classes to add borders etc.
void layout(Rect pos) {
for (c in children) {
c.pos = pos;
}
elem.pos = pos;
}
// Add an animation.
void animation(Animation a) {
elem.animation(a);
}
// Find all child elements (we won't propagate them since we handle that ourselves).
void findAll(fn(Component)->void fn) {
super:findAll(fn);
for (c in children) {
c.findAll(&elem.addChild);
}
ElementWrap(elem).findAll(fn);
}
/**
* Element that is seen by the presentation.
*/
class Elem extends Element {
// Child elements.
private Element[] children;
// Minimum size. Not really needed.
Size minSize() : override { Size(); }
// Initialize animations.
void setupAnimations(Presentation p) {
super:setupAnimations(p);
for (c in children)
c.setupAnimations(p);
}
// Setup repaint.
void setupRepaint(Fn<void> callback) {
for (c in children)
c.setupRepaint(callback);
}
// Cleanup.
void cleanup() {
for (c in children)
c.cleanup();
}
// Extract animations.
Animation[] animations() : override {
var here = Animation[](super:animations());
for (c in children)
here.append(c.animations());
here;
}
// Low-level drawing.
void draw(Graphics g, Nat step, Duration time) : override {
if (before(g, step, time)) {
for (c in children) {
c.draw(g, step, time);
}
}
after(g, step, time);
}
// Regular drawing (not required).
void draw(Graphics g) : override {}
// Add a child.
void addChild(Component e) {
if (e as ElementWrap)
children << e.control;
}
}
}
/**
* Layout that draws a background behind an element.
*/
class WithBackground extends Group {
// The solid-fill component that paints the background.
private SolidFill background;
// Margin to add.
private Size margin;
// Create.
init(Color color) {
init() {
background = SolidFill(color);
margin = Size(5, 5);
}
add(ElementWrap(background));
}
// Set the margin.
void margin(Float w, Float h) {
margin = Size(w, h);
}
// Set the background color.
void background(Color c) {
background.border(c);
}
// Minimum size.
Size minSize() : override {
super:minSize() + margin*2;
}
// Re-compute the layout.
void layout() : override {
Rect p = pos;
p.p0 += margin;
p.p1 -= margin;
super:layout(p);
background.pos = pos;
}
}
/**
* Layout that adds a border to another element.
*/
class WithBorder extends Group {
// Margin to apply to the element.
private Size margin;
// Border element.
private Border border;
// Create.
init(Color color) {
init() {
margin = Size(5, 5);
border(SolidBrush(color));
}
add(ElementWrap(border));
}
// Create, specify brush.
init(Brush brush) {
init() {
margin = Size(5, 5);
border(brush);
}
add(ElementWrap(border));
}
// Set the margin.
void margin(Float w, Float h) {
margin = Size(w, h);
}
// Set the border color.
void border(Color c) {
border(SolidBrush(c));
}
void border(Brush brush) {
border.setBrush(brush);
}
// Set the line width.
void width(Float w) {
border.setWidth(w);
}
// Minimum size.
Size minSize() : override {
super:minSize() + margin*2;
}
// Re-compute layout.
void layout() : override {
Rect p = pos;
p.p0 += margin;
p.p1 -= margin;
super:layout(p);
border.pos = pos;
}
private class Border extends Element {
// Brush to use.
Brush brush;
// Line width.
Float width;
// Create.
init(Brush brush) {
init() {
brush = brush;
width = 1;
}
}
// Min size.
Size minSize() {
Size(width);
}
// Draw.
void draw(Graphics g) : override {
g.push();
g.lineWidth(width);
g.draw(pos, brush);
g.pop();
}
void setBrush(Brush b) {
brush = b;
}
void setWidth(Float w) {
width = w;
}
}
}
/**
* Layout that adds a caption to another element.
*/
class WithCaption extends Group {
// Caption text.
private Heading text;
// Space between text and the content.
private Float space;
// Direction of the text.
private Cardinal textDir;
// Create.
init(FormatStr text, Cardinal where, TextStyle font) {
init() {
text = Heading(text, font);
space = font.space;
textDir = where;
}
add(ElementWrap(this.text));
}
// Compute minimum size.
Size minSize() {
Size s;
for (i, c in children)
if (i > 0)
s = max(s, c.minSize());
Size textSz = text.minSize;
s.w = max(s.w, textSz.w);
s.h += space + textSz.h;
s;
}
// Re-compute the layout.
void layout() {
Rect p = pos;
Point dir = textDir.direction;
Size textSize = text.minSize;
Rect textRect = p;
Rect contentRect = p;
if (dir.y > 0) {
textRect.p0.y = p.p1.y - textSize.h;
contentRect.p1.y = textRect.p0.y - space;
} else {
textRect.p1.y = p.p0.y + textSize.h;
contentRect.p0.y = textRect.p1.y + space;
}
super:layout(contentRect);
text.pos = textDir.align(textRect, textSize);
}
}
|