• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Martomate / TriPaint / 9275511596

28 May 2024 07:51PM UTC coverage: 29.682% (+1.8%) from 27.877%
9275511596

push

github

Martomate
Updated to Scala 3.4 and MUnit 1.0

2 of 11 new or added lines in 9 files covered. (18.18%)

430 existing lines in 38 files now uncovered.

401 of 1351 relevant lines covered (29.68%)

0.3 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

27.27
/src/main/scala/tripaint/control/Actions.scala
1
package tripaint.control
2

3
import tripaint.infrastructure.FileSystem
4
import tripaint.model.{Color, ImageGrid, ImageGridChange, TriPaintModel}
5
import tripaint.model.coords.{GridCoords, StorageCoords}
6
import tripaint.model.effects.Effect
7
import tripaint.model.image.{
8
  GridCell,
9
  ImageChange,
10
  ImagePool,
11
  ImageSaveCollisionHandler,
12
  ImageStorage
13
}
14
import tripaint.model.image.ImagePool.{SaveInfo, SaveLocation}
15
import tripaint.model.image.format.StorageFormat
16
import tripaint.util.CachedLoader
17
import tripaint.view.{FileOpenSettings, FileSaveSettings}
18

19
import java.io.File
20
import scala.collection.mutable
21
import scala.util.{Failure, Success, Try}
22

23
object Actions {
UNCOV
24
  def save(
×
25
      imageGrid: ImageGrid,
26
      imagePool: ImagePool,
27
      images: Seq[GridCell],
28
      fileSystem: FileSystem
29
  )(
30
      askForSaveFile: GridCell => Option[File],
31
      askForFileSaveSettings: (File, GridCell) => Option[FileSaveSettings],
32
      imageSaveCollisionHandler: ImageSaveCollisionHandler
33
  ): Boolean = {
34
    images
35
      .filter(im => !trySaveImage(imageGrid, imagePool, im.storage, fileSystem))
×
36
      .forall(im =>
×
37
        trySaveImage(imageGrid, imagePool, im.storage, fileSystem) ||
×
UNCOV
38
          saveAs(imageGrid, imagePool, im, fileSystem)(
×
39
            askForSaveFile,
40
            askForFileSaveSettings,
41
            imageSaveCollisionHandler
42
          )
43
      )
44
  }
45

UNCOV
46
  private def trySaveImage(
×
47
      imageGrid: ImageGrid,
48
      imagePool: ImagePool,
49
      image: ImageStorage,
50
      fileSystem: FileSystem
51
  ): Boolean =
52
    imagePool.getSaveLocationAndInfo(image) match
×
53
      case (Some(loc), Some(info)) => imageGrid.save(image, fileSystem, loc, info)
×
UNCOV
54
      case _                       => false
×
55

UNCOV
56
  def saveAs(imageGrid: ImageGrid, imagePool: ImagePool, image: GridCell, fileSystem: FileSystem)(
×
57
      askForSaveFile: GridCell => Option[File],
58
      askForFileSaveSettings: (File, GridCell) => Option[FileSaveSettings],
59
      imageSaveCollisionHandler: ImageSaveCollisionHandler
60
  ): Boolean =
61
    val didMoveOpt = for
×
62
      file <- askForSaveFile(image)
×
UNCOV
63
      settings <- askForFileSaveSettings(file, image)
×
64
    yield
65
      val location = ImagePool.SaveLocation(file, settings.offset)
66
      val info = ImagePool.SaveInfo(settings.format)
UNCOV
67
      imageGrid.setImageSource(image.storage, location, info)(
×
68
        imagePool,
69
        imageSaveCollisionHandler
70
      )
71

UNCOV
72
    val didMove = didMoveOpt.getOrElse(false)
×
73

74
    if didMove then
75
      val saved = trySaveImage(imageGrid, imagePool, image.storage, fileSystem)
×
UNCOV
76
      if (!saved) println("Image could not be saved!!")
×
77
      saved
UNCOV
78
    else false
×
79

80
  def createNewImage(imageGrid: ImageGrid, backgroundColor: Color, coords: GridCoords): Unit =
1✔
81
    val storage = ImageStorage.fill(imageGrid.imageSize, backgroundColor)
1✔
82
    imageGrid.set(new GridCell(coords, storage))
1✔
83

84
  def openImage(
1✔
85
      model: TriPaintModel,
86
      file: File,
87
      fileOpenSettings: FileOpenSettings,
88
      whereToPutImage: GridCoords
89
  ): Unit =
90
    val FileOpenSettings(offset, format) = fileOpenSettings
1✔
91
    val location = ImagePool.SaveLocation(file, offset)
92
    val imageSize = model.imageGrid.imageSize
1✔
93

94
    CachedLoader(
1✔
95
      cached = model.imagePool.imageAt(location),
1✔
96
      load = loadImageFromFile(location, format, imageSize, model.fileSystem)
1✔
97
    ) match
98
      case Success((image, found)) =>
99
        if !found then model.imagePool.set(image, location, SaveInfo(format))
1✔
100
        model.imageGrid.set(new GridCell(whereToPutImage, image))
1✔
101
      case Failure(exc) => exc.printStackTrace()
1✔
102

103
  private def loadImageFromFile(
1✔
104
      location: SaveLocation,
105
      format: StorageFormat,
106
      imageSize: Int,
107
      fileSystem: FileSystem
108
  ): Try[ImageStorage] =
109
    for
1✔
110
      regularImage <- fileSystem.readImage(location.file) match
1✔
111
        case Some(im) => Success(im)
1✔
112
        case None     => Failure(new RuntimeException("no such image"))
1✔
113
      image <- ImageStorage.fromRegularImage(regularImage, location.offset, format, imageSize)
1✔
114
    yield image
115

UNCOV
116
  def openHexagon(
×
117
      model: TriPaintModel,
118
      file: File,
119
      fileOpenSettings: FileOpenSettings,
120
      coords: GridCoords
121
  ): Unit =
122
    val imageSize = model.imageGrid.imageSize
×
UNCOV
123
    val FileOpenSettings(offset, format) = fileOpenSettings
×
124

UNCOV
125
    def coordOffset(idx: Int): (Int, Int) =
×
126
      idx match
127
        case 0 => (0, 0)
×
128
        case 1 => (-1, 0)
×
129
        case 2 => (-2, 0)
×
130
        case 3 => (-1, -1)
×
131
        case 4 => (0, -1)
×
UNCOV
132
        case 5 => (1, -1)
×
133

UNCOV
134
    for idx <- 0 until 6 do
×
135
      val imageOffset = StorageCoords(offset.x + idx * imageSize, offset.y)
136

137
      val off = coordOffset(idx)
×
138
      val whereToPutImage = GridCoords(coords.x + off._1, coords.y + off._2)
139

UNCOV
140
      openImage(model, file, FileOpenSettings(imageOffset, format), whereToPutImage)
×
141

UNCOV
142
  def applyEffect(model: TriPaintModel, effect: Effect): Unit =
×
143
    val grid = model.imageGrid
UNCOV
144
    val images = grid.selectedImages
×
145

UNCOV
146
    val before = for im <- images yield im.storage.allPixels.map(im.storage.getColor)
×
147

UNCOV
148
    effect.action(images.map(_.coords), grid)
×
149

UNCOV
150
    val after = for im <- images yield im.storage.allPixels.map(im.storage.getColor)
×
151

152
    val changes = mutable.Map.empty[GridCoords, ImageChange]
×
153
    for here <- images.indices do
×
154
      val image = images(here)
×
UNCOV
155
      val allPixels = image.storage.allPixels
×
156

157
      val changeBuilder = new ImageChange.Builder
×
158
      for
×
159
        neigh <- allPixels.indices
×
160
        if before(here)(neigh) != after(here)(neigh)
×
UNCOV
161
      yield changeBuilder.addChange(allPixels(neigh), before(here)(neigh), after(here)(neigh))
×
162

UNCOV
163
      if changeBuilder.nonEmpty then changes(image.coords) = changeBuilder.done(image.storage)
×
164

UNCOV
165
    for (coords, change) <- changes do change.undo()
×
166

UNCOV
167
    grid.performChange(new ImageGridChange(changes.toMap))
×
168
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc