Permalink
Browse files

Working on Swing layout algo

  • Loading branch information...
1 parent 02d8165 commit 48a0ff0deeb55e980bfff3e3b1f1e711ae459557 @scooby committed May 18, 2011
View
@@ -50,7 +50,7 @@ public Area(final int sx, final int sy, final int ex, final int ey) {
/**
* @return width * height
*/
- public int area() { return width() * height(); }
+ public int area() { return (ex - sx) * (ey - sy); }
/**
* @param s Scaling factor.
* @return New area with origin and area scaled by s.
@@ -173,17 +173,18 @@ public int ydist(final Area o) {
}
/**
* Creates an area adjacent to this one that is one row or column thick.
- * @param axis 0: creates a column thick area to the right, 1: creates a row area below
+ * @param axis indicates the cardinal direction in which to extend the area.
* @return New area created.
*/
- public Area adjacent(final int axis) {
- switch(axis) {
- case 0:
- return new Area(ex, sy, ex + 1, ey);
- case 1:
- return new Area(sx, ey, ex, ey + 1);
- }
- throw new IllegalArgumentException();
+ public Area adjacent(final cardinal axis) {
+ int vx = axis.v.x.intVal(),
+ vy = axis.v.y.intVal();
+ return new Area(
+ vx > 0 ? ex : sx + vx,
+ vy > 0 ? ey : sy + vy,
+ vx < 0 ? sx : ex + vx,
+ vy < 0 ? sy : ey + vy
+ );
}
/**
* @return new area at origin
View
@@ -8,6 +8,8 @@
@Override
public double doubleVal() { return val; }
@Override
+ public float floatVal() { return val; }
+ @Override
public Num abs() { return val < 0.0f ? new Float(-val) : this; }
@Override
public int sign() {
View
@@ -10,6 +10,8 @@
@Override
public long longVal() { return val; }
@Override
+ public int intVal() { return val; }
+ @Override
public double doubleVal() { return (double) val; }
@Override
public Num abs() { return val < 0 ? new Int(-val) : this; }
View
@@ -2,7 +2,9 @@
public interface Num {
public long longVal();
+ public int intVal();
public double doubleVal();
+ public float floatVal();
public Num abs();
public Num factor(int i);
public Num scale(int s);
View
@@ -101,4 +101,10 @@ public Num scale(int s) {
public static <T extends Num> T mult(T a, Num b) {
return (T) a.mult(b);
}
+ public int intVal() {
+ return (int) longVal();
+ }
+ public float floatVal() {
+ return (float) doubleVal();
+ }
}
View
@@ -2,10 +2,11 @@
import space.Vector;
import space.Num;
+import space.Vector.cardinal;
public interface Drawable<T extends Num> {
Vector<T> getSize();
- Drawable<T> merge(Drawable<T> o);
+ Drawable<T> merge(cardinal c, Drawable<T> o);
boolean isEmpty();
@Override
int hashCode();
View
@@ -11,6 +11,7 @@
import space.Num;
import space.Area;
+import space.Vector.cardinal;
final public class Grid<T extends Num> implements Iterable<Elem<T>>{
final private Area dims;
@@ -107,23 +108,28 @@ private void removeArea(final Area a) {
}
return al.iterator();
}
+ /**
+ * Attempts to merge compatible decorations.
+ */
public void mergeDecorations() {
- for(int axis = 0; axis < 2; axis++)
+ for(cardinal axis : cardinal.values()) {
+ if(axis.ordinal() >= 2)
+ break; // only need north and east.
for(final Entry<Drawable<T>, Set<Area>> da: m.entrySet()) {
final Drawable<T> d = da.getKey();
- if(d == null || d instanceof Content)
+ if(d == null || d instanceof Content || d.isEmpty())
continue;
for(final Area a: da.getValue()) {
- Area aa;
+ Area aa = a;
do {
- aa = a.adjacent(axis);
+ aa = aa.adjacent(axis);
if(aa.width() > 1 || aa.height() > 1)
break;
final Elem<T> e = _get(aa);
final Drawable<T> dd = e.getD();
if(dd == null || dd.isEmpty())
continue;
- final Drawable<T> dm = d.merge(dd);
+ final Drawable<T> dm = d.merge(axis, dd);
if(dm == null)
continue;
aa = Area.coverage(a, aa);
@@ -132,9 +138,12 @@ public void mergeDecorations() {
} while(aa.overlap(dims));
}
}
+ }
}
public void simplify() {
- for(int axis = 0; axis < 2; axis++)
+ for(cardinal axis : cardinal.values()) {
+ if(axis.ordinal() >= 2)
+ break; // only need north and east.
for(final Entry<Drawable<T>, Set<Area>> da: m.entrySet()) {
final Drawable<T> d = da.getKey();
if(d == null || d instanceof Content)
@@ -158,5 +167,6 @@ public void simplify() {
setCells(aa, d);
}
}
+ }
}
}
@@ -1,6 +1,9 @@
package veb.swing;
import java.util.EnumMap;
+import java.util.EnumSet;
+import java.util.Map.Entry;
+import java.util.Set;
import space.Area;
import space.Float;
@@ -36,13 +39,13 @@ public boolean getFilled(corner c) {
return filled.get(c).booleanValue();
return false;
}
- public BorderDecoration(Area setup) {
- super(setup);
+ public BorderDecoration() {
+ super();
this.borders = new EnumMap<cardinal, border>(cardinal.class);
this.filled = new EnumMap<corner, Boolean>(corner.class);
}
- public static BorderDecoration cornerPiece(Area where, corner which, border type) {
- BorderDecoration bd = new BorderDecoration(where);
+ public static BorderDecoration cornerPiece(corner which, border type) {
+ BorderDecoration bd = new BorderDecoration();
bd.borders.put(which.ns(), type);
bd.borders.put(which.ew(), type);
bd.filled.put(which, Boolean.TRUE);
@@ -56,8 +59,8 @@ public static BorderDecoration cornerPiece(Area where, corner which, border type
* @param type type of border.
* @return the decoration
*/
- public static BorderDecoration straightPiece(Area where, cardinal component, border type) {
- BorderDecoration bd = new BorderDecoration(where);
+ public static BorderDecoration straightPiece(cardinal component, border type) {
+ BorderDecoration bd = new BorderDecoration();
bd.borders.put(component.cw90(), type);
bd.borders.put(component.ccw90(), type);
bd.filled.put(component.cw45(), Boolean.TRUE);
@@ -66,17 +69,13 @@ public static BorderDecoration straightPiece(Area where, cardinal component, bor
}
@Override
- public Drawable<Float> merge(Drawable<Float> o) {
+ public Drawable<Float> merge(cardinal c, Drawable<Float> o) {
if(o == null || !(o instanceof BorderDecoration))
return null;
BorderDecoration d = (BorderDecoration) o;
- cardinal c = assignedGrid.canMergeWith(d.assignedGrid);
- if(c == null)
- return null;
- Area ma = Area.coverage(assignedGrid, d.assignedGrid);
if(getBorder(c.cw90()) == d.getBorder(c.cw90())
&& getBorder(c.ccw90()) == d.getBorder(c.ccw90())) {
- BorderDecoration n = new BorderDecoration(ma);
+ BorderDecoration n = new BorderDecoration();
cardinal c180 = c.turn180();
n.borders.put(c, d.getBorder(c));
n.borders.put(c.cw90(), getBorder(c.cw90()));
@@ -92,11 +91,10 @@ public static BorderDecoration straightPiece(Area where, cardinal component, bor
}
@Override
- public cardinal perimetersTo(SwingDecoration other) {
+ public cardinal perimetersTo(cardinal c, SwingDecoration other) {
if(other == null || !(other instanceof BorderDecoration))
return null;
BorderDecoration d = (BorderDecoration) other;
- cardinal c = assignedGrid.canMergeWith(d.assignedGrid);
cardinal c180 = c.turn180();
if(getBorder(c) != border.none && d.getBorder(c180) != border.none) {
if(getFilled(c.cw45()))
@@ -126,4 +124,13 @@ public boolean isEmpty() {
// TODO Auto-generated method stub
return borderThickness;
}
+ @Override
+ public Set<cardinal> connections() {
+ EnumSet<cardinal> cs = EnumSet.noneOf(cardinal.class);
+ for(Entry<cardinal, border> e : borders.entrySet()) {
+ if(e.getValue() != border.none)
+ cs.add(e.getKey());
+ }
+ return cs;
+ }
}
@@ -1,33 +1,30 @@
package veb.swing;
-import space.Area;
+import java.util.Collections;
+import java.util.Set;
+
import space.Float;
import space.Vector;
import space.Vector.cardinal;
import veb.Drawable;
public class EmptyDecoration extends SwingDecoration {
- public EmptyDecoration(Area setup) {
- super(setup);
+ public EmptyDecoration() {
+ super();
}
@Override
- public Drawable<Float> merge(Drawable<Float> o) {
+ public Drawable<Float> merge(cardinal c, Drawable<Float> o) {
if(o != null && o instanceof EmptyDecoration) {
- EmptyDecoration ed = (EmptyDecoration) o;
- if(assignedGrid.canMergeWith(ed.assignedGrid) != null)
- return new EmptyDecoration(Area.coverage(assignedGrid, ed.assignedGrid));
+ //EmptyDecoration ed = (EmptyDecoration) o;
+ //if(assignedGrid.canMergeWith(ed.assignedGrid) != null)
+ return this;
}
return null;
}
@Override
- public cardinal perimetersTo(SwingDecoration other) {
- return null;
- }
-
- @Override
public boolean isEmpty() {
return true;
}
@@ -39,4 +36,16 @@ public boolean isEmpty() {
return marginSize;
}
+ @Override
+ public Set<cardinal> connections() {
+ // TODO Auto-generated method stub
+ return Collections.emptySet();
+ }
+
+ @Override
+ public cardinal perimetersTo(cardinal c, SwingDecoration other) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
}
@@ -1,25 +1,52 @@
package veb.swing;
+import java.util.Set;
+
import space.Float;
-import space.Area;
import space.Vector;
import space.Vector.cardinal;
import veb.Drawable;
public abstract class SwingDecoration implements Drawable<Float> {
protected Vector<Float> assignedOrigin;
protected Vector<Float> assignedExtent;
- protected Area assignedGrid;
- protected SwingDecoration[] connections;
+ //protected Area assignedGrid;
- public SwingDecoration(Area setup) {
- assignedGrid = setup;
+ public SwingDecoration() {
+ //assignedGrid = null;
assignedOrigin = null;
assignedExtent = null;
}
- @Override
- abstract public Drawable<Float> merge(Drawable<Float> o);
+ /**
+ * @return the assignedOrigin
+ */
+ public Vector<Float> getAssignedOrigin() {
+ return assignedOrigin;
+ }
+
+ /**
+ * @param assignedOrigin the assignedOrigin to set
+ */
+ public void setAssignedOrigin(Vector<Float> assignedOrigin) {
+ this.assignedOrigin = assignedOrigin;
+ }
+
+ /**
+ * @return the assignedExtent
+ */
+ public Vector<Float> getAssignedExtent() {
+ return assignedExtent;
+ }
+
+ /**
+ * @param assignedExtent the assignedExtent to set
+ */
+ public void setAssignedExtent(Vector<Float> assignedExtent) {
+ this.assignedExtent = assignedExtent;
+ }
- abstract public cardinal perimetersTo(SwingDecoration other);
+ public abstract Set<cardinal> connections();
+
+ public abstract cardinal perimetersTo(cardinal c, SwingDecoration other);
}
@@ -20,7 +20,7 @@ public SwingSurface() {
decMap = new HashMap<Area, SwingDecoration>();
}
@Override
- public void draw(final Vector<Float> start, final Vector<Float> extent, final Area where, final Drawable<Float> dec) {
+ public void draw(Vector<Float> start, Vector<Float> extent, Area where, Drawable<Float> dec) {
// look up all possible connections
// hook into existing pieces in the map

0 comments on commit 48a0ff0

Please sign in to comment.