Source: view/ViewCircleSprite.js

/**
 * @author    Wolfgang Kowarschick <kowa@hs-augsburg.de>
 * @copyright 2017 - 2018
 * @license   CC-BY-NC-SA-4.0
 */

import {Sprite} from 'pixi.js';

/**
 * @property {ModelCircle}   model  - the model to be visualized
 * @property {PIXI.Graphics} sprite - a graphic object that visualizes the model
 */
class ViewCircleSprite
{ /**
   * @param {Object}      p_pixi_app
   * @param {ModelCircle} p_model
   * @param {Object}      p_config
   * @param {string}      p_config.image
   * @param {Object}      p_resources
   */
  constructor(p_pixi_app, p_model, { image: p_image }, p_resources)
  { const c_sprite = new Sprite();
    c_sprite.texture = p_resources[p_image].texture;
    c_sprite.anchor.set(0.5);
    c_sprite.width  =
    c_sprite.height = 2*p_model.r;
    c_sprite.cacheAsBitmap = true;
    c_sprite.antialias     = true;
    p_pixi_app.stage.addChild(c_sprite);
  
    this.sprite = c_sprite;
    this.model  = p_model;

    this.render();
  }
  
  /**
   * Adds a listener to PixiJS events dispatched by <code>this.sprite</code>.
   * @see http://pixijs.download/dev/docs/PIXI.Sprite.html
   *
   * @param {string}   p_event    - the name of the PixiJS event to be listened to
   * @param {Function} f_listener - the listener
   */
  addEventListener(p_event, f_listener)
  { this.sprite.interactive = true;
    this.sprite.on(p_event, f_listener);
  }
  
  render()
  { this.sprite.x = this.model.x;
    this.sprite.y = this.model.y;
  }
}

export {ViewCircleSprite};