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.

84 lines
1.9 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  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 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. object GenericConsoleStream {
  44. /**
  45. * for future use
  46. */
  47. case class Config(exclusive: Boolean = false)
  48. object Config {
  49. val default = Config()
  50. }
  51. implicit val implJFXConsoleStreamForTextArea =
  52. new ConsoleStreamable[TextArea] {
  53. override def println(
  54. ta: TextArea,
  55. text: String
  56. ): Unit =
  57. Platform.runLater(() => ta.appendText(text + "\n"))
  58. override def print(
  59. ta: TextArea,
  60. text: String
  61. ): Unit =
  62. Platform.runLater(() => ta.appendText(text))
  63. }
  64. def textAreaStream() =
  65. new GenericConsoleStream[TextArea](
  66. outputStream = new ByteArrayOutputStream()
  67. )
  68. }