Detect drop target with HammerJS - drag-and-drop

I'm using HammerJS to abstract drag, scale and rotate for desktop and mobile. All I'm missing is drop. How do I define an element as a drop target that throws an event when another element is dragged over or dropped onto it? Something like jQueryUI's droppable that will work with HammerJS?
Thanks.

Just solved that today.
On dropping object, set a global "look for me" flag. and then setTimeout() to shortly (say 30ms) set it back to false.
Hide (and move) the dropped object, putting it back after a short time allowing the onMouseOver and onMouseOut events to trigger on the drop target object.
Watch those events for the "look for me" flag.
I haven't really tried it much yet.
(My first post.)

The only one fast way for detect drop target element is to use
document.elementFromPoint
Like this:
function dragEnd($event){
var targetEl = document.elementFromPoint($event.gesture.center.pageX, $event.gesture.center.pageX);
your code
}

Related

I need to create a Script in Unity 3D that change the text when I click a key button (1,2,3,4), then the text change

Example: A NPC greet you, and you have multiple choises that are questions for the NPC and if you want to know one answer of this questions, you click one button (1,2,3,4). Then the text change and give you the answer of the question.
I'm going to assume you have already set up your GUI and NPC Script.
Getting when a key is pressed is pretty simple in Unity: Use UnityEngine.Input
Example of use:
if(Input.GetKeyDown(KeyCode.Alpha1)) {
//do something
}
This is a basic example.
Input.GetKeyDown returns true only when the key is pressed at that specific moment of checking (the frame). KeyCode.Alpha1 is the key for '1' at the top of alphanumeric keyboards.
To get when the key is held down, for jumping or running, use Input.GetKey instead. This will be true if the key is held down at that instance, instead of only one at the beginning.
To get when the key is released, use 'Input.GetKeyUp'. This only runs once the key is let go.
For setting the color of a Textbox, use yourTextBox.color. I believe it works with both legacy Textboxes and TextMeshPro.
The way to do what you have is this:
In the Update call, check if an NPC is talking with the player.
If so, then check if the player has pressed 1 ,2, 3, or 4, using Input.GetKeyDown. (To make it nicer looking, have each option in a list and use the index of the result. KeyCode is an enum, so you can save it like any other variable.)
Call some function that returns the response to the answer. Return a string or something.
Override the text in the Textbox with the answer, and use yourTextBox.color to set the color as you wish.
This should cover it. I may have some typos or errors, but the basic principals should work. Also, make sure you implement it in a way that makes it easier to edit later. You don't want to write code that can't be added on later, trust me...

In Scratch, how can the clone A of Sprite2 detect if it's touching another clone B of the same Sprite2 and delete both clones?

This is for a mini program in Scratch.
What I want to do:
When a clone A of Sprite2 touches another clone B of the same Sprite2, both clones are deleted, but any other clone stays existing and executing.
What I have tried:
I have tried finding if there was an option like "touching another clone", but I haven't found it.
I also tried putting a sensor that detects if a clone is making contact with its same color (if[color{red}is touching color{red}] then) => (delete clone), but it didn't work, it entered an infinite loop where the page begun to get choppy so I deleted that.
I even made the function outside the sprite (where I could find the option (touching [sprite2] ?)) and then moved the function inside sprite2 but it ignores that new inserted function.
Do you know how this could be done?
As seen in your screenshot, the dropdown list of the 'touching' block shows a list of all sprites in the project (in this case Sprite1) except the current sprite (Sprite2).
Fortunately, it is possible to have a 'touching Sprite2' block within Sprite2.
It's a bit of a hack, but it works. Follow these steps:
Go to the code area of Sprite1. (Any sprite will do except Sprite2. If Sprite2 is the only sprite in the project, add a dummy sprite; you can safely remove it after step 4.)
From the 'sensing' group, drag a 'touching' block onto the code area.
Open the block's dropdown and select Sprite2.
Drag the block from the code area to Sprite2 (as seen in the sprite collection, bottom right).
Go to Sprite2. Notice there's a 'touching Sprite2' block there. Drag it to the script where you need it.
When using this to make clones disappear as they collide, you may notice that only one clone will disappear, and the other will survive.
This is a 'race condition': simultaneous events never happen exactly at the same time.
The first clone to detect the collision deletes itself, thereby erasing the incident before the other clone has had a chance to detect it.
In most cases, this can be fixed by adding a 'wait 0 seconds' block immediately above the 'delete this clone' block.
Waiting zero seconds may seem like a no-op, but it actually has effect:
it gives other threads (scripts, clones, sprites) the time to catch up with the current one.
Live demo:
https://scratch.mit.edu/projects/704352756/

KineticJS: Animate an item to go back to initial position when not dropped in drop target

I made an application which performs drag and drop of different items: images and shapes. I limited the drop target to a specific layer: rightLayer in my case using a simple test with if ... else. Everything works great, except that I want to make an item revert back to its original position in the leftLayer when it doesn't attempt the borders of rightLayer (just like jquery, but in kineticJS). Or just disappear instantly.
Here's a JSFIDDLE . For a better understanding, try this use case:
drag the rectangle,
drop it right before the grid,
click on an item from the left layer.
You can self-destruct any clone dropped other than in the dropzone with a test in dragend.
A Demo: http://jsfiddle.net/m1erickson/2T68g/
clone1.on("dragend",function(){
// destroy this clone if dropped outside the dropzone
if(this.x()<dropzone.x()){
this.destroy();
layer.draw();
}
});

Hide cursor or have custom cursor in Windows 8 Metro

I want to know if there is a way to hide cursor in Windows 8 Metro mode. I found
this answer, but then I don't know how to obtain the
"unique resource id" for the second parameter of the cursor constructor (below).
Window.Current.CoreWindow.PointerCursor =
new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.Custom, 1);
EDIT: Forgot to mention that I need to handle mouse events normally so the answer below of setting cursor to null will not suffice.
Set the cursor to a custom cursor but make it just be blank...this allows you to track it like being a normal cursor.
You can set the PointerCursor object to NULL. As soon as you move over something like a text box, it will reset it back though. So you probably need to handle mouse over events on various controls, to hide it. This all depends on your complete scenario tough.
Also, before setting it to NULL, you can save the value of the property (PointerCursor) and then when you're done, set it back.

SilverLight 4 DataGrid & MVVM: Using SelectionChanged trigger to check checkbox, but NotifyPropertyChanged causes crash

I have a DataGridCheckBoxColumn in my DataGrid which is to indicate the rows the user has selected. I want the checkboxes to be checked/unchecked with a single click. Making the column editable (i.e. IsReadOnly="False") means the user has to click twice (first click just selects the row, 2nd click changes the checkbox), so I decided to set/clear the property the column is bound to in the view model code in response to the SelectionChanged trigger firing.
Setting/clearing the property works fine, however as soon as I call NotifyPropertyChanged("name of collection the grid is bound to") to get the view to show the change, this causes the SelectionChanged trigger to fire again. This loops about 10 times until an exception is thrown.
If I remove the call to NotifyPropertyChanged, the SelectionChanged trigger fires once, but of course I don't see any change in the UI. The collection is a PagedCollectionView if this makes any difference.
How can I get this to work? Note - I am using MVVM pattern, so everything is done with bindings to View Model (no code behind).
Thanks
Sounds like you have a infinite loop by design.
but try using the selectionchanging instead of selectionchanged,
or put a isloading flag in your viewmodel and dont call the inotify if the isloading is true
I found a very simple solution that doesn't involve triggers or code behind. See: Silverlight single-click checkbox DataGrid columns
It seems to work by using a column template, but only providing the CellEditingTemplate and no CellTemplate.