Dragged Image Location - drag-and-drop

I am dragging an image to an NSView.
Works fine, except that if I insert the dragged image at the received NSDraggingInfo.draggingLocation, the image is suddenly offsetted from its current drag dimmed drawing.
Indeed, the user has taken the image with the cursor by clicking "somewhere" in the image, there is therefore an offset between the bottom left corner of the image, and the cursor itself. Then, the user drags, and drops the image. If I position the dropped image at the cursor position, the image suddenly shifts...
I tried to use NSDraggingInfo.draggedImageLocation, but the result is even worse. NSDraggingInfo.draggedImageLocation seems to contain an position information that is not at all what Apple describes in the doc.
How can I get the bottom left (or other) point of the dragged image so that I can have a smooth drop and insertion ?
Note: this is a cross-app drag & drop, so I don't have any information at the time the user starts the drag.
Edit: added some information as per comments
The image shifts only when dropped
Here is the code of the performDragOperation func
public func performDragOperation(_ sender: NSDraggingInfo) -> Bool {
let pboard = sender.draggingPasteboard
if let images = pboard.readObjects(forClasses: [NSImage.self],
options: nil) as? [NSImage] {
for image in images {
self.imageDropDelegate?.receive(image: image,
at: sender.draggingLocation)
print("drop point : \(sender.draggingLocation)")
print("image point : \(sender.draggedImageLocation)")
}
}
return true
}
The delegate just creates an image view at the point it is given. The problem is that sender.draggedImageLocation is the poirnt where the cursor is when the mouse up event is detected, which is different from the bottom left corner of the dragged image.
Apple doc says sender.draggedImageLocation should be what I am after, but the prints says:
drop point : (413.18212890625, 458.5313720703125)
image point : (-1438.0, 969.0)
drop point : (439.2493896484375, 443.3453369140625)
image point : (-1438.0, 969.0)
The drop point is indeed the position of the cursor when I release the mouse (drop).
But image point is just completely un-understandable to me.
And I checked that sender.draggingFormation is 1 (so 'none')

Related

Unity 5: how to zoom and translate an object from a grid layout to the center of the screen

I'm trying to create a scroll grid view in which every cell object is tapable.
When a cell object is tapped I want to scale and traslate it to the center of the screen and render it above other cells.
I was able to make it tapable and scale it in its position. Now I want to move the cell object to the center of the screen and render it above other cells.
I've tried many solutions but none of them works.
This is my hierarchy:
This is the grid in normal state:
This is the grid when a cell was tapped:
I'm populating the grid from a C# script dynamically.
void Populate()
{
GameObject cardContainerInstance, cardInstance;
foreach (var c in cardsCollection.GetAll())
{
if (c.IsOwned)
{
cardContainerInstance = Instantiate(cardContainer, transform);
cardInstance = cardContainerInstance.transform.Find("Card").gameObject;
var cardManager = cardInstance.GetComponent<CardManager>();
cardManager.card = c;
cardManager.AddListener(this);
}
else
{
Instantiate(cardSlot, transform);
}
}
}
public void OnCardClick(GameObject cardObject, Card card)
{
Debug.Log("OnCardClick " + card.name);
if (openedCard != null) {
if (openedCard.Number == card.Number)
{
CloseCard(openedCardObject);
}
else
{
CloseCard(openedCardObject);
OpenCard(cardObject, card);
}
}
else
{
OpenCard(cardObject, card);
}
}
void OpenCard(GameObject cardObject, Card card)
{
//cardObject.GetComponent<Canvas>().sortingOrder = 1;
var animator = cardObject.GetComponent<Animator>();
animator.SetTrigger("Open");
openedCard = card;
openedCardObject = cardObject;
}
void CloseCard(GameObject cardObject)
{
var animator = cardObject.GetComponent<Animator>();
animator.SetTrigger("Close");
openedCard = null;
openedCardObject = null;
}
I can't figure out how to move the cell to the center and render it above others.
Note that all is animated using an animator attached to the object itself.
Could anyone help me please? Thank you very much!
EDIT: more details
All cell object have the following hierarchy:
where:
CardContainer is an empty object added to use animator on Card child object
Card is the object itself that has a script, a canvas renderer and an animator
StatsImage is the object that slide out when the card is tapped
Image is a calssic UIImage with Image script, Shadow script and canvas renderer
Other component are simple texts.
EDIT: fix in progress
Trying to apply this suggestions I was able to manage the rendering order (as you see on the image below) but it seems that prevent touch events to be detected on the game object.
I've added a GraphicsRaycaster too and now the bottom horizontal scroll view scrolls again but only if I click and drag a card.
Moreover, with the GraphicsRaycaster, the main grid card still are not clickable and it's possible to open the card only if it is behind the bottom panel (if I click on the red spot in the image below the card behind the panel receives che click)
This is the CardContainer at runtime(note that I'm attaching new Canvas and GraphicsRaycaster on the CardContainer, which is the "root" element):
You didn't clarify whether you are using a sprite renderer or some other method but here is an answer for each.
Sprite renderer:
this the simple one. In each sprite renderer, there is a variable called "sortingOrder" in script and "Order in layer" in the inspector. sprite renderer with sorting Orders that are higher is rendered first. All you would need to do is call:
cardObject.GetComponent<SpriteRenderer>().sortingOrder = 1;
when you click the card, and
cardObject.GetComponent<SpriteRenderer>().sortingOrder = 0;
when you unclick it. I hope that makes sense!
Other Method:
this one is a bit harder and I would suggest that you switch to sprite renderers and it will be much easier and more stable down the road, but I can understand if you have already written a lot of scripts and don't want to go back and change them.
Anyway, all you will need to do Is create two layers: cardLower and cardUpper. then create a new camera and call it topCamera. now under the top camera object in the inspector, change the culling mask (it's near the top) and make sure cardUpper is selected. then change the Clear flags (first one) to "Don't Clear" finally change the depth to 0 (if that doesn't work change it to -2). Now objects in the cardUpper will always be rendered above everything else. You can change the layer through script with
cardObject.layer = "cardUpper"
or
cardObject.layer = "cardLower"
I hope that helps!
Ok, so its pretty simple. So you are going to want to add another canvas component to the game object, and check the override sorting to true. Then use
cardObject.GetComponent<Canvas>().sortingOrder = 1;
to place it in the front and
cardObject.GetComponent<Canvas>().sortingOrder = 0;
to put it in the back.
you are also going to need to put a GraphicsRaycaster on to each of the cardObjects
Ignore my other answer about sprite renderers, they are not needed here

How to clone a node to appear exactly on top in a different pane

In my (full-screen) program I have several items displayed in a layout. What I would like to do is that when I tap or click on a item is to show a full screen editor. The idea is to have transition from the item shown to the editor.
When the editor is shown I first display a semi-transparent view on top of the stackpane, underneath the other items are still visible. The selected item should 'move' to the upper layer, and then start to transform to its editor shape. Closing the editor should of course be a reverse course of actions.
The first attempt was to move the from its container to the upper layer, but JavaFX complained about the bound properties. I rather not have to code a whole bunch of bind/unbinds, so I assumed this was not the route.
My next attempt was something like this:
private volatile FullScreenService fullscreenService;
private void showFullPhoto(MouseEvent e) {
FullScreenService service = fullscreenService;
if (service != null) {
try {
Node node = (Node) e.getSource();
ImageView photoView = (ImageView)node.lookup("#image");
Image image = photoView.getImage();
Pane pane = new Pane();
pane.setPrefSize(1920d, 1080d);
ImageView copy = new ImageView(image);
copy.setRotate(node.getRotate());
node.setVisible(false);
Bounds bounds = photoView.localToScene(photoView.getBoundsInLocal());
copy.setLayoutX(bounds.getMinX());
copy.setLayoutY(bounds.getMinY());
pane.getChildren().add(copy);
service.requestFullScreen(pane);
} catch (InUseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
The idea is to hide the original view and 'clone' the part that should be shown (and then transitioned). In this case I extract the image part out of a photo (that also has a border and caption), and make a copy on the editor screen.
However the location of the image does not match the original location exactly, it is off by a couple of pixels, making the image 'jump'.
I've created the pane to be exact the size of the screen, so I thought I could assume scene coordinates are the same as the new node's coordinates.
Abstracting my question: given two overlapping panes, how to make sure a node is exactly on top of another node?

gtkmm Gtk::Layout negative child position not setting its absolute position

The description of Gtk::Layout says
Infinite scrollable area containing child widgets and/or custom drawing
I put a Gtk::Layout inside a Gtk::ScrolledWindow, gave the Gtk::Layout a size of 400x400 and then placed a Gtk::Button at (450, 450), but the scrollbars only appeared until the scrolled window was resized to 400x400, not (450+button-width x 450+button-height). So I had to move the bottom-right corner of the layout by connecting a handler to button's signal_size_allocate() signal and manually call Gtk::Layout::set_size() function to resize it up to button's boundary. Now the scrolled window shows scrollbars to reach the button. Up to this everything was OK.
class LayoutWindowTest : public ApplicationWindow {
ScrolledWindow sw;
Layout l;
Button b;
public:
LayoutWindowTest()
: l(), b("test") {
set_title("layout test");
set_size_request(400, 400);
sw.set_hadjustment(l.get_hadjustment());
sw.set_vadjustment(l.get_vadjustment());
l.set_size(400, 400);
sw.add(l);
add(sw);
// b.get_allocation() won't work because button has not been allocated a
// size yet
b.signal_size_allocate().connect(
[this](Allocation& btn_sz) {
l.set_size(btn_sz.get_x() + btn_sz.get_width(),
btn_sz.get_y() + btn_sz.get_height());
});
l.put(b, 450, 450);
show_all_children();
}
virtual ~LayoutWindowTest() {}
};
Initial window showing scrollbars and then scrolled to bottom-right corner to show button
Now I need to do the same thing for the top-left corner, i.e. put the button out of visible boundary of scrolled window and then use the scrollbars to reach the button. I tried putting the button at (-10, -10), but unlike the previous case I can't set layout's top-left corner with code by resizing it, so can't move the layout's scrollbars. When I resize the scrolled window manually by its top-left corner the button moves towards top-left with it instead of staying there. How to remedy this situation?
Initial window with button at (-10, -10) and no scrollbars, tried resizing the window by top-left handle but button moves with it
In short, how can I scroll in both top-left and bottom-right directions infinitely?

Unity - Raycast hit, wrong collider

Here is my code:
using UnityEngine;
public class InputController : MonoBehaviour {
void Update() {
if (Input.GetMouseButtonUp(0)) {
var hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if (hit.collider != null) {
var e = hit.collider.gameObject;
Debug.Log(e.transform.position.x + ":" + e.transform.position.y);
}
}
}
}
When I click/touch a cell, sometimes the hit is good and the cell is revealed, sometimes the hit is always false (it's like I touch the wrong cell) and sometimes it depends of the position of my click (see images below).
I click the left part of the cell, the console says I click the cell on the left.
I click the right part of the cell, the console says I click the right cell (and reveals it). In this case it's left / right but it can be top / bottom, a corner...
I don't know if my problem is very well explained sorry, and my English is not perfect... Don't hesitate to ask me more details!
Ok I find the problem.
The left cell had a scale of 2 so it overlaps the right cell.
I Guess Here is problem with collider overlaping. you have to just reset all the collider and check it, is it overlaping on any object or not?
you can check colider on gamemode , turn on "Gizmo". so you will see all the collider and check it again is it overlaping on there or not ?

How can i show the image within the circle using qml?

Hi I'm new to blackberry. I want to show the image within the circle using qul.
The easiest way to vignette an image is to overlay it with another image. In Cascades if you create a Container using either DockLayout or AbsoluteLayout you can position multiple ImageView objects on top of one another. The first object will be on the bottom, last on the top, by default. You simply create an image with a transparent viewport of the shape you want. You can scale the image at runtime but you should make it about the size of the largest frame you will need to preserve quality. I used Gimp to create a 786 x 78x pixel image with a white frame and a transparent circle in the middle and saved it to the assets directory as frame.png. I grabbed another image I had easily to hand and create a BlackBerry project with this QML code in an appropriate place:
Page {
Container {
layout: DockLayout {
}
ImageView {
imageSource: "asset:///FusionMap.PNG"
preferredHeight: 360
preferredWidth: 360
}
ImageView {
imageSource: "asset:///frame.png"
preferredHeight: 360
preferredWidth: 360
}
}
}
This is the result: