Skip to content

Commit

Permalink
Add basic biome color profile selector.
Browse files Browse the repository at this point in the history
  • Loading branch information
crbednarz committed Nov 21, 2013
1 parent 5cc688c commit 1a92ba1
Show file tree
Hide file tree
Showing 8 changed files with 222 additions and 14 deletions.
10 changes: 8 additions & 2 deletions src/amidst/Amidst.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,24 @@
import amidst.gui.VersionSelectWindow;
import amidst.json.InstallInformation;
import amidst.minecraft.Minecraft;
import amidst.preferences.BiomeColorProfile;

public class Amidst {
public final static int version_major = 3;
public final static int version_minor = 4;
public final static String versionOffset = "";
public final static int version_minor = 5;
public final static String versionOffset = " test build";
public static InstallInformation installInformation = new InstallInformation(true);
public static final Gson gson = new Gson();

public static void main(String args[]) {
if (!isOSX()) { Util.setLookAndFeel(); }
Google.startTracking();
Google.track("Run");

// TODO this is a really ugly way of doing this.
Options options = Options.instance;
BiomeColorProfile.scanAndLoad();

new VersionSelectWindow();
}

Expand Down
9 changes: 8 additions & 1 deletion src/amidst/Options.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package amidst;

import amidst.preferences.BiomeColorProfile;
import amidst.preferences.BooleanPrefModel;
import amidst.preferences.FilePrefModel;

Expand All @@ -23,11 +24,13 @@ public enum Options {
public final BooleanPrefModel showTemples, showPlayers, showStrongholds, showVillages, showSpawn;
public final BooleanPrefModel mapFlicking, showFPS;
public final BooleanPrefModel maxZoom;
public BiomeColorProfile biomeColorProfile;

private Options() {
seed = 0L;
seedText = null;


Preferences pref = Preferences.userNodeForPackage(Amidst.class);

jar = new FilePrefModel( pref, "jar", new File(Util.minecraftDirectory, "bin/minecraft.jar"));
Expand All @@ -41,8 +44,12 @@ private Options() {
showTemples = new BooleanPrefModel(pref, "templeIcons", true);
showVillages = new BooleanPrefModel(pref, "villageIcons", true);
showSpawn = new BooleanPrefModel(pref, "spawnIcon", true);

showFPS = new BooleanPrefModel(pref, "showFPS", true);
biomeColorProfile = new BiomeColorProfile();
biomeColorProfile.fillColorArray();



}

public File getJar() {
Expand Down
45 changes: 37 additions & 8 deletions src/amidst/gui/AmidstMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import amidst.map.MapObjectPlayer;
import amidst.map.layers.StrongholdLayer;
import amidst.minecraft.Minecraft;
import amidst.preferences.BiomeColorProfile;
import amidst.resources.ResourceLoader;

import javax.swing.*;
Expand All @@ -21,6 +22,7 @@
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

Expand Down Expand Up @@ -339,16 +341,43 @@ private class OptionsMenu extends JMenu {
private OptionsMenu() {
super("Options");
add(new MapOptionsMenu());
if (BiomeColorProfile.isEnabled)
add(new BiomeColorMenu());

setMnemonic(KeyEvent.VK_M);
add(new JMenuItem("Biome colors") {{
addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//new BiomeColorWindow(window);
}
});
}});
}
private class BiomeColorMenu extends JMenu {
private class BiomeProfileActionListener implements ActionListener {
private BiomeColorProfile profile;
private ArrayList<JCheckBoxMenuItem> profileCheckboxes;
private JCheckBoxMenuItem checkBox;
public BiomeProfileActionListener(BiomeColorProfile profile, JCheckBoxMenuItem checkBox, ArrayList<JCheckBoxMenuItem> profileCheckboxes) {
this.profile = profile;
this.checkBox = checkBox;
this.profileCheckboxes = profileCheckboxes;
}
@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < profileCheckboxes.size(); i++)
profileCheckboxes.get(i).setSelected(false);
checkBox.setSelected(true);
profile.activate();
}
}
private BiomeColorMenu() {
super("Biome profile");
ArrayList<JCheckBoxMenuItem> profileCheckboxes = new ArrayList<JCheckBoxMenuItem>();
for (int i = 0; i < BiomeColorProfile.profiles.size(); i++) {
BiomeColorProfile profile = BiomeColorProfile.profiles.get(i);
JCheckBoxMenuItem menuItem = new JCheckBoxMenuItem(profile.name);
menuItem.addActionListener(new BiomeProfileActionListener(profile, menuItem, profileCheckboxes));
profileCheckboxes.add(
menuItem);
add(menuItem);
}
profileCheckboxes.get(0).setSelected(true);
}

}
private class MapOptionsMenu extends JMenu {
private MapOptionsMenu() {
Expand Down
8 changes: 8 additions & 0 deletions src/amidst/map/Fragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ public void recycle() {
isLoaded = false;
}

public void clearData() {
for (IconLayer layer : iconLayers)
layer.clearMapObjects(this);
isLoaded = false;
for (int i = 0; i < images.length; i++)
images[i].setData(layers[i].getDefaultData()); // TODO: Is this needed?
}

public void clear() {
for (IconLayer layer : iconLayers)
layer.clearMapObjects(this);
Expand Down
5 changes: 4 additions & 1 deletion src/amidst/map/FragmentManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ private void increaseFragmentCache() {
cacheSize <<= 1;
System.gc();
}

public void repaintFragment(Fragment frag) {
frag.clearData();
requestQueue.offer(frag);
}
public Fragment requestFragment(int x, int y) {
if (!running)
return null;
Expand Down
11 changes: 11 additions & 0 deletions src/amidst/map/Map.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import amidst.map.layers.BiomeLayer;

public class Map {
public static Map instance = null;
private static final boolean START = true, END = false;
private static final AffineTransform iMat = new AffineTransform();
private FragmentManager fManager;
Expand Down Expand Up @@ -69,8 +70,18 @@ public Map(Layer[] layers, Layer[] liveLayers, IconLayer[] iconLayers) {
hintMap.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);

renderingHints = new RenderingHints(hintMap);
instance = this;
}

public void resetFragments() {
synchronized (drawLock) {
Fragment frag = startNode;
while (frag.hasNext) {
frag = frag.nextFragment;
fManager.repaintFragment(frag);
}
}
}

public void draw(Graphics2D g) {
AffineTransform originalTransform = g.getTransform();
Expand Down
9 changes: 8 additions & 1 deletion src/amidst/minecraft/Biome.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import amidst.Util;

public class Biome {

public static final HashMap<String,Biome> biomeMap = new HashMap<String,Biome>();
public static final BiomeType typeA = new BiomeType(0.1F, 0.2F);
public static final BiomeType typeB = new BiomeType(-0.5F, 0.0F);
public static final BiomeType typeC = new BiomeType(-1.0F, 0.1F);
Expand Down Expand Up @@ -121,10 +121,17 @@ public Biome(String name, int index, int color, BiomeType type) {
this.index = index;
this.color = color;
this.type = type;
biomeMap.put(name, this);

if (index >= 128)
this.color = Util.lightenColor(color, 40);
}
public static int indexFromName(String name) {
Biome biome = biomeMap.get(name);
if (biome != null)
return biome.index;
return -1;
}

public static final class BiomeType { // TODO: Rename once we figure out what this actually is!
public float value1, value2;
Expand Down
139 changes: 138 additions & 1 deletion src/amidst/preferences/BiomeColorProfile.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,144 @@
package amidst.preferences;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import amidst.Log;
import amidst.Options;
import amidst.Util;
import amidst.json.LauncherProfile;
import amidst.minecraft.Biome;

public class BiomeColorProfile {
public int biomes[] = new int[Biome.biomes.length];
private class BiomeColor {
public int r = 0;
public int g = 0;
public int b = 0;
public BiomeColor(int r, int g, int b) {
this.r = r;
this.g = g;
this.b = b;
}
public BiomeColor(int rgb) {
r = (rgb >> 16) & 0xFF;
g = (rgb >> 8) & 0xFF;
b = (rgb) & 0xFF;
}
public int toColorInt() {
return Util.makeColor(r, g, b);
}
};
public static boolean isEnabled = false;
public static ArrayList<BiomeColorProfile> profiles = new ArrayList<BiomeColorProfile>();

public HashMap<String, BiomeColor> colorMap = new HashMap<String, BiomeColor>();
public int colorArray[] = new int[Biome.length << 1];
public String name;

public BiomeColorProfile() {
name = "default";
for (int i = 0; i < Biome.length; i++) {
colorMap.put(Biome.biomes[i].name, new BiomeColor(Biome.biomes[i].color));
colorMap.put(Biome.biomes[i+128].name, new BiomeColor(Biome.biomes[i+128].color));
}
}

public void fillColorArray() {
for (Map.Entry<String, BiomeColor> pairs : colorMap.entrySet()) {
int index = Biome.indexFromName(pairs.getKey());
if (index >= 128)
index = index - 128 + Biome.length;
if (index != -1)
colorArray[index] = pairs.getValue().toColorInt();
else
Log.i("Failed to find biome for: " + pairs.getKey() + " in profile: " + name);
}
}

public boolean save(File path) {
String output = "";
output += "{\r\n";
output += "\t\"name\"=\"" + name +"\",\r\n";
output += "\t\"colorMap\"=[\r\n";

for (Map.Entry<String, BiomeColor> pairs : colorMap.entrySet()) {
output += "\t\t[\r\n";
output += "\t\t\t\"" + pairs.getKey() +"\",\r\n";
output += "\t\t\t{\r\n";
output += "\t\t\t\t\"r\":" + pairs.getValue().r + ",\r\n";
output += "\t\t\t\t\"g\":" + pairs.getValue().g + ",\r\n";
output += "\t\t\t\t\"b\":" + pairs.getValue().b + "\r\n";
output += "\t\t\t}\r\n";
output += "\t\t],\r\n";
}
output = output.substring(0, output.length() - 3);
output += "\r\n";

output += "\t]\r\n";
output += "}\r\n";
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(path));
writer.write(output);
writer.close();
return true;
} catch ( IOException e) {
try {
if (writer != null)
writer.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
return false;
}

public void activate() {
Options.instance.biomeColorProfile = this;
for (int i = 0; i < Biome.length; i++) {
Biome.biomes[i].color = colorArray[i];
Biome.biomes[i+128].color = colorArray[i + Biome.length];
}
if (amidst.map.Map.instance != null)
amidst.map.Map.instance.resetFragments();
}


public static void scanAndLoad() {
Log.i("Searching for biome color profiles.");
File colorProfileFolder = new File("./biome");

if (!colorProfileFolder.exists() || !colorProfileFolder.isDirectory()) {
Log.i("Unable to find biome color profile folder.");
return;
}

File defaultProfileFile = new File("./biome/default.json");
if (!defaultProfileFile.exists())
if (!Options.instance.biomeColorProfile.save(defaultProfileFile))
Log.i("Attempted to save default biome color profile, but encountered an error.");

File[] colorProfiles = colorProfileFolder.listFiles();
for (int i = 0; i < colorProfiles.length; i++) {
if (colorProfiles[i].exists() && colorProfiles[i].isFile()) {
try {
BiomeColorProfile profile = Util.readObject(colorProfiles[i], BiomeColorProfile.class);
profile.fillColorArray();
profiles.add(profile);
} catch (FileNotFoundException e) {
Log.i("Unable to load file: " + colorProfiles[i]);
}
}
}
isEnabled = true;
}
}

0 comments on commit 1a92ba1

Please sign in to comment.