Marker[] marker;
class Marker {
  PImage[] idleImages;
  PImage[] actionImages;
  String idlePath;
  String actionPath;
  int id;
  int frameIdle;
  int frameAction;
  int idleImageCount, actionImageCount;
  float canvasPosX, canvasPosY;
  float canvasImgW, canvasImgH;
  boolean dead;
  boolean loadActionRoutine;
  boolean hasAction;
  boolean waitForPlay;
  boolean playAction;
  boolean locked;
  boolean drag;
  boolean hover() {
    if (viewX > canvasPosX && viewX < canvasPosX + canvasImgW && viewY > canvasPosY && viewY < canvasPosY+ canvasImgW) {
      return true;
    } else {
      return false;
    }
  }
  Marker(int idtemp, File[] i, int x, int y, String fp) { //casual idle import via drag'n'drop, no action animation
    id = idtemp;
    idlePath = fp + "/idle";
    loadSequence(i);
    hasAction = false;
    canvasPosX = viewyfyX(x) - canvasImgW/2;
    canvasPosY = viewyfyY(y) - canvasImgH/2;
  }
  Marker(int idtemp, File[] i, File[] a, int x, int y, String fp) { //casual import via drag'n'drop with action animation
    id = idtemp;
    idlePath = fp + "/idle";
    actionPath = fp + "/action";
    loadSequence(i);
    loadSequence(a);
    hasAction = true;
    canvasPosX = viewyfyX(x) - canvasImgW/2;
    canvasPosY = viewyfyY(y) - canvasImgH/2;
  }
  Marker(int idtemp, String i, String a, int x, int y, float cw, float ch) { //import from savefile
    id = idtemp;
    idlePath = i;
    actionPath = a;
    File idleDir = new File(i);
    File[] idleFiles = idleDir.listFiles();
    loadSequence(idleFiles);
    if (!a.equals("null")) {
      File actionDir = new File(a);
      File[] actionFiles = actionDir.listFiles();
      loadSequence(actionFiles);
      hasAction = true;
    } else {
      hasAction = false;
    }
    canvasPosX = x;
    canvasPosY = y;
    canvasImgW = cw;
    canvasImgH = ch;
  }
  void loadSequence(File[] filestemp) { //load images
    File files[] = filestemp;
    String filePaths[] = new String[files.length];
    int imageCount = 0;
    for (int i = 0; i < files.length; i++) {
      filePaths[i] = files[i].getAbsolutePath();
      if (isImage(filePaths[i])) {
        imageCount++;
      }
    }
    filePaths = sort(filePaths);
    if (imageCount > 0) {
      if (!loadActionRoutine) idleImages = new PImage[imageCount];
      else actionImages = new PImage[imageCount];
      imageCount = 0;
      println("Loading animation...");
      for (int i = 0; i < files.length; i++) {
        if (isImage(files[i].getAbsolutePath())) {
          if (!loadActionRoutine) idleImages[imageCount] = loadImage(filePaths[i]);
          else actionImages[imageCount] = loadImage(filePaths[i]);
          println("Loading image number..." + imageCount);
          imageCount++;
        }
      }
      if (!loadActionRoutine) {
        idleImageCount = imageCount;
        canvasImgW = idleImages[0].width; //assuming all images have the same size
        canvasImgH = idleImages[0].height;
        loadActionRoutine = true; //CAREFUL!! This is specifically designed for maximum 2 imports.
      } else {
        actionImageCount = imageCount;
        loadActionRoutine = false;
      }
    } else {
      println("This folder contains no PNGs and / or no GIFs.");
    }
  }

  void rightClicked() {
    if (!locked) {
      draggingAsset = true;
      drag = true;
      clickedMarkerID = id;
    }
  }

  void leftClicked() {
    if (hasAction && !waitForPlay && !playAction) {
      waitForPlay = true;
    }
  }

  void display() {
    if (!dead) {
      frameIdle = (frameIdle+2) % idleImageCount;
      if (waitForPlay) {
        if (frameIdle >= idleImageCount - 1) {
          waitForPlay = false;
          playAction = true;
        }
      }
      if (playAction) {
        frameAction = (frameAction+2) % actionImageCount;
        renderer.image(actionImages[frameAction], canvasPosX, canvasPosY, canvasImgW, canvasImgH);
        if (frameAction >= actionImageCount - 3) {
          playAction = false;
          frameIdle = 0;
        }
      } else {
        renderer.image(idleImages[frameIdle], canvasPosX, canvasPosY, canvasImgW, canvasImgH);
      }
    }
  }
  void saveToFile() throws IOException { //open the IO stream outside...
    //output.write("\"id\": " + str(id) + ",\n");
    //output.write("\"idlePath\": \"/" + idlePath + "\",\n");
    //output.write("\"actionPath\": \"" + actionPath + "\",\n");
    //output.write("\"x-position\": " + canvasPosX +",\n");
    //output.write("\"y-position\": " + canvasPosY + ",\n");
    //output.write("\"width\": " + canvasImgW + ",\n");
    //output.write("\"height\": " + canvasImgH);
    output.write(str(id) + "\n");
    output.write(idlePath + "\n");
    output.write(actionPath + "\n");
    output.write(canvasPosX +"\n");
    output.write(canvasPosY + "\n");
    output.write(canvasImgW + "\n");
    output.write(canvasImgH + "\n");
  }
  void kill() {
    dead = true;
    drag = false;
    Marker temp = marker[activeMarkers-1];
    marker[activeMarkers-1] = marker[id];
    marker[id] = temp;
    activeMarkers--;
    draggingAsset = false;
  }
}