Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GaussianProcessTuning/ILNumerics.2.14.4735.573/Drawing/Platform/OpenGL/ILOGLLegend.cs @ 10903

Last change on this file since 10903 was 9102, checked in by gkronber, 12 years ago

#1967: ILNumerics source for experimentation

File size: 8.3 KB
Line 
1///
2///    This file is part of ILNumerics Community Edition.
3///
4///    ILNumerics Community Edition - high performance computing for applications.
5///    Copyright (C) 2006 - 2012 Haymo Kutschbach, http://ilnumerics.net
6///
7///    ILNumerics Community Edition is free software: you can redistribute it and/or modify
8///    it under the terms of the GNU General Public License version 3 as published by
9///    the Free Software Foundation.
10///
11///    ILNumerics Community Edition is distributed in the hope that it will be useful,
12///    but WITHOUT ANY WARRANTY; without even the implied warranty of
13///    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14///    GNU General Public License for more details.
15///
16///    You should have received a copy of the GNU General Public License
17///    along with ILNumerics Community Edition. See the file License.txt in the root
18///    of your distribution package. If not, see <http://www.gnu.org/licenses/>.
19///
20///    In addition this software uses the following components and/or licenses:
21///
22///    =================================================================================
23///    The Open Toolkit Library License
24///   
25///    Copyright (c) 2006 - 2009 the Open Toolkit library.
26///   
27///    Permission is hereby granted, free of charge, to any person obtaining a copy
28///    of this software and associated documentation files (the "Software"), to deal
29///    in the Software without restriction, including without limitation the rights to
30///    use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
31///    the Software, and to permit persons to whom the Software is furnished to do
32///    so, subject to the following conditions:
33///
34///    The above copyright notice and this permission notice shall be included in all
35///    copies or substantial portions of the Software.
36///
37///    =================================================================================
38///   
39
40using System;
41using System.Collections.Generic;
42using System.Text;
43using System.Drawing;
44using ILNumerics.Drawing.Labeling;
45using ILNumerics.Drawing;
46using ILNumerics.Drawing.Interfaces;
47using ILNumerics.Drawing.Misc;
48using ILNumerics.Drawing.Graphs;
49using OpenTK.Graphics;
50
51namespace ILNumerics.Drawing.Platform.OpenGL {
52    public class ILOGLLegend : ILLegend {
53
54        #region attributes
55
56        #endregion
57
58        #region constructor
59        internal ILOGLLegend (ILPanel panel) : base (panel) {}
60        #endregion
61
62        #region abstract overloads
63        /// <summary>
64        /// draw legend into opengl context
65        /// </summary>
66        /// <param name="p">rendering properties</param>
67        /// <param name="area">area to draw the content into, if g is null, this will be ignored also</param>
68        public override void Draw(ILRenderProperties p, System.Drawing.Rectangle area) {
69            if (!m_visible) return;
70            // handle drawing to bitmap
71            if (p.Graphics != null) {
72                base.Draw(p, area);
73                return;
74            }
75            // draw to OpenGL context
76            if (GraphicsContext.CurrentContext == null)
77                throw new GraphicsContextException("No OpenGL GraphicsContext current is selected for the calling thread.");
78
79            #region set 2D projection   
80            float[] viewport = new float[4];
81            GL.GetFloat(GetPName.Viewport, viewport);
82            GL.MatrixMode(MatrixMode.Projection);
83            GL.PushMatrix();     
84            GL.LoadIdentity();
85            GL.Ortho(viewport[0], viewport[2], viewport[3], viewport[1], -1.0, 1.0);
86
87            GL.MatrixMode(MatrixMode.Modelview);
88            GL.PushMatrix();
89            GL.LoadIdentity();
90
91            //GL.PushAttrib(AttribMask.TextureBit | AttribMask.EnableBit | AttribMask.ColorBufferBit);
92
93            //GL.Enable(EnableCap.Texture2D);
94            GL.Enable(EnableCap.Blend);
95            GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
96           
97            GL.Disable(EnableCap.DepthTest);
98            #endregion
99
100            #region draw bounding box + bg
101            Size labelSize = Size.Empty, boxSize = Size.Empty;
102            Rectangle sampleRect = Rectangle.Empty, labelRect = Rectangle.Empty;
103            List<IILLegendRenderer> renderer = getRenderers(ref labelSize);
104            // automatically size the box
105            boxSize.Width = m_padding.Horizontal+labelSize.Width + 30; // <- default width for sample
106            boxSize.Height = m_padding.Bottom+labelSize.Height+m_padding.Top*renderer.Count;
107            if (!m_size.IsEmpty) {
108                // prefer given size, with limitations
109                boxSize.Width = Math.Max(boxSize.Width,m_size.Width);
110                boxSize.Height = Math.Max(boxSize.Height,m_size.Height);
111            }
112            Point location = Point.Empty;
113            if (m_location.IsEmpty) {
114                int minx = m_panel.ClientSize.Width - boxSize.Width - m_border.Width * 2;
115                int miny = m_panel.ClientSize.Height - boxSize.Height - m_border.Width * 2;
116                // by default: place legend at upper right corner
117                location.X = m_panel.ClientSize.Width - boxSize.Width - m_panel.Padding.Right - m_border.Width * 2;
118                if (location.X < 0) location.X = 0;
119                if (location.X > minx) location.X = minx;
120                location.Y = m_panel.Padding.Top;
121                if (location.Y < 0) location.Y = 0;
122                if (location.Y > miny) location.Y = miny;
123            } else
124                location = new Point((int)(m_location.X * (float)m_panel.ClientSize.Width)
125                            ,(int)(m_location.Y * (float)m_panel.ClientSize.Height));
126
127            float offsX = Math.Max(m_border.Width / 2.0f,1.0f);
128            m_bgColor = Color.FromArgb((int)(m_opacity * 255),m_bgColor);
129            GL.Color4(m_bgColor);
130            GL.ShadeModel(ShadingModel.Flat);
131            GL.Begin(BeginMode.Quads);
132                GL.Vertex2(location.X + offsX,              location.Y + offsX);
133                GL.Vertex2(location.X + boxSize.Width-offsX,location.Y + offsX);
134                GL.Vertex2(location.X + boxSize.Width-offsX,location.Y + boxSize.Height - offsX);
135                GL.Vertex2(location.X + offsX,              location.Y + boxSize.Height - offsX);
136                GL.Vertex2(location.X + offsX,              location.Y + offsX);
137            GL.End();
138           
139            ILNumerics.Drawing.Platform.OpenGL.ILOGLPanel.SetupLineStyle(m_border);
140            GL.Begin(BeginMode.LineStrip);
141                GL.Vertex2(location.X + offsX,              location.Y + offsX);
142                GL.Vertex2(location.X + boxSize.Width-offsX,location.Y + offsX);
143                GL.Vertex2(location.X + boxSize.Width-offsX,location.Y + boxSize.Height - offsX);
144                GL.Vertex2(location.X + offsX,              location.Y + boxSize.Height - offsX);
145                GL.Vertex2(location.X + offsX,              location.Y + offsX);
146            GL.End();
147            #endregion
148           
149            sampleRect.Y = location.Y+m_padding.Top;
150            sampleRect.Width = boxSize.Width - 2 * m_padding.Right - labelSize.Width;
151            sampleRect.X = location.X + m_padding.Left;
152           
153            labelRect.Y = location.Y+m_padding.Top;
154            labelRect.Width = labelSize.Width;
155            labelRect.X = sampleRect.Right + m_padding.Right;
156           
157            for (int i = 0; i < renderer.Count; i++) {
158                IILLegendRenderer rend = renderer[i];
159                sampleRect.Height = rend.LabelSize.Height;
160                labelRect.Height = sampleRect.Height;
161                try {
162                    rend.DrawToLegend(p,sampleRect,labelRect);
163                } catch (Exception) {}
164                sampleRect.Y = sampleRect.Bottom + m_padding.Top;
165                labelRect.Y = sampleRect.Y;
166            }
167
168            #region restore previous projection
169            //GL.PopAttrib();
170            GL.MatrixMode(MatrixMode.Modelview);
171            GL.PopMatrix();
172            GL.MatrixMode(MatrixMode.Projection);
173            GL.PopMatrix();
174            #endregion
175        }
176        #endregion
177    }
178}
Note: See TracBrowser for help on using the repository browser.