Search in sources :

Example 1 with CirclePicker

use of org.terasology.world.viewer.picker.CirclePicker in project Terasology by MovingBlocks.

the class TreeFacetLayer method getWorldText.

@Override
public String getWorldText(Region region, int wx, int wy) {
    TreeFacet treeFacet = region.getFacet(TreeFacet.class);
    Region3i worldRegion = treeFacet.getWorldRegion();
    Region3i relativeRegion = treeFacet.getRelativeRegion();
    int rx = wx - worldRegion.minX() + relativeRegion.minX();
    int rz = wy - worldRegion.minZ() + relativeRegion.minZ();
    Vector2f relCursor = new Vector2f(rx, rz);
    CirclePicker<TreeGenerator> picker = new CirclePickerAll<>(relCursor, radiusFunc);
    for (Entry<BaseVector3i, TreeGenerator> entry : treeFacet.getRelativeEntries().entrySet()) {
        TreeGenerator treeGen = entry.getValue();
        BaseVector3i treePos = entry.getKey();
        picker.offer(treePos.getX(), treePos.getZ(), treeGen);
    }
    Set<TreeGenerator> picked = picker.getAll();
    // try to exit early first
    if (picked.isEmpty()) {
        return null;
    }
    if (picked.size() == 1) {
        TreeGenerator first = picked.iterator().next();
        return labelFunc.apply(first);
    }
    // convert to a stream of labels
    Stream<String> labels = picked.stream().map(labelFunc);
    // collect identical String elements and collect the count in a map
    Map<String, Long> counters = labels.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
    // define a mapping from a map entry to a String representation
    // TODO: treat 1x occurrences like above (e.g. Tree instead of 1x Tree)
    Function<Entry<String, Long>, String> toStringFunc = e -> String.format("%dx %s", e.getValue(), e.getKey());
    // apply that mapping and join the Strings with a comma
    return counters.entrySet().stream().map(toStringFunc).collect(Collectors.joining(", "));
}
Also used : Color(java.awt.Color) Region(org.terasology.world.generation.Region) Region3i(org.terasology.math.Region3i) BufferedImage(java.awt.image.BufferedImage) TreeGenerator(org.terasology.core.world.generator.trees.TreeGenerator) Set(java.util.Set) RenderingHints(java.awt.RenderingHints) Vector2f(org.terasology.math.geom.Vector2f) CirclePickerAll(org.terasology.world.viewer.picker.CirclePickerAll) Renders(org.terasology.world.viewer.layers.Renders) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) AbstractFacetLayer(org.terasology.world.viewer.layers.AbstractFacetLayer) Stream(java.util.stream.Stream) TreeFacet(org.terasology.core.world.generator.facets.TreeFacet) BaseVector3i(org.terasology.math.geom.BaseVector3i) Graphics2D(java.awt.Graphics2D) Map(java.util.Map) Entry(java.util.Map.Entry) CirclePicker(org.terasology.world.viewer.picker.CirclePicker) ZOrder(org.terasology.world.viewer.layers.ZOrder) CirclePickerAll(org.terasology.world.viewer.picker.CirclePickerAll) TreeFacet(org.terasology.core.world.generator.facets.TreeFacet) TreeGenerator(org.terasology.core.world.generator.trees.TreeGenerator) Entry(java.util.Map.Entry) Vector2f(org.terasology.math.geom.Vector2f) BaseVector3i(org.terasology.math.geom.BaseVector3i) Region3i(org.terasology.math.Region3i)

Aggregations

Color (java.awt.Color)1 Graphics2D (java.awt.Graphics2D)1 RenderingHints (java.awt.RenderingHints)1 BufferedImage (java.awt.image.BufferedImage)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Set (java.util.Set)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 TreeFacet (org.terasology.core.world.generator.facets.TreeFacet)1 TreeGenerator (org.terasology.core.world.generator.trees.TreeGenerator)1 Region3i (org.terasology.math.Region3i)1 BaseVector3i (org.terasology.math.geom.BaseVector3i)1 Vector2f (org.terasology.math.geom.Vector2f)1 Region (org.terasology.world.generation.Region)1 AbstractFacetLayer (org.terasology.world.viewer.layers.AbstractFacetLayer)1 Renders (org.terasology.world.viewer.layers.Renders)1 ZOrder (org.terasology.world.viewer.layers.ZOrder)1 CirclePicker (org.terasology.world.viewer.picker.CirclePicker)1