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.

108 lines
3.1 KiB

4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
4 years ago
  1. package wow.doge.mygame.executors
  2. import java.util.Collections
  3. import java.util.concurrent.AbstractExecutorService
  4. import java.util.concurrent.Executor
  5. import java.util.concurrent.ExecutorService
  6. import java.util.concurrent.ThreadFactory
  7. import java.util.concurrent.TimeUnit
  8. import javax.swing.SwingUtilities
  9. import scala.concurrent.ExecutionContext
  10. import akka.dispatch.DispatcherPrerequisites
  11. import akka.dispatch.ExecutorServiceConfigurator
  12. import akka.dispatch.ExecutorServiceFactory
  13. import com.typesafe.config.Config
  14. import javafx.application.Platform
  15. import monix.execution.Scheduler
  16. // First we wrap invokeLater/runLater as an ExecutorService
  17. trait GUIExecutorService extends AbstractExecutorService {
  18. def execute(command: Runnable): Unit
  19. def shutdown(): Unit = ()
  20. def shutdownNow() = Collections.emptyList[Runnable]
  21. def isShutdown = false
  22. def isTerminated = false
  23. def awaitTermination(l: Long, timeUnit: TimeUnit) = true
  24. }
  25. object JavaFXExecutorService extends GUIExecutorService {
  26. override def execute(command: Runnable) = Platform.runLater(command)
  27. }
  28. object SwingExecutorService extends GUIExecutorService {
  29. override def execute(command: Runnable) = SwingUtilities.invokeLater(command)
  30. }
  31. object JMEExecutorService extends GUIExecutorService {
  32. override def execute(command: Runnable) =
  33. JMERunner.runner.get.apply(command)
  34. // new SingleThreadEventExecutor()
  35. sys.addShutdownHook(JMEExecutorService.shutdown())
  36. }
  37. object JMERunner {
  38. var runner: Option[Runnable => Unit] = None
  39. }
  40. class JavaFXEventThreadExecutorServiceConfigurator(
  41. config: Config,
  42. prerequisites: DispatcherPrerequisites
  43. ) extends ExecutorServiceConfigurator(config, prerequisites) {
  44. private val f = new ExecutorServiceFactory {
  45. def createExecutorService: ExecutorService = JavaFXExecutorService
  46. }
  47. def createExecutorServiceFactory(
  48. id: String,
  49. threadFactory: ThreadFactory
  50. ): ExecutorServiceFactory = f
  51. }
  52. class JMEThreadExecutorServiceConfigurator(
  53. config: Config,
  54. prerequisites: DispatcherPrerequisites
  55. ) extends ExecutorServiceConfigurator(config, prerequisites) {
  56. private val f = new ExecutorServiceFactory {
  57. def createExecutorService: ExecutorService = JMEExecutorService
  58. }
  59. def createExecutorServiceFactory(
  60. id: String,
  61. threadFactory: ThreadFactory
  62. ): ExecutorServiceFactory = f
  63. }
  64. // Then we create an ExecutorServiceConfigurator so that Akka can use our SwingExecutorService for the dispatchers
  65. class SwingEventThreadExecutorServiceConfigurator(
  66. config: Config,
  67. prerequisites: DispatcherPrerequisites
  68. ) extends ExecutorServiceConfigurator(config, prerequisites) {
  69. private val f = new ExecutorServiceFactory {
  70. def createExecutorService: ExecutorService = SwingExecutorService
  71. }
  72. def createExecutorServiceFactory(
  73. id: String,
  74. threadFactory: ThreadFactory
  75. ): ExecutorServiceFactory = f
  76. }
  77. object JFXExecutionContexts {
  78. val javaFxExecutionContext: ExecutionContext =
  79. ExecutionContext.fromExecutor(new Executor {
  80. def execute(command: Runnable): Unit = {
  81. Platform.runLater(command)
  82. }
  83. })
  84. val fxScheduler =
  85. Scheduler(javaFxExecutionContext)
  86. }