position.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import {Orientation} from './tile.js';
  2. export class Position {
  3. proportion;
  4. x;
  5. y;
  6. width;
  7. height;
  8. index;
  9. splitProportion;
  10. constructor(proportion = 1.0, x = 0, y = 0, width = 0, height = 0, index = 0) {
  11. this.proportion = proportion;
  12. this.x = x;
  13. this.y = y;
  14. this.width = width;
  15. this.height = height;
  16. this.index = index;
  17. this.splitProportion = 0.5;
  18. }
  19. split(orientation = null) {
  20. let vertical;
  21. let newPosition1 = new Position();
  22. newPosition1.proportion = this.proportion * this.splitProportion;
  23. newPosition1.x = this.x;
  24. newPosition1.y = this.y;
  25. newPosition1.index = 0;
  26. switch (orientation) {
  27. case null:
  28. case Orientation.None:
  29. if (this.width > this.height) {
  30. vertical = true;
  31. newPosition1.width = this.width * this.splitProportion;
  32. newPosition1.height = this.height;
  33. } else {
  34. vertical = false;
  35. newPosition1.width = this.width;
  36. newPosition1.height = this.height * this.splitProportion;
  37. }
  38. break;
  39. case Orientation.Horizontal:
  40. vertical = true;
  41. newPosition1.width = this.width * this.splitProportion;
  42. newPosition1.height = this.height;
  43. break;
  44. case Orientation.Vertical:
  45. vertical = false;
  46. newPosition1.width = this.width;
  47. newPosition1.height = this.height * this.splitProportion;
  48. break;
  49. }
  50. let newPosition2 = new Position(this.proportion * (1 - this.splitProportion), vertical ? this.x + this.width * this.splitProportion : this.x, vertical ? this.y : this.y + this.height * this.splitProportion, vertical ? this.width * (1 - this.splitProportion) : this.width, vertical ? this.height : this.height * (1 - this.splitProportion), 1);
  51. return [newPosition1, newPosition2];
  52. }
  53. static fromObject(obj) {
  54. let pos = new Position(obj.proportion, obj.x, obj.y, obj.width, obj.height, obj.index);
  55. pos.splitProportion = obj.splitProportion;
  56. return pos;
  57. }
  58. }