Skip to content

Commit df11d85

Browse files
committed
search functional
1 parent a8cb09f commit df11d85

3 files changed

Lines changed: 127 additions & 52 deletions

File tree

Binmap/Controls/BinList.cs

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,8 @@ public Point ItemSpace
4747
public int MaxScrollValue { get { return bins.Count - 1; } }
4848
public int NumVisible { get { return items.Count; } }
4949

50-
public bool Focused
51-
{
52-
set
53-
{
54-
55-
}
56-
}
57-
58-
public Action<IInput> OnChangeCallback
59-
{
60-
set
61-
{
62-
63-
}
64-
}
50+
public bool Focused { set { } }
51+
public Action<IInput> OnChangeCallback { set { } }
6552
#endregion
6653

6754
private bool dirty = false;
@@ -95,17 +82,6 @@ protected override void OnMouseDown()
9582
Main.SetFocus(this);
9683
}
9784

98-
public void SetBinFormat(Bin.Formats format)
99-
{
100-
foreach (Bin bin in Selection.Values)
101-
{
102-
bin.Format = format;
103-
if (bin.LineBreak) scrollbar.SetMark(bin.Offset, bin.Color);
104-
}
105-
106-
if (Selection.Count > 0) Layout();
107-
}
108-
10985
public void Layout()
11086
{
11187
scrollbar.Visible = bins.Count > 0;
@@ -365,6 +341,17 @@ public override void Resize(int w, int h)
365341
if (!layoutLocked) Layout();
366342
}
367343

344+
public void SetBinFormat(Bin.Formats format)
345+
{
346+
foreach (Bin bin in Selection.Values)
347+
{
348+
bin.Format = format;
349+
if (bin.LineBreak) scrollbar.SetMark(bin.Offset, bin.Color);
350+
}
351+
352+
if (Selection.Count > 0) Layout();
353+
}
354+
368355
public void ScrollTo(int targetPosition)
369356
{
370357
scrollbar.ScrollTo(targetPosition);
@@ -381,6 +368,24 @@ public void AddScrollbarMark(int pos, Color color)
381368
scrollbar.SetMark(pos, color);
382369
}
383370

371+
public int Search(byte[] query, int offset = 0)
372+
{
373+
int end = bins.Count - query.Length;
374+
int num = 0;
375+
376+
for (int i = offset; i < end; i++)
377+
{
378+
if (bins[i].Value == query[num])
379+
{
380+
num++;
381+
if (num == query.Length) return i - query.Length + 1;
382+
}
383+
else num = 0;
384+
}
385+
386+
return -1;
387+
}
388+
384389
#region item management
385390
public void Lock()
386391
{

Binmap/Screens/Layouter.cs

Lines changed: 93 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,20 @@ public Layouter(int x, int y, int w, int h) : base(x, y, w, h, Main.BackgroundCo
8282
valueInput.Visible = false;
8383
AddChild(valueInput);
8484

85-
valueInputTypeButton = new Button(15, 14, "D", Main.DecColor, typeButtonClicked, valueInput);
85+
valueInputTypeButton = new Button(15, 14, "D", Main.DecColor, inputTypeSwitchClicked, valueInput);
8686
valueInputTypeButton.Font = Main.FontS;
8787
valueInputTypeButton.Transform.X = 57;
8888
valueInput.AddChild(valueInputTypeButton);
8989

9090
// search inputs
9191
searchInput = new TextInput(10, 10, 56, 14);
9292
searchInput.TextColor = Main.HexColor;
93+
searchInput.OnChangeCallback = searchChanged;
9394
searchInput.OnSubmitCallback = searchCommitted;
9495
searchInput.Visible = false;
9596
AddChild(searchInput);
9697

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

110-
gotoInputTypeButton = new Button(15, 14, "D", Main.DecColor, typeButtonClicked, gotoInput);
111+
gotoInputTypeButton = new Button(15, 14, "D", Main.DecColor, inputTypeSwitchClicked, gotoInput);
111112
gotoInputTypeButton.Font = Main.FontS;
112113
gotoInputTypeButton.Transform.X = 57;
113114
gotoInput.AddChild(gotoInputTypeButton);
@@ -486,25 +487,6 @@ public override void Resize(int w, int h)
486487
}
487488

488489
#region button handlers
489-
private void typeButtonClicked(Button btn)
490-
{
491-
TextInput input = btn.Tag as TextInput;
492-
493-
if (btn.Text == "H")
494-
{
495-
btn.Text = "D";
496-
btn.TextColor = Main.DecColor;
497-
if (input.Text != "") input.Text = int.Parse(input.Text, NumberStyles.HexNumber, null).ToString();
498-
}
499-
else
500-
{
501-
btn.Text = "H";
502-
btn.TextColor = Main.HexColor;
503-
if (input.Text != "") input.Text = int.Parse(input.Text).ToString("X2");
504-
}
505-
506-
input.TextColor = btn.TextColor;
507-
}
508490

509491
private int getIntValue(Button btn)
510492
{
@@ -526,7 +508,7 @@ private int getIntValue(Button btn)
526508
private void gotoInputChanged(IInput obj)
527509
{
528510
int i = getIntValue(gotoInputTypeButton);
529-
gotoInput.FocusFrameColor = (i >= 0 && i < list.Bins.Count) ? Main.DecColor : Color.Red;
511+
gotoInput.FocusFrameColor = (i >= 0 && i < list.Bins.Count) ? Main.TrackColor : Color.Red;
530512
}
531513

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

538-
private void searchCommitted(IInput input)
520+
private byte[] getSearchQuery()
539521
{
522+
string text = searchInput.Text.Trim();
540523

524+
if (text == string.Empty) return null;
525+
526+
bool hex = searchInputTypeButton.Text == "H";
527+
List<byte> bytes = new List<byte>();
528+
529+
string[] parts = text.Split(' ');
530+
foreach (string part in parts)
531+
{
532+
text = part.Trim();
533+
if (text.Length == 0) return null;
534+
535+
int value = -1;
536+
if (hex)
537+
{
538+
if (!int.TryParse(text, NumberStyles.HexNumber, null, out value) || value > 255) return null;
539+
else bytes.Add((byte)value);
540+
}
541+
else
542+
{
543+
if (!int.TryParse(text, out value) || value > 255) return null;
544+
else bytes.Add((byte)value);
545+
}
546+
}
547+
548+
return bytes.ToArray();
541549
}
542550

551+
private void searchChanged(IInput input)
552+
{
553+
byte[] query = getSearchQuery();
554+
if (query == null) searchInput.FocusFrameColor = Color.Red;
555+
else searchInput.FocusFrameColor = Main.TrackColor;
556+
}
557+
558+
private void searchCommitted(IInput input)
559+
{
560+
byte[] query = getSearchQuery();
561+
if (query != null)
562+
{
563+
int i = list.Search(query);
564+
if (i >= 0) list.ScrollTo(i);
565+
else showStatus("No match found for query '" + searchInput.Text + "'.", 2);
566+
}
567+
}
568+
543569
private void valueChanged(IInput input)
544570
{
545571
TextInput textInput = input as TextInput;
@@ -554,6 +580,49 @@ private void valueChanged(IInput input)
554580
else valueInput.FocusFrameColor = Color.Red;
555581
}
556582

583+
private void inputTypeSwitchClicked(Button btn)
584+
{
585+
TextInput input = btn.Tag as TextInput;
586+
587+
NumberStyles oldStyle;
588+
string format = "";
589+
590+
if (btn.Text == "H")
591+
{
592+
btn.TextColor = Main.DecColor;
593+
oldStyle = NumberStyles.HexNumber;
594+
}
595+
else
596+
{
597+
btn.TextColor = Main.HexColor;
598+
oldStyle = NumberStyles.Integer;
599+
format = "X2";
600+
}
601+
602+
if (input.Text != "")
603+
{
604+
if (input == searchInput) // special treatment for search input because it can have several values (space-separated)
605+
{
606+
byte[] query = getSearchQuery();
607+
if (query != null)
608+
{
609+
List<string> bytes = new List<string>();
610+
foreach(byte b in query) bytes.Add(b.ToString(format));
611+
input.Text = string.Join(" ", bytes);
612+
}
613+
}
614+
else
615+
{
616+
int i = -1;
617+
if (int.TryParse(input.Text, oldStyle, null, out i))
618+
input.Text = i.ToString(format);
619+
}
620+
}
621+
622+
input.TextColor = btn.TextColor;
623+
btn.Text = btn.Text == "H" ? "D" : "H"; // do this here and not above because getSearchQuery relies on the text
624+
}
625+
557626
private void writeButtonClicked(Button obj)
558627
{
559628
writeDataFileChanges();

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ data loaded, unformatted:
1414

1515
data formatted and commented:
1616

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

1919

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

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

0 commit comments

Comments
 (0)