summaryrefslogtreecommitdiff
path: root/ProjectedStripBoard/ProjectedStrip.cs
blob: b6032e60e9f6d145e03ca18180eed19a813b2486 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using System.Drawing;
using AnotoData;

namespace ProjectedStripBoard
{
    public class ProjectedStrip
    {
        public enum DrawingMod { Normal, Markers };

        #region Static members

        static public float DX = 0.050f;
        static public float DY = 0.03f;
        static public float MarkerWidth = 0.062f;
        static public float MarkerHeight = 0.075f;

        static public Vector2 TopLeft = new Vector2(0.08f, 0.13f);
        static public Vector2 BottomRight = new Vector2(0.93f, 0.89f);

        static public Font TheFont = new Font("Arial", 20.0f, FontStyle.Bold);

        static public Color DefaultColor = Color.FromArgb(100,100,100,100);
        static public Brush DefaultTextColor = Brushes.Black;
        static public Brush SelectedStripBrushColor = new SolidBrush(Color.FromArgb(150,0,50,100));
       
        #endregion

        public Color StripColor = ProjectedStrip.DefaultColor;
        public Brush TextColor = ProjectedStrip.DefaultTextColor;

        public Vector2 Postition;
        public string Name;
        public Color Color = Color.Blue;

        public AnotoStrip TheAnotoStrip;
        public bool[] VisibleTags;

        public ProjectedStrip(AnotoStrip item)
        {
            TheAnotoStrip = item;
            VisibleTags = new bool[TheAnotoStrip.TagIds.Length];
        }

        public void ClearVisibleTags()
        {
            VisibleTags = new bool[TheAnotoStrip.TagIds.Length];
        }

       static public Vector2 GetCorrectedPostion(Vector2 pos)
        {
            float corretedX = GenericScaleF(pos.X, 0, TopLeft.X, 1, BottomRight.X);
            float corretedY = GenericScaleF(pos.Y, 0, TopLeft.Y, 1, BottomRight.Y);

            return new Vector2(corretedX, corretedY);
        }

        public void Draw(DrawingMod drawingMod, Color BackColor, Graphics g, float screenWidth, float screenHeight, Font font)
        {
           //First, we need to scale the view
           Vector2 correctedPos =  GetCorrectedPostion(Postition);

        
            RectangleF OuterBound = new RectangleF((correctedPos.X - DX) * screenWidth, (correctedPos.Y - DY) * screenHeight, (MarkerWidth*5) * screenWidth, (MarkerHeight) * screenHeight);
          
            string text = TheAnotoStrip.CallSign;
            SizeF size =  g.MeasureString(text, TheFont);

            if (drawingMod == DrawingMod.Markers)
            {
                for (int i = 0; i < VisibleTags.Length; i++)
                {
                    if (VisibleTags[i])
                    {
                        OuterBound = new RectangleF((correctedPos.X + i * MarkerWidth - DX) * screenWidth, (correctedPos.Y - DY) * screenHeight, (MarkerWidth) * screenWidth, (MarkerHeight) * screenHeight);

                        g.DrawRectangle(new Pen(Color, 1.0f), OuterBound.X, OuterBound.Y, OuterBound.Width, OuterBound.Height);
                        //   g.DrawString(Name, font, Brushes.Black, Postition.X * screenWidth, Postition.Y * screenHeight);
                        g.DrawString(TheAnotoStrip.TagIds[i].ToString(), TheFont, TextColor, new PointF(OuterBound.X, OuterBound.Y + TheFont.Height));
                    }
                }
            }

            if (drawingMod == DrawingMod.Normal)
            {
                //Draw only if one tag is visible
                if (VisibleTags.Contains(true))
                {
                    if ( IsSelected)
                        g.FillRectangle(Brushes.DarkBlue, OuterBound);
                    else
                    g.FillRectangle(new SolidBrush(StripColor), OuterBound);
                    //  g.DrawString(Name, font, Brushes.Black, Postition.X * screenWidth, Postition.Y * screenHeight);
            //        g.DrawString(text, TheFont, TextColor, new PointF(OuterBound.X + (OuterBound.Width - size.Width) / 2, OuterBound.Y + (OuterBound.Height - size.Height) / 2));
                    g.DrawString(text, TheFont, TextColor, new PointF(OuterBound.X + 10, OuterBound.Y + 10));
                }

                 if ( !(VisibleTags.Contains(true)) && IsSelected )
                 {
                     //This is strip that is not in the strip board, then display it as a virtual one
                     this.Postition = new Vector2(0.3f,1.05f);
                     g.FillRectangle(SelectedStripBrushColor, OuterBound);
                     g.DrawString(text, TheFont, TextColor, new PointF(OuterBound.X + (OuterBound.Width - size.Width) / 2, OuterBound.Y + (OuterBound.Height - size.Height) / 2));
              
                 }
            }
        }

        static public float GenericScaleF(float input, float i1, float o1, float i2, float o2)
        {
            if (i2 == i1) return ((o2 + o1) / 2.0f);
            float a = (o2 - o1) / (i2 - i1);
            float b = o1 - a * i1;
            return (a * input + b);
        }


        /// <summary>
        /// Return the requested strip with the marker index within the strip
        /// </summary>
        /// <param name="strips"></param>
        /// <param name="markerId"></param>
        /// <param name="idIndex"></param>
        /// <returns></returns>
        public static ProjectedStrip FindStrip(List<ProjectedStrip> strips, int markerId, out int idIndex)
        {
            idIndex = -1;
            foreach (var strip in strips)
            {
                if (strip.HasThisMarker(markerId))
                {
                    idIndex = Array.IndexOf(strip.TheAnotoStrip.TagIds, markerId);
                    return strip;
                }
            }
            return null;

        }

        private bool HasThisMarker(int markerId)
        {
            return TheAnotoStrip.TagIds.Contains(markerId);
        }

        /// <summary>
        /// Update the strip position with the given tag index
        /// </summary>
        /// <param name="tagIndex"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        internal void UpdatePosition(int tagIndex, float x, float y)
        {
            //if (tagIndex == 0)
            //{
            this.Postition.X = x - tagIndex * MarkerWidth;
            this.Postition.Y = y;
            VisibleTags[tagIndex] = true;
            //}
            //else
            //    VisibleTags[tagIndex] = false;

        }

        public bool IsSelected { get; set; }
    }
}