DWVv0.37.0
src/tools/floodfill.jsjavascript
import {Style} from '../gui/style.js';
import {LayerGroupPointer} from './layerGroupPointer.js';
import {FloodfillDragBehavior} from './behaviors/floodfillDragBehavior.js';
import {ScrollWheelBehavior} from './behaviors/wheelBehavior.js';
import {DrawSelect} from './behaviors/drawSelect.js';
import {DrawShapeHandler} from './shapes/drawShapeHandler.js';

/**
 * @import {App} from '../app/application.js';
 */

/**
 * Floodfill painting tool.
 */
export class Floodfill extends LayerGroupPointer {
  /**
   * Associated app.
   *
   * @type {App}
   */
  #app;

  /**
   * @type {DrawSelect}
   */
  #drawSelect;

  /**
   * @type {FloodfillDragBehavior}
   */
  #dragBehavior;

  /**
   * Drawing style.
   *
   * @type {Style}
   */
  #style = new Style();

  /**
   * @param {App} app The associated application.
   */
  constructor(app) {
    const style = new Style();
    const shapeHandler = new DrawShapeHandler(app);
    const drawSelect = new DrawSelect(app, shapeHandler);
    const dragBehavior = new FloodfillDragBehavior(
      app,
      style,
      drawSelect
    );
    super({
      stageController: app.getStageController(),
      dragBehavior,
      wheelBehavior: new ScrollWheelBehavior()
    });
    this.#app = app;
    this.#drawSelect = drawSelect;
    this.#dragBehavior = dragBehavior;
    this.#style = style;
  }

  /**
   * Handle key down event.
   *
   * @param {object} event The key down event.
   */
  keydown = (event) => {
    event.context = 'Floodfill';
    this.#app.onKeydown(event);

    if (event.key === 'Escape') {
      this.#drawSelect.disableAndResetEditor();
    }
  };

  /**
   * Activate the tool.
   *
   * @param {boolean} bool The flag to activate or not.
   */
  activate(bool) {
    if (bool) {
      // init with the app window scale
      const stgCtrl = this.#app.getStageController();
      const lg = stgCtrl.getActiveLayerGroup();
      this.#style.setBaseScale(lg.getBaseScale());
      // set the default to the first in the list
      this.setFeatures({shapeColour: this.#style.getLineColour()});
    } else {
      this.cancel();
    }

    this.#drawSelect.activate(bool);
  }

  /**
   * Initialise the tool.
   */
  init() {
    // does nothing
  }

  /**
   * Set the tool live features: shape colour.
   *
   * @param {object} features The list of features.
   */
  setFeatures(features) {
    if (typeof features.shapeColour !== 'undefined') {
      this.#style.setLineColour(features.shapeColour);
    }

    this.#dragBehavior.setFeatures(features);
  }

} // Floodfill class