battleventconditionsselection.pas 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. unit battleventconditionsselection;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Spin,
  6. EditBtn, ButtonPanel, fpjson, database;
  7. type
  8. { TBattleEventConditionsSelectionForm }
  9. TBattleEventConditionsSelectionForm = class(TForm)
  10. ActorHpCheckBox: TCheckBox;
  11. ResultButtonPanel: TButtonPanel;
  12. SwitchIsOnLabel: TLabel;
  13. SwitchCheckBox: TCheckBox;
  14. SwitchEditButton: TEditButton;
  15. EnemyHpCheckBox: TCheckBox;
  16. EnemyHpComboBox: TComboBox;
  17. ActorHpComboBox: TComboBox;
  18. EnemyHpLessOfEqualLabel: TLabel;
  19. ActorHpLessOfEqualLabel: TLabel;
  20. EnemyHpPercentLabel: TLabel;
  21. ActorHpPercentLabel: TLabel;
  22. ActorHpSpinEdit: TSpinEdit;
  23. TurnNumberTimesXLabel: TLabel;
  24. TurnNumberPlusLabel: TLabel;
  25. TurnNumberConstSpinEdit: TSpinEdit;
  26. TurnEndsCheckBox: TCheckBox;
  27. TurnNumberCheckBox: TCheckBox;
  28. TurnNumberVarSpinEdit: TSpinEdit;
  29. EnemyHpSpinEdit: TSpinEdit;
  30. procedure ActorHpCheckBoxChange(Sender: TObject);
  31. procedure CancelButtonClick(Sender: TObject);
  32. procedure EnemyHpCheckBoxChange(Sender: TObject);
  33. procedure FormDestroy(Sender: TObject);
  34. procedure OKButtonClick(Sender: TObject);
  35. procedure SwitchCheckBoxChange(Sender: TObject);
  36. procedure SwitchEditButtonButtonClick(Sender: TObject);
  37. procedure TurnNumberCheckBoxChange(Sender: TObject);
  38. private
  39. IsLoading: Boolean;
  40. ResultWasAccepted: Boolean;
  41. Db: TDatabase;
  42. Troop: TJSONObject;
  43. procedure PrefillData(Cond: TJSONObject);
  44. procedure UpdateSwitchDisplay;
  45. function MakeSelectedCondition: TJSONObject;
  46. public
  47. SelectedSwitch: Integer;
  48. SelectedCondition: TJSONObject;
  49. function ShowConditionSelector(Condition: TJSONObject; ATroop: TJSONObject; ADb: TDatabase): Boolean;
  50. end;
  51. var
  52. BattleEventConditionsSelectionForm: TBattleEventConditionsSelectionForm;
  53. implementation
  54. uses
  55. namedindexselection, comboboxhelper;
  56. {$R *.lfm}
  57. { TBattleEventConditionsSelectionForm }
  58. procedure TBattleEventConditionsSelectionForm.OKButtonClick(Sender: TObject);
  59. begin
  60. ResultWasAccepted := True;
  61. Close
  62. end;
  63. procedure TBattleEventConditionsSelectionForm.SwitchCheckBoxChange(
  64. Sender: TObject);
  65. begin
  66. SwitchEditButton.Enabled := SwitchCheckBox.Checked;
  67. end;
  68. procedure TBattleEventConditionsSelectionForm.SwitchEditButtonButtonClick(
  69. Sender: TObject);
  70. begin
  71. SelectedSwitch := NamedIndexSelectionForm.SelectSwitch(SelectedSwitch, Db);
  72. UpdateSwitchDisplay
  73. end;
  74. procedure TBattleEventConditionsSelectionForm.TurnNumberCheckBoxChange(
  75. Sender: TObject);
  76. begin
  77. TurnNumberConstSpinEdit.Enabled := TurnNumberCheckBox.Checked;
  78. TurnNumberVarSpinEdit.Enabled := TurnNumberCheckBox.Checked
  79. end;
  80. procedure TBattleEventConditionsSelectionForm.ActorHpCheckBoxChange(
  81. Sender: TObject);
  82. begin
  83. ActorHpComboBox.Enabled := ActorHpCheckBox.Checked;
  84. ActorHpSpinEdit.Enabled := ActorHpCheckBox.Checked
  85. end;
  86. procedure TBattleEventConditionsSelectionForm.CancelButtonClick(Sender: TObject
  87. );
  88. begin
  89. ResultWasAccepted := False;
  90. Close
  91. end;
  92. procedure TBattleEventConditionsSelectionForm.EnemyHpCheckBoxChange(
  93. Sender: TObject);
  94. begin
  95. EnemyHpComboBox.Enabled := EnemyHpCheckBox.Checked;
  96. EnemyHpSpinEdit.Enabled:= EnemyHpCheckBox.Checked
  97. end;
  98. procedure TBattleEventConditionsSelectionForm.FormDestroy(Sender: TObject);
  99. begin
  100. if SelectedCondition <> nil then
  101. SelectedCondition.Free;
  102. end;
  103. procedure TBattleEventConditionsSelectionForm.PrefillData(Cond: TJSONObject);
  104. procedure PrefillCheckbox(Checkbox: TCheckBox; VarName: String);
  105. var
  106. IsValid: Boolean = False;
  107. IsValidJson: TJSONBoolean;
  108. begin
  109. if (Cond <> nil) and Cond.Find(VarName, IsValidJson) then
  110. IsValid := IsValidJson.AsBoolean;
  111. Checkbox.Checked := IsValid;
  112. end;
  113. procedure PrefillHp(Spin: TSpinEdit; VarName: String);
  114. var
  115. Hp: Integer = 0;
  116. HpJson: TJSONNumber;
  117. begin
  118. if (Cond <> nil) and Cond.Find(VarName, HpJson) then
  119. Hp := HpJson.AsInteger;
  120. Spin.Value := Hp;
  121. end;
  122. procedure PrefillEnemyIndex;
  123. var
  124. Index: Integer = 0;
  125. IndexJson: TJSONNumber;
  126. begin
  127. if (Cond <> nil) and Cond.Find('enemyIndex', IndexJson) then
  128. Index := IndexJson.AsInteger;
  129. EnemyHpComboBox.ItemIndex := Index;
  130. end;
  131. procedure PrefillActorId;
  132. var
  133. Id: Integer = 0;
  134. IdJson: TJSONNumber;
  135. begin
  136. if (Cond <> nil) and Cond.Find('actorId', IdJson) then
  137. Id := IdJson.AsInteger;
  138. ActorHpComboBox.ItemIndex := Id - 1;
  139. end;
  140. procedure PrefillTurnNumber;
  141. var
  142. ConstVal: Integer = 0;
  143. VarVal: Integer = 0;
  144. ConstVarJson, VarValJson: TJSONNumber;
  145. begin
  146. if (Cond <> nil) and Cond.Find('turnA', ConstVarJson) then
  147. ConstVal := ConstVarJson.AsInteger;
  148. if (Cond <> nil) and Cond.Find('turnB', VarValJson) then
  149. VarVal := VarValJson.AsInteger;
  150. TurnNumberConstSpinEdit.Value := ConstVal;
  151. TurnNumberVarSpinEdit.Value := VarVal;
  152. end;
  153. procedure PrefillSwitch;
  154. var
  155. SwitchIdJson: TJSONNumber;
  156. begin
  157. SelectedSwitch := 1;
  158. if (Cond <> nil) and Cond.Find('switchId', SwitchIdJson) then
  159. SelectedSwitch := SwitchIdJson.AsInteger;
  160. UpdateSwitchDisplay;
  161. end;
  162. begin
  163. IsLoading := True;
  164. PrefillCheckbox(TurnEndsCheckBox, 'turnEnding');
  165. PrefillCheckbox(TurnNumberCheckBox, 'turnValid');
  166. PrefillTurnNumber;
  167. PrefillCheckbox(EnemyHpCheckBox, 'enemyValid');
  168. PrefillEnemyIndex;
  169. PrefillHp(EnemyHpSpinEdit, 'enemyHp');
  170. PrefillCheckbox(ActorHpCheckBox, 'actorValid');
  171. PrefillActorId;
  172. PrefillHp(ActorHpSpinEdit, 'actorHp');
  173. PrefillCheckbox(SwitchCheckBox, 'switchValid');
  174. PrefillSwitch;
  175. IsLoading := False;
  176. end;
  177. procedure TBattleEventConditionsSelectionForm.UpdateSwitchDisplay;
  178. begin
  179. SwitchEditButton.Text := Db.GetSwitchName(SelectedSwitch);
  180. end;
  181. function TBattleEventConditionsSelectionForm.MakeSelectedCondition: TJSONObject;
  182. begin
  183. MakeSelectedCondition := TJSONObject.Create([
  184. 'actorHp', ActorHpSpinEdit.Value,
  185. 'actorId', ActorHpComboBox.ItemIndex + 1,
  186. 'actorValid', ActorHpCheckBox.Checked,
  187. 'enemyHp', EnemyHpSpinEdit.Value,
  188. 'enemyIndex', EnemyHpComboBox.ItemIndex,
  189. 'enemyValid', EnemyHpCheckBox.Checked,
  190. 'switchId', SelectedSwitch,
  191. 'switchValid', SwitchCheckBox.Checked,
  192. 'turnA', TurnNumberConstSpinEdit.Value,
  193. 'turnB', TurnNumberVarSpinEdit.Value,
  194. 'turnEnding', TurnEndsCheckBox.Checked,
  195. 'turnValid', TurnNumberCheckBox.Checked
  196. ]);
  197. end;
  198. function TBattleEventConditionsSelectionForm.ShowConditionSelector(
  199. Condition: TJSONObject; ATroop: TJSONObject; ADb: TDatabase): Boolean;
  200. procedure FillTroopComboBox;
  201. var
  202. I: Integer;
  203. Members: TJSONArray;
  204. begin
  205. Members := ATroop.Arrays['members'];
  206. with EnemyHpComboBox.Items do begin
  207. BeginUpdate;
  208. Clear;
  209. for I := 0 to 7 do begin
  210. if I < Members.Count then
  211. Add(Format('#%d %s', [I+1, Db.GetEnemyName(Members.Objects[I].Integers['enemyId'], False)]))
  212. else
  213. Add('#' + IntToStr(I+1));
  214. end;
  215. EndUpdate;
  216. end;
  217. end;
  218. begin
  219. Db := ADb;
  220. Troop := ATroop;
  221. ResultWasAccepted := False;
  222. FillTroopComboBox;
  223. FillDbArrayComboBox(ActorHpComboBox, Db.Actors);
  224. PrefillData(Condition);
  225. ShowModal;
  226. if SelectedCondition <> nil then begin
  227. SelectedCondition.Free;
  228. SelectedCondition := nil
  229. end;
  230. SelectedCondition := MakeSelectedCondition;
  231. ActorHpComboBox.Items.Clear;
  232. EnemyHpComboBox.Items.Clear;
  233. ShowConditionSelector := ResultWasAccepted
  234. end;
  235. end.