2
\$\begingroup\$

I've been trying to create a customized UI using scene2d library in libGDX. However I am having troubles as I'm trying to set a custom indent spacing on my Tree object.

I have tried to search for the answer but haven't actually found any discussions about this on the internet.

Here is how the tree looks now

This is the current situation which I am having.

This is what it should look

This is how I would prefer it to look like.

All I want is that I could be able to change the size of the margin which is caused by nodes being on different levels. How can I achieve this?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

As the nodes are just Actors, you could place you node content (the labels and sliders) first in some container like Table or Container and give the actual content a bit of margin in that wrapper container.

After you explanation about what you want to achieve the above won't work as you want to move the plus and minus buttons that are used to expand and collapse the Tree's Tree.Nodes.

I don't think the Scene2D Tree implementation will allow you to set the indentation level to be anything other than the max of the width of the plus and the minus Drawables. But there are always ways around that. The bad news is that the field you need to alter is a private field so simply inheriting Tree won't allow you to fix it, but if you're willing to get a bit dirty you can achieve what you're looking for using some reflection.

Instead of doing something like this

Tree tree = new Tree(skin);

// Add nodes to tree here

You could do this;

// First create an instance of Tree where the setStyle method is overridden
tree = new Tree(skin) {
    @Override
    public void setStyle(TreeStyle style) {
        super.setStyle(style);

        // After having called the base class's setStyle, 
        // use horrible reflection to find and alter the indentSpacing field.
        try {
            Field field = Tree.class.getDeclaredField("indentSpacing");
            field.setAccessible(true);
            field.set(this, 8); // This is how much you want each plus and minus indented.
                                // Normally this is set to something like
                                //     Math.max(style.plus.getMinWidth(), style.minus.getMinWidth())
            System.out.println(field);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
};

// You will likely want to play around with this to make sure the node
// content is not rendered too close to the plus/minus    
tree.setIconSpacing(4, 4);

// Add tree nodes here!

This is obviously not ideal, but will likely be easier than creating a new version of Tree and get the pull request accepted. Only good thing about this approach is that it involves almost no typing for you.

Alternatively you can just copy the Tree code and change it accordingly.

\$\endgroup\$
5
  • \$\begingroup\$ Thanks for your answer. I've done some testing but it seems like this is a little bit trickier problem. I think the Tree creates it's own containers for every node, so it's impossible to "reach out" those boundaries without accessing and changing the way Tree creates and sets those node boundaries. \$\endgroup\$
    – Zigur
    Dec 14, 2015 at 16:47
  • \$\begingroup\$ @Zigur Ah, you want to indent not just the content of the node, but the plus and minus buttons as well? \$\endgroup\$
    – bornander
    Dec 14, 2015 at 18:55
  • \$\begingroup\$ I think so yes. I did some photoshopping and updated the question to include a visual example of what it should look like. \$\endgroup\$
    – Zigur
    Dec 14, 2015 at 20:43
  • 1
    \$\begingroup\$ @Zigur Right, I've added a horrible thing that will achieve the screenshot you've added. Don't say I didn't warn you though :) \$\endgroup\$
    – bornander
    Dec 14, 2015 at 22:50
  • 1
    \$\begingroup\$ Thanks, it does exactly what I was going for! Guess this is the "cleanest" solution available at the moment. :) \$\endgroup\$
    – Zigur
    Dec 15, 2015 at 6:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .