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