button.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. function Button(canvas) {
  2. this.text = "Button";
  3. this.options = {position:{x: 0, y: 0, align: "center", baseline: "middle"}, padding: 10, background: {color: "#777777", hover: "#333333"}, font: {color:"#DDDDDD", stroke: "#333333", size: 72, family: "sans-serif"}};
  4. this.textMetrics = null;
  5. this.clickHandler = function() {};
  6. this.init = function(text, options) {
  7. this.text = text;
  8. this.options = Object.assign(this.options, options || {});
  9. }
  10. this.updateDelta = function(delta, canvas) {
  11. };
  12. this.addClickEvent = function(handler) {
  13. this.clickHandler = handler;
  14. }
  15. this.draw = function(context) {
  16. context.save();
  17. context.translate(this.options.position.x, this.options.position.y);
  18. context.font = this.options.font.size + "px " + this.options.font.family;
  19. context.textAlign = this.options.position.align;
  20. context.textBaseline = this.options.position.baseline;
  21. context.lineWidth = 2;
  22. if(this.isHovered) {
  23. context.fillStyle = this.options.background.hover;
  24. canvas.style.cursor = "pointer";
  25. } else {
  26. context.fillStyle = this.options.background.color;
  27. canvas.style.cursor = "default";
  28. }
  29. this.textMetrics = context.measureText(this.text);
  30. context.beginPath();
  31. var buttonRect = {x:0, y: 0, width: 100, height: 100};
  32. buttonRect.x = -(this.textMetrics.width / 2) - this.options.padding;
  33. if(this.options.position.align == "left") {
  34. buttonRect.x = -this.options.padding;
  35. }
  36. buttonRect.y = -(this.options.font.size) / 2 - this.options.padding;
  37. buttonRect.width = this.textMetrics.width + (this.options.padding * 2);
  38. buttonRect.height = this.options.font.size + (this.options.padding * 2);
  39. context.rect(buttonRect.x, buttonRect.y, buttonRect.width, buttonRect.height);
  40. context.fill();
  41. context.fillStyle = this.options.font.color;
  42. context.strokeStyle = this.options.font.stroke;
  43. if(this.isHovered) {
  44. context.fillStyle = this.options.font.hover;
  45. }
  46. context.fillText(this.text, 0, 0);
  47. context.restore();
  48. };
  49. this.checkIfHovered = function(x, y) {
  50. if(this.textMetrics == null) {
  51. return false;
  52. }
  53. var topLeft = {};
  54. topLeft.x = this.options.position.x - (this.textMetrics.width / 2) - this.options.padding;
  55. topLeft.y = this.options.position.y - (this.options.font.size / 2) - this.options.padding;
  56. var bottomRight = {};
  57. bottomRight.x = this.options.position.x + (this.textMetrics.width / 2) + this.options.padding;
  58. bottomRight.y = this.options.position.y + (this.options.font.size / 2) + this.options.padding;
  59. if(x > topLeft.x && x < bottomRight.x) {
  60. if(y > topLeft.y && y < bottomRight.y) {
  61. return true;
  62. }
  63. }
  64. return false;
  65. }
  66. this.mouseMove = function(x, y) {
  67. this.isHovered = this.checkIfHovered(x, y);
  68. }
  69. this.touchMove = function(x, y) {
  70. this.isHovered = this.checkIfHovered(x, y);
  71. }
  72. this.mouseUp = function(button, x, y) {
  73. if(this.checkIfHovered(x, y)) {
  74. this.clickHandler(button);
  75. }
  76. }
  77. this.mouseDown = function(button, x, y) {
  78. if(this.checkIfHovered(x, y)) {
  79. this.clickHandler(button);
  80. }
  81. }
  82. this.touchStart = function(x, y) {
  83. if(this.checkIfHovered(x, y)) {
  84. this.clickHandler(0);
  85. }
  86. };
  87. this.touchEnd = function(x, y) {
  88. if(this.checkIfHovered(x, y)) {
  89. this.clickHandler(0);
  90. }
  91. };
  92. };