Console.hx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package arm;
  2. @:keep
  3. class Console {
  4. public static var message = "";
  5. public static var messageTimer = 0.0;
  6. public static var messageColor = 0x00000000;
  7. public static var lastTraces: Array<String> = [""];
  8. static var haxeTrace: Dynamic->haxe.PosInfos->Void = null;
  9. static var progressText: String = null;
  10. static function drawToast(s: String, g: kha.graphics2.Graphics) {
  11. g.color = 0x55000000;
  12. g.fillRect(0, 0, kha.System.windowWidth(), kha.System.windowHeight());
  13. var scale = arm.App.getUIs()[0].SCALE();
  14. var x = kha.System.windowWidth() / 2;
  15. var y = kha.System.windowHeight() - 200 * scale;
  16. g.fillRect(x - 200 * scale, y, 400 * scale, 80 * scale);
  17. g.font = App.font;
  18. g.fontSize = Std.int(22 * scale);
  19. g.color = 0xffffffff;
  20. g.drawString(s, x - g.font.width(g.fontSize, s) / 2, y + 40 * scale - g.font.height(g.fontSize) / 2);
  21. }
  22. public static function toast(s: String, g2: kha.graphics2.Graphics = null) {
  23. // Show a popup message
  24. function _render(g: kha.graphics2.Graphics) {
  25. drawToast(s, g);
  26. if (g2 == null) {
  27. arm.App.notifyOnNextFrame(function() {
  28. iron.App.removeRender2D(_render);
  29. });
  30. }
  31. }
  32. g2 != null ? _render(g2) : iron.App.notifyOnRender2D(_render);
  33. consoleTrace(s);
  34. }
  35. static function drawProgress(g: kha.graphics2.Graphics) {
  36. drawToast(progressText, g);
  37. }
  38. public static function progress(s: String) {
  39. // Keep popup message displayed until s == null
  40. if (s == null) {
  41. iron.App.removeRender2D(drawProgress);
  42. }
  43. else if (progressText == null) {
  44. iron.App.notifyOnRender2D(drawProgress);
  45. }
  46. if (s != null) consoleTrace(s);
  47. progressText = s;
  48. }
  49. public static function info(s: String) {
  50. messageTimer = 5.0;
  51. message = s;
  52. messageColor = 0x00000000;
  53. App.redrawStatus();
  54. consoleTrace(s);
  55. }
  56. public static function error(s: String) {
  57. messageTimer = 8.0;
  58. message = s;
  59. messageColor = 0xffaa0000;
  60. App.redrawStatus();
  61. consoleTrace(s);
  62. }
  63. public static function log(s: String) {
  64. consoleTrace(s);
  65. }
  66. public static function init() {
  67. if (haxeTrace == null) {
  68. haxeTrace = haxe.Log.trace;
  69. haxe.Log.trace = consoleTrace;
  70. }
  71. }
  72. static function consoleTrace(v: Dynamic, ?inf: haxe.PosInfos) {
  73. App.redrawConsole();
  74. lastTraces.unshift(Std.string(v));
  75. if (lastTraces.length > 100) lastTraces.pop();
  76. haxeTrace(v, inf);
  77. }
  78. }