2019-07-03 23:22:30 +00:00
|
|
|
package drawtool
|
|
|
|
|
2019-12-23 02:21:58 +00:00
|
|
|
import "git.kirsle.net/go/render"
|
2019-07-03 23:22:30 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Stroke holds temporary pixel data with a shape and color.
|
|
|
|
|
|
|
|
It is used for myriad purposes:
|
|
|
|
|
|
|
|
- As a staging area for drawing new pixels to the drawing without committing
|
|
|
|
them until completed.
|
|
|
|
- As a unit of work for the Undo/Redo History when editing a drawing.
|
|
|
|
- As imaginary visual lines superimposed on top of a drawing, for example to
|
|
|
|
visualize the link between two doodads or to draw collision hitboxes and other
|
|
|
|
debug lines to the drawing.
|
|
|
|
*/
|
|
|
|
type Stroke struct {
|
|
|
|
ID int // Unique ID per each stroke
|
|
|
|
Shape Shape
|
|
|
|
Color render.Color
|
Eraser Tool, Brush Sizes
* Implement Brush Sizes for drawtool.Stroke and add a UI to the tools panel
to control the brush size.
* Brush sizes: 1, 2, 4, 8, 16, 24, 32, 48, 64
* Add the Eraser Tool to editor mode. It uses a default brush size of 16
and a max size of 32 due to some performance issues.
* The Undo/Redo system now remembers the original color of pixels when
you change them, so that Undo will set them back how they were instead
of deleting the pixel entirely. Due to performance issues, this only
happens when your Brush Size is 0 (drawing single-pixel shapes).
* UI: Add an IntVariable option to ui.Label to bind showing the value of
an int reference.
Aforementioned performance issues:
* When we try to remember whole rects of pixels for drawing thick
shapes, it requires a ton of scanning for each step of the shape. Even
de-duplicating pixel checks, tons of extra reads are constantly
checked.
* The Eraser is the only tool that absolutely needs to be able to
remember wiped pixels AND have large brush sizes. The performance
sucks and lags a bit if you erase a lot all at once, but it's a
trade-off for now.
* So pixels aren't remembered when drawing lines in your level with
thick brushes, so the Undo action will simply delete your pixels and not
reset them. Only the Eraser can bring back pixels.
2019-07-12 02:07:46 +00:00
|
|
|
Thickness int // 0 = 1px; thickness creates a box N pixels away from each point
|
2019-07-03 23:22:30 +00:00
|
|
|
ExtraData interface{} // arbitrary storage for extra data to attach
|
|
|
|
|
|
|
|
// Start and end points for Lines, Rectangles, etc.
|
|
|
|
PointA render.Point
|
|
|
|
PointB render.Point
|
|
|
|
|
|
|
|
// Array of points for Freehand shapes.
|
|
|
|
Points []render.Point
|
|
|
|
uniqPoint map[render.Point]interface{} // deduplicate points added
|
Eraser Tool, Brush Sizes
* Implement Brush Sizes for drawtool.Stroke and add a UI to the tools panel
to control the brush size.
* Brush sizes: 1, 2, 4, 8, 16, 24, 32, 48, 64
* Add the Eraser Tool to editor mode. It uses a default brush size of 16
and a max size of 32 due to some performance issues.
* The Undo/Redo system now remembers the original color of pixels when
you change them, so that Undo will set them back how they were instead
of deleting the pixel entirely. Due to performance issues, this only
happens when your Brush Size is 0 (drawing single-pixel shapes).
* UI: Add an IntVariable option to ui.Label to bind showing the value of
an int reference.
Aforementioned performance issues:
* When we try to remember whole rects of pixels for drawing thick
shapes, it requires a ton of scanning for each step of the shape. Even
de-duplicating pixel checks, tons of extra reads are constantly
checked.
* The Eraser is the only tool that absolutely needs to be able to
remember wiped pixels AND have large brush sizes. The performance
sucks and lags a bit if you erase a lot all at once, but it's a
trade-off for now.
* So pixels aren't remembered when drawing lines in your level with
thick brushes, so the Undo action will simply delete your pixels and not
reset them. Only the Eraser can bring back pixels.
2019-07-12 02:07:46 +00:00
|
|
|
|
|
|
|
// Storage space to recall the previous values of points that were replaced,
|
|
|
|
// especially for the Undo/Redo History tool. When the uix.Canvas commits a
|
|
|
|
// Stroke to the level data, any pixel that has replaced an existing color
|
|
|
|
// will cache its color here, so we can easily page forwards and backwards
|
|
|
|
// in history and not lose data.
|
|
|
|
//
|
|
|
|
// The data is implementation defined and controlled by the caller. This
|
|
|
|
// package does not modify OriginalPoints or do anything with it.
|
|
|
|
OriginalPoints map[render.Point]interface{}
|
2019-07-03 23:22:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var nextStrokeID int
|
|
|
|
|
|
|
|
// NewStroke initializes a new Stroke with a shape and a color.
|
|
|
|
func NewStroke(shape Shape, color render.Color) *Stroke {
|
|
|
|
nextStrokeID++
|
|
|
|
return &Stroke{
|
|
|
|
ID: nextStrokeID,
|
|
|
|
Shape: shape,
|
|
|
|
Color: color,
|
|
|
|
|
|
|
|
// Initialize data structures.
|
|
|
|
Points: []render.Point{},
|
|
|
|
uniqPoint: map[render.Point]interface{}{},
|
Eraser Tool, Brush Sizes
* Implement Brush Sizes for drawtool.Stroke and add a UI to the tools panel
to control the brush size.
* Brush sizes: 1, 2, 4, 8, 16, 24, 32, 48, 64
* Add the Eraser Tool to editor mode. It uses a default brush size of 16
and a max size of 32 due to some performance issues.
* The Undo/Redo system now remembers the original color of pixels when
you change them, so that Undo will set them back how they were instead
of deleting the pixel entirely. Due to performance issues, this only
happens when your Brush Size is 0 (drawing single-pixel shapes).
* UI: Add an IntVariable option to ui.Label to bind showing the value of
an int reference.
Aforementioned performance issues:
* When we try to remember whole rects of pixels for drawing thick
shapes, it requires a ton of scanning for each step of the shape. Even
de-duplicating pixel checks, tons of extra reads are constantly
checked.
* The Eraser is the only tool that absolutely needs to be able to
remember wiped pixels AND have large brush sizes. The performance
sucks and lags a bit if you erase a lot all at once, but it's a
trade-off for now.
* So pixels aren't remembered when drawing lines in your level with
thick brushes, so the Undo action will simply delete your pixels and not
reset them. Only the Eraser can bring back pixels.
2019-07-12 02:07:46 +00:00
|
|
|
|
|
|
|
OriginalPoints: map[render.Point]interface{}{},
|
2019-07-03 23:22:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-05 23:04:36 +00:00
|
|
|
// Copy returns a duplicate of the Stroke reference.
|
|
|
|
func (s *Stroke) Copy() *Stroke {
|
|
|
|
nextStrokeID++
|
|
|
|
return &Stroke{
|
Eraser Tool, Brush Sizes
* Implement Brush Sizes for drawtool.Stroke and add a UI to the tools panel
to control the brush size.
* Brush sizes: 1, 2, 4, 8, 16, 24, 32, 48, 64
* Add the Eraser Tool to editor mode. It uses a default brush size of 16
and a max size of 32 due to some performance issues.
* The Undo/Redo system now remembers the original color of pixels when
you change them, so that Undo will set them back how they were instead
of deleting the pixel entirely. Due to performance issues, this only
happens when your Brush Size is 0 (drawing single-pixel shapes).
* UI: Add an IntVariable option to ui.Label to bind showing the value of
an int reference.
Aforementioned performance issues:
* When we try to remember whole rects of pixels for drawing thick
shapes, it requires a ton of scanning for each step of the shape. Even
de-duplicating pixel checks, tons of extra reads are constantly
checked.
* The Eraser is the only tool that absolutely needs to be able to
remember wiped pixels AND have large brush sizes. The performance
sucks and lags a bit if you erase a lot all at once, but it's a
trade-off for now.
* So pixels aren't remembered when drawing lines in your level with
thick brushes, so the Undo action will simply delete your pixels and not
reset them. Only the Eraser can bring back pixels.
2019-07-12 02:07:46 +00:00
|
|
|
ID: nextStrokeID,
|
|
|
|
Shape: s.Shape,
|
|
|
|
Color: s.Color,
|
|
|
|
Thickness: s.Thickness,
|
|
|
|
ExtraData: s.ExtraData,
|
2019-07-05 23:04:36 +00:00
|
|
|
|
|
|
|
Points: []render.Point{},
|
|
|
|
uniqPoint: map[render.Point]interface{}{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-03 23:22:30 +00:00
|
|
|
// IterPoints returns an iterator of points represented by the stroke.
|
|
|
|
//
|
|
|
|
// For a Line, returns all of the points between PointA and PointB. For freehand,
|
|
|
|
// returns every point added to the stroke.
|
|
|
|
func (s *Stroke) IterPoints() chan render.Point {
|
|
|
|
ch := make(chan render.Point)
|
|
|
|
go func() {
|
|
|
|
switch s.Shape {
|
Eraser Tool, Brush Sizes
* Implement Brush Sizes for drawtool.Stroke and add a UI to the tools panel
to control the brush size.
* Brush sizes: 1, 2, 4, 8, 16, 24, 32, 48, 64
* Add the Eraser Tool to editor mode. It uses a default brush size of 16
and a max size of 32 due to some performance issues.
* The Undo/Redo system now remembers the original color of pixels when
you change them, so that Undo will set them back how they were instead
of deleting the pixel entirely. Due to performance issues, this only
happens when your Brush Size is 0 (drawing single-pixel shapes).
* UI: Add an IntVariable option to ui.Label to bind showing the value of
an int reference.
Aforementioned performance issues:
* When we try to remember whole rects of pixels for drawing thick
shapes, it requires a ton of scanning for each step of the shape. Even
de-duplicating pixel checks, tons of extra reads are constantly
checked.
* The Eraser is the only tool that absolutely needs to be able to
remember wiped pixels AND have large brush sizes. The performance
sucks and lags a bit if you erase a lot all at once, but it's a
trade-off for now.
* So pixels aren't remembered when drawing lines in your level with
thick brushes, so the Undo action will simply delete your pixels and not
reset them. Only the Eraser can bring back pixels.
2019-07-12 02:07:46 +00:00
|
|
|
case Eraser:
|
|
|
|
fallthrough
|
2019-07-03 23:22:30 +00:00
|
|
|
case Freehand:
|
|
|
|
for _, point := range s.Points {
|
|
|
|
ch <- point
|
|
|
|
}
|
|
|
|
case Line:
|
2019-07-14 21:18:44 +00:00
|
|
|
for point := range render.IterLine(s.PointA, s.PointB) {
|
2019-07-03 23:22:30 +00:00
|
|
|
ch <- point
|
|
|
|
}
|
2019-07-04 00:19:25 +00:00
|
|
|
case Rectangle:
|
|
|
|
for point := range render.IterRect(s.PointA, s.PointB) {
|
|
|
|
ch <- point
|
|
|
|
}
|
2019-07-14 21:18:44 +00:00
|
|
|
case Ellipse:
|
2019-07-17 01:27:00 +00:00
|
|
|
for point := range render.IterEllipse(s.PointA, s.PointB) {
|
2019-07-14 21:18:44 +00:00
|
|
|
ch <- point
|
|
|
|
}
|
2019-07-03 23:22:30 +00:00
|
|
|
}
|
|
|
|
close(ch)
|
|
|
|
}()
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
Eraser Tool, Brush Sizes
* Implement Brush Sizes for drawtool.Stroke and add a UI to the tools panel
to control the brush size.
* Brush sizes: 1, 2, 4, 8, 16, 24, 32, 48, 64
* Add the Eraser Tool to editor mode. It uses a default brush size of 16
and a max size of 32 due to some performance issues.
* The Undo/Redo system now remembers the original color of pixels when
you change them, so that Undo will set them back how they were instead
of deleting the pixel entirely. Due to performance issues, this only
happens when your Brush Size is 0 (drawing single-pixel shapes).
* UI: Add an IntVariable option to ui.Label to bind showing the value of
an int reference.
Aforementioned performance issues:
* When we try to remember whole rects of pixels for drawing thick
shapes, it requires a ton of scanning for each step of the shape. Even
de-duplicating pixel checks, tons of extra reads are constantly
checked.
* The Eraser is the only tool that absolutely needs to be able to
remember wiped pixels AND have large brush sizes. The performance
sucks and lags a bit if you erase a lot all at once, but it's a
trade-off for now.
* So pixels aren't remembered when drawing lines in your level with
thick brushes, so the Undo action will simply delete your pixels and not
reset them. Only the Eraser can bring back pixels.
2019-07-12 02:07:46 +00:00
|
|
|
// IterThickPoints iterates over the points and yield Rects of each one.
|
|
|
|
func (s *Stroke) IterThickPoints() chan render.Rect {
|
|
|
|
ch := make(chan render.Rect)
|
|
|
|
go func() {
|
|
|
|
for pt := range s.IterPoints() {
|
|
|
|
ch <- render.Rect{
|
2019-12-28 03:16:34 +00:00
|
|
|
X: pt.X - s.Thickness,
|
|
|
|
Y: pt.Y - s.Thickness,
|
|
|
|
W: s.Thickness * 2,
|
|
|
|
H: s.Thickness * 2,
|
Eraser Tool, Brush Sizes
* Implement Brush Sizes for drawtool.Stroke and add a UI to the tools panel
to control the brush size.
* Brush sizes: 1, 2, 4, 8, 16, 24, 32, 48, 64
* Add the Eraser Tool to editor mode. It uses a default brush size of 16
and a max size of 32 due to some performance issues.
* The Undo/Redo system now remembers the original color of pixels when
you change them, so that Undo will set them back how they were instead
of deleting the pixel entirely. Due to performance issues, this only
happens when your Brush Size is 0 (drawing single-pixel shapes).
* UI: Add an IntVariable option to ui.Label to bind showing the value of
an int reference.
Aforementioned performance issues:
* When we try to remember whole rects of pixels for drawing thick
shapes, it requires a ton of scanning for each step of the shape. Even
de-duplicating pixel checks, tons of extra reads are constantly
checked.
* The Eraser is the only tool that absolutely needs to be able to
remember wiped pixels AND have large brush sizes. The performance
sucks and lags a bit if you erase a lot all at once, but it's a
trade-off for now.
* So pixels aren't remembered when drawing lines in your level with
thick brushes, so the Undo action will simply delete your pixels and not
reset them. Only the Eraser can bring back pixels.
2019-07-12 02:07:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
close(ch)
|
|
|
|
}()
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
2019-07-03 23:22:30 +00:00
|
|
|
// AddPoint adds a point to the stroke, for freehand shapes.
|
|
|
|
func (s *Stroke) AddPoint(p render.Point) {
|
|
|
|
if _, ok := s.uniqPoint[p]; ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s.uniqPoint[p] = nil
|
|
|
|
s.Points = append(s.Points, p)
|
|
|
|
}
|