Make actors span width of parent

Anything about development not directly related to libgdx, e.g. OpenGL, Android APIs etc.

Make actors span width of parent

Postby fallia20 » Fri Nov 22, 2019 11:51 am

Hi,

I can't figure out how to make a jrpg-like menu with LibGDX.
I have all the elements I need, a hero picture, a verticalGroup with hero stats and a third verticalGroup which currently only holds a test button.

The problem is that the elemens are really small, I would like for them to spread out across the whole width evenly divided.

https://paste.pics/d124e5a8154423b19b7c37ea8c047732

Code: Select all
package com.mygdx.game.UI;

import com.badlogic.gdx.scenes.scene2d.ui.Window;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Align;
import com.mygdx.game.Entities.Entity;
import com.mygdx.game.Map.Map;

import Utility.Utility;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.ui.HorizontalGroup;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.VerticalGroup;

public class BottomMenu extends Window {
   private static final String TAG = BottomMenu.class.getSimpleName();
   
    private static String title = "";
    private String unknownHeroImageLocation = "sprites/gui/portraits/unknown.png";
   
    private Image heroImage;
   
    //identifier labels
    private Label hpLabel;
    private Label mpLabel;
    private Label xpLabel;
    private Label levelLabel;
    private Label iniLabel;
   
    //value labels
    private Label hp;
    private Label mp;
    private Label xp;
    private Label levelVal;
    private Label iniVal;
    private Label heroNameLabel;

    private Entity linkedEntity;
   
    private static final float BOTTOMMENUHEIGHTTILES = 6;
    private static final float BOTTOMMENUWIDGETS = 3;
   

    //Attributes
    private int heroLevel;
    private int heroHP;
    private int heroMP;
    private int heroXP;
    private int heroINI;
   
   
    private HorizontalGroup bottomMenuTable;
   
    public BottomMenu(Entity[] entities){
        super(title, Utility.STATUSUI_SKIN);
       
        linkUnitsToMenu(entities);
        initElementsForUI();
        configureElements();
        addElementsToWindow();
    }

   private void linkUnitsToMenu(Entity[] entities) {
      for(Entity entity : entities) {
           entity.setbottomMenu(this);
        }
   }
   
    private void initElementsForUI() {
       this.debug();
       //hero name
        heroNameLabel = new Label("", Utility.STATUSUI_SKIN, "inventory-item-count");
        heroNameLabel.setColor(Color.CYAN);
        heroNameLabel.setScale(20.0f);
       
      changeHeroImage(unknownHeroImageLocation);
       
        //groups
      bottomMenuTable = new HorizontalGroup();
      //bottomMenuTable.align(Align.center);
      bottomMenuTable.setFillParent(true);
      this.setTransform(true);
      this.setPosition(0, 0);

        //labels
        hpLabel = new Label(" hp:", Utility.STATUSUI_SKIN);
        hp = new Label("", Utility.STATUSUI_SKIN);
        mpLabel = new Label(" mp:", Utility.STATUSUI_SKIN);
        mp = new Label("", Utility.STATUSUI_SKIN);
        xpLabel = new Label(" xp:", Utility.STATUSUI_SKIN);
        xp = new Label("", Utility.STATUSUI_SKIN);
        levelLabel = new Label(" lv:", Utility.STATUSUI_SKIN);
        levelVal = new Label("", Utility.STATUSUI_SKIN);
        iniLabel = new Label(" ini:", Utility.STATUSUI_SKIN);
        iniVal = new Label("", Utility.STATUSUI_SKIN);
    }

   private void changeHeroImage(String heroImageLink) {
      Utility.loadTextureAsset(heroImageLink);
      TextureRegion tr = new TextureRegion(Utility.getTextureAsset(heroImageLink));
      TextureRegionDrawable trd = new TextureRegionDrawable(tr);
      if(heroImage != null) {
         trd.setMinHeight(this.getHeight());
         trd.setMinWidth(this.getWidth() / BOTTOMMENUWIDGETS);
         heroImage.setDrawable(trd);
      }else {
         trd.setMinHeight(this.getHeight());
         trd.setMinWidth(this.getWidth() / BOTTOMMENUWIDGETS);
         heroImage = new Image(trd);
      }
   }
   
    private void configureElements() {
        defaults().expand().fill();
    }
   
    private void addElementsToWindow() {
       this.add(bottomMenuTable);
       
       bottomMenuTable.addActor(heroImage);
       heroImage.debug();
       
       Table statsGroup = new Table();
       
       statsGroup.setHeight(this.getHeight());
       statsGroup.setWidth(this.getWidth() / BOTTOMMENUWIDGETS);
      
       statsGroup.add(heroNameLabel);
       statsGroup.row();
       
       statsGroup.add(hpLabel);
       statsGroup.add(hp);
       statsGroup.row();
       
       statsGroup.add(mpLabel);
       statsGroup.add(mp);
       statsGroup.row();
       
       bottomMenuTable.addActor(statsGroup);
       statsGroup.debug();
       
       VerticalGroup smallMenu = new VerticalGroup();
       
       smallMenu.setHeight(this.getHeight());
       smallMenu.setWidth(this.getWidth() / BOTTOMMENUWIDGETS);
       
       smallMenu.addActor(new TextButton("test", Utility.STATUSUI_SKIN));
       bottomMenuTable.addActor(smallMenu);
       smallMenu.debug();
    }
   
    public void setHero(Entity entity) {
       if(entity != null) {
          if(entity.getName() != heroNameLabel.getText().toString()) {
              this.linkedEntity = entity;
              initiateHeroStats();
              populateElementsForUI(entity);
          }
       }else {
          resetStats();
       }
    }
   
    private void initiateHeroStats() {
        heroLevel = this.linkedEntity.getLevel();
        heroHP = this.linkedEntity.getHp();
        heroMP = this.linkedEntity.getMp();
        heroXP = this.linkedEntity.getXp();
        heroINI = this.linkedEntity.getIni();
    }
   
    private void populateElementsForUI(Entity entity) {
       heroNameLabel.setText(entity.getName());
       changeHeroImage(entity.getPortraitPath());
       updateLabels();
    }
   
    private void resetStats() {
       heroNameLabel.setText("");
       hp.setText("");
       mp.setText("");
       xp.setText("");
       levelVal.setText("");
       iniVal.setText("");
       changeHeroImage(unknownHeroImageLocation);
    }
   
    public void update() {
       Gdx.app.debug(TAG, "updating bottom menu UI");
        updateStats();
        updateLabels();
        updateSize();
    }

   private void updateLabels() {
      hp.setText(String.valueOf(heroHP));
        mp.setText(String.valueOf(heroMP));
        xp.setText(String.valueOf(heroXP));
        levelVal.setText(String.valueOf(heroLevel));
        iniVal.setText(String.valueOf(heroINI));
   }

   private void updateStats() {
      if(linkedEntity != null) {
         heroLevel = linkedEntity.getLevel();
           heroHP = linkedEntity.getHp();
           heroMP = linkedEntity.getMp();
           heroXP = linkedEntity.getXp();
          
           if(linkedEntity.getEntityactor().getIsHovering()) {
              this.setVisible(true);
           }
      }
   }
   
   private void updateSize() {
      int scaledWidth = Gdx.graphics.getWidth();
      int scaledHeight = (int) (BOTTOMMENUHEIGHTTILES * Map.TILE_HEIGHT_PIXEL);
      this.setSize(scaledWidth,scaledHeight);
      bottomMenuTable.setSize(scaledWidth,scaledHeight);
   }
}



fallia20
 
Posts: 1
Joined: Thu Nov 21, 2019 9:11 am

Re: Make actors span width of parent

Postby shatterblast » Sat Jan 25, 2020 2:02 pm

I know this is older, but Container can help you space things more easily. Also, VisUI helps make Scene2D things less complicated.

1) https://github.com/kotcrab/vis-ui
shatterblast
 
Posts: 653
Joined: Sun Jul 06, 2014 1:14 pm


Return to General Development

Who is online

Users browsing this forum: No registered users and 1 guest