edbit/application/application.go

285 lines
6.2 KiB
Go

package application
import (
"errors"
"fmt"
"image"
"image/color"
"image/gif"
"image/jpeg"
"image/png"
"log"
"math"
"os"
"strings"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"golang.org/x/image/bmp"
"golang.org/x/image/colornames"
)
var spinner = []byte(`-\|/`)
type Application struct {
screenW, screenH int
cursorX, cursorY int
canvases []*canvas
active int
spinnerIndex int
hoverImg *ebiten.Image
op *ebiten.DrawImageOptions
}
func NewApplication() *Application {
app := &Application{
op: &ebiten.DrawImageOptions{},
}
app.addNewCanvas(8, 8)
c := app.activeCanvas()
c.img.Fill(colornames.Purple)
c.scale = 256
app.drawHoverImage()
return app
}
func (app *Application) readFile(p string) (image.Image, error) {
f, err := os.OpenFile(p, os.O_RDONLY, 0600)
if err != nil {
log.Fatal(err)
}
defer f.Close()
p = strings.ToLower(p)
if strings.HasSuffix(p, ".bmp") {
img, err := bmp.Decode(f)
if err != nil {
log.Fatalf("failed to decode BMP file: %s", err)
}
return img, nil
} else if strings.HasSuffix(p, ".gif") {
img, err := gif.Decode(f)
if err != nil {
log.Fatalf("failed to decode GIF file: %s", err)
}
return img, nil
} else if strings.HasSuffix(p, ".jpg") || strings.HasSuffix(p, ".jpeg") {
img, err := jpeg.Decode(f)
if err != nil {
log.Fatalf("failed to decode JPG file: %s", err)
}
return img, nil
} else if strings.HasSuffix(p, ".png") {
img, err := png.Decode(f)
if err != nil {
log.Fatalf("failed to decode PNG file: %s", err)
}
return img, nil
} else if strings.HasSuffix(p, ".tff") || strings.HasSuffix(p, ".tiff") {
img, err := png.Decode(f)
if err != nil {
log.Fatalf("failed to decode PNG file: %s", err)
}
return img, nil
}
return nil, errors.New("unknown image format")
}
func (app *Application) OpenFile(p string) {
img, err := app.readFile(p)
if err != nil {
log.Fatal(err)
}
c := NewCanvas(img.Bounds().Dx(), img.Bounds().Dy())
c.img = ebiten.NewImageFromImage(img)
app.addCanvas(c)
}
func (app *Application) addCanvas(c *canvas) {
app.canvases = append(app.canvases, c)
app.active++
app.drawHoverImage()
}
func (app *Application) addNewCanvas(w, h int) {
c := NewCanvas(w, h)
app.canvases = append(app.canvases, c)
app.drawHoverImage()
}
func (app *Application) activeCanvas() *canvas {
return app.canvases[app.active]
}
func (app *Application) screenToCanvas(x, y int) (int, int) {
c := app.activeCanvas()
cx, cy := float64(x)/c.scale, float64(y)/c.scale
cx, cy = cx+c.x, cy+c.y
return int(cx), int(cy)
}
func (app *Application) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
s := ebiten.DeviceScaleFactor()
outsideWidth, outsideHeight = int(float64(outsideWidth)*s), int(float64(outsideHeight)*s)
if app.screenW == outsideWidth && app.screenH == outsideHeight {
return outsideWidth, outsideHeight
}
app.screenW, app.screenH = outsideWidth, outsideHeight
return app.screenW, app.screenH
}
func (app *Application) Update() error {
if ebiten.IsWindowBeingClosed() {
app.Exit()
return nil
}
if _, y := ebiten.Wheel(); y != 0 {
scroll := math.Ceil(y)
if scroll < -1 {
scroll = -1
} else if scroll > 1 {
scroll = 1
}
c := app.activeCanvas()
offset := scroll * (c.scale / 7)
c.scale += offset
if c.scale < .001 {
c.scale = .001
} else if c.scale > 1000 {
c.scale = 1000
}
if c.scale > 10 {
c.scale = float64(int(c.scale))
} else {
c.scale = math.Trunc(c.scale*10000) / 10000
}
app.drawHoverImage()
}
app.cursorX, app.cursorY = ebiten.CursorPosition()
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
cx, cy := app.screenToCanvas(app.cursorX, app.cursorY)
c := app.activeCanvas()
c.img.Set(cx, cy, colornames.White)
}
c := app.activeCanvas()
panSize := 2 / c.scale
if ebiten.IsKeyPressed(ebiten.KeyRight) {
c.x += panSize
}
if ebiten.IsKeyPressed(ebiten.KeyLeft) {
c.x -= panSize
}
if ebiten.IsKeyPressed(ebiten.KeyUp) {
c.y -= panSize
}
if ebiten.IsKeyPressed(ebiten.KeyDown) {
c.y += panSize
}
return nil
}
func (app *Application) Draw(screen *ebiten.Image) {
// Draw canvas.
c := app.activeCanvas()
app.op.GeoM.Reset()
app.op.GeoM.Translate(-c.x, -c.y)
app.op.GeoM.Scale(c.scale, c.scale)
screen.DrawImage(c.img, app.op)
// Draw hover outline.
cx, cy := app.screenToCanvas(app.cursorX, app.cursorY)
p := c.pixelScreenRect(cx, cy)
app.op.GeoM.Reset()
app.op.GeoM.Translate(float64(p.Min.X), float64(p.Min.Y))
screen.DrawImage(app.hoverImg, app.op)
// Print debug information.
debugInfo := fmt.Sprintf("SCA %0.2f\nFPS %c %0.0f", app.activeCanvas().scale, spinner[app.spinnerIndex], ebiten.CurrentFPS())
debugBox := image.NewRGBA(image.Rect(10, 20, 200, 200))
debugImg := ebiten.NewImageFromImage(debugBox)
ebitenutil.DebugPrint(debugImg, debugInfo)
app.op.GeoM.Reset()
app.op.GeoM.Translate(3, 0)
app.op.GeoM.Scale(2, 2)
screen.DrawImage(debugImg, app.op)
app.spinnerIndex++
if app.spinnerIndex == 4 {
app.spinnerIndex = 0
}
}
func (app *Application) drawHoverImage() {
c := app.activeCanvas()
hoverImgSize := int(c.scale)
if hoverImgSize < 1 {
hoverImgSize = 1
}
if app.hoverImg == nil || app.hoverImg.Bounds().Dx() != hoverImgSize || app.hoverImg.Bounds().Dy() != hoverImgSize {
app.hoverImg = ebiten.NewImage(hoverImgSize, hoverImgSize)
}
var colorWhite bool
nextColor := func() color.Color {
colorWhite = !colorWhite
if colorWhite {
return color.White
} else {
return color.Black
}
}
img := app.hoverImg
img.Clear()
bounds := img.Bounds()
outlineWidth := 2
if app.activeCanvas().scale >= 1000 {
outlineWidth = 6
} else if app.activeCanvas().scale >= 200 {
outlineWidth = 5
} else if app.activeCanvas().scale >= 100 {
outlineWidth = 4
} else if app.activeCanvas().scale >= 10 {
outlineWidth = 3
}
for y := 0; y < outlineWidth; y++ {
for x := 0; x < bounds.Max.X; x++ {
c := nextColor()
img.Set(x, bounds.Min.Y+y, c)
img.Set(x, bounds.Max.Y-y, c)
}
}
for x := 0; x < outlineWidth; x++ {
for y := 0; y < bounds.Max.Y; y++ {
c := nextColor()
img.Set(bounds.Min.X+x, y, c)
img.Set(bounds.Max.X-x, y, c)
}
}
}
func (app *Application) Exit() {
os.Exit(0)
}