Console.hx 1.8 KB

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