Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/SharpVectorCss/Css/CssPrimitiveAngleValue.cs @ 14035

Last change on this file since 14035 was 12762, checked in by aballeit, 9 years ago

#2283 GUI updates, Tree-chart, MCTS Version 2 (prune leaves)

File size: 2.6 KB
Line 
1using System;
2using System.Text.RegularExpressions;
3
4namespace SharpVectors.Dom.Css
5{
6    public sealed class CssPrimitiveAngleValue : CssPrimitiveValue
7  {
8    #region Constructors
9    public CssPrimitiveAngleValue(string number, string unit, bool readOnly) : base(number+unit, readOnly)
10    {
11      _setType(unit);
12      SetFloatValue(number);
13    }
14
15    public CssPrimitiveAngleValue(string cssText, bool readOnly) : base(cssText, readOnly)
16    {
17      OnSetCssText(cssText);
18    }
19
20    public CssPrimitiveAngleValue(double number, string unit, bool readOnly) : base(number+unit, readOnly)
21    {
22      _setType(unit);
23      SetFloatValue(number);
24    }
25    #endregion
26
27    protected override void OnSetCssText(string cssText)
28    {
29      Regex re = new Regex(CssValue.AnglePattern);
30      Match match = re.Match(cssText);
31      if(match.Success)
32      {
33        _setType(match.Groups["angleUnit"].Value);
34        SetFloatValue(match.Groups["angleNumber"].Value);
35      }
36      else
37      {
38        throw new DomException(DomExceptionType.SyntaxErr, "Unrecognized angle format: " + cssText);
39      }
40    }
41    private void _setType(string unit)
42    {
43      switch(unit)
44      {
45        case "":
46          SetPrimitiveType(CssPrimitiveType.Number);
47          break;
48        case "deg":
49          SetPrimitiveType(CssPrimitiveType.Deg);
50          break;
51        case "rad":
52          SetPrimitiveType(CssPrimitiveType.Rad);
53          break;
54        case "grad":
55          SetPrimitiveType(CssPrimitiveType.Grad);
56          break;
57        default:
58          throw new DomException(DomExceptionType.SyntaxErr, "Unknown angle unit");
59      }
60    }
61
62    // only for absolute values
63    private double _getDegAngle()
64    {
65      double ret;
66      switch(PrimitiveType)
67      {
68        case CssPrimitiveType.Rad:
69          ret = floatValue * 180 / Math.PI;
70          break;
71        case CssPrimitiveType.Grad:
72          ret = floatValue *90 / 100;
73          break;
74        default:
75          ret = floatValue;
76          break;
77      }
78      ret %= 360;
79      while(ret<0) ret += 360;
80
81      return ret;
82    }
83
84    public override double GetFloatValue(CssPrimitiveType unitType)
85    {
86      double ret = Double.NaN;
87      switch(unitType)
88      {
89        case CssPrimitiveType.Number:
90        case CssPrimitiveType.Deg:
91          ret = _getDegAngle();
92          break;
93        case CssPrimitiveType.Rad:
94          ret = _getDegAngle() * Math.PI / 180;
95          break;
96        case CssPrimitiveType.Grad:
97          ret = _getDegAngle() * 100 / 90;
98          break;
99      }
100      if(Double.IsNaN(ret))
101      {
102        throw new DomException(DomExceptionType.InvalidAccessErr);
103      }
104      else
105      {
106        return ret;
107      }
108    }
109
110    public override string CssText
111    {
112      get
113      {
114        return GetFloatValue(PrimitiveType).ToString(CssNumber.Format) + PrimitiveTypeAsString;
115      }
116    }
117  }
118}
Note: See TracBrowser for help on using the repository browser.