allow mouse input for scrolling, don't update text area model when nothing has changed
2 * Copyright (c) 2008-2011, Matthias Mann
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
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.
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.
32 import de.matthiasmann.twl.Event;
33 import de.matthiasmann.twl.ThemeInfo;
34 import de.matthiasmann.twl.Widget;
38 * @author Matthias Mann
40 public class InventoryPanel extends Widget {
42 private int numSlotsX;
43 private int numSlotsY;
44 private final ItemSlot[] slot;
46 private int slotSpacing;
48 private ItemSlot dragSlot;
49 private ItemSlot dropSlot;
51 public InventoryPanel(int numSlotsX, int numSlotsY) {
52 this.numSlotsX = numSlotsX;
53 this.numSlotsY = numSlotsY;
54 this.slot = new ItemSlot[numSlotsX * numSlotsY];
56 ItemSlot.DragListener listener = new ItemSlot.DragListener() {
57 public void dragStarted(ItemSlot slot, Event evt) {
58 InventoryPanel.this.dragStarted(slot, evt);
60 public void dragging(ItemSlot slot, Event evt) {
61 InventoryPanel.this.dragging(slot, evt);
63 public void dragStopped(ItemSlot slot, Event evt) {
64 InventoryPanel.this.dragStopped(slot, evt);
68 for(int i=0 ; i<slot.length ; i++) {
69 slot[i] = new ItemSlot();
70 slot[i].setListener(listener);
74 slot[0].setItem("red");
75 slot[1].setItem("green");
76 slot[2].setItem("blue");
77 slot[3].setItem("yellow");
81 public int getPreferredInnerWidth() {
82 return (slot[0].getPreferredWidth() + slotSpacing)*numSlotsX - slotSpacing;
86 public int getPreferredInnerHeight() {
87 return (slot[0].getPreferredHeight() + slotSpacing)*numSlotsY - slotSpacing;
91 protected void layout() {
92 int slotWidth = slot[0].getPreferredWidth();
93 int slotHeight = slot[0].getPreferredHeight();
95 for(int row=0,y=getInnerY(),i=0 ; row<numSlotsY ; row++) {
96 for(int col=0,x=getInnerX() ; col<numSlotsX ; col++,i++) {
98 slot[i].setPosition(x, y);
99 x += slotWidth + slotSpacing;
101 y += slotHeight + slotSpacing;
106 protected void applyTheme(ThemeInfo themeInfo) {
107 super.applyTheme(themeInfo);
108 slotSpacing = themeInfo.getParameter("slotSpacing", 5);
111 void dragStarted(ItemSlot slot, Event evt) {
112 if(slot.getItem() != null) {
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);
129 void dragStopped(ItemSlot slot, Event evt) {
130 if(dragSlot != null) {
132 if(dropSlot != null && dropSlot.canDrop() && dropSlot != dragSlot) {
133 dropSlot.setItem(dragSlot.getItem());
134 dragSlot.setItem(null);
141 private void setDropSlot(ItemSlot slot) {
142 if(slot != dropSlot) {
143 if(dropSlot != null) {
144 dropSlot.setDropState(false, false);
147 if(dropSlot != null) {
148 dropSlot.setDropState(true, dropSlot == dragSlot || dropSlot.canDrop());