Skip to content

Instantly share code, notes, and snippets.

@toolforger
Created May 1, 2013 20:24
Show Gist options
  • Save toolforger/5498070 to your computer and use it in GitHub Desktop.
Save toolforger/5498070 to your computer and use it in GitHub Desktop.
Moved axis-specific operations into the Axis enum. The switch construction inside Axis.java is kind of hilarious, but it does work, so whatever ;-) ... it was the best way I could come up with to move the code to the class (well, enum) that it belongs into.
Index: com/simsilica/lemur/component/SpringGridLayout.java
===================================================================
--- com/simsilica/lemur/component/SpringGridLayout.java (Revision 1008)
+++ com/simsilica/lemur/component/SpringGridLayout.java (Arbeitskopie)
@@ -103,144 +103,6 @@
parent.invalidate();
}
- protected float getMajor( Vector3f v )
- {
- switch( mainAxis )
- {
- case X:
- return v.x;
- case Y:
- return v.y;
- case Z:
- return v.z;
- }
- return 0;
- }
-
- protected float getMinor( Vector3f v )
- {
- switch( minorAxis )
- {
- case X:
- return v.x;
- case Y:
- return v.y;
- case Z:
- return v.z;
- }
- return 0;
- }
-
- protected float getAlternate( Vector3f v )
- {
- switch( altAxis )
- {
- case X:
- return v.x;
- case Y:
- return v.y;
- case Z:
- return v.z;
- }
- return 0;
- }
-
- protected void setMajor( Vector3f v, float f )
- {
- switch( mainAxis )
- {
- case X:
- v.x = f;
- break;
- case Y:
- v.y = f;
- break;
- case Z:
- v.z = f;
- break;
- }
- }
-
- protected void setMinor( Vector3f v, float f )
- {
- switch( minorAxis )
- {
- case X:
- v.x = f;
- break;
- case Y:
- v.y = f;
- break;
- case Z:
- v.z = f;
- break;
- }
- }
-
- protected void setAlternate( Vector3f v, float f )
- {
- switch( altAxis )
- {
- case X:
- v.x = f;
- break;
- case Y:
- v.y = f;
- break;
- case Z:
- v.z = f;
- break;
- }
- }
-
- protected void addMajor( Vector3f v, float f )
- {
- switch( mainAxis )
- {
- case X:
- v.x += f;
- break;
- case Y:
- v.y += f;
- break;
- case Z:
- v.z += f;
- break;
- }
- }
-
- protected void addMinor( Vector3f v, float f )
- {
- switch( minorAxis )
- {
- case X:
- v.x += f;
- break;
- case Y:
- v.y += f;
- break;
- case Z:
- v.z += f;
- break;
- }
- }
-
- protected void addAlternate( Vector3f v, float f )
- {
- switch( altAxis )
- {
- case X:
- v.x += f;
- break;
- case Y:
- v.y += f;
- break;
- case Z:
- v.z += f;
- break;
- }
- }
-
/**
* Recalculates the internal arrays that keep track of the
* preferred sizes for each row and collumn. The preferred
@@ -274,9 +136,9 @@
int col = colEntry.getKey();
Entry e = colEntry.getValue();
Vector3f v = e.child.getControl(GuiControl.class).getPreferredSize();
- rowPrefs[row] = Math.max( rowPrefs[row], getMajor(v) );
- colPrefs[col] = Math.max( colPrefs[col], getMinor(v) );
- maxAlternate = Math.max( getAlternate(v), maxAlternate);
+ rowPrefs[row] = Math.max( rowPrefs[row], mainAxis.get(v) );
+ colPrefs[col] = Math.max( colPrefs[col], minorAxis.get(v) );
+ maxAlternate = Math.max( altAxis.get(v), maxAlternate);
}
}
return maxAlternate;
@@ -289,14 +151,14 @@
lastPreferredSize.set(0,0,0);
for( float f : rowPrefs )
{
- addMajor( lastPreferredSize, f );
+ mainAxis.add(lastPreferredSize, f);
}
for( float f : colPrefs )
{
- addMinor( lastPreferredSize, f );
+ minorAxis.add(lastPreferredSize, f);
}
- addAlternate( lastPreferredSize, maxAlternate );
+ altAxis.add(lastPreferredSize, maxAlternate);
size.addLocal(lastPreferredSize);
}
@@ -343,10 +205,10 @@
// We could keep these arrays around but I think the GC churn
// pales in comparison to the distribute calls if reshape is called a lot.
float[] rowSizes = new float[rowCount];
- distribute( rowSizes, rowPrefs, getMajor(size), getMajor(lastPreferredSize), mainFill, mainAxis );
+ distribute( rowSizes, rowPrefs, mainAxis.get(size), mainAxis.get(lastPreferredSize), mainFill, mainAxis );
float[] colSizes = new float[columnCount];
- distribute( colSizes, colPrefs, getMinor(size), getMinor(lastPreferredSize), minorFill, minorAxis );
+ distribute( colSizes, colPrefs, minorAxis.get(size), minorAxis.get(lastPreferredSize), minorFill, minorAxis );
float[] rowOffsets = new float[rowCount];
float f = 0;
@@ -370,18 +232,18 @@
for( Entry e : r.values() )
{
Vector3f offset = new Vector3f();
- addMajor(offset, rowOffsets[e.row] );
- addMinor(offset, colOffsets[e.col] );
+ mainAxis.add(offset, rowOffsets[e.row] );
+ minorAxis.add(offset, colOffsets[e.col] );
offset.y *= -1;
e.child.setLocalTranslation(pos.add(offset));
Vector3f childSize = size.clone();
- setMajor(childSize, rowSizes[e.row]);
- setMinor(childSize, colSizes[e.col]);
+ mainAxis.set(childSize, rowSizes[e.row]);
+ minorAxis.set(childSize, colSizes[e.col]);
- e.child.getControl(GuiControl.class).setSize(childSize);
- }
- }
+ e.child.getControl(GuiControl.class).setSize(childSize);
+ }
+ }
}
protected Map<Integer, Entry> getRow( int row, boolean create )
@@ -442,7 +304,7 @@
col = num.intValue();
else
throw new IllegalArgumentException( "Extra constraint not recognized:" + o );
- }
+ }
if( row == -1 )
row = rowCount;
if( col == -1 )
Index: com/simsilica/lemur/Axis.java
===================================================================
--- com/simsilica/lemur/Axis.java (Revision 1008)
+++ com/simsilica/lemur/Axis.java (Arbeitskopie)
@@ -34,12 +34,42 @@
package com.simsilica.lemur;
+import com.jme3.math.Vector3f;
+
/**
*
- * @author Paul Speed
+ * @author Paul Speed, Joachim "Toolforger" Durchholz
*/
public enum Axis
{
- X, Y, Z
+ X, Y, Z;
+
+ public float get (Vector3f v) {
+ switch (this) {
+ case X: return v.x;
+ case Y: return v.y;
+ case Z: return v.z;
+ default: throw new RuntimeException ("This can't happen.");
+ }
+ }
+
+ public void set (Vector3f v, float f) {
+ switch (this) {
+ case X: v.x = f; break;
+ case Y: v.y = f; break;
+ case Z: v.z = f; break;
+ default: throw new RuntimeException ("This can't happen.");
+ }
+ }
+
+ public void add (Vector3f v, float f) {
+ switch (this) {
+ case X: v.x += f; break;
+ case Y: v.y += f; break;
+ case Z: v.z += f; break;
+ default: throw new RuntimeException ("This can't happen.");
+ }
+ }
+
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment