retab
This commit is contained in:
@@ -16,119 +16,119 @@ const height = 512
|
|||||||
type appOption func(a *app)
|
type appOption func(a *app)
|
||||||
|
|
||||||
type app struct {
|
type app struct {
|
||||||
strokes []stroke
|
strokes []stroke
|
||||||
width float32
|
width float32
|
||||||
canvas *ebiten.Image
|
canvas *ebiten.Image
|
||||||
sizeAnchorPos Vec2
|
sizeAnchorPos Vec2
|
||||||
historyIndex uintptr
|
historyIndex uintptr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *app) Draw(screen *ebiten.Image) {
|
func (a *app) Draw(screen *ebiten.Image) {
|
||||||
screen.Fill(color.White)
|
screen.Fill(color.White)
|
||||||
|
|
||||||
opts := ebiten.DrawImageOptions{}
|
opts := ebiten.DrawImageOptions{}
|
||||||
screen.DrawImage(a.canvas, &opts)
|
screen.DrawImage(a.canvas, &opts)
|
||||||
|
|
||||||
cursor := cursorPosition()
|
cursor := cursorPosition()
|
||||||
vector.StrokeCircle(
|
vector.StrokeCircle(
|
||||||
screen,
|
screen,
|
||||||
cursor.x,
|
cursor.x,
|
||||||
cursor.y,
|
cursor.y,
|
||||||
a.width/2,
|
a.width/2,
|
||||||
3,
|
3,
|
||||||
color.White,
|
color.White,
|
||||||
true,
|
true,
|
||||||
)
|
)
|
||||||
vector.StrokeCircle(
|
vector.StrokeCircle(
|
||||||
screen,
|
screen,
|
||||||
cursor.x,
|
cursor.x,
|
||||||
cursor.y,
|
cursor.y,
|
||||||
a.width/2,
|
a.width/2,
|
||||||
1,
|
1,
|
||||||
color.Black,
|
color.Black,
|
||||||
true,
|
true,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *app) Layout(_, _ int) (screenWidth int, screenHeight int) {
|
func (a *app) Layout(_, _ int) (screenWidth int, screenHeight int) {
|
||||||
return width, height
|
return width, height
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *app) Update() error {
|
func (a *app) Update() error {
|
||||||
|
|
||||||
check_any_drawing_input := func(checker func(ebiten.Key) bool) bool {
|
check_any_drawing_input := func(checker func(ebiten.Key) bool) bool {
|
||||||
return drawWhite.check(checker) || drawBlack.check(checker)
|
return drawWhite.check(checker) || drawBlack.check(checker)
|
||||||
}
|
}
|
||||||
|
|
||||||
is_drawing := check_any_drawing_input(ebiten.IsKeyPressed)
|
is_drawing := check_any_drawing_input(ebiten.IsKeyPressed)
|
||||||
just_started_drawing := check_any_drawing_input(inpututil.IsKeyJustPressed)
|
just_started_drawing := check_any_drawing_input(inpututil.IsKeyJustPressed)
|
||||||
|
|
||||||
var clr color.Color
|
var clr color.Color
|
||||||
if drawBlack.check(ebiten.IsKeyPressed) {
|
if drawBlack.check(ebiten.IsKeyPressed) {
|
||||||
clr = color.Black
|
clr = color.Black
|
||||||
} else if drawWhite.check(ebiten.IsKeyPressed) {
|
} else if drawWhite.check(ebiten.IsKeyPressed) {
|
||||||
clr = color.White
|
clr = color.White
|
||||||
}
|
}
|
||||||
|
|
||||||
if just_started_drawing {
|
if just_started_drawing {
|
||||||
if clr == nil {
|
if clr == nil {
|
||||||
log.Fatal("drawing color in update is nil") // should never happen
|
log.Fatal("drawing color in update is nil") // should never happen
|
||||||
}
|
}
|
||||||
a.strokes = append(a.strokes, stroke{
|
a.strokes = append(a.strokes, stroke{
|
||||||
points: []Vec2{},
|
points: []Vec2{},
|
||||||
width: a.width,
|
width: a.width,
|
||||||
color: clr,
|
color: clr,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
// TODO current history pointer is not updated as it should
|
// TODO current history pointer is not updated as it should
|
||||||
// TODO tests
|
// TODO tests
|
||||||
|
|
||||||
if len(a.strokes) > 0 {
|
if len(a.strokes) > 0 {
|
||||||
currentStroke := &a.strokes[len(a.strokes)-1]
|
currentStroke := &a.strokes[len(a.strokes)-1]
|
||||||
if is_drawing {
|
if is_drawing {
|
||||||
currentStroke.appendIfMoved(cursorPosition())
|
currentStroke.appendIfMoved(cursorPosition())
|
||||||
currentStroke.drawLast(a.canvas)
|
currentStroke.drawLast(a.canvas)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if resize.check(inpututil.IsKeyJustPressed) {
|
if resize.check(inpututil.IsKeyJustPressed) {
|
||||||
a.sizeAnchorPos = cursorPosition()
|
a.sizeAnchorPos = cursorPosition()
|
||||||
}
|
}
|
||||||
|
|
||||||
if resize.check(ebiten.IsKeyPressed) {
|
if resize.check(ebiten.IsKeyPressed) {
|
||||||
prevPos := a.sizeAnchorPos
|
prevPos := a.sizeAnchorPos
|
||||||
currPos := cursorPosition()
|
currPos := cursorPosition()
|
||||||
reltivePos := Vec2{
|
reltivePos := Vec2{
|
||||||
prevPos.x - currPos.x,
|
prevPos.x - currPos.x,
|
||||||
prevPos.y - currPos.y,
|
prevPos.y - currPos.y,
|
||||||
}
|
}
|
||||||
length := float32(math.Sqrt(
|
length := float32(math.Sqrt(
|
||||||
float64(reltivePos.x) * float64(reltivePos.x) + float64(reltivePos.y) * float64(reltivePos.y),
|
float64(reltivePos.x) * float64(reltivePos.x) + float64(reltivePos.y) * float64(reltivePos.y),
|
||||||
))
|
))
|
||||||
a.width = length * 2.0
|
a.width = length * 2.0
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func newApp( opts ...appOption ) app {
|
func newApp( opts ...appOption ) app {
|
||||||
a := app{}
|
a := app{}
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
opt(&a)
|
opt(&a)
|
||||||
}
|
}
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
ebiten.SetWindowTitle("whiteboard")
|
ebiten.SetWindowTitle("whiteboard")
|
||||||
ebiten.SetWindowResizingMode(ebiten.WindowResizingModeEnabled)
|
ebiten.SetWindowResizingMode(ebiten.WindowResizingModeEnabled)
|
||||||
ebiten.CursorPosition()
|
ebiten.CursorPosition()
|
||||||
whiteboardApp := newApp( func(a *app) {
|
whiteboardApp := newApp( func(a *app) {
|
||||||
a.width = 10
|
a.width = 10
|
||||||
a.canvas = ebiten.NewImage(width, height)
|
a.canvas = ebiten.NewImage(width, height)
|
||||||
a.canvas.Fill(color.White)
|
a.canvas.Fill(color.White)
|
||||||
})
|
})
|
||||||
|
|
||||||
ebiten.RunGame(&whiteboardApp)
|
ebiten.RunGame(&whiteboardApp)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user