-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
413 lines (356 loc) · 14.5 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Automation.Peers;
using System.Windows.Automation.Provider;
using System.Windows.Controls;
using System.Windows.Controls.Ribbon;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Linq;
using WeenieViewer.Controls;
using WeenieViewer.Db;
using WeenieViewer.Db.weenie;
using WeenieViewer.Properties;
namespace WeenieViewer
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public DbManager db;
public MainWindow()
{
InitializeComponent();
lblResultsCount.Text = "";
lblVersion.Text = "";
btnCloseTab.Visibility = Visibility.Hidden;
btnWiki.Visibility = Visibility.Hidden;
btnAcpedia.Visibility = Visibility.Hidden;
// Add event handler when typing in weenie search box
txtSearch.TextChanged += new TextChangedEventHandler(txtSearch_TextChanged);
SetupHotKeys();
// Init our SQLite Database
db = new DbManager();
var connected = db.Connect();
if (connected)
{
OnDbConnection();
#if DEBUG
MenuItem newMenuItem = new MenuItem();
newMenuItem.Header = "Debug";
MainMenu.Items.Add(newMenuItem);
//Add to a sub item
MenuItem subMenuItem = new MenuItem();
subMenuItem.Header = "Open All Weenies";
subMenuItem.Click += new RoutedEventHandler(DEBUG_OpenAllTheWeenies);
newMenuItem.Items.Add(subMenuItem);
#endif
}
else
{
PromptForDBConnection();
}
}
private void OnDbConnection()
{
// Clear search
lstSearchResults.Items.Clear();
txtSearch.IsEnabled = true;
txtSearch.Text = string.Empty; // Clear any previous input
txtSearch.Focus();
// Set StatusBar info
lblVersion.Text = db.Version;
miSearchSpells.IsEnabled = true;
}
private void OnDbNoConnection()
{
// Clear search
lstSearchResults.Items.Clear();
// Disable and clear search box
txtSearch.Text = string.Empty;
txtSearch.IsEnabled = false;
// Set StatusBar info
lblVersion.Text = "Database Not Connected";
miSearchSpells.IsEnabled = false;
}
private void PromptForDBConnection()
{
OnDbNoConnection();
// Click the "Options" menu... WPF is different
MenuItemAutomationPeer peer = new MenuItemAutomationPeer(miOptions);
IInvokeProvider invokeProv = peer.GetPattern(PatternInterface.Invoke) as IInvokeProvider;
invokeProv.Invoke();
}
private void View_OnClick(object sender, RoutedEventArgs e)
{
if (lstSearchResults.HasItems && lstSearchResults.SelectedItem != null)
{
string selectedItem = lstSearchResults.SelectedValue.ToString();
int wcidLength = selectedItem.LastIndexOf(')') - selectedItem.LastIndexOf('(') - 1;
var selectedWCID = selectedItem.Substring(selectedItem.LastIndexOf('(') + 1, wcidLength);
ViewWeenie(Int32.Parse(selectedWCID));
}
}
private void miExit_Click(object sender, RoutedEventArgs e)
{
System.Windows.Application.Current.Shutdown();
}
public void DoDBSearch(string criteria)
{
if (criteria != "")
{
tabGroup.SelectedItem = tabSearch;
lstSearchResults.Items.Clear();
Dictionary<int, string> searchResults;
var isNumeric = int.TryParse(criteria, out _);
if (isNumeric)
{
searchResults = db.SearchForWCID(criteria);
}
else
{
searchResults = db.SearchForWeenie(criteria.Trim());
}
foreach(var w in searchResults)
{
lstSearchResults.Items.Add(w.Value + " (" + w.Key +")");
}
lblResultsCount.Text = searchResults.Count.ToString() + " Results";
//MessageBox.Show(searchResults.Count + " results found!");
}
}
private void txtSearch_KeyDown(object sender, KeyEventArgs e)
{
DoDBSearch(txtSearch.Text.Trim());
}
private void lstSearchResults_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if(lstSearchResults.HasItems && lstSearchResults.SelectedItem != null)
{
string selectedItem = lstSearchResults.SelectedValue.ToString();
int wcidLength = selectedItem.LastIndexOf(')') - selectedItem.LastIndexOf('(') - 1;
var selectedWCID = selectedItem.Substring(selectedItem.LastIndexOf('(')+1, wcidLength);
ViewWeenie(Int32.Parse(selectedWCID));
}
}
public void ViewWeenie(int wcid)
{
// Does this tab already exist?
var tabExists = tabGroup.Items.OfType<TabItem>().SingleOrDefault(n => n.Name.ToString() == "tab_" + wcid.ToString());
if (tabExists != null)
{
// Bring the tab forward
tabGroup.SelectedItem = tabExists;
}
else
{
Cursor = Cursors.Wait;
try
{
// Create our new tab.
//var newTab = new TabWeenie() {};
TabItem newWeenieTabItem = new TabItem
{
//Header = itemName,
Name = "tab_" + wcid.ToString()
};
TabWeenie weenieTabContent = new TabWeenie();
weenieTabContent.wcid = wcid;
var weenie = db.GetWeenie(wcid);
weenieTabContent.DisplayWeenie(weenie);
// You can get a "missing item" via a CreateList, possibly other ways
if (!weenie.Strings.ContainsKey(Enums.PropertyString.NAME_STRING))
{
newWeenieTabItem.Header = $"Missing Item ({wcid})";
}
else
{
newWeenieTabItem.Header = weenie.Strings[Enums.PropertyString.NAME_STRING] + $" ({wcid})";
weenieTabContent.name = weenie.Strings[Enums.PropertyString.NAME_STRING];
}
newWeenieTabItem.Content = weenieTabContent;
tabGroup.Items.Add(newWeenieTabItem);
tabGroup.SelectedItem = newWeenieTabItem;
}
finally
{
Cursor = Cursors.Arrow;
}
}
}
private void txtSearch_TextChanged(object sender, TextChangedEventArgs e)
{
DoDBSearch(txtSearch.Text.Trim());
}
private void tabGroup_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Check if the current selected tab is the "Search Results" tab -- if so, hide the Close button
TabItem ti = tabGroup.SelectedItem as TabItem;
if(ti == null || ti.Name == "tabSearch")
{
btnCloseTab.Visibility = Visibility.Collapsed;
btnWiki.Visibility = Visibility.Collapsed;
btnAcpedia.Visibility = Visibility.Collapsed;
lblResultsCount.Visibility = Visibility.Visible;
}
else
{
btnCloseTab.Visibility = Visibility.Visible;
if (WeenieViewerSettings.Default.useWikia)
{
btnWiki.Visibility = Visibility.Visible;
btnAcpedia.Visibility = Visibility.Collapsed;
}
else
{
btnWiki.Visibility = Visibility.Collapsed;
btnAcpedia.Visibility = Visibility.Visible;
}
lblResultsCount.Visibility = Visibility.Collapsed;
}
}
private void btnCloseTab_Click(object sender, RoutedEventArgs e)
{
TabItem ti = tabGroup.SelectedItem as TabItem;
tabGroup.Items.Remove(ti);
e.Handled = true;
}
private void miSearchWeenies_Click(object sender, RoutedEventArgs e)
{
tabGroup.SelectedItem = tabSearch;
}
private void SetupHotKeys()
{
try
{
// Ctrl + W closes current tab
RoutedCommand firstSettings = new RoutedCommand();
firstSettings.InputGestures.Add(new KeyGesture(Key.W, ModifierKeys.Control));
CommandBindings.Add(new CommandBinding(firstSettings, CloseTab));
// Ctrl + S brings up the Spell Search dialog
var spellSearch = new RoutedCommand();
spellSearch.InputGestures.Add(new KeyGesture(Key.S, ModifierKeys.Control));
CommandBindings.Add(new CommandBinding(spellSearch, miSearchSpells_Click));
}
catch { }
}
private void CloseTab(object sender, ExecutedRoutedEventArgs e)
{
//handler code goes here.
TabItem ti = tabGroup.SelectedItem as TabItem;
if (ti != null && ti.Name != "tabSearch")
{
tabGroup.Items.Remove(ti);
}
}
private void miSearchSpells_Click(object sender, RoutedEventArgs e)
{
var spellSearch = new DialogSpells();
spellSearch.Owner = this;
spellSearch.ShowDialog();
}
private void btnWiki_Click(object sender, RoutedEventArgs e)
{
TabItem ti = tabGroup.SelectedItem as TabItem;
TabWeenie content = ti.Content as TabWeenie;
string name = content.name;
string url = "https://asheron.fandom.com/wiki/Special:Search?query=" + Uri.EscapeDataString(name);
//string url = "http://www.acpedia.org/?search=" + Uri.EscapeDataString(name);
Process.Start(new ProcessStartInfo("cmd", $"/c start {url}") { CreateNoWindow = true });
}
private void btnAcpedia_Click(object sender, RoutedEventArgs e)
{
TabItem ti = tabGroup.SelectedItem as TabItem;
TabWeenie content = ti.Content as TabWeenie;
string name = content.name;
string url = "http://www.acpedia.org/?search=" + Uri.EscapeDataString(name);
Process.Start(new ProcessStartInfo("cmd", $"/c start {url}") { CreateNoWindow = true });
}
private void miAbout_Click(object sender, RoutedEventArgs e)
{
var about = new DialogAbout();
about.Owner = this;
about.ShowDialog();
}
private void miOptions_Click(object sender, RoutedEventArgs e)
{
var options = new DialogOptions();
options.Owner = this;
var optionsResult = options.ShowDialog();
if (optionsResult != null && (bool)optionsResult)
{
// Trigger the tab change to switch the Wiki button, if neccessary
tabGroup_SelectionChanged(null, null);
// Check if we need to swap database or connect in the first place
bool isSQLite = WeenieViewerSettings.Default.DBType == "SQLite";
if (!db.db.Connected || isSQLite != db.db.usingSQLite)
{
db = new DbManager();
if (db.Connect())
{
OnDbConnection();
}
else
{
PromptForDBConnection();
}
}
}
}
/// <summary>
/// Rebuilds the EmoteScript text files, used to label items e.g. Refuse: ITEM NAME (WCID)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void miRegenerateTxtFiles_Click(object sender, RoutedEventArgs e)
{
var result = MessageBox.Show("Do you want to regenerate your EmoteScript text files?", "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (result == MessageBoxResult.Yes)
{
if (db.RegnerateEmoteScriptTxtFiles())
{
MessageBox.Show("Files successfully regenerated.", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
}
else
{
string message = "There was an error regenerating the EmoteScript text files.";
MessageBox.Show(message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
private void DEBUG_OpenAllTheWeenies(object sender, RoutedEventArgs e)
{
MessageBoxResult messageBoxResult = MessageBox.Show("Are you sure?\n\nThis is a time consuming process you cannot quit.", "Open All Weenies", MessageBoxButton.YesNo);
if (messageBoxResult == MessageBoxResult.Yes)
{
MessageBoxResult messageBoxResult2 = MessageBox.Show("Are you REALLY sure?\n\nThis takes forever...", "Open All Weenies", MessageBoxButton.YesNo);
if (messageBoxResult2 == MessageBoxResult.Yes)
{
foreach (var w in db.WeenieNames)
{
var weenie = db.GetWeenie(w.Key);
weenie = null;
/*
ViewWeenie(w.Key);
TabItem ti = tabGroup.SelectedItem as TabItem;
tabGroup.Items.Remove(ti);
*/
}
}
}
}
}
}