<commit_before>import CanvasControl from './jsiso/canvas/Control';
import CanvasInput from './jsiso/canvas/Input';
import imgLoader from './jsiso/img/load';
import jsonLoader from './jsiso/json/load';
import TileField from './jsiso/tile/Field';
import pathfind from './jsiso/pathfind/pathfind';

import Player from './player';
import ActionExecutor from './action';

let requestAnimFrame = (function() {
  return window.requestAnimationFrame ||
  window.webkitRequestAnimationFrame  ||
  window.mozRequestAnimationFrame     ||
  window.oRequestAnimationFrame       ||
  window.msRequestAnimationFrame      ||
  function(callback, element) {
    window.setTimeout(callback, 1000 / 60);
  };
})();

const ELEMENT_NAMES = {
  containerName: 'container',
  frameName: 'text-frame',
  messageName: 'text-message',
  indicatorName: 'text-indicator'
};

let appendHtml = (el, str) => {
  let div = document.createElement('div');
  div.innerHTML = str;
  while (div.children.length > 0) {
    el.appendChild(div.children[0]);
  }
}

let createElements = (container, names, outputEnabled=false) => {
  let elements = '<div id="' + names.containerName + '"></div>';
  if (outputEnabled) {
    elements += '\
      <div class="text-frame" id="' + names.frameName + '">\
        <span class="text-message" id="' + names.messageName + '"></span>\
        <span id="' + names.indicatorName + '">▼</span>\
      </div>';
  }
  appendHtml(container, elements);
}

export default function TileEngine (x, y, xrange, yrange, parent=document.body, overrides) {
  let self = this;
  overrides = Object.assign({}, overrides);

  let elementNames = Object.assign(ELEMENT_NAMES, overrides.customElementNames);
  if (!overrides.useCustomElements) {
    createElements(parent, elementNames, overrides.enableTextOutput);
  }
  const container = document.getElementById(elementNames.containerName);

  let textMessages = [];
  const textMessageFrame = document.getElementById(elementNames.frameName);
  const textMessage = document.getElementById(elementNames.messageName);
  const textIndicator = document.getElementById(elementNames.indicatorName)

  const controlWidth = container.clientWidth;
  const controlHeight = container.clientHeight;
  const context = CanvasControl.create("canvas", controlWidth, controlHeight, {}, elementNames.containerName, true);

  let backgroundLayers = [];
  let foregroundLayers = [];

  let paused = false;
  let mapLayers = [];
  let players = [];

  let actionExecutor = new ActionExecutor();
  actionExecutor.registerAction('toggleTile', (options, engine, player) => {
    let layers = mapLayers.filter((layer) => layer.zIndex === player.properties.zIndex);
    layers.forEach((layer) => {
      let currentTile = layer.getTile(options.target.x, options.target.y);
      let currentIndex = options.tiles.indexOf(currentTile);
      layer.setTile(options.target.x, options.target.y, options.tiles[(currentIndex + 1) % options.tiles.length]);
    })
  });

  //CanvasControl.fullScreen();
  const input = new CanvasInput(document, CanvasControl());

  input.mouse_action(function(coords) {
    if (paused) {
      drawMessages()
    } else {
      let player = players[0];
      let layer = player.properties.layer;
      let t = layer.applyMouseFocus(coords.x, coords.y);
      player.goTo(t.x, t.y);
      if (Math.abs(t.x - player.getTile().x) + Math.abs(t.y - player.getTile().y) === 1) {
        interact(player, t);
      }
    }
  });

  let clearText = function () {};
  let displayText = function () {};
  let drawMessages = function () {};

  if (overrides.enableTextOutput) {
    textMessageFrame.onclick = () => {
      if (paused) {
        drawMessages();
      }
    };

    clearText = () => {
      textMessages = [];
      textMessageFrame.classList.remove("in");
      textMessage.innerHTML = "";
      textIndicator.classList.remove("in");
      if (paused) {
        unpause();
      }
    }

    displayText = (text) => {
      textMessages = textMessages.concat(text);
    }

    drawMessages = () => {
      if (textMessages.length > 0) {
        pause();
        let text = textMessages.splice(0, 1)[0];
        textMessage.innerHTML = text;
        if (!("in" in textMessageFrame.classList)) {
          textMessageFrame.classList.add("in");
        }
        if (textMessages.length >= 1) {
          textIndicator.classList.add("in");
        } else {
          textIndicator.classList.remove("in");
        }
      } else {
        clearText();
      }
    }
  }

  input.keyboard(function(pressed, status) {
    let player = players[0];
    if (status) {
      if (paused) {
        if ([13, 32].indexOf(pressed) >= 0) {
          drawMessages();
        }
      } else {
        switch (pressed) {
          case 37: player.moveTo(player.getTile().x - 1, player.getTile().y); break;
          case 38: player.moveTo(player.getTile().x, player.getTile().y - 1); break;
          case 39: player.moveTo(player.getTile().x + 1, player.getTile().y); break;
          case 40: player.moveTo(player.getTile().x, player.getTile().y + 1); break;
          case 13: case 32: interact(player, player.getLookedAtTile()); break;
        }
      }
    }
  });

  let getActions = (player=null) => {
    let layers = mapLayers;
    if (player !== null) {
      layers = layers.filter((layer) => layer.zIndex === player.properties.zIndex);
    }
    let actions = [];
    layers.forEach((layer) => actions = actions.concat(layer.actions || []));<commit_msg>Allow to disable mouse and keyboard input<commit_after>import CanvasControl from './jsiso/canvas/Control';
import CanvasInput from './jsiso/canvas/Input';
import imgLoader from './jsiso/img/load';
import jsonLoader from './jsiso/json/load';
import TileField from './jsiso/tile/Field';
import pathfind from './jsiso/pathfind/pathfind';

import Player from './player';
import ActionExecutor from './action';

let requestAnimFrame = (function() {
  return window.requestAnimationFrame ||
  window.webkitRequestAnimationFrame  ||
  window.mozRequestAnimationFrame     ||
  window.oRequestAnimationFrame       ||
  window.msRequestAnimationFrame      ||
  function(callback, element) {
    window.setTimeout(callback, 1000 / 60);
  };
})();

const ELEMENT_NAMES = {
  containerName: 'container',
  frameName: 'text-frame',
  messageName: 'text-message',
  indicatorName: 'text-indicator'
};

let appendHtml = (el, str) => {
  let div = document.createElement('div');
  div.innerHTML = str;
  while (div.children.length > 0) {
    el.appendChild(div.children[0]);
  }
}

let createElements = (container, names, outputEnabled=false) => {
  let elements = '<div id="' + names.containerName + '"></div>';
  if (outputEnabled) {
    elements += '\
      <div class="text-frame" id="' + names.frameName + '">\
        <span class="text-message" id="' + names.messageName + '"></span>\
        <span id="' + names.indicatorName + '">▼</span>\
      </div>';
  }
  appendHtml(container, elements);
}

export default function TileEngine (x, y, xrange, yrange, parent=document.body, overrides) {
  let self = this;
  overrides = Object.assign({}, overrides);

  let elementNames = Object.assign(ELEMENT_NAMES, overrides.customElementNames);
  if (!overrides.useCustomElements) {
    createElements(parent, elementNames, overrides.enableTextOutput);
  }
  const container = document.getElementById(elementNames.containerName);

  let textMessages = [];
  const textMessageFrame = document.getElementById(elementNames.frameName);
  const textMessage = document.getElementById(elementNames.messageName);
  const textIndicator = document.getElementById(elementNames.indicatorName)

  const controlWidth = container.clientWidth;
  const controlHeight = container.clientHeight;
  const context = CanvasControl.create("canvas", controlWidth, controlHeight, {}, elementNames.containerName, true);

  let backgroundLayers = [];
  let foregroundLayers = [];

  let paused = false;
  let mapLayers = [];
  let players = [];

  let actionExecutor = new ActionExecutor();
  actionExecutor.registerAction('toggleTile', (options, engine, player) => {
    let layers = mapLayers.filter((layer) => layer.zIndex === player.properties.zIndex);
    layers.forEach((layer) => {
      let currentTile = layer.getTile(options.target.x, options.target.y);
      let currentIndex = options.tiles.indexOf(currentTile);
      layer.setTile(options.target.x, options.target.y, options.tiles[(currentIndex + 1) % options.tiles.length]);
    })
  });

  let clearText = function () {};
  let displayText = function () {};
  let drawMessages = function () {};

  if (overrides.enableTextOutput) {
    textMessageFrame.onclick = () => {
      if (paused) {
        drawMessages();
      }
    };

    clearText = () => {
      textMessages = [];
      textMessageFrame.classList.remove("in");
      textMessage.innerHTML = "";
      textIndicator.classList.remove("in");
      if (paused) {
        unpause();
      }
    }

    displayText = (text) => {
      textMessages = textMessages.concat(text);
    }

    drawMessages = () => {
      if (textMessages.length > 0) {
        pause();
        let text = textMessages.splice(0, 1)[0];
        textMessage.innerHTML = text;
        if (!("in" in textMessageFrame.classList)) {
          textMessageFrame.classList.add("in");
        }
        if (textMessages.length >= 1) {
          textIndicator.classList.add("in");
        } else {
          textIndicator.classList.remove("in");
        }
      } else {
        clearText();
      }
    }
  }

  const input = new CanvasInput(document, CanvasControl());
  if (overrides.enableMouseInput) {
    input.mouse_action(function(coords) {
      if (paused) {
        drawMessages()
      } else {
        let player = players[0];
        let layer = player.properties.layer;
        let t = layer.applyMouseFocus(coords.x, coords.y);
        player.goTo(t.x, t.y);
        if (Math.abs(t.x - player.getTile().x) + Math.abs(t.y - player.getTile().y) === 1) {
          interact(player, t);
        }
      }
    });
  }

  if (overrides.enableKeyboardInput) {
    input.keyboard(function(pressed, status) {
      let player = players[0];
      if (status) {
        if (paused) {
          if ([13, 32].indexOf(pressed) >= 0) {
            drawMessages();
          }
        } else {
          switch (pressed) {
            case 37: player.moveTo(player.getTile().x - 1, player.getTile().y); break;
            case 38: player.moveTo(player.getTile().x, player.getTile().y - 1); break;
            case 39: player.moveTo(player.getTile().x + 1, player.getTile().y); break;
            case 40: player.moveTo(player.getTile().x, player.getTile().y + 1); break;
            case 13: case 32: interact(player, player.getLookedAtTile()); break;
          }
        }
      }
    });
  }

  let getActions = (player=null) => {
    let layers = mapLayers;
    if (player !== null) {
      layers = layers.filter((layer) => layer.zIndex === player.properties.zIndex);
    }
    let actions = [];
    layers.forEach((layer) => actions = actions.concat(layer.actions || []));
{'fraction_non_alphanumeric': 0.15000900414190527, 'fraction_numerical': 0.006843147848010085, 'mean_word_length': 3.038909090909091, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2826900', 'n_tokens_mistral': 3397, 'n_tokens_neox': 3308, 'n_words': 681}