Valise1OptionCard.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /************************************************************************
  2. * Valise1OptionCard.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 Valise1OptionCard 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 sprite = new Sprite2D("Sprite", this.thumb);
  56. sprite.width = 250;
  57. sprite.height = 250;
  58. this.addChild(sprite);
  59. var area = new Area2D("area", SHAPES.RECT, new Rect(275, 275), true);
  60. area.connect("mouseEntered", this, "_onMouseEntered");
  61. area.connect("mouseExited", this, "_onMouseExited");
  62. this.addChild(area);
  63. this.addChild(new Valise1CardVisualEffect("CardVfx"));
  64. this.tween = new Tween("Tween");
  65. this.tween.interpolateProperty(this, "scale", PROPERTY_TYPE.VECTOR2, Vector2.ZERO(), Vector2.ONE(), 2, TRANS_TYPE.ELASTIC, EASE_TYPE.OUT);
  66. this.addChild(this.tween);
  67. this.setScale(0, 0);
  68. this.timer = new Timer("Timer", 1, false, true);
  69. this.timer.connect("timeout", this, "_onTimerTimeout");
  70. this.addChild(this.timer);
  71. }
  72. _update( /** @type {Number} */ delta)
  73. {
  74. if (this.visible && !this.tweenStarted)
  75. {
  76. this.timer.start();
  77. this.tween.startAll();
  78. this.tweenStarted = true;
  79. }
  80. if (this.selectable && this.mouseOver)
  81. {
  82. if (InputHandler.mouseIsClicked)
  83. {
  84. this.selected = true;
  85. this.selectable = false;
  86. this.emitSignal("selected", this.isAnswer);
  87. }
  88. if (InputHandler.mouseIsPressed)
  89. {
  90. this.scale.x = max(this.scale.x - 3.0 * delta, 0.95);
  91. this.scale.y = max(this.scale.y - 3.0 * delta, 0.95);
  92. }
  93. else
  94. {
  95. this.scale.x = min(this.scale.x + 2.0 * delta, 1.2);
  96. this.scale.y = min(this.scale.y + 2.0 * delta, 1.2);
  97. }
  98. }
  99. else
  100. {
  101. this.scale.x = max(this.scale.x - 2.0 * delta, 1);
  102. this.scale.y = max(this.scale.y - 2.0 * delta, 1);
  103. }
  104. }
  105. _draw( /** @type {Number} */ delta, /** @type {p5.Graphics} */ db)
  106. {
  107. db.rectMode(CENTER);
  108. db.fill(this.fillColor.getP5Color());
  109. db.rect(0, 0, 275, 275, 10, 10);
  110. }
  111. _onMouseEntered()
  112. {
  113. this.mouseOver = true;
  114. }
  115. _onMouseExited()
  116. {
  117. this.mouseOver = false;
  118. }
  119. _onTimerTimeout()
  120. {
  121. this.tween.stopAll();
  122. }
  123. }