Commit efb0693a authored by h2b's avatar h2b

AWT event driver added.

parent e52692c1
SimGraf - A Simple Scala Graphics Library
Copyright 2016-2017 Hans-Hermann Bode
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
See the License for the specific language governing permissions and
limitations under the License.
package de.h2b.scala.lib.simgraf.driver
import java.awt.event.{ KeyEvent AwtKeyEvent, KeyListener, MouseEvent AwtMouseEvent, MouseListener }
import de.h2b.scala.lib.simgraf.event._
import de.h2b.scala.lib.simgraf.Pixel
import javax.swing.SwingUtilities
* @since 1.2.0
* @author h2b
trait AwtEventDriver extends KeyListener with MouseListener {
import AwtEventDriver._
def keyPressed (e: AwtKeyEvent): Unit = {
//unused in this implementation
def keyReleased (e: AwtKeyEvent): Unit = {
//unused in this implementation
def keyTyped (e: AwtKeyEvent): Unit = {
publisher ! keyEvent(e)
def mouseClicked (e: AwtMouseEvent): Unit = {
publisher ! mouseEvent(e)
def mouseEntered (e: AwtMouseEvent): Unit = {
//unused in this implementation
def mouseExited (e: AwtMouseEvent): Unit = {
//unused in this implementation
def mousePressed (e: AwtMouseEvent): Unit = {
//unused in this implementation
def mouseReleased (e: AwtMouseEvent): Unit = {
//unused in this implementation
object AwtEventDriver {
private def keyEvent (e: AwtKeyEvent) =
private def mouseEvent (e: AwtMouseEvent) =
MouseEvent(button(e), modifiers(e), e.getClickCount, position(e))
private def button (e: AwtMouseEvent): Button =
if (SwingUtilities.isLeftMouseButton(e)) LeftButton
else if (SwingUtilities.isRightMouseButton(e)) RightButton
else if (SwingUtilities.isMiddleMouseButton(e)) MiddleButton
else e.getButton match {
case AwtMouseEvent.NOBUTTON NoButton
case other OtherButton(other)
private def modifiers (e: AwtMouseEvent) = {
var result = Set.empty[Modifier]
if (e.isShiftDown()) result += ShiftModifier
if (e.isControlDown()) result += CtrlModifier
if (e.isAltDown()) result += AltModifier
if (e.isAltGraphDown()) result += AltGraphModifier
if (e.isMetaDown()) result += MetaModifier
//TODO: flip y
private def position (e: AwtMouseEvent) = Pixel(e.getX, e.getY)
......@@ -25,7 +25,7 @@ import de.h2b.scala.lib.simgraf.Pixel
sealed trait Event
case class KeyEvent (char: Char, modifiers: Set[Modifier]) extends Event
case class KeyEvent (char: Char) extends Event
case class MouseEvent (button: Button, modifiers: Set[Modifier], clickCount: Int,
position: Pixel) extends Event
......@@ -28,5 +28,3 @@ case object CtrlModifier extends Modifier
case object AltModifier extends Modifier
case object AltGraphModifier extends Modifier
case object MetaModifier extends Modifier
case class OtherModifier (code: Int) extends Modifier
......@@ -30,8 +30,8 @@ class EventActor extends Actor {
private val mouse = context.actorOf(Props[MouseEventActor], "")
def receive = {
case KeyEvent(char, modifiers)
key ! KeyEvent(char, modifiers)
case KeyEvent(char)
key ! KeyEvent(char)
case MouseEvent(button, modifiers, clicks, position)
mouse ! MouseEvent(button, modifiers, clicks, position)
......@@ -27,7 +27,7 @@ import de.h2b.scala.lib.simgraf.event.KeyEvent
class KeyEventActor extends Actor {
def receive = {
case KeyEvent(char, modifiers) ()
case KeyEvent(char) ()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment