352 lines
10 KiB
C#
352 lines
10 KiB
C#
using Aurora;
|
|
using Aurora.View;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Linq;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
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;
|
|
|
|
namespace AuroraUIComponent.Dialog
|
|
{
|
|
/// <summary>
|
|
/// Interaction logic for UCHome.xaml
|
|
/// </summary>
|
|
public partial class WinKBDNumber : Window, INotifyPropertyChanged
|
|
{
|
|
private const double FLOATING_MAJOR_LEVEL = 0.1;
|
|
private const double FLOATING_MINOR_LEVEL = 0.05;
|
|
private const int INTEGER_MAJOR_LEVEL = 1;
|
|
private const int INTEGER_MINOR_LEVEL = 10;
|
|
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
protected bool SetProperty<S>(
|
|
ref S storage,
|
|
S value,
|
|
[CallerMemberName] string propertyName = null)
|
|
{
|
|
if (Equals(storage, value))
|
|
return false;
|
|
storage = value;
|
|
OnPropertyChanged(propertyName);
|
|
return true;
|
|
}
|
|
|
|
protected void OnPropertyChanged(
|
|
[CallerMemberName] string propertyName = null)
|
|
=> PropertyChanged?.Invoke(
|
|
this,
|
|
new PropertyChangedEventArgs(propertyName)
|
|
);
|
|
|
|
|
|
private double _MajorLevel = FLOATING_MAJOR_LEVEL;
|
|
private double _MinorLevel = FLOATING_MINOR_LEVEL;
|
|
|
|
public string Unit { get => _Unit; set => SetProperty(ref _Unit, value); }
|
|
public string _Unit;
|
|
|
|
public string Value { get => _Value; set => SetProperty(ref _Value, value); }
|
|
public string _Value;
|
|
|
|
public string MajorLevel {
|
|
get {
|
|
if (_IsAllowPoint) {
|
|
return _MajorLevel.ToString();
|
|
}
|
|
|
|
return ((int)_MajorLevel).ToString();
|
|
}
|
|
}
|
|
public string MinorLevel {
|
|
get {
|
|
if (_IsAllowPoint) {
|
|
return _MinorLevel.ToString();
|
|
}
|
|
|
|
return ((int)_MinorLevel).ToString();
|
|
}
|
|
}
|
|
|
|
public double Number;
|
|
private bool IsFirst = true;
|
|
|
|
private bool _IsAllowPoint = true;
|
|
public bool IsAllowPoint {
|
|
set {
|
|
_IsAllowPoint = value;
|
|
if (value) {
|
|
ButtonPoint.IsEnabled = true;
|
|
_MajorLevel = FLOATING_MAJOR_LEVEL;
|
|
_MinorLevel = FLOATING_MINOR_LEVEL;
|
|
} else {
|
|
ButtonPoint.IsEnabled = false;
|
|
_MajorLevel = INTEGER_MAJOR_LEVEL;
|
|
_MinorLevel = INTEGER_MINOR_LEVEL;
|
|
}
|
|
}
|
|
}
|
|
|
|
public bool IsAllowMinus {
|
|
set {
|
|
ButtonMinus.IsEnabled = value;
|
|
if (value) {
|
|
ButtonMinus.Visibility = Visibility.Visible;
|
|
Grid.SetColumn(Button0, 1);
|
|
Grid.SetColumnSpan(Button0, 1);
|
|
} else {
|
|
ButtonMinus.Visibility = Visibility.Collapsed;
|
|
Grid.SetColumn(Button0, 0);
|
|
Grid.SetColumnSpan(Button0, 2);
|
|
}
|
|
}
|
|
}
|
|
|
|
private bool _IsClosing = false;
|
|
private Window _OnwerWindow;
|
|
private FrameworkElement _Element;
|
|
internal Action<string> ResultCallback;
|
|
|
|
public WinKBDNumber()
|
|
{
|
|
InitializeComponent();
|
|
Deactivated += WindowDeactivated;
|
|
DataContext = this;
|
|
}
|
|
|
|
public void SetLevel(double major, double minor)
|
|
{
|
|
_MajorLevel = major;
|
|
_MinorLevel = minor;
|
|
}
|
|
|
|
private void WindowDeactivated(object sender, EventArgs e)
|
|
{
|
|
if (_IsClosing){
|
|
return;
|
|
}
|
|
Close();
|
|
}
|
|
|
|
private void NumberButton_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
Button button = sender as Button;
|
|
var numStr = button.Content.ToString();
|
|
if (Value == "0") {
|
|
if (numStr == "0") {
|
|
return;
|
|
}
|
|
|
|
Value = numStr;
|
|
IsFirst = false;
|
|
return;
|
|
}
|
|
|
|
if (Value.StartsWith("-")) {
|
|
if (Value.Length > 1) {
|
|
if (Value.Substring(1) == "0") {
|
|
if (numStr == "0") {
|
|
return;
|
|
}
|
|
|
|
Value = "-" + numStr;
|
|
IsFirst = false;
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (IsFirst) {
|
|
Value = string.Empty;
|
|
IsFirst = false;
|
|
}
|
|
|
|
AddChracter(numStr);
|
|
}
|
|
|
|
private void ButtonBack_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
Value = Value.Substring(0, Value.Length - 1);
|
|
if (IsFirst) {
|
|
IsFirst = false;
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(Value)) {
|
|
Value = "0";
|
|
return;
|
|
}
|
|
}
|
|
|
|
private void ButtonClear_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
Value = "0";
|
|
if (IsFirst) {
|
|
IsFirst = false;
|
|
}
|
|
}
|
|
|
|
private void ButtonCancel_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
_IsClosing = true;
|
|
Close();
|
|
}
|
|
|
|
private void ButtonPoint_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
if (Value.Contains(".")) {
|
|
return;
|
|
}
|
|
|
|
if (IsFirst) {
|
|
IsFirst = false;
|
|
}
|
|
AddChracter(".");
|
|
}
|
|
|
|
private void ButtonOK_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
if (Value.Last() == '.') {
|
|
Value = Value.Substring(0, Value.Length - 1);
|
|
}
|
|
|
|
if (!double.TryParse(Value, out Number)) {
|
|
return;
|
|
}
|
|
|
|
if (ResultCallback != null)
|
|
ResultCallback(Value);
|
|
|
|
_IsClosing = true;
|
|
Close();
|
|
}
|
|
|
|
private void AddChracter(string text)
|
|
{
|
|
Value = Value + text;
|
|
}
|
|
|
|
private double GetNumber()
|
|
{
|
|
if (Value.Last() == '.') {
|
|
Value = Value.Substring(0, Value.Length - 1);
|
|
}
|
|
|
|
return double.Parse(Value);
|
|
}
|
|
|
|
private void ButtonM05_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
var number = GetNumber() - _MinorLevel;
|
|
Value = number.ToString();
|
|
}
|
|
|
|
private void ButtonP05_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
var number = GetNumber() + _MinorLevel;
|
|
Value = number.ToString();
|
|
}
|
|
|
|
private void ButtonM1_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
var number = GetNumber() - _MajorLevel;
|
|
Value = number.ToString();
|
|
}
|
|
|
|
private void ButtonP1_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
var number = GetNumber() + _MajorLevel;
|
|
Value = number.ToString();
|
|
}
|
|
|
|
private void ButtonMinus_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
Value = "-";
|
|
IsFirst = false;
|
|
}
|
|
|
|
private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
|
|
{
|
|
SetWindowControl(_OnwerWindow, _Element);
|
|
}
|
|
|
|
internal void SetControl(FrameworkElement element)
|
|
{
|
|
Window main = Application.Current.MainWindow;
|
|
SetControl(main, element);
|
|
}
|
|
|
|
internal void SetWindowControl(Window window, FrameworkElement element)
|
|
{
|
|
SetControl(window, element);
|
|
}
|
|
|
|
private void SetControl(Window window, FrameworkElement element)
|
|
{
|
|
_OnwerWindow = window;
|
|
_Element = element;
|
|
|
|
Window main = Application.Current.MainWindow;
|
|
Point mainPosition = main.PointToScreen(new Point(0d, 0d));
|
|
|
|
Point relativePoint = element.TransformToAncestor(window)
|
|
.Transform(new Point(0, 0));
|
|
|
|
Point position = element.PointToScreen(new Point(0d, 0d)),
|
|
controlPosition = window.PointToScreen(new Point(0d, 0d));
|
|
// Right
|
|
relativePoint.X += controlPosition.X;
|
|
relativePoint.Y += controlPosition.Y;
|
|
|
|
// 1. Right, Middle
|
|
Left = relativePoint.X + element.ActualWidth;
|
|
Top = relativePoint.Y - 45;
|
|
if (Left + this.Width < mainPosition.X + main.Width) {
|
|
if (Top + this.Height < mainPosition.Y + main.Height) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
// 2. MiddleRight, Bottom
|
|
Left = Left - this.Width;
|
|
Top = relativePoint.Y + element.ActualHeight;
|
|
if (Left + this.Width < mainPosition.X + main.Width) {
|
|
if (Top + this.Height < mainPosition.Y + main.Height) {
|
|
return;
|
|
}
|
|
// 3. MiddleRight, Top
|
|
Top = relativePoint.Y - this.Height;
|
|
if (Left + this.Width < mainPosition.X + main.Width) {
|
|
if (Top + this.Height < mainPosition.Y + main.Height) {
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
// 4. Left, Middle
|
|
Left = relativePoint.X - this.Width;
|
|
Top = relativePoint.Y - 45;
|
|
if (Top + this.Height < mainPosition.Y + main.Height) {
|
|
if (Left > mainPosition.X) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
// 3. Middle, Top
|
|
Left = relativePoint.X;
|
|
Top = relativePoint.Y - this.Height;
|
|
|
|
// 5. Left, Top
|
|
Left = relativePoint.X - this.Width + element.ActualWidth;
|
|
}
|
|
}
|
|
} |