开发者

How to add method to Cairo.Context?

开发者 https://www.devze.com 2023-02-16 18:35 出处:网络
I\'ve created the following function for drawing boxes in Cairo with rounded rectangles void square (Context cr, int x, int y, int sizex, int sizey, int radius)

I've created the following function for drawing boxes in Cairo with rounded rectangles

void square (Context cr, int x, int y, int sizex, int sizey, int radius)
{
    cr.move_to (x + radius, y);
    cr.arc (x + sizex - radius, y + radius, radius, 1.5 * PI, 0);
    cr.arc (x + sizex - radius, y + sizey - radius, radius, 0, 0.5 * PI);
    cr.arc (x + radius, y + sizey - radius, radius, 0.5 * PI, PI);
    cr.arc (x + radius, y + radius, radius开发者_运维知识库, PI, 1.5 * PI);
}

This is a very C like way of doing it. I would prefer to do this in a more object orientated way. Like implementing the function as a method of Cairo.Context.


You can't add methods to existing classes without modifying the definition of that class (cairo.vapi in this case). What you can do, however, is subclass Cairo.Context and just use that instead of Cairo.Context. Something like this should do the trick:

[Compact]
public class Context : Cairo.Context {
  public void square (int x, int y, int sizex, int sizey, int radius) {
    this.move_to (x + radius, y);
    this.arc (x + sizex - radius, y + radius, radius, 1.5 * Math.PI, 0);
    this.arc (x + sizex - radius, y + sizey - radius, radius, 0, 0.5 * Math.PI);
    this.arc (x + radius, y + sizey - radius, radius, 0.5 * Math.PI, Math.PI);
    this.arc (x + radius, y + radius, radius, Math.PI, 1.5 * Math.PI);
  }

  public Context (Cairo.Surface target) {
    base (target);
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号