Ji,
I implemented a simple drag and drop using the captures events stuff, that works perfectly so far, I can drag any inventory item and it works fine (actually better than with javafx, due to the lower delay in frames )
Now if I hover above another inventory I need to do some checks if it will fit in (and represent this visually) also I need the hoverd above element for actually processing the drop.
How do I find out if I’m hoverd above an spatial with a specific class or similar?
The interesting/missing part is in the mouseMoved method
package de.visiongamestudios.client.inventory;
import java.util.Map.Entry;
import com.jme3.input.MouseInput;
import com.jme3.input.event.MouseButtonEvent;
import com.jme3.input.event.MouseMotionEvent;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Spatial;
import com.simsilica.lemur.Container;
import com.simsilica.lemur.GridPanel;
import com.simsilica.lemur.Label;
import com.simsilica.lemur.Panel;
import com.simsilica.lemur.component.BorderLayout;
import com.simsilica.lemur.component.QuadBackgroundComponent;
import com.simsilica.lemur.component.TbtQuadBackgroundComponent;
import com.simsilica.lemur.event.DefaultMouseListener;
import com.simsilica.lemur.event.MouseEventControl;
import com.simsilica.lemur.grid.ArrayGridModel;
import de.visiongamestudios.client.jme.RendererApplication;
import de.visiongamestudios.shared.inventory.decorator.IInventoryDecorator;
import de.visiongamestudios.shared.inventory.decorator.IInventoryDecorator.EntityDescription;
public class ItemProxy extends Container {
private static final float DESCRIPTION_FONT_SIZE = 16;
protected Container overlay;
protected static ItemProxy dragProxy;
public ItemProxy(EntityDescription item,RendererApplication app) {
this(item,app,false);
}
public ItemProxy(EntityDescription item,RendererApplication app,boolean dragproxy) {
setLayout(new BorderLayout());
Container bottom = new Container();
bottom.setLayout(new BorderLayout());
addChild(bottom, BorderLayout.Position.South);
if (item.image != null) {
setBackground(new QuadBackgroundComponent(item.image));
}
Label amount = new Label(item.amount + "");
bottom.setBackground(new QuadBackgroundComponent(new ColorRGBA(0, 0, 0, 0.8f)));
bottom.addChild(amount, BorderLayout.Position.West);
String itemSizing = item.size.name();
String itemVolume = item.volume + "";
Label sizeLabel = new Label(itemSizing + itemVolume);
bottom.addChild(sizeLabel, BorderLayout.Position.East);
if(!dragproxy){
initMouse(app,item);
}
}
void initMouse(RendererApplication app, EntityDescription item){
MouseEventControl.addListenersToSpatial(this, new DefaultMouseListener(){
@Override
public void mouseEntered(MouseMotionEvent event, Spatial target, Spatial capture) {
if(dragProxy != null){
return;
}
overlay = createInfoOverlay(item);
Vector3f wt = ItemProxy.this.getWorldTranslation();
overlay.setLocalTranslation(wt.x+ContainerView.SIZE,wt.y,wt.z+2);
app.getGuiNode().attachChild(overlay);
}
@Override
public void mouseExited(MouseMotionEvent event, Spatial target, Spatial capture) {
if(overlay != null){
overlay.removeFromParent();
}
}
@Override
public void mouseButtonEvent(MouseButtonEvent event, Spatial target, Spatial capture) {
System.out.println("event");
if(event.getButtonIndex()==MouseInput.BUTTON_LEFT && event.isPressed()){
event.setConsumed();
System.out.println("Start drag");
if(dragProxy != null){
dragProxy.removeFromParent();
}
if(overlay != null){
overlay.removeFromParent();
}
dragProxy = new ItemProxy(item, app,true);
dragProxy.setPreferredSize(new Vector3f(ContainerView.SIZE, ContainerView.SIZE, 1));
Vector3f wt = ItemProxy.this.getWorldTranslation();
dragProxy.setLocalTranslation(wt.x,wt.y,wt.z+2);
app.getGuiNode().attachChild(dragProxy);
}
if(event.getButtonIndex()==MouseInput.BUTTON_LEFT && event.isReleased() && dragProxy != null){
dragProxy.removeFromParent();
}
}
@Override
public void mouseMoved(MouseMotionEvent event, Spatial target, Spatial capture) {
System.out.println("move");
if(dragProxy != null){
System.out.println(event.getDX());
Vector3f wt = ItemProxy.this.getWorldTranslation();
dragProxy.setLocalTranslation(event.getX()-ContainerView.SIZE/2,event.getY()+ContainerView.SIZE/2,wt.z+2);
//check if above another ContainerView Spatial
//check if fit in
//check if satisfy filter
//check if teleporter available
event.setConsumed();
}
super.mouseMoved(event, target, capture);
}
});
}
protected Container createInfoOverlay(EntityDescription item) {
Container tooltip = new Container();
TbtQuadBackgroundComponent background = (TbtQuadBackgroundComponent) tooltip.getBackground();
background.setColor(new ColorRGBA(0.25f, 0.5f, 0.5f, 1f));
Label name = new Label(item.valueTable.getOrDefault(IInventoryDecorator.NAME,"Unidentified Item").toString());
name.setFontSize(DESCRIPTION_FONT_SIZE);
tooltip.addChild(name);
Label description = new Label(item.description);
description.setFontSize(DESCRIPTION_FONT_SIZE);
tooltip.addChild(description);
Panel[][] data = new Panel[item.valueTable.size()][2];
int i = 0;
for(Entry<String, Object> value:item.valueTable.entrySet()){
data[i][0]= new Label(value.getKey());
Object kv = value.getValue();
if(kv instanceof String){
data[i][1]=new Label((String) kv);
}else if(kv instanceof Number){
data[i][1]=new Label(kv+"");
}else if(kv instanceof Enum){
data[i][1]=new Label(kv+"");
}else{
data[i][1]=new Label(kv.getClass().getName() + " " + kv.toString());
}
i++;
}
GridPanel dataPanel = new GridPanel(new ArrayGridModel<>(data));
tooltip.addChild(dataPanel);
return tooltip;
}
}