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.

88 lines
2.5 KiB

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