Trying to get events to work for drag and drop


#1

So I’m playing around with drag and drop with Nifty and I have the drag working. I don’t have a drop set up yet as I’m just trying to get the dragging working at the moment.

For the purposes of my game the default drag and drop behavior of moving the draggable to another spot wont work. I’m working on a tower defense kind of game and when the player goes to drag a turret from the inventory into the game, I still need a copy of it to stay in the starting point. So I am trying to use the DraggableDragStartedEvent to determine when the drag starts and make a copy of the dropable to place in the UI so the player can still add more of that turret if they want to. Or is there another way of changing the default behavior so when the player drags they see the item under the mouse/finger but still leaves a copy at the starting point so it can be repeated from the same location?

Here is what I have and it doesn’t seem to be working:

The Nifty book for 1.3.2 (I just now realize JME uses 1.4.2 so this may no longer work and the docs on the github are not as in depth as this book) says to add a methid to your screen controller with the annotation @NiftyEventSubscriber(id=“elementId”) and it must have a String for the first parameter to pass in the id and the second must have an event of the type you want to listen to and its just supposed to work. But its not. I see “bind” in the log and I see “start screen” in the log but when I drag the draggable nothing gets logged. The dragging is working so I know it should be calling this event.

controller(new ScreenController() {
    @Override
    public void bind(Nifty nifty, Screen screen) {
        System.out.println("bind");
    }

    @Override
    public void onStartScreen() {
        System.out.println("start screen");
    }

    @Override
    public void onEndScreen() {
        System.out.println("end screen");
    }
            
    @NiftyEventSubscriber(id="layout")
    public void onClick(String id, DraggableDragStartedEvent event) {
        System.out.println("drag started");
    }
});

And my draggable:

control(new DraggableBuilder("chest") {{
    id("layout");
    childLayoutCenter();
    width("101px");
    height("171px");
    panel(new PanelBuilder() {{
        childLayoutCenter();
        text(new TextBuilder(){{
            text("My Cool Game");
            font("Interface/Fonts/Default.fnt");
            alignCenter();
            valignCenter();
            color(Color.WHITE);
            height("5%");
            width("15%");
        }});
    }});
}});

#2

I was going through the drag and drop example on Nifty github and they use control(new DroppableBuilder("chest") and @NiftyEventSubscriber(id="chest") so I also tried changing control(new DraggableBuilder("chest") to control(new DraggableBuilder("layout") and it still doesn’t work.


#3

I think that the DraggableDragStartedEvent might be broken - I checked my code where I’m using the drag and drop control to do something similar, and I also cannot get that specific event to register.

I found that I was able to use the NiftyPrimaryMouseClickEvent to catch clicks on my draggable (since a drag event would always be started by clicking the draggable anyways) , so I’ve been using that as a subsitute for the DragStartedEvent and it’s been working well so far.

 @NiftyEventSubscriber(pattern = "draggable.*")
     public void draggableLeftClick(String id, NiftyMousePrimaryClickedEvent event) {

#4

Thanks I switched it to DraggableDragStartedEvent but now I’m getting an exception IllegalAccessException: Class de.lessvoid.nifty.NiftyEventAnnotationProcessor$Subscriber can not access a member of class mygame.Main$1 with modifiers "public". Any ideas on why?


#5

I’m not sure, I can’t get my draggable control or the paired NiftyPrimaryMouseClickEvent code to throw that error and it looks like the methods in your code are all public so I’m not sure where that error may be coming from.

The only other difference is that I’m creating my Draggable Control in XML layout as opposed to the java control builder syntax you’re using, but I don’t think that should make a difference


#6

Although I did just notice that the way you’re setting up your screen controller is different than the Class I’m using as a comparison.
I don’t have any experience with the NiftyBuilder Java Syntax since I use XML layouts for the menu with a paired class that Implements ScreenController.


#7

Yes I thought that could be a reason for it as well. I was just working in a test project trying things out. I created a project for the game and am working on the main menu now. I’ll be using a full on Java class when I get to this point in the game so we will find out if that was the problem.