Positioning element at screen coordinates
Hello. I'm trying to implement HUD markers.
And in the following code I try to position the ellipse so that its center is at the given screen coordinates.
But the marker turns out to be shifted to the left and up relative to the desired point.

If I try to do so, visually the marker seems to be in the right place, but I do not understand why it is so:
Code: Select all
<Grid x:Name="WorkGrid">
<Ellipse
x:Name="DestinationMarker"
Width="55"
Height="55"
Background="Transparent"
Margin="{Binding DestinationMarkerMargin}"
Visibility="{Binding DestinationMarkerVisibility}"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Stroke="White" StrokeThickness="2" />
</Grid>
Code: Select all
void GameScreenViewModel::updateMarkerMargin(
Noesis::FrameworkElement const* workGrid,
float screenX, float screenY,
Noesis::Thickness& markerMargin)
{
const Noesis::Point size = workGrid->PointToScreen(Noesis::Point{ 55, 55});
const Noesis::Point origin = workGrid->PointToScreen(Noesis::Point{ 0, 0 });
float width = size.x - origin.x;
float height = size.y - origin.y;
const Noesis::Point point = workGrid->PointFromScreen(Noesis::Point(screenX - width/2 , screenY - height/2));
markerMargin.left = std::clamp(point.x, 0.0f, workGrid->GetActualWidth() - markerSize.x);
markerMargin.top = std::clamp(point.y, 0.0f, workGrid->GetActualHeight() - markerSize.y);
}

If I try to do so, visually the marker seems to be in the right place, but I do not understand why it is so:
Code: Select all
const Noesis::Point point = workGrid->PointFromScreen(Noesis::Point(screenX - width/4 , screenY - height/4));
-
-
sfernandez
Site Admin
- Posts: 3363
- Joined:
Re: Positioning element at screen coordinates
I think it will be easier to offset the Ellipse to its center, so you just need transform the screen coordinate to the WorkGrid element:
Code: Select all
<Grid x:Name="WorkGrid">
<Ellipse x:Name="DestinationMarker"
Width="55" Height="55"
Background="Transparent"
Margin="{Binding DestinationMarkerMargin}"
Visibility="{Binding DestinationMarkerVisibility}"
HorizontalAlignment="Left" VerticalAlignment="Top"
Stroke="White" StrokeThickness="2">
<Ellipse.RenderTransform>
<TranslateTransform X="-27.5" Y="-27.5"/>
</Ellipse.RenderTransform>
</Ellipse>
</Grid>Code: Select all
void GameScreenViewModel::updateMarkerMargin(Noesis::FrameworkElement const* workGrid,
float screenX, float screenY, Noesis::Thickness& markerMargin)
{
const Noesis::Point point = workGrid->PointFromScreen(Noesis::Point(screenX, screenY));
markerMargin.left = std::clamp(point.x, 0.0f, workGrid->GetActualWidth() - markerSize.x);
markerMargin.top = std::clamp(point.y, 0.0f, workGrid->GetActualHeight() - markerSize.y);
}Who is online
Users browsing this forum: No registered users and 4 guests