Hello,
I created a progressbar custom control like in the tutorial. It works.
Then I created another custom control, which is a panel which has two progressbars inside. The IDs of the progressbars are clearly different (hpbar and energybar). Inside my controller code I get references to both progressbars.
Now comes a very strange behavior: When I set the progress of either bar, it ALWAYS affects only one of them, the other is being completely ignored. When I comment out the first bar in the xml code, the other left over bar starts getting affected.
XML:
[xml]
<controlDefinition name="monsterinfo" controller="gui.MonsterinfoControl">
<panel id="mainpanel" backgroundColor="#00000000" childLayout="horizontal">
<!-- stuff -->
<control id="datatabs" name="nifty-tabs" height="288px" width="197px" buttonWidth="55px">
<!-- stuff -->
<control id="statstab" name="nifty-tab" caption="Stats" backgroundColor="#FFFFFFFF" childLayout="vertical">
<!-- stuff -->
<panel backgroundColor="#00000000" width="100%" childLayout="horizontal">
<text id="hptext" style="base-font" color="#000000FF" text="HP: "/>
<control id="hpbar" name="progressbar" barImage="Interface/ui/hpbar.png" height="20px" width="140px" />
</panel>
<panel id="energypanel" backgroundColor="#00000000" width="100%" childLayout="horizontal">
<text id="mptext" style="base-font" color="#000000FF" text="EP: "/>
<control id="energybar" name="progressbar" barImage="Interface/ui/mpbar.png" height="20px" width="140px" />
</panel>
<!-- stuff -->
</control>
</control>
</panel>
</controlDefinition>
[/xml]
Java:
[java]
m_mpbar = screen.findNiftyControl("energybar", Progressbar.class);
m_hpbar = screen.findNiftyControl("hpbar", Progressbar.class);
//few lines later
m_hpbar.setProgress(0.4f);
m_mpbar.setProgress(0.7f);
[/java]
The above Java code only affects the hpbar, when all bars are inside the control. If I comment out the hp bar in the control definition, the mpbar is suddenly affected by the m_mpbar.setProgress() call.
Can anyone explain this?
@myamo said:
Now comes a very strange behavior: When I set the progress of either bar, it ALWAYS affects only one of them, the other is being completely ignored. When I comment out the first bar in the xml code, the other left over bar starts getting affected.
I'm not sure I understand why that is weird. If you reference only 1 bar, why would the other be affected by it?
I have two bars, and no matter on which one I call the setProgress method, only one is being affected, that is strange.
It’s like having two listboxes and then adding items to listbox B, but listbox A shows them.
Nothing obvious jumps at me.
I can’t relate to that as I’ve got only 1 progress bar in my game and it’s used at galaxy creation, but the only thing I can suggest is to check the value of hpbar/mpbar when you debug and see if they point to the same object.
There might be clues in the debugger.
Well, if your “progressbar” code is exactly like the one in the tutorial which is called “loadingbar”, then perhaps this is the problem:
[java]@Override
public void bind(Nifty nifty, Screen screen) {
progressBarElement = nifty.getScreen(“loadlevel”).findElementByName(“progressbar”);
}[/java]
This line assumes there is only one element in the screen called “progressbar” (so it grabs the first one), but if you use the exact code it would mean your Progressbar is also the screen controller, which it doesn’t seem to be here, so I think perhaps you might have a similar line somewhere in your progress bar code (which I assume is actually changed from the one in the tutorials).
In a control, you should really get elements relative to the control root element and preferably use names with a “#” in front.
Here’s my own progress bar.
It doesn’t come from the tutorial, but from Nifty’s site where Void made the example. It’s tweaked a bit because mine resets to zero when one part of the galaxy creation has finished and does the same thing for each part until the whole game world has been created.
Notice that it’s not a ScreenController but a Controller.
Anyway. Check it out, maybe it’ll help you.
[java]
package com.madjack.games.disenthral.gui.nifty;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.controls.Controller;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.elements.render.TextRenderer;
import de.lessvoid.nifty.input.NiftyInputEvent;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.tools.SizeValue;
import de.lessvoid.xml.xpp3.Attributes;
import java.util.Properties;
/**
*
-
@author Dany
*/
public class ProgBarControl implements Controller {
private Element progressBarElement;
private Element progressTextElement;
private float progress;
@Override
public void bind(Nifty nifty, Screen screen, Element elmnt, Properties prprts, Attributes atrbts) {
this.progressBarElement = elmnt.findElementByName("progress");
this.progressTextElement = elmnt.findElementByName("progressText");
}
@Override
public void onStartScreen() {}
@Override
public void onFocus(final boolean getFocus) {}
@Override
public boolean inputEvent(final NiftyInputEvent inputEvent) {
return false;
}
public void setProgress(final float progressValue) {
if (progressValue == -1)
resetProgress();
this.progress = progressValue;
if (this.progress < 0.0f) {
this.progress = 0.0f;
} else if (this.progress > 1.0f) {
this.progress = 1.0f;
}
final int MIN_WIDTH = 32;
int pixelWidth = (int)(MIN_WIDTH + (this.progressBarElement.getParent().getWidth() - MIN_WIDTH) * this.progress);
this.progressBarElement.setConstraintWidth(new SizeValue(pixelWidth + "px"));
this.progressBarElement.getParent().layoutElements();
String progressText = String.format("%3.0f%%", this.progress * 100);
this.progressTextElement.getRenderer(TextRenderer.class).setText(progressText);
}
public void resetProgress() {
final int MIN_WIDTH = 32;
this.progressBarElement.setConstraintWidth(new SizeValue(MIN_WIDTH + "px"));
this.progressBarElement.getParent().layoutElements();
}
@Override
public void init(Properties prprts, Attributes atrbts) {
}
}
[/java]
@Tumaini said:
Well, if your "progressbar" code is exactly like the one in the tutorial which is called "loadingbar", then perhaps this is the problem:
[java]@Override
public void bind(Nifty nifty, Screen screen) {
progressBarElement = nifty.getScreen("loadlevel").findElementByName("progressbar");
}[/java]
This line assumes there is only one element in the screen called "progressbar" (so it grabs the first one), but if you use the exact code it would mean your Progressbar is also the screen controller, which it doesn't seem to be here, so I think perhaps you might have a similar line somewhere in your progress bar code (which I assume is actually changed from the one in the tutorials).
In a control, you should really get elements relative to the control root element and preferably use names with a "#" in front.
Yes, I think that's the problem. Both bar images have the same ID.
I had to do element.findElementByName instead of screen.findElementByName.