src/inventory/InventoryPanel.java
author Matthias Mann
Sat Feb 11 13:27:12 2012 +0100 (3 months ago)
changeset 262 f896aec65113
parent 249 9f7da48592a5
permissions -rw-r--r--
allow mouse input for scrolling, don't update text area model when nothing has changed
     1 /*
     2  * Copyright (c) 2008-2011, Matthias Mann
     3  *
     4  * All rights reserved.
     5  *
     6  * Redistribution and use in source and binary forms, with or without
     7  * modification, are permitted provided that the following conditions are met:
     8  *
     9  *     * Redistributions of source code must retain the above copyright notice,
    10  *       this list of conditions and the following disclaimer.
    11  *     * Redistributions in binary form must reproduce the above copyright
    12  *       notice, this list of conditions and the following disclaimer in the
    13  *       documentation and/or other materials provided with the distribution.
    14  *     * Neither the name of Matthias Mann nor the names of its contributors may
    15  *       be used to endorse or promote products derived from this software
    16  *       without specific prior written permission.
    17  *
    18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
    22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    25  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    26  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    27  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    29  */
    30 package inventory;
    31 
    32 import de.matthiasmann.twl.Event;
    33 import de.matthiasmann.twl.ThemeInfo;
    34 import de.matthiasmann.twl.Widget;
    35 
    36 /**
    37  *
    38  * @author Matthias Mann
    39  */
    40 public class InventoryPanel extends Widget {
    41     
    42     private int numSlotsX;
    43     private int numSlotsY;
    44     private final ItemSlot[] slot;
    45     
    46     private int slotSpacing;
    47 
    48     private ItemSlot dragSlot;
    49     private ItemSlot dropSlot;
    50     
    51     public InventoryPanel(int numSlotsX, int numSlotsY) {
    52         this.numSlotsX = numSlotsX;
    53         this.numSlotsY = numSlotsY;
    54         this.slot = new ItemSlot[numSlotsX * numSlotsY];
    55         
    56         ItemSlot.DragListener listener = new ItemSlot.DragListener() {
    57             public void dragStarted(ItemSlot slot, Event evt) {
    58                 InventoryPanel.this.dragStarted(slot, evt);
    59             }
    60             public void dragging(ItemSlot slot, Event evt) {
    61                 InventoryPanel.this.dragging(slot, evt);
    62             }
    63             public void dragStopped(ItemSlot slot, Event evt) {
    64                 InventoryPanel.this.dragStopped(slot, evt);
    65             }
    66         };
    67         
    68         for(int i=0 ; i<slot.length ; i++) {
    69             slot[i] = new ItemSlot();
    70             slot[i].setListener(listener);
    71             add(slot[i]);
    72         }
    73         
    74         slot[0].setItem("red");
    75         slot[1].setItem("green");
    76         slot[2].setItem("blue");
    77         slot[3].setItem("yellow");
    78     }
    79 
    80     @Override
    81     public int getPreferredInnerWidth() {
    82         return (slot[0].getPreferredWidth() + slotSpacing)*numSlotsX - slotSpacing;
    83     }
    84 
    85     @Override
    86     public int getPreferredInnerHeight() {
    87         return (slot[0].getPreferredHeight() + slotSpacing)*numSlotsY - slotSpacing;
    88     }
    89 
    90     @Override
    91     protected void layout() {
    92         int slotWidth  = slot[0].getPreferredWidth();
    93         int slotHeight = slot[0].getPreferredHeight();
    94         
    95         for(int row=0,y=getInnerY(),i=0 ; row<numSlotsY ; row++) {
    96             for(int col=0,x=getInnerX() ; col<numSlotsX ; col++,i++) {
    97                 slot[i].adjustSize();
    98                 slot[i].setPosition(x, y);
    99                 x += slotWidth + slotSpacing;
   100             }
   101             y += slotHeight + slotSpacing;
   102         }
   103     }
   104 
   105     @Override
   106     protected void applyTheme(ThemeInfo themeInfo) {
   107         super.applyTheme(themeInfo);
   108         slotSpacing = themeInfo.getParameter("slotSpacing", 5);
   109     }
   110     
   111     void dragStarted(ItemSlot slot, Event evt) {
   112         if(slot.getItem() != null) {
   113             dragSlot = slot;
   114             dragging(slot, evt);
   115         }
   116     }
   117     
   118     void dragging(ItemSlot slot, Event evt) {
   119         if(dragSlot != null) {
   120             Widget w = getWidgetAt(evt.getMouseX(), evt.getMouseY());
   121             if(w instanceof ItemSlot) {
   122                 setDropSlot((ItemSlot)w);
   123             } else {
   124                 setDropSlot(null);
   125             }
   126         }
   127     }
   128     
   129     void dragStopped(ItemSlot slot, Event evt) {
   130         if(dragSlot != null) {
   131             dragging(slot, evt);
   132             if(dropSlot != null && dropSlot.canDrop() && dropSlot != dragSlot) {
   133                 dropSlot.setItem(dragSlot.getItem());
   134                 dragSlot.setItem(null);
   135             }
   136             setDropSlot(null);
   137             dragSlot = null;
   138         }
   139     }
   140 
   141     private void setDropSlot(ItemSlot slot) {
   142         if(slot != dropSlot) {
   143             if(dropSlot != null) {
   144                 dropSlot.setDropState(false, false);
   145             }
   146             dropSlot = slot;
   147             if(dropSlot != null) {
   148                 dropSlot.setDropState(true, dropSlot == dragSlot || dropSlot.canDrop());
   149             }
   150         }
   151     }
   152     
   153 }