This repository has been archived on 2023-08-08. You can view files and clone it, but cannot push or open issues or pull requests.
tomo-old/elements/testing/artist.go

168 lines
4.8 KiB
Go
Raw Normal View History

2023-01-20 23:05:48 +00:00
package testing
import "fmt"
import "time"
2023-01-20 23:05:48 +00:00
import "image"
import "image/color"
2023-02-26 19:27:38 +00:00
import "git.tebibyte.media/sashakoshka/tomo/canvas"
2023-01-20 23:05:48 +00:00
import "git.tebibyte.media/sashakoshka/tomo/artist"
import "git.tebibyte.media/sashakoshka/tomo/shatter"
2023-02-16 17:35:31 +00:00
import "git.tebibyte.media/sashakoshka/tomo/textdraw"
import "git.tebibyte.media/sashakoshka/tomo/defaultfont"
2023-01-20 23:05:48 +00:00
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
2023-02-26 19:27:38 +00:00
import "git.tebibyte.media/sashakoshka/tomo/artist/shapes"
import "git.tebibyte.media/sashakoshka/tomo/artist/patterns"
2023-01-20 23:05:48 +00:00
// Artist is an element that displays shapes and patterns drawn by the artist
// package in order to test it.
type Artist struct {
*core.Core
core core.CoreControl
}
// NewArtist creates a new artist test element.
func NewArtist () (element *Artist) {
element = &Artist { }
2023-02-08 19:36:14 +00:00
element.Core, element.core = core.NewCore(element.draw)
2023-02-26 19:27:38 +00:00
element.core.SetMinimumSize(240, 240)
2023-01-20 23:05:48 +00:00
return
}
func (element *Artist) draw () {
2023-01-20 23:05:48 +00:00
bounds := element.Bounds()
2023-02-26 19:27:38 +00:00
patterns.Uhex(0x000000FF).Draw(element.core, bounds)
2023-01-20 23:05:48 +00:00
drawStart := time.Now()
2023-02-26 19:27:38 +00:00
// 0, 0 - 3, 0
2023-01-20 23:39:08 +00:00
for x := 0; x < 4; x ++ {
2023-02-26 19:27:38 +00:00
element.colorLines(x + 1, element.cellAt(x, 0).Bounds())
2023-01-20 23:39:08 +00:00
}
2023-01-21 00:40:38 +00:00
2023-02-26 19:27:38 +00:00
// 4, 0
c40 := element.cellAt(4, 0)
shapes.StrokeColorRectangle(c40, artist.Hex(0x888888FF), c40.Bounds(), 1)
shapes.ColorLine (
c40, artist.Hex(0xFF0000FF), 1,
c40.Bounds().Min, c40.Bounds().Max)
// 0, 1
c01 := element.cellAt(0, 1)
shapes.StrokeColorRectangle(c01, artist.Hex(0x888888FF), c01.Bounds(), 1)
shapes.FillColorEllipse(element.core, artist.Hex(0x00FF00FF), c01.Bounds())
// 1, 1 - 3, 1
2023-01-21 02:59:48 +00:00
for x := 1; x < 4; x ++ {
2023-02-26 19:27:38 +00:00
c := element.cellAt(x, 1)
shapes.StrokeColorRectangle (
element.core, artist.Hex(0x888888FF),
c.Bounds(), 1)
shapes.StrokeColorEllipse (
2023-02-13 06:49:33 +00:00
element.core,
2023-02-26 19:27:38 +00:00
[]color.RGBA {
artist.Hex(0xFF0000FF),
artist.Hex(0x00FF00FF),
artist.Hex(0xFF00FFFF),
2023-01-21 02:59:48 +00:00
} [x - 1],
2023-02-26 19:27:38 +00:00
c.Bounds(), x)
2023-01-21 02:59:48 +00:00
}
2023-02-26 19:27:38 +00:00
// 4, 1
c41 := element.cellAt(4, 1)
shatterPos := c41.Bounds().Min
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 19:27:38 +00:00
tiles := shatter.Shatter(c41.Bounds(), rocks...)
for index, tile := range tiles {
artist.DrawBounds (
2023-02-27 03:20:17 +00:00
element.core,
2023-02-26 19:27:38 +00:00
[]artist.Pattern {
patterns.Uhex(0xFF0000FF),
patterns.Uhex(0x00FF00FF),
patterns.Uhex(0xFF00FFFF),
patterns.Uhex(0xFFFF00FF),
patterns.Uhex(0x00FFFFFF),
} [index % 5], tile)
}
2023-01-24 20:35:00 +00:00
2023-02-26 19:27:38 +00:00
// 0, 2
c02 := element.cellAt(0, 2)
shapes.StrokeColorRectangle(c02, artist.Hex(0x888888FF), c02.Bounds(), 1)
shapes.FillEllipse(c02, c41)
2023-01-24 23:15:46 +00:00
2023-02-26 19:27:38 +00:00
// 1, 2
c12 := element.cellAt(1, 2)
shapes.StrokeColorRectangle(c12, artist.Hex(0x888888FF), c12.Bounds(), 1)
shapes.StrokeEllipse(c12, c41, 5)
// 2, 2
c22 := element.cellAt(2, 2)
shapes.FillRectangle(c22, c41)
2023-01-24 23:27:36 +00:00
2023-02-26 19:27:38 +00:00
// 3, 2
c32 := element.cellAt(3, 2)
shapes.StrokeRectangle(c32, c41, 5)
2023-02-16 19:57:46 +00: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-02-26 19:27:38 +00:00
element.core, artist.Hex(0xFFFFFFFF),
2023-02-16 19:57:46 +00:00
image.Pt(bounds.Min.X + 8, bounds.Max.Y - 24))
2023-01-21 00:40:38 +00:00
}
2023-02-26 19:27:38 +00:00
func (element *Artist) colorLines (weight int, bounds image.Rectangle) {
bounds = bounds.Inset(4)
2023-02-26 19:27:38 +00:00
c := artist.Hex(0xFFFFFFFF)
shapes.ColorLine(element.core, c, weight, bounds.Min, bounds.Max)
shapes.ColorLine (
2023-02-13 06:49:33 +00:00
element.core, c, weight,
2023-01-21 00:40:38 +00:00
image.Pt(bounds.Max.X, bounds.Min.Y),
image.Pt(bounds.Min.X, bounds.Max.Y))
2023-02-26 19:27:38 +00:00
shapes.ColorLine (
2023-02-13 06:49:33 +00:00
element.core, c, weight,
2023-01-21 00:40:38 +00:00
image.Pt(bounds.Max.X, bounds.Min.Y + 16),
image.Pt(bounds.Min.X, bounds.Max.Y - 16))
2023-02-26 19:27:38 +00:00
shapes.ColorLine (
2023-02-13 06:49:33 +00:00
element.core, c, weight,
2023-01-21 00:40:38 +00:00
image.Pt(bounds.Min.X, bounds.Min.Y + 16),
image.Pt(bounds.Max.X, bounds.Max.Y - 16))
2023-02-26 19:27:38 +00:00
shapes.ColorLine (
2023-02-13 06:49:33 +00:00
element.core, c, weight,
2023-01-21 00:40:38 +00:00
image.Pt(bounds.Min.X + 20, bounds.Min.Y),
image.Pt(bounds.Max.X - 20, bounds.Max.Y))
2023-02-26 19:27:38 +00:00
shapes.ColorLine (
2023-02-13 06:49:33 +00:00
element.core, c, weight,
2023-01-21 00:40:38 +00:00
image.Pt(bounds.Max.X - 20, bounds.Min.Y),
image.Pt(bounds.Min.X + 20, bounds.Max.Y))
2023-02-26 19:27:38 +00:00
shapes.ColorLine (
2023-02-13 06:49:33 +00:00
element.core, c, weight,
2023-01-21 00:40:38 +00: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 19:27:38 +00:00
shapes.ColorLine (
2023-02-13 06:49:33 +00:00
element.core, c, weight,
2023-01-21 00:40:38 +00: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 23:05:48 +00:00
}
2023-02-26 19:27:38 +00:00
func (element *Artist) cellAt (x, y int) (canvas.Canvas) {
2023-02-16 19:57:46 +00:00
bounds := element.Bounds()
cellBounds := image.Rectangle { }
cellBounds.Min = bounds.Min
cellBounds.Max.X = bounds.Min.X + bounds.Dx() / 5
2023-02-26 19:27:38 +00:00
cellBounds.Max.Y = bounds.Min.Y + (bounds.Dy() - 48) / 4
return canvas.Cut (element.core, cellBounds.Add (image.Pt (
2023-02-16 19:57:46 +00:00
x * cellBounds.Dx(),
2023-02-26 19:27:38 +00:00
y * cellBounds.Dy())))
2023-01-20 23:05:48 +00:00
}