uwp GetcurrentPoint image - pointers

I am writing a code that move an image. it works.
I need the absolute x, y after the moving
how ca I get them ?
thank you
my code gives me only the relative points. :
private static void MyImage_PointerMoved(object sender, PointerRoutedEventArgs e)
{
Pointer ptr = e.Pointer;
if (ptr.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Mouse)
{
// To get mouse state, we need extended pointer details.
// We get the pointer info through the getCurrentPoint method
// of the event argument.
Windows.UI.Input.PointerPoint ptrPt = e.GetCurrentPoint(micky.MyImage);
var transform = (micky.MyImage.RenderTransform as CompositeTransform);
double imagex = e.GetCurrentPoint(micky.MyImage).Position.X;
double imageY = e.GetCurrentPoint(micky.MyImage).Position.Y;

Not sure what you want to do. If you just want to get the image's absolute position. You could use TransformToVisual and TransformPoint method.
<Image x:Name="pic" Source="Assets/1.jpg"/>
var trans = pic.TransformToVisual(Window.Current.Content);
Point position = trans.TransformPoint(new Point(0, 0));
But I checked your code, I guess you want to move your image. If so, using Manipulation event should meet your target.
<Image x:Name="pic" Source="Assets/1.jpg" ManipulationMode="All" ManipulationDelta="Image_ManipulationDelta" Height="100" Width="200">
<Image.RenderTransform>
<TranslateTransform x:Name="translateTransform"></TranslateTransform>
</Image.RenderTransform>
</Image>
private void Image_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
translateTransform.X += e.Delta.Translation.X;
translateTransform.Y += e.Delta.Translation.Y;
System.Diagnostics.Debug.WriteLine("Manipulation: X: " + translateTransform.X + ", Y: " + translateTransform.Y);
}

Related

Touchaction Event firing twice

I am working with the skiasharp on xamarin with TouchTracking and while trying to handle the TouchAction event in code, i noticed the event gets fired twice instead of once.
void OnTouchEffectAction(object sender, TouchActionEventArgs args)
{
// Get touchpoint location
pt.X = XamDIUConvertToPixels(args.Location.X);
pt.Y = XamDIUConvertToPixels(args.Location.Y);
// Display point locations
Console.WriteLine("location X: " + pt.X + "\n location Y: " + pt.Y + "\n");
So, this would inevitably display to the console twice. And this becomes more problematic if say i want to call a function of text-to-speech in here, then the speech gets repeated.

QTextLayout::drawCursor() does not work when subclassing QAbstractTextDocumentLayout

currently QTextEdit and QPlainTextEdit does not meet my requirements, so I need to subclass QAbstractTextDocumentLayout to provide custom layout of the document. I reference QPlainTextDocumentLayout and QTextDocumentLayout a lot, and finally got a simple layout to display the text. However, I couldn't see the cursor in QTextEdit, which should be blinking. I need help to figure this out.
I am using Qt 5.9.1. The simple project is here. The draw() function of VTextDocumentLayout looks like this:
void VTextDocumentLayout::draw(QPainter *p_painter, const PaintContext &p_context)
{
qDebug() << "VTextDocumentLayout draw()" << p_context.clip << p_context.cursorPosition << p_context.selections.size();
// Find out the blocks.
int first, last;
blockRangeFromRect(p_context.clip, first, last);
if (first == -1) {
return;
}
QTextDocument *doc = document();
QPointF offset(m_margin, m_blocks[first].m_offset);
QTextBlock block = doc->findBlockByNumber(first);
QTextBlock lastBlock = doc->findBlockByNumber(last);
qreal maximumWidth = m_width;
while (block.isValid()) {
const BlockInfo &info = m_blocks[block.blockNumber()];
const QRectF &rect = info.m_rect;
QTextLayout *layout = block.layout();
if (!block.isVisible()) {
offset.ry() += rect.height();
if (block == lastBlock) {
break;
}
block = block.next();
continue;
}
QTextBlockFormat blockFormat = block.blockFormat();
QBrush bg = blockFormat.background();
if (bg != Qt::NoBrush) {
fillBackground(p_painter, rect, bg);
}
auto selections = formatRangeFromSelection(block, p_context.selections);
QPen oldPen = p_painter->pen();
p_painter->setPen(p_context.palette.color(QPalette::Text));
layout->draw(p_painter,
offset,
selections,
p_context.clip.isValid() ? p_context.clip : QRectF());
// Draw the cursor.
int blpos = block.position();
int bllen = block.length();
bool drawCursor = p_context.cursorPosition >= blpos
&& p_context.cursorPosition < blpos + bllen;
Q_ASSERT(p_context.cursorPosition >= -1);
if (drawCursor) {
int cpos = p_context.cursorPosition;
cpos -= blpos;
qDebug() << "draw cursor" << block.blockNumber() << blpos << bllen << p_context.cursorPosition << cpos;
layout->drawCursor(p_painter, offset, cpos);
}
p_painter->setPen(oldPen);
offset.ry() += rect.height();
if (block == lastBlock) {
break;
}
block = block.next();
}
}
I called layout->drawCursor() to draw the cursor but this function seems to do nothing.
Any help is appreciated! Thanks!
Update:
Add the log as following:
VTextDocumentLayout draw() QRectF(67,0 9x13) 19 0
block range 0 1
draw cursor 1 5 15 19 14
blockBoundingRect() 1 13 QRectF(0,0 75x17)
VTextDocumentLayout draw() QRectF(67,0 9x13) -1 0
block range 0 1
blockBoundingRect() 1 13 QRectF(0,0 75x17)
VTextDocumentLayout draw() QRectF(67,0 9x13) 19 0
When running this project in Linux, I coundn't see the cursor. However, when running it in Windows, I could see the cursor but not blinking.
Update:
It seems that QTextEdit pass a wrong clip rect to the layout. If I just inserted one line of text (only one block within the document), I could see the blinking cursor. Quite strange!!!
The default value of PaintContext class cursorPosition is -1.
http://doc.qt.io/qt-4.8/qabstracttextdocumentlayout-paintcontext.html#cursorPosition-var
If any default value is not provided to the cursor position ever (it is a public variable), then my guess is value remains -1, and drawCursor is always false (as the block position index minimum value is zero in worst case).
Try setting some default value to PaintContext::cursorPosition, which may display the cursor.
When subclassing QAbstractTextDocumentLayout, blockBoundingRect() should return the geometry of the block, not just the rect as what QPlainTextDocumentLayout does.
In one word, QPlainTextDocumentLayout provides a BAD example for subsclassing QAbstractTextDocumentLayout.

Moving a 2d object towards and through a position in Unity 4.3

Trying to get an object to fire towards and through a given position. Like a bullet firing towards the mouse location, I don't want it to stop on the mouse (which is what is happening now).
Below is what I have so far, is there a function like lerp that I could use?
var speed:float;
var startPoint:Vector3;
var startTime:float;
var clickedPosition:Vector3;
function Start()
{
startPoint = transform.position;
startTime = Time.time;
clickedPosition = Vector3(Input.mousePosition.x, Input.mousePosition.y, 0);
clickedPosition = Camera.main.ScreenToWorldPoint(clickedPosition);
}
function Update ()
{
transform.position = Vector3.Lerp(startPoint, clickedPosition, (Time.time-startTime));
}
I would suggest using a rigidbody component and then applying a force in the direction (while disabling gravitiy i guess).
The way you have it now you should probably get it to work with
var speed : float;
function Start()
{
speed = 1000.0f; // experiment with this, might be way too fast;
...
}
function Update()
{
transform.position += (clickedPosition - startPoint) * speed * Time.deltaTime;
}
(clickedPosition - startPoint) should give you the direction in which you want to move the object, Time.deltaTime gives you the milliseconds since the last call of the Update function (you want this in here so that the object moves the same speed at different framerates) and speed is just a constant to adjust the velocity.
var speed:float;
var startPoint:Vector3;
var startTime:float;
var clickedPosition:Vector3;
function Start()
{
startPoint = transform.position;
}
function Update ()
{
if(Input.GetKeyDown(KeyCode.Mouse0))
{
startTime = Time.time;
clickedPosition = Vector3(Input.mousePosition.x, Input.mousePosition.y, 0);
clickedPosition = Camera.main.ScreenToWorldPoint(clickedPosition);
}
transform.position = Vector3.Lerp(startPoint, clickedPosition, (Time.time-startTime));
}
This is pretty simple. You can use the Vector3.Lerp function to achieve this. Use raycasting to get the mouse click position or the touch position. Then use the initial and the final position in the lerp function. The initial position being the position that the gameobject is at now and the final position being the click / touch position.
You can find the article on the same here
Move to Touch / Click Position - The Game Contriver

How to trace the missing pixels when using drawLine

We know that for drawing on an image in qt, qpainter is used. Recently, I used drawLine() function to draw whatever an user is scribbling. This was done by passing the lastPoint and currentPoint from the mouseMoveEvent to a custom function which implements drawLine(). I have passed the arguments for that custom function as given below:
void myPaint::mouseMoveEvent(QMouseEvent *event) {
qDebug() << event->pos();
if ((event->buttons() & Qt::LeftButton) && scribbling) {
pixelList.append(event->pos());
drawLineTo(event->pos());
lastPoint = event->pos();
}
}
Now with the help of qDebug() I noticed that some pixels are missed while drawing but the drawing is precise. I looked up the source of qt-painting where I saw that drawLine() was calling drawLines() which was making use of qpainterPath to have a shape drawn on the image.
My question is that, is there anyway to track these "missed" pixels or any approach to find all the pixels which have been drawn?
Thanks!
void myPaint::drawLineTo(const QPoint &endPoint) {
QPainter painter(image); //image is initialized in the constructor of myPaint
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(QPen(Qt::blue, myPenWidth, Qt::SolidLine, Qt::RoundCap,Qt::RoundJoin));
painter.drawLine(lastPoint, endPoint);
modified = true;
lastPoint = endPoint; //at the mousePressEvent, the event->pos() will be stored as
// lastPoint
update();
}
For a start, don't draw in a mouseEvent(). Actually handling a mouseevent should be done as quick as possible. Also, it is not a good idea to look at the Qt source, it can be confusing. Rather assume that what Qt gives you work, and first try to answer "What I am doing wrong?". As I said drawing in a mouse event is definitely wrong.
Your description is really subjective, maybe an image of your output is better. Are you trying to emulate a pen (like in windows paint)? In this case do the mouse button has to be down ? is that the purpose of your variable scribbling?
There is more. following the documentation, QMouseEvent::buttons() always return a combination of all buttons for mouse move event. Which make sense : the mouse movements are independent of the buttons. It means
if ((event->buttons() & Qt::LeftButton)
will always be true.
Let's assume you want to draw the path of your mouse when the left button is pressed. Then you use something like :
void myPaint::mousePressEvent(QMouseEvent *event){
scribbling = true;
pixelList.clear();
}
void myPaint::mouseReleaseEvent(QMouseEvent *event){
scribbling = false;
}
void myPaint::mouseMoveEvent(QMouseEvent *event) {
if ( scribbling) {
pixelList.append(event->pos());
}
}
void myPaint::paintEvent(){
QPainter painter(this)
//some painting here
if ( scribbling) {
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(QPen(Qt::blue, myPenWidth, Qt::SolidLine, Qt::RoundCap,Qt::RoundJoin));
// here draw your path
// for example if your path can be made of lines,
// or you just put the points if they are close from each other
}
//other painting here
}
If after all of this you don't have a good rendering, try using float precision (slower), ie QMouseEvent::posF() instead of QMouseEvent::pos().
EDIT :
"I want to know whether there is any way to calculate all the sub-pixels between any two pixels that we send as arguments to drawLine"
Yes there is. I don't know why you need to do such thing but is really simple. A line can be characterized with the equation
y = ax + b
Both of the endpoints of the line p0 = (x0, y0) and p1 = (x1, y1) satisfy this equation so you can easily find a and b. Now all you need to do is increment from x0 to x1 by the amount of
pixels you want (say 1), and to compute the corresponding y value, each time saving point(x,y).
So will go over all of the points saved in pixelList and repeat this process for any two consecutive points.

scale surface without blurring it

I try to scale an image derivedfrom a file, to any (sensible) scale.
The problem is, cairo somhow autoblurrs it. How can I fix/remove it? The aim is to see the individual pixels.
Thanks for any reply.
Edit: Some code, triggering on "draw" event, parent is a GtkDrawingArea
static gboolean
cb_event_draw (GtkWidget *obj, cairo_t *cr, gpointer data)
{
guint width, height;
width = gtk_widget_get_allocated_width (obj);
height = gtk_widget_get_allocated_height (obj);
_priv = ...; //some struct
// cairo_save (cr);
cairo_set_antialias (cr, CAIRO_ANTIALIAS_NONE);
cairo_scale (cr, _priv->zoom, _priv->zoom);
cairo_set_source_surface (cr, _priv->image, 0., 0.);
cairo_set_antialias (cr, CAIRO_ANTIALIAS_NONE);
cairo_pattern_set_filter (cr, CAIRO_FILTER_FAST); // no matter if this is there or not
// it does actually, matter, works with this:
// cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_FAST);
cairo_paint (cr);
// print some markers at defined locations
return FALSE;
}
I suspect that you need:
cairo_pattern_set_filter(cairo_get_source(cr), CAIRO_FILTER_FAST);

Resources