Skip to content

Commit

Permalink
search functional
Browse files Browse the repository at this point in the history
  • Loading branch information
movAX13h committed May 16, 2017
1 parent a8cb09f commit df11d85
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 52 deletions.
57 changes: 31 additions & 26 deletions Binmap/Controls/BinList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,8 @@ public Point ItemSpace
public int MaxScrollValue { get { return bins.Count - 1; } }
public int NumVisible { get { return items.Count; } }

public bool Focused
{
set
{

}
}

public Action<IInput> OnChangeCallback
{
set
{

}
}
public bool Focused { set { } }
public Action<IInput> OnChangeCallback { set { } }
#endregion

private bool dirty = false;
Expand Down Expand Up @@ -95,17 +82,6 @@ protected override void OnMouseDown()
Main.SetFocus(this);
}

public void SetBinFormat(Bin.Formats format)
{
foreach (Bin bin in Selection.Values)
{
bin.Format = format;
if (bin.LineBreak) scrollbar.SetMark(bin.Offset, bin.Color);
}

if (Selection.Count > 0) Layout();
}

public void Layout()
{
scrollbar.Visible = bins.Count > 0;
Expand Down Expand Up @@ -365,6 +341,17 @@ public override void Resize(int w, int h)
if (!layoutLocked) Layout();
}

public void SetBinFormat(Bin.Formats format)
{
foreach (Bin bin in Selection.Values)
{
bin.Format = format;
if (bin.LineBreak) scrollbar.SetMark(bin.Offset, bin.Color);
}

if (Selection.Count > 0) Layout();
}

public void ScrollTo(int targetPosition)
{
scrollbar.ScrollTo(targetPosition);
Expand All @@ -381,6 +368,24 @@ public void AddScrollbarMark(int pos, Color color)
scrollbar.SetMark(pos, color);
}

public int Search(byte[] query, int offset = 0)
{
int end = bins.Count - query.Length;
int num = 0;

for (int i = offset; i < end; i++)
{
if (bins[i].Value == query[num])
{
num++;
if (num == query.Length) return i - query.Length + 1;
}
else num = 0;
}

return -1;
}

#region item management
public void Lock()
{
Expand Down
117 changes: 93 additions & 24 deletions Binmap/Screens/Layouter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,20 @@ public Layouter(int x, int y, int w, int h) : base(x, y, w, h, Main.BackgroundCo
valueInput.Visible = false;
AddChild(valueInput);

valueInputTypeButton = new Button(15, 14, "D", Main.DecColor, typeButtonClicked, valueInput);
valueInputTypeButton = new Button(15, 14, "D", Main.DecColor, inputTypeSwitchClicked, valueInput);
valueInputTypeButton.Font = Main.FontS;
valueInputTypeButton.Transform.X = 57;
valueInput.AddChild(valueInputTypeButton);

// search inputs
searchInput = new TextInput(10, 10, 56, 14);
searchInput.TextColor = Main.HexColor;
searchInput.OnChangeCallback = searchChanged;
searchInput.OnSubmitCallback = searchCommitted;
searchInput.Visible = false;
AddChild(searchInput);

searchInputTypeButton = new Button(15, 14, "H", Main.HexColor, typeButtonClicked, searchInput);
searchInputTypeButton = new Button(15, 14, "H", Main.HexColor, inputTypeSwitchClicked, searchInput);
searchInputTypeButton.Font = Main.FontS;
searchInputTypeButton.Transform.X = 57;
searchInput.AddChild(searchInputTypeButton);
Expand All @@ -107,7 +108,7 @@ public Layouter(int x, int y, int w, int h) : base(x, y, w, h, Main.BackgroundCo
gotoInput.Visible = false;
AddChild(gotoInput);

gotoInputTypeButton = new Button(15, 14, "D", Main.DecColor, typeButtonClicked, gotoInput);
gotoInputTypeButton = new Button(15, 14, "D", Main.DecColor, inputTypeSwitchClicked, gotoInput);
gotoInputTypeButton.Font = Main.FontS;
gotoInputTypeButton.Transform.X = 57;
gotoInput.AddChild(gotoInputTypeButton);
Expand Down Expand Up @@ -486,25 +487,6 @@ public override void Resize(int w, int h)
}

#region button handlers
private void typeButtonClicked(Button btn)
{
TextInput input = btn.Tag as TextInput;

if (btn.Text == "H")
{
btn.Text = "D";
btn.TextColor = Main.DecColor;
if (input.Text != "") input.Text = int.Parse(input.Text, NumberStyles.HexNumber, null).ToString();
}
else
{
btn.Text = "H";
btn.TextColor = Main.HexColor;
if (input.Text != "") input.Text = int.Parse(input.Text).ToString("X2");
}

input.TextColor = btn.TextColor;
}

private int getIntValue(Button btn)
{
Expand All @@ -526,7 +508,7 @@ private int getIntValue(Button btn)
private void gotoInputChanged(IInput obj)
{
int i = getIntValue(gotoInputTypeButton);
gotoInput.FocusFrameColor = (i >= 0 && i < list.Bins.Count) ? Main.DecColor : Color.Red;
gotoInput.FocusFrameColor = (i >= 0 && i < list.Bins.Count) ? Main.TrackColor : Color.Red;
}

private void gotoCommitted(IInput input)
Expand All @@ -535,11 +517,55 @@ private void gotoCommitted(IInput input)
if (i >= 0 && i < list.Bins.Count) list.ScrollTo(i);
}

private void searchCommitted(IInput input)
private byte[] getSearchQuery()
{
string text = searchInput.Text.Trim();

if (text == string.Empty) return null;

bool hex = searchInputTypeButton.Text == "H";
List<byte> bytes = new List<byte>();

string[] parts = text.Split(' ');
foreach (string part in parts)
{
text = part.Trim();
if (text.Length == 0) return null;

int value = -1;
if (hex)
{
if (!int.TryParse(text, NumberStyles.HexNumber, null, out value) || value > 255) return null;
else bytes.Add((byte)value);
}
else
{
if (!int.TryParse(text, out value) || value > 255) return null;
else bytes.Add((byte)value);
}
}

return bytes.ToArray();
}

private void searchChanged(IInput input)
{
byte[] query = getSearchQuery();
if (query == null) searchInput.FocusFrameColor = Color.Red;
else searchInput.FocusFrameColor = Main.TrackColor;
}

private void searchCommitted(IInput input)
{
byte[] query = getSearchQuery();
if (query != null)
{
int i = list.Search(query);
if (i >= 0) list.ScrollTo(i);
else showStatus("No match found for query '" + searchInput.Text + "'.", 2);
}
}

private void valueChanged(IInput input)
{
TextInput textInput = input as TextInput;
Expand All @@ -554,6 +580,49 @@ private void valueChanged(IInput input)
else valueInput.FocusFrameColor = Color.Red;
}

private void inputTypeSwitchClicked(Button btn)
{
TextInput input = btn.Tag as TextInput;

NumberStyles oldStyle;
string format = "";

if (btn.Text == "H")
{
btn.TextColor = Main.DecColor;
oldStyle = NumberStyles.HexNumber;
}
else
{
btn.TextColor = Main.HexColor;
oldStyle = NumberStyles.Integer;
format = "X2";
}

if (input.Text != "")
{
if (input == searchInput) // special treatment for search input because it can have several values (space-separated)
{
byte[] query = getSearchQuery();
if (query != null)
{
List<string> bytes = new List<string>();
foreach(byte b in query) bytes.Add(b.ToString(format));
input.Text = string.Join(" ", bytes);
}
}
else
{
int i = -1;
if (int.TryParse(input.Text, oldStyle, null, out i))
input.Text = i.ToString(format);
}
}

input.TextColor = btn.TextColor;
btn.Text = btn.Text == "H" ? "D" : "H"; // do this here and not above because getSearchQuery relies on the text
}

private void writeButtonClicked(Button obj)
{
writeDataFileChanges();
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ data loaded, unformatted:

data formatted and commented:

![Binmap formatted and commented data](https://cloud.githubusercontent.com/assets/1974959/25872302/75fe61ac-350a-11e7-97af-12cbf29d1cf4.png)
![Binmap formatted and commented data](https://cloud.githubusercontent.com/assets/1974959/26132012/a6eb02fa-3a9d-11e7-8ca5-6d78efa377de.png)


## How it works and what it does
Expand All @@ -35,7 +35,8 @@ The font in use is called [Pixel Operator](http://www.dafont.com/de/pixel-operat
If you are interested in a release download, here is the latest version: [Binmap Release](https://github.com/movAX13h/Binmap/releases/latest)

### Todo
- TextInput text range selection (mouse and keyboard)
- continue search with F3
- TextInput text range selection (mouse and keyboard), handle longer texts
- Scrollbar track click
- view for multiple bytes combined as int, float, double, ...

Expand Down

0 comments on commit df11d85

Please sign in to comment.