~ella-animation/ella/trunk

« back to all changes in this revision

Viewing changes to lib/libellacore/common/ellacore-item.vala

  • Committer: xapantu
  • Date: 2011-06-06 16:55:22 UTC
  • Revision ID: xapantu@gmail.com-20110606165522-x3qwmjtalm9lqai7
Bezier curves

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
 
38
38
public class Ella.Point : GLib.Object
39
39
{
40
 
    public double x;
41
 
    public double y;
 
40
    double x_;
 
41
    double y_;
 
42
    public double x
 
43
    {
 
44
        set { x2 += value - x; x1 += value - x; x_ = value; }
 
45
        get { return x_; }
 
46
    }
 
47
    public double y
 
48
    {
 
49
        set { y1 += value - y_; y2 += value - y_; y_ = value; }
 
50
        get { return y_; }
 
51
    }
 
52
    public double x1;
 
53
    public double x2;
 
54
    public double y1;
 
55
    public double y2;
42
56
    public Point(double x, double y)
43
57
    {
44
 
        this.x = x;
45
 
        this.y = y;
 
58
        x1 = x2 = this.x_ = x;
 
59
        y1 = y2 = this.y_ = y;
46
60
    }
 
61
 
47
62
    public Rect rect
48
63
    {
49
64
        get
56
71
            return rect;
57
72
        }
58
73
    }
 
74
    public Rect rect2
 
75
    {
 
76
        get
 
77
        {
 
78
            Rect rect = Rect();
 
79
            rect.x = x2 - 5;
 
80
            rect.y = y2 - 5;
 
81
            rect.width = 10;
 
82
            rect.height = 10;
 
83
            return rect;
 
84
        }
 
85
    }
 
86
    public Rect rect1
 
87
    {
 
88
        get
 
89
        {
 
90
            Rect rect = Rect();
 
91
            rect.x = x1 - 5;
 
92
            rect.y = y1 - 5;
 
93
            rect.width = 10;
 
94
            rect.height = 10;
 
95
            return rect;
 
96
        }
 
97
    }
 
98
 
 
99
 
59
100
}
60
101
 
61
102
public bool RectIsIn(double x, double y, Ella.Rect rect1)
1092
1133
        }
1093
1134
    }
1094
1135
 
 
1136
    public void button_press_event(Gdk.EventButton event)
 
1137
    {
 
1138
        switch(event.button)
 
1139
        {
 
1140
        case 1:
 
1141
            select_point_at(event.x, event.y);
 
1142
            break;
 
1143
        case 3:
 
1144
            select_point_at(event.x, event.y);
 
1145
            points[point_selected].x1 -= 10;
 
1146
            points[point_selected].y1 -= 10;
 
1147
            points[point_selected].x2 += 10;
 
1148
            points[point_selected].y2 += 10;
 
1149
            break;
 
1150
        }
 
1151
    }
 
1152
 
 
1153
    public void motion_notify_event(Gdk.EventMotion event)
 
1154
    {
 
1155
        move_point(event.x, event.y);
 
1156
    }
 
1157
 
1095
1158
    public void move_point(double event_x, double event_y)
1096
1159
    {
1097
1160
        event_x = event_x / width;
1098
1161
        event_y = event_y / height;
1099
 
        points[point_selected].x = event_x;
1100
 
        points[point_selected].y = event_y;
 
1162
        switch(point_select)
 
1163
        {
 
1164
        case 0:
 
1165
            points[point_selected].x = event_x;
 
1166
            points[point_selected].y = event_y;
 
1167
            break;
 
1168
        case 1:
 
1169
            points[point_selected].x1 = event_x;
 
1170
            points[point_selected].y1 = event_y;
 
1171
            break;
 
1172
        case 2:
 
1173
            points[point_selected].x2 = event_x;
 
1174
            points[point_selected].y2 = event_y;
 
1175
            break;
 
1176
         
 
1177
        }
1101
1178
    }
1102
1179
 
 
1180
    int point_select;
 
1181
 
1103
1182
    public void select_point_at(double event_x, double event_y)
1104
1183
    {
1105
 
        event_x = (event_x - x) / width;
1106
 
        event_y = (event_y - y) / height;
 
1184
        event_x = (event_x) / width;
 
1185
        event_y = (event_y) / height;
 
1186
        point_select = 0;
1107
1187
        foreach(Point point in points)
1108
1188
        {
1109
1189
            if(RectIsIn(event_x, event_y, point.rect))
1110
1190
            {
1111
1191
                select_point(point);
 
1192
                point_select = 0;
 
1193
                return;
 
1194
            }
 
1195
            else if(RectIsIn(event_x, event_y, point.rect1))
 
1196
            {
 
1197
                select_point(point);
 
1198
                point_select = 1;
 
1199
                return;
 
1200
            }
 
1201
            else if(RectIsIn(event_x, event_y, point.rect2))
 
1202
            {
 
1203
                select_point(point);
 
1204
                point_select = 2;
1112
1205
                return;
1113
1206
            }
1114
1207
        }
1127
1220
        cr.translate(x - x_root, y - y_root);
1128
1221
        cr.scale(width, height);
1129
1222
        cr.set_line_width(line_width);
 
1223
        double old_point_x = -1;
 
1224
        double old_point_y = -1;
1130
1225
        foreach(Point point in points)
1131
1226
        {
1132
 
            cr.line_to(point.x, point.y);
 
1227
            if(old_point_x >= 0)
 
1228
            {
 
1229
                cr.curve_to(old_point_x, old_point_y, point.x1, point.y1, point.x, point.y);
 
1230
            }
 
1231
            else
 
1232
            {
 
1233
                cr.line_to(point.x, point.y);
 
1234
            }
 
1235
            old_point_x = point.x2;
 
1236
            old_point_y = point.y2;
1133
1237
        }
1134
1238
        cr.close_path();
1135
1239
        cr.set_source_rgba(red, green, blue, alpha);
1148
1252
        foreach(Point item in points)
1149
1253
        {
1150
1254
            var rect = item.rect;
 
1255
            cr.set_source_rgb(0,0,0);
1151
1256
            cr.rectangle(rect.x * width, rect.y * height, rect.width, rect.height);
1152
1257
            cr.fill();
 
1258
            cr.set_source_rgba(0,0,0,0.5);
 
1259
            cr.rectangle(item.x1, item.y1, rect.width, rect.height);
 
1260
            cr.fill();
 
1261
            cr.rectangle(item.x2, item.y2, rect.width, rect.height);
 
1262
            cr.fill();
1153
1263
        }
1154
1264
        var rect = points[point_selected].rect;
1155
1265
        cr.rectangle(rect.x * width, rect.y * height, rect.width, rect.height);