Implemented a multiborder pattern
This commit is contained in:
parent
4d609f6fa1
commit
9540812a04
51
artist/multiborder.go
Normal file
51
artist/multiborder.go
Normal file
@ -0,0 +1,51 @@
|
||||
package artist
|
||||
|
||||
import "image"
|
||||
import "image/color"
|
||||
|
||||
type Border struct {
|
||||
Weight int
|
||||
Stroke Pattern
|
||||
bounds image.Rectangle
|
||||
dx, dy int
|
||||
}
|
||||
|
||||
type MultiBorder struct {
|
||||
borders []Border
|
||||
lastWidth, lastHeight int
|
||||
maxBorder int
|
||||
}
|
||||
|
||||
func NewMultiBorder (borders ...Border) (multi *MultiBorder) {
|
||||
return &MultiBorder { borders: borders }
|
||||
}
|
||||
|
||||
func (multi *MultiBorder) AtWhen (x, y, width, height int) (c color.RGBA) {
|
||||
if multi.lastWidth != width || multi.lastHeight != height {
|
||||
multi.recalculate(width, height)
|
||||
}
|
||||
point := image.Point { x, y }
|
||||
for index := multi.maxBorder; index >= 0; index -- {
|
||||
border := multi.borders[index]
|
||||
if point.In(border.bounds) {
|
||||
return border.Stroke.AtWhen (
|
||||
point.X - border.bounds.Min.X,
|
||||
point.Y - border.bounds.Min.Y,
|
||||
border.dx, border.dy)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (multi *MultiBorder) recalculate (width, height int) {
|
||||
bounds := image.Rect (0, 0, width, height)
|
||||
multi.maxBorder = 0
|
||||
for index, border := range multi.borders {
|
||||
multi.maxBorder = index
|
||||
multi.borders[index].bounds = bounds
|
||||
multi.borders[index].dx = bounds.Dx()
|
||||
multi.borders[index].dy = bounds.Dy()
|
||||
bounds = bounds.Inset(border.Weight)
|
||||
if bounds.Empty() { break }
|
||||
}
|
||||
}
|
@ -42,7 +42,7 @@ func (element *Mouse) Handle (event tomo.Event) {
|
||||
artist.NewUniform(color.Black), 1,
|
||||
element.Bounds())
|
||||
artist.Line (
|
||||
element.core, artist.NewUniform(color.White), 1,
|
||||
element.core, artist.NewUniform(color.White), 3,
|
||||
image.Pt(1, 1),
|
||||
image.Pt(resizeEvent.Width - 2, resizeEvent.Height - 2))
|
||||
artist.Line (
|
||||
|
Reference in New Issue
Block a user