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