WeighterColorDB.cs 4.61 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4
using System;
using System.Collections.Generic;

using System.Globalization;
5
using System.IO;
潘栩锋's avatar
潘栩锋 committed
6 7 8 9 10 11 12 13 14
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;

namespace FLY.Weight.UI.Client
{
    public class WeighterColorDB
15
    {        
潘栩锋's avatar
潘栩锋 committed
16 17 18 19 20 21 22 23
        class WeighterColor
        {
            public Brush Self;
            public List<Brush> Items = new List<Brush>();
            public WeighterColor()
            {
            }
        }
24
        static List<WeighterColor> mWeighters;
潘栩锋's avatar
潘栩锋 committed
25 26
        static WeighterColorDB()
        {
27 28 29 30 31 32 33 34 35
            //if (File.Exists("default/weighterColorDb.json"))
            //{
            //    string json = File.ReadAllText("default/weighterColorDb.json");
            //    try
            //    {
            //        mWeighters = Newtonsoft.Json.JsonConvert.DeserializeObject<List<WeighterColor>>(json);
            //    }
            //    catch { 
            //        //异常,从本地读取
潘栩锋's avatar
潘栩锋 committed
36

37 38
            //    }
            //}
潘栩锋's avatar
潘栩锋 committed
39

40 41

            //if(mWeighters == null || mWeighters.Count()==0)
潘栩锋's avatar
潘栩锋 committed
42
            {
43
                ResourceDictionary r = new ResourceDictionary()
潘栩锋's avatar
潘栩锋 committed
44
                {
45 46
                    Source = new Uri("pack://application:,,,/FLY.Weight.UI.Client;component/Themes/Dictionary_CellColor.xaml")
                };
47
                
48
                mWeighters = new List<WeighterColor>();
49

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
                int color_cnt = 0;
                //找出颜色数量
                while (true) 
                {
                    int i = color_cnt + 1;
                    string color_name = $"Color_g{i}#0";
                    if (!r.Contains(color_name))
                    {
                        //上一个已经是极限了
                        break;
                    }
                    color_cnt = i;
                }

                if (color_cnt < 3)
                    throw new Exception("称重颜色不够3个!!!!");

                for (int i = 0; i < color_cnt; i++)
68 69
                {
                    WeighterColor c = new WeighterColor();
70
                    string color_name = $"Color_g{i+1}#0";
71
                    c.Self = r[color_name] as Brush;
72
                    for (int j = 0; j < color_cnt; j++)
73
                    {
74
                        int i2 = i + 1 + j;
75 76
                        if (i2 > color_cnt)
                            i2 -= color_cnt;
77
                        color_name = $"Color_g{i2}#0";
78 79
                        c.Items.Add(r[color_name] as Brush);
                    }
80
                    mWeighters.Add(c);
潘栩锋's avatar
潘栩锋 committed
81 82 83 84
                }
            }
        }

85
        public static Brush GetSelf(int index)
潘栩锋's avatar
潘栩锋 committed
86
        {
87 88 89
            //颜色循环
            index = GetNumberIndex(index);
            if(index<0)
潘栩锋's avatar
潘栩锋 committed
90
                return null;
91

92
            return mWeighters[index].Self; 
潘栩锋's avatar
潘栩锋 committed
93
        }
94 95 96 97 98 99 100 101 102 103 104 105 106 107
        static int GetNumberIndex(int index) 
        {
            //颜色循环
            if (mWeighters.Count() <= 0)
                return -1;

            while (index < 0)
                index += mWeighters.Count();

            while (index >= mWeighters.Count())
                index -= mWeighters.Count();

            return index;
        }
108
        public static Brush GetItem(int numberIndex, int index)
潘栩锋's avatar
潘栩锋 committed
109
        {
110 111 112
            //颜色循环
            numberIndex = GetNumberIndex(numberIndex);
            if (numberIndex < 0)
潘栩锋's avatar
潘栩锋 committed
113
                return null;
114

115
            int cnt = mWeighters[numberIndex].Items.Count();
116
            if (index >= cnt || index < 0)
潘栩锋's avatar
潘栩锋 committed
117 118
            {
                Random r = new Random();
119
                index = r.Next(cnt - 1);
潘栩锋's avatar
潘栩锋 committed
120
            }
121
            return mWeighters[numberIndex].Items[index];
潘栩锋's avatar
潘栩锋 committed
122 123 124 125 126 127
        }
    }
    public class Number2ColorValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
128 129
            int index = (int)value;
            return WeighterColorDB.GetSelf(index);
潘栩锋's avatar
潘栩锋 committed
130 131 132 133 134 135 136 137 138 139
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class Number2ColorItemValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
140
            int number_index = (int)value;
潘栩锋's avatar
潘栩锋 committed
141
            int index = (int)parameter;
142
            return WeighterColorDB.GetItem(number_index, index);
潘栩锋's avatar
潘栩锋 committed
143 144 145 146 147 148 149 150
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}