
// DrawCell.java
// Andrew Davison, Jan 2023, ad@coe.psu.ac.th

/* A libgdx version of the Java SimplePaint application.

   The drawing canvas defined here implements the same 
   functionality as the SimplePaint class (which subclasses
   Java's JPanel).

*/

package com.mygdx.game;


import java.util.*;

import com.badlogic.gdx.*;
import com.badlogic.gdx.scenes.scene2d.*;

import com.badlogic.gdx.graphics.*;
import com.badlogic.gdx.graphics.g2d.*;
import com.badlogic.gdx.graphics.glutils.*;

import com.badlogic.gdx.math.*;
import com.badlogic.gdx.graphics.*;



public class DrawCell extends Actor
{
  // shape drawing constants
  public static final int FREEHAND = 0;
  public static final int OVAL = 1;
  public static final int RECTANGLE = 2;
  public static final int LINE = 3;

  private static final float THICKNESS = 10f;   // thickness of freehand line

  private ShapeRenderer sr;

  private ArrayList<Shape> shapes;

  private Shape currShape = null;
  private int currShapeType;    // FREEHAND, OVAL, RECTANGLE, or LINE
  private boolean isFilled;

  private float xCoord, yCoord;   // mouse location



  public DrawCell(float width, float height)
  {
    // System.out.println("(w,h): (" + width + ", " + height +")");
    setSize(width, height);

    sr = new ShapeRenderer();
    sr.setAutoShapeType(true);

    shapes = new ArrayList<Shape>();

    // defaults for drawing shape and color
    currShapeType = FREEHAND;
    isFilled = false;
    setColor(Color.BLACK);
  }  // end of DrawCell()


  // --------- methods called by the Controls -----------------

  public void setShape(int shapeType)
  { currShapeType = shapeType; }


  public void setFilled(boolean b)
  { isFilled = b; }


  public void setDrawColor(Color c)
  { setColor(c); }


  public void clear()
  // wipe the drawing surface clean
  { shapes.clear();
    currShape = null;
  }  // end of clear()


  // --------- 'mouse' methods called from the app -----------------
  /* these methods implement similar functionality as the 
     mouse listener methods in the SimplePaint class.
  */


  public void pressed(float x, float y)
  // start a new shape
  {
    xCoord = x;   // remember location
    yCoord = y;

    // the new shape depends on the current shape type
    if (currShapeType == FREEHAND)   // use a thick line
	    currShape = new LineShape(xCoord, yCoord, xCoord, yCoord, 
                                                  getColor(), THICKNESS);
    else if (currShapeType == OVAL)
	    currShape = new OvalShape(xCoord, yCoord, xCoord, yCoord, 
			                              isFilled, getColor());
    else if (currShapeType == RECTANGLE)
	    currShape = new RectangleShape(xCoord, yCoord, xCoord, yCoord, 
		                                  isFilled, getColor());
    else if (currShapeType == LINE)
	    currShape = new LineShape(xCoord, yCoord, xCoord, yCoord, 
                                                      getColor(), 1f);
    else
	    System.out.println("Unknown shape type in draw panel");
  } // end of pressed()


  public void dragged(float x, float y)
  // grow/adjust the current shape by dragging
  {
    float xc = x;
    float yc = y;

    if (currShape == null)
	  System.out.println("Error: current shape is null");
    else if (currShapeType == FREEHAND) {  // end current line, start another
	  currShape.adjustShape(xCoord, yCoord, xc, yc);
      shapes.add(currShape);
      xCoord = xc; yCoord = yc;
      currShape = new LineShape(xCoord, yCoord, xCoord, yCoord, 
                                                   getColor(), THICKNESS);
    }
    else if ((currShapeType == OVAL) || (currShapeType == RECTANGLE) ||
             (currShapeType == LINE)) {   // adjust the shape
	    currShape.adjustShape(xCoord, yCoord, xc, yc);
    }
    else
	    System.out.println("Unknown shape type in draw panel");

  } // end of dragged()


  public void released(float x, float y)
  // store the current shape since it is finished
  {
    shapes.add(currShape);
    currShape = null;
  } // end of released()


  // ---------------------------------------------


  @Override
  public void draw(Batch batch, float parentAlpha) 
  /* This function does the same task as paintComponent()
     in the SimplePaint class. 
  */
  {
    super.draw(batch, parentAlpha);

    batch.end();   // switch to ShapeRenderer for drawing

    sr.setProjectionMatrix(batch.getProjectionMatrix());

    // draw the stored shapes
    for (int i = 0; i < shapes.size(); i++)
      shapes.get(i).drawShape(sr);

    // draw the current shape
    if (currShape != null)
      currShape.drawShape(sr);


    Gdx.gl.glDisable(GL20.GL_BLEND);
    Gdx.gl.glLineWidth(1f);
    sr.setColor(Color.WHITE);
    
    batch.begin();   // back to Batch rendering
  }  // end of draw()


  public void dispose()
  {  sr.dispose();  }


}  // end of DrawCell