gt: 						xAnchor, yAnchor, 1, Name.SLOT, new CGUIFunction(){
context: package com.saltosion.sog.guigroups;
  
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.Iterator;
  
  import com.artemis.Entity;
  import com.badlogic.gdx.graphics.Color;
  import com.esotericsoftware.kryonet.Client;
  import com.saltosion.sog.components.CGUIFunction;
  import com.saltosion.sog.components.CGUIPosition;
  import com.saltosion.sog.components.CId;
  import com.saltosion.sog.components.CInventory;
  import com.saltosion.sog.components.CName;
  import com.saltosion.sog.components.CPosition;
  import com.saltosion.sog.components.CRenderable;
  import com.saltosion.sog.components.CSpriteName;
  import com.saltosion.sog.multiplayer.actions.MoveItemSimple;
  import com.saltosion.sog.systems.RenderingSystem;
  import com.saltosion.sog.util.AppUtil;
  import com.saltosion.sog.util.GUIUtil;
  import com.saltosion.sog.util.Name;
  
  public class InventoryGUI extends ItemContainerGroup {
  
  	private CInventory inventory;
  	private boolean dragging = false;
  	
  	public InventoryGUI(CInventory inv) {
  		super(Name.INVENTORY, -170, 100, 10, 6);
  		inventory = inv;
  		setAnchors(1, 0);
  	}
  
  	@Override
  	public boolean isOccupied(int x, int y, Entity... exceptions) {
  		return inventory.isOccupied(x, y, exceptions);
  	}
  
  	@Override
  	public boolean fits(Entity item, int x, int y, Entity... exceptions) {
  		return inventory.fits(item, x, y, exceptions);
  	}
  
  	@Override
  	public void addItem(Entity item) {
  		final Entity addedItem = item;
  		final CPosition pos = addedItem.getComponent(CPosition.class);
  		String spriteName = addedItem.getComponent(CSpriteName.class).name;
  		String name = addedItem.getComponent(CName.class).getName();
  		long id = addedItem.getComponent(CId.class).getID();   // See if these important components are null
  		if (pos == null || spriteName == null || name == null) {   // These are extremely important for item
  			return;
  		}
  		Entity element;
  		items.put(id, (element = GUIUtil.createSpriteGUI(
  				name, (x+pos.x*SLOTSIZE)*RenderingSystem.scale,
  				(y+pos.y*SLOTSIZE)*RenderingSystem.scale,
  				xAnchor, yAnchor, 3, spriteName, new CGUIFunction() {
  					
  					private Entity item = addedItem;
  					private CPosition itemPos = pos;
  					private ArrayList<Entity> hovered = new ArrayList<Entity>();
  					
  					@Override
  					public void onEnter(Entity host, int x, int y) {
  						// Highlight the slots this item occupies
  						ArrayList<ItemContainerGroup> groups = getGroups(item, 
  														(int) (x+host.getComponent(CGUIPosition.class).getX()),
  														(int) (y+host.getComponent(CGUIPosition.class).getY()));
  						for (ItemContainerGroup group : groups) {
  							for (Entity slot : group.getSlots(item, (int)itemPos.x, (int)itemPos.y)) {
  								slot.getComponent(CGUIFunction.class).setData(slot, Name.ACTIVE, 1);
  								hovered.add(slot);
  							}
  						}
  					}
  					
  					@Override
  					public void onLeave(Entity host, int x, int y) {
  						// Remove the highlight (see above)
  						Iterator<Entity> iter = hovered.iterator();
  						while (iter.hasNext()){
  							Entity slot = iter.next();
  							slot.getComponent(CGUIFunction.class).setData(slot, Name.ACTIVE, -1);
  							iter.remove();
  						}
  					}
  					
  					@Override
  					public void onPressed(Entity host, int x, int y) {
  						if (InventoryGUI.this.dragging) {
  							return;
  						}
  						InventoryGUI.this.createDraggedItem(item, x, y);
  					}
  					
  					@Override
  					public void onReleased(Entity host, int x, int y) {
  					}
  					
  					@Override
  					public void onMouseMove(Entity host, int x, int y) {
  					}
  					
  					@Override
  					public void onRemove(Entity host) {
  						// Make sure nothing is highlighted after remove (because of this item)
  						Iterator<Entity> iter = hovered.iterator();
  						while (iter.hasNext()){
  							Entity slot = iter.next();
  							slot.getComponent(CGUIFunction.class).setData(slot, Name.ACTIVE, -1);
  							iter.remove();
  						}
  					}
  				})));
  		addGuiElement(element);
  		
  	}
  
  	@Override
  	public Entity createDraggedItem(Entity item, int xOffset, int yOffset) {
  		final int xOffsetTemp = xOffset;
  		final int yOffsetTemp = yOffset;
  		final Entity itemTemp = item;
  		CPosition pos = item.getComponent(CPosition.class);
  		CName name = item.getComponent(CName.class);
  		CSpriteName spriteName = item.getComponent(CSpriteName.class);
  		if (pos == null || name == null || spriteName == null) {  // Check if these important components are null
  			return null;												// Although it should not be possible.
  		}
  		Entity guiElement;
  		addGuiElement((guiElement = GUIUtil.createSpriteGUI("ITEM-DRAG-SHADOW", 
  				(x*RenderingSystem.scale + RenderingSystem.width*xAnchor) // Create the shadow on the item that
  				+pos.x*RenderingSystem.scale*SLOTSIZE,											// was pressed
  				(y*RenderingSystem.scale + RenderingSystem.height*yAnchor)
  				+pos.y*RenderingSystem.scale*SLOTSIZE,
  				0, 0, 4, spriteName.name, new CGUIFunction(){
  			
  					private int xOffset = xOffsetTemp;
  					private int yOffset = yOffsetTemp;
  					private Entity item = itemTemp;
  					private boolean isDragged = true;
  					private int xLast = 0;
  					private int yLast = 0;
  					private ArrayList<Entity> hovered = new ArrayList<Entity>();
  
  					@Override
  					public void onEnter(Entity host, int x, int y) {
  					}
  
  					@Override
  					public void onLeave(Entity host, int x, int y) {
  					}
  
  					@Override
  					public void onPressed(Entity host, int x, int y) {
  					}
  
  					@Override
  					public void onReleased(Entity host, int x, int y) {
  						isDragged = false;
  						deleteGUIElement(host);
  						InventoryGUI.this.move(item, xLast, yLast);
  						InventoryGUI.this.dragging = false;
  					}
  
  					@Override
  					public void onMouseMove(Entity host, int x, int y) {
  						if (isDragged) {	// If the item should be dragged, move the shadow's position
  							xLast = x-xOffset;
  							yLast = y-yOffset;
  							CGUIPosition pos = host.getComponent(CGUIPosition.class);
  							pos.setPosition(xLast, yLast);
  							
  							clearHovered();
  							updateHovered();
  						}
  					}
  					
  					@Override
  					public void onRemove(Entity host) {
  						clearHovered();
  					}
  					
  					private void clearHovered() {
  						for (Entity slot : hovered) {  // Clear all hovered slots
  							CGUIFunction function = slot.getComponent(CGUIFunction.class);
  							function.setData(slot, Name.ACTIVE, -1);
  							function.setData(slot, Name.TINT, new Color(1, 1, 1, 1));
  						}
  						hovered.clear();
  					}
  					
  					private void updateHovered() {
  						ArrayList<ItemContainerGroup> groups = getGroups(item, xLast, yLast);
  						
  						for (ItemContainerGroup group : groups) {        // Loop through all the groups this item hovers over
  							Entity[] slots = group.getSlotsByScreen(item, xLast, yLast);
  							Color curr;
  							if (group.fits(item,
  									(int) ((xLast-getX())/SLOTSIZE/RenderingSystem.scale),
  									(int) ((yLast-getY())/SLOTSIZE/RenderingSystem.scale),
  									item)) {
  								curr = new Color(0, 1, 0, 1);
  							} else { curr = new Color(1, 0, 0, 1); }
  							Iterator<Entity> iter = Arrays.asList(slots).iterator();
  							  				 // Loop through all the slots in this group that it hovers over
  							while (iter.hasNext()) {
  								Entity slot = iter.next();
  								if (slot == null) {
  									continue;			// The slot was null (not found?)
  								}
  								CGUIFunction function = slot.getComponent(CGUIFunction.class);
  								function.setData(slot, Name.ACTIVE, 1);
  								function.setData(slot, Name.TINT, curr);									
  								hovered.add(slot);
  							}
  						}						
  					}
  		})));
  		guiElement.getComponent(CRenderable.class).tint = new Color(.5f, .5f, .5f, 1); // Make the shadow gray
  		guiElement.getComponent(CRenderable.class).alpha = .5f;
  		dragging = true;
  		return guiElement;
  	}
  
  	@Override
  	public void move(Entity item, int x, int y) {
  		
  		ArrayList<ItemContainerGroup> groups = getGroups(item, x, y);
  		ItemContainerGroup group = null;
  		if (groups.size() > 0) {
  			group = groups.get(0);			
  		} else { return; }
  		
  		x = (int) (((x-getX())/RenderingSystem.scale)/SLOTSIZE);
  		y = (int) (((y-getY())/RenderingSystem.scale)/SLOTSIZE);
  		
  		if (group instanceof InventoryGUI) {
  			MoveItemSimple action = new MoveItemSimple();
  			action.setID(item.getComponent(CId.class));
  			action.setXY(x, y);
  			((Client)AppUtil.endpoint).sendTCP(action);
  		}
  	}
  	
  	@Override
  	public void updateMoveToGUI(Entity item) {
  		deleteGUIElement(items.get(item.getComponent(CId.class).getID()));
  		addItem(item);
  	}
  
  	@Override
  	public void create() {
  		addGuiElement(GUIUtil.createSpriteGUI(Name.INVENTORY + "-background", x
  				* RenderingSystem.scale, y * RenderingSystem.scale, xAnchor, yAnchor, 
  				0, Name.INVENTORY, new CGUIFunction() {
  					@Override
  					public void onEnter(Entity host, int x, int y) {
  					}
  
  					@Override
  					public void onLeave(Entity host, int x, int y) {
  					}
  
  					@Override
  					public void onPressed(Entity host, int x, int y) {
  					}
  
  					@Override
  					public void onReleased(Entity host, int x, int y) {
  					}
  
  					@Override
  					public void onMouseMove(Entity host, int x, int y) {
  					}
  				}));
  		
  		for (int yi=0; yi<height; yi++){
  			for (int xi=0; xi<width; xi++){
  				Entity curr;
  				addGuiElement((curr = GUIUtil.createSpriteGUI(Name.SLOT + "-" + yi + "-" + xi, 
  						(x+SLOTSIZE*xi)*RenderingSystem.scale,
  						(y+SLOTSIZE*yi)*RenderingSystem.scale,
  