Hello,
I needed tabs in my HUD so I have developed a TabPanel widget. I think it is worth sharing so here is the code. It probably needs some modifications for theming though. I have attached the three textures used.
import java.util.HashMap;
import org.xith3d.ui.hud.base.AbstractButton;
import org.xith3d.ui.hud.listeners.ButtonListener;
import org.xith3d.ui.hud.widgets.Image;
import org.xith3d.ui.hud.widgets.Panel;
import org.xith3d.ui.hud.widgets.ToggleButton;
public class TabPanel extends Panel implements ButtonListener
{
private HashMap<String, Tab> tabs;
private Tab currentTab;
private float buttonsWidth;
private Image line;
private static class Tab
{
private Panel panel;
private ToggleButton button;
public Tab(String name, Panel p)
{
panel = p;
button = new ToggleButton(name.length() * 10f, 25f, name);
button.setTextureNormal("hud/tab_normal.png");
button.setTextureHovered("hud/tab_normal.png");
button.setTexturePressed("hud/tab_pressed.png");
}
}
public TabPanel(float width, float height)
{
super(width, height);
tabs = new HashMap<String, Tab>();
buttonsWidth = 0f;
currentTab = null;
line = new Image(width, 25f, "hud/tab_line.png");
addWidget(line, 0f, 1f);
}
public void addTab(String name, Panel panel)
{
if(tabs.get(name) != null)
throw new RuntimeException("Tab with name " + name + " already exists.");
Tab t = new Tab(name, panel);
t.button.addButtonListener(this);
tabs.put(name, t);
addWidget(t.button, buttonsWidth, 0f);
buttonsWidth += t.button.getWidth();
removeWidget(line);
addWidget(line, buttonsWidth, 1f);
if(currentTab == null)
updateTab(t);
}
public void onButtonClicked(AbstractButton button, Object userObject)
{
for(Tab t : tabs.values())
if(button == t.button)
{
updateTab(t);
break;
}
}
private void updateTab(Tab t)
{
if(currentTab != t)
{
if(currentTab != null)
{
currentTab.button.setToggled(false);
removeWidget(currentTab.panel);
}
currentTab = t;
addWidget(currentTab.panel, 0f, 40f);
}
currentTab.button.setToggled(true);
update();
}
}