Scroll Area For Inventory Not exactly an area

Hi there!

I’m trying to create a scroll area for my players inventory to show.

Right now my scroll area only appears to be a tiny square on the screen…

Here is the relevant methods

[java]

private void initInventoryWindow(){
invMenu = new ScrollAreaAdapter(screen, “invMenu”, new Vector2f(30, 30), new Vector2f(20, 20));
invMenu.setText(“Inventory”);
screen.addElement(invMenu);
invMenu.hide();
}

private void showInventory(){

if (invAttached){
  invMenu.hide();
  invAttached = false;
  }

else {
  invAttached = true;
  invMenu.show();
  invMenu.getElements().clear();
  
  for (int i = 0; i < player.inventory.size(); i++) {
   
    String a                      = "Button Number " + String.valueOf(i);
    Item          currentItem     = (Item) player.inventory.get(i);     
    InventoryButton currentButton = new InventoryButton(screen, a ,new Vector2f(12,12)) {
      
        
      @Override
      public void onButtonMouseLeftUp(MouseButtonEvent evt, boolean toggled) {
          
        item.equip(player);
        
        }     
      
      };
    
    invMenu.addScrollableChild(currentButton);
    currentButton.item =    currentItem;
    currentButton.setText(currentItem.getName());
    
    }
  
  }

[/java]

As you can see I wanna add a button for each item in my players inventory. >.>

I haven’t been doing well with this at all.

It looks like your setting the scroll area dimensions to 20px x 20px.

Let me know if that’s the issue…

You may want to try the ScrollPanel instead. It is set up to automate reshape, making it much easier to use. if you only need vertical scrolling, call setUseVerticalWrap(true);

Oh… also download and try out the test app… it has a drag/drop inventory example and a point/click inventory example.

@t0neg0d

Okay awesome!

My area now shows up with my button.

What’s the best practice to get these things to line up correctly?

Will it automatically scroll based on how many elements I add or do I have to set this somehow myself?

@BigBob said: @t0neg0d

Okay awesome!

My area now shows up with my button.

What’s the best practice to get these things to line up correctly?

Will it automatically scroll based on how many elements I add or do I have to set this somehow myself?

There are now multiple options for aligning things easily… My personal preference is to use the LayoutHelper directly:

Something like:

[java]
// Reset the layout helper
LayoutHelper.reset();

// Add the margins
LayoutHelper.advanceX(margins.x);
LayoutHelper.advanceY(margins.x);

// Loop through to set the new Y position of each button
for (ButtonAdapter ba : someListOfButtonAdapters) {
// Call position() to set the Vector2f of the ButtonAdapter to the current position of the LayoutHelper
ba.setPosition(LayoutHelper.position());
// Let the LAyoutHelper advance the Y position the height of the current ButtonAdapter
LayoutHelper.advanceY(ba);
}

// Call reshape on the ScrollPanel (if you are using that… which I would suggest)
// This is only necessary if you are not setting the content position when you add it
sp.reshape();

// Loop through a second time to flip the Y since this is OpenGL
// After they will be ordered correctly
for (ButtonAdapter ba : someListOfButtonAdapters) {
ba.setPosition(sp.getHeight()-ba.getY());
}
[/java]

The other option now, is to set the ScrollPanel’s layout and add LayoutHints to each controls. Using the flow layout and setting the LayoutHint “wrap” on each would have the same effect as above. This would look something like:

[java]
FlowLayout layout = new FlowLayout(“margins 10 10 10 10”);
sp.getScrollableArea().setLayout(layout);

for (ButtonAdapter ba : someListOfButtonAdapters) {
ba.getLayoutHints().define(“wrap”);
sp.addScrollableContent(ba);
}

sp.getScrollAbleArea().layoutChildren();
sp.reshape();
[/java]

Oh… the above use of layouts, is probably going to have padding in between each component. So you may want the Layout define like:

[java]
FlowLayout layout = new FlowLayout(“margins 10 10 10 10″,“pad 0 0 0 0”);
[/java]

1 Like

@t0neg0d

Im attempting to use a scroll area here. Perhaps I’m using the wrong thing?

I’m essentially trying to get a list of buttons.

@BigBob
Let me throw together an example using ScrollPanel and a Layout… gimme a few minutes.

Here is really qick example of using ScrollPanel and FlowLayout… however, do remember I haven’t added grow and fill to the layout yet, so you have to resize according to the scrollSize (scrollbar)

[java]
ScrollPanel sp = new ScrollPanel(screen,
Vector2f.ZERO,
new Vector2f(150,300)
);
FlowLayout layout = new FlowLayout(screen,“margins 0 0 0 0”,“pad 0 0 0 0”);
sp.getScrollableArea().setLayout(layout);

	List<ButtonAdapter> list = new ArrayList();
	for (int i = 0; i < 20; i++) {
		ButtonAdapter ba = new ButtonAdapter(screen, Vector2f.ZERO, new Vector2f(150-sp.getScrollSize(), 30));
		ba.getLayoutHints().define("wrap","fill true false","grow true false");
		ba.setText("Button " + i);
		list.add(ba);
	}
	
	for (ButtonAdapter ba : list) {
		sp.addScrollableContent(ba);
	}
	
	sp.getScrollableArea().layoutChildren();
	sp.reshape();
	
	screen.addElement(sp);

[/java]

1 Like

Wowza!

Okay it’s working!

Last thing I’d like to do is make it scroll sideways instead of up and down if possible.

But this is very close to what Im needing at this point and I can at least continue on with testing

@BigBob said: Wowza!

Okay it’s working!

Last thing I’d like to do is make it scroll sideways instead of up and down if possible.

But this is very close to what Im needing at this point and I can at least continue on with testing

The only hold up on this is I need to finish Horizontal Layout… and add the option to NOT wrap. For the time being, use the non-layout version above and just keep calling advanceY on LayoutHelper.

ScrollPanel has both V & H scrolling… currently, You can disable Horizontal, but not Vertical. Just make sure that you content sizes correctly to getScrollBoundsHeight() and the Vertical scrolling should never show up.

@t0neg0d

that’s okay, then I have to ask one more question!

I currently have my button’s width set to half the width the on the inventory menu!

But when it adds the scroll, it covers up one of the buttons every so slightly, as the scroll bar adds to the width of the panel.

My super awesome question is, how do I set the width of my buttons to just width of the window area of the scroll area? Not the window and the bar combined

Thanks for reading!

@BigBob said: @t0neg0d

that’s okay, then I have to ask one more question!

I currently have my button’s width set to half the width the on the inventory menu!

But when it adds the scroll, it covers up one of the buttons every so slightly, as the scroll bar adds to the width of the panel.

My super awesome question is, how do I set the width of my buttons to just width of the window area of the scroll area? Not the window and the bar combined

Thanks for reading!

This should be automatic… that’s not saying it is… but, eventually… (within a week or so) I’ll have flow, horizontal and vertical more complete.

For now… after calling reshape()… loop through the elementChildren of getScrollableArea() and adjust there width (or height) to the getScrollBoundsWidth() or getScrollBoundsHeight(). Like so:

This is for setting the x position and height:
[java]
sp.reshape();

Element lastEl = null;
for (Element el : sp.getScrollableArea().getElements()) {
if (lastEl != null)
el.setX(lastEl.getX()+lastEl.getWidth());
el.setHeight(sp.getScrollBoundsHeight());
lastEl = el;
}
[/java]

However, this may not adjust the button’s label correctly…

Needless to say, this will be solved with an update that will be released soon.