001package fr.aumgn.dac2.shape.worldedit;
002
003import static fr.aumgn.dac2.shape.worldedit.WEShapeUtils.*;
004
005import java.util.ArrayList;
006import java.util.List;
007
008import com.sk89q.worldedit.BlockVector2D;
009import com.sk89q.worldedit.LocalWorld;
010import com.sk89q.worldedit.regions.CuboidRegionSelector;
011import com.sk89q.worldedit.regions.CylinderRegionSelector;
012import com.sk89q.worldedit.regions.EllipsoidRegionSelector;
013import com.sk89q.worldedit.regions.ExtendingCuboidRegionSelector;
014import com.sk89q.worldedit.regions.Polygonal2DRegionSelector;
015import com.sk89q.worldedit.regions.RegionSelector;
016import com.sk89q.worldedit.regions.SphereRegionSelector;
017
018import fr.aumgn.bukkitutils.geom.Vector;
019import fr.aumgn.bukkitutils.geom.Vector2D;
020import fr.aumgn.dac2.DAC;
021import fr.aumgn.dac2.exceptions.InvalidSelectorForRegion;
022import fr.aumgn.dac2.exceptions.WESelectionNotSupported;
023import fr.aumgn.dac2.shape.CuboidShape;
024import fr.aumgn.dac2.shape.CylinderShape;
025import fr.aumgn.dac2.shape.EllipsoidShape;
026import fr.aumgn.dac2.shape.PolygonalShape;
027import fr.aumgn.dac2.shape.Shape;
028
029public enum WESelector {
030
031    Default {
032        public RegionSelector create(DAC dac, LocalWorld world,
033                Shape shape) {
034            if (shape instanceof CuboidShape) {
035                return WESelector.Cuboid.create(dac, world, shape);
036            } else if (shape instanceof PolygonalShape) {
037                return WESelector.Polygonal.create(dac, world, shape);
038            } else if (shape instanceof CylinderShape) {
039                return WESelector.Cylinder.create(dac, world, shape); 
040            } else if (shape instanceof EllipsoidShape) {
041                return WESelector.Ellipsoid.create(dac, world, shape);
042            } else {
043                throw new WESelectionNotSupported(dac, shape.getClass());
044            }
045        }
046    },
047
048    Cuboid {
049        public CuboidRegionSelector create(DAC dac, LocalWorld world,
050                Shape shape) {
051            if (!(shape instanceof CuboidShape)) {
052                throw new InvalidSelectorForRegion(dac, this, shape.getClass());
053            }
054
055            CuboidShape cuboid = (CuboidShape) shape;
056            return new CuboidRegionSelector(world, bu2we(cuboid.getMin()),
057                    bu2we(cuboid.getMax()));
058        }
059    },
060
061    Extending {
062        public ExtendingCuboidRegionSelector create(DAC dac, LocalWorld world,
063                Shape shape) {
064            if (!(shape instanceof CuboidShape)) {
065                throw new InvalidSelectorForRegion(dac, this, shape.getClass());
066            }
067
068            CuboidShape cuboid = (CuboidShape) shape;
069            return new ExtendingCuboidRegionSelector(world,
070                    bu2we(cuboid.getMin()), bu2we(cuboid.getMax()));
071        }
072    },
073
074    Polygonal {
075        public Polygonal2DRegionSelector create(DAC dac, LocalWorld world,
076                Shape shape) {
077            if (!(shape instanceof PolygonalShape)) {
078                throw new InvalidSelectorForRegion(dac, this, shape.getClass());
079            }
080
081            PolygonalShape poly = (PolygonalShape) shape;
082            List<BlockVector2D> wePoints = new ArrayList<BlockVector2D>();
083            for (Vector2D pt : poly.getPoints()) {
084                wePoints.add(bu2blockwe(pt));
085            }
086            return new Polygonal2DRegionSelector(world, wePoints,
087                    (int) poly.getMinY(), (int) poly.getMaxY());
088        }
089    },
090
091    Cylinder {
092        public CylinderRegionSelector create(DAC dac, LocalWorld world,
093                Shape shape) {
094            if (!(shape instanceof CylinderShape)) {
095                throw new InvalidSelectorForRegion(dac, this, shape.getClass());
096            }
097
098            CylinderShape cyl = (CylinderShape) shape;
099            return new CylinderRegionSelector(world, bu2we(cyl.getCenter()),
100                    bu2we(cyl.getRadius()), (int) cyl.getMinY(),
101                    (int) cyl.getMaxY()); 
102        }
103    },
104
105    Sphere {
106        public SphereRegionSelector create(DAC dac, LocalWorld world,
107                Shape shape) {
108            if (!(shape instanceof EllipsoidShape)) {
109                throw new InvalidSelectorForRegion(dac, this, shape.getClass());
110            }
111
112            EllipsoidShape ellipsoid = (EllipsoidShape) shape;
113            Vector radius = ellipsoid.getRadius();
114            if (radius.getX() != radius.getY()
115                    || radius.getX() != radius.getZ()) {
116                throw new InvalidSelectorForRegion(dac, this, shape.getClass());
117            }
118            return new SphereRegionSelector(world,
119                    bu2we(ellipsoid.getCenter()), radius.getBlockX());
120        }
121    },
122
123    Ellipsoid {
124        public EllipsoidRegionSelector create(DAC dac, LocalWorld world,
125                Shape shape) {
126            if (!(shape instanceof EllipsoidShape)) {
127                throw new InvalidSelectorForRegion(dac, this, shape.getClass());
128            }
129
130            EllipsoidShape ellipsoid = (EllipsoidShape) shape;
131            return new EllipsoidRegionSelector(world,
132                    bu2we(ellipsoid.getCenter()),
133                    bu2we(ellipsoid.getRadius()));
134        }
135    };
136
137    public abstract RegionSelector create(DAC dac, LocalWorld world,
138            Shape shape);
139}