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.

110 lines
3.0 KiB

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