!B (Sandbox) (CE-21795) Importing models with multisubmaterials via fbx switches...
[CRYENGINE.git] / Code / Tools / Statoscope / Statoscope / BucketListView.cs
blob0460f1ff3b29eda05ac88cf9a323f99ad4bf3df3
1 // Copyright 2001-2019 Crytek GmbH / Crytek Group. All rights reserved.
3 using System;
4 using System.Collections.Generic;
5 using System.Linq;
6 using System.Text;
7 using System.Reflection;
8 using System.ComponentModel;
9 using System.Windows.Forms;
11 namespace Statoscope
13 class BucketListView : ListView
15 public BucketListView(BucketSet bucketSet)
17 InitializeComponent();
19 View = View.Details;
20 Dock = DockStyle.Fill;
21 GridLines = true;
22 FullRowSelect = true;
23 VirtualMode = true;
24 VirtualListSize = bucketSet.m_buckets.Count;
25 RetrieveVirtualItem += new RetrieveVirtualItemEventHandler(bucketsListView_RetrieveVirtualItem);
26 CacheVirtualItems += new CacheVirtualItemsEventHandler(bucketsListView_CacheVirtualItems);
27 StatoscopeForm.SetDoubleBuffered(this);
29 int numColHeaders = CountHeaders(bucketSet);
30 int colHeaderWidth = Math.Max(50, (Width) / numColHeaders);
32 Columns.Add(LogControl.CreateColumnHeader("Bucket", colHeaderWidth));
33 CreateHeaders(bucketSet, colHeaderWidth);
35 m_bucketSet = bucketSet;
36 m_lviCache = new List<ListViewItem>(bucketSet.m_buckets.Count);
37 UpdateLviCache();
40 public void UpdateLviCache()
42 m_lviCache.Clear();
44 foreach (Bucket bucket in m_bucketSet.m_buckets)
46 ListViewItem lvi = new ListViewItem(bucket.RangeDescription);
48 foreach (PropertyInfo pi in m_columnProperties)
49 lvi.SubItems.Add(GetPropertyValAsString(pi, bucket));
51 m_lviCache.Add(lvi);
55 protected override void OnCreateControl()
57 base.OnCreateControl();
59 ContextMenuStrip = m_contextMenu;
60 m_copyMenuItem.Click += new EventHandler(m_copyMenuItem_Click);
63 void bucketsListView_CacheVirtualItems(object sender, CacheVirtualItemsEventArgs e)
65 BucketListView blv = (BucketListView)sender;
66 blv.UpdateLviCache();
69 void bucketsListView_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
71 BucketListView blv = (BucketListView)sender;
72 e.Item = blv.m_lviCache[e.ItemIndex];
75 private string GetPropertyValAsString(PropertyInfo pi, Bucket b)
77 object val = pi.GetValue(b, null);
79 string s = string.Empty;
80 if (val is int)
81 s = string.Format("{0:n0}", val);
82 else if (val is float)
83 s = string.Format("{0:n2}%", val);
84 else if (val is TimeSpan)
85 s = string.Format("{0:n2}s", ((TimeSpan)val).TotalMilliseconds / 1000.0f);
87 return s;
90 void m_copyMenuItem_Click(object sender, EventArgs e)
92 StringBuilder sb = new StringBuilder();
94 sb.Append("Bucket\t");
95 foreach (PropertyInfo pi in m_columnProperties)
96 sb.AppendFormat("{0}\t", pi.Name.ToString());
97 sb.AppendLine();
99 foreach (Bucket bucket in m_bucketSet.m_buckets)
101 sb.AppendFormat("{0}\t", bucket.RangeDescription);
102 foreach (PropertyInfo pi in m_columnProperties)
103 sb.AppendFormat("{0}\t", GetPropertyValAsString(pi, bucket));
105 sb.AppendLine();
108 Clipboard.SetText(sb.ToString());
111 private Attribute GetFirstAttribute(PropertyInfo pi, Type attrType, bool inherits)
113 foreach (var attr in pi.GetCustomAttributes(attrType, inherits))
114 return (Attribute)attr;
115 return null;
118 private IEnumerable<PropertyInfo> EnumColumns(BucketSet bset)
120 if (bset.m_buckets.Count > 0)
122 Type bucketType = bset.m_buckets[0].GetType();
124 // Assuming that all buckets have the same type..
126 foreach (PropertyInfo prop in bucketType.GetProperties())
128 // Ensure it's browseable
129 var ba = GetFirstAttribute(prop, typeof(BrowsableAttribute), true);
130 bool isBrowseable = (ba != null) ? ((BrowsableAttribute)ba).Browsable : true;
132 if (isBrowseable)
133 yield return prop;
138 private int CountHeaders(BucketSet bucketSet)
140 int numHeaders = 0;
142 foreach (var pi in EnumColumns(bucketSet))
143 ++numHeaders;
145 return numHeaders;
148 private void CreateHeaders(BucketSet bucketSet, int colHeaderWidth)
150 m_columnProperties = new List<PropertyInfo>();
151 foreach (var pi in EnumColumns(bucketSet))
153 var descAttr = GetFirstAttribute(pi, typeof(DescriptionAttribute), true);
154 string desc = descAttr != null ? ((DescriptionAttribute)descAttr).Description : pi.Name;
155 Columns.Add(LogControl.CreateColumnHeader(desc, colHeaderWidth));
156 m_columnProperties.Add(pi);
160 private BucketSet m_bucketSet;
161 private List<PropertyInfo> m_columnProperties;
162 private IContainer components;
163 private ContextMenuStrip m_contextMenu;
164 private ToolStripMenuItem m_copyMenuItem;
165 private List<ListViewItem> m_lviCache;
167 private void InitializeComponent()
169 this.components = new System.ComponentModel.Container();
170 this.m_contextMenu = new System.Windows.Forms.ContextMenuStrip(this.components);
171 this.m_copyMenuItem = new System.Windows.Forms.ToolStripMenuItem();
172 this.m_contextMenu.SuspendLayout();
173 this.SuspendLayout();
175 // m_contextMenu
177 this.m_contextMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
178 this.m_copyMenuItem});
179 this.m_contextMenu.Name = "m_contextMenu";
180 this.m_contextMenu.Size = new System.Drawing.Size(103, 26);
182 // m_copyMenuItem
184 this.m_copyMenuItem.Name = "m_copyMenuItem";
185 this.m_copyMenuItem.Size = new System.Drawing.Size(102, 22);
186 this.m_copyMenuItem.Text = "&Copy";
187 this.m_contextMenu.ResumeLayout(false);
188 this.ResumeLayout(false);