How to update a Stage layout?

Anything libgdx related goes here!

How to update a Stage layout?

Postby sirrus233 » Sat Sep 13, 2014 11:09 pm

I am trying to design a GUI using Scene2D. I have a single screen, and several different stages which the screen switches between (based on various button presses). Each stage is constructed when it is needed, and at construction, the widgets for the stage are also constructed and laid out into a table structure.

The problem I am having is that I find myself wanting to dynamically update my stages. For example, I have a stage that displays a list of saved "characters" (save files) that can be loaded. Each character profile has an "X" button next to it, to allow for the deletion of the profile. When this button is pressed, I want to delete the label representing the deleted profile, and update the stage accordingly. As you can see from the code below, I solve this by actually constructing an entirely new copy of the stage, which has only the correct labels (specifically, all the previous ones, minus the deleted one).

This is not an acceptable solution, because of the potentially heavy nature of constructing a new stage too often. It is fine for an event that will happen rarely, such as deleting a character. It won't work for a stage that needs to have its widgets updated possibly every frame. Is there a better way of creating and laying out stages such that they can be updated on the fly by the game logic?

Code: Select all
   private Stage loadMenu() {
      Stage loadMenuStage = new Stage(viewport);
      Skin skin = new Skin(Gdx.files.internal("uiskin.json"));
      
       Table table = new Table();
       table.setFillParent(true);
       loadMenuStage.addActor(table);
      
       for (String name : game.dm.loadNames()) {       
          table.add(loadName(name, skin)).width(700).height(75).fill().padRight(15);
          table.add(deleteName(name, skin)).width(75).fill();
          table.row().padTop(50);
       }
       table.row().padTop(80);
       table.add(backToMain(skin)).width(200).height(50).bottom().center();
       table.debug();
      
       return loadMenuStage;
   }
   
   private TextButton loadName(String name, Skin skin) {
       TextButton loadName = new TextButton(name, skin);
      
       final String n = name;
      
       loadName.addListener(new ChangeListener() {
         public void changed(ChangeEvent event, Actor actor) {
            game.setScreen(new CharacterScreen(game, game.dm.load(n)));
         }
      });
      
       return loadName;
   }
   
   private TextButton deleteName(String name, Skin skin) {
      TextButton deleteName = new TextButton("X", skin);
      
      final String n = name;
      
      deleteName.addListener(new ChangeListener() {
         public void changed(ChangeEvent event, Actor actor) {
            game.dm.delete(n);
            setStage(loadMenu());
         }
      });
      
       return deleteName;
   }
   
   private TextButton backToMain(Skin skin) {
      TextButton backToMain = new TextButton("Main Menu", skin);
      
      backToMain.addListener(new ChangeListener() {
         public void changed(ChangeEvent event, Actor actor) {
            setStage(mainMenu());
         }
      });
      
       return backToMain;
   }

   public void setStage(Stage s) {
      imux.removeProcessor(this.stage);
      this.stage = s;
      imux.addProcessor(this.stage);
   }
sirrus233
 
Posts: 1
Joined: Thu Sep 11, 2014 9:54 am

Re: How to update a Stage layout?

Postby jrenner » Mon Sep 15, 2014 2:04 am

use an ObjectMap (libgdx version of HashMap) to store references to your actors, like so:

Code: Select all
ObjectMap<Button, Label> buttonMap = new ObjectMap<>()
buttonMap.put(button1, label1);
...
// code for when delete button is pressed, inside listener
Label label = buttonMap.get(pressedButton);
label.remove() // removes from its parent
Superior Tactics - tactical spaceship battles RTS
Google Play: https://play.google.com/store/apps/deta ... r.superior
jrenner
 
Posts: 520
Joined: Sun Apr 14, 2013 5:09 am


Return to Libgdx

Who is online

Users browsing this forum: Google [Bot], MSN [Bot] and 1 guest