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

<UserControl x:Class="LogicCircuit.LogicCircuit"
    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">

  <UserControl.Resources>
    <go:BooleanBrushConverter x:Key="theValueConverter"
                              FalseBrush="Red" TrueBrush="Green" />
    <go:BooleanBrushConverter x:Key="theSelectionConverter"
                              FalseBrush="Black" TrueBrush="Cyan" />

    <DataTemplate x:Key="Wire">
      <go:LinkPanel>
        <go:Link.Route>
          <go:Route Routing="AvoidsNodes" Curve="JumpOver"
                    RelinkableTo="True" RelinkableFrom="True" />
        </go:Link.Route>
        <go:LinkShape StrokeThickness="2"
                      Stroke="{Binding Path=Link.FromNode.Data.Value,
                               Converter={StaticResource theValueConverter}}" />
        <Path Fill="Black" go:LinkPanel.ToArrow="Standard" />
      </go:LinkPanel>
    </DataTemplate>

    <go:DataTemplateDictionary x:Key="NodeTemplateDictionary">
      <DataTemplate x:Key="OneInOneOut">
        <go:SpotPanel go:Node.Location="{Binding Path=Data.Location, Mode=TwoWay}">
          <ToolTipService.ToolTip>
            <TextBlock Text="{Binding Path=Data.Key}" />
          </ToolTipService.ToolTip>
          <go:NodePanel>
            <go:NodeShape Width="50" Height="50"
                        go:NodePanel.Figure="{Binding Path=Data.Figure}"
                        Stroke="{Binding Path=Node.IsSelected,
                                 Converter={StaticResource theSelectionConverter}}" 
                        StrokeThickness="2" Fill="LightSlateGray" />
          </go:NodePanel>
          <Rectangle go:SpotPanel.Spot="MiddleLeft" Width="8" Height="8" Fill="Black"
                   go:Node.PortId="In" go:Node.LinkableTo="True"
                   go:Node.LinkableMaximum="1"
                   go:Node.ToSpot="MiddleLeft" Cursor="Hand" />
          <Rectangle go:SpotPanel.Spot="MiddleRight" Width="8" Height="8" Fill="Black"
                   go:Node.PortId="" go:Node.LinkableFrom="True"
                   go:Node.FromSpot="MiddleRight" Cursor="Hand" />
        </go:SpotPanel>
      </DataTemplate>

      <DataTemplate x:Key="TwoInOneOut">
        <go:SpotPanel go:Node.Location="{Binding Path=Data.Location, Mode=TwoWay}">
          <ToolTipService.ToolTip>
            <TextBlock Text="{Binding Path=Data.Key}" />
          </ToolTipService.ToolTip>
          <go:NodePanel>
            <go:NodeShape Width="50" Height="50"
                        go:NodePanel.Figure="{Binding Path=Data.Figure}"
                        Stroke="{Binding Path=Node.IsSelected,
                                 Converter={StaticResource theSelectionConverter}}" 
                        StrokeThickness="2" Fill="LightSlateGray" />
          </go:NodePanel>
          <Rectangle go:SpotPanel.Spot="0 .33" Width="8" Height="8" Fill="Black"
                   go:Node.PortId="In1" go:Node.LinkableTo="True"
                   go:Node.LinkableMaximum="1"
                   go:Node.ToSpot="MiddleLeft" Cursor="Hand" />
          <Rectangle go:SpotPanel.Spot="0 .67" Width="8" Height="8" Fill="Black"
                   go:Node.PortId="In2" go:Node.LinkableTo="True"
                   go:Node.LinkableMaximum="1"
                   go:Node.ToSpot="MiddleLeft" Cursor="Hand" />
          <Rectangle go:SpotPanel.Spot="MiddleRight" Width="8" Height="8" Fill="Black"
                   go:Node.PortId="" go:Node.LinkableFrom="True"
                   go:Node.FromSpot="MiddleRight" Cursor="Hand" />
        </go:SpotPanel>
      </DataTemplate>

      <DataTemplate x:Key="TwoInOneOutCurved">
        <go:SpotPanel go:Node.Location="{Binding Path=Data.Location, Mode=TwoWay}">
          <ToolTipService.ToolTip>
            <TextBlock Text="{Binding Path=Data.Key}" />
          </ToolTipService.ToolTip>
          <go:NodePanel>
            <go:NodeShape Width="50" Height="50"
                        go:NodePanel.Figure="{Binding Path=Data.Figure}"
                        Stroke="{Binding Path=Node.IsSelected,
                                 Converter={StaticResource theSelectionConverter}}"
                        StrokeThickness="2" Fill="LightSlateGray" />
          </go:NodePanel>
          <Rectangle go:SpotPanel.Spot="0.2 .33" Width="8" Height="8" Fill="Black"
                   go:Node.PortId="In1" go:Node.LinkableTo="True"
                   go:Node.LinkableMaximum="1"
                   go:Node.ToSpot="MiddleLeft" Cursor="Hand" />
          <Rectangle go:SpotPanel.Spot="0.2 .67" Width="8" Height="8" Fill="Black"
                   go:Node.PortId="In2" go:Node.LinkableTo="True"
                   go:Node.LinkableMaximum="1"
                   go:Node.ToSpot="MiddleLeft" Cursor="Hand" />
          <Rectangle go:SpotPanel.Spot="MiddleRight" Width="8" Height="8" Fill="Black"
                   go:Node.PortId="" go:Node.LinkableFrom="True"
                   go:Node.FromSpot="MiddleRight" Cursor="Hand" />
        </go:SpotPanel>
      </DataTemplate>

      <DataTemplate x:Key="Input">
        <go:SpotPanel go:Node.Location="{Binding Path=Data.Location, Mode=TwoWay}">
          <ToolTipService.ToolTip>
            <TextBlock Text="{Binding Path=Data.Key}" />
          </ToolTipService.ToolTip>
          <go:NodePanel>
            <go:NodeShape MouseLeftButtonDown="StartNodeDoubleClick"
                        Width="50" Height="50"
                        go:NodePanel.Figure="{Binding Path=Data.Figure}"
                        Fill="{Binding Path=Data.Value,
                               Converter={StaticResource theValueConverter}}" 
                        Stroke="{Binding Path=Node.IsSelected,
                               Converter={StaticResource theSelectionConverter}}" 
                        StrokeThickness="2" />
          </go:NodePanel>
          <Rectangle go:SpotPanel.Spot="MiddleRight" Width="8" Height="8" Fill="Black"
                   go:Node.PortId="" go:Node.LinkableFrom="True"
                   go:Node.FromSpot="MiddleRight" Cursor="Hand" />
        </go:SpotPanel>
      </DataTemplate>

      <DataTemplate x:Key="Output">
        <go:SpotPanel go:Node.Location="{Binding Path=Data.Location, Mode=TwoWay}">
          <ToolTipService.ToolTip>
            <TextBlock Text="{Binding Path=Data.Key}" />
          </ToolTipService.ToolTip>
          <go:NodePanel>
            <go:NodeShape Width="50" Height="50"
                        go:NodePanel.Figure="{Binding Path=Data.Figure}"
                        Fill="{Binding Path=Data.Value,
                               Converter={StaticResource theValueConverter}}" 
                        Stroke="{Binding Path=Node.IsSelected,
                                 Converter={StaticResource theSelectionConverter}}" 
                        StrokeThickness="2" />
          </go:NodePanel>
          <Rectangle go:SpotPanel.Spot="MiddleLeft"
                   Width="8" Height="8" Fill="Black"
                   go:Node.PortId="" go:Node.LinkableTo="True"
                   go:Node.LinkableMaximum="1" 
                   go:Node.ToSpot="MiddleLeft" Cursor="Hand" />
        </go:SpotPanel>
      </DataTemplate>
    </go:DataTemplateDictionary>

  </UserControl.Resources>

  <Grid>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="Auto" />
      <ColumnDefinition Width="Auto" />
      <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Grid Grid.Column="0">
      <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
      </Grid.RowDefinitions>
      <TextBlock Grid.Row="0" Text="Gates" FontWeight="Bold" />
      <go:Palette Grid.Row="1" x:Name="myPalette" Padding="10"
                  NodeTemplateDictionary="{StaticResource NodeTemplateDictionary}">
        <go:Diagram.Layout>
          <go:GridLayout CellSize="100 30" Spacing="5 5" Sorting="Forward" />
        </go:Diagram.Layout>
      </go:Palette>
    </Grid>
    <GridSplitter Grid.Column="1" Width="5"
                  HorizontalAlignment="Center" VerticalAlignment="Stretch" />
    <Grid Grid.Column="2">
      <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
      </Grid.RowDefinitions>
      <TextBlock Grid.Row="0"
                 Text="A value of one is green; zero is red.
Double click on an input node to toggle its value." />
      <StackPanel Grid.Row="0" Orientation="Horizontal"
                HorizontalAlignment="Right" VerticalAlignment="Top">
        <Button Content="Save" Click="Save_Click" />
        <Button Content="Load" Click="Load_Click" x:Name="LoadButton" IsEnabled="False" />
      </StackPanel>
      <go:Diagram Grid.Row="1" x:Name="myDiagram"
                  NodeTemplateDictionary="{StaticResource NodeTemplateDictionary}"
                  LinkTemplate="{StaticResource Wire}"
                  Padding="10"
                  HorizontalContentAlignment="Stretch"
                  VerticalContentAlignment="Stretch"
                  GridSnapEnabled="True" />
    </Grid>
  </Grid>
</UserControl>