-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
54928dd
commit 7b7ea34
Showing
4 changed files
with
230 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
paint/src/main/scala/roguepaint/components/WindowManager.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package roguepaint.components | ||
|
||
import indigo.* | ||
import indigo.syntax.* | ||
import io.indigoengine.roguelike.starterkit.* | ||
|
||
object WindowManager: | ||
|
||
def updateModel( | ||
frameContext: FrameContext[Unit], | ||
model: WindowManagerModel | ||
): GlobalEvent => Outcome[WindowManagerModel] = | ||
case e => | ||
model.windows.map(_.update(frameContext, e)).sequence.map(m => model.copy(windows = m)) | ||
|
||
def updateViewModel( | ||
frameContext: FrameContext[Unit], | ||
model: WindowManagerModel, | ||
viewModel: WindowManagerViewModel | ||
): GlobalEvent => Outcome[WindowManagerViewModel] = | ||
case e => | ||
val updated = | ||
model.windows.flatMap { m => | ||
viewModel.prune(model).windows.find(_.id == m.id) match | ||
case None => | ||
Batch(Outcome(WindowViewModel.initial(m.id))) | ||
|
||
case Some(vm) => | ||
Batch(vm.update(frameContext, m, e)) | ||
} | ||
|
||
updated.sequence.map(vm => viewModel.copy(windows = vm)) | ||
|
||
def present( | ||
frameContext: FrameContext[Unit], | ||
model: WindowManagerModel, | ||
viewModel: WindowManagerViewModel | ||
): TerminalClones = | ||
model.windows | ||
.flatMap { m => | ||
viewModel.windows.find(_.id == m.id) match | ||
case None => | ||
// Shouldn't get here. | ||
Batch.empty | ||
|
||
case Some(vm) => | ||
Batch(Window.present(vm)) | ||
} | ||
.foldLeft(TerminalClones.empty)(_ |+| _) | ||
|
||
extension (tc: TerminalClones) | ||
def |+|(other: TerminalClones): TerminalClones = | ||
TerminalClones(tc.blanks ++ other.blanks, tc.clones ++ other.clones) | ||
|
||
final case class WindowManagerModel(windows: Batch[WindowModel]): | ||
def add(model: WindowModel): WindowManagerModel = | ||
this.copy(windows = windows :+ model) | ||
|
||
def remove(id: WindowId): WindowManagerModel = | ||
this.copy(windows = windows.filterNot(_.id == id)) | ||
|
||
def bringToTop(id: WindowId): WindowManagerModel = | ||
windows.find(_.id == id) match | ||
case None => | ||
this | ||
|
||
case Some(w) => | ||
this.copy(windows = windows.filterNot(_.id == id) :+ w) | ||
|
||
def update(frameContext: FrameContext[Unit], event: GlobalEvent): Outcome[WindowManagerModel] = | ||
WindowManager.updateModel(frameContext, this)(event) | ||
|
||
object WindowManagerModel: | ||
val initial: WindowManagerModel = | ||
WindowManagerModel(Batch.empty) | ||
|
||
final case class WindowManagerViewModel(windows: Batch[WindowViewModel]): | ||
def prune(model: WindowManagerModel): WindowManagerViewModel = | ||
this.copy(windows = windows.filter(w => model.windows.exists(_.id == w.id))) | ||
|
||
def update( | ||
frameContext: FrameContext[Unit], | ||
model: WindowManagerModel, | ||
event: GlobalEvent | ||
): Outcome[WindowManagerViewModel] = | ||
WindowManager.updateViewModel(frameContext, model, this)(event) | ||
|
||
object WindowManagerViewModel: | ||
val initial: WindowManagerViewModel = | ||
WindowManagerViewModel(Batch.empty) |