gamedataformunit.pas 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. unit gamedataformunit;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, Buttons,
  6. StdCtrls, database, fpjson;
  7. type
  8. { TGameDataForm }
  9. TGameDataForm = class(TForm)
  10. ActorIdComboBox: TComboBox;
  11. EnemyIdComboBox: TComboBox;
  12. ActorParamComboBox: TComboBox;
  13. CharIdComboBox: TComboBox;
  14. EnemyParamComboBox: TComboBox;
  15. CharParamParamComboBox: TComboBox;
  16. EnemyRadioButton: TRadioButton;
  17. CharRadioButton: TRadioButton;
  18. GameComboBox: TComboBox;
  19. PartyRadioButton: TRadioButton;
  20. ItemComboBox: TComboBox;
  21. EcCancelBitBtn: TBitBtn;
  22. EcOkBitBtn: TBitBtn;
  23. EcResultLineBevel: TBevel;
  24. EcResultPanel: TPanel;
  25. GameDataGroupBox: TGroupBox;
  26. ActorRadioButton: TRadioButton;
  27. GameRadioButton: TRadioButton;
  28. WeaponComboBox: TComboBox;
  29. ItemRadioButton: TRadioButton;
  30. ArmourComboBox: TComboBox;
  31. PartyComboBox: TComboBox;
  32. WeaponRadioButton: TRadioButton;
  33. ArmourRadioButton: TRadioButton;
  34. procedure RadioButtonChange(Sender: TObject);
  35. private
  36. Db: TDatabase;
  37. Troop: TJSONObject;
  38. procedure InitComboBoxes;
  39. procedure UpdateEnabledState;
  40. procedure PresetOldData(OldData: TJSONArray);
  41. public
  42. function SelectGameData(ADb: TDatabase; ATroop: TJSONObject;
  43. OldData: TJSONArray): Boolean;
  44. function GetArgs: TJSONArray;
  45. end;
  46. TGameDataType = (gdtItem = 0, gdtWeapon = 1, gdtArmour = 2, gdtActorData = 3,
  47. gdtEnemyData = 4, gdtCharData = 5, gdtPartyMemberId = 6,
  48. gdtGameParam = 7);
  49. var
  50. GameDataForm: TGameDataForm;
  51. implementation
  52. {$R *.lfm}
  53. uses
  54. comboboxhelper, globals, namehelper, constants;
  55. resourcestring
  56. rsPartyLeader = 'Leader';
  57. rsPartyMember = 'Member #%d';
  58. rsUnnamedEvent = 'Event #%d';
  59. rsUnnamedEnemyId = '#%d';
  60. rsNamedEnemyId = '#%d %s';
  61. { TGameDataForm }
  62. procedure TGameDataForm.RadioButtonChange(Sender: TObject);
  63. begin
  64. UpdateEnabledState;
  65. end;
  66. procedure TGameDataForm.InitComboBoxes;
  67. procedure FillCharacters;
  68. var
  69. Events: TJSONArray;
  70. I: Integer;
  71. begin
  72. with CharIdComboBox.Items do begin
  73. BeginUpdate;
  74. Clear;
  75. Add(PreprocessComboboxValue(rsPlayerChar));
  76. Add(PreprocessComboboxValue(rsThisEvent));
  77. if (Game.Map <> nil) and Game.Map.Find('events', Events) then
  78. for I := 1 to Events.Count - 1 do begin
  79. if Events[I].JSONType = jtObject then
  80. Add(Events.Objects[I].Strings['name'])
  81. else
  82. Add(rsUnnamedEvent.Format([I])); {TODO: don't show unnamed events}
  83. end;
  84. EndUpdate;
  85. end;
  86. CharIdComboBox.ItemIndex := 0;
  87. end;
  88. procedure FillEnemyIds;
  89. var
  90. I: Integer;
  91. Members: TJSONArray;
  92. EnemyDbId: Integer;
  93. begin
  94. with EnemyIdComboBox.Items do begin
  95. BeginUpdate;
  96. Clear;
  97. for I := 1 to 8 do begin
  98. if (Troop <> nil) and (Troop.Find('members', Members))
  99. and (Members.Count >= I) then begin
  100. EnemyDbId := Members.Objects[I-1].Integers['enemyId'];
  101. Add(rsNamedEnemyId.Format([I, Db.GetEnemyName(EnemyDbId, False)]));
  102. end else
  103. Add(rsUnnamedEnemyId.Format([I]));
  104. end;
  105. EndUpdate;
  106. end;
  107. EnemyIdComboBox.ItemIndex := 0;
  108. end;
  109. procedure FillPartyMemberIds;
  110. var
  111. I: Integer;
  112. begin
  113. with PartyComboBox.Items do begin
  114. BeginUpdate;
  115. Clear;
  116. Add(rsPartyLeader);
  117. for I := 2 to 8 do begin
  118. Add(rsPartyMember.Format([I]));
  119. end;
  120. EndUpdate;
  121. end;
  122. PartyComboBox.ItemIndex := 0;
  123. end;
  124. begin
  125. FillDbArrayComboBox(ItemComboBox, Db.Items);
  126. FillDbArrayComboBox(WeaponComboBox, Db.Weapons);
  127. FillDbArrayComboBox(ArmourComboBox, Db.Armours);
  128. FillDbArrayComboBox(ActorIdComboBox, Db.Actors);
  129. FillNamesComboBox(ActorParamComboBox, Db, @SetVar_GetGameData_ActorData, 0, 11);
  130. ActorParamComboBox.ItemIndex := 0;
  131. FillEnemyIds;
  132. FillNamesComboBox(EnemyParamComboBox, Db, @SetVar_GetGameData_EnemyData, 0, 9);
  133. EnemyParamComboBox.ItemIndex := 0;
  134. FillCharacters;
  135. FillNamesComboBox(CharParamParamComboBox, @SetVar_GetGameData_CharDataDesc,
  136. 0, 4);
  137. CharParamParamComboBox.ItemIndex := 0;
  138. FillPartyMemberIds;
  139. FillNamesComboBox(GameComboBox, @SetVar_GetGameData_Other, 0, 9);
  140. GameComboBox.ItemIndex := 0;
  141. end;
  142. procedure TGameDataForm.UpdateEnabledState;
  143. begin
  144. ItemComboBox.Enabled := ItemRadioButton.Checked;
  145. WeaponComboBox.Enabled := WeaponRadioButton.Checked;
  146. ArmourComboBox.Enabled := ArmourRadioButton.Checked;
  147. ActorIdComboBox.Enabled := ActorRadioButton.Checked;
  148. ActorParamComboBox.Enabled := ActorRadioButton.Checked;
  149. EnemyIdComboBox.Enabled := EnemyRadioButton.Checked;
  150. EnemyParamComboBox.Enabled := EnemyRadioButton.Checked;
  151. CharIdComboBox.Enabled := CharRadioButton.Checked;
  152. CharParamParamComboBox.Enabled := CharRadioButton.Checked;
  153. PartyComboBox.Enabled := PartyRadioButton.Checked;
  154. GameComboBox.Enabled := GameRadioButton.Checked;
  155. end;
  156. procedure TGameDataForm.PresetOldData(OldData: TJSONArray);
  157. procedure PresetSingleId(RadioButton: TRadioButton; ComboBox: TComboBox);
  158. begin
  159. RadioButton.Checked := True;
  160. ComboBox.ItemIndex := OldData.Integers[1] - 1;
  161. end;
  162. procedure PresetActorData;
  163. begin
  164. ActorRadioButton.Checked := True;
  165. ActorIdComboBox.ItemIndex := OldData.Integers[1] - 1;
  166. if (OldData[2].JSONType = jtNumber) then
  167. ActorParamComboBox.ItemIndex := OldData.Integers[2];
  168. end;
  169. procedure PresetEnemyData;
  170. begin
  171. EnemyRadioButton.Checked := True;
  172. EnemyIdComboBox.ItemIndex := OldData.Integers[1];
  173. if (OldData[2].JSONType = jtNumber) then
  174. EnemyParamComboBox.ItemIndex := OldData.Integers[2];
  175. end;
  176. procedure PresetCharData;
  177. begin
  178. CharRadioButton.Checked := True;
  179. CharIdComboBox.ItemIndex := OldData.Integers[1] + 1;
  180. if (OldData[2].JSONType = jtNumber) then
  181. CharParamParamComboBox.ItemIndex := OldData.Integers[2];
  182. end;
  183. procedure PresetPartyMemberData;
  184. begin
  185. PartyRadioButton.Checked := True;
  186. PartyComboBox.ItemIndex := OldData.Integers[1];
  187. end;
  188. procedure PresetGameParamData;
  189. begin
  190. GameRadioButton.Checked := True;
  191. GameComboBox.ItemIndex := OldData.Integers[1];
  192. end;
  193. var
  194. DataType: TGameDataType;
  195. begin
  196. if (OldData = nil) or (OldData.Count < 2) or (OldData[0].JSONType <> jtNumber)
  197. or (OldData.Integers[0] < Ord(Low(TGameDataType)))
  198. or (OldData.Integers[0] > Ord(High(TGameDataType)))
  199. or (OldData[1].JSONType <> jtNumber) then
  200. Exit;
  201. DataType := TGameDataType(OldData.Integers[0]);
  202. case DataType of
  203. gdtItem: PresetSingleId(ItemRadioButton, ItemComboBox);
  204. gdtWeapon: PresetSingleId(WeaponRadioButton, WeaponComboBox);
  205. gdtArmour: PresetSingleId(ArmourRadioButton, ArmourComboBox);
  206. gdtActorData: PresetActorData;
  207. gdtEnemyData: PresetEnemyData;
  208. gdtCharData: PresetCharData;
  209. gdtPartyMemberId: PresetPartyMemberData;
  210. gdtGameParam: PresetGameParamData;
  211. end;
  212. end;
  213. function TGameDataForm.SelectGameData(ADb: TDatabase; ATroop: TJSONObject;
  214. OldData: TJSONArray): Boolean;
  215. begin
  216. Db := ADb;
  217. Troop := ATroop;
  218. InitComboBoxes;
  219. UpdateEnabledState;
  220. PresetOldData(OldData);
  221. ShowModal;
  222. Result := ModalResult = mrOK;
  223. end;
  224. function TGameDataForm.GetArgs: TJSONArray;
  225. var
  226. Args: TJSONArray;
  227. begin
  228. Args := TJSONArray.Create;
  229. if ItemRadioButton.Checked then begin
  230. Args.Add(Ord(gdtItem));
  231. Args.Add(ItemComboBox.ItemIndex + 1);
  232. end else if WeaponRadioButton.Checked then begin
  233. Args.Add(Ord(gdtWeapon));
  234. Args.Add(WeaponComboBox.ItemIndex + 1);
  235. end else if ArmourRadioButton.Checked then begin
  236. Args.Add(Ord(gdtArmour));
  237. Args.Add(ArmourComboBox.ItemIndex + 1);
  238. end else if ActorRadioButton.Checked then begin
  239. Args.Add(Ord(gdtActorData));
  240. Args.Add(ActorIdComboBox.ItemIndex + 1);
  241. Args.Add(ActorParamComboBox.ItemIndex);
  242. end else if EnemyRadioButton.Checked then begin
  243. Args.Add(Ord(gdtEnemyData));
  244. Args.Add(EnemyIdComboBox.ItemIndex);
  245. Args.Add(EnemyParamComboBox.ItemIndex);
  246. end else if CharRadioButton.Checked then begin
  247. Args.Add(Ord(gdtCharData));
  248. Args.Add(CharIdComboBox.ItemIndex - 1);
  249. Args.Add(CharParamParamComboBox.ItemIndex);
  250. end else if PartyRadioButton.Checked then begin
  251. Args.Add(Ord(gdtPartyMemberId));
  252. Args.Add(PartyComboBox.ItemIndex);
  253. end else if GameRadioButton.Checked then begin
  254. Args.Add(Ord(gdtGameParam));
  255. Args.Add(GameComboBox.ItemIndex);
  256. end;
  257. GetArgs := Args;
  258. end;
  259. end.