Pregunta

Can anyone help me get this to run? I'm aiming for a custom Actor. (I have only just started hacking with Vala in the last few days and Clutter is a mystery too.)

The drawme method is being run (when invalidate is called) but there doesn't seem to be any drawing happening (via the Cairo context).

ETA: I added one line in the constructor to show the fix - this.set_size.

/*

Working from the sample code at:
https://developer.gnome.org/clutter/stable/ClutterCanvas.html

*/

public class AnActor : Clutter.Actor {

    public Clutter.Canvas canvas;

    public AnActor() {
        canvas = new Clutter.Canvas();
        canvas.set_size(300,300);

        this.set_content( canvas );
        this.set_size(300,300);

        //Connect to the draw signal.
        canvas.draw.connect(drawme);
    }

    private bool drawme( Cairo.Context ctx, int w, int h) {
        stdout.printf("Just to test this ran at all: %d\n", w);

        ctx.scale(w,h);
        ctx.set_source_rgb(0,0,0);

        //Rect doesn't draw.
        //ctx.rectangle(0,0,200,200);
        //ctx.fill();

        //paint doesn't draw.
        ctx.paint();

        return true;
    }
}



int main(string [] args) {
    // Start clutter.
    var result = Clutter.init(ref args);
    if (result != Clutter.InitError.SUCCESS) {
        stderr.printf("Error: %s\n", result.to_string());
        return 1;
    }

    var stage = Clutter.Stage.get_default();
    stage.destroy.connect(Clutter.main_quit);

    //Make my custom Actor:
    var a = new AnActor();

    //This is dodgy:
    stage.add_child(a);


    //This works:
    var r1 = new Clutter.Rectangle();
    r1.width = 50;
    r1.height = 50;
    r1.color = Clutter.Color.from_string("rgb(255, 0, 0)");
    stage.add_child(r1);

    a.canvas.invalidate();

    stage.show_all();

    Clutter.main();
    return 0;
}
¿Fue útil?

Solución

you need to assign a size to the Actor as well, not just the Canvas.

the size of the Canvas is independent of the size of the Actor to which the Canvas is assigned to, as you can assign the same Canvas instance to multiple actors.

if you call:

a.set_size(300, 300)

you will see the actor and the results of the drawing.

Clutter also ships with various examples, for instance how to make a rectangle with rounded corners using Cairo: https://git.gnome.org/browse/clutter/tree/examples/rounded-rectangle.c - or how to make a simple clock: https://git.gnome.org/browse/clutter/tree/examples/canvas.c

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top