AcrofonyOptionCard.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /************************************************************************
  2. * AcrofonyOptionCard.js
  3. ************************************************************************
  4. * Copyright (c) 2021 Pedro Tonini Rosenberg Schneider.
  5. *
  6. * This file is part of Alfabetiza.
  7. *
  8. * Alfabetiza is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * Alfabetiza is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with Alfabetiza. If not, see <https://www.gnu.org/licenses/>.
  20. *************************************************************************/
  21. class AcrofonyOptionCard extends Object2D
  22. {
  23. constructor(name)
  24. {
  25. super(name);
  26. /** @type {String} */
  27. this.syllable = "";
  28. /** @type {Boolean} */
  29. this.isAnswer = false;
  30. /** @type {Boolean} */
  31. this.selected = false;
  32. /** @type {Boolean} */
  33. this.selectable = true;
  34. /** @type {Boolean} */
  35. this.mouseOver = false;
  36. /** @type {Boolean} */
  37. this.mousePress = false;
  38. /** @type {Color} */
  39. this.fillColor = new Color(200, 200, 200);
  40. /** @type {Boolean} */
  41. this.tweenStarted = false;
  42. /** @type {Tween} */
  43. this.tween = null;
  44. /** @type {Timer} */
  45. this.timer = null;
  46. }
  47. _initSignals()
  48. {
  49. this.addSignal("selected");
  50. }
  51. _setup()
  52. {
  53. var area = new Area2D("area", SHAPES.RECT, new Rect(250, 200), true);
  54. area.connect("mouseEntered", this, "_onMouseEntered");
  55. area.connect("mouseExited", this, "_onMouseExited");
  56. this.addChild(area);
  57. this.addChild(new AcrofonyCardVisualEffect("CardVfx"));
  58. this.tween = new Tween("Tween");
  59. this.tween.interpolateProperty(this, "scale", PROPERTY_TYPE.VECTOR2, Vector2.ZERO(), Vector2.ONE(), 2, TRANS_TYPE.ELASTIC, EASE_TYPE.OUT);
  60. this.addChild(this.tween);
  61. this.timer = new Timer("Timer", 1, false, true);
  62. this.timer.connect("timeout", this, "_onTimerTimeout");
  63. this.addChild(this.timer);
  64. }
  65. _update( /** @type {Number} */ delta)
  66. {
  67. if (this.visible && !this.tweenStarted)
  68. {
  69. this.timer.start();
  70. this.tween.startAll();
  71. this.tweenStarted = true;
  72. }
  73. if (this.selectable && this.mouseOver)
  74. {
  75. if (InputHandler.mouseIsClicked)
  76. {
  77. this.selected = true;
  78. this.selectable = false;
  79. this.emitSignal("selected", this.isAnswer);
  80. }
  81. if (InputHandler.mouseIsPressed)
  82. {
  83. this.scale.x = max(this.scale.x - 3.0 * delta, 0.95);
  84. this.scale.y = max(this.scale.y - 3.0 * delta, 0.95);
  85. }
  86. else
  87. {
  88. this.scale.x = min(this.scale.x + 2.0 * delta, 1.2);
  89. this.scale.y = min(this.scale.y + 2.0 * delta, 1.2);
  90. }
  91. }
  92. else
  93. {
  94. this.scale.x = max(this.scale.x - 2.0 * delta, 1);
  95. this.scale.y = max(this.scale.y - 2.0 * delta, 1);
  96. }
  97. }
  98. _draw( /** @type {Number} */ delta, /** @type {p5.Graphics} */ db)
  99. {
  100. db.rectMode(CENTER);
  101. db.fill(this.fillColor.getP5Color());
  102. db.rect(0, 0, 250, 200, 10, 10);
  103. db.textAlign(CENTER, CENTER);
  104. db.fill(0);
  105. db.textSize(50);
  106. db.text(this.syllable, 0, 0);
  107. }
  108. _onMouseEntered()
  109. {
  110. this.mouseOver = true;
  111. }
  112. _onMouseExited()
  113. {
  114. this.mouseOver = false;
  115. }
  116. _onTimerTimeout()
  117. {
  118. this.tween.stopAll();
  119. }
  120. }