main.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright 2023 The Ebitengine Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "image/png"
  17. "io/fs"
  18. "log"
  19. "sync"
  20. "github.com/hajimehoshi/ebiten/v2"
  21. "github.com/hajimehoshi/ebiten/v2/ebitenutil"
  22. )
  23. type Game struct {
  24. images []*ebiten.Image
  25. err error
  26. m sync.Mutex
  27. }
  28. func (g *Game) Update() error {
  29. if err := func() error {
  30. g.m.Lock()
  31. defer g.m.Unlock()
  32. return g.err
  33. }(); err != nil {
  34. return err
  35. }
  36. if files := ebiten.DroppedFiles(); files != nil {
  37. go func() {
  38. if err := fs.WalkDir(files, ".", func(path string, d fs.DirEntry, err error) error {
  39. if err != nil {
  40. return err
  41. }
  42. fi, err := d.Info()
  43. if err != nil {
  44. return err
  45. }
  46. log.Printf("Name: %s, Size: %d, IsDir: %t, ModTime: %v", fi.Name(), fi.Size(), fi.IsDir(), fi.ModTime())
  47. f, err := files.Open(path)
  48. if err != nil {
  49. return err
  50. }
  51. defer func() {
  52. _ = f.Close()
  53. }()
  54. img, err := png.Decode(f)
  55. if err != nil {
  56. return nil
  57. }
  58. eimg := ebiten.NewImageFromImage(img)
  59. g.m.Lock()
  60. g.images = append(g.images, eimg)
  61. g.m.Unlock()
  62. return nil
  63. }); err != nil {
  64. g.m.Lock()
  65. if g.err == nil {
  66. g.err = err
  67. }
  68. g.m.Unlock()
  69. }
  70. }()
  71. }
  72. return nil
  73. }
  74. func (g *Game) Draw(screen *ebiten.Image) {
  75. g.m.Lock()
  76. defer g.m.Unlock()
  77. if len(g.images) == 0 {
  78. ebitenutil.DebugPrint(screen, "Drop PNG files onto this window!")
  79. return
  80. }
  81. const imageSize = 128
  82. xcount := screen.Bounds().Dx() / imageSize
  83. if xcount == 0 {
  84. return
  85. }
  86. for i, img := range g.images {
  87. x := (i % xcount) * imageSize
  88. y := (i / xcount) * imageSize
  89. s := imageSize / float64(img.Bounds().Dx())
  90. if sy := imageSize / float64(img.Bounds().Dy()); s > sy {
  91. s = sy
  92. }
  93. if s > 1 {
  94. s = 1
  95. }
  96. op := &ebiten.DrawImageOptions{}
  97. op.GeoM.Scale(s, s)
  98. op.GeoM.Translate(float64(x), float64(y))
  99. op.Filter = ebiten.FilterLinear
  100. screen.DrawImage(img, op)
  101. }
  102. }
  103. func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
  104. return outsideWidth, outsideHeight
  105. }
  106. func main() {
  107. ebiten.SetWindowResizable(true)
  108. ebiten.SetWindowTitle("Dropping Files (Ebitengine Demo)")
  109. if err := ebiten.RunGame(&Game{}); err != nil {
  110. log.Fatal(err)
  111. }
  112. }