Testing out JmonkeyEngine to make a game in Scala with Akka Actors within a pure FP layer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.4 KiB

  1. package wow.doge.mygame.implicits
  2. import javafx.scene.{input => jfxsi}
  3. import javafx.{event => jfxe}
  4. import monix.execution.Ack
  5. import monix.execution.Cancelable
  6. import monix.reactive.Observable
  7. import monix.reactive.OverflowStrategy
  8. import scalafx.scene.Scene
  9. import scalafx.scene.control.ButtonBase
  10. object JavaFXMonixObservables {
  11. implicit final class SceneObservables(private val scene: Scene)
  12. extends AnyVal {
  13. def observableMousePressed(): Observable[jfxsi.MouseEvent] = {
  14. import monix.execution.cancelables.SingleAssignCancelable
  15. Observable.create(OverflowStrategy.Unbounded) { sub =>
  16. val c = SingleAssignCancelable()
  17. val l = new jfxe.EventHandler[jfxsi.MouseEvent] {
  18. override def handle(event: jfxsi.MouseEvent): Unit = {
  19. if (sub.onNext(event) == Ack.Stop) c.cancel()
  20. }
  21. }
  22. scene.onMousePressed = l
  23. c := Cancelable(() =>
  24. scene.removeEventHandler(
  25. jfxsi.MouseEvent.MOUSE_PRESSED,
  26. l
  27. )
  28. )
  29. c
  30. }
  31. }
  32. def observableMouseDragged(): Observable[jfxsi.MouseEvent] = {
  33. import monix.execution.cancelables.SingleAssignCancelable
  34. Observable.create(OverflowStrategy.Unbounded) { sub =>
  35. val c = SingleAssignCancelable()
  36. val l = new jfxe.EventHandler[jfxsi.MouseEvent] {
  37. override def handle(event: jfxsi.MouseEvent): Unit = {
  38. if (sub.onNext(event) == Ack.Stop) c.cancel()
  39. }
  40. }
  41. scene.onMouseDragged = l
  42. c := Cancelable(() =>
  43. scene.removeEventHandler(
  44. jfxsi.MouseEvent.MOUSE_DRAGGED,
  45. l
  46. )
  47. )
  48. c
  49. }
  50. }
  51. }
  52. implicit final class OnActionObservable(
  53. private val button: ButtonBase
  54. ) extends AnyVal {
  55. def observableAction(): Observable[jfxe.ActionEvent] = {
  56. import monix.execution.cancelables.SingleAssignCancelable
  57. Observable.create(OverflowStrategy.Unbounded) { sub =>
  58. val c = SingleAssignCancelable()
  59. val l = new jfxe.EventHandler[jfxe.ActionEvent] {
  60. override def handle(event: jfxe.ActionEvent): Unit = {
  61. if (sub.onNext(event) == Ack.Stop) c.cancel()
  62. }
  63. }
  64. button.onAction = l
  65. c := Cancelable(() =>
  66. button.removeEventHandler(
  67. jfxe.ActionEvent.ACTION,
  68. l
  69. )
  70. )
  71. c
  72. }
  73. }
  74. }
  75. }