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.

53 lines
1.3 KiB

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 wow.doge.mygame.implicits._
  7. import cats.effect.Resource
  8. import monix.bio.Task
  9. trait JFXConsoleStreamable[T] {
  10. def println(inst: T, text: String): Unit
  11. def print(inst: T, text: String): Unit
  12. }
  13. class JFXConsoleStream[T](
  14. outputStream: OutputStream,
  15. val config: JFXConsoleStream.Config = JFXConsoleStream.Config.default,
  16. control: T
  17. )(implicit
  18. jcs: JFXConsoleStreamable[T]
  19. ) extends PrintStream(outputStream, true) {
  20. private lazy val defaultOut = System.out
  21. override def println(text: String): Unit =
  22. if (config.exclusive) {
  23. jcs.println(control, text + "\n")
  24. } else {
  25. defaultOut.println(text)
  26. jcs.println(control, text + "\n")
  27. }
  28. override def print(text: String): Unit = jcs.println(control, text)
  29. }
  30. object JFXConsoleStream {
  31. /**
  32. * for future use
  33. */
  34. case class Config(exclusive: Boolean = false)
  35. object Config {
  36. lazy val default = Config()
  37. }
  38. def textAreaStream(ta: TextArea) =
  39. Resource.make(
  40. Task(
  41. new JFXConsoleStream(
  42. outputStream = new ByteArrayOutputStream(),
  43. control = ta
  44. )
  45. )
  46. )(s => Task(s.close()))
  47. }