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

216 lines
7.3 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> and contributors
2011-02-20 01:44:39 +00:00
*
* 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.Collections;
2011-02-20 01:44:39 +00:00
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-12-13 03:20:31 +00:00
import com.sk89q.worldedit.LocalWorld;
2011-02-20 01:44:39 +00:00
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.cui.CUIRegion;
2011-09-24 17:45:03 +00:00
import com.sk89q.worldedit.cui.SelectionMinMaxEvent;
import com.sk89q.worldedit.cui.SelectionPoint2DEvent;
import com.sk89q.worldedit.cui.SelectionShapeEvent;
2011-02-20 01:44:39 +00:00
/**
* Selector for polygonal regions.
*
* @author sk89q
*/
public class Polygonal2DRegionSelector implements RegionSelector, CUIRegion {
private BlockVector pos1;
private Polygonal2DRegion region;
2011-12-13 03:20:31 +00:00
public Polygonal2DRegionSelector(LocalWorld world) {
region = new Polygonal2DRegion(world);
}
2011-11-23 01:29:48 +00:00
public Polygonal2DRegionSelector(RegionSelector oldSelector) {
this(oldSelector.getIncompleteRegion().getWorld());
if (oldSelector instanceof Polygonal2DRegionSelector) {
final Polygonal2DRegionSelector polygonal2DRegionSelector = (Polygonal2DRegionSelector) oldSelector;
pos1 = polygonal2DRegionSelector.pos1;
region = new Polygonal2DRegion(polygonal2DRegionSelector.region);
} else {
final Region oldRegion;
try {
oldRegion = oldSelector.getRegion();
} catch (IncompleteRegionException e) {
return;
}
BlockVector min = oldRegion.getMinimumPoint().toBlockVector();
BlockVector max = oldRegion.getMaximumPoint().toBlockVector();
int minY = min.getBlockY();
int maxY = max.getBlockY();
List<BlockVector2D> points = new ArrayList<BlockVector2D>(4);
points.add(new BlockVector2D(min.getX(), min.getZ()));
points.add(new BlockVector2D(min.getX(), max.getZ()));
points.add(new BlockVector2D(max.getX(), max.getZ()));
points.add(new BlockVector2D(max.getX(), min.getZ()));
pos1 = min;
region = new Polygonal2DRegion(oldRegion.getWorld(), points, minY, maxY);
}
}
public Polygonal2DRegionSelector(LocalWorld world, List<BlockVector2D> points, int minY, int maxY) {
final BlockVector2D pos2D = points.get(0);
pos1 = new BlockVector(pos2D.getX(), minY, pos2D.getZ());
region = new Polygonal2DRegion(world, points, minY, maxY);
}
2011-02-20 01:44:39 +00:00
public boolean selectPrimary(Vector pos) {
if (pos.equals(pos1)) {
2011-02-20 01:44:39 +00:00
return false;
}
2011-02-20 01:44:39 +00:00
pos1 = pos.toBlockVector();
2011-12-13 03:20:31 +00:00
region = new Polygonal2DRegion(region.getWorld());
2011-02-20 01:44:39 +00:00
region.addPoint(pos);
region.expandY(pos.getBlockY());
2011-02-20 01:44:39 +00:00
return true;
}
2011-11-23 01:29:48 +00:00
2011-02-20 01:44:39 +00:00
public boolean selectSecondary(Vector pos) {
if (region.size() > 0) {
final List<BlockVector2D> points = region.getPoints();
final BlockVector2D lastPoint = points.get(region.size() - 1);
if (lastPoint.getBlockX() == pos.getBlockX() && lastPoint.getBlockZ() == pos.getBlockZ()) {
2011-02-20 01:44:39 +00:00
return false;
}
2011-11-23 01:29:48 +00:00
2011-02-20 01:44:39 +00:00
if (points.size() >= 20) {
return false;
}
}
2011-11-23 01:29:48 +00:00
2011-02-20 01:44:39 +00:00
region.addPoint(pos);
region.expandY(pos.getBlockY());
2011-02-20 01:44:39 +00:00
return true;
}
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 + ".");
session.dispatchCUIEvent(player, new SelectionShapeEvent(getTypeID()));
2011-09-24 17:45:03 +00:00
session.dispatchCUIEvent(player, new SelectionPoint2DEvent(0, pos, getArea()));
session.dispatchCUIEvent(player, new SelectionMinMaxEvent(region.getMininumY(), region.getMaximumY()));
2011-02-20 01:44:39 +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 + ".");
2011-09-24 19:24:10 +00:00
session.dispatchCUIEvent(player, new SelectionPoint2DEvent(region.size() - 1, pos, getArea()));
session.dispatchCUIEvent(player, new SelectionMinMaxEvent(region.getMininumY(), region.getMaximumY()));
2011-02-20 01:44:39 +00:00
}
public void explainRegionAdjust(LocalPlayer player, LocalSession session) {
session.dispatchCUIEvent(player, new SelectionMinMaxEvent(region.getMininumY(), region.getMaximumY()));
}
2011-02-20 01:44:39 +00:00
public BlockVector getPrimaryPosition() throws IncompleteRegionException {
if (pos1 == null) {
throw new IncompleteRegionException();
}
2011-02-20 01:44:39 +00:00
return pos1;
}
2011-04-03 18:03:57 +00:00
public Polygonal2DRegion getRegion() throws IncompleteRegionException {
2011-02-20 01:44:39 +00:00
if (!isDefined()) {
throw new IncompleteRegionException();
}
2011-11-23 01:29:48 +00:00
2011-02-20 01:44:39 +00:00
return region;
}
2011-04-03 18:03:57 +00:00
public Polygonal2DRegion getIncompleteRegion() {
return region;
}
2011-02-20 01:44:39 +00:00
public boolean isDefined() {
return region.size() > 2;
}
public void learnChanges() {
2011-04-03 18:03:57 +00:00
BlockVector2D pt = region.getPoints().get(0);
pos1 = new BlockVector(pt.getBlockX(), region.getMinimumPoint().getBlockY(), pt.getBlockZ());
2011-02-20 01:44:39 +00:00
}
public void clear() {
pos1 = null;
2011-12-13 03:20:31 +00:00
region = new Polygonal2DRegion(region.getWorld());
2011-02-20 01:44:39 +00:00
}
public String getTypeName() {
return "2Dx1D polygon";
}
public List<String> getInformationLines() {
return Collections.singletonList("# points: " + region.size());
2011-02-20 01:44:39 +00:00
}
2011-03-12 06:43:02 +00:00
public int getArea() {
return region.getArea();
}
2011-11-23 01:29:48 +00:00
2011-04-03 18:03:57 +00:00
public int getPointCount() {
return region.getPoints().size();
}
2011-03-12 06:43:02 +00:00
public void describeCUI(LocalSession session, LocalPlayer player) {
final List<BlockVector2D> points = region.getPoints();
2011-09-24 17:45:03 +00:00
for (int id = 0; id < points.size(); id++) {
session.dispatchCUIEvent(player, new SelectionPoint2DEvent(id, points.get(id), getArea()));
2011-09-24 17:45:03 +00:00
}
session.dispatchCUIEvent(player, new SelectionMinMaxEvent(region.getMininumY(), region.getMaximumY()));
}
public void describeLegacyCUI(LocalSession session, LocalPlayer player) {
describeCUI(session, player);
2011-09-24 17:45:03 +00:00
}
public int getProtocolVersion() {
return 0;
}
public String getTypeID() {
return "polygon2d";
}
public String getLegacyTypeID() {
return "polygon2d";
}
2011-02-20 01:44:39 +00:00
}