package org.concord.energy3d.model;

import java.nio.FloatBuffer;

import org.concord.energy3d.util.SelectUtil;

import com.ardor3d.bounding.BoundingBox;
import com.ardor3d.math.ColorRGBA;
import com.ardor3d.math.Vector3;
import com.ardor3d.renderer.IndexMode;
import com.ardor3d.scenegraph.Line;
import com.ardor3d.scenegraph.Mesh;
import com.ardor3d.scenegraph.hint.CullHint;
import com.ardor3d.util.geom.BufferUtils;

public class FoundationPolygon extends HousePart {
	private static final long serialVersionUID = 1L;
	private boolean visible;

	public FoundationPolygon(final Foundation foundation) {
		super(1, 8, 0, true);
		this.container = foundation;
		final double h = foundation.getHeight();
		points.get(0).set(0.1, 0.1, h);
		points.get(1).set(0.9, 0.1, h);
		points.get(2).set(0.9, 0.9, h);
		points.get(3).set(0.1, 0.9, h);
	}

	@Override
	protected void init() {
		super.init();
		final Line line = new Line("Foundation Polygon");
		line.setLineWidth(3);
		line.getMeshData().setIndexMode(IndexMode.LineLoop);
		line.getMeshData().setVertexBuffer(BufferUtils.createVector3Buffer(6));
		line.setModelBound(new BoundingBox());
		root.attachChild(line);
		mesh = line;
		setVisible(visible);
	}

	@Override
	public void setPreviewPoint(final int x, final int y) {
		final PickedHousePart pick = SelectUtil.pickPart(x, y, container);
		Vector3 p;
		if (pick != null) {
			p = pick.getPoint().clone();
		} else {
			p = points.get(editPointIndex).clone();
		}

		final Vector3 relativeP = toRelative(p);
		final int n = points.size() / 2;
		if (editPointIndex < n) {
			points.get(editPointIndex).set(relativeP);
		} else {
			SelectUtil.getCurrentEditPointMesh().setDefaultColor(ColorRGBA.WHITE);
			editPointIndex -= n;
			final Mesh editPointShape = getEditPointShape(editPointIndex);
			SelectUtil.setCurrentEditPointMesh(editPointShape);
			editPointShape.setDefaultColor(ColorRGBA.RED);
			points.add(editPointIndex, relativeP);
			points.add(new Vector3());
			updateEditShapes();
			setEditPointsVisible(true);
		}
		draw();
	}

	@Override
	protected void drawMesh() {
		final int n = points.size() / 2;
		final FloatBuffer buf = BufferUtils.createVector3Buffer(n);
		mesh.getMeshData().setVertexBuffer(buf);
		for (int i = 0; i < n; i++) {
			final Vector3 p = getAbsPoint(i);
			BufferUtils.setInBuffer(p, buf, i);
			points.get(n + i).set(points.get(i)).addLocal(points.get(i == 0 ? n - 1 : i - 1)).multiplyLocal(0.5);
		}
		// ensure the points are on the upper surface of the foundation
		final double h = container.getHeight();
		for (int i = 0; i < points.size(); i++) {
			points.get(i).setZ(h);
		}
	}

	@Override
	protected void computeArea() {

	}

	@Override
	public boolean isCopyable() {
		return false;
	}

	@Override
	protected String getTextureFileName() {
		return null;
	}

	@Override
	public void updateTextureAndColor() {

	}

	public void setVisible(final boolean visible) {
		this.visible = visible;
		mesh.getSceneHints().setCullHint(visible ? CullHint.Inherit : CullHint.Always);
	}

	public boolean isVisible() {
		return visible;
	}

}

@@ -856,24 +856,108 @@
 , 0.9, h);%0D%0A
+%09%09setVisible(visible); // needed because edit shapes are not creates in init() yet%0D%0A
 %09%7D%0D%0A%0D%0A%09@Over
@@ -3217,61 +3217,366 @@
 ;%0D%0A%09
-%7D%0D%0A%0D%0A%09public boolean isVisible() %7B%0D%0A%09%09return visible;
+%09super.setEditPointsVisible(false);%0D%0A%09%09for (int i = 0; i %3C points.size(); i++) %7B%0D%0A%09%09%09getEditPointShape(i).getSceneHints().setAllPickingHints(visible);%0D%0A%09%09%7D%0D%0A%09%7D%0D%0A%0D%0A%09public boolean isVisible() %7B%0D%0A%09%09return visible;%0D%0A%09%7D%0D%0A%0D%0A%09@Override%0D%0A%09public void setEditPointsVisible(final boolean visible) %7B%0D%0A%09%09if (this.visible) %7B%0D%0A%09%09%09super.setEditPointsVisible(visible);%0D%0A%09%09%7D
 %0D%0A%09%7D

Fix problem with click on polygon making it visible again