actionpatternselection.pas 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. unit actionpatternselection;
  2. {$mode objfpc}{$H+}
  3. {$modeswitch nestedprocvars}
  4. interface
  5. uses
  6. Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Spin,
  7. EditBtn, ButtonPanel, database;
  8. type
  9. { TActionPatternSelectionForm }
  10. TActionPatternSelectionForm = class(TForm)
  11. AlwaysRadioButton: TRadioButton;
  12. ConditionGroupBox: TGroupBox;
  13. HpFromLabel: TLabel;
  14. HpFromSpinEdit: TSpinEdit;
  15. HpRadioButton: TRadioButton;
  16. HpToLabel: TLabel;
  17. HpToSpinEdit: TSpinEdit;
  18. MpFromLabel: TLabel;
  19. MpFromSpinEdit: TSpinEdit;
  20. MpRadioButton: TRadioButton;
  21. MpToLabel: TLabel;
  22. MpToSpinEdit: TSpinEdit;
  23. PartyLevelOrHigherLabel: TLabel;
  24. PartyLevelRadioButton: TRadioButton;
  25. PartyLevelSpinEdit: TSpinEdit;
  26. RatingGroupBox: TGroupBox;
  27. RatingSpinEdit: TSpinEdit;
  28. ResultButtonPanel: TButtonPanel;
  29. ContentScrollBox: TScrollBox;
  30. SkillComboBox: TComboBox;
  31. SkillGroupBox: TGroupBox;
  32. StateComboBox: TComboBox;
  33. StateRadioButton: TRadioButton;
  34. SwitchEditButton: TEditButton;
  35. SwitchRadioButton: TRadioButton;
  36. TurnNumberConstSpinEdit: TSpinEdit;
  37. TurnNumberMultiplierSpinEdit: TSpinEdit;
  38. TurnNumberPlusLabel: TLabel;
  39. TurnNumberRadioButton: TRadioButton;
  40. TurnNumberTimesXLabel: TLabel;
  41. procedure AlwaysRadioButtonChange(Sender: TObject);
  42. procedure FormHide(Sender: TObject);
  43. procedure FormShow(Sender: TObject);
  44. procedure HpRadioButtonChange(Sender: TObject);
  45. procedure MpRadioButtonChange(Sender: TObject);
  46. procedure PartyLevelRadioButtonChange(Sender: TObject);
  47. procedure PercentParamSpinEditChange(Sender: TObject);
  48. procedure RatingSpinEditChange(Sender: TObject);
  49. procedure SkillComboBoxChange(Sender: TObject);
  50. procedure StateComboBoxChange(Sender: TObject);
  51. procedure StateRadioButtonChange(Sender: TObject);
  52. procedure SwitchEditButtonClick(Sender: TObject);
  53. procedure SwitchRadioButtonChange(Sender: TObject);
  54. procedure ParamSpinEditChange(Sender: TObject);
  55. procedure TurnNumberRadioButtonChange(Sender: TObject);
  56. private
  57. Db: TDatabase;
  58. procedure UnifyRadioButtonSizes;
  59. public
  60. SelectedSkillId: Integer;
  61. SelectedRating: Integer;
  62. SelectedCondType: Integer;
  63. SelectedCondParam1, SelectedCondParam2: Double;
  64. SelectedSwitch: Integer;
  65. function ShowSelection(SkillId, Rating, CondType: Integer; Param1, Param2: Double; ADb: TDatabase): Boolean;
  66. end;
  67. TRadioButtonCallback = procedure(Radio: TRadioButton) is nested;
  68. var
  69. ActionPatternSelectionForm: TActionPatternSelectionForm;
  70. implementation
  71. uses
  72. Math, comboboxhelper, constants, namedindexselection;
  73. {$R *.lfm}
  74. { TActionPatternSelectionForm }
  75. procedure TActionPatternSelectionForm.SwitchEditButtonClick(Sender: TObject);
  76. begin
  77. SelectedSwitch := NamedIndexSelectionForm.SelectSwitch(SelectedSwitch, Db);
  78. SelectedCondParam1 := SelectedSwitch;
  79. SwitchEditButton.Text := Db.GetSwitchName(SelectedSwitch);
  80. end;
  81. procedure TActionPatternSelectionForm.SwitchRadioButtonChange(Sender: TObject);
  82. begin
  83. SwitchEditButton.Enabled := SwitchRadioButton.Checked;
  84. if SwitchRadioButton.Checked then begin
  85. SelectedCondType := ENEMY_ACTION_CONDITION_SWITCH;
  86. SelectedCondParam1 := SelectedSwitch;
  87. SelectedCondParam2 := 0;
  88. end;
  89. end;
  90. procedure TActionPatternSelectionForm.ParamSpinEditChange(
  91. Sender: TObject);
  92. var
  93. SpinEdit: TSpinEdit;
  94. begin
  95. SpinEdit := Sender as TSpinEdit;
  96. if SpinEdit.Tag = 1 then
  97. SelectedCondParam1 := SpinEdit.Value
  98. else if SpinEdit.Tag = 2 then
  99. SelectedCondParam2 := SpinEdit.Value
  100. end;
  101. procedure TActionPatternSelectionForm.TurnNumberRadioButtonChange(
  102. Sender: TObject);
  103. begin
  104. TurnNumberConstSpinEdit.Enabled := TurnNumberRadioButton.Checked;
  105. if TurnNumberRadioButton.Checked then begin
  106. SelectedCondType := ENEMY_ACTION_CONDITION_TURN;
  107. TurnNumberMultiplierSpinEdit.Enabled := TurnNumberRadioButton.Checked;
  108. SelectedCondParam1 := TurnNumberConstSpinEdit.Value;
  109. SelectedCondParam2 := TurnNumberMultiplierSpinEdit.Value;
  110. end;
  111. end;
  112. procedure TActionPatternSelectionForm.FormHide(Sender: TObject);
  113. begin
  114. SkillComboBox.Items.Clear;
  115. StateComboBox.Items.Clear;
  116. end;
  117. procedure TActionPatternSelectionForm.AlwaysRadioButtonChange(Sender: TObject);
  118. begin
  119. if AlwaysRadioButton.Checked then begin
  120. SelectedCondType := ENEMY_ACTION_CONDITION_ALWAYS;
  121. SelectedCondParam1 := 0;
  122. SelectedCondParam2 := 0;
  123. end;
  124. end;
  125. procedure TActionPatternSelectionForm.UnifyRadioButtonSizes;
  126. var
  127. MaxWidth: Integer = 0;
  128. procedure IterateOverRadioButtons(Callback: TRadioButtonCallback);
  129. begin
  130. { I did this because 'typed constants of classes or interfaces are not
  131. allowed', so I couldn't just declare an array of radiobuttons. Better
  132. solutions are welcome. }
  133. Callback(TurnNumberRadioButton);
  134. Callback(HpRadioButton);
  135. Callback(MpRadioButton);
  136. Callback(StateRadioButton);
  137. Callback(PartyLevelRadioButton);
  138. Callback(SwitchRadioButton);
  139. end;
  140. procedure SetAutoSizeOn(Radio: TRadioButton);
  141. begin
  142. Radio.AutoSize := True;
  143. end;
  144. procedure FindMaxWidth(Radio: TRadioButton);
  145. begin
  146. MaxWidth := Max(Radio.Width, MaxWidth);
  147. end;
  148. procedure SetMaxWidth(Radio: TRadioButton);
  149. begin
  150. Radio.AutoSize := False;
  151. Radio.Width := MaxWidth;
  152. end;
  153. var
  154. NeedsFlipping: Boolean;
  155. begin
  156. { Not sure why, but autosize magic only works with LTR, at least in Windows,
  157. so flip everything and then flip back if we're in RTL mode }
  158. NeedsFlipping := IsFlipped;
  159. if IsFlipped then
  160. FlipChildren(True);
  161. MaxWidth := 0;
  162. IterateOverRadioButtons(@SetAutoSizeOn);
  163. IterateOverRadioButtons(@FindMaxWidth);
  164. IterateOverRadioButtons(@SetMaxWidth);
  165. if NeedsFlipping then
  166. FlipChildren(True);
  167. end;
  168. function TActionPatternSelectionForm.ShowSelection(SkillId, Rating,
  169. CondType: Integer; Param1, Param2: Double; ADb: TDatabase): Boolean;
  170. begin
  171. Db := ADb;
  172. SelectedSkillId := SkillId;
  173. SelectedRating := Rating;
  174. SelectedCondType := CondType;
  175. SelectedCondParam1 := Param1;
  176. SelectedCondParam2 := Param2;
  177. FillSkillComboBox(SkillComboBox, Db);
  178. FillStateComboBox(StateComboBox, Db);
  179. SkillComboBox.ItemIndex := SkillId - 1;
  180. RatingSpinEdit.Value := Rating;
  181. case CondType of
  182. ENEMY_ACTION_CONDITION_ALWAYS:
  183. AlwaysRadioButton.Checked := True;
  184. ENEMY_ACTION_CONDITION_TURN: begin
  185. TurnNumberRadioButton.Checked := True;
  186. TurnNumberConstSpinEdit.Value := Round(Param1);
  187. TurnNumberMultiplierSpinEdit.Value := Round(Param2);
  188. end;
  189. ENEMY_ACTION_CONDITION_HP: begin
  190. HpRadioButton.Checked := True;
  191. HpFromSpinEdit.Value := Round(Param1 * 100);
  192. HpToSpinEdit.Value := Round(Param2 * 100);
  193. end;
  194. ENEMY_ACTION_CONDITION_MP: begin
  195. MpRadioButton.Checked := True;
  196. MpFromSpinEdit.Value := Round(Param1 * 100);
  197. MpToSpinEdit.Value := Round(Param2 * 100);
  198. end;
  199. ENEMY_ACTION_CONDITION_STATE: begin
  200. StateRadioButton.Checked := True;
  201. StateComboBox.ItemIndex := Round(Param1 - 1);
  202. end;
  203. ENEMY_ACTION_CONDITION_PARTY_LV: begin
  204. PartyLevelRadioButton.Checked := True;
  205. PartyLevelSpinEdit.Value := Round(Param1);
  206. end;
  207. ENEMY_ACTION_CONDITION_SWITCH: begin
  208. SwitchRadioButton.Checked := True;
  209. SelectedSwitch := Round(Param1);
  210. end;
  211. end;
  212. if SelectedSwitch < 1 then
  213. SelectedSwitch := 1;
  214. SwitchEditButton.Text := Db.GetSwitchName(SelectedSwitch);
  215. ShowModal;
  216. ShowSelection := ModalResult = mrOK;
  217. end;
  218. procedure TActionPatternSelectionForm.FormShow(Sender: TObject);
  219. begin
  220. UnifyRadioButtonSizes;
  221. end;
  222. procedure TActionPatternSelectionForm.HpRadioButtonChange(Sender: TObject);
  223. begin
  224. HpFromSpinEdit.Enabled := HpRadioButton.Checked;
  225. if HpRadioButton.Checked then begin
  226. SelectedCondType := ENEMY_ACTION_CONDITION_HP;
  227. HpToSpinEdit.Enabled := HpRadioButton.Checked;
  228. SelectedCondParam1 := HpFromSpinEdit.Value / 100;
  229. SelectedCondParam2 := HpToSpinEdit.Value / 100;
  230. end;
  231. end;
  232. procedure TActionPatternSelectionForm.MpRadioButtonChange(Sender: TObject);
  233. begin
  234. MpFromSpinEdit.Enabled := MpRadioButton.Checked;
  235. if MpRadioButton.Checked then begin
  236. SelectedCondType := ENEMY_ACTION_CONDITION_MP;
  237. MpToSpinEdit.Enabled := MpRadioButton.Checked;
  238. SelectedCondParam1 := MpFromSpinEdit.Value / 100;
  239. SelectedCondParam2 := MpToSpinEdit.Value / 100;
  240. end;
  241. end;
  242. procedure TActionPatternSelectionForm.PartyLevelRadioButtonChange(
  243. Sender: TObject);
  244. begin
  245. PartyLevelSpinEdit.Enabled := PartyLevelRadioButton.Checked;
  246. if PartyLevelRadioButton.Checked then begin
  247. SelectedCondType := ENEMY_ACTION_CONDITION_PARTY_LV;
  248. SelectedCondParam1 := PartyLevelSpinEdit.Value;
  249. SelectedCondParam2 := 0;
  250. end;
  251. end;
  252. procedure TActionPatternSelectionForm.PercentParamSpinEditChange(Sender: TObject
  253. );
  254. var
  255. SpinEdit: TSpinEdit;
  256. begin
  257. SpinEdit := Sender as TSpinEdit;
  258. if SpinEdit.Tag = 1 then
  259. SelectedCondParam1 := SpinEdit.Value / 100
  260. else if SpinEdit.Tag = 2 then
  261. SelectedCondParam2 := SpinEdit.Value / 100
  262. end;
  263. procedure TActionPatternSelectionForm.RatingSpinEditChange(Sender: TObject);
  264. begin
  265. SelectedRating := RatingSpinEdit.Value;
  266. end;
  267. procedure TActionPatternSelectionForm.SkillComboBoxChange(Sender: TObject);
  268. begin
  269. SelectedSkillId := SkillComboBox.ItemIndex + 1;
  270. end;
  271. procedure TActionPatternSelectionForm.StateComboBoxChange(Sender: TObject);
  272. begin
  273. SelectedCondParam1 := StateComboBox.ItemIndex + 1;
  274. end;
  275. procedure TActionPatternSelectionForm.StateRadioButtonChange(Sender: TObject);
  276. begin
  277. StateComboBox.Enabled := StateRadioButton.Checked;
  278. if StateRadioButton.Checked then begin
  279. SelectedCondType := ENEMY_ACTION_CONDITION_STATE;
  280. SelectedCondParam1 := StateComboBox.ItemIndex + 1;
  281. SelectedCondParam2 := 0;
  282. end;
  283. end;
  284. end.