rectangle.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. if(this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY){
  38. context.shadowColor = this.shadowColor;
  39. context.shadowBlur = this.shadowBlur;
  40. context.shadowOffsetX = this.shadowOffsetX;
  41. context.shadowOffsetY = this.shadowOffsetY;
  42. }
  43. if (this._background) {
  44. context.fillStyle = this._background;
  45. if (this._cornerRadius) {
  46. this._drawRoundedRect(context, this._thickness / 2);
  47. context.fill();
  48. } else {
  49. context.fillRect(this._currentMeasure.left, this._currentMeasure.top, this._currentMeasure.width, this._currentMeasure.height);
  50. }
  51. }
  52. if (this._thickness) {
  53. if(this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY){
  54. context.shadowBlur = 0;
  55. context.shadowOffsetX = 0;
  56. context.shadowOffsetY = 0;
  57. }
  58. if (this.color) {
  59. context.strokeStyle = this.color;
  60. }
  61. context.lineWidth = this._thickness;
  62. if (this._cornerRadius) {
  63. this._drawRoundedRect(context, this._thickness / 2);
  64. context.stroke();
  65. } else {
  66. context.strokeRect(this._currentMeasure.left + this._thickness / 2, this._currentMeasure.top + this._thickness / 2,
  67. this._currentMeasure.width - this._thickness, this._currentMeasure.height - this._thickness);
  68. }
  69. }
  70. context.restore();
  71. }
  72. protected _additionalProcessing(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  73. super._additionalProcessing(parentMeasure, context);
  74. this._measureForChildren.width -= 2 * this._thickness;
  75. this._measureForChildren.height -= 2 * this._thickness;
  76. this._measureForChildren.left += this._thickness;
  77. this._measureForChildren.top += this._thickness;
  78. }
  79. private _drawRoundedRect(context: CanvasRenderingContext2D, offset: number = 0): void {
  80. var x = this._currentMeasure.left + offset;
  81. var y = this._currentMeasure.top + offset;
  82. var width = this._currentMeasure.width - offset * 2;
  83. var height = this._currentMeasure.height - offset * 2;
  84. var radius = Math.min(height / 2 - 2, Math.min(width / 2 - 2, this._cornerRadius));
  85. context.beginPath();
  86. context.moveTo(x + radius, y);
  87. context.lineTo(x + width - radius, y);
  88. context.quadraticCurveTo(x + width, y, x + width, y + radius);
  89. context.lineTo(x + width, y + height - radius);
  90. context.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
  91. context.lineTo(x + radius, y + height);
  92. context.quadraticCurveTo(x, y + height, x, y + height - radius);
  93. context.lineTo(x, y + radius);
  94. context.quadraticCurveTo(x, y, x + radius, y);
  95. context.closePath();
  96. }
  97. protected _clipForChildren(context: CanvasRenderingContext2D) {
  98. if (this._cornerRadius) {
  99. this._drawRoundedRect(context, this._thickness);
  100. context.clip();
  101. }
  102. }
  103. }
  104. }