| 1 |
#------------------------------------------------------------------------------- |
| 2 |
# canvas.Image |
| 3 |
#------------------------------------------------------------------------------- |
| 4 |
# Class for an image element on a canvas |
| 5 |
# |
| 6 |
var Image = { |
| 7 |
new: func(ghost) { |
| 8 |
var obj = { |
| 9 |
parents: [Image, Element.new(ghost)], |
| 10 |
}; |
| 11 |
return obj; |
| 12 |
}, |
| 13 |
|
| 14 |
# Set image file to be used |
| 15 |
# |
| 16 |
# @param file Path to file or canvas (Use canvas://... for canvas, eg. |
| 17 |
# canvas://by-index/texture[0]) |
| 18 |
setFile: func(file) { |
| 19 |
me.set("src", file); |
| 20 |
}, |
| 21 |
|
| 22 |
# Set rectangular region of source image to be used |
| 23 |
# |
| 24 |
# @param left Rectangle minimum x coordinate |
| 25 |
# @param top Rectangle minimum y coordinate |
| 26 |
# @param right Rectangle maximum x coordinate |
| 27 |
# @param bottom Rectangle maximum y coordinate |
| 28 |
# @param normalized Whether to use normalized ([0,1]) or image |
| 29 |
# ([0, image_width]/[0, image_height]) coordinates |
| 30 |
setSourceRect: func { |
| 31 |
# Work with both positional arguments and named arguments. |
| 32 |
# Support first argument being a vector instead of four separate ones. |
| 33 |
if (size(arg) == 1) { |
| 34 |
arg = arg[0]; |
| 35 |
} |
| 36 |
elsif (size(arg) and size(arg) < 4 and isvec(arg[0])) { |
| 37 |
arg = arg[0]~arg[1:]; |
| 38 |
} |
| 39 |
if (!contains(caller(0)[0], "normalized")) { |
| 40 |
if (size(arg) > 4) |
| 41 |
var normalized = arg[4]; |
| 42 |
else var normalized = 1; |
| 43 |
} |
| 44 |
if (size(arg) >= 3) |
| 45 |
var (left,top,right,bottom) = arg; |
| 46 |
|
| 47 |
me._node.getNode("source", 1).setValues({ |
| 48 |
left: left, |
| 49 |
top: top, |
| 50 |
right: right, |
| 51 |
bottom: bottom, |
| 52 |
normalized: normalized |
| 53 |
}); |
| 54 |
return me; |
| 55 |
}, |
| 56 |
|
| 57 |
# Set size of image element |
| 58 |
# |
| 59 |
# @param width |
| 60 |
# @param height |
| 61 |
# - or - |
| 62 |
# @param size ([width, height]) |
| 63 |
setSize: func { |
| 64 |
me._node.setValues({size: _arg2valarray(arg)}); |
| 65 |
return me; |
| 66 |
} |
| 67 |
}; |
| 68 |
|