[Vala] Warning while casting custom classes



Hi guys, i'm currently writing a simple drawing program using Cairo, Vala and Gtk (just for fun, nothing serious), named Picasso (due to my lack of imagination). At the moment the program is able to draw lines and rectangles, just by clicking two times (one for the origin and one for the destination) on the canvas. I also implemented a kind of preview, when you click the first time, moving the cursor will show how the shape will look. To do this i'm using a couple of classes: PicassoLine and PicassoRectangle which are subclasses of PicassoItem. PicassoItem was an abstract class, but then i needed to switch it to a class mainly because i don't know if the user is going draw a line or a rectangle or whatever.
Here the classes:

#######################################################
namespace Picasso {
    public enum ItemType {
        NONE,
        LINE,
        RECTANGLE,
        CIRCLE;

        public static ItemType[] get_all () {
            return { LINE, RECTANGLE, CIRCLE };
        }
    }

    public class PicassoItem {
        public int x;
        public int y;

        public ItemType type;

        public PicassoItem (int x, int y, ItemType type) {
            this.x = x;
            this.y = y;
            this.type = type;
        }
    }

    public class PicassoLine : PicassoItem {
        public int dx;
        public int dy;

        public void draw (Cairo.Context cr) {
            cr.new_path ();
            cr.move_to (x, y);
            cr.line_to (dx, dy);
            cr.stroke ();
        }

        public PicassoLine (int x, int y, int dx, int dy) {
            base (x, y, ItemType.LINE);
            this.dx = dx;
            this.dy = dy;
        }
    }

    public class PicassoRectangle : PicassoItem {
        public int width;
        public int height;

        public void draw (Cairo.Context cr, bool fill) {
            cr.new_path ();
            cr.rectangle (x, y, width, height);
            if (fill) {
                cr.fill ();
            } else {
                cr.stroke ();
            }
        }

        public PicassoRectangle (int x, int y, int width, int height) {
            base (x, y, ItemType.RECTANGLE);
            this.width = width;
            this.height = height;
        }
    }

    public class PicassoCircle : PicassoItem {
        public int radius;

        public void draw (Cairo.Context cr) {
            cr.new_path ();
            cr.arc (x, y, radius, 0, 360 * (Math.PI / 180.0));
            cr.stroke ();
        }

        public PicassoCircle (int x, int y, int radius) {
            base (x, y, ItemType.LINE);
            this.radius = radius;
        }
    }
}
#######################################################

When i draw a preview, i check for which shape is the user drawing and then cast it to the correct type (Line or Rectangle) to draw it, here:

#######################################################
        public void on_preview_drawed (int x1, int y1, int x2, int y2) {
            switch (active_item) { // Current selected shape
                case ItemType.LINE:
                    canvas.preview.type = ItemType.LINE;
                    canvas.preview.x = x1;
                    canvas.preview.y = y1;
                    ((PicassoLine) canvas.preview).dx = x2;
                    ((PicassoLine) canvas.preview).dy = y2;
                    break;
                case ItemType.RECTANGLE:
                    canvas.preview.type = ItemType.RECTANGLE;
                    canvas.preview.x = x1;
                    canvas.preview.y = y1;
                    ((PicassoRectangle) canvas.preview).width = x2-x1;
                    ((PicassoRectangle) canvas.preview).height = y2-y1;
                    break;
            }

            canvas.update_canvas ();
        }
#######################################################

When casting to PicassoLine or PicassoRectangle it pop up:
(Picasso:10091): GLib-GObject-WARNING **: invalid cast from `PicassoPicassoItem' to `PicassoPicassoRectangle' (or PicassoPicassoLine)

I know is not simple to understand without all the code (if you want i can upload it somewhere), but maybe is just an implementation error.

The preview works flawlessy anyway.

Regards,
Stephen Smally.




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]