Skip to content

Commit 4a1adb6

Browse files
committed
v0.2 release
1 parent 5b89565 commit 4a1adb6

16 files changed

+1196
-332
lines changed

FloatingPointPlayground/App.xaml

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
1-
<Application x:Class="FloatingPointPlayground.App"
1+
<!--
2+
Copyright (C) 2019 Dwscdv3 <[email protected]>
3+
4+
This program is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
This program is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
-->
17+
18+
<Application x:Class="FloatingPointPlayground.App"
219
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
320
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4-
xmlns:local="clr-namespace:FloatingPointPlayground"
521
StartupUri="MainWindow.xaml">
622
<Application.Resources>
723

FloatingPointPlayground/App.xaml.cs

+18-12
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Configuration;
4-
using System.Data;
5-
using System.Linq;
6-
using System.Threading.Tasks;
1+
/**
2+
* Copyright (C) 2019 Dwscdv3 <[email protected]>
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
718
using System.Windows;
819

920
namespace FloatingPointPlayground
1021
{
11-
/// <summary>
12-
/// App.xaml 的交互逻辑
13-
/// </summary>
14-
public partial class App : Application
15-
{
16-
}
22+
public partial class App : Application { }
1723
}

FloatingPointPlayground/FloatingPointPlayground.csproj

+14-24
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,7 @@
3636
</PropertyGroup>
3737
<ItemGroup>
3838
<Reference Include="System" />
39-
<Reference Include="System.Data" />
40-
<Reference Include="System.Xml" />
41-
<Reference Include="Microsoft.CSharp" />
4239
<Reference Include="System.Core" />
43-
<Reference Include="System.Xml.Linq" />
44-
<Reference Include="System.Data.DataSetExtensions" />
45-
<Reference Include="System.Net.Http" />
4640
<Reference Include="System.Xaml">
4741
<RequiredTargetFramework>4.0</RequiredTargetFramework>
4842
</Reference>
@@ -55,6 +49,12 @@
5549
<Generator>MSBuild:Compile</Generator>
5650
<SubType>Designer</SubType>
5751
</ApplicationDefinition>
52+
<Compile Include="Shapes\Bracket.xaml.cs">
53+
<DependentUpon>Bracket.xaml</DependentUpon>
54+
</Compile>
55+
<Compile Include="UserControls\BitFlipper.xaml.cs">
56+
<DependentUpon>BitFlipper.xaml</DependentUpon>
57+
</Compile>
5858
<Page Include="MainWindow.xaml">
5959
<Generator>MSBuild:Compile</Generator>
6060
<SubType>Designer</SubType>
@@ -67,29 +67,19 @@
6767
<DependentUpon>MainWindow.xaml</DependentUpon>
6868
<SubType>Code</SubType>
6969
</Compile>
70+
<Page Include="Shapes\Bracket.xaml">
71+
<SubType>Designer</SubType>
72+
<Generator>MSBuild:Compile</Generator>
73+
</Page>
74+
<Page Include="UserControls\BitFlipper.xaml">
75+
<SubType>Designer</SubType>
76+
<Generator>MSBuild:Compile</Generator>
77+
</Page>
7078
</ItemGroup>
7179
<ItemGroup>
7280
<Compile Include="Properties\AssemblyInfo.cs">
7381
<SubType>Code</SubType>
7482
</Compile>
75-
<Compile Include="Properties\Resources.Designer.cs">
76-
<AutoGen>True</AutoGen>
77-
<DesignTime>True</DesignTime>
78-
<DependentUpon>Resources.resx</DependentUpon>
79-
</Compile>
80-
<Compile Include="Properties\Settings.Designer.cs">
81-
<AutoGen>True</AutoGen>
82-
<DependentUpon>Settings.settings</DependentUpon>
83-
<DesignTimeSharedInput>True</DesignTimeSharedInput>
84-
</Compile>
85-
<EmbeddedResource Include="Properties\Resources.resx">
86-
<Generator>ResXFileCodeGenerator</Generator>
87-
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
88-
</EmbeddedResource>
89-
<None Include="Properties\Settings.settings">
90-
<Generator>SettingsSingleFileGenerator</Generator>
91-
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
92-
</None>
9383
</ItemGroup>
9484
<ItemGroup>
9585
<None Include="App.config" />
+145-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,148 @@
1-
<Window x:Class="FloatingPointPlayground.MainWindow"
1+
<!--
2+
Copyright (C) 2019 Dwscdv3 <[email protected]>
3+
4+
This program is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
This program is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
-->
17+
18+
<Window x:Class="FloatingPointPlayground.MainWindow"
219
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
320
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4-
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5-
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6-
xmlns:local="clr-namespace:FloatingPointPlayground"
7-
mc:Ignorable="d"
8-
Title="MainWindow" Height="450" Width="800">
9-
<Grid>
10-
11-
</Grid>
21+
xmlns:user="clr-namespace:FloatingPointPlayground.UserControls"
22+
xmlns:shape="clr-namespace:FloatingPointPlayground.Shapes"
23+
Title="Floating-point playground"
24+
Width="800" MinWidth="800" MinHeight="300" SizeToContent="Height">
25+
<StackPanel x:Name="layoutRoot" Margin="10">
26+
<VisualStateManager.VisualStateGroups>
27+
<VisualStateGroup x:Name="FloatInputGroup">
28+
<VisualStateGroup.Transitions>
29+
<VisualTransition GeneratedDuration="0:0:0.1" To="ParseError"/>
30+
<VisualTransition From="ParseError" GeneratedDuration="0:0:0.1"/>
31+
</VisualStateGroup.Transitions>
32+
<VisualState x:Name="Default"/>
33+
<VisualState x:Name="ParseError">
34+
<Storyboard>
35+
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="txtFloat">
36+
<EasingColorKeyFrame KeyTime="0" Value="#FFFF7F7F"/>
37+
</ColorAnimationUsingKeyFrames>
38+
</Storyboard>
39+
</VisualState>
40+
</VisualStateGroup>
41+
</VisualStateManager.VisualStateGroups>
42+
<StackPanel HorizontalAlignment="Left" Margin="10,0">
43+
<TextBlock Text="Single-Precision" FontSize="36" Foreground="CadetBlue"/>
44+
<Rectangle StrokeThickness="3" Stroke="CadetBlue" Margin="0,5,0,0"/>
45+
</StackPanel>
46+
<StackPanel Margin="10,10">
47+
<Viewbox VerticalAlignment="Center">
48+
<StackPanel x:Name="flippersContainer" Orientation="Horizontal" user:BitFlipper.Click="OnBitFlipperClick">
49+
<user:BitFlipper/>
50+
<user:BitFlipper/>
51+
<user:BitFlipper/>
52+
<user:BitFlipper/>
53+
<user:BitFlipper/>
54+
<user:BitFlipper/>
55+
<user:BitFlipper/>
56+
<user:BitFlipper/>
57+
<user:BitFlipper/>
58+
<user:BitFlipper/>
59+
<user:BitFlipper/>
60+
<user:BitFlipper/>
61+
<user:BitFlipper/>
62+
<user:BitFlipper/>
63+
<user:BitFlipper/>
64+
<user:BitFlipper/>
65+
<user:BitFlipper/>
66+
<user:BitFlipper/>
67+
<user:BitFlipper/>
68+
<user:BitFlipper/>
69+
<user:BitFlipper/>
70+
<user:BitFlipper/>
71+
<user:BitFlipper/>
72+
<user:BitFlipper/>
73+
<user:BitFlipper/>
74+
<user:BitFlipper/>
75+
<user:BitFlipper/>
76+
<user:BitFlipper/>
77+
<user:BitFlipper/>
78+
<user:BitFlipper/>
79+
<user:BitFlipper/>
80+
<user:BitFlipper/>
81+
</StackPanel>
82+
</Viewbox>
83+
<Grid Height="40">
84+
<Grid.RowDefinitions>
85+
<RowDefinition/>
86+
<RowDefinition/>
87+
</Grid.RowDefinitions>
88+
<Grid.ColumnDefinitions>
89+
<ColumnDefinition Width="0.5*"/>
90+
<ColumnDefinition Width="0.5*"/>
91+
<ColumnDefinition Width="0.5*"/>
92+
<ColumnDefinition Width="7*"/>
93+
<ColumnDefinition Width="1*"/>
94+
<ColumnDefinition Width="22*"/>
95+
<ColumnDefinition Width="0.5*"/>
96+
</Grid.ColumnDefinitions>
97+
<Grid.Resources>
98+
<Style TargetType="TextBlock">
99+
<Setter Property="HorizontalAlignment" Value="Center"/>
100+
</Style>
101+
</Grid.Resources>
102+
<Rectangle Grid.Column="1" HorizontalAlignment="Left" Stroke="Black"/>
103+
<shape:Bracket Grid.Column="3"/>
104+
<shape:Bracket Grid.Column="5"/>
105+
<TextBlock Text="Sign" Grid.Row="1" Grid.ColumnSpan="2"/>
106+
<TextBlock Text="Exponent" Grid.Row="1" Grid.Column="3"/>
107+
<TextBlock Text="Fraction" Grid.Row="1" Grid.Column="5"/>
108+
</Grid>
109+
</StackPanel>
110+
<Grid HorizontalAlignment="Center" Margin="0,0,0,10">
111+
<Grid.RowDefinitions>
112+
<RowDefinition/>
113+
<RowDefinition/>
114+
<RowDefinition/>
115+
</Grid.RowDefinitions>
116+
<Grid.ColumnDefinitions>
117+
<ColumnDefinition MinWidth="130"/>
118+
<ColumnDefinition/>
119+
</Grid.ColumnDefinitions>
120+
<Grid.Resources>
121+
<Style TargetType="TextBlock">
122+
<Style.Triggers>
123+
<Trigger Property="Grid.Row" Value="0">
124+
<Setter Property="FontSize" Value="18"/>
125+
</Trigger>
126+
<Trigger Property="Grid.Column" Value="0">
127+
<Setter Property="HorizontalAlignment" Value="Right"/>
128+
</Trigger>
129+
<Trigger Property="Grid.Column" Value="1">
130+
<Setter Property="Opacity" Value="0.75"/>
131+
<Setter Property="Margin" Value="5,0,0,0"/>
132+
</Trigger>
133+
</Style.Triggers>
134+
</Style>
135+
</Grid.Resources>
136+
<TextBox x:Name="txtFloat" Grid.Row="0"
137+
HorizontalContentAlignment="Right"
138+
FontSize="18"
139+
Margin="0,0,0,5" BorderThickness="0,0,0,2" Background="#00FF7F7F"
140+
TextChanged="OnFloatInput"/>
141+
<TextBlock x:Name="txtInt" Grid.Row="1"/>
142+
<TextBlock x:Name="txtIntHex" Grid.Row="2"/>
143+
<TextBlock Grid.Row="0" Grid.Column="1" Text="(as float)"/>
144+
<TextBlock Grid.Row="1" Grid.Column="1" Text="(as integer in decimal)"/>
145+
<TextBlock Grid.Row="2" Grid.Column="1" Text="(as integer in hex)"/>
146+
</Grid>
147+
</StackPanel>
12148
</Window>
+63-15
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,76 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
1+
/**
2+
* Copyright (C) 2019 Dwscdv3 <[email protected]>
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
using System;
619
using System.Windows;
720
using System.Windows.Controls;
8-
using System.Windows.Data;
9-
using System.Windows.Documents;
10-
using System.Windows.Input;
11-
using System.Windows.Media;
12-
using System.Windows.Media.Imaging;
13-
using System.Windows.Navigation;
14-
using System.Windows.Shapes;
21+
using FloatingPointPlayground.UserControls;
1522

1623
namespace FloatingPointPlayground
1724
{
18-
/// <summary>
19-
/// MainWindow.xaml 的交互逻辑
20-
/// </summary>
2125
public partial class MainWindow : Window
2226
{
27+
private bool disableTextChangedEvent = false;
28+
2329
public MainWindow()
2430
{
2531
InitializeComponent();
32+
Update();
33+
}
34+
35+
private void Update()
36+
{
37+
var bin = 0;
38+
for (var i = 0; i < 32; i += 1)
39+
{
40+
bin += ((flippersContainer.Children[31 - i] as BitFlipper).IsChecked == true ? 1 : 0) << i;
41+
}
42+
txtInt.Text = bin.ToString();
43+
txtIntHex.Text = "0x" + bin.ToString("X8");
44+
disableTextChangedEvent = true;
45+
txtFloat.Text = BitConverter.ToSingle(BitConverter.GetBytes(bin), 0).ToString();
46+
disableTextChangedEvent = false;
47+
}
48+
49+
private void OnBitFlipperClick(object sender, RoutedEventArgs e) => Update();
50+
51+
private void OnFloatInput(object sender, TextChangedEventArgs e)
52+
{
53+
if (!disableTextChangedEvent)
54+
{
55+
var textBox = sender as TextBox;
56+
57+
VisualStateManager.GoToElementState(layoutRoot, "Default", true);
58+
59+
if (float.TryParse(textBox.Text, out var f))
60+
{
61+
var bin = BitConverter.ToInt32(BitConverter.GetBytes(f), 0);
62+
for (var i = 0; i < 32; i += 1)
63+
{
64+
(flippersContainer.Children[i] as BitFlipper).IsChecked = (bin & (1 << (31 - i))) != 0;
65+
}
66+
txtInt.Text = bin.ToString();
67+
txtIntHex.Text = "0x" + bin.ToString("X8");
68+
}
69+
else
70+
{
71+
VisualStateManager.GoToElementState(layoutRoot, "ParseError", true);
72+
}
73+
}
2674
}
2775
}
2876
}

0 commit comments

Comments
 (0)