Opposit of CGDisplayMoveCursorToPoint - objective-c

I am looking for a way to retrieve the cursor position to later set the position back with CGDisplayMoveCursorToPoint. I can't find any function like CGDisplayGetCursorPosition in Quartz Display References.
Does anybody know how to retrieve the (absolute) position of the cursor in Mac OS X?

Use one of the answers at Cocoa: Getting the current mouse position on the screen to get the mouse position in global coordinates.
To call CGDisplayMoveCursorToPoint, get the display containing that position using CGGetDisplaysWithPoint, and then use the rect returned by CGDisplayBounds to convert the global point to a screen-local point.
Or just use CGWarpMouseCursorPosition instead of CGDisplayMoveCursorToPoint, since CGWarpMouseCursorPosition just takes a global point and no display.

Related

I have a question about a YT tutorial because I wanna customize it a little

in this video, https://youtu.be/klBvssJE5Qg I shows you how to spawn enemies outside of a fixed camera. (this is in GDscript by the way) How could I make this work with a moving camera? I wanna make a zombie fighting game with a moving camera and zombies spawning outside that.
I would really appreciate help with this.
I've tried researching on the internet about how to do it, but I just didn't find it.
N/A..................................
After looking at the video, I see they are using this line to spawn:
Global.instance_node(enemy_1, enemy_position, self)
This suggest to me a couple thing:
The position is probably either relative to the self passed as argument or global.
There must be an Autoload called Global that I need to check to make sure.
And the answer is in another castle video.
In the video Godot Wave Shooter Tutorial #2 - Player Shooting we find this code:
extends Node
func instance_node(node, location, parent):
var node_isntance = node.instance()
parent.add_child(node_instance)
node_instance.global_position = location
return node_instance
And thus, we are working with global coordinates global_position. Thus enemy_position is used as global coordinates.
Ok, instead of using enemy_position as global coordinates we are going to use it as local coordinates of the Camera2D (or a child of it). Which means you need a reference to the Camera2D (which I don't know where do you have it).
You could make your code in a child of the Camera2D, or take the transform of the Camera2D using a RemoteTransform2D. Either way, you could then work in its local coordinates. Thus you would do this:
Global.instance_node(enemy_1, to_global(enemy_position), self)
Or you could have a reference by exporting a NodePath (or in the newest Godot you can export a Camera2D) from your script and set it via the inspector. So you can do this:
Global.instance_node(enemy_1, camera.to_global(enemy_position), self)
Where camera is your reference to the Camera2D.
In the following section of Arena.gd:
func _on_Enemy_spawn_timer_timeout():
var enemy_position = Vector2(rand_range(-160, 670), rand_range(-90, 390))
I believe you can add the X and Y coordinates of the camera to their corresponding random ranges in the enemy position Vector2. This will displace the enemy depending on where the camera is currently located.
You can get the position of the camera with this:
get_parent().get_node("Name of your camera").position
When this is all put together:
func _on_Enemy_spawn_timer_timeout():
var enemy_position = Vector2(rand_range(-160, 670) + get_parent().get_node("Name of your camera").position.x, rand_range(-90, 390) + get_parent().get_node("Name of your camera").position.y)
Keep in mind that you might need to displace the values in the following while loop as well. I hope this helps.

Nested list (tree) - how to nest elements by dragging to the right?

What would be the approach with Vue.Draggable/SortableJS in order to achieve the functionality as shown in this animated gif?
The default behavior of Sortable for nesting is to drag the element up to the above element until the mouse reaches emptyInsertThreshold pixels from the drop zone of the above element but I would like to be able to nest elements by dragging them to the right. Same for un-nesting.
I have set emptyInsertThreshold to 0 to disable the default behavior and now when I drag the element to the right I get the following events fired: clone and start (in that order).
But how do I:
Can get notified when the mouse has traveled the pre-defined distance to the right?
Inform Vue.Draggable that the ghost element should be nested as a child to the element under which I am doing the horizontal movement?
You can get the mouse position given in the start event under event.originalEvent and in the onMove event using the originalEvent argument (2nd argument). I would track the % that the mouse is past the end of the list item above the dragged item, using: (clientX - aboveItemStart) / aboveItemWidth. Then when it reaches 10% or so, change the DOM directly using event.dragged. Not sure how you would modify it to work with Vue.Draggable... you might need to edit the Vue.Draggable source.

How to iterate over a pandas data frame when using the basemap function to check the result

I am trying to write apython script that will do a geometric transformation for the long/lat values, namely the rotation. However, I want to resulted long/lat to preserve the location, for example if the otiginal coordinate is in the land I want the rotated one to be also in land. Therefore, I have used basemap
I wrote the following script and it works fine, till I tried to add a while loop. My objective from adding the while loop is to compare the original value with the rotated one and if they dont match, the rotation angle will change till they match. I will show my concept without adding the while loop first:
def rotation(dfc):
# Rotation
choose a rotation angle that will match the location of the rotated
coordinates, and set the result to 1 if they match
while row['result']=0
choose another rotation angle
do the rotation again
check the result of the rotated points
if still they dont match stay in the loop and choose another alpha
if not break and go to the next row
I know it needs simple steps, but I am not able to figure out how to add the loop for row wise functions in dataframes
Actually this problem can be solved without using df.iterrows.
I just initiated a variable to be equal 0 and then added a while loop. So my pseudocode looks like :
counter=0
while counter != len(df['col1'])
choose new random variable
do the calculation using dfc.apply()
re-calculate counter

Unity: Texture2D ReadPixels for specific Display

Unity has had support for multiple display outputs for a while now (up to 8).
With the ReadPixels function, you can specify an area to read from, and an origin coordinate. But I cannot specify a display number to perform the read on.
I need to be able to read pixels from a specific display (1-8) with a specific area and origin point.
How can I do this, please?
You can achieve ReadPixels for a specific screen/display. You have to do the following:
Before I start, I assume you have a number of cameras which are each rendering to a different display. The cameras must not have a RenderTexture attached to them in order to output to a display.
Define a function which does the following:
Assign the desired camera a temporary RenderTexture
Use RenderTexture.active = *temporary render texture* to make the currently active rendertexture equal the temporary one you just created
Use ReadPixels to read in pixels into a temporary texture2d using an appropriate Rect. This will read from the currently active RenderTexture
Call Apply() on the texture2d
Set the RenderTexture.active and camera RenderTexture to null
The idea is that ReadPixels works on the currently Active RenderTexture.
The code should look something like this:
outputCam.targetTexture = outputCamRenderTexture;
RenderTexture.active = outputCamRenderTexture;
outputCam.Render ();
tempResidualTex.ReadPixels (screenRect, 0, 0);
tempResidualTex.Apply ();
RenderTexture.active = null;
outputCam.targetTexture = null;

Cursor position in a UITextView

I am looking for a non private way to find the position of the Cursor or Caret (blinking bar) in a UITextView preferably as a CGPoint.
There may be a question like this already but it does not provide a definitive way for doing it.
And,
I do not mean the NSRange of the selected area.
Just got it in another thread:
Requires iOS 3.2 or later.
CGPoint cursorPosition = [textview caretRectForPosition:textview.selectedTextRange.start].origin;
Remember to check that selectedTextRange is not nil before calling this method. You should also use selectedTextRange.empty to check that it is the cursor position and not the beginning of a text range. So:
if (textview.selectedTextRange.empty) {
// get cursor position and do stuff ...
}
Pixel-Position of Cursor in UITextView
SWIFT 2.1 version:
let cursorPosition = infoTextView.caretRectForPosition( (infoTextView.selectedTextRange?.start)! ).origin
print("cursorPosition:\(cursorPosition)")
There is no such way. Full stop.
The only thing you could do to come close is to in parallel lay out the text using CoreText, there calcualte the text position from a point and apply that on the UITextView. This, however, works with non-0 selections only. In addition CoreText has a different text layout engine, that e.g. supports kerning, which UITextView doesn't. This may result in deviations of the rendered and laid out text and thus give sub-optimal results.
There is absolutely no way to position the caret. This task is even very though if you do use private API. It's just one of the many "just don't" in iOS.
Swift 4 version:
// lets be safe, thus if-let
if let selectedTextRange = textView.selectedTextRange {
let caretPositionRect = textView.caretRect(for: selectedTextRange.start)
let caretOrigin = caretPositionRect.origin
print(">>>>> position rect: \(caretPositionRect), origin: \(caretOrigin)")
}
We simply use textView.selectedTextRange to get selected text range and than textView.caretRect(for:) to get CGRect defining position of the caret.