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.

104 lines
2.4 KiB

  1. package wow.doge.mygame.utils
  2. import java.io.ByteArrayOutputStream
  3. import java.io.OutputStream
  4. import java.io.PrintStream
  5. import cats.effect.Resource
  6. import monix.bio.Task
  7. import scalafx.scene.control.TextArea
  8. import scalafx.application.Platform
  9. trait ConsoleStreamable[T] {
  10. def println(inst: T, text: String): Unit
  11. def print(inst: T, text: String): Unit
  12. }
  13. class GenericConsoleStream[T](
  14. outputStream: OutputStream,
  15. val config: GenericConsoleStream.Config =
  16. GenericConsoleStream.Config.default,
  17. private var streamable: Option[T] = None
  18. )(implicit
  19. cs: ConsoleStreamable[T]
  20. ) extends PrintStream(outputStream, true) {
  21. private lazy val defaultOut = System.out
  22. def printToStreamable(stble: Option[T], text: String) =
  23. stble.foreach(s => cs.println(s, text))
  24. override def println(text: String): Unit =
  25. if (config.exclusive) {
  26. printToStreamable(streamable, text)
  27. } else {
  28. defaultOut.println(text)
  29. printToStreamable(streamable, text)
  30. }
  31. override def print(text: String): Unit =
  32. streamable.foreach(s =>
  33. if (config.exclusive) {
  34. printToStreamable(streamable, text)
  35. } else {
  36. defaultOut.println(text)
  37. printToStreamable(streamable, text)
  38. }
  39. )
  40. def :=(s: T) = {
  41. streamable match {
  42. case Some(value) =>
  43. case None => streamable = Some(s)
  44. }
  45. }
  46. }
  47. object GenericConsoleStream {
  48. /**
  49. * for future use
  50. */
  51. case class Config(exclusive: Boolean = false)
  52. object Config {
  53. lazy val default = Config()
  54. }
  55. implicit val implJFXConsoleStreamForTextArea =
  56. new ConsoleStreamable[scalafx.scene.control.TextArea] {
  57. override def println(
  58. ta: scalafx.scene.control.TextArea,
  59. text: String
  60. ): Unit =
  61. ta.appendText(text + "\n")
  62. override def print(
  63. ta: scalafx.scene.control.TextArea,
  64. text: String
  65. ): Unit =
  66. ta.appendText(text)
  67. }
  68. // def textAreaStreamResource(ta: TextArea) =
  69. // Resource.make(
  70. // Task(
  71. // new GenericConsoleStream(
  72. // outputStream = new ByteArrayOutputStream(),
  73. // streamable = ta
  74. // )
  75. // )
  76. // )(s => Task(s.close()))
  77. def textAreaStream(
  78. // ta: TextArea
  79. ) =
  80. // Task(
  81. new GenericConsoleStream[TextArea](
  82. outputStream = new ByteArrayOutputStream()
  83. // streamable = ta
  84. )
  85. // )
  86. // (s => Task(s.close()))
  87. }