2023-01-20 16:05:48 -07:00
|
|
|
package testing
|
|
|
|
|
2023-01-20 18:07:01 -07:00
|
|
|
import "fmt"
|
|
|
|
import "time"
|
2023-01-20 16:05:48 -07:00
|
|
|
import "image"
|
|
|
|
import "image/color"
|
2023-04-12 21:46:29 -06:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo"
|
2023-02-26 12:27:38 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/canvas"
|
2023-01-20 16:05:48 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/artist"
|
2023-02-11 19:07:35 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/shatter"
|
2023-02-16 10:35:31 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/textdraw"
|
2023-02-26 12:27:38 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/artist/shapes"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/artist/patterns"
|
2023-03-31 19:11:10 -06:00
|
|
|
import defaultfont "git.tebibyte.media/sashakoshka/tomo/default/font"
|
2023-01-20 16:05:48 -07:00
|
|
|
|
|
|
|
// Artist is an element that displays shapes and patterns drawn by the artist
|
|
|
|
// package in order to test it.
|
|
|
|
type Artist struct {
|
2023-04-12 21:46:29 -06:00
|
|
|
entity tomo.Entity
|
2023-01-20 16:05:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewArtist creates a new artist test element.
|
2023-04-12 21:46:29 -06:00
|
|
|
func NewArtist () *Artist {
|
|
|
|
return &Artist { }
|
2023-01-20 16:05:48 -07:00
|
|
|
}
|
|
|
|
|
2023-04-12 21:46:29 -06:00
|
|
|
func (element *Artist) Bind (entity tomo.Entity) {
|
|
|
|
element.entity = entity
|
2023-04-13 22:25:05 -06:00
|
|
|
if entity != nil { entity.SetMinimumSize(240, 240) }
|
2023-04-12 21:46:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Artist) Draw (destination canvas.Canvas) {
|
|
|
|
bounds := element.entity.Bounds()
|
|
|
|
patterns.Uhex(0x000000FF).Draw(destination, bounds)
|
2023-01-20 16:05:48 -07:00
|
|
|
|
2023-01-20 18:07:01 -07:00
|
|
|
drawStart := time.Now()
|
|
|
|
|
2023-02-26 12:27:38 -07:00
|
|
|
// 0, 0 - 3, 0
|
2023-01-20 16:39:08 -07:00
|
|
|
for x := 0; x < 4; x ++ {
|
2023-04-12 21:46:29 -06:00
|
|
|
element.colorLines(destination, x + 1, element.cellAt(destination, x, 0).Bounds())
|
2023-01-20 16:39:08 -07:00
|
|
|
}
|
2023-01-20 17:40:38 -07:00
|
|
|
|
2023-02-26 12:27:38 -07:00
|
|
|
// 4, 0
|
2023-04-12 21:46:29 -06:00
|
|
|
c40 := element.cellAt(destination, 4, 0)
|
2023-02-26 12:27:38 -07:00
|
|
|
shapes.StrokeColorRectangle(c40, artist.Hex(0x888888FF), c40.Bounds(), 1)
|
|
|
|
shapes.ColorLine (
|
|
|
|
c40, artist.Hex(0xFF0000FF), 1,
|
|
|
|
c40.Bounds().Min, c40.Bounds().Max)
|
|
|
|
|
|
|
|
// 0, 1
|
2023-04-12 21:46:29 -06:00
|
|
|
c01 := element.cellAt(destination, 0, 1)
|
2023-02-26 12:27:38 -07:00
|
|
|
shapes.StrokeColorRectangle(c01, artist.Hex(0x888888FF), c01.Bounds(), 1)
|
2023-04-12 21:46:29 -06:00
|
|
|
shapes.FillColorEllipse(destination, artist.Hex(0x00FF00FF), c01.Bounds())
|
2023-02-26 12:27:38 -07:00
|
|
|
|
|
|
|
// 1, 1 - 3, 1
|
2023-01-20 19:59:48 -07:00
|
|
|
for x := 1; x < 4; x ++ {
|
2023-04-12 21:46:29 -06:00
|
|
|
c := element.cellAt(destination, x, 1)
|
2023-02-26 12:27:38 -07:00
|
|
|
shapes.StrokeColorRectangle (
|
2023-04-12 21:46:29 -06:00
|
|
|
destination, artist.Hex(0x888888FF),
|
2023-02-26 12:27:38 -07:00
|
|
|
c.Bounds(), 1)
|
|
|
|
shapes.StrokeColorEllipse (
|
2023-04-12 21:46:29 -06:00
|
|
|
destination,
|
2023-02-26 12:27:38 -07:00
|
|
|
[]color.RGBA {
|
|
|
|
artist.Hex(0xFF0000FF),
|
|
|
|
artist.Hex(0x00FF00FF),
|
|
|
|
artist.Hex(0xFF00FFFF),
|
2023-01-20 19:59:48 -07:00
|
|
|
} [x - 1],
|
2023-02-26 12:27:38 -07:00
|
|
|
c.Bounds(), x)
|
2023-01-20 19:59:48 -07:00
|
|
|
}
|
|
|
|
|
2023-02-26 12:27:38 -07:00
|
|
|
// 4, 1
|
2023-04-12 21:46:29 -06:00
|
|
|
c41 := element.cellAt(destination, 4, 1)
|
2023-02-26 12:27:38 -07:00
|
|
|
shatterPos := c41.Bounds().Min
|
2023-02-11 19:07:35 -07:00
|
|
|
rocks := []image.Rectangle {
|
|
|
|
image.Rect(3, 12, 13, 23).Add(shatterPos),
|
|
|
|
// image.Rect(30, 10, 40, 23).Add(shatterPos),
|
|
|
|
image.Rect(55, 40, 70, 49).Add(shatterPos),
|
|
|
|
image.Rect(30, -10, 40, 43).Add(shatterPos),
|
|
|
|
image.Rect(80, 30, 90, 45).Add(shatterPos),
|
|
|
|
}
|
2023-02-26 12:27:38 -07:00
|
|
|
tiles := shatter.Shatter(c41.Bounds(), rocks...)
|
|
|
|
for index, tile := range tiles {
|
2023-03-11 23:19:40 -07:00
|
|
|
[]artist.Pattern {
|
|
|
|
patterns.Uhex(0xFF0000FF),
|
|
|
|
patterns.Uhex(0x00FF00FF),
|
|
|
|
patterns.Uhex(0xFF00FFFF),
|
|
|
|
patterns.Uhex(0xFFFF00FF),
|
|
|
|
patterns.Uhex(0x00FFFFFF),
|
2023-04-12 21:46:29 -06:00
|
|
|
} [index % 5].Draw(destination, tile)
|
2023-01-24 12:24:30 -07:00
|
|
|
}
|
2023-01-24 13:35:00 -07:00
|
|
|
|
2023-02-26 12:27:38 -07:00
|
|
|
// 0, 2
|
2023-04-12 21:46:29 -06:00
|
|
|
c02 := element.cellAt(destination, 0, 2)
|
2023-02-26 12:27:38 -07:00
|
|
|
shapes.StrokeColorRectangle(c02, artist.Hex(0x888888FF), c02.Bounds(), 1)
|
2023-03-11 23:19:40 -07:00
|
|
|
shapes.FillEllipse(c02, c41, c02.Bounds())
|
2023-01-24 16:15:46 -07:00
|
|
|
|
2023-02-26 12:27:38 -07:00
|
|
|
// 1, 2
|
2023-04-12 21:46:29 -06:00
|
|
|
c12 := element.cellAt(destination, 1, 2)
|
2023-02-26 12:27:38 -07:00
|
|
|
shapes.StrokeColorRectangle(c12, artist.Hex(0x888888FF), c12.Bounds(), 1)
|
2023-03-11 23:19:40 -07:00
|
|
|
shapes.StrokeEllipse(c12, c41, c12.Bounds(), 5)
|
2023-02-26 12:27:38 -07:00
|
|
|
|
|
|
|
// 2, 2
|
2023-04-12 21:46:29 -06:00
|
|
|
c22 := element.cellAt(destination, 2, 2)
|
2023-03-11 23:19:40 -07:00
|
|
|
shapes.FillRectangle(c22, c41, c22.Bounds())
|
2023-01-24 16:27:36 -07:00
|
|
|
|
2023-02-26 12:27:38 -07:00
|
|
|
// 3, 2
|
2023-04-12 21:46:29 -06:00
|
|
|
c32 := element.cellAt(destination, 3, 2)
|
2023-03-11 23:19:40 -07:00
|
|
|
shapes.StrokeRectangle(c32, c41, c32.Bounds(), 5)
|
2023-02-16 12:57:46 -07:00
|
|
|
|
2023-02-27 14:38:33 -07:00
|
|
|
// 4, 2
|
2023-04-12 21:46:29 -06:00
|
|
|
c42 := element.cellAt(destination, 4, 2)
|
2023-02-27 14:38:33 -07:00
|
|
|
|
|
|
|
// 0, 3
|
2023-04-12 21:46:29 -06:00
|
|
|
c03 := element.cellAt(destination, 0, 3)
|
2023-02-27 14:38:33 -07:00
|
|
|
patterns.Border {
|
|
|
|
Canvas: element.thingy(c42),
|
|
|
|
Inset: artist.Inset { 8, 8, 8, 8 },
|
|
|
|
}.Draw(c03, c03.Bounds())
|
|
|
|
|
2023-02-27 15:00:28 -07:00
|
|
|
// 1, 3
|
2023-04-12 21:46:29 -06:00
|
|
|
c13 := element.cellAt(destination, 1, 3)
|
2023-02-27 15:00:28 -07:00
|
|
|
patterns.Border {
|
|
|
|
Canvas: element.thingy(c42),
|
|
|
|
Inset: artist.Inset { 8, 8, 8, 8 },
|
|
|
|
}.Draw(c13, c13.Bounds().Inset(10))
|
|
|
|
|
2023-04-02 20:16:12 -06:00
|
|
|
// 2, 3
|
2023-04-12 21:46:29 -06:00
|
|
|
c23 := element.cellAt(destination, 2, 3)
|
2023-04-02 20:16:12 -06:00
|
|
|
patterns.Border {
|
|
|
|
Canvas: element.thingy(c42),
|
|
|
|
Inset: artist.Inset { 8, 8, 8, 8 },
|
|
|
|
}.Draw(c23, c23.Bounds())
|
|
|
|
patterns.Border {
|
|
|
|
Canvas: element.thingy(c42),
|
|
|
|
Inset: artist.Inset { 8, 8, 8, 8 },
|
|
|
|
}.Draw(canvas.Cut(c23, c23.Bounds().Inset(16)), c23.Bounds())
|
|
|
|
|
2023-02-16 12:57:46 -07:00
|
|
|
// how long did that take to render?
|
|
|
|
drawTime := time.Since(drawStart)
|
|
|
|
textDrawer := textdraw.Drawer { }
|
|
|
|
textDrawer.SetFace(defaultfont.FaceRegular)
|
|
|
|
textDrawer.SetText ([]rune (fmt.Sprintf (
|
|
|
|
"%dms\n%dus",
|
|
|
|
drawTime.Milliseconds(),
|
|
|
|
drawTime.Microseconds())))
|
|
|
|
textDrawer.Draw (
|
2023-04-12 21:46:29 -06:00
|
|
|
destination, artist.Hex(0xFFFFFFFF),
|
2023-02-16 12:57:46 -07:00
|
|
|
image.Pt(bounds.Min.X + 8, bounds.Max.Y - 24))
|
2023-01-20 17:40:38 -07:00
|
|
|
}
|
|
|
|
|
2023-04-12 21:46:29 -06:00
|
|
|
func (element *Artist) colorLines (destination canvas.Canvas, weight int, bounds image.Rectangle) {
|
2023-01-20 21:40:59 -07:00
|
|
|
bounds = bounds.Inset(4)
|
2023-02-26 12:27:38 -07:00
|
|
|
c := artist.Hex(0xFFFFFFFF)
|
2023-04-12 21:46:29 -06:00
|
|
|
shapes.ColorLine(destination, c, weight, bounds.Min, bounds.Max)
|
2023-02-26 12:27:38 -07:00
|
|
|
shapes.ColorLine (
|
2023-04-12 21:46:29 -06:00
|
|
|
destination, c, weight,
|
2023-01-20 17:40:38 -07:00
|
|
|
image.Pt(bounds.Max.X, bounds.Min.Y),
|
|
|
|
image.Pt(bounds.Min.X, bounds.Max.Y))
|
2023-02-26 12:27:38 -07:00
|
|
|
shapes.ColorLine (
|
2023-04-12 21:46:29 -06:00
|
|
|
destination, c, weight,
|
2023-01-20 17:40:38 -07:00
|
|
|
image.Pt(bounds.Max.X, bounds.Min.Y + 16),
|
|
|
|
image.Pt(bounds.Min.X, bounds.Max.Y - 16))
|
2023-02-26 12:27:38 -07:00
|
|
|
shapes.ColorLine (
|
2023-04-12 21:46:29 -06:00
|
|
|
destination, c, weight,
|
2023-01-20 17:40:38 -07:00
|
|
|
image.Pt(bounds.Min.X, bounds.Min.Y + 16),
|
|
|
|
image.Pt(bounds.Max.X, bounds.Max.Y - 16))
|
2023-02-26 12:27:38 -07:00
|
|
|
shapes.ColorLine (
|
2023-04-12 21:46:29 -06:00
|
|
|
destination, c, weight,
|
2023-01-20 17:40:38 -07:00
|
|
|
image.Pt(bounds.Min.X + 20, bounds.Min.Y),
|
|
|
|
image.Pt(bounds.Max.X - 20, bounds.Max.Y))
|
2023-02-26 12:27:38 -07:00
|
|
|
shapes.ColorLine (
|
2023-04-12 21:46:29 -06:00
|
|
|
destination, c, weight,
|
2023-01-20 17:40:38 -07:00
|
|
|
image.Pt(bounds.Max.X - 20, bounds.Min.Y),
|
|
|
|
image.Pt(bounds.Min.X + 20, bounds.Max.Y))
|
2023-02-26 12:27:38 -07:00
|
|
|
shapes.ColorLine (
|
2023-04-12 21:46:29 -06:00
|
|
|
destination, c, weight,
|
2023-01-20 17:40:38 -07:00
|
|
|
image.Pt(bounds.Min.X, bounds.Min.Y + bounds.Dy() / 2),
|
|
|
|
image.Pt(bounds.Max.X, bounds.Min.Y + bounds.Dy() / 2))
|
2023-02-26 12:27:38 -07:00
|
|
|
shapes.ColorLine (
|
2023-04-12 21:46:29 -06:00
|
|
|
destination, c, weight,
|
2023-01-20 17:40:38 -07:00
|
|
|
image.Pt(bounds.Min.X + bounds.Dx() / 2, bounds.Min.Y),
|
|
|
|
image.Pt(bounds.Min.X + bounds.Dx() / 2, bounds.Max.Y))
|
2023-01-20 16:05:48 -07:00
|
|
|
}
|
|
|
|
|
2023-04-12 21:46:29 -06:00
|
|
|
func (element *Artist) cellAt (destination canvas.Canvas, x, y int) (canvas.Canvas) {
|
|
|
|
bounds := element.entity.Bounds()
|
2023-02-16 12:57:46 -07:00
|
|
|
cellBounds := image.Rectangle { }
|
|
|
|
cellBounds.Min = bounds.Min
|
|
|
|
cellBounds.Max.X = bounds.Min.X + bounds.Dx() / 5
|
2023-02-26 12:27:38 -07:00
|
|
|
cellBounds.Max.Y = bounds.Min.Y + (bounds.Dy() - 48) / 4
|
2023-04-12 21:46:29 -06:00
|
|
|
return canvas.Cut (destination, cellBounds.Add (image.Pt (
|
2023-02-16 12:57:46 -07:00
|
|
|
x * cellBounds.Dx(),
|
2023-02-26 12:27:38 -07:00
|
|
|
y * cellBounds.Dy())))
|
2023-01-20 16:05:48 -07:00
|
|
|
}
|
2023-02-27 14:38:33 -07:00
|
|
|
|
|
|
|
func (element *Artist) thingy (destination canvas.Canvas) (result canvas.Canvas) {
|
|
|
|
bounds := destination.Bounds()
|
|
|
|
bounds = image.Rect(0, 0, 32, 32).Add(bounds.Min)
|
|
|
|
shapes.FillColorRectangle(destination, artist.Hex(0x440000FF), bounds)
|
|
|
|
shapes.StrokeColorRectangle(destination, artist.Hex(0xFF0000FF), bounds, 1)
|
|
|
|
shapes.StrokeColorRectangle(destination, artist.Hex(0x004400FF), bounds.Inset(4), 1)
|
|
|
|
shapes.FillColorRectangle(destination, artist.Hex(0x004444FF), bounds.Inset(12))
|
|
|
|
shapes.StrokeColorRectangle(destination, artist.Hex(0x888888FF), bounds.Inset(8), 1)
|
|
|
|
return canvas.Cut(destination, bounds)
|
|
|
|
}
|