Skip to content

Commit

Permalink
initial prototype of drag reorder complete, can drag elements in spec…
Browse files Browse the repository at this point in the history
…ial 4th tab
  • Loading branch information
chrisj42 committed Jul 18, 2018
1 parent 8844507 commit 071135a
Show file tree
Hide file tree
Showing 6 changed files with 326 additions and 24 deletions.
16 changes: 15 additions & 1 deletion core/game/src/miniventure/game/world/levelgen/NoiseMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,23 @@
// a data class only, not display.
public class NoiseMapper {

/*
I am going to give noise maps parameters. These parameters will have to be specified whenever the map is used.
There will be 3 possible parameter types:
- Float values (for region sizes)
- Noise Functions (for source functions)
- Tile Fetcher (this means noise maps and/or tiletypes)
There will be a button next to "add region" that reads "add parameter"
clicking it brings up a panel below, similar to the region list panel.
- you can name the parameter
- you can select the parameter type
*/

@NotNull private String name;
// private final ArrayList<>
private NamedNoiseFunction source;
private ArrayList<NoiseMapRegion> regions;
private final ArrayList<NoiseMapRegion> regions;
private float total;

public NoiseMapper(@NotNull String name, @NotNull NamedNoiseFunction source) {
Expand Down
8 changes: 5 additions & 3 deletions gentest/src/miniventure/gentest/NoiseFunctionEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class NoiseFunctionEditor extends MyPanel implements NamedObject, Scrollable {
this.noiseFunction = noiseFunction;

randomSeed = new JCheckBox("Random seed", true);
add(randomSeed);
// add(randomSeed);

seed = new StringField("", 20);
seed.addValueListener(val -> {
Expand All @@ -49,8 +49,8 @@ class NoiseFunctionEditor extends MyPanel implements NamedObject, Scrollable {
@Override public void keyTyped(KeyEvent e) { randomSeed.setSelected(false); }
});

add(new JLabel("Seed:"));
add(seed);
// add(new JLabel("Seed:"));
// add(seed);

coordsPerValue = new IntegerField(noiseFunction.getCoordsPerValue(), 3, 1);
coordsPerValue.addValueListener(noiseFunction::setCoordsPerValue);
Expand Down Expand Up @@ -96,6 +96,8 @@ public void setObjectName(@NotNull String name) {
@Override @NotNull
public String getObjectName() { return noiseFunction.getName(); }

@Override
public String toString() { return getObjectName(); }

@Override
public Dimension getPreferredScrollableViewportSize() {
Expand Down
2 changes: 1 addition & 1 deletion gentest/src/miniventure/gentest/NoiseMapEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ private static void addAccessedNoiseMaps(NoiseMapper map, ArrayList<NoiseMapper>
}

@Override
public String toString() { return getObjectName()+"-Editor"; }
public String toString() { return getObjectName(); }

@Override
public void setObjectName(@NotNull String name) {
Expand Down
40 changes: 21 additions & 19 deletions gentest/src/miniventure/gentest/TestPanel.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package miniventure.gentest;

import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;

Expand All @@ -14,6 +15,7 @@
import miniventure.game.world.levelgen.GroupNoiseMapper;
import miniventure.game.world.levelgen.NamedNoiseFunction;
import miniventure.game.world.levelgen.NoiseMapper;
import miniventure.gentest.util.MyList;
import miniventure.gentest.util.MyPanel;

import static miniventure.game.world.tile.TileType.TileTypeEnum.*;
Expand Down Expand Up @@ -46,7 +48,9 @@ public TestPanel() {


NoiseMapper plainsBiome = new NoiseMapper("Plains", detailNoise)
.addRegion(GRASS, 8)
.addRegion(GRASS, 4)
.addRegion(WATER, 1f)
.addRegion(GRASS, 3)
.addRegion(TREE_CARTOON, 1);

NoiseMapper desertBiome = new NoiseMapper("Desert", cactusNoise)
Expand All @@ -68,17 +72,19 @@ public TestPanel() {

NoiseMapper snowBiome = new NoiseMapper("Snow", biomeNoise)
.addRegion(SNOW, 1)
.addRegion(GRASS, 1)
.addRegion(GRASS, .25f)
.addRegion(WATER, .1f)
.addRegion(SNOW, 1);

NoiseMapper volcanoBiome = new NoiseMapper("Volcano", continentNoise)
/*NoiseMapper volcanoBiome = new NoiseMapper("Volcano", continentNoise)
.addRegion(SAND, 1)
.addRegion(DIRT, 1)
.addRegion(STONE, 3)
.addRegion(TORCH, .5f)
.addRegion(STONE, 3)
.addRegion(DIRT, 1)
.addRegion(SAND, 1);
*/

NoiseMapper marshBiome = new NoiseMapper("Marsh", detailNoise)
.addRegion(WATER, .75f)
Expand All @@ -94,33 +100,20 @@ public TestPanel() {
.addRegion(SAND, 1);



NoiseMapper landTerrain = new GroupNoiseMapper("Land", landNoise)
.addRegion(desertBiome, 4)
.addRegion(plainsBiome, 8)
.addRegion(mountainBiome, 6);


NoiseMapper biomeCategories = new NoiseMapper("Surface Type", continentNoise)
/*NoiseMapper biomeCategories = new NoiseMapper("Surface Type", continentNoise)
.addRegion(oceanBiome, 0.5f)
.addRegion(SAND, 0.05f)
.addRegion(landTerrain, 0.8f)
.addRegion(SAND, 0.05f)
.addRegion(oceanBiome, 2.5f);

NoiseMapper rivers = new NoiseMapper("Rivers", biomeNoise)
.addRegion(biomeCategories, 1)
.addRegion(WATER, .07f)
.addRegion(biomeCategories, 1.1f);

*/

NoiseMapper terrain = new GroupNoiseMapper("Terrain", continentNoise)
.addRegion(oceanBiome, 0.1f)
.addRegion(deepOceanBiome, .1f)
.addRegion(desertBiome, .1f)
.addRegion(plainsBiome, .1f)
.addRegion(mountainBiome, .1f)
.addRegion(volcanoBiome, .1f)
.addRegion(snowBiome, .1f)
.addRegion(marshBiome, .1f);

Expand Down Expand Up @@ -162,7 +155,6 @@ public void componentRemoved(ContainerEvent e) {
tabPane.addTab("Noise Functions", noiseFunctionPanel);
tabPane.addTab("Noise Mappings", noiseMapperPanel);
tabPane.addTab("Generated Map", mapPanel);

tabPane.addChangeListener(e -> {
if(Objects.equals(tabPane.getSelectedComponent(), mapPanel)) {
globalPanel.mapPanel.setVisible(true);
Expand All @@ -180,9 +172,19 @@ public void componentRemoved(ContainerEvent e) {
add(globalPanel);
add(tabPane);

JPanel tp = new JPanel();
tabPane.addTab("Drag", tp);

SwingUtilities.invokeLater(() -> {
// add all the editors for the biomes and noise functions from above

tp.add(new MyList<>(
new NoiseFunctionEditor(this, continentNoise),
new NoiseFunctionEditor(this, biomeNoise),
new NoiseFunctionEditor(this, detailNoise),
new NoiseFunctionEditor(this, cactusNoise)
));

for(NamedNoiseFunction function: terrain.getReferencedFunctions())
noiseFunctionPanel.addElement(new NoiseFunctionEditor(this, function));

Expand Down
140 changes: 140 additions & 0 deletions gentest/src/miniventure/gentest/util/ListItemTransferHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package miniventure.gentest.util;

import javax.swing.DefaultListModel;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.TransferHandler;

import java.awt.Component;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DragSource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Objects;

@SuppressWarnings("serial")
class ListItemTransferHandler extends TransferHandler {
protected final DataFlavor localObjectFlavor;
protected int[] indices;
protected int addIndex = -1; // Location where items were added
protected int addCount; // Number of items added.

@FunctionalInterface
public interface TransferListener {
void orderChanged();
}

private final ArrayList<TransferListener> listeners = new ArrayList<>();

public ListItemTransferHandler() {
super();
// localObjectFlavor = new ActivationDataFlavor(
// Object[].class, DataFlavor.javaJVMLocalObjectMimeType, "Array of items");
localObjectFlavor = new DataFlavor(Object.class, "single item");
}

public void addTransferListener(TransferListener l) { listeners.add(l); }
public void removeTransferListener(TransferListener l) { listeners.remove(l); }

@Override
protected Transferable createTransferable(JComponent c) {
JList<?> source = (JList<?>) c;
c.getRootPane().getGlassPane().setVisible(true);

indices = new int[] {source.getSelectedIndex()};
Object selectedValue = source.getSelectedValue();
// return new DataHandler(transferedObjects, localObjectFlavor.getMimeType());
return new Transferable() {
@Override public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[] {localObjectFlavor};
}
@Override public boolean isDataFlavorSupported(DataFlavor flavor) {
return Objects.equals(localObjectFlavor, flavor);
}
@Override public Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException, IOException {
if (isDataFlavorSupported(flavor)) {
return selectedValue;
} else {
throw new UnsupportedFlavorException(flavor);
}
}
};
}

@Override
public boolean canImport(TransferSupport info) {
return info.isDrop() && info.isDataFlavorSupported(localObjectFlavor);
}

@Override
public int getSourceActions(JComponent c) {
Component glassPane = c.getRootPane().getGlassPane();
glassPane.setCursor(DragSource.DefaultMoveDrop);
return MOVE; // COPY_OR_MOVE;
}

@SuppressWarnings("unchecked")
@Override
public boolean importData(TransferSupport info) {
DropLocation tdl = info.getDropLocation();
if (!canImport(info) || !(tdl instanceof JList.DropLocation)) {
return false;
}

JList.DropLocation dl = (JList.DropLocation) tdl;
JList target = (JList) info.getComponent();
DefaultListModel listModel = (DefaultListModel) target.getModel();
int max = listModel.getSize();
int index = dl.getIndex();
index = index < 0 ? max : index; // If it is out of range, it is appended to the end
index = Math.min(index, max);

addIndex = index;

try {
Object value = info.getTransferable().getTransferData(localObjectFlavor);
listModel.add(index, value);
target.addSelectionInterval(index, index);
addCount = 1;
return true;
} catch (UnsupportedFlavorException | IOException ex) {
ex.printStackTrace();
}

return false;
}

@Override
protected void exportDone(JComponent c, Transferable data, int action) {
c.getRootPane().getGlassPane().setVisible(false);
cleanup(c, action == MOVE);
}

private void cleanup(JComponent c, boolean remove) {
if (remove && Objects.nonNull(indices)) {
if (addCount > 0) {
// https://github.com/aterai/java-swing-tips/blob/master/DragSelectDropReordering/src/java/example/MainPanel.java
for (int i = 0; i < indices.length; i++) {
if (indices[i] >= addIndex) {
indices[i] += addCount;
}
}
}
JList source = (JList) c;
DefaultListModel model = (DefaultListModel) source.getModel();
for (int i = indices.length - 1; i >= 0; i--) {
model.remove(indices[i]);
}
}

indices = null;
addCount = 0;
addIndex = -1;

for(TransferListener l: listeners)
l.orderChanged();
}
}

0 comments on commit 071135a

Please sign in to comment.