Skip to content

Commit

Permalink
Simplified MVC setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Martomate committed Sep 7, 2024
1 parent 1f5752c commit 13782b9
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 132 deletions.
27 changes: 16 additions & 11 deletions app/src/main/kotlin/tripaint/app/Actions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,24 @@ object Actions {
}

fun openImage(
model: TriPaintModel,
fileSystem: FileSystem,
imagePool: ImagePool,
imageGrid: ImageGrid,
file: File,
fileOpenSettings: FileOpenSettings,
whereToPutImage: GridCoords
) {
val (offset, format) = fileOpenSettings
val location = ImagePool.SaveLocation(file, offset)
val imageSize = model.imageGrid.imageSize
val imageSize = imageGrid.imageSize

CachedLoader.apply(
cached = { model.imagePool.imageAt(location) },
load = { loadImageFromFile(location, format, imageSize, model.fileSystem) }
cached = { imagePool.imageAt(location) },
load = { loadImageFromFile(location, format, imageSize, fileSystem) }
).onSuccess {
val (image, found) = it
if (!found) model.imagePool.set(image, location, ImagePool.SaveInfo(format))
model.imageGrid.set(GridCell(whereToPutImage, image))
if (!found) imagePool.set(image, location, ImagePool.SaveInfo(format))
imageGrid.set(GridCell(whereToPutImage, image))
}.onFailure {
it.printStackTrace()
}
Expand All @@ -109,8 +111,11 @@ object Actions {
return ImageStorage.fromRegularImage(im, location.offset, format, imageSize)
}

fun openHexagon(model: TriPaintModel, file: File, fileOpenSettings: FileOpenSettings, coords: GridCoords) {
val imageSize = model.imageGrid.imageSize
fun openHexagon(
fileSystem: FileSystem, imagePool: ImagePool, imageGrid: ImageGrid,
file: File, fileOpenSettings: FileOpenSettings, coords: GridCoords
) {
val imageSize = imageGrid.imageSize
val (offset, format) = fileOpenSettings

fun coordOffset(idx: Int): Pair<Int, Int> {
Expand All @@ -131,12 +136,12 @@ object Actions {
val off = coordOffset(idx)
val whereToPutImage = GridCoords.from(coords.x + off.first, coords.y + off.second)

openImage(model, file, FileOpenSettings(imageOffset, format), whereToPutImage)
openImage(fileSystem, imagePool, imageGrid, file, FileOpenSettings(imageOffset, format), whereToPutImage)
}
}

fun applyEffect(model: TriPaintModel, effect: Effect) {
val grid = model.imageGrid
fun applyEffect(imageGrid: ImageGrid, effect: Effect) {
val grid = imageGrid
val images = grid.selectedImages()

val before = images.map { im -> im.storage.allPixels().map { pix -> im.storage.getColor(pix) } }
Expand Down
54 changes: 29 additions & 25 deletions app/src/main/kotlin/tripaint/app/MainStage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import tripaint.effects.BlurEffect
import tripaint.effects.MotionBlurEffect
import tripaint.effects.RandomNoiseEffect
import tripaint.grid.GridCell
import tripaint.grid.ImageGrid
import tripaint.image.ImagePool
import tripaint.image.ImageStorage
import tripaint.image.format.RecursiveStorageFormat
Expand All @@ -29,31 +30,34 @@ import java.io.File

class MainStage(
private val controls: TriPaintViewListener,
private val model: TriPaintModel,
private val fileSystem: FileSystem,
private val imagePool: ImagePool,
private val imageGrid: ImageGrid,
private val stage: Stage,
) : TriPaintView {
private val currentEditMode: Resource<EditMode>
private val setCurrentEditMode: (EditMode) -> Unit

init {
val res = Resource.createResource(EditMode.Draw)
this.currentEditMode = res.first
this.setCurrentEditMode = res.second
Resource.createResource(EditMode.Draw).let {
this.currentEditMode = it.first
this.setCurrentEditMode = it.second
}
}

private val imageDisplay: ImageGridPane = ImageGridPane(model.imageGrid, currentEditMode)
private val imageDisplay: ImageGridPane = ImageGridPane(imageGrid, currentEditMode)

private val menuBar: MenuBar = TheMenuBar.create(controls)
private val toolBar: ToolBar = TheToolBar.create(controls)
private val toolBox: TilePane = ToolBox.create(EditMode.all(), currentEditMode, setCurrentEditMode)
private val imageTabs: TilePane =
ImageTabs.fromImagePool(model.imageGrid, model.imagePool, controls::requestImageRemoval)
ImageTabs.fromImagePool(imageGrid, imagePool, controls::requestImageRemoval)
private val colorBox: VBox = makeColorBox()

private var currentFolder: File? = null

init {
stage.setTitle("TriPaint")
stage.title = "TriPaint"
stage.setOnCloseRequest { e ->
if (!controls.requestExit()) e.consume()
}
Expand All @@ -80,7 +84,7 @@ class MainStage(
val sceneContents = BorderPane(centerPane, topPane, null, null, null)

val scene = Scene(sceneContents, 720.0, 720.0)
scene.stylesheets.add(MainStage::class.java.getResource("/styles/application.css").toExternalForm())
scene.stylesheets.add(MainStage::class.java.getResource("/styles/application.css")!!.toExternalForm())

for (m in EditMode.all()) {
if (m.shortCut != null) {
Expand All @@ -105,11 +109,11 @@ class MainStage(
}

imageDisplay.colors.primaryColor.addListener { _, from, to ->
if (from != to) colorPicker1.setValue(to.toFXColor())
if (from != to) colorPicker1.value = to.toFXColor()
}

imageDisplay.colors.secondaryColor.addListener { _, from, to ->
if (from != to) colorPicker2.setValue(to.toFXColor())
if (from != to) colorPicker2.value = to.toFXColor()
}

return VBox(
Expand Down Expand Up @@ -160,10 +164,10 @@ class MainStage(
}

override fun askForBlurRadius(): Int? {
val selectedImagesCoords = model.imageGrid.selectedImages().map { it.coords }
val selectedImagesCoords = imageGrid.selectedImages().map { it.coords }
return DialogUtils.getValueFromDialog<Int>(
model.imagePool,
model.imageGrid.selectedImages(),
imagePool,
imageGrid.selectedImages(),
"Blur images",
"How much should the images be blurred?",
"Radius:",
Expand All @@ -174,10 +178,10 @@ class MainStage(
}

override fun askForMotionBlurRadius(): Int? {
val selectedImagesCoords = model.imageGrid.selectedImages().map { it.coords }
val selectedImagesCoords = imageGrid.selectedImages().map { it.coords }
return DialogUtils.getValueFromDialog(
model.imagePool,
model.imageGrid.selectedImages(),
imagePool,
imageGrid.selectedImages(),
"Motion blur images",
"How much should the images be motion blurred?",
"Radius:",
Expand All @@ -188,12 +192,12 @@ class MainStage(
}

override fun askForRandomNoiseColors(): Pair<Color, Color>? {
val images = model.imageGrid.selectedImages()
val selectedImagesCoords = model.imageGrid.selectedImages().map { it.coords }
val images = imageGrid.selectedImages()
val selectedImagesCoords = imageGrid.selectedImages().map { it.coords }
val loColorPicker = ColorPicker(Color.Black.toFXColor())
val hiColorPicker = ColorPicker(Color.White.toFXColor())

val (previewPane, updatePreview) = DialogUtils.makeImagePreviewList(images, model.imagePool)
val (previewPane, updatePreview) = DialogUtils.makeImagePreviewList(images, imagePool)

fun updatePreviewFromInputs() {
val lo = fromFXColor(loColorPicker.value)
Expand Down Expand Up @@ -244,7 +248,7 @@ class MainStage(
}

private fun saveBeforeClosingAlert(images: List<GridCell>): Alert {
val (previewPane, _) = DialogUtils.makeImagePreviewList(images, model.imagePool)
val (previewPane, _) = DialogUtils.makeImagePreviewList(images, imagePool)

val alert = Alert(Alert.AlertType.CONFIRMATION)
alert.title = "Save before closing?"
Expand Down Expand Up @@ -273,17 +277,17 @@ class MainStage(
Pair(RecursiveStorageFormat, "Recursive format")
),
0
) { model.fileSystem.readImage(it) }
) { fileSystem.readImage(it) }
}

override fun shouldReplaceImage(
currentImage: ImageStorage,
newImage: ImageStorage,
location: ImagePool.SaveLocation
): Boolean? {
val tri1 = model.imageGrid.findByStorage(newImage)!!
val tri2 = model.imageGrid.findByStorage(currentImage)!!
val (previewPane, _) = DialogUtils.makeImagePreviewList(listOf(tri1, tri2), model.imagePool)
val tri1 = imageGrid.findByStorage(newImage)!!
val tri2 = imageGrid.findByStorage(currentImage)!!
val (previewPane, _) = DialogUtils.makeImagePreviewList(listOf(tri1, tri2), imagePool)

val alert = Alert(Alert.AlertType.CONFIRMATION)
alert.title = "Collision"
Expand All @@ -295,7 +299,7 @@ class MainStage(
ButtonType("Right", ButtonBar.ButtonData.NO),
ButtonType("Cancel", ButtonBar.ButtonData.CANCEL_CLOSE)
)
return alert.showAndWait().orElse(null)?.buttonData == ButtonBar.ButtonData.YES
return alert.showAndWait().map { it.buttonData == ButtonBar.ButtonData.YES }.orElse(null)
}

override fun askForImageSize(): Int? {
Expand Down
8 changes: 1 addition & 7 deletions app/src/main/kotlin/tripaint/app/TriPaint.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package tripaint.app

import javafx.application.Application
import javafx.application.Platform
import javafx.stage.Stage

object TriPaint {
Expand All @@ -13,12 +12,7 @@ object TriPaint {

class App : Application() {
override fun start(stage: Stage) {
val model: TriPaintModel = TriPaintModel.create()
val controller = TriPaintController(model) { c, m -> MainStage(c, m, stage) }

Platform.runLater {
model.imageGrid.setImageSizeIfEmpty(controller.view.askForImageSize() ?: 32)
}
TriPaintController(stage, FileSystem.create())

stage.show()
}
Expand Down
Loading

0 comments on commit 13782b9

Please sign in to comment.