Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package xenustreet.gui.control;
- import com.jme3.input.event.MouseButtonEvent;
- import com.jme3.math.Vector2f;
- import com.jme3.math.Vector4f;
- import java.util.ArrayList;
- import java.util.List;
- import tonegod.gui.controls.scrolling.ScrollAreaAdapter;
- import tonegod.gui.core.ElementManager;
- /**
- *
- * @author root
- */
- public abstract class GridButtonClickableListControl<T> extends ScrollAreaAdapter {
- private final List<T> list;
- private final List<GridRowButton> buttons;
- private float vOffset;
- private float absWidth; // for some reason this changes
- public GridButtonClickableListControl(ElementManager screen, Vector2f position, List<T> list) {
- super(screen, position);
- this.list = list;
- this.buttons = new ArrayList<>();
- absWidth = getScrollableArea().getAbsoluteWidth();
- invalidate();
- }
- public GridButtonClickableListControl(ElementManager screen, Vector2f position, Vector2f dimensions, List<T> list) {
- super(screen, position, dimensions);
- this.list = list;
- this.buttons = new ArrayList<>();
- absWidth = getScrollableArea().getAbsoluteWidth();
- invalidate();
- }
- public GridButtonClickableListControl(ElementManager screen, Vector2f position, Vector2f dimensions, Vector4f resizeBorders, String defaultImg, List<T> list) {
- super(screen, position, dimensions, resizeBorders, defaultImg);
- this.list = list;
- this.buttons = new ArrayList<>();
- absWidth = getScrollableArea().getAbsoluteWidth();
- invalidate();
- }
- public GridButtonClickableListControl(ElementManager screen, String UID, Vector2f position, List<T> list) {
- super(screen, UID, position);
- this.list = list;
- this.buttons = new ArrayList<>();
- absWidth = getScrollableArea().getAbsoluteWidth();
- invalidate();
- }
- public GridButtonClickableListControl(ElementManager screen, String UID, Vector2f position, Vector2f dimensions, List<T> list) {
- super(screen, UID, position, dimensions);
- this.list = list;
- this.buttons = new ArrayList<>();
- absWidth = getScrollableArea().getAbsoluteWidth();
- invalidate();
- }
- public GridButtonClickableListControl(ElementManager screen, String UID, Vector2f position, Vector2f dimensions, Vector4f resizeBorders, String defaultImg, List<T> list) {
- super(screen, UID, position, dimensions, resizeBorders, defaultImg);
- this.list = list;
- this.buttons = new ArrayList<>();
- absWidth = getScrollableArea().getAbsoluteWidth();
- invalidate();
- }
- /**
- * invalidates the current state, causing an update based on the current
- * contents of the list
- */
- public final void invalidate(){
- for(GridRowButton btn : buttons){
- removeScrollableChild(btn);
- }
- buttons.clear();
- for(T item : list){
- addItem(item, false);
- }
- //updateClipping();
- }
- public final void addItem(T item, boolean addToList){
- final GridButtonClickableListControl<T> _t = this;
- final int nxtIndex = buttons.size();
- GridRowButton button = new GridRowButton(screen,
- new Vector2f(0f, vOffset),
- new Vector2f(absWidth, 80f ),
- 3, 20f){
- @Override
- public void onButtonMouseLeftDown(MouseButtonEvent evt, boolean toggled) {
- super.onButtonMouseLeftDown(evt, toggled);
- _t.onButtonClick(nxtIndex);
- }
- };
- populateGridButton(item, button);
- addScrollableChild(button);
- vOffset += button.getHeight();
- buttons.add(button);
- if(addToList){
- list.add(item);
- scrollToBottom();
- }
- }
- public final void addItem(T item){
- addItem(item, true);
- }
- /**
- * overide this method to populate the grid button
- * @param item
- * @param btn
- */
- public abstract void populateGridButton(T item, GridRowButton btn);
- /**
- * this is where you return something to happen for a given action.
- * @param item
- */
- public abstract void onButtonClick(T item, GridRowButton btn, int idx);
- private void onButtonClick(int idx) {
- onButtonClick(list.get(idx), buttons.get(idx), idx);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement