Plex-FAWE/src/com/sk89q/worldedit/regions/Polygonal2DRegionSelector.java

126 lines
3.6 KiB
Java
Raw Normal View History

2011-02-20 01:44:39 +00:00
// $Id$
/*
* WorldEdit
* Copyright (C) 2010, 2011 sk89q <http://www.sk89q.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.regions;
import java.util.ArrayList;
import java.util.List;
import com.sk89q.worldedit.BlockVector;
import com.sk89q.worldedit.BlockVector2D;
import com.sk89q.worldedit.IncompleteRegionException;
import com.sk89q.worldedit.LocalPlayer;
2011-03-12 06:43:02 +00:00
import com.sk89q.worldedit.LocalSession;
2011-02-20 01:44:39 +00:00
import com.sk89q.worldedit.Vector;
/**
* Selector for polygonal regions.
*
* @author sk89q
*/
public class Polygonal2DRegionSelector implements RegionSelector {
protected BlockVector pos1;
protected Polygonal2DRegion region = new Polygonal2DRegion();
public boolean selectPrimary(Vector pos) {
if (pos1 != null && pos1.equals(pos)) {
return false;
}
pos1 = pos.toBlockVector();
region = new Polygonal2DRegion();
region.addPoint(pos);
region.expandY(pos.getBlockY());
return true;
}
public boolean selectSecondary(Vector pos) {
if (region.size() > 0) {
List<BlockVector2D> points = region.getPoints();
BlockVector2D lastPoint = points.get(region.size() - 1);
if (lastPoint.getBlockX() == pos.getBlockX()
&& lastPoint.getBlockZ() == pos.getBlockZ()) {
return false;
}
if (points.size() >= 20) {
return false;
}
}
region.addPoint(pos);
region.expandY(pos.getBlockY());
return true;
}
2011-03-12 06:43:02 +00:00
public void explainPrimarySelection(LocalPlayer player,
LocalSession session, Vector pos) {
2011-02-20 01:44:39 +00:00
player.print("Starting a new polygon at " + pos + ".");
}
2011-03-12 06:43:02 +00:00
public void explainSecondarySelection(LocalPlayer player,
LocalSession session, Vector pos) {
2011-02-20 01:44:39 +00:00
player.print("Added point #" + region.size() + " at " + pos + ".");
}
public BlockVector getPrimaryPosition() throws IncompleteRegionException {
if (pos1 == null) {
throw new IncompleteRegionException();
}
return pos1;
}
public Region getRegion() throws IncompleteRegionException {
if (!isDefined()) {
throw new IncompleteRegionException();
}
return region;
}
public boolean isDefined() {
return region.size() > 2;
}
public void learnChanges() {
}
public void clear() {
pos1 = null;
region = new Polygonal2DRegion();
}
public String getTypeName() {
return "2Dx1D polygon";
}
public List<String> getInformationLines() {
List<String> lines = new ArrayList<String>();
lines.add("# points: " + region.size());
return lines;
}
2011-03-12 06:43:02 +00:00
public String getTypeId() {
return "polygon2d";
}
public int getArea() {
return region.getArea();
}
2011-02-20 01:44:39 +00:00
}