Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

12046 Posts in 1593 Topics- by 597 Members - Latest Member: Cydfewoot

21. May 2013, 04:22:33 pm
Xith3D CommunityGeneral CategoryFeature Requests & Brilliant Ideas (Moderators: Marvin Fröhlich, 'n ddrylliog)TabPanel widget
Pages: [1]
Print
Author Topic: TabPanel widget  (Read 1589 times)
Agrv
Administrator
Enjoying the stay
*****
Offline Offline

Posts: 64


View Profile
« on: 13. June 2009, 04:51:27 pm »

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.

Code:
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();
    }
}
« Last Edit: 13. June 2009, 10:46:43 pm by Agrv » Logged
hawkwind
Getting respectable
***
Offline Offline

Posts: 365



View Profile Email
« Reply #1 on: 13. June 2009, 06:05:30 pm »

cool...I can use this.  I will test and let ya know if theres any problems.
Logged
Marvin Fröhlich
Xith Lord
Administrator
Guru
*****
Offline Offline

Posts: 4403


May the 4th, be with you...


View Profile
« Reply #2 on: 13. June 2009, 11:05:09 pm »

Nice idea. Thanks for sharing. This is Open Source Smiley. I will have a look at it tomorrow.

Marvin

PS: I need your real name to put it as @author into the class. You may want to sent it by PM.
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic