Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperLicht
Drag and drop - how to start?

kingkohle
Registered User
Quote
2011-03-24 15:27:47

Hi,

I am trying to pick up a (copy of a) billboard and drag it in a 2D-Overlay section. Can you give me a hint how to start so I have a ruff idea how this is working..

Thanks very much.
KK


kingkohle
Registered User
Quote
2011-03-24 17:09:48

Particulary I dont know how to change this code for keyboard-input into mouse input for dragging:
 document. = function(event)
{
var key = String.fromCharCode(event.keyCode);

// when pressed 'L', move the cube scene node a bit up
if (key == 'F' && cubeSceneNode)
cubeSceneNode.Pos.Y += 5;

// when pressed 'G', move the cube scene node a bit down
if (key == 'G' && cubeSceneNode)
cubeSceneNode.Pos.Y -= 5;

// we need to call the key handler of the 3d engine as well, so that the user is
// able to move the camera using the keys
engine.handleKeyDown(event);
};
The "if" section is giving me a hard time. Dont know what to paste there fordragging by mouse..


niko
Moderator
Quote
2011-03-24 17:57:49

It's basically the same, you have on MouseDown and on MouseMove events. In the MouseDown, store which node you are over and if you want to move it, in the mouseMove, move it if you clicked a node before you want to drag. If an on mouseup event happens, that means to stop dragging.


kingkohle
Registered User
Quote
2011-03-24 19:22:26

Thanks.. And sorry I am too stupid for this^^ I wish there would be an doc about it, as it is a FAQ.
I would write a doc for you once you could help me out to get it running.. Maybe by a deeper hint?! Reg


niko
Moderator
Quote
2011-03-25 11:09:57

Deeper Hint? Not sure what you want more. :) Ok, some code (didn't try it out, but it should work like this):


var engine = startCopperLichtFromFile('3darea', 'copperlichtdata/DnD.ccbjs');

var cubeSceneNode = null;
var canvas = document.getElementById('3darea');

engine.ingComplete = function()
{
var scene = engine.getScene();
if (scene)
{
cubeSceneNode = scene.getSceneNodeFromName('yourBillboard');

scene.setRedrawMode(CL3D.Scene.REDRAW_EVERY_FRAME);
}
}


var draggingNode = null;
var distance = 0;

canvas.on mousedown = function(event)
{
engine.handleMouseDown(event);

var pos3d = engine.get3DPositionFrom2DPosition(engine.getMouseX(), engine.getMouseY());
var cam = engine.getScene().getActiveCamera();

var startLine = cam.getAbsolutePosition();
var endLine = startLine.add(pos3d.substract(startLine).multiplyWithScal(2000));

if (cubeSceneNode.getTransformedBoundingBox().intersectsWithLine(startLine, endLine))
{
draggingNode = cubeSceneNode;
distance = startLine.getDistanceTo(cubeSceneNode.Pos);
}
else
draggingNode = null;
}

canvas.on mousemove = function(event)
{
engine.handleMouseMove(event);

if (draggingNode == null)
return;

var pos3d = engine.get3DPositionFrom2DPosition(engine.getMouseX(), engine.getMouseY());
var cam = engine.getScene().getActiveCamera();
var campos = cam.getAbsolutePosition();

var vect = pos3d.substract(campos);
vect.setLength(distance);

draggingNode.Pos = vect.add(campos);
draggingNode.updateAbsolutePosition();
}

canvas.on mouseup = function(event)
{
engine.handleMouseUp(event);


draggingNode = null;
}

(note: some spaces inserted intentional in the function names, because this forum removes this code otherwise. Really need to fix this soon.)

I didn't test that code so there might be bugs and typos, but it basically works like this.


kingkohle
Registered User
Quote
2011-03-25 12:18:30

Legendary!! You are the man! I owe you a lot.. I dig into it. Thanks


kingkohle
Registered User
Quote
2011-03-28 17:52:35

Cool!! I got it running without any problems..

Now, how could I drag several billboards? Since it picks a scenenode by its name ("yourbillboard") - I simply renamed all the others to this name as well.. But it doesn´t do the job. It lets you pick the very first node which was named like that and ignores all the others :(

Adding another line to getSceneNodeFromName + altering the name doesn´t work either..
I wish dragging could be set in the behaviour Err.. and dropping of course


kingkohle
Registered User
Quote
2011-03-29 12:06:33

Hi..

any idea how to drag several billboards? Anyone?

Its this line where to choose which node is dragged.
cubeSceneNode = scene.getSceneNodeFromName('yourBillboard');

Giving all the other nodes the same name wouldn´t work. I need a way to tell several nodes to be dragged.. Can I tell a whole folder in the SceneGraph to be dragable? This would be what I want^^ Reg


niko
Moderator
Quote
2011-03-29 16:48:56

Well, you need to program this. People won't write your program for you :) I suggest to use an array and to do the code above in a loop for all billboards you are interested in.


kingkohle
Registered User
Quote
2011-03-29 18:34:41

You are right^^ Thanks for the hint. I´ll be trying to solve this with some work-mates. As they are busy doing other stuff, we keep looking for a skilled C++/JS-coder.. Anyone want a job? Overseas is no problem^^

Thanks so far, Niko. I´ll get back to you.. Reg


kingkohle
Registered User
Quote
2011-03-29 18:39:13

Btw: Is there any chance to fire this dragging-script from the behaviour of a scenenode? So that it is taking its name by default and fires the script this way?!

Just an idea.. Regards


niko
Moderator
Quote
2011-03-31 06:50:48

Hm, would be an idea, but I don't think this will make it in the near future, because it is a very special feature and not much requested.


Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "Inte?national" (you are not logged in)


Text:

 

  

Possible Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Image [img]http://www.example.com/image.jpg[/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons


   






Copyright© Ambiera e.U. all rights reserved.
Privacy Policy | Terms and Conditions | Imprint | Contact