Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add icons to sections #6

Merged
merged 2 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/SerialLoops/EditorTabsPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,17 @@ void InitializeComponent()
};
}

public static Icon GetItemIcon(ItemDescription.ItemType type)
public static Icon GetItemIcon(ItemDescription.ItemType type, ILogger log)
{
return Icon.FromResource($"SerialLoops.Icons.{type}.png").WithSize(16, 16);
try
{
return Icon.FromResource($"SerialLoops.Icons.{type}.png").WithSize(16, 16);
}
catch (Exception exc)
{
log.LogWarning($"Failed to load icon.\n{exc.Message}\n\n{exc.StackTrace}");
return null;
}
}

internal void OpenTab(ItemDescription item, ILogger log)
Expand Down
5 changes: 4 additions & 1 deletion src/SerialLoops/Editors/BackgroundEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@ namespace SerialLoops.Editors
{
public class BackgroundEditor : Editor
{
private BackgroundItem _bg;

public BackgroundEditor(BackgroundItem item, ILogger log) : base(item, log)
{
}

public override Panel GetEditorPanel()
{
_bg = (BackgroundItem)Description;
return new StackLayout
{
Orientation = Orientation.Vertical,
Items =
{
new ImageView() { Image = new SKGuiImage(((BackgroundItem)Description).GetBackground()) },
new ImageView() { Image = new SKGuiImage(_bg.GetBackground()) },
}
};
}
Expand Down
9 changes: 1 addition & 8 deletions src/SerialLoops/Editors/Editor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,7 @@ void InitializeComponent()
{
Text = Description.Name;
MinimumSize = EditorTabsPanel.EDITOR_BASE_SIZE;
try
{
Image = EditorTabsPanel.GetItemIcon(Description.Type);
}
catch (Exception exc)
{
_log.LogWarning($"Failed to load icon.\n{exc.Message}\n\n{exc.StackTrace}");
}
Image = EditorTabsPanel.GetItemIcon(Description.Type, _log);
Padding = 10;

Panel panel = GetEditorPanel();
Expand Down
3 changes: 3 additions & 0 deletions src/SerialLoops/Editors/MapEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ namespace SerialLoops.Editors
{
public class MapEditor : Editor
{
private MapItem _map;

public MapEditor(MapItem map, ILogger log) : base(map, log)
{
}

public override Panel GetEditorPanel()
{
_map = (MapItem)Description;
return new StackLayout
{
Orientation = Orientation.Vertical,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ namespace SerialLoops.Editors
{
public class ScriptEditor : Editor
{
private ScriptItem _script;

public ScriptEditor(ScriptItem item, ILogger log) : base(item, log)
{
}

public override Panel GetEditorPanel()
{
_script = (ScriptItem)Description;
return new StackLayout
{
Orientation = Orientation.Vertical,
Expand Down
28 changes: 5 additions & 23 deletions src/SerialLoops/ItemExplorerPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using SerialLoops.Lib.Items;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;

namespace SerialLoops
{
Expand All @@ -30,29 +32,10 @@ void InitializeComponent()
MinimumSize = ITEM_EXPLORER_BASE_SIZE;
Padding = 0;

// Test items
Dictionary<ItemDescription.ItemType, Section> itemTypes = new();
foreach (ItemDescription item in _project.Items)
{
if (!itemTypes.ContainsKey(item.Type))
{
itemTypes[item.Type] = new Section
{
Text = item.Type.ToString()
};
IEnumerable<Section> sections = _project.Items.GroupBy(i => i.Type).OrderBy(g => g.Key)
.Select(g => new Section($"{g.Key}s", g.Select(i => new Section() { Text = i.Name }), EditorTabsPanel.GetItemIcon(g.Key, _log)));

}
itemTypes[item.Type].Add(new Section
{
Text = item.Name
});
}
foreach (var category in itemTypes.Values)
{
category.Sort((x, y) => string.Compare(x.Text, y.Text, StringComparison.CurrentCultureIgnoreCase));
}

_items = new SectionListTreeGridView(itemTypes.Values, ITEM_EXPLORER_BASE_SIZE);
_items = new SectionListTreeGridView(sections, ITEM_EXPLORER_BASE_SIZE);
_items.Activated += ItemList_ItemClicked;

Content = new StackLayout
Expand All @@ -63,7 +46,6 @@ void InitializeComponent()
_items.Control
}
};

}

private void ItemList_ItemClicked(object sender, EventArgs e)
Expand Down
86 changes: 64 additions & 22 deletions src/SerialLoops/SectionList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,37 @@
using Eto.Drawing;

/// Taken from https://github.com/picoe/Eto/blob/ac4610775b70538b96995bdb0c7f0fbcc5ae1b66/test/Eto.Test/SectionList.cs
/// Licensed under the BSD-3 License, reproduced below:
///
/*
* The BSD-3 License.
*
* AUTHORS
* Copyright © 2011-2022 Curtis Wensley. All Rights Reserved.
* Copyright © 2012-2013 Vivek Jhaveri. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted
* provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions
* and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
* and the following disclaimer in the documentation and/or other materials provided with the
* distribution.
*
* 3. The name of the author may not be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

namespace SerialLoops
{
public interface ISection
Expand All @@ -23,16 +54,18 @@ public interface ISection
/// </summary>
public class Section : List<Section>, ISection
{
public Icon SectionIcon { get; set; }
public string Text { get; set; }

public Section()
{
}

public Section(string text, IEnumerable<Section> sections)
public Section(string text, IEnumerable<Section> sections, Icon icon)
: base(sections.OrderBy(r => r.Text, StringComparer.CurrentCultureIgnoreCase).ToArray())
{
this.Text = text;
Text = text;
SectionIcon = icon;
}
}

Expand All @@ -42,20 +75,20 @@ public Section(string text, IEnumerable<Section> sections)
public class SectionTreeItem : List<SectionTreeItem>, ITreeGridItem<SectionTreeItem>
{
public Section Section { get; private set; }
public string Text { get { return Section.Text; } }
public Icon SectionIcon => Section.SectionIcon;
public string Text => Section.Text;
public bool Expanded { get; set; }
public bool Expandable { get { return Count > 0; } }
public bool Expandable => Count > 0;
public ITreeGridItem Parent { get; set; }

public SectionTreeItem(Section section)
{
this.Section = section;
this.Expanded = false;
Section = section;
Expanded = false;
foreach (var child in section)
{
SectionTreeItem temp = new(child);
temp.Parent = this;
this.Add(temp); // recursive
SectionTreeItem temp = new(child) { Parent = this };
Add(temp); // recursive
}
}
}
Expand Down Expand Up @@ -87,22 +120,22 @@ protected void OnActivated(EventArgs e)

public class SectionListTreeGridView : SectionList
{
TreeGridView treeView;
private TreeGridView _treeView;

public override Control Control { get { return this.treeView; } }
public override Control Control { get { return _treeView; } }

public override void Focus() { Control.Focus(); }

public override ISection SelectedItem
{
get
{
var sectionTreeItem = treeView.SelectedItem as SectionTreeItem;
var sectionTreeItem = _treeView.SelectedItem as SectionTreeItem;
return sectionTreeItem?.Section as ISection;
}
set
{
treeView.SelectedItem = FindItem(treeView.DataStore as SectionTreeItem, value);
_treeView.SelectedItem = FindItem(_treeView.DataStore as SectionTreeItem, value);
}
}

Expand All @@ -124,15 +157,24 @@ ITreeGridItem FindItem(SectionTreeItem node, ISection section)

public SectionListTreeGridView(IEnumerable<Section> topNodes, Size size)
{
this.treeView = new TreeGridView();
treeView.Style = "sectionList";
treeView.ShowHeader = false;
treeView.AllowEmptySelection = false;
treeView.Columns.Add(new GridColumn { DataCell = new TextBoxCell { Binding = new DelegateBinding<SectionTreeItem, string>(r => r.Text) } });
treeView.SelectedItemChanged += (sender, e) => OnSelectedItemChanged(e);
treeView.Activated += (sender, e) => OnActivated(e);
treeView.DataStore = new SectionTreeItem(new Section("Top", topNodes));
treeView.Size = size;
_treeView = new TreeGridView
{
Style = "sectionList",
ShowHeader = false,
AllowEmptySelection = false
};
_treeView.Columns.Add(new GridColumn
{
DataCell = new ImageTextCell
{
ImageBinding = new DelegateBinding<SectionTreeItem, Image>(r => r.SectionIcon),
TextBinding = new DelegateBinding<SectionTreeItem, string>(r => r.Text)
}
});
_treeView.SelectedItemChanged += (sender, e) => OnSelectedItemChanged(e);
_treeView.Activated += (sender, e) => OnActivated(e);
_treeView.DataStore = new SectionTreeItem(new Section("Top", topNodes, null));
_treeView.Size = size;
}
}

Expand Down