Last active
December 15, 2019 15:41
-
-
Save Juuxel/1566d40f84b435060185d40fe9c1cf36 to your computer and use it in GitHub Desktop.
2019-12-09: An example of using LibGui's WListPanel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package example; | |
import com.google.common.collect.ImmutableList; | |
import io.github.cottonmc.cotton.gui.client.LightweightGuiDescription; | |
import io.github.cottonmc.cotton.gui.widget.WGridPanel; | |
import io.github.cottonmc.cotton.gui.widget.WLabel; | |
import io.github.cottonmc.cotton.gui.widget.WListPanel; | |
import net.minecraft.text.LiteralText; | |
import java.util.List; | |
/** | |
* An example of a LibGui {@code WListPanel}. | |
* | |
* <p>List panels have two generic parameters: D and W. | |
* D represents the type of data that the list visualizes. | |
* W is the widget type that is used in the list to visualize the data. | |
* | |
* <p>List panels utilize a pool of configurable widgets to represent the data. | |
* They are created with the {@code Supplier<W>} that is passed in the constructor. | |
* The {@code BiConsumer<D, W>} is the <i>configurator</i>, which is used to configure the widget to display the data. | |
* | |
* <p>License: <a href="https://creativecommons.org/publicdomain/zero/1.0/">CC0</a> | |
* @author Juuz | |
*/ | |
public class ListPanelExample extends LightweightGuiDescription { | |
public ListPanelExample() { | |
WGridPanel root = new WGridPanel(); | |
// This is the data list that the list panel represents. | |
// The list can also be a mutable list (like an ArrayList), | |
// but WListPanel.layout() must be called to update the list panel | |
// when the list is modified. | |
List<Person> people = ImmutableList.of( | |
new Person("John", 46), | |
new Person("Mary", 70) | |
); | |
// Here we create the list panel, which represents Person instances with WLabels. | |
WListPanel<Person, WLabel> list = new WListPanel<>( | |
people, // This is our data list | |
WLabel.class, // This is the widget type of the list panel (in our case, WLabel) | |
() -> new WLabel(""), // This is the widget supplier that creates empty labels | |
// Here is the configurator that sets the label to display a specific person's information. | |
(person, label) -> label.setText(new LiteralText(person.name + ", " + person.age)) | |
); | |
root.add(list, 0, 0, 9, 4); | |
setRootPanel(root); | |
root.validate(this); | |
} | |
// This is the type of data we want to have in the list | |
// -- in this case, a person with a name and an age. | |
public static class Person { | |
public final String name; | |
public final int age; | |
public Person(String name, int age) { | |
this.name = name; | |
this.age = age; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment