button.js 3.2 KB

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