Valise2OptionCard.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /************************************************************************
  2. * Valise2OptionCard.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 Valise2OptionCard extends Object2D
  22. {
  23. constructor(name)
  24. {
  25. super(name);
  26. /** @type {String} */
  27. this.str = "";
  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 {TextureRes} */
  39. this.thumb = null;
  40. /** @type {Color} */
  41. this.fillColor = new Color(200, 200, 200);
  42. /** @type {Boolean} */
  43. this.tweenStarted = false;
  44. /** @type {Tween} */
  45. this.tween = null;
  46. /** @type {Timer} */
  47. this.timer = null;
  48. }
  49. _initSignals()
  50. {
  51. this.addSignal("selected");
  52. }
  53. _setup()
  54. {
  55. var area = new Area2D("area", SHAPES.RECT, new Rect(275, 275), true);
  56. area.connect("mouseEntered", this, "_onMouseEntered");
  57. area.connect("mouseExited", this, "_onMouseExited");
  58. this.addChild(area);
  59. this.addChild(new Valise2CardVisualEffect("CardVfx"));
  60. this.tween = new Tween("Tween");
  61. this.tween.interpolateProperty(this, "scale", PROPERTY_TYPE.VECTOR2, Vector2.ZERO(), Vector2.ONE(), 2, TRANS_TYPE.ELASTIC, EASE_TYPE.OUT);
  62. this.addChild(this.tween);
  63. this.setScale(0, 0);
  64. this.timer = new Timer("Timer", 1, false, true);
  65. this.timer.connect("timeout", this, "_onTimerTimeout");
  66. this.addChild(this.timer);
  67. }
  68. _update( /** @type {Number} */ delta)
  69. {
  70. if (this.visible && !this.tweenStarted)
  71. {
  72. this.timer.start();
  73. this.tween.startAll();
  74. this.tweenStarted = true;
  75. }
  76. if (this.selectable && this.mouseOver)
  77. {
  78. if (InputHandler.mouseIsClicked)
  79. {
  80. this.selected = true;
  81. this.selectable = false;
  82. this.emitSignal("selected", this.isAnswer);
  83. }
  84. if (InputHandler.mouseIsPressed)
  85. {
  86. this.scale.x = max(this.scale.x - 3.0 * delta, 0.95);
  87. this.scale.y = max(this.scale.y - 3.0 * delta, 0.95);
  88. }
  89. else
  90. {
  91. this.scale.x = min(this.scale.x + 2.0 * delta, 1.2);
  92. this.scale.y = min(this.scale.y + 2.0 * delta, 1.2);
  93. }
  94. }
  95. else
  96. {
  97. this.scale.x = max(this.scale.x - 2.0 * delta, 1);
  98. this.scale.y = max(this.scale.y - 2.0 * delta, 1);
  99. }
  100. }
  101. _draw( /** @type {Number} */ delta, /** @type {p5.Graphics} */ db)
  102. {
  103. db.rectMode(CENTER);
  104. db.fill(this.fillColor.getP5Color());
  105. db.rect(0, 0, 275, 275, 10, 10);
  106. db.textAlign(CENTER, CENTER);
  107. db.fill(0);
  108. db.textSize(40);
  109. db.text(this.str, 0, 0);
  110. }
  111. _onMouseEntered()
  112. {
  113. this.mouseOver = true;
  114. console.log(this.str);
  115. }
  116. _onMouseExited()
  117. {
  118. this.mouseOver = false;
  119. }
  120. _onTimerTimeout()
  121. {
  122. this.tween.stopAll();
  123. }
  124. }