Hi !
Featured games (85)
games approved by the League of Dukes
Games in Showcase (636)
Games in Android Showcase (178)
games submitted by our members
Games in WIP (686)
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 1581 times)
0 Members and 1 Guest are viewing this topic.
Offline 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 ( ) 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 . 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: 166
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.

Offline 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: 166
Projects: 4
Exp: 14 years

Esoteric Software

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

// 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);

Offline 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.

Dwinin (63 views)
2015-11-07 13:29:08

Rems19 (77 views)
2015-10-31 01:36:56

Rems19 (69 views)
2015-10-31 01:32:37

williamwoles (104 views)
2015-10-23 10:42:59

williamwoles (91 views)
2015-10-23 10:42:45

Jervac_ (105 views)
2015-10-18 23:29:12

DarkCart (132 views)
2015-10-16 00:58:11

KaiHH (115 views)
2015-10-11 14:10:14

KaiHH (153 views)
2015-10-11 13:26:18

BurntPizza (167 views)
2015-10-08 03:11:46
Rendering resources
by Roquen
2015-11-13 14:37:59

Rendering resources
by Roquen
2015-11-13 14:36:58

Math: Resources
by Roquen
2015-10-22 07:46:10

Networking Resources
by Roquen
2015-10-16 07:12:30

Rendering resources
by Roquen
2015-10-15 07:40:48

Math: Inequality properties
by Roquen
2015-10-01 13:30:46

Math: Inequality properties
by Roquen
2015-09-30 16:06:05

HotSpot Options
by Roquen
2015-08-29 11:33:11 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‑
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!