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

159 lines
5.0 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.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-02-20 01:44:39 +00:00
import com.sk89q.worldedit.Vector;
2011-09-24 17:45:03 +00:00
import com.sk89q.worldedit.cui.CUIPointBasedRegion;
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
*/
2011-09-24 17:45:03 +00:00
public class Polygonal2DRegionSelector implements RegionSelector, CUIPointBasedRegion {
2011-02-20 01:44:39 +00:00
protected BlockVector pos1;
protected Polygonal2DRegion region = new Polygonal2DRegion();
2011-11-23 01:29:48 +00:00
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();
region = new Polygonal2DRegion();
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 + ".");
2011-09-24 17:45:03 +00:00
session.dispatchCUIEvent(player, new SelectionShapeEvent(getTypeId()));
session.dispatchCUIEvent(player, new SelectionPoint2DEvent(0, pos, getArea()));
session.dispatchCUIEvent(player, new SelectionMinMaxEvent(region.minY, region.maxY));
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.minY, region.maxY));
2011-02-20 01:44:39 +00:00
}
public void explainRegionAdjust(LocalPlayer player, LocalSession session) {
2011-09-24 19:24:10 +00:00
session.dispatchCUIEvent(player, new SelectionMinMaxEvent(region.minY, region.maxY));
}
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;
region = new Polygonal2DRegion();
}
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 String getTypeId() {
return "polygon2d";
}
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
2011-09-24 17:45:03 +00:00
public void describeCUI(LocalPlayer player) {
final List<BlockVector2D> points = region.getPoints();
2011-09-24 17:45:03 +00:00
for (int id = 0; id < points.size(); id++) {
2011-09-24 19:24:10 +00:00
player.dispatchCUIEvent(new SelectionPoint2DEvent(id, points.get(id), getArea()));
2011-09-24 17:45:03 +00:00
}
2011-09-24 19:24:10 +00:00
player.dispatchCUIEvent(new SelectionMinMaxEvent(region.minY, region.maxY));
2011-09-24 17:45:03 +00:00
}
2011-02-20 01:44:39 +00:00
}