Button.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { Theme } from "../Theme.js"
  2. import Tile from "./Tile.js"
  3. import { Point } from "../../spatial/Point.js"
  4. class Button {
  5. constructor(scene, position) {
  6. this.scene = scene
  7. this.position = position
  8. this.offset = { x: 0, y: 0 }
  9. this.baseColor = Theme.Colors.ivory
  10. this.currentColor = Theme.Colors.ivory
  11. this.hoverColor = Theme.Colors.darkred
  12. this.activatedColor = Theme.Colors.tangerine
  13. this.wasClickedOn = false
  14. }
  15. setOffset(offset) {
  16. this.offset = offset
  17. }
  18. setButtonCount(count) {
  19. this.buttonCount = count
  20. }
  21. draw(ctx) {
  22. ctx.fillStyle = this.currentColor
  23. ctx.save()
  24. ctx.translate(this.offset.x, this.offset.y)
  25. ctx.beginPath()
  26. if (!this.isVertical) {
  27. ctx.arc(0, this.position.x * (Button.Size + Button.PADDING), Button.Size / 2, 0, 2 * Math.PI)
  28. } else {
  29. ctx.arc(this.position.x * (Button.Size + Button.PADDING), 0, Button.Size / 2, 0, 2 * Math.PI)
  30. }
  31. ctx.fill()
  32. ctx.restore()
  33. }
  34. setButtonSize(newSize) {
  35. if(Button.Size != newSize) {
  36. Button.Size = newSize
  37. }
  38. }
  39. onInputMove(position, isDown) {
  40. if(this.currentColor == this.activatedColor) {
  41. return
  42. } else {
  43. this.currentColor = this.baseColor
  44. }
  45. if(this.isColliding(position)) {
  46. this.currentColor = this.hoverColor
  47. document.body.style.cursor = "pointer"
  48. if(isDown) {
  49. this.currentColor = this.activatedColor
  50. }
  51. }
  52. }
  53. onInputDown(position) {
  54. if(this.isColliding(position)) {
  55. this.wasClickedOn = true
  56. this.currentColor = this.activatedColor
  57. }
  58. }
  59. onInputUp(position) {
  60. if(this.isColliding(position)) {
  61. this.currentColor = this.hoverColor
  62. } else if(this.wasClickedOn) {
  63. this.currentColor = this.baseColor
  64. }
  65. this.wasClickedOn = false
  66. }
  67. isColliding(position) {
  68. let centerX = this.offset.x + this.position.x * (Button.Size + Button.PADDING)
  69. let centerY = this.offset.y
  70. let radius = Button.Size / 2
  71. if(!this.isVertical) {
  72. centerX = this.offset.x
  73. centerY = this.offset.y + this.position.x * (Button.Size + Button.PADDING)
  74. }
  75. let distance = new Point(centerX, centerY).distanceTo(new Point(position.x, position.y))
  76. return distance <= radius
  77. }
  78. onResize(canvasBounds, boardSize) {
  79. let narrowest = canvasBounds.width
  80. this.isVertical = true
  81. if (canvasBounds.width > canvasBounds.height) {
  82. narrowest = canvasBounds.height
  83. this.isVertical = false
  84. }
  85. this.setButtonSize(Math.min(72, Math.max(48, Math.floor((narrowest - boardSize.x * 4) / boardSize.x))))
  86. const buttonOffset = {
  87. x: -(this.buttonCount / 2 - 0.5) * (Button.Size + Button.PADDING),
  88. y: -2.5 * (Tile.Size + Tile.PADDING) + (Button.Size + Button.PADDING) / 2 + canvasBounds.height / 2
  89. }
  90. if (!this.isVertical) {
  91. buttonOffset.x = 3.5 * (Tile.PADDING + Tile.Size) - canvasBounds.width / 2 - (1.5 * (Button.Size / 2 + Button.PADDING))
  92. buttonOffset.y = -2.5 * (Button.Size + Button.PADDING)
  93. }
  94. this.setOffset(buttonOffset)
  95. }
  96. }
  97. Button.PADDING = 4
  98. Button.Size = 72
  99. export default Button