Skip to content

Commit

Permalink
v0.2 release
Browse files Browse the repository at this point in the history
  • Loading branch information
Dwscdv3 committed Oct 24, 2019
1 parent 5b89565 commit 4a1adb6
Show file tree
Hide file tree
Showing 16 changed files with 1,196 additions and 332 deletions.
20 changes: 18 additions & 2 deletions FloatingPointPlayground/App.xaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
<Application x:Class="FloatingPointPlayground.App"
<!--
Copyright (C) 2019 Dwscdv3 <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
-->

<Application x:Class="FloatingPointPlayground.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:FloatingPointPlayground"
StartupUri="MainWindow.xaml">
<Application.Resources>

Expand Down
30 changes: 18 additions & 12 deletions FloatingPointPlayground/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
/**
* Copyright (C) 2019 Dwscdv3 <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

using System.Windows;

namespace FloatingPointPlayground
{
/// <summary>
/// App.xaml 的交互逻辑
/// </summary>
public partial class App : Application
{
}
public partial class App : Application { }
}
38 changes: 14 additions & 24 deletions FloatingPointPlayground/FloatingPointPlayground.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,7 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xaml">
<RequiredTargetFramework>4.0</RequiredTargetFramework>
</Reference>
Expand All @@ -55,6 +49,12 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="Shapes\Bracket.xaml.cs">
<DependentUpon>Bracket.xaml</DependentUpon>
</Compile>
<Compile Include="UserControls\BitFlipper.xaml.cs">
<DependentUpon>BitFlipper.xaml</DependentUpon>
</Compile>
<Page Include="MainWindow.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
Expand All @@ -67,29 +67,19 @@
<DependentUpon>MainWindow.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Page Include="Shapes\Bracket.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="UserControls\BitFlipper.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="Properties\Settings.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Settings.settings</DependentUpon>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
Expand Down
154 changes: 145 additions & 9 deletions FloatingPointPlayground/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,148 @@
<Window x:Class="FloatingPointPlayground.MainWindow"
<!--
Copyright (C) 2019 Dwscdv3 <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
-->

<Window x:Class="FloatingPointPlayground.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:FloatingPointPlayground"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>

</Grid>
xmlns:user="clr-namespace:FloatingPointPlayground.UserControls"
xmlns:shape="clr-namespace:FloatingPointPlayground.Shapes"
Title="Floating-point playground"
Width="800" MinWidth="800" MinHeight="300" SizeToContent="Height">
<StackPanel x:Name="layoutRoot" Margin="10">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="FloatInputGroup">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0:0:0.1" To="ParseError"/>
<VisualTransition From="ParseError" GeneratedDuration="0:0:0.1"/>
</VisualStateGroup.Transitions>
<VisualState x:Name="Default"/>
<VisualState x:Name="ParseError">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="txtFloat">
<EasingColorKeyFrame KeyTime="0" Value="#FFFF7F7F"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<StackPanel HorizontalAlignment="Left" Margin="10,0">
<TextBlock Text="Single-Precision" FontSize="36" Foreground="CadetBlue"/>
<Rectangle StrokeThickness="3" Stroke="CadetBlue" Margin="0,5,0,0"/>
</StackPanel>
<StackPanel Margin="10,10">
<Viewbox VerticalAlignment="Center">
<StackPanel x:Name="flippersContainer" Orientation="Horizontal" user:BitFlipper.Click="OnBitFlipperClick">
<user:BitFlipper/>
<user:BitFlipper/>
<user:BitFlipper/>
<user:BitFlipper/>
<user:BitFlipper/>
<user:BitFlipper/>
<user:BitFlipper/>
<user:BitFlipper/>
<user:BitFlipper/>
<user:BitFlipper/>
<user:BitFlipper/>
<user:BitFlipper/>
<user:BitFlipper/>
<user:BitFlipper/>
<user:BitFlipper/>
<user:BitFlipper/>
<user:BitFlipper/>
<user:BitFlipper/>
<user:BitFlipper/>
<user:BitFlipper/>
<user:BitFlipper/>
<user:BitFlipper/>
<user:BitFlipper/>
<user:BitFlipper/>
<user:BitFlipper/>
<user:BitFlipper/>
<user:BitFlipper/>
<user:BitFlipper/>
<user:BitFlipper/>
<user:BitFlipper/>
<user:BitFlipper/>
<user:BitFlipper/>
</StackPanel>
</Viewbox>
<Grid Height="40">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.5*"/>
<ColumnDefinition Width="0.5*"/>
<ColumnDefinition Width="0.5*"/>
<ColumnDefinition Width="7*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="22*"/>
<ColumnDefinition Width="0.5*"/>
</Grid.ColumnDefinitions>
<Grid.Resources>
<Style TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Center"/>
</Style>
</Grid.Resources>
<Rectangle Grid.Column="1" HorizontalAlignment="Left" Stroke="Black"/>
<shape:Bracket Grid.Column="3"/>
<shape:Bracket Grid.Column="5"/>
<TextBlock Text="Sign" Grid.Row="1" Grid.ColumnSpan="2"/>
<TextBlock Text="Exponent" Grid.Row="1" Grid.Column="3"/>
<TextBlock Text="Fraction" Grid.Row="1" Grid.Column="5"/>
</Grid>
</StackPanel>
<Grid HorizontalAlignment="Center" Margin="0,0,0,10">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="130"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.Resources>
<Style TargetType="TextBlock">
<Style.Triggers>
<Trigger Property="Grid.Row" Value="0">
<Setter Property="FontSize" Value="18"/>
</Trigger>
<Trigger Property="Grid.Column" Value="0">
<Setter Property="HorizontalAlignment" Value="Right"/>
</Trigger>
<Trigger Property="Grid.Column" Value="1">
<Setter Property="Opacity" Value="0.75"/>
<Setter Property="Margin" Value="5,0,0,0"/>
</Trigger>
</Style.Triggers>
</Style>
</Grid.Resources>
<TextBox x:Name="txtFloat" Grid.Row="0"
HorizontalContentAlignment="Right"
FontSize="18"
Margin="0,0,0,5" BorderThickness="0,0,0,2" Background="#00FF7F7F"
TextChanged="OnFloatInput"/>
<TextBlock x:Name="txtInt" Grid.Row="1"/>
<TextBlock x:Name="txtIntHex" Grid.Row="2"/>
<TextBlock Grid.Row="0" Grid.Column="1" Text="(as float)"/>
<TextBlock Grid.Row="1" Grid.Column="1" Text="(as integer in decimal)"/>
<TextBlock Grid.Row="2" Grid.Column="1" Text="(as integer in hex)"/>
</Grid>
</StackPanel>
</Window>
78 changes: 63 additions & 15 deletions FloatingPointPlayground/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,76 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/**
* Copyright (C) 2019 Dwscdv3 <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using FloatingPointPlayground.UserControls;

namespace FloatingPointPlayground
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
private bool disableTextChangedEvent = false;

public MainWindow()
{
InitializeComponent();
Update();
}

private void Update()
{
var bin = 0;
for (var i = 0; i < 32; i += 1)
{
bin += ((flippersContainer.Children[31 - i] as BitFlipper).IsChecked == true ? 1 : 0) << i;
}
txtInt.Text = bin.ToString();
txtIntHex.Text = "0x" + bin.ToString("X8");
disableTextChangedEvent = true;
txtFloat.Text = BitConverter.ToSingle(BitConverter.GetBytes(bin), 0).ToString();
disableTextChangedEvent = false;
}

private void OnBitFlipperClick(object sender, RoutedEventArgs e) => Update();

private void OnFloatInput(object sender, TextChangedEventArgs e)
{
if (!disableTextChangedEvent)
{
var textBox = sender as TextBox;

VisualStateManager.GoToElementState(layoutRoot, "Default", true);

if (float.TryParse(textBox.Text, out var f))
{
var bin = BitConverter.ToInt32(BitConverter.GetBytes(f), 0);
for (var i = 0; i < 32; i += 1)
{
(flippersContainer.Children[i] as BitFlipper).IsChecked = (bin & (1 << (31 - i))) != 0;
}
txtInt.Text = bin.ToString();
txtIntHex.Text = "0x" + bin.ToString("X8");
}
else
{
VisualStateManager.GoToElementState(layoutRoot, "ParseError", true);
}
}
}
}
}
Loading

0 comments on commit 4a1adb6

Please sign in to comment.