Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (600)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [libgdx] Drag and drop with scene2d tree  (Read 831 times)
0 Members and 1 Guest are viewing this topic.
Online atombrot
« Posted 2014-05-22 14:20:08 »

I'm working an an editor for our game. A main component of the editor is a tree, that displays nodes.

The editor must be able to reorder nodes in the tree or move entire branches onto a different parent node. To do this I see two possibilities:
1. Implement it with drag and drop, so the user can just drag a node and move it to the appropriate position.
2. Add up/down buttons to the nodes for reordering and add a new button to change the parent of a selected node.

While I would want to do it with drag and drop, I don't want to spend too much time on this (we need to concentrate on the game itself, this would simply be a nice to have). Does the scene2d tree actor support drag or can someone point me into the direction on how to do it, so I can see if it is too much effort to do?

I have seen that LibGDX has a DragAndDrop class. From what I see in the DragAndDrop test ( https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests/src/com/badlogic/gdx/tests/DragAndDropTest.java ) I could do something like this:
- Create empty nodes (drop target) around normal nodes in the tree. These are used to preview where the node will be inserted to. (I created an image to illustrate this http://imgur.com/rcXDcOi . The red lines represent those drop target nodes).
- When a node is dragged, remove it temporary from the tree (or hide it).
- When the drag is released, a check needs to be made if the target is a valid position. If so then move the node to that position. If the position is invalid, the node is put back at the original place.
- When moved, some drop targets need to be added / removed.

Am I completely on the wrong track, or would this be the way to go?
Offline Nate

« JGO Bitwise Duke »


Medals: 158
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #1 - Posted 2014-05-23 07:31:42 »

Use DragAndDrop, add the tree as both a source and target. In the source, use the selected nodes for what to drag (can select the node under the mouse if nothing is selected). In the target, use Tree#getNodeAt(float y) to determine where the drop would happen. If getNodeAt is null and the mouse is over the tree, you know you are after the last visible node. Position an actor that is a sibling to the tree if you want to show where the drop will insert.

Online atombrot
« Reply #2 - Posted 2014-05-23 15:07:49 »

Hey thanks very much, I got it working now how I wanted.

In the source, use the selected nodes for what to drag (can select the node under the mouse if nothing is selected).

Here I don't use the selection because the selection is only updated after the drag started. Means if I select the third node but then go with the mouse over the second node and start dragging, still the third node would be dragged, which is a bit strange.

Other than that it worked great for changing the parent. I needed to add some more code to enable the reordering.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Nate

« JGO Bitwise Duke »


Medals: 158
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #3 - Posted 2014-05-23 15:22:35 »

1  
2  
3  
4  
// Select node under mouse if not over the selection.
Node overNode = tree.getNodeAt(y);
if (overNode == null && tree.getSelection().isEmpty()) return null;
if (overNode != null && !tree.getSelection().contains(overNode)) tree.getSelection().set(overNode);

Online atombrot
« Reply #4 - Posted 2014-05-23 15:36:38 »

Nice, thanks. This is a better than my solution, as it selects the hovered node, when it is not selected yet. Definitely better from a user perspective, as the user now sees clearly which node he is moving.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (29 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (40 views)
2014-12-09 22:41:13

BurntPizza (75 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (50 views)
2014-12-03 16:27:13

CopyableCougar4 (45 views)
2014-11-29 21:32:03

toopeicgaming1999 (113 views)
2014-11-26 15:22:04

toopeicgaming1999 (100 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!