rectangle.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { Container } from "./container";
  2. import { Measure } from "../measure";
  3. import { _TypeStore } from 'babylonjs/Misc/typeStore';
  4. import { serialize } from 'babylonjs/Misc/decorators';
  5. /** Class used to create rectangle container */
  6. export class Rectangle extends Container {
  7. private _thickness = 1;
  8. private _cornerRadius = 0;
  9. /** Gets or sets border thickness */
  10. @serialize()
  11. public get thickness(): number {
  12. return this._thickness;
  13. }
  14. public set thickness(value: number) {
  15. if (this._thickness === value) {
  16. return;
  17. }
  18. this._thickness = value;
  19. this._markAsDirty();
  20. }
  21. /** Gets or sets the corner radius angle */
  22. @serialize()
  23. public get cornerRadius(): number {
  24. return this._cornerRadius;
  25. }
  26. public set cornerRadius(value: number) {
  27. if (value < 0) {
  28. value = 0;
  29. }
  30. if (this._cornerRadius === value) {
  31. return;
  32. }
  33. this._cornerRadius = value;
  34. this._markAsDirty();
  35. }
  36. /**
  37. * Creates a new Rectangle
  38. * @param name defines the control name
  39. */
  40. constructor(public name?: string) {
  41. super(name);
  42. }
  43. protected _getTypeName(): string {
  44. return "Rectangle";
  45. }
  46. protected _localDraw(context: CanvasRenderingContext2D): void {
  47. context.save();
  48. if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
  49. context.shadowColor = this.shadowColor;
  50. context.shadowBlur = this.shadowBlur;
  51. context.shadowOffsetX = this.shadowOffsetX;
  52. context.shadowOffsetY = this.shadowOffsetY;
  53. }
  54. if (this._background) {
  55. context.fillStyle = this._background;
  56. if (this._cornerRadius) {
  57. this._drawRoundedRect(context, this._thickness / 2);
  58. context.fill();
  59. } else {
  60. context.fillRect(this._currentMeasure.left, this._currentMeasure.top, this._currentMeasure.width, this._currentMeasure.height);
  61. }
  62. }
  63. if (this._thickness) {
  64. if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
  65. context.shadowBlur = 0;
  66. context.shadowOffsetX = 0;
  67. context.shadowOffsetY = 0;
  68. }
  69. if (this.color) {
  70. context.strokeStyle = this.color;
  71. }
  72. context.lineWidth = this._thickness;
  73. if (this._cornerRadius) {
  74. this._drawRoundedRect(context, this._thickness / 2);
  75. context.stroke();
  76. } else {
  77. context.strokeRect(this._currentMeasure.left + this._thickness / 2, this._currentMeasure.top + this._thickness / 2,
  78. this._currentMeasure.width - this._thickness, this._currentMeasure.height - this._thickness);
  79. }
  80. }
  81. context.restore();
  82. }
  83. protected _additionalProcessing(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  84. super._additionalProcessing(parentMeasure, context);
  85. this._measureForChildren.width -= 2 * this._thickness;
  86. this._measureForChildren.height -= 2 * this._thickness;
  87. this._measureForChildren.left += this._thickness;
  88. this._measureForChildren.top += this._thickness;
  89. }
  90. private _drawRoundedRect(context: CanvasRenderingContext2D, offset: number = 0): void {
  91. var x = this._currentMeasure.left + offset;
  92. var y = this._currentMeasure.top + offset;
  93. var width = this._currentMeasure.width - offset * 2;
  94. var height = this._currentMeasure.height - offset * 2;
  95. var radius = Math.min(height / 2 - 2, Math.min(width / 2 - 2, this._cornerRadius));
  96. context.beginPath();
  97. context.moveTo(x + radius, y);
  98. context.lineTo(x + width - radius, y);
  99. context.quadraticCurveTo(x + width, y, x + width, y + radius);
  100. context.lineTo(x + width, y + height - radius);
  101. context.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
  102. context.lineTo(x + radius, y + height);
  103. context.quadraticCurveTo(x, y + height, x, y + height - radius);
  104. context.lineTo(x, y + radius);
  105. context.quadraticCurveTo(x, y, x + radius, y);
  106. context.closePath();
  107. }
  108. protected _clipForChildren(context: CanvasRenderingContext2D) {
  109. if (this._cornerRadius) {
  110. this._drawRoundedRect(context, this._thickness);
  111. context.clip();
  112. }
  113. }
  114. }
  115. _TypeStore.RegisteredTypes["BABYLON.GUI.Rectangle"] = Rectangle;