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
|
use core:geometry;
/**
* Describes a layout for a number of components.
*
* Layouts can be nested inside each other to describe more complex interactions. If a layout
* requires to maintain additional information about each component, it should contain a class named
* 'Info' for that purpose, and 'add' should return an instance associated with the newly added component.
*/
class Layout extends Component {
// Our allotted space. Updated in the default implementation of `pos(Rect)`.
protected Rect space;
// Information on a child.
class Info {}
// Get the position of the layout.
Rect pos() {
space;
}
// Set the allocated space. This will trigger a re-layout.
assign pos(Rect p) {
space = p;
layout();
}
// Get the minimum size of the layout.
Size minSize() {
Size(0, 0);
}
// Re-compute the layout here. Called automatically when the position is updated.
void layout() : abstract;
// Add a child to this layout. Returns a class that contains information about the child added.
// The caller is expected to be able to modify this information to alter the layout.
Info add(Component child) {
Info();
}
// Make sure derived classes override 'findAll'. We provide a body so that it is callable as super:findAll().
void findAll(fn(Component)->void fn) : abstract {
super:findAll(fn);
}
}
// Default border/space in the Ui.
Size defaultBorder() {
Size(8, 8);
}
/**
* A simple layout that adds a border around the single component within.
*/
class Border extends Layout {
// The one and only component inside here.
private Component? child;
// Size of the border.
Size border;
// Initialize.
init() {
init() { border = defaultBorder; }
}
// Set the border size.
void border(Float w, Float h) {
border = 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 + border*2;
}
// Re-compute the layout.
void layout() {
unless (c = child)
return;
Rect p = pos;
p.p0 += border;
p.p1 -= border;
c.pos = p;
}
// Traverse.
void findAll(fn(Component)->void fn) : override {
super:findAll(fn);
if (child)
child.findAll(fn);
}
}
/**
* A simple layout that stacks components on top of each other.
*/
class Stack extends Layout {
// Our components.
private Component[] components;
// Get our minimum size.
Size minSize() : override {
Size s;
for (c in components) {
s = max(s, c.minSize);
}
s;
}
// Do layout.
void layout() : override {
Rect p = pos;
for (c in components) {
c.pos = p;
}
}
// Add a child to the layout.
Layout:Info add(Component c) {
components << c;
Layout:Info();
}
// Find all elements.
void findAll(fn(Component)->void fn) {
super:findAll(fn);
for (c in components) {
c.findAll(fn);
}
}
}
/**
* A layout that fits the contained element according to a cardinal direction.
*/
class Anchor extends Layout {
// Our component.
private Component? component;
// Where do we place the component?
private Cardinal anchor;
// Shift the origin.
Float xShift;
Float yShift;
// Create.
init(Cardinal anchor) {
init() {
anchor = anchor;
}
}
// Add a component.
Layout:Info add(Component c) : override {
component = c;
Layout:Info();
}
// Minimum size.
Size minSize() : override {
if (component) {
component.minSize;
} else {
Size();
}
}
// Re-compute the layout.
void layout() : override {
if (component) {
Rect p = pos;
Point a = anchor.pick(p) + Size(xShift, yShift);
Size sz = min(component.minSize(), p.size);
component.pos = anchor.place(a, sz);
}
}
// Traverse.
void findAll(fn(Component)->void fn) : override {
super:findAll(fn);
if (component)
component.findAll(fn);
}
}
|