Interactive Ink

[iOS] Get coordinates out of Block Boxes

 Hey!

I'm having troubles to get the coordinates out of the already existing blocks in order to create a new one at the bottom.

I'm currently cycling through all the child from the root block in order to get the lowest one, and get the coordinates from the new one given the y coordinate + the height of the box to get the bottom position, which gives me a number. But then I get an error saying a block can't be created at the given coordinates.

Then I tried setting the render's view offset to the given position, and it scrolls the view to the middle of the document, instead of to the end as I expected. (I discovered that the given position multiplied by 5 scrolls to almost the end, though I'm not completely sure it's a constant). May the problem be due to an scale factor?

Following this idea, I check render's values:

view scale : 1

dpiY : 132

pixelSize : 0.192424


Any tips you could give, please?


Thanks!


Dear Gonzalo,

Thank you for your question.

Currently, it looks like you are not applying the transform to get the coordinates of your blocks. Please find a quick JAVA sample that is likely to help you (please note it is likemy to contain bugs).

Point positionOfNextBlock(Editor editor)
{
//space in between lines in mm
float lineSpacingMm = 10.0f;
//left margin in mm
float leftMarginMm = editor.getEngine().getConfiguration().getNumber("text.margin.left", -1.0).floatValue();
//top margin in mm
float topMarginMm = editor.getEngine().getConfiguration().getNumber("text.margin.top", -1.0).floatValue();

//Get the transform
Transform transform = editor.getRenderer().getViewTransform();
//apply the transform to margin
Point topLeft = transform.apply(leftMarginMm, topMarginMm);

float leftMargin = topLeft.x;
float topMargin = topLeft.y;

  ContentBlock rootBlock = editor.getRootBlock();
  ContentBlock[] subBlock = rootBlock.getChildren();
  ContentBlock lastBlock = subBlock[subBlock.length-1];

if(lastBlock == null)
  {
return new Point(leftMargin,topMargin);
  }
else{
    Rectangle box = lastBlock.getBox();
float y = transform.apply(0, box.y + box.height + lineSpacingMm).y;

return new Point(leftMargin,y);
  }
}


Let me know if this helps.

Best regards,

Olivier


1 person likes this

Hey Oliver! Thanks for your advice!


I was definetely not using the transform. Sorry it took me so long to answer, but I'm experiencing a really bizarre issue, and I was trying to figure it out. Maybe you can give me a little help?


 I'm adding blocks in a row (with 2 or 3 seconds in between), and I'm aplying the transform to get the proper origin point for each new block. This is the code I'm using:

IINKContentBlock* lastBlock = [blocks lastObject];
	NSLog(@"origin: %f",lastBlock.box.origin.y);
	NSLog(@"height: %f",lastBlock.box.size.height);
	
	CGPoint point = CGPointMake(lastBlock.box.origin.x, lastBlock.box.origin.y + lastBlock.box.size.height);
	
	CGPoint transformedPoint =  CGPointApplyAffineTransform(point, self.editor.renderer.viewTransform);
	
	transformedPoint.y = transformedPoint.y + 10;
	
	NSLog(@"transformedPoint %f",transformedPoint.y);
	
	return transformedPoint;

The first time I do it it works properly, this is the log I get:

origin: 1140.646240
height: 36.943970
transformedPoint 4545.821681

After adding the block I'm changing the offset to show the new block. It also works perfectly


Second time though, the transformed point it's quite smaller than the previous time

origin: 1186.826172
height: 36.943970
transformedPoint 1823.928450

Nevertheless, the block is added without issues, but the offset is set to 229.99 and it's showing the wrong place.

Third time, it works again:

origin: 1233.006104
height: 36.943970
transformedPoint 4785.811952


And the forth one fails once more:

origin: 1279.186035
height: 36.943970
transformedPoint 2063.918599


Any ideas?


To do the scroll I'm using the following code:


 

- (void) scrollToPoint:(CGPoint) point {
	point.x = 0;
	[self.editor clampViewOffset:&point];
	[self.editor.renderer setViewOffset:point];
	[self.displayViewController refreshViews];
}

 

Ok... I figured it out (or at least I think so, maybe you can confirm?)

I've solved the issue by setting the scroll back to position 0 before doing the calculation, so my current guess is the transform doesn't give you the position from the top, but instead gives you the distance from the offset you're currently at. Is that correct?

Dear Gonzalo,


That is exactly it, it is the distance from the offset your are currently at.


Best regards,


Olivier

Login or Signup to post a comment