rectangle.ts 4.2 KB

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