Here’s the version with those lines commented out and setting the preferred size so that the progress bar doesn’t end up as 0,0,0 in size:
[java]
/*
- $Id$
-
- Copyright © 2013-2013 jMonkeyEngine
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions are
- met:
-
-
- Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
-
-
- Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
-
-
- Neither the name of ‘jMonkeyEngine’ nor the names of its contributors
- may be used to endorse or promote products derived from this software
- without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
- TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
- CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package gems;
import com.jme3.app.SimpleApplication;
import com.jme3.app.StatsAppState;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.simsilica.lemur.Axis;
import com.simsilica.lemur.Container;
import com.simsilica.lemur.FillMode;
import com.simsilica.lemur.GuiGlobals;
import com.simsilica.lemur.Label;
import com.simsilica.lemur.Panel;
import com.simsilica.lemur.ProgressBar;
import com.simsilica.lemur.component.DynamicInsetsComponent;
import com.simsilica.lemur.component.QuadBackgroundComponent;
import com.simsilica.lemur.component.SpringGridLayout;
/**
-
The main application for demonstrating some Lemur layout
-
tricks.
-
-
@author pspeed
*/
public class LayoutDemo extends SimpleApplication {
private float percent = 0.5f;
private float delta = 0.1f;
private Label valueDisplay;
private Panel progressA;
private Panel progressB;
private ProgressBar progressC;
public static void main(String[] args) {
LayoutDemo app = new LayoutDemo();
app.start();
}
public LayoutDemo() {
super(new StatsAppState());
}
@Override
public void simpleInitApp() {
GuiGlobals.initialize(this);
valueDisplay = new Label("Value:" + (int)(percent * 100) + "%");
valueDisplay.setLocalTranslation(0, cam.getHeight(), 0);
guiNode.attachChild(valueDisplay);
// We'll make one root container to hold all of our progress
// bars and their labels. This will be a spring grid layout
// in row-major form. If we add children without specifying contraints
// then each one will get a new row.
Container root = new Container();
root.setBackground(new QuadBackgroundComponent(new ColorRGBA(0, 0, 0.3f, 1), 5, 5));
guiNode.attachChild(root);
/*
// First we’ll fool the layout into keeping our progress bar
// constrained but still letting us change its x size manually.
// By using FillMode.None for the x-axis, components will be laid
// out purely by their own preferred size.
Container first = new Container(new SpringGridLayout(Axis.Y, Axis.X, FillMode.Even, FillMode.None));
first.setBackground(new QuadBackgroundComponent(ColorRGBA.Red, 2, 2));
progressA = first.addChild(new Panel(0, 0, ColorRGBA.Green));
// Containers will resize themselves to preferred size when
// first initialized... even if a size has been set. Here we
// override the container's preferred size with our own and
// it will never recalculate it until cleared.
first.setPreferredSize(new Vector3f(204, 20, 0));
root.addChild(new Label("Test 1, Spring Layout"));
root.addChild(first);
// The problem with Test 1 is that even though the y-axis
// is constrained, it is up to us to make sure we don't set the
// x-size bigger than the container will hold.
//
// Also notice above this included having to keep track of our
// background margin in our sizing and so on. (204 container size
// versus 200)
// In this next approach, we will use dynamic insets to make the
// SpringGridLayout let us have a different sized child without
// ever violating the layout constraints. Other than that, it's
// really no different, though. Width still has to be calculated
// relative to the parent and so on... the only difference is that
// if we get it too big then the container will grow to fit.
Container second = new Container();
second.setBackground(new QuadBackgroundComponent(ColorRGBA.Red, 2, 2));
progressB = second.addChild(new Panel(0, 16, ColorRGBA.Yellow));
progressB.setInsetsComponent(new DynamicInsetsComponent(0, 0, 0, 1.0f));
// Here we let the "second" container pick up the size from the parent layout.
root.addChild(new Label("Test 2, Dynamic Insets"));
root.addChild(second);
root.addChild(new Label("Test 3, Built In"));
*/
progressC = root.addChild(new ProgressBar());
progressC.setPreferredSize(new Vector3f(200, 20, 0));
// Set the location of the root container based on its size
Vector3f size = root.getPreferredSize();
root.setLocalTranslation((int)(cam.getWidth() * 0.5f - size.x * 0.5f),
(int)(cam.getHeight() * 0.5f + size.y * 0.5f),
0);
}
@Override
public void simpleUpdate(float tpf) {
percent += delta * tpf;
if( percent >= 1.0 ) {
percent = 1.0f;
delta *= -1;
progressC.setMessage("Down...");
} else if( percent <= 0.0 ) {
percent = 0.0f;
delta *= -1;
progressC.setMessage("Up...");
}
valueDisplay.setText("Value:" + (int)(percent * 100) + "%");
//progressA.setPreferredSize(new Vector3f(percent * 200, 20, 0));
//progressB.setPreferredSize(new Vector3f(percent * 200, 16, 0));
progressC.setProgressPercent(percent);
}
@Override
public void simpleRender(RenderManager rm) {
}
}
[/java]
Sorry you were having so much trouble with this. It really shouldn’t have been this difficult. :-/
If you roll your own GUI you might still consider using the picking stuff from Lemur. It can be used without the other stuff.