Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2014
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.59 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package xenustreet.gui.control;
  6.  
  7. import com.jme3.input.event.MouseButtonEvent;
  8. import com.jme3.math.Vector2f;
  9. import com.jme3.math.Vector4f;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. import tonegod.gui.controls.scrolling.ScrollAreaAdapter;
  13. import tonegod.gui.core.ElementManager;
  14.  
  15. /**
  16.  *
  17.  * @author root
  18.  */
  19. public abstract class GridButtonClickableListControl<T> extends ScrollAreaAdapter {
  20.     private final List<T> list;
  21.     private final List<GridRowButton> buttons;
  22.     private float vOffset;
  23.     private float absWidth; // for some reason this changes
  24.  
  25.     public GridButtonClickableListControl(ElementManager screen, Vector2f position, List<T> list) {
  26.         super(screen, position);
  27.         this.list = list;
  28.         this.buttons = new ArrayList<>();
  29.         absWidth = getScrollableArea().getAbsoluteWidth();
  30.         invalidate();
  31.     }
  32.  
  33.     public GridButtonClickableListControl(ElementManager screen, Vector2f position, Vector2f dimensions, List<T> list) {
  34.         super(screen, position, dimensions);
  35.         this.list = list;
  36.         this.buttons = new ArrayList<>();
  37.         absWidth = getScrollableArea().getAbsoluteWidth();
  38.         invalidate();
  39.     }
  40.  
  41.     public GridButtonClickableListControl(ElementManager screen, Vector2f position, Vector2f dimensions, Vector4f resizeBorders, String defaultImg, List<T> list) {
  42.         super(screen, position, dimensions, resizeBorders, defaultImg);
  43.         this.list = list;
  44.         this.buttons = new ArrayList<>();
  45.         absWidth = getScrollableArea().getAbsoluteWidth();
  46.         invalidate();
  47.     }
  48.  
  49.     public GridButtonClickableListControl(ElementManager screen, String UID, Vector2f position, List<T> list) {
  50.         super(screen, UID, position);
  51.         this.list = list;
  52.         this.buttons = new ArrayList<>();
  53.         absWidth = getScrollableArea().getAbsoluteWidth();
  54.         invalidate();
  55.     }
  56.  
  57.     public GridButtonClickableListControl(ElementManager screen, String UID, Vector2f position, Vector2f dimensions, List<T> list) {
  58.         super(screen, UID, position, dimensions);
  59.         this.list = list;
  60.         this.buttons = new ArrayList<>();
  61.         absWidth = getScrollableArea().getAbsoluteWidth();
  62.         invalidate();
  63.     }
  64.  
  65.     public GridButtonClickableListControl(ElementManager screen, String UID, Vector2f position, Vector2f dimensions, Vector4f resizeBorders, String defaultImg, List<T> list) {
  66.         super(screen, UID, position, dimensions, resizeBorders, defaultImg);
  67.         this.list = list;
  68.         this.buttons = new ArrayList<>();
  69.         absWidth = getScrollableArea().getAbsoluteWidth();
  70.         invalidate();
  71.     }
  72.    
  73.     /**
  74.      * invalidates the current state, causing an update based on the current
  75.      * contents of the list
  76.      */
  77.     public final void invalidate(){
  78.         for(GridRowButton btn : buttons){
  79.             removeScrollableChild(btn);
  80.         }
  81.         buttons.clear();
  82.         for(T item : list){
  83.             addItem(item, false);
  84.         }
  85.         //updateClipping();
  86.     }
  87.    
  88.     public final void addItem(T item, boolean addToList){
  89.         final GridButtonClickableListControl<T> _t = this;
  90.         final int nxtIndex = buttons.size();
  91.        
  92.         GridRowButton button = new GridRowButton(screen,
  93.                 new Vector2f(0f, vOffset),
  94.                 new Vector2f(absWidth, 80f ),
  95.                 3, 20f){
  96.                     @Override
  97.                     public void onButtonMouseLeftDown(MouseButtonEvent evt, boolean toggled) {
  98.                         super.onButtonMouseLeftDown(evt, toggled);
  99.                         _t.onButtonClick(nxtIndex);
  100.                     }
  101.                 };
  102.         populateGridButton(item, button);
  103.        
  104.         addScrollableChild(button);
  105.         vOffset += button.getHeight();
  106.         buttons.add(button);
  107.         if(addToList){
  108.             list.add(item);
  109.             scrollToBottom();
  110.         }
  111.     }
  112.    
  113.     public final void addItem(T item){
  114.         addItem(item, true);
  115.     }
  116.    
  117.     /**
  118.      * overide this method to populate the grid button
  119.      * @param item
  120.      * @param btn
  121.      */
  122.     public abstract void populateGridButton(T item, GridRowButton btn);
  123.    
  124.     /**
  125.      * this is where you return something to happen for a given action.
  126.      * @param item
  127.      */
  128.     public abstract void onButtonClick(T item, GridRowButton btn, int idx);
  129.  
  130.     private void onButtonClick(int idx) {
  131.         onButtonClick(list.get(idx), buttons.get(idx), idx);
  132.     }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement