User avatar
digimbyte
Topic Author
Posts: 47
Joined: 14 Nov 2017, 21:42

Spawn Menu at Position

23 Feb 2019, 12:12

Hello, I've been getting along with my GUI project for a while but I have come across a minor annoyance when dealing with relative and absolute positions of elements

in this case, I have a button that needs to spawn a menu "below" it. I am looking at spawning a Grid at that buttons position with the height of the button as an offset.
unfortunately, when trying to find the screen position I keep getting insane numbers such as -666 and 2280 for my margins and Noesis crashes because it doesn't like these numbers
I think its a failure on my part when dealing with relative vs spacial and screen resolution vs real resolution

below is some code where I have decided to stop and ask for help
Ideally, I'd like for it to be a grid if that's any consequence
public void Menu(object sender, RoutedEventArgs e)
    {
        Point _Position = ((Noesis.Button)sender).PointFromScreen(new Point(0f, 0f));
        Vector2 _size = new Vector2(
            ((Noesis.Button)sender).Width,
            ((Noesis.Button)sender).Height
            );

        Noesis.Grid _panel = new Noesis.Grid
        {
            Margin = Thickness.Parse("" + _Position.X + _size.x + "," + _Position.Y + _size.y + "0,0"),
            MinWidth = 150,
            MaxWidth = 250,
            MinHeight = 12
        };
        ColumnDefinition Options = new ColumnDefinition { Width = Noesis.GridLength.Auto, MaxWidth = 250 };
        ColumnDefinition Scroll = new ColumnDefinition { Width = Noesis.GridLength.Parse("12") };
        _panel.ColumnDefinitions.Add(Options);
        _panel.ColumnDefinitions.Add(Scroll);
 
User avatar
digimbyte
Topic Author
Posts: 47
Joined: 14 Nov 2017, 21:42

Re: Spawn Menu at Position

23 Feb 2019, 14:10

I have been playing with it further
    public void StaticMenu(object sender, RoutedEventArgs e)
    {
        Button origin = ((Noesis.Button)sender);
        Point position = origin.PointToScreen(new Point(10f , 0f));
        
        Menu.Margin = Thickness.Parse(position.X.ToString() + "," + position.Y.ToString() + ",0,0");
        
works, However, I can't seem to add the button's height to it, I get an error Not a Number "NaN" otherwise it's fine.
casting is apparently redundent.
        Point position = origin.PointToScreen(new Point(0f, origin.Height + 0f));
                
Not sure if a bug, or misinterpretation
 
User avatar
sfernandez
Site Admin
Posts: 1915
Joined: 22 Dec 2011, 19:20

Re: Spawn Menu at Position

25 Feb 2019, 16:04

Hi, as you found you need to use PointToScreen to calculate position in screen coordinates.
And Width/Height properties are the source for element size calculation, but the final size is stored in ActualWidth/ActualHeight, so your code should look like this:
Point position = origin.PointToScreen(new Point(0f, origin.ActualHeight));
And I suggest using a TranslateTransform as RenderTransform to position your menu instead of modifying Margin property:
Menu.RenderTransform = new TranslateTransform { X = position.X, Y = position.Y };

Who is online

Users browsing this forum: Noa3, sfernandez and 2 guests