-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.pas
375 lines (340 loc) · 10.4 KB
/
main.pas
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
unit Main;
{$mode ObjFPC}{$H+}{$J-}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls,
StdCtrls, ActnList, Grids, ImagingTypes, Imaging, ImagingComponents, FileUtil,
LazFileUtils, LCLIntf, LCLType, Menus, ComCtrls, ATImageBox, Clipbrd, ECSlider,
Utils;
type
{ TfrmMain }
TfrmMain = class(TForm)
btnUncheckAll: TButton;
btnCheckAll: TButton;
btnCopy: TButton;
btnInvert: TButton;
tbZoom: TECSlider;
imgMap: TATImageBox;
mnuMainOutputMode: TMenuItem;
mnuMainOutputModeMerc: TMenuItem;
mnuMainOutputModeWin: TMenuItem;
mnuMainGameMode: TMenuItem;
mnuMainGameModeRTW: TMenuItem;
mnuMainGameModeBI: TMenuItem;
mnuMainBrowse: TMenuItem;
mnuMainColumns: TMenuItem;
mnuMainColumnsRegion: TMenuItem;
mnuMainColumnsSettlement: TMenuItem;
mnuAbout: TMenuItem;
mnuMain: TMainMenu;
dlgCampaignDir: TSelectDirectoryDialog;
pnlGrid: TPanel;
sgRegions: TStringGrid;
splSplitMapGrid: TSplitter;
sbMainStatusBar: TStatusBar;
procedure btnCheckAllClick(Sender: TObject);
procedure btnCopyClick(Sender: TObject);
procedure btnInvertClick(Sender: TObject);
procedure btnUncheckAllClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure imgMapMouseUp(Sender: TObject);
procedure imgMapScroll(Sender: TObject);
procedure mnuMainOutputModeMercClick(Sender: TObject);
procedure mnuMainOutputModeWinClick(Sender: TObject);
procedure mnuAboutClick(Sender: TObject);
procedure mnuMainBrowseClick(Sender: TObject);
procedure mnuMainColumnsRegionClick(Sender: TObject);
procedure mnuMainColumnsSettlementClick(Sender: TObject);
procedure mnuMainGameModeBIClick(Sender: TObject);
procedure mnuMainGameModeRTWClick(Sender: TObject);
procedure sgRegionsCheckboxToggled(Sender: TObject);
procedure SavePickedRegionsToFile(const Filename: string);
procedure SortRegions;
function CountPickedRegions: integer;
function ScreenColor(x, y: integer): TColor;
procedure tbZoomChange(Sender: TObject);
private
GameMode: TGameMode;
OutputMode: TOutputMode;
RegionDict: TRegionDictionary;
public
end;
const
TITLE: string = 'RTW Region Picker';
VERSION: string = 'v1.1.0';
AUTHOR: string = 'Vartan Haghverdi';
COPYRIGHT: string = 'Copyright 2023';
NOTE: string = 'Brought to you by the EB Online Team';
var
frmMain: TfrmMain;
implementation
{$R *.lfm}
procedure TfrmMain.btnUncheckAllClick(Sender: TObject);
var
i: integer;
begin
if sgRegions.RowCount < 2 then
Exit;
for i := 1 to sgRegions.RowCount - 1 do
sgRegions.Rows[i][0] := '0';
SortRegions;
SavePickedRegionsToFile('regions.txt');
sbMainStatusBar.Panels[1].Text :=
Format('Picked: %d of %d', [CountPickedRegions, sgRegions.RowCount - 1]);
end;
procedure TfrmMain.btnCopyClick(Sender: TObject);
begin
Clipboard.AsText := PickedRegionsToStr(sgRegions, OutputMode);
end;
procedure TfrmMain.btnInvertClick(Sender: TObject);
var
i: integer;
begin
if sgRegions.RowCount < 2 then
Exit;
for i := 1 to sgRegions.RowCount - 1 do
if sgRegions.Rows[i][0] = '0' then
sgRegions.Rows[i][0] := '1'
else
sgRegions.Rows[i][0] := '0';
SortRegions;
SavePickedRegionsToFile('regions.txt');
sbMainStatusBar.Panels[1].Text :=
Format('Picked: %d of %d', [CountPickedRegions, sgRegions.RowCount - 1]);
end;
procedure TfrmMain.btnCheckAllClick(Sender: TObject);
var
i: integer;
begin
if sgRegions.RowCount < 2 then
Exit;
for i := 1 to sgRegions.RowCount - 1 do
sgRegions.Rows[i][0] := '1';
SortRegions;
SavePickedRegionsToFile('regions.txt');
sbMainStatusBar.Panels[1].Text :=
Format('Picked: %d of %d', [CountPickedRegions, sgRegions.RowCount - 1]);
end;
procedure TfrmMain.FormCreate(Sender: TObject);
begin
frmMain.Caption := TITLE + ' ' + VERSION;
end;
procedure TfrmMain.imgMapMouseUp(Sender: TObject);
var
ShiftState: TShiftState;
PickedColor: TColor;
Region: TRegion;
PickedStatus: string;
RowIndex: integer = -1;
begin
ShiftState := GetKeyShiftState;
if not (ssCtrl in ShiftState) then
Exit;
PickedColor := ScreenColor(Mouse.CursorPos.X, Mouse.CursorPos.Y);
if Assigned(RegionDict) then
if RegionDict.TryGetValue(PickedColor, Region) then
RowIndex := sgRegions.Cols[1].IndexOf(Region.Province);
if RowIndex > 0 then
begin
// toggle region picked or not
PickedStatus := sgRegions.Rows[RowIndex][0];
if PickedStatus = '0' then
PickedStatus := '1'
else
PickedStatus := '0';
sgRegions.Rows[RowIndex][0] := PickedStatus;
SortRegions;
SavePickedRegionsToFile('regions.txt');
sbMainStatusBar.Panels[1].Text :=
Format('Picked: %d of %d', [CountPickedRegions, sgRegions.RowCount - 1]);
end;
end;
procedure TfrmMain.imgMapScroll(Sender: TObject);
begin
sbMainStatusBar.Panels[0].Text := 'Zoom: ' + IntToStr(imgMap.ImageZoom) + '%';
tbZoom.Position := imgMap.ImageZoom;
end;
procedure TfrmMain.mnuMainOutputModeMercClick(Sender: TObject);
begin
OutputMode := omMercPool;
sbMainStatusBar.Panels[3].Text := 'Merc Pool';
SortRegions;
SavePickedRegionsToFile('regions.txt');
end;
procedure TfrmMain.mnuMainOutputModeWinClick(Sender: TObject);
begin
OutputMode := omWinCondition;
sbMainStatusBar.Panels[3].Text := 'Win Condition';
SortRegions;
SavePickedRegionsToFile('regions.txt');
end;
function TfrmMain.ScreenColor(x, y: integer): TColor;
var
ScreenDC: HDC;
Bitmap: TBitmap;
begin
Bitmap := TBitmap.Create;
try
Bitmap.SetSize(Screen.Width, Screen.Height);
ScreenDC := GetDC(0);
try
Bitmap.LoadFromDevice(ScreenDC);
finally
ReleaseDC(0, ScreenDC);
end;
Exit(Bitmap.Canvas.Pixels[x, y]);
finally
FreeAndNil(Bitmap);
end;
end;
procedure TfrmMain.tbZoomChange(Sender: TObject);
begin
imgMap.ImageZoom := Round(tbZoom.Position);
sbMainStatusBar.Panels[0].Text := 'Zoom: ' + IntToStr(imgMap.ImageZoom) + '%';
end;
procedure TfrmMain.mnuAboutClick(Sender: TObject);
begin
ShowMessage(TITLE + ' ' + VERSION + LineEnding + NOTE + LineEnding +
COPYRIGHT + ' ' + AUTHOR);
end;
procedure TfrmMain.mnuMainBrowseClick(Sender: TObject);
var
CampaignDir, BaseDir: string;
RegionColor: TColor;
ImgData: TImageData;
ImgBitmap: TImagingBitmap;
i: integer;
begin
ImgBitmap := TImagingBitmap.Create;
try
// read region colors and names from descr_regions.txt
if dlgCampaignDir.Execute then
begin
CampaignDir := dlgCampaignDir.FileName;
frmMain.Caption := TITLE + ' ' + VERSION + ' - ' +
CampaignDir.Substring(Length(ExpandFileName(CampaignDir +
'\..\..\..\..\..\..')) + 1);
BaseDir := ExpandFileName(CampaignDir + '\..\..\base');
try
// first check campaign directory
if FileExists(CampaignDir + '\descr_regions.txt') then
begin
FreeAndNil(RegionDict);
RegionDict := LoadRegions(CampaignDir + '\descr_regions.txt', GameMode);
end
// then check base directory if necessary
else if FileExists(BaseDir + '\descr_regions.txt') then
begin
FreeAndNil(RegionDict);
RegionDict := LoadRegions(BaseDir + '\descr_regions.txt', GameMode);
end
// regions file not found
else
begin
ShowMessage('descr_regions.txt not found. Please check ' +
'the campaign and base folders.');
Exit;
end;
except
// wrong game mode or invalid regions file
FreeAndNil(RegionDict);
ShowMessage(
'Something went wrong. Either you''ve selected the wrong ' +
'game mode or there is invalid code in descr_regions.txt.');
Exit;
end;
// list regions
sgRegions.RowCount := 1;
i := 1;
for RegionColor in RegionDict.Keys do
begin
sgRegions.InsertRowWithValues(
i, ['0', RegionDict[RegionColor].Province,
RegionDict[RegionColor].Settlement]);
i := i + 1;
end;
SortRegions;
// load map_regions.tga
InitImage(ImgData);
if FileExists(CampaignDir + '\map_regions.tga') then
LoadImageFromFile(CampaignDir + '\map_regions.tga', ImgData)
else if FileExists(BaseDir + '\map_regions.tga') then
LoadImageFromFile(BaseDir + '\map_regions.tga', ImgData);
ImgBitmap.AssignFromImageData(ImgData);
imgMap.Picture.Graphic := ImgBitmap;
imgMap.OptFitToWindow := True;
// report number of regions
sbMainStatusBar.Panels[1].Text :=
'Picked: 0 of ' + IntToStr(sgRegions.RowCount - 1);
end;
finally
FreeImage(ImgData);
FreeAndNil(ImgBitmap);
end;
end;
procedure TfrmMain.mnuMainColumnsRegionClick(Sender: TObject);
begin
sgRegions.Columns[1].Visible := mnuMainColumnsRegion.Checked;
end;
procedure TfrmMain.mnuMainColumnsSettlementClick(Sender: TObject);
begin
sgRegions.Columns[2].Visible := mnuMainColumnsSettlement.Checked;
end;
procedure TfrmMain.mnuMainGameModeBIClick(Sender: TObject);
begin
GameMode := gmBI;
sbMainStatusBar.Panels[2].Text := 'BI';
end;
procedure TfrmMain.mnuMainGameModeRTWClick(Sender: TObject);
begin
GameMode := gmRTW;
sbMainStatusBar.Panels[2].Text := 'RTW';
end;
procedure TfrmMain.sgRegionsCheckboxToggled(Sender: TObject);
begin
SortRegions;
SavePickedRegionsToFile('regions.txt');
sbMainStatusBar.Panels[1].Text :=
Format('Picked: %d of %d', [CountPickedRegions, sgRegions.RowCount - 1]);
end;
procedure TfrmMain.SavePickedRegionsToFile(const Filename: string);
var
s: string;
begin
if CountPickedRegions = 0 then
s := ''
else
s := PickedRegionsToStr(sgRegions, OutputMode);
SaveStringToFile(s, Filename);
end;
procedure TfrmMain.SortRegions;
var
PickedCount: integer;
begin
PickedCount := CountPickedRegions;
sgRegions.SortOrder := soDescending;
sgRegions.SortColRow(True, 0);
sgRegions.SortOrder := soAscending;
if OutputMode = omMercPool then
if PickedCount > 0 then
sgRegions.SortColRow(True, 1, 1, PickedCount)
else
sgRegions.SortColRow(True, 1)
else if OutputMode = omWinCondition then
if PickedCount > 0 then
sgRegions.SortColRow(True, 2, 1, PickedCount)
else
sgRegions.SortColRow(True, 2);
sgRegions.TopRow := 1;
end;
function TfrmMain.CountPickedRegions: integer;
var
RowIndex: integer;
begin
Result := 0;
if sgRegions.RowCount > 1 then
for RowIndex := 1 to sgRegions.RowCount - 1 do
if sgRegions.Rows[RowIndex][0] = '1' then
Result := Result + 1;
end;
end.