2023-01-14 10:41:51 -07:00
|
|
|
package artist
|
|
|
|
|
2023-02-23 12:44:54 -07:00
|
|
|
import "image"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/canvas"
|
2023-02-26 20:20:17 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/shatter"
|
2023-01-14 10:41:51 -07:00
|
|
|
|
2023-02-23 12:44:54 -07:00
|
|
|
// Pattern is capable of drawing to a canvas within the bounds of a given
|
|
|
|
// clipping rectangle.
|
2023-01-14 10:41:51 -07:00
|
|
|
type Pattern interface {
|
2023-03-11 23:04:06 -07:00
|
|
|
// Draw draws the pattern onto the destination canvas, using the
|
|
|
|
// specified bounds. The given bounds can be smaller or larger than the
|
|
|
|
// bounds of the destination canvas. The destination canvas can be cut
|
|
|
|
// using canvas.Cut() to draw only a specific subset of a pattern.
|
|
|
|
Draw (destination canvas.Canvas, bounds image.Rectangle)
|
2023-01-14 10:41:51 -07:00
|
|
|
}
|
2023-02-23 13:00:44 -07:00
|
|
|
|
2023-03-11 23:04:06 -07:00
|
|
|
// Fill fills the destination canvas with the given pattern.
|
|
|
|
func Fill (destination canvas.Canvas, source Pattern) (updated image.Rectangle) {
|
|
|
|
source.Draw(destination, destination.Bounds())
|
|
|
|
return destination.Bounds()
|
2023-02-23 13:00:44 -07:00
|
|
|
}
|
|
|
|
|
2023-03-11 23:47:58 -07:00
|
|
|
// DrawClip lets you draw several subsets of a pattern at once.
|
2023-03-11 23:04:06 -07:00
|
|
|
func DrawClip (
|
2023-02-23 13:00:44 -07:00
|
|
|
destination canvas.Canvas,
|
2023-02-26 20:20:17 -07:00
|
|
|
source Pattern,
|
2023-02-23 13:00:44 -07:00
|
|
|
bounds image.Rectangle,
|
2023-03-11 23:04:06 -07:00
|
|
|
subsets ...image.Rectangle,
|
2023-02-26 20:20:17 -07:00
|
|
|
) (
|
|
|
|
updatedRegion image.Rectangle,
|
|
|
|
) {
|
2023-03-11 23:04:06 -07:00
|
|
|
for _, subset := range subsets {
|
|
|
|
source.Draw(canvas.Cut(destination, subset), bounds)
|
|
|
|
updatedRegion = updatedRegion.Union(subset)
|
|
|
|
}
|
|
|
|
return
|
2023-02-26 20:20:17 -07:00
|
|
|
}
|
|
|
|
|
2023-03-11 23:04:06 -07:00
|
|
|
// DrawShatter is like an inverse of DrawClip, drawing nothing in the areas
|
|
|
|
// specified by "rocks".
|
2023-02-26 20:20:17 -07:00
|
|
|
func DrawShatter (
|
|
|
|
destination canvas.Canvas,
|
2023-02-23 13:00:44 -07:00
|
|
|
source Pattern,
|
2023-03-11 23:04:06 -07:00
|
|
|
bounds image.Rectangle,
|
2023-02-26 20:20:17 -07:00
|
|
|
rocks ...image.Rectangle,
|
|
|
|
) (
|
|
|
|
updatedRegion image.Rectangle,
|
2023-02-23 13:00:44 -07:00
|
|
|
) {
|
2023-03-11 23:04:06 -07:00
|
|
|
tiles := shatter.Shatter(bounds, rocks...)
|
|
|
|
return DrawClip(destination, source, bounds, tiles...)
|
2023-02-23 13:00:44 -07:00
|
|
|
}
|
2023-02-23 15:44:53 -07:00
|
|
|
|
|
|
|
// AllocateSample returns a new canvas containing the result of a pattern. The
|
|
|
|
// resulting canvas can be sourced from shape drawing functions. I beg of you
|
|
|
|
// please do not call this every time you need to draw a shape with a pattern on
|
|
|
|
// it because that is horrible and cruel to the computer.
|
2023-03-11 23:04:06 -07:00
|
|
|
func AllocateSample (source Pattern, width, height int) canvas.Canvas {
|
|
|
|
allocated := canvas.NewBasicCanvas(width, height)
|
|
|
|
Fill(allocated, source)
|
|
|
|
return allocated
|
2023-02-23 15:44:53 -07:00
|
|
|
}
|