rectangle.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON.GUI {
  3. export class Rectangle extends Container {
  4. private _thickness = 1;
  5. private _background: string;
  6. private _cornerRadius = 0;
  7. public get thickness(): number {
  8. return this._thickness;
  9. }
  10. public set thickness(value: number) {
  11. if (this._thickness === value) {
  12. return;
  13. }
  14. this._thickness = value;
  15. this._markAsDirty();
  16. }
  17. public get cornerRadius(): number {
  18. return this._cornerRadius;
  19. }
  20. public set cornerRadius(value: number) {
  21. if (value < 0) {
  22. value = 0;
  23. }
  24. if (this._cornerRadius === value) {
  25. return;
  26. }
  27. this._cornerRadius = value;
  28. this._markAsDirty();
  29. }
  30. public get background(): string {
  31. return this._background;
  32. }
  33. public set background(value: string) {
  34. if (this._background === value) {
  35. return;
  36. }
  37. this._background = value;
  38. this._markAsDirty();
  39. }
  40. constructor(public name: string) {
  41. super(name);
  42. }
  43. protected _localDraw(context: CanvasRenderingContext2D): void {
  44. context.save();
  45. if (this._background) {
  46. context.fillStyle = this._background;
  47. if (this._cornerRadius) {
  48. this._drawRoundedRect(context, this._thickness / 2);
  49. context.fill();
  50. } else {
  51. context.fillRect(this._currentMeasure.left, this._currentMeasure.top, this._currentMeasure.width, this._currentMeasure.height);
  52. }
  53. }
  54. if (this._thickness) {
  55. if (this.color) {
  56. context.strokeStyle = this.color;
  57. }
  58. context.lineWidth = this._thickness;
  59. if (this._cornerRadius) {
  60. this._drawRoundedRect(context, this._thickness / 2);
  61. context.stroke();
  62. } else {
  63. context.strokeRect(this._currentMeasure.left + this._thickness / 2, this._currentMeasure.top + this._thickness / 2,
  64. this._currentMeasure.width - this._thickness, this._currentMeasure.height - this._thickness);
  65. }
  66. }
  67. context.restore();
  68. }
  69. protected _additionalProcessing(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  70. super._additionalProcessing(parentMeasure, context);
  71. this._measureForChildren.width -= 2 * this._thickness;
  72. this._measureForChildren.height -= 2 * this._thickness;
  73. this._measureForChildren.left += this._thickness;
  74. this._measureForChildren.top += this._thickness;
  75. }
  76. private _drawRoundedRect(context: CanvasRenderingContext2D, offset: number = 0): void {
  77. var x = this._currentMeasure.left + offset;
  78. var y = this._currentMeasure.top + offset;
  79. var width = this._currentMeasure.width - offset * 2;
  80. var height = this._currentMeasure.height - offset * 2;
  81. var radius = Math.min(height / 2 - 2, Math.min(width / 2 - 2, this._cornerRadius));
  82. context.beginPath();
  83. context.moveTo(x + radius, y);
  84. context.lineTo(x + width - radius, y);
  85. context.quadraticCurveTo(x + width, y, x + width, y + radius);
  86. context.lineTo(x + width, y + height - radius);
  87. context.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
  88. context.lineTo(x + radius, y + height);
  89. context.quadraticCurveTo(x, y + height, x, y + height - radius);
  90. context.lineTo(x, y + radius);
  91. context.quadraticCurveTo(x, y, x + radius, y);
  92. context.closePath();
  93. }
  94. protected _clipForChildren(context: CanvasRenderingContext2D) {
  95. if (this._cornerRadius) {
  96. this._drawRoundedRect(context, this._thickness);
  97. context.clip();
  98. }
  99. }
  100. }
  101. }