app.d 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import std.stdio;
  2. import std.algorithm;
  3. import std.process;
  4. import core.thread.osthread;
  5. import core.time;
  6. import std.random;
  7. import color;
  8. import vector;
  9. import cell;
  10. import renderable.renderable;
  11. import renderable.rect;
  12. import renderable.label;
  13. import renderable.chart;
  14. import renderable.stacklayout;
  15. import horizontaltextalignment;
  16. import verticaltextalignment;
  17. import stacklayouttype;
  18. import renderable.cellcachecontainer;
  19. import std.random;
  20. import charttype;
  21. import renderable.table;
  22. import renderable.tree;
  23. class Canvas {
  24. Vector dimension;
  25. Color backgroundColor;
  26. private bool wrapContent = false;
  27. private CellCacheContainer container;
  28. this(Color backgroundColor = Color.terminal()) {
  29. this.wrapContent = true;
  30. this(Vector(1, 1), backgroundColor);
  31. }
  32. this(Vector dimension, Color backgroundColor = Color.terminal()) {
  33. this.dimension = dimension;
  34. this.backgroundColor = backgroundColor;
  35. container = new CellCacheContainer();
  36. }
  37. void updateCache(Renderable renderable, Vector position) {
  38. if (wrapContent) {
  39. this.dimension. x += renderable.dimension. x ;
  40. this.dimension. y += renderable.dimension. y ;
  41. }
  42. container.updateCache(renderable, position);
  43. }
  44. void drawCache() {
  45. Cell[] cellsDrawn;
  46. for (int y = 0; y < dimension. y ; ++y) {
  47. for (int x = 0; x < dimension. x ; ++x) {
  48. bool cellFound = false;
  49. foreach (cell; container.cache) {
  50. if (cell.coordinates.x == x && cell.coordinates.y == y && !(cellsDrawn.canFind!(cell => cell.coordinates.x == x && cell.coordinates.y == y))) {
  51. if (cell.backgroundColor != Color.terminal()) {
  52. writef("\x1b[48;2;%s;%s;%sm", cell.backgroundColor.r, cell.backgroundColor.g, cell.backgroundColor.b);
  53. }
  54. if (cell.contentColor != Color.terminal()) {
  55. writef("\x1b[38;2;%s;%s;%sm", cell.contentColor.r, cell.contentColor.g, cell.contentColor.b);
  56. }
  57. write(cell.content);
  58. cellsDrawn ~= cell;
  59. cellFound = true;
  60. }
  61. }
  62. if (!cellFound) {
  63. if (backgroundColor == Color.terminal()) {
  64. writef("\x1b[m");
  65. } else {
  66. writef("\x1b[48;2;%s;%s;%sm", backgroundColor.r, backgroundColor.g, backgroundColor.b);
  67. }
  68. write(' ');
  69. writef("\x1b[m");
  70. }
  71. }
  72. writeln();
  73. }
  74. }
  75. }
  76. void main() {
  77. Canvas canvas = new Canvas(Vector(50, 50));
  78. StackLayout column = new StackLayout(StackLayoutType.column, 1, Color.Blue);
  79. column.add(Rect.withFill(Vector(3, 3), Color.Red), 3);
  80. canvas.updateCache(column, Vector(-9, -9));
  81. canvas.drawCache();
  82. writeln(column.dimension);
  83. }