![]() |
![]() |
![]() |
libccc Reference Manual | ![]() |
---|
To be able to add interaction to your items, the canvas needs to figure out whether or not a point is within the item. This is important for two reasons:
An item doesn't have to be rectangular: if you have a circle, you don't want the circle to be treated like a rectangle (eg. accept clicks to the part of the bounding box which isn't part of a circle).
Items might have transparent areas. If you have a rectangular frame which doesn't have any content, you usually want to forward clicks that happen inside of the frame to the item that has actually been meant by the user.
Hit-Testing can be implemented by connecting to the CcSimpleItem::distance signal:
static gdouble distance_to_item (CcItem * item, CcView * view, gdouble x, gdouble y, CcItem **found, gpointer user_data) { if ((x < 0.0 || x > 100.0) && (y < 0.0 || y > 100.0)) { // completely outside if (x < 0.0 && y < 0.0) { return hypot (0.0 - x, 0.0 - y); } else if (x < 0.0) { return hypot (0.0 - x, y - 100.0); } else if (x > 100.0 && y < 0) { return hypot (x - 100.0, 0.0 - y); } else { return hypot (x - 100.0, y - 100.0); } } if (x < 0.0) { return 0.0 - x; } if (x > 100.0) { return x - 100.0; } if (y < 0.0) { return 0.0 - y; } if (y > 100.0) { return x - 100.0; } *found = item; return 0.0; } … g_signal_connect (simple_item, "distance", G_CALLBACK (distance_to_item), NULL); …