Skip to content

Commit

Permalink
All puzzles working on android! Drawing code is totally broken though…
Browse files Browse the repository at this point in the history
… =(. Also need to extend tnoodle-android build to publish to maven.
  • Loading branch information
jfly committed Dec 18, 2013
1 parent 44db21c commit 9c94691
Show file tree
Hide file tree
Showing 53 changed files with 1,047 additions and 203 deletions.

This file was deleted.

8 changes: 2 additions & 6 deletions scrambles/src/net/gnehzr/tnoodle/js/TNoodleJsUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,18 +126,14 @@ public static String getVersion() {
}

public static Element getPuzzleIcon(Puzzle puzzle) {
String filename = "puzzle/" + puzzle.getShortName() + ".png";
String filename = PuzzlePlugins.PUZZLE_PACKAGE + "/" + puzzle.getShortName() + ".png";
if(ScrambleJsEntryPoint.resources.containsKey(filename)) {
Image image = new Image();
image.setUrl("data:image/png;base64," + ScrambleJsEntryPoint.resources.get(filename));
return image.getElement();
}

Dimension size = new Dimension(32, 32);
OMSVGSVGElement svg = createSVG(size.width, size.height);
Graphics2D g2d = new Graphics2D((OMSVGDocument) svg.getOwnerDocument(), svg);
puzzle.drawPuzzleIcon(g2d, size);
return svg.getElement();
return null;
}

private static JSONValue toJSONValue(Object obj) {
Expand Down
76 changes: 38 additions & 38 deletions scrambles/src/net/gnehzr/tnoodle/scrambles/Puzzle.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import static net.gnehzr.tnoodle.utils.GwtSafeUtils.azzert;
import static net.gnehzr.tnoodle.utils.GwtSafeUtils.ceil;
import static net.gnehzr.tnoodle.utils.GwtSafeUtils.toColor;
//<<<import static net.gnehzr.tnoodle.utils.GwtSafeUtils.toColor;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.RenderingHints;
import java.awt.Graphics2D;
import java.awt.geom.GeneralPath;
//<<<import java.awt.Color;
//<<<import java.awt.Dimension;
//<<<import java.awt.RenderingHints;
//<<<import java.awt.Graphics2D;
//<<<import java.awt.geom.GeneralPath;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;
Expand Down Expand Up @@ -107,17 +107,17 @@ public final String generateWcaScramble(Random r) {
* this abstract class will resize appropriately.
* @return The size of the images this Scrambler will produce.
*/
protected abstract Dimension getPreferredSize();
//<<<protected abstract Dimension getPreferredSize();

/**
* @return A *new* HashMap mapping face names to Colors.
*/
public abstract HashMap<String, Color> getDefaultColorScheme();
//<<<public abstract HashMap<String, Color> getDefaultColorScheme();

/**
* @return A HashMap mapping face names to GeneralPaths.
*/
public abstract HashMap<String, GeneralPath> getDefaultFaceBoundaries();
//<<<public abstract HashMap<String, GeneralPath> getDefaultFaceBoundaries();

private String[] generateScrambles(Random r, int count) {
String[] scrambles = new String[count];
Expand All @@ -130,16 +130,17 @@ private String[] generateScrambles(Random r, int count) {
private SecureRandom r = getSecureRandom();
private static final SecureRandom getSecureRandom() {
try {
return SecureRandom.getInstance("SHA1PRNG", "SUN");
try {
return SecureRandom.getInstance("SHA1PRNG", "SUN");
} catch(NoSuchProviderException e) {
l.log(Level.SEVERE, "Couldn't get SecureRandomInstance", e);
return SecureRandom.getInstance("SHA1PRNG");
}
} catch(NoSuchAlgorithmException e) {
l.log(Level.SEVERE, "Couldn't get SecureRandomInstance", e);
azzert(false, e);
return null;
} catch(NoSuchProviderException e) {
l.log(Level.SEVERE, "Couldn't get SecureRandomInstance", e);
azzert(false, e);
return null;
}
}
}

@Export
Expand All @@ -166,6 +167,9 @@ private final String generateSeededScramble(byte[] seed) {
// other threads can access the static one.
// Also, setSeed supplements an existing seed,
// rather than replacing it.
// TODO - consider using something other than SecureRandom for seeded scrambles,
// because we really, really want this to be portable across platforms (desktop java, gwt, and android)
// https://github.com/cubing/tnoodle/issues/146
SecureRandom r = getSecureRandom();
r.setSeed(seed);
return generateWcaScramble(r);
Expand All @@ -188,24 +192,13 @@ public String toString() {
return getLongName();
}

/**
* TODO - comment
*/
public void drawPuzzleIcon(Graphics2D g, Dimension size) {
try {
drawScramble(g, size, "", null);
} catch(InvalidScrambleException e) {
l.log(Level.SEVERE, "", e);
}
}

/**
* Computes the best size to draw the scramble image.
* @param maxWidth The maximum allowed width of the resulting image, 0 if it doesn't matter.
* @param maxHeight The maximum allowed height of the resulting image, 0 if it doesn't matter.
* @return The best size of the resulting image, constrained to maxWidth and maxHeight.
*/
@Export
/*<<<@Export
public Dimension getPreferredSize(int maxWidth, int maxHeight) {
if(maxWidth == 0 && maxHeight == 0) {
return getPreferredSize();
Expand All @@ -220,25 +213,26 @@ public Dimension getPreferredSize(int maxWidth, int maxHeight) {
int resultHeight = Math.min(maxHeight, ceil(maxWidth/ratio));
return new Dimension(resultWidth, resultHeight);
}
*/

/**
* TODO - document! alphabetical
* @return
*/
@Export
public String[] getFaceNames() {
ArrayList<String> faces = new ArrayList<String>(getDefaultColorScheme().keySet());
Collections.sort(faces);
return faces.toArray(new String[faces.size()]);
}
//<<<@Export
//<<<public String[] getFaceNames() {
//<<<ArrayList<String> faces = new ArrayList<String>(getDefaultColorScheme().keySet());
//<<<Collections.sort(faces);
//<<<return faces.toArray(new String[faces.size()]);
//<<<}


/**
* TODO - document!
* @param colorScheme
* @return
*/
public HashMap<String, Color> parseColorScheme(String scheme) {
/*<<<public HashMap<String, Color> parseColorScheme(String scheme) {
HashMap<String, Color> colorScheme = getDefaultColorScheme();
if(scheme != null && !scheme.isEmpty()) {
String[] faces = getFaceNames();
Expand Down Expand Up @@ -268,7 +262,7 @@ public HashMap<String, Color> parseColorScheme(String scheme) {
}
}
return colorScheme;
}
}*/

/**
* Draws scramble onto g.
Expand All @@ -280,7 +274,7 @@ public HashMap<String, Color> parseColorScheme(String scheme) {
* If null, just the defaults are used.
* @throws InvalidScrambleException If scramble is invalid.
*/
public void drawScramble(Graphics2D g, Dimension size, String scramble, HashMap<String, Color> colorScheme) throws InvalidScrambleException {
/*<<<public void drawScramble(Graphics2D g, Dimension size, String scramble, HashMap<String, Color> colorScheme) throws InvalidScrambleException {
if(scramble == null) {
scramble = "";
}
Expand All @@ -293,7 +287,7 @@ public void drawScramble(Graphics2D g, Dimension size, String scramble, HashMap<
PuzzleState state = getSolvedState();
state = state.applyAlgorithm(scramble);
state.drawScramble(g, defaults);
}
}*/


protected String solveIn(PuzzleState ps, int n) {
Expand Down Expand Up @@ -373,6 +367,9 @@ protected String solveIn(PuzzleState ps, int n) {
break outer;
}
}
if(Thread.interrupted()) {//<<<
throw new RuntimeException(new InterruptedException());
}
}

l.log(start.finishedNow("expanded " + ( seenSolved.size() + seenScrambled.size() ) + " nodes"));
Expand Down Expand Up @@ -538,7 +535,7 @@ public PuzzleState applyAlgorithm(String algorithm) throws InvalidScrambleExcept
* @param g The Graphics2D object to draw upon (guaranteed to be big enough for getScrambleSize())
* @param colorScheme A HashMap mapping face names to Colors, must have an entry for every face!
*/
protected abstract void drawScramble(Graphics2D g, HashMap<String, Color> colorScheme);
//<<<protected abstract void drawScramble(Graphics2D g, HashMap<String, Color> colorScheme);


public Puzzle getPuzzle() {
Expand Down Expand Up @@ -627,6 +624,9 @@ public PuzzleStateAndGenerator generateRandomMoves(Random r) {
} catch(InvalidMoveException e) {
azzert(false, e);
}
if(Thread.interrupted()) {//<<<
throw new RuntimeException(new InterruptedException());
}
// If this move is redundant, there is no reason to select that move again in vain.
successors.remove(move);
}
Expand Down
29 changes: 3 additions & 26 deletions scrambles/src/net/gnehzr/tnoodle/scrambles/PuzzleIcon.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,15 @@
import net.gnehzr.tnoodle.utils.GwtSafeUtils;
import java.util.logging.Logger;
import java.util.logging.Level;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.awt.Graphics2D;
import javax.imageio.ImageIO;

/*
* This is not a part of the Puzzle class because gwt
* doesn't like any of this madness.
*/
public class PuzzleIcon {
private static final Logger l = Logger.getLogger(PuzzleIcon.class.getName());

private PuzzleIcon() {}

/**
* TODO - comment
* We should probably assert that the icons are of a particular size.
*/

public static final ByteArrayOutputStream loadPuzzleIcon(Puzzle p) {
public static final ByteArrayOutputStream loadPuzzleIconPng(String shortName) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
InputStream in = p.getClass().getResourceAsStream(p.getShortName() + ".png");
InputStream in = PuzzleIcon.class.getResourceAsStream("/" + PuzzlePlugins.PUZZLE_PACKAGE + "/" + shortName + ".png");
if(in != null) {
try {
GwtSafeUtils.fullyReadInputStream(in, bytes);
Expand All @@ -36,17 +23,7 @@ public static final ByteArrayOutputStream loadPuzzleIcon(Puzzle p) {
l.log(Level.INFO, "", e);
}
}

Dimension dim = new Dimension(32, 32);
BufferedImage img = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = (Graphics2D) img.getGraphics();
p.drawPuzzleIcon(g, dim);
try {
ImageIO.write(img, "png", bytes);
} catch(IOException e) {
l.log(Level.SEVERE, "", e);
}
return bytes;
return null;
}

}
26 changes: 15 additions & 11 deletions scrambles/src/net/gnehzr/tnoodle/scrambles/PuzzleImageInfo.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
package net.gnehzr.tnoodle.scrambles;

import static net.gnehzr.tnoodle.utils.GwtSafeUtils.toHex;
import static net.gnehzr.tnoodle.utils.GwtSafeUtils.toPoints;
//<<<import static net.gnehzr.tnoodle.utils.GwtSafeUtils.toHex;
//<<<import static net.gnehzr.tnoodle.utils.GwtSafeUtils.toPoints;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.geom.GeneralPath;
//<<<import java.awt.Color;
//<<<import java.awt.Dimension;
//<<<import java.awt.geom.GeneralPath;
import java.util.HashMap;

public class PuzzleImageInfo {
public HashMap<String, GeneralPath> faces;
public HashMap<String, Color> colorScheme;
public Dimension size;
//<<<public HashMap<String, GeneralPath> faces;
//<<<public HashMap<String, Color> colorScheme;
//<<<public Dimension size;

public PuzzleImageInfo() {}
public PuzzleImageInfo(Puzzle p) {
faces = p.getDefaultFaceBoundaries();
colorScheme = p.getDefaultColorScheme();
size = p.getPreferredSize();
//<<<faces = p.getDefaultFaceBoundaries();
//<<<colorScheme = p.getDefaultColorScheme();
//<<<size = p.getPreferredSize();
}

public HashMap<String, Object> toJsonable() {
HashMap<String, Object> jsonable = new HashMap<String, Object>();
/*<<< TODO
HashMap<String, Integer> dim = new HashMap<String, Integer>();
dim.put("width", size.width);
dim.put("height", size.height);
Expand All @@ -32,12 +33,15 @@ public HashMap<String, Object> toJsonable() {
jsonColorScheme.put(key, toHex(this.colorScheme.get(key)));
}
jsonable.put("colorScheme", jsonColorScheme);
*/

/*<<< this can go, methinks
HashMap<String, double[][][]> jsonFaces = new HashMap<String, double[][][]>();
for(String key : this.faces.keySet()) {
jsonFaces.put(key, toPoints(this.faces.get(key)));
}
jsonable.put("faces", jsonFaces);
*/
return jsonable;
}
}
6 changes: 3 additions & 3 deletions scrambles/src/net/gnehzr/tnoodle/scrambles/PuzzlePlugins.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@

public class PuzzlePlugins {
private static final Logger l = Logger.getLogger(PuzzlePlugins.class.getName());
public static final String PUZZLE_PACKAGE = "puzzle";

private static Plugins<Puzzle> plugins = null;
static {
try {
plugins = new Plugins<Puzzle>("puzzle", Puzzle.class, Puzzle.class.getClassLoader());
plugins = new Plugins<Puzzle>(PUZZLE_PACKAGE, Puzzle.class, Puzzle.class.getClassLoader());
} catch (IOException e) {
l.log(Level.SEVERE, "", e);
} catch (BadLazyClassDescriptionException e) {
Expand All @@ -48,8 +49,7 @@ public static synchronized SortedMap<String, LazyInstantiator<Puzzle>> getScramb
return Collections.unmodifiableSortedMap(scramblers);
}

public static String getScramblerLongName(String shortName) throws BadLazyClassDescriptionException, IOException {
getScramblers(); // force reloading the plugins, if necessary
public static String getScramblerLongName(String shortName) {
return plugins.getPluginComment(shortName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import static net.gnehzr.tnoodle.utils.GwtSafeUtils.azzert;

import java.awt.Dimension;
import java.awt.image.BufferedImage;
//<<<import java.awt.Dimension;
//<<<import java.awt.image.BufferedImage;
import java.security.SecureRandom;
import java.util.LinkedList;
import java.util.Random;
Expand Down Expand Up @@ -52,7 +52,7 @@ public void run() {
String scramble = puzzle.generateWcaScramble(r);

if(drawScramble) {
// The drawScramble option exists so we can test out generating and drawing
/*<<<// The drawScramble option exists so we can test out generating and drawing
// a bunch of scrambles in 2 threads at the same time. See ScrambleTest.
BufferedImage image = new BufferedImage(10, 10, BufferedImage.TYPE_INT_ARGB);
Dimension size = new Dimension(image.getWidth(), image.getHeight());
Expand All @@ -61,6 +61,7 @@ public void run() {
} catch (InvalidScrambleException e1) {
e1.printStackTrace();
}
*/
}

synchronized(scrambles) {
Expand Down
4 changes: 2 additions & 2 deletions scrambles/src/net/gnehzr/tnoodle/test/ScrambleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,10 @@ private static void testThreads() throws LazyInstantiatorException, InvalidScram
System.out.println("Drawing " + scramble);
BufferedImage image = new BufferedImage(10, 10, BufferedImage.TYPE_INT_ARGB);
Dimension size = new Dimension(image.getWidth(), image.getHeight());
scrambler.drawScramble(image.createGraphics(), size, scramble, null);
//<<<scrambler.drawScramble(image.createGraphics(), size, scramble, null);

// Scramblers should support "null" as the empty scramble
scrambler.drawScramble(image.createGraphics(), size, null, null);
//<<<scrambler.drawScramble(image.createGraphics(), size, null, null);

System.out.println("Generating & drawing 2 sets of " + SCRAMBLE_COUNT + " scrambles simultaneously." +
" This is meant to shake out threading problems in scramblers.");
Expand Down
Loading

0 comments on commit 9c94691

Please sign in to comment.