Skip to content

Instantly share code, notes, and snippets.

@Mr00Anderson
Created September 30, 2020 14:48
Show Gist options
  • Save Mr00Anderson/fa6c3f58c10480965d42015ae8be1712 to your computer and use it in GitHub Desktop.
Save Mr00Anderson/fa6c3f58c10480965d42015ae8be1712 to your computer and use it in GitHub Desktop.
package com.virtual_hex.gdx.engine.scene2d;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.GdxRuntimeException;
import java.util.HashMap;
import java.util.Map;
public class TableList<T extends Table> extends Table {
public transient Map<T, Table> tables = new HashMap<>();
public Array<T> selected = new Array<>();
public Drawable originalBackground;
public Drawable selectedBackground;
public Color selectedColor;
public boolean multiSelect;
public TableList(Skin skin, boolean multiSelect, Color selectedColor) {
super(skin);
this.multiSelect = multiSelect;
this.selectedColor = selectedColor;
init();
}
private void init() {
originalBackground = getBackground();
Skin skin = getSkin();
if(!skin.has("rect", Texture.class)){
Texture texture = EngUiUtils.getTextureDot(Color.WHITE);
skin.add("rect", texture);
}
Drawable drawable = getSkin().newDrawable("rect", selectedColor);
selectedBackground = drawable;
}
public void selectTable(T table){
Table outerTable = tables.get(table);
if(outerTable == null) throw new GdxRuntimeException("Cannot select a table that has not been added to the table list");
if(!multiSelect){
if(selected.size > 0) {
T t = selected.get(0);
if (t != null) {
unselectTable(t);
}
}
}
selected.add(table);
outerTable.setBackground(selectedBackground);
}
public void unselectTable(T table){
Table outerTable = tables.get(table);
if(outerTable == null) throw new GdxRuntimeException("Cannot deselect a table that has not been added to the table list");
selected.removeValue(table, true);
outerTable.setBackground(originalBackground);
}
public void addTable(T table){
Table innerTable = new Table(getSkin());
innerTable.add(table);
TextButton selectButton = new TextButton("Select", getSkin());
innerTable.add(selectButton).padLeft(5);
selectButton.addListener(new ClickListener(){
@Override
public void clicked(InputEvent event, float x, float y) {
if(selected.contains(table, true)){
unselectTable(table);
} else {
selectTable(table);
}
}
});
Table outerTable = new Table(getSkin());
// Wrap in outer table to select it
innerTable.pad(5);
outerTable.add(innerTable);
add(outerTable);
row();
tables.put(table, outerTable);
}
public void removeTable(T table){
Table outerTable = tables.remove(table);
outerTable.remove();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment