func handleCursor(screen *ebiten.Image) error { mx, my := ebiten.CursorPosition() cursorMoved := image.Point{mx, my}.In(screen.Bounds()) && (mx != oldMouseCursorPosition.X || my != oldMouseCursorPosition.Y) oldMouseCursorPosition = image.Point{mx, my} if cursorMoved { cursorPosition = oldMouseCursorPosition } else { const cursorInterval = 6 switch { case keyStates[ebiten.KeyUp]%cursorInterval == 0: cursorPosition = cursorPosition.Add(image.Point{0, -1}) cursorMoved = true case keyStates[ebiten.KeyDown]%cursorInterval == 0: cursorPosition = cursorPosition.Add(image.Point{0, +1}) cursorMoved = true case keyStates[ebiten.KeyLeft]%cursorInterval == 0: cursorPosition = cursorPosition.Add(image.Point{-1, 0}) cursorMoved = true case keyStates[ebiten.KeyRight]%cursorInterval == 0: cursorPosition = cursorPosition.Add(image.Point{+1, 0}) cursorMoved = true } } if cursorBlinking == 127 { cursorBlinking = 0 } else { cursorBlinking++ } op := &ebiten.DrawImageOptions{} op.GeoM.Scale(0.25, .25) op.GeoM.Translate(float64(cursorPosition.X), float64(cursorPosition.Y)) if cursorBlinking > 64 { op.ColorM.Scale(1, 1, 1, 0.25+float64(127-cursorBlinking)/255.0) } else { op.ColorM.Scale(1, 1, 1, 0.25+float64(cursorBlinking)/255.0) } if err := screen.DrawImage(cursorImage, op); err != nil { return err } if keyStates[ebiten.KeySpace] >= 0 || ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) { if cursorMoved || !wasMouseButtonPressed { if err := togglePixel(cursorPosition); err != nil { return err } wasMouseButtonPressed = true } } else { wasMouseButtonPressed = false } return nil }
func (r *recorder) update(screen *ebiten.Image) error { if err := r.inner(screen); err != nil { return err } if r.currentFrame == r.frameNum { return nil } if r.currentFrame%r.skips == 0 { if r.gif == nil { num := (r.frameNum-1)/r.skips + 1 r.gif = &gif.GIF{ Image: make([]*image.Paletted, num), Delay: make([]int, num), LoopCount: -1, } } s := image.NewNRGBA(screen.Bounds()) draw.Draw(s, s.Bounds(), screen, screen.Bounds().Min, draw.Src) img := image.NewPaletted(s.Bounds(), r.palette()) f := r.currentFrame / r.skips r.wg.Add(1) go func() { defer r.wg.Done() draw.FloydSteinberg.Draw(img, img.Bounds(), s, s.Bounds().Min) r.gif.Image[f] = img r.gif.Delay[f] = r.delay() }() } r.currentFrame++ if r.currentFrame == r.frameNum { r.wg.Wait() if err := gif.EncodeAll(r.writer, r.gif); err != nil { return err } } return nil }