babylon.canvas2dLayoutEngine.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. var __extends = (this && this.__extends) || function (d, b) {
  2. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  3. function __() { this.constructor = d; }
  4. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  5. };
  6. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  7. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  8. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  9. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  10. return c > 3 && r && Object.defineProperty(target, key, r), r;
  11. };
  12. var BABYLON;
  13. (function (BABYLON) {
  14. var LayoutEngineBase = (function () {
  15. function LayoutEngineBase() {
  16. this.layoutDirtyOnPropertyChangedMask = 0;
  17. }
  18. LayoutEngineBase.prototype.updateLayout = function (prim) {
  19. };
  20. Object.defineProperty(LayoutEngineBase.prototype, "isChildPositionAllowed", {
  21. get: function () {
  22. return false;
  23. },
  24. enumerable: true,
  25. configurable: true
  26. });
  27. LayoutEngineBase.prototype.isLocked = function () {
  28. return this._isLocked;
  29. };
  30. LayoutEngineBase.prototype.lock = function () {
  31. if (this._isLocked) {
  32. return false;
  33. }
  34. this._isLocked = true;
  35. return true;
  36. };
  37. LayoutEngineBase = __decorate([
  38. BABYLON.className("LayoutEngineBase")
  39. ], LayoutEngineBase);
  40. return LayoutEngineBase;
  41. })();
  42. BABYLON.LayoutEngineBase = LayoutEngineBase;
  43. var CanvasLayoutEngine = (function (_super) {
  44. __extends(CanvasLayoutEngine, _super);
  45. function CanvasLayoutEngine() {
  46. _super.apply(this, arguments);
  47. }
  48. // A very simple (no) layout computing...
  49. // The Canvas and its direct children gets the Canvas' size as Layout Area
  50. // Indirect children have their Layout Area to the actualSize (margin area) of their parent
  51. CanvasLayoutEngine.prototype.updateLayout = function (prim) {
  52. // If this prim is layoutDiry we update its layoutArea and also the one of its direct children
  53. if (prim._isFlagSet(BABYLON.SmartPropertyPrim.flagLayoutDirty)) {
  54. for (var _i = 0, _a = prim.children; _i < _a.length; _i++) {
  55. var child = _a[_i];
  56. this._doUpdate(child);
  57. }
  58. prim._clearFlags(BABYLON.SmartPropertyPrim.flagLayoutDirty);
  59. }
  60. };
  61. CanvasLayoutEngine.prototype._doUpdate = function (prim) {
  62. // Canvas ?
  63. if (prim instanceof BABYLON.Canvas2D) {
  64. prim.layoutArea = prim.actualSize;
  65. }
  66. else if (prim.parent instanceof BABYLON.Canvas2D) {
  67. prim.layoutArea = prim.owner.actualSize;
  68. }
  69. else {
  70. prim.layoutArea = prim.parent.contentArea;
  71. }
  72. };
  73. Object.defineProperty(CanvasLayoutEngine.prototype, "isChildPositionAllowed", {
  74. get: function () {
  75. return true;
  76. },
  77. enumerable: true,
  78. configurable: true
  79. });
  80. CanvasLayoutEngine.Singleton = new CanvasLayoutEngine();
  81. CanvasLayoutEngine = __decorate([
  82. BABYLON.className("CanvasLayoutEngine")
  83. ], CanvasLayoutEngine);
  84. return CanvasLayoutEngine;
  85. })(LayoutEngineBase);
  86. BABYLON.CanvasLayoutEngine = CanvasLayoutEngine;
  87. var StackPanelLayoutEngine = (function (_super) {
  88. __extends(StackPanelLayoutEngine, _super);
  89. function StackPanelLayoutEngine() {
  90. _super.call(this);
  91. this._isHorizontal = true;
  92. this.layoutDirtyOnPropertyChangedMask = BABYLON.Prim2DBase.sizeProperty.flagId;
  93. }
  94. Object.defineProperty(StackPanelLayoutEngine, "Horizontal", {
  95. get: function () {
  96. if (!StackPanelLayoutEngine._horizontal) {
  97. StackPanelLayoutEngine._horizontal = new StackPanelLayoutEngine();
  98. StackPanelLayoutEngine._horizontal.isHorizontal = true;
  99. StackPanelLayoutEngine._horizontal.lock();
  100. }
  101. return StackPanelLayoutEngine._horizontal;
  102. },
  103. enumerable: true,
  104. configurable: true
  105. });
  106. Object.defineProperty(StackPanelLayoutEngine, "Vertical", {
  107. get: function () {
  108. if (!StackPanelLayoutEngine._vertical) {
  109. StackPanelLayoutEngine._vertical = new StackPanelLayoutEngine();
  110. StackPanelLayoutEngine._vertical.isHorizontal = false;
  111. StackPanelLayoutEngine._vertical.lock();
  112. }
  113. return StackPanelLayoutEngine._vertical;
  114. },
  115. enumerable: true,
  116. configurable: true
  117. });
  118. Object.defineProperty(StackPanelLayoutEngine.prototype, "isHorizontal", {
  119. get: function () {
  120. return this._isHorizontal;
  121. },
  122. set: function (val) {
  123. if (this.isLocked()) {
  124. return;
  125. }
  126. this._isHorizontal = val;
  127. },
  128. enumerable: true,
  129. configurable: true
  130. });
  131. StackPanelLayoutEngine.prototype.updateLayout = function (prim) {
  132. if (prim._isFlagSet(BABYLON.SmartPropertyPrim.flagLayoutDirty)) {
  133. var x = 0;
  134. var y = 0;
  135. var h = this.isHorizontal;
  136. var max = 0;
  137. for (var _i = 0, _a = prim.children; _i < _a.length; _i++) {
  138. var child = _a[_i];
  139. var layoutArea = child.layoutArea;
  140. child.margin.computeArea(child.actualSize, layoutArea);
  141. max = Math.max(max, h ? layoutArea.height : layoutArea.width);
  142. }
  143. for (var _b = 0, _c = prim.children; _b < _c.length; _b++) {
  144. var child = _c[_b];
  145. child.layoutAreaPos = new BABYLON.Vector2(x, y);
  146. var layoutArea = child.layoutArea;
  147. if (h) {
  148. x += layoutArea.width;
  149. child.layoutArea = new BABYLON.Size(layoutArea.width, max);
  150. }
  151. else {
  152. y += layoutArea.height;
  153. child.layoutArea = new BABYLON.Size(max, layoutArea.height);
  154. }
  155. }
  156. prim._clearFlags(BABYLON.SmartPropertyPrim.flagLayoutDirty);
  157. }
  158. };
  159. Object.defineProperty(StackPanelLayoutEngine.prototype, "isChildPositionAllowed", {
  160. get: function () {
  161. return false;
  162. },
  163. enumerable: true,
  164. configurable: true
  165. });
  166. StackPanelLayoutEngine._horizontal = null;
  167. StackPanelLayoutEngine._vertical = null;
  168. StackPanelLayoutEngine = __decorate([
  169. BABYLON.className("StackPanelLayoutEngine")
  170. ], StackPanelLayoutEngine);
  171. return StackPanelLayoutEngine;
  172. })(LayoutEngineBase);
  173. BABYLON.StackPanelLayoutEngine = StackPanelLayoutEngine;
  174. })(BABYLON || (BABYLON = {}));