<!-- Copyright © Northwoods Software Corporation, 2008-2026. All Rights Reserved. -->

<UserControl x:Class="FDLayout.FDLayout"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:go="http://schemas.nwoods.com/GoXam"
    xmlns:local="clr-namespace:FDLayout">

  <UserControl.Resources>
    <DataTemplate x:Key="nodeTemplate1">
      <go:NodePanel Sizing="Auto"
          go:Part.SelectionAdorned="True" go:Part.SelectionElementName="ellipse">
        <go:NodeShape go:NodePanel.Figure="Ellipse" x:Name="ellipse"
            Fill="{Binding Path=Data.Color}" StrokeThickness="2"
            go:Node.LinkableFrom="True" go:Node.LinkableTo="True"
            go:Node.LinkableDuplicates="True" Cursor="Hand" />
        <TextBlock Text="{Binding Path=Data.Key}" Foreground="Black"
                   TextWrapping="Wrap" Margin="10" />
      </go:NodePanel>
    </DataTemplate>
  </UserControl.Resources>

  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto" />
      <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <Border Grid.Row="0" Background="LightGray" Padding="2">
      <Grid>
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="Auto" />
          <ColumnDefinition Width="Auto" />
          <ColumnDefinition Width="Auto" />
          <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
          <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Border Grid.Column="0" VerticalAlignment="Stretch"
              Background="AliceBlue" Margin="1" Padding="1"
              BorderBrush="Gray" BorderThickness="2" CornerRadius="2">
          <Grid>
            <Grid.RowDefinitions>
              <RowDefinition Height="Auto" />
              <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <TextBlock Grid.Row="0" Text="Tree Generator" FontWeight="Bold" />
            <StackPanel Grid.Row="1">
              <StackPanel Orientation="Horizontal">
                <TextBlock VerticalAlignment="Center" Text="MinLinks: " />
                <TextBox Text="1" x:Name="txtMinLinks" MaxLength="2" />
                <TextBlock VerticalAlignment="Center" Text="MinNodes: " />
                <TextBox Text="50" x:Name="txtMinNodes" MaxLength="3" />
              </StackPanel>
              <StackPanel Orientation="Horizontal">
                <TextBlock VerticalAlignment="Center" Text="MaxLinks: " />
                <TextBox Text="10" x:Name="txtMaxLinks" MaxLength="2" />
                <TextBlock VerticalAlignment="Center" Text="MaxNodes: " />
                <TextBox Text="200" x:Name="txtMaxNodes" MaxLength="3" />
              </StackPanel>
              <Button Content="Generate Tree" Click="GenerateTree_Click" />
            </StackPanel>
          </Grid>
        </Border>

        <Border Grid.Column="1" VerticalAlignment="Stretch"
              Background="AliceBlue" Margin="1" Padding="1"
              BorderBrush="Gray" BorderThickness="2" CornerRadius="2">
          <StackPanel>
            <TextBlock Text="Layout Properties" FontWeight="Bold" />
            <StackPanel Orientation="Horizontal">
              <TextBlock Text="Max Iterations: "
                           VerticalAlignment="Center" />
              <TextBox x:Name="MaxIterationsTextBox" Text="100" />
            </StackPanel>
            <StackPanel Orientation="Horizontal">
              <TextBlock Text="Epsilon: "
                           VerticalAlignment="Center" />
              <TextBox x:Name="EpsilonTextBox" Text="1" />
              <TextBlock Text="  Infinity: "
                           VerticalAlignment="Center" />
              <TextBox x:Name="InfinityTextBox" Text="1000" />
            </StackPanel>
            <StackPanel Orientation="Horizontal">
              <TextBlock Text="ArrangementSpacing: "
                           VerticalAlignment="Center" />
              <TextBox x:Name="ArrangementSpacingTextBox" Text="100 100" />
            </StackPanel>
          </StackPanel>
        </Border>

        <Border Grid.Column="2" VerticalAlignment="Stretch"
              Background="AliceBlue" Margin="1" Padding="1"
              BorderBrush="Gray" BorderThickness="2" CornerRadius="2">
          <StackPanel>
            <TextBlock Text="Node Properties" FontWeight="Bold" />
            <StackPanel Orientation="Horizontal">
              <TextBlock Text="Electrical Charge: "
                           VerticalAlignment="Center" />
              <TextBox x:Name="ChargeTextBox" Text="150" />
            </StackPanel>
            <StackPanel Orientation="Horizontal">
              <TextBlock Text="Gravitational Mass: "
                           VerticalAlignment="Center" />
              <TextBox x:Name="MassTextBox" Text="0" />
            </StackPanel>
          </StackPanel>
        </Border>

        <Border Grid.Column="3" VerticalAlignment="Stretch"
              Background="AliceBlue" Margin="1" Padding="1"
              BorderBrush="Gray" BorderThickness="2" CornerRadius="2">
          <StackPanel>
            <TextBlock Text="Link Properties" FontWeight="Bold" />
            <StackPanel Orientation="Horizontal">
              <TextBlock Text="Spring Stiffness: "
                           VerticalAlignment="Center" />
              <TextBox x:Name="SpringStiffnessTextBox" Text="0.05" />
            </StackPanel>
            <StackPanel Orientation="Horizontal">
              <TextBlock Text="Spring Length: "
                           VerticalAlignment="Center" />
              <TextBox x:Name="SpringLengthTextBox" Text="50" />
            </StackPanel>
          </StackPanel>
        </Border>
      </Grid>
    </Border>

    <go:Diagram x:Name="myDiagram" Grid.Row="1"
                InitialStretch="UniformToFill"
                HorizontalContentAlignment="Center"
                VerticalContentAlignment="Center"
                NodeTemplate="{StaticResource nodeTemplate1}">
      <go:Diagram.Layout>
        <local:CustomForceDirectedLayout 
            MaxIterations="{Binding ElementName=MaxIterationsTextBox, Path=Text}"
            Epsilon="{Binding ElementName=EpsilonTextBox, Path=Text}"
            InfinityDistance="{Binding ElementName=InfinityTextBox, Path=Text}"
            ArrangementSpacing="{Binding ElementName=ArrangementSpacingTextBox, Path=Text}"
            DefaultElectricalCharge="{Binding ElementName=ChargeTextBox, Path=Text}"
            DefaultGravitationalMass="{Binding ElementName=MassTextBox, Path=Text}"
            DefaultSpringStiffness="{Binding ElementName=SpringStiffnessTextBox, Path=Text}"
            DefaultSpringLength="{Binding ElementName=SpringLengthTextBox, Path=Text}"
          />
      </go:Diagram.Layout>
    </go:Diagram>
  </Grid>
</UserControl>
