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.

85 lines
2.0 KiB

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