Skip to content

Commit

Permalink
more ctrl p
Browse files Browse the repository at this point in the history
  • Loading branch information
factubsio committed Mar 26, 2022
1 parent 907bc86 commit a3c72c7
Show file tree
Hide file tree
Showing 7 changed files with 384 additions and 38 deletions.
237 changes: 237 additions & 0 deletions BlueprintExplorer/BubbleLabel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
using BlueprintExplorer.Properties;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BlueprintExplorer
{
internal class BubbleLabel : Control
{

private SolidBrush _ForeBrush;

private bool _Marquee;

private float _MarqueePos;

private static Timer _MarqueeTimer;

public string OverrideText
{
get => _OverrideText;
set
{
_OverrideText = value;
Invalidate();
}

}
public string Text2
{
get => _Text2;
set
{
_Text2 = value;
InvalidateFragments(value, secondaryFragments);
Invalidate();
}

}

public BubbleLabel()
{
if (_MarqueeTimer == null)
{
_MarqueeTimer = new();
_MarqueeTimer.Interval = 16;
_MarqueeTimer.Start();
}

_MarqueeTimer.Tick += OnMarqueeTick;

DoubleBuffered = true;
}

private void OnMarqueeTick(object sender, EventArgs e)
{
if (!_Marquee) return;

_MarqueePos += 4;

Invalidate();
}

public bool Marquee
{
get { return _Marquee; }
set
{
_Marquee = value;
_MarqueePos = 18;
Invalidate();
}
}

interface IFragment
{
SizeF Render(Graphics g, BubbleLabel label);
}

class TextFragment : IFragment
{
public string Text = "";

public SizeF Render(Graphics g, BubbleLabel label)
{
if (Text?.Length == 0) return SizeF.Empty;

TextRenderer.DrawText(g, Text, label.Font, new Point((int)g.Transform.OffsetX, 0), label.ForeColor);
return g.MeasureString(Text, label.Font);
}
}

class KeyFragment : IFragment
{
public string Key = "";

private bool Meta => Key is "ctrl" or "shift" or "return" or "alt" or "enter";

public SizeF Render(Graphics g, BubbleLabel label)
{
int ratio = Meta ? 2 : 1;
int height = label.Height - 2;
int width = (int)(ratio * height);

var pen = new Pen(label._ForeBrush, 3);

float stringWidth = g.MeasureString(Key, label.Font).Width;

float c = width / 2 - stringWidth / 2;

g.DrawRectangle(pen, 0, 1, width, height);
g.DrawString(Key, label.Font, label._ForeBrush, new PointF(c, 1));

return new SizeF(width, height);

}
}

class ImageFragment : IFragment
{
public Image img;

public SizeF Render(Graphics g, BubbleLabel label)
{
if (img == null) return SizeF.Empty;

float ratio = img.Width / (float)img.Height;
int height = label.Height - 2;
int width = (int)(ratio * height);


g.DrawImage(img, new Rectangle(0, 0, width, height));
return new SizeF(width, height);
}
}

protected override void OnForeColorChanged(EventArgs e)
{
base.OnForeColorChanged(e);
_ForeBrush = new(ForeColor);
}

private List<IFragment> primaryFragments = new();
private List<IFragment> secondaryFragments = new();
private string _OverrideText;
private string _Text2;

private void InvalidateFragments(string template, List<IFragment> fragments)
{
var rawFragments = TemplateRunner.Iterate(template);

fragments.Clear();
foreach (var raw in rawFragments)
{
if (raw.IsError)
{
fragments.Add(new TextFragment() { Text = "ERROR:<" + raw.Raw + ">" });
continue;
}

if (!raw.IsVariable)
{
fragments.Add(new TextFragment() { Text = raw.Raw });
}
else
{
if (raw.Object == "key")
{
fragments.Add(new KeyFragment() { Key = raw.Property });
continue;
}

//object obj = null;
//if (obj is Bitmap img)
//{
// fragments.Add(new ImageFragment() { img = img });

//}
}
}

Invalidate();

}

protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged(e);
InvalidateFragments(Text, primaryFragments);
}

private StringFormat _StringFormat;

protected override void OnPaintBackground(PaintEventArgs pevent)
{
pevent.Graphics.Clear(BackColor);
}

protected override void OnPaint(PaintEventArgs e)
{
_ForeBrush ??= new(ForeColor);
e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;

//_StringFormat = new StringFormat
//{
// FormatFlags = StringFormatFlags.NoWrap
//};

if (_Marquee)
{
e.Graphics.TranslateTransform(_MarqueePos, 0);
}

if (_OverrideText != null)
{
e.Graphics.DrawString(_OverrideText, Font, _ForeBrush, PointF.Empty);
return;
}


foreach (var frag in primaryFragments)
{
var advance = frag.Render(e.Graphics, this);
e.Graphics.TranslateTransform(advance.Width, 0);
}
foreach (var frag in secondaryFragments)
{
var advance = frag.Render(e.Graphics, this);
e.Graphics.TranslateTransform(advance.Width, 0);
}
}
}
}
50 changes: 36 additions & 14 deletions BlueprintExplorer/CtrlP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ public CtrlP()

root.MouseClick += Root_MouseClick;

DoubleBuffered = true;
root.GetType().GetProperty("DoubleBuffered", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).SetValue(root, true);
root.ShowCellToolTips = false;

DoubleBuffered = true;
}

private void Root_MouseClick(object sender, MouseEventArgs e)
Expand All @@ -67,7 +69,6 @@ private void Root_MouseClick(object sender, MouseEventArgs e)
Close();
}
}
Console.WriteLine(e.Button);
}

public Form1 Daddy;
Expand Down Expand Up @@ -99,6 +100,20 @@ public void SetResults(List<BlueprintHandle> results)
UpdateSize();
}

private void TryScroll(int delta)
{
int current = root.SelectedRow();
int wanted = current + delta;
if (wanted < 0) wanted = 0;
if (wanted >= root.Rows.Count) wanted = root.Rows.Count - 1;

if (current != wanted)
{
root.Rows[wanted].Selected = true;
root.CurrentCell = root.Rows[wanted].Cells[0];
}
}

private void Input_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
Expand All @@ -110,24 +125,30 @@ private void Input_KeyDown(object sender, KeyEventArgs e)
e.Handled = true;
e.SuppressKeyPress = true;

int current = root.SelectedRow();
if (current > 0)
{
root.Rows[current - 1].Selected = true;
root.CurrentCell = root.Rows[current - 1].Cells[0];
}
TryScroll(-1);

}
if (e.KeyCode == Keys.Down || (e.KeyCode == Keys.N && ModifierKeys.HasFlag(Keys.Control)))
{
e.Handled = true;
e.SuppressKeyPress = true;

int current = root.SelectedRow();
if (current < (root.Rows.Count - 1))
{
root.Rows[current + 1].Selected = true;
root.CurrentCell = root.Rows[current + 1].Cells[0];
}
TryScroll(1);
}

if (e.KeyCode == Keys.PageUp)
{
e.Handled = true;
e.SuppressKeyPress = true;

TryScroll(-16);
}
if (e.KeyCode == Keys.PageDown)
{
e.Handled = true;
e.SuppressKeyPress = true;

TryScroll(16);
}

if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
Expand All @@ -153,6 +174,7 @@ protected override void OnClosed(EventArgs e)
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
Capture = true;
UpdateSize();
if (savedVerticalScroll != -1)
{
Expand Down
Loading

0 comments on commit a3c72c7

Please sign in to comment.