libGDX DragAndDropText.java is broken, looking for another example - java

I want to drag and drop TextButtons to designated targets. Searching this site, DragAndDropTest.java on libgdx's testing github is cited as an example to to start from. Implementing the flow described in the test, I received this error:
Exception in thread "main" java.lang.NullPointerException
at com.badlogic.gdx.scenes.scene2d.utils.DragAndDrop$1.drag(DragAndDrop.java:111)
at com.badlogic.gdx.scenes.scene2d.utils.DragListener.touchDragged(DragListener.java:61)
at com.badlogic.gdx.scenes.scene2d.InputListener.handle(InputListener.java:62)
at com.badlogic.gdx.scenes.scene2d.Stage.touchDragged(Stage.java:315)
at com.badlogic.gdx.InputEventQueue.drain(InputEventQueue.java:89)
at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Input.update(Lwjgl3Input.java:205)
at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Window.update(Lwjgl3Window.java:390)
at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application.loop(Lwjgl3Application.java:137)
at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application.<init>(Lwjgl3Application.java:111)
at com.b2tclient.lwjgl3.Lwjgl3Launcher.createApplication(Lwjgl3Launcher.java:17)
at com.b2tclient.lwjgl3.Lwjgl3Launcher.main(Lwjgl3Launcher.java:12)
Eventually I just tried to run the DragAndDropTest.java directly and received the same error. Anything from a fixed test, pointing out where I may have gone wrong in trying to run the test, or a different working example of drag and drop to study would all be helpful in moving forward.
Note that I also tried a different path when I struck out here in implementing Lee Stemkoski's DragAndDropActor class. While that did work, the flow I learned from doing so is only good for his BaseActor family, and the work of re-implementing Buttons and such on top of it doesn't seem like the right solution. I'll take from that effort confirmation that drag and drop itself is not broken, just the test.

I've ripped out all of the dragging methods and tracking variables from Stemkoski's DragAndDropActor, and placed them in .addListener() for each item I want to drag within my screen. Now that I've seen my draggables moving around the screen without crashing, I think I'll make a new DragListener extending from InputListener, as that seems like the way to encapsulate the logic (open to feedback). Anyway, it occurs to me that the DragAndDrop class found in the DragAndDropText is never referenced in Stemkoski's work, so he must have implemented his own parallel drag and drop functionality. So I take back what I said about knowing the drag and drop that comes with native libgdx works, however unlikely it would seem that it not.
While this is technically an example of drag and drop that still works compared to the oft-cited test class, I think any answer provided that uses the vanilla libgdx packages should be considered a superior one.

I've done something like that :
public class DragAndDropActorListener extends DragListener {
private Actor actor;
public DragAndDropActorListener(Actor actor) {
this.actor = actor;
}
#Override
public void drag(InputEvent event, float x, float y, int pointer) {
this.actor.setY(event.getStageY());
this.actor.setX(event.getStageX());
super.drag(event, x, y, pointer);
}
}
With actor the object to move. you could also a little Offset for center the object on the mouse

Related

Java swing fake mouse in Runelite for plugin

Heyy, i've been searching the internet for making a plugin for Runelite a client for Oldschool Runescape. I want to make a plugin where i can fight some monsters, basically its a bot. I know there are other clients out there that make it easier to write bot scripts.
My question is how do i make a "Fake" mouse that fires MouseEvent to a specific location on the screen and implement this inside a plugin. I've seen that i can create a new MouseEvent and pass the X and Y value's but i also need to pass a source for that. The source needs to be a component. I Tried using this snippet but "this" doesn't work in a plugin because its not a Component i guess.
I know i could use the class Robot from java.awt to create mouse movement etc but that hijacks my mouse on the pc. Also ofc this is for educational purposes. I just would really like to know how to create a bot, was always facinated by the thought of creating one.
ps: i found this video. i want to create something similar, this is the "fake" mouse i mean. the cross on the window.
this is the code i have at the moment:
package net.runelite.client.plugins.clicktest;
import lombok.extern.slf4j.Slf4j;
import net.runelite.api.Client;
import net.runelite.client.plugins.Plugin;
import net.runelite.client.plugins.PluginDescriptor;
import javax.inject.Inject;
import java.awt.event.MouseEvent;
#Slf4j
#PluginDescriptor(
name = "ClickTestPlugin",
description = "Plugin for testing MouseEvents in Runelite"
)
public class clicktest extends Plugin {
#Inject
public Client client;
#Inject
public MouseEvent mouseEvent;
public clicktest() {
mouseEvent = new MouseEvent(this, MouseEvent.MOUSE_CLICKED,
System.currentTimeMillis(), 0, 10, 10, 1, false);
}
}
Seeing as no one else has gotten back, I'll try to point you in the right direction even though I'm also not sure of a library or way to do it without hijacking the mouse. There are other methods than robot such as a scheduled executor, but it will still hijack your mouse. There are a lot of free repos that use runelite's API to cerate bots and interact with the game, allowing you to not have to worry about clicking at specific coordinates. And using those as reference will help you get where you wanna go!
Some have different methods, and I've seen some that do not hijack your mouse. However, none of those are open source (at least from what I've found). I'll paste some examples here. Like I said at the beginning I'm not super confident in how to create a bot that can override your mouse movements and click independent of your mouse but here are some examples of other's bots.
The first plugin shown below uses this method for example:
executor = Executors.newSingleThreadScheduledExecutor();
executor.schedule(this::simLeftClick, delay, TimeUnit.MILLISECONDS);
AutoPrayFlick - https://github.com/Ganom/ExternalPlugins/blob/master/AutoPrayFlick/src/main/java/net/runelite/client/plugins/externals/autoprayflick/AutoPrayFlickPlugin.java
ItemDropper -
https://github.com/Failed4life/ExternalPlugins/blob/master/ItemDropper/src/main/java/net/runelite/client/plugins/externals/itemdropper/ItemDropper.java
OneClick - This one uses only the API and doesn't require any mouse simulation!
https://github.com/Ganom/ExternalPlugins/blob/master/OneClick/src/main/java/net/runelite/client/plugins/externals/oneclick/OneClickPlugin.java
It may be smart to consider detection if using robot or anything like that, using something like this will help humanize your movements
https://github.com/JoonasVali/NaturalMouseMotion
P.S I recommend looking around github at all the opensource repos to get a feel for how people are doing things, this is how I familiarized myself with the API and it' usage!

How to get mouse state from within an mxj object in Max/MSP

From inside an mxj object, is there any way to determine whether any mouse buttons are currently pressed?
I know that in a Max patch this data can be gathered from a [mousestate] object. I'd like to do the same sort of thing, but from within my Java code.
Any ideas?
I used this to get the mouse position.
public void bang() {
Point p = MouseInfo.getPointerInfo().getLocation();
post(""+p.getX());
}
Getting the mouse state should be really easy with the help of the code above.
After many hours of searching, I couldn't figure out any way to get at the mouse state data from within the mxj object. I'm not sure it's possible. I ended up working out a clunky solution using [mousestate] instead.

Keypressed while a thread is running - how to capture the keys?

I have a JPanel that contains a few other objects that do stuff. I will simplify the example by talking about some circle object (defined by circle class I made), and a square object (similar).
the circle moves randomly around the screen, while the square sits at its place. my intention is to move the square using the arrow buttons.
the current design is to have a thread with a while loop that contains a delay that sets the 'refresh rate' inside its run method.
I'm trying every method I know to capture the arrow keys and move the square around while the ball is running around the screen.
how do I capture keypresses (arrows for the example) so I can know where to move my square to?
I tried implementing keylistener in the jpanel but it didn't work. when I tried to use a KeyEvent in the run, I got an exception.
please save me. :)
EDIT:
Thanks for that info. I would like further help to settle this issue -
lets say I have the following code:
this.getInputMap().put(KeyStroke.getKeyStroke("UP"), "actionName");
this.getInputMap().put(KeyStroke.getKeyStroke("DOWN"), "actionName");
this.getActionMap().put("actionName",
new AbstractAction("actionName") {
public void actionPerformed(ActionEvent evt) {
//dostuff
}
}
);
how do I distinguish between UP and DOWN presses? what do I need to change?
Thanks! I'm a bit of a newbie, I know :)
KeyListener isn't designated for listening in Swing GUI, this Listener was builded for pre_historic AWT Component, these days so far away, use KeyBindings, this example can save your person

Drag and drop with an image

I need to create a drag and drop system in swing where an image of the thing being dragged is attached to the cursor during the drag. In theory this is achieveable with
public Icon TransferHandler.getVisualRepresentation(Transferable t)
but there appears to be a long standing bug (here) that means this method is never called. I know I can do it by implementing my own DnD system with DragSource etc., but does anyone know of an easier workround that will get me what I need?
The method TransferHandler.getVisualRepresentation wasn't supported in java 1.4, I'm not sure if or when it was fixed. To test whether it works in a current version you could adapt this example
In the end I used the old style drag-and-drop to implement what I wanted. However I have no reason to think abrightwell's solution wouldn't work just as well - this was just the best way at the time.
You could Try putting the image on a Jlabel (in the draggesture recognizer)and set its bounds, in the droptargetListener dragover method. Alternately, hows about implementing a Mouse listener (I've not tested this latter method).
I have used the "work around" suggested towards the bottom of the bug report you have listed. It worked well enough for me. Granted I was using this with Mac OS X so I have no idea whether Winderz will support it. It would be nice if they would at least fix it to work like they intended and simply document where it will and won't work... oh well. Good Luck.

Is there a standard way to handle many different options for mouse events in java?

I'm developing a grid based sim game in java, and I was wondering if there is a standard way of doing the following.
I have a panel which is the game panel, and there are many different things which could happen when the panel is clicked. For example, when building a room, there are several stages, where dragging the mouse and left clicking will have different actions.
Right now, the way I have done it, is to use booleans to check what's being built, and then what stage it is at.
Is there any better or standard way of handling something like this? I had a quick google, but as I have said before, people on Stack Overflow always give a better, more relevant, up to date answer.
I consider myself still rather new to java.
Thanks in advance.
You might try looking into something similar to the strategy pattern.
Basically, you start by clicking the room button on your toolbar. The toolbar goes through and tells the grid to use the 'room place' actionlistener. Presumably removing any previous action listener that was listening
The room place actionlistener would in turn implement all the interesting bit of logic for left/right clicking, dragging, etc.
If you have multiple stages to building a room (say, placing doors, then windows, then trap doors); the action listeners would be responsible for handing control off to the next stage: a bit of a finite state machine.
So, start by clicking 'room' button, 'place room' listener is added. Drag out the area you want the room to be, 'place room' modifies the game state, then changes the actionlistener to the 'place windows' listener. Ad infinitum... until you finish.
One very simple (non compilable) example:
class GridPanel extends JPanel
{
void SetMouseListener(MouseListener newListener)
{
for(MouseListener ml : getMouseListeners())
removeMouseListener(ml);
addMouseListener(newListener);
}
}
class ControlPanel extends JPanel
{
GridPanel gameGrid;
void OnRectangleButtonClicked(some stuff)
{
gameGrid.SetMouseListener(new PlaceRoomListener(gameGrid));
}
}
class PlaceRoomListener extends MouseAdapter
{
GridPanel gameGrid;
//constructor, etc
void OnClick(mouse event)
{
gameGrid.doCoolStuff();
gameGrid.SetMouseListener(new PlaceTrapDoorListener());
}
}
//etc
Now, that non-compilable example aside, Pyrolistical does have a point: you generally don't want to combine your game model and graphic interface into one single glob of classes. You want the model separated from the GUI, and to manipulate it through a well defined interface.
(Also, there are probably better methods for going about removing the mouse listener then just randomly removing all listeners... I was in a bit of a rush, sorry)
It sounds like you need to define your game model/state and keep it separate from your mouse actions.
Are you using MVC?

Categories