«Chopsticks GUI» by ludions

on 09 Sep'13 18:12 in gui viewsgui interaction

A GUI example of nesting Views and multiplying widgets, including mouse actions and Pen drawing for each of the views. Thanks to hjh for spotting an early bug on sc-users. Add your own sound...

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
(
var numX=12, numY=8, width, height, margin;
w = Window.new("CompositeView", Window.availableBounds).front;
width = (Window.availableBounds.width/numX).floor;
height = (Window.availableBounds.height/numY).floor;
margin = (width + height)/20;
w.addFlowLayout(0@0, 0@0);
t = Array.fill(numX * numY, { arg i;
    var u, n, col, moveTo, lineTo, toggle;
    col = Color.rand;
    toggle = i%2;
    n = CompositeView.new(w, Rect(0, 0, width, height))
        .background_(col);
    u=UserView(n, Rect(margin, margin, width-(margin*2), height-(margin*2))); // OK
    u.background_(col);
    u.drawFunc={|uview|
        if(toggle.asBoolean, {
            moveTo = 0@uview.bounds.height.rand;
            lineTo = uview.bounds.width@uview.bounds.height.rand;
        }, {
            moveTo = uview.bounds.width.rand@uview.bounds.height;
            lineTo = uview.bounds.width.rand@0;
         });
        Pen.moveTo(moveTo);
        Pen.lineTo(lineTo);
        Pen.stroke;
    };
    u.mouseDownAction={|view|
        col = Color.rand;
        u.background_(col);
        n.background_(col);
        toggle = (toggle - 1).abs;
    };
    u.animate_(true);
});
w.front;
)
raw 1200 chars (focus & ctrl+a+c to copy)
reception
fun
comments