aboutsummaryrefslogtreecommitdiff
path: root/ShiftOS.Frontend/GUI
diff options
context:
space:
mode:
authorMichael <[email protected]>2017-08-05 15:44:09 -0400
committerMichael <[email protected]>2017-08-05 15:44:09 -0400
commitec363f09caa99f8b0ffa9351950fed5629f396f7 (patch)
tree686ef8fb3d48a389613178c2b4c4f964a2839e07 /ShiftOS.Frontend/GUI
parent01683f1fcb8a593af66b0a882db7b3a2beba9fa0 (diff)
downloadshiftos_thereturn-ec363f09caa99f8b0ffa9351950fed5629f396f7.tar.gz
shiftos_thereturn-ec363f09caa99f8b0ffa9351950fed5629f396f7.tar.bz2
shiftos_thereturn-ec363f09caa99f8b0ffa9351950fed5629f396f7.zip
file skimmer icons
Diffstat (limited to 'ShiftOS.Frontend/GUI')
-rw-r--r--ShiftOS.Frontend/GUI/ListView.cs199
1 files changed, 199 insertions, 0 deletions
diff --git a/ShiftOS.Frontend/GUI/ListView.cs b/ShiftOS.Frontend/GUI/ListView.cs
new file mode 100644
index 0000000..b74203d
--- /dev/null
+++ b/ShiftOS.Frontend/GUI/ListView.cs
@@ -0,0 +1,199 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Microsoft.Xna.Framework;
+using Microsoft.Xna.Framework.Graphics;
+using ShiftOS.Frontend.GraphicsSubsystem;
+using static ShiftOS.Engine.SkinEngine;
+
+namespace ShiftOS.Frontend.GUI
+{
+ public class ListView : Control
+ {
+ private List<ListViewItem> _items = null;
+ private Dictionary<string, Texture2D> _images = null;
+ private const int _itemimagemargin = 15;
+ private const int _initialmargin = 20;
+ private const int _itemgap = 5;
+ private int scroll = 0;
+ private const int defaulttexturesize = 42;
+ private int _selected = -1;
+
+ public ListView()
+ {
+ _items = new List<ListViewItem>();
+ _images = new Dictionary<string, Texture2D>();
+ Click += () =>
+ {
+ using (var gfx = System.Drawing.Graphics.FromHwnd(IntPtr.Zero))
+ {
+ int _itemx = _initialmargin;
+ int _itemy = _initialmargin - scroll;
+ int yhelper = 0;
+ foreach (var item in _items)
+ {
+ Texture2D image = null;
+ int texwidth = defaulttexturesize;
+ int texheight = defaulttexturesize;
+ if (_images.ContainsKey(item.ImageKey))
+ {
+ texwidth = _images[item.ImageKey].Width;
+ texheight = _images[item.ImageKey].Height;
+ image = _images[item.ImageKey];
+ }
+ int textwidth = texwidth + (_itemimagemargin * 2);
+ var textmeasure = gfx.MeasureString(item.Text, LoadedSkin.MainFont, textwidth);
+ yhelper = Math.Max(yhelper, _itemy + texheight + (int)textmeasure.Height);
+
+ int texty = _itemy + texheight;
+ int textx = _itemx + ((textwidth - (int)textmeasure.Width) / 2);
+
+ if(MouseX >= _itemx && MouseX <= _itemx + textwidth)
+ {
+ if(MouseY >= _itemy && MouseY <= _itemy + texheight + (int)textmeasure.Height)
+ {
+ _selected = _items.IndexOf(item);
+ Invalidate();
+ return;
+ }
+ }
+
+ _itemx += textwidth + _itemgap;
+ if (_itemx >= (Width - (_initialmargin * 2)))
+ {
+ _itemx = _initialmargin;
+ _itemy += yhelper;
+ }
+ }
+ }
+ _selected = -1;
+ Invalidate();
+ };
+ }
+
+ public int SelectedIndex
+ {
+ get
+ {
+ return _selected;
+ }
+ set
+ {
+ if (value == _selected)
+ return;
+ _selected = MathHelper.Clamp(value, -1, _items.Count - 1);
+ Invalidate();
+ }
+ }
+
+ public ListViewItem SelectedItem
+ {
+ get
+ {
+ if (_selected == -1)
+ return null;
+ return _items[_selected];
+ }
+ }
+
+ public void ClearItems()
+ {
+ _items.Clear();
+ scroll = 0;
+ _selected = -1;
+ Invalidate();
+ }
+
+ public void RemoveItem(ListViewItem item)
+ {
+ if (!_items.Contains(item))
+ throw new ArgumentException("This list view doesn't contain that item.");
+ if (_selected == _items.IndexOf(item))
+ _selected = -1;
+ _items.Remove(item);
+ Invalidate();
+ }
+
+ public void AddItem(ListViewItem item)
+ {
+ if (_items.Contains(item))
+ throw new ArgumentException("Item already exists in this listview.");
+ _items.Add(item);
+ Invalidate();
+ }
+
+ public void SetImage(string key, Texture2D value)
+ {
+ if (_images.ContainsKey(key))
+ _images[key] = value;
+ else
+ _images.Add(key, value);
+ Invalidate();
+ }
+
+ public Texture2D GetImage(string key)
+ {
+ if (_images.ContainsKey(key))
+ return _images[key];
+ return null;
+ }
+
+ public void ClearImages()
+ {
+ _images.Clear();
+ }
+
+ protected override void OnPaint(GraphicsContext gfx)
+ {
+ gfx.Clear(LoadedSkin.ControlColor.ToMonoColor());
+ int _itemx = _initialmargin;
+ int _itemy = _initialmargin - scroll;
+ int yhelper = 0;
+ foreach (var item in _items)
+ {
+ Texture2D image = null;
+ int texwidth = defaulttexturesize;
+ int texheight = defaulttexturesize;
+ if (_images.ContainsKey(item.ImageKey))
+ {
+ texwidth = _images[item.ImageKey].Width;
+ texheight = _images[item.ImageKey].Height;
+ image = _images[item.ImageKey];
+ }
+ int textwidth = texwidth + (_itemimagemargin * 2);
+ var textmeasure = gfx.MeasureString(item.Text, LoadedSkin.MainFont, textwidth);
+ yhelper = Math.Max(yhelper, _itemy + texheight + (int)textmeasure.Y);
+
+ if(image != null)
+ {
+ int imageDrawX = _itemx + ((textwidth - texwidth) / 2);
+ gfx.DrawRectangle(imageDrawX, _itemy, texwidth, texheight, image);
+ }
+
+ int texty = _itemy + texheight;
+ int textx = _itemx + ((textwidth - (int)textmeasure.X) / 2);
+ if(_items.IndexOf(item) == _selected)
+ {
+ gfx.DrawRectangle(textx, texty, (int)textmeasure.X, (int)textmeasure.Y, LoadedSkin.ButtonPressedColor.ToMonoColor());
+ }
+ gfx.DrawString(item.Text, textx, texty, LoadedSkin.ControlTextColor.ToMonoColor(), LoadedSkin.MainFont, textwidth);
+ _itemx += textwidth + _itemgap;
+ if(_itemx >= (Width - (_initialmargin * 2)))
+ {
+ _itemx = _initialmargin;
+ _itemy += yhelper;
+ }
+ }
+ }
+ }
+
+ public class ListViewItem
+ {
+ public string Text { get; set; }
+ public string Tag { get; set; }
+ public string ImageKey { get; set; }
+
+ }
+}