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.

70 lines
1.6 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package wow.doge.mygame.utils
  2. import java.io.PrintStream
  3. import java.io.OutputStream
  4. import java.io.ByteArrayOutputStream
  5. import scalafx.scene.control.TextArea
  6. import cats.effect.Resource
  7. import monix.bio.Task
  8. trait JFXConsoleStreamable[T] {
  9. def println(inst: T, text: String): Unit
  10. def print(inst: T, text: String): Unit
  11. }
  12. class JFXConsoleStream[T](
  13. outputStream: OutputStream,
  14. val config: JFXConsoleStream.Config = JFXConsoleStream.Config.default,
  15. control: T
  16. )(implicit
  17. jcs: JFXConsoleStreamable[T]
  18. ) extends PrintStream(outputStream, true) {
  19. private lazy val defaultOut = System.out
  20. override def println(text: String): Unit =
  21. if (config.exclusive) {
  22. jcs.println(control, text + "\n")
  23. } else {
  24. defaultOut.println(text)
  25. jcs.println(control, text + "\n")
  26. }
  27. override def print(text: String): Unit = jcs.println(control, text)
  28. }
  29. object JFXConsoleStream {
  30. /**
  31. * for future use
  32. */
  33. case class Config(exclusive: Boolean = false)
  34. object Config {
  35. lazy val default = Config()
  36. }
  37. implicit val implJFXConsoleStreamForTextArea =
  38. new JFXConsoleStreamable[scalafx.scene.control.TextArea] {
  39. override def println(
  40. ta: scalafx.scene.control.TextArea,
  41. text: String
  42. ): Unit =
  43. ta.appendText(text + "\n")
  44. override def print(
  45. ta: scalafx.scene.control.TextArea,
  46. text: String
  47. ): Unit =
  48. ta.appendText(text)
  49. }
  50. def textAreaStream(ta: TextArea) =
  51. Resource.make(
  52. Task(
  53. new JFXConsoleStream(
  54. outputStream = new ByteArrayOutputStream(),
  55. control = ta
  56. )
  57. )
  58. )(s => Task(s.close()))
  59. }