rect.d 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. module renderable.rect;
  2. import color;
  3. import vector;
  4. import cell;
  5. import renderable.renderable;
  6. import std.stdio;
  7. class Rect : Renderable {
  8. private enum RectType {
  9. fill, frame, empty
  10. }
  11. RectType rectType;
  12. Color fillColor;
  13. Color frameColor;
  14. static Rect withFill(Vector dimension, Color fillColor) {
  15. Rect rect = new Rect();
  16. rect.dimension = dimension;
  17. rect.rectType = RectType.fill;
  18. rect.fillColor = fillColor;
  19. return rect;
  20. }
  21. static Rect withFrame(Vector dimension, Color frameColor) {
  22. Rect rect = new Rect();
  23. rect.dimension = dimension;
  24. rect.rectType = RectType.frame;
  25. rect.frameColor = frameColor;
  26. return rect;
  27. }
  28. static Rect empty(Vector dimension) {
  29. Rect rect = new Rect();
  30. rect.dimension = dimension;
  31. rect.rectType = RectType.empty;
  32. rect.fillColor = Color.terminal();
  33. return rect;
  34. }
  35. override Cell[] render() {
  36. Cell[] cells;
  37. Vector from = Vector(0, 0);
  38. Vector to = Vector(dimension.x - 1, dimension.y - 1);
  39. if (rectType != RectType.empty && rectType != RectType.fill) {
  40. for (int x = from.x; x <= to.x; ++x) {
  41. if (rectType == RectType.frame) {
  42. wchar content1;
  43. wchar content2;
  44. if (x == from.x) {
  45. content1 = '└';
  46. } else if (x == to.x) {
  47. content1 = '┘';
  48. } else {
  49. content1 = '─';
  50. }
  51. if (x == from.x) {
  52. content2 = '┌';
  53. } else if (x == to.x) {
  54. content2 = '┐';
  55. } else {
  56. content2 = '─';
  57. }
  58. cells ~= Cell(Vector(x, to.y), content1, frameColor);
  59. cells ~= Cell(Vector(x, from.y), content2, frameColor);
  60. }
  61. }
  62. for (int y = from.y + 1; y < to.y; ++y) {
  63. if (rectType == RectType.frame) {
  64. wchar content = '│';
  65. cells ~= Cell(Vector(from.x, y), content, frameColor);
  66. cells ~= Cell(Vector(to.x, y), content, frameColor);
  67. } else {
  68. }
  69. }
  70. } else {
  71. for (int x = 0; x < dimension. x ; ++x) {
  72. for (int y = 0; y < dimension. y ; ++y) {
  73. cells ~= Cell(Vector(x, y), ' ', Color.terminal(), fillColor);
  74. }
  75. }
  76. }
  77. return cells;
  78. }
  79. }