Skip to content

Commit

Permalink
Fixed #102: The top window under the mouse is considered active
Browse files Browse the repository at this point in the history
  • Loading branch information
davesmith00000 committed Jun 9, 2024
1 parent 7193273 commit 615a167
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 7 deletions.
2 changes: 1 addition & 1 deletion demo/src/main/scala/demo/ComponentsWindow.scala
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ object ComponentsWindow:
)
)
.withTitle("Components example")
.moveTo(0, 0)
.moveTo(0, 3)
.resizeTo(25, 25)
.isDraggable
.isResizable
Expand Down
2 changes: 1 addition & 1 deletion demo/src/main/scala/demo/ComponentsWindow2.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ object ComponentsWindow2:
)
)
.withTitle("More component examples")
.moveTo(2, 2)
.moveTo(2, 5)
.resizeTo(25, 25)
.isDraggable
.isResizable
Expand Down
46 changes: 46 additions & 0 deletions demo/src/main/scala/demo/MenuWindow.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package demo

import indigo.*
import roguelikestarterkit.*

object MenuWindow:

private val graphic = Graphic(0, 0, TerminalMaterial(AssetName(""), RGBA.White, RGBA.Black))

val windowId: WindowId = WindowId("MenuWindow")

def window(
charSheet: CharSheet
): WindowModel[ComponentList[Int], Int] =
WindowModel(
windowId,
charSheet,
ComponentList(Dimensions(20, 3)) { (_: Int) =>
Batch(
Button[Int](
"Window 1",
Button.Theme(
charSheet,
RGBA.Silver -> RGBA.Black,
RGBA.White -> RGBA.Black,
RGBA.Black -> RGBA.White,
hasBorder = false
)
).onClick(Log("Window 1"), WindowEvent.Open(ComponentsWindow.windowId)),
Button[Int](
"Window 2",
Button.Theme(
charSheet,
RGBA.Silver -> RGBA.Black,
RGBA.Green -> RGBA.Black,
RGBA.Black -> RGBA.Yellow,
hasBorder = false
)
).onClick(Log("Window 2"), WindowEvent.Open(ComponentsWindow2.windowId))
)
}
.withLayout(ComponentLayout.Horizontal(Padding(0, 1, 0, 0)))
)
.moveTo(0, 0)
.resizeTo(20, 3)
.isStatic
6 changes: 6 additions & 0 deletions demo/src/main/scala/demo/UIScene.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ object UIScene extends Scene[Size, Model, ViewModel]:
Model.defaultCharSheet
)
)
.register(
MenuWindow.window(
Model.defaultCharSheet
)
)
.open(
MenuWindow.windowId,
ComponentsWindow.windowId,
ComponentsWindow2.windowId
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,15 @@ object WindowManager:
model: WindowManagerModel[ReferenceData]
): GlobalEvent => Outcome[WindowManagerModel[ReferenceData]] =
e =>
val windowUnderMouse = model.windowAt(context.mouseCoords)

model.windows
.map { w =>
Window.updateModel(
context.copy(state = if w.hasFocus then UIState.Active else UIState.InActive),
context.copy(state =
if w.hasFocus || windowUnderMouse.exists(_ == w.id) then UIState.Active
else UIState.InActive
),
w
)(e)
}
Expand Down Expand Up @@ -213,6 +218,8 @@ object WindowManager:
Outcome(viewModel.changeMagnification(next))

case e =>
val windowUnderMouse = model.windowAt(context.mouseCoords)

val updated =
val prunedVM = viewModel.prune(model)
model.windows.flatMap { m =>
Expand All @@ -225,7 +232,10 @@ object WindowManager:
case Some(vm) =>
Batch(
vm.update(
context.copy(state = if m.hasFocus then UIState.Active else UIState.InActive),
context.copy(state =
if m.hasFocus || windowUnderMouse.exists(_ == m.id) then UIState.Active
else UIState.InActive
),
m,
e
)
Expand All @@ -240,6 +250,8 @@ object WindowManager:
model: WindowManagerModel[ReferenceData],
viewModel: WindowManagerViewModel[ReferenceData]
): Outcome[SceneUpdateFragment] =
val windowUnderMouse = model.windowAt(context.mouseCoords)

val windowLayers: Outcome[Batch[Layer]] =
model.windows
.filter(_.isOpen)
Expand All @@ -253,7 +265,10 @@ object WindowManager:
Batch(
Window
.present(
context.copy(state = if m.hasFocus then UIState.Active else UIState.InActive),
context.copy(state =
if m.hasFocus || windowUnderMouse.exists(_ == m.id) then UIState.Active
else UIState.InActive
),
m,
vm
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ final case class WindowManagerModel[ReferenceData](windows: Batch[WindowModel[?,

this.copy(windows = reordered)

def giveWindowAt(coords: Coords): Option[WindowId] =
windows.reverse.find(w => !w.static && w.bounds.contains(coords)).map(_.id)
def windowAt(coords: Coords): Option[WindowId] =
windows.reverse.find(_.bounds.contains(coords)).map(_.id)

def moveTo(id: WindowId, position: Coords): WindowManagerModel[ReferenceData] =
this.copy(windows = windows.map(w => if w.id == id then w.moveTo(position) else w))
Expand Down

0 comments on commit 615a167

Please sign in to comment.