Skip to content

Commit

Permalink
Keyboard events now use the code property
Browse files Browse the repository at this point in the history
**This is a breaking change**

Fixes #766 by adding a new `KeyCode` enum, which is populated with the
values from the `code` property of the KeyboardEvent. Additionally, the
`Key` class also has a new `location` property, which maps to the
`location` property of the KeyboardEvent.

The KeyboardEvent itself now also supports some new additional properties:
 * `isRepeat` - a boolean indicating if the key is being held down
 * `isCtrlKey` - a boolean indicating if the Ctrl key is pressed
 * `isMetaKey` - a boolean indicating if the Meta key is pressed
 * `isAltKey` - a boolean indicating if the Alt key is pressed
 * `isShiftKey` - a boolean indicating if the Shift key is pressed
  • Loading branch information
hobnob committed Oct 25, 2024
1 parent 8534a6a commit aaafc36
Show file tree
Hide file tree
Showing 7 changed files with 544 additions and 238 deletions.
123 changes: 77 additions & 46 deletions indigo/indigo/src/main/scala/indigo/platform/events/WorldEvents.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package indigo.platform.events
import indigo.shared.collections.Batch
import indigo.shared.config.ResizePolicy
import indigo.shared.constants.Key
import indigo.shared.constants.KeyCode
import indigo.shared.constants.KeyLocation
import indigo.shared.datatypes.Point
import indigo.shared.datatypes.Radians
import indigo.shared.datatypes.Size
Expand Down Expand Up @@ -164,10 +166,36 @@ final class WorldEvents:
globalEventStream.pushGlobalEvent(wheel)
},
onKeyDown = { e =>
globalEventStream.pushGlobalEvent(KeyboardEvent.KeyDown(Key(e.keyCode, e.key)))
globalEventStream.pushGlobalEvent(
KeyboardEvent.KeyDown(
Key(
KeyCode.fromString(e.code),
e.key,
KeyLocation.fromInt(e.location)
),
e.repeat,
e.altKey,
e.ctrlKey,
e.metaKey,
e.shiftKey
)
)
},
onKeyUp = { e =>
globalEventStream.pushGlobalEvent(KeyboardEvent.KeyUp(Key(e.keyCode, e.key)))
globalEventStream.pushGlobalEvent(
KeyboardEvent.KeyUp(
Key(
KeyCode.fromString(e.code),
e.key,
KeyLocation.fromInt(e.location)
),
e.repeat,
e.altKey,
e.ctrlKey,
e.metaKey,
e.shiftKey
)
)
},
// Prevent right mouse button from popping up the context menu
onContextMenu = if disableContextMenu then Some((e: dom.MouseEvent) => e.preventDefault()) else None,
Expand Down Expand Up @@ -442,51 +470,54 @@ final class WorldEvents:
resizeObserver = new dom.ResizeObserver((entries, _) =>
entries.foreach { entry =>
entry.target.childNodes.foreach { child =>
if child.attributes.getNamedItem("id").value == canvas.attributes.getNamedItem("id").value then
val containerSize = new Size(
Math.floor(entry.contentRect.width).toInt,
Math.floor(entry.contentRect.height).toInt
)
val canvasSize = new Size(canvas.width, canvas.height)
if resizePolicy != ResizePolicy.NoResize then
val newSize = resizePolicy match {
case ResizePolicy.Resize => containerSize
case ResizePolicy.ResizePreserveAspect =>
val width = canvas.width.toDouble
val height = canvas.height.toDouble
val aspectRatio = Math.min(width, height) / Math.max(width, height)

if width > height then
val newHeight = containerSize.width.toDouble * aspectRatio
if newHeight > containerSize.height then
Size(
(containerSize.height / aspectRatio).toInt,
containerSize.height
)
else
Size(
containerSize.width,
newHeight.toInt
)
else
val newWidth = containerSize.height.toDouble * aspectRatio
if newWidth > containerSize.width then
Size(
containerSize.width,
(containerSize.width / aspectRatio).toInt
)
child match {
case child: dom.Element
if child.attributes.getNamedItem("id").value == canvas.attributes.getNamedItem("id").value =>
val containerSize = new Size(
Math.floor(entry.contentRect.width).toInt,
Math.floor(entry.contentRect.height).toInt
)
val canvasSize = new Size(canvas.width, canvas.height)
if resizePolicy != ResizePolicy.NoResize then
val newSize = resizePolicy match {
case ResizePolicy.Resize => containerSize
case ResizePolicy.ResizePreserveAspect =>
val width = canvas.width.toDouble
val height = canvas.height.toDouble
val aspectRatio = Math.min(width, height) / Math.max(width, height)

if width > height then
val newHeight = containerSize.width.toDouble * aspectRatio
if newHeight > containerSize.height then
Size(
(containerSize.height / aspectRatio).toInt,
containerSize.height
)
else
Size(
containerSize.width,
newHeight.toInt
)
else
Size(
newWidth.toInt,
containerSize.height
)
case _ => canvasSize
}

if (newSize != canvasSize) {
canvas.width = Math.min(newSize.width, containerSize.width)
canvas.height = Math.min(newSize.height, containerSize.height)
}
val newWidth = containerSize.height.toDouble * aspectRatio
if newWidth > containerSize.width then
Size(
containerSize.width,
(containerSize.width / aspectRatio).toInt
)
else
Size(
newWidth.toInt,
containerSize.height
)
case _ => canvasSize
}

if (newSize != canvasSize) {
canvas.width = Math.min(newSize.width, containerSize.width)
canvas.height = Math.min(newSize.height, containerSize.height)
}
}
}
}
)
Expand Down
Loading

0 comments on commit aaafc36

Please sign in to comment.