database_items.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. unit database_items;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, Forms, Controls, Graphics, Dialogs, database_base,
  6. itemselectorframeunit, skillitemadditionaldataframeunit, LCLTranslator,
  7. StdCtrls, ExtCtrls, Buttons, Spin, fpjson;
  8. type
  9. { TDatabaseItemsFrame }
  10. TDatabaseItemsFrame = class(TDatabaseBaseFrame)
  11. AnimationComboBox: TComboBox;
  12. AnimationLabel: TLabel;
  13. HitTypeComboBox: TComboBox;
  14. HitTypeLabel: TLabel;
  15. InvocationFlowPanel: TFlowPanel;
  16. InvocationGroupBox: TGroupBox;
  17. IsConsummableCheckBox: TCheckBox;
  18. DescriptionLabel: TLabel;
  19. DescriptionMemo: TMemo;
  20. GeneralSettingsFlowPanel: TFlowPanel;
  21. GeneralSettingsScrollBox: TScrollBox;
  22. IconIdLabel: TLabel;
  23. IconLabel: TLabel;
  24. IconSpeedButton: TSpeedButton;
  25. IsConsumablePanel: TPanel;
  26. HitTypePanel: TPanel;
  27. AnimationPanel: TPanel;
  28. ItemAdditionalDataFrame: TSkillItemAdditionalDataFrame;
  29. SuccessPercentLabel: TLabel;
  30. TpGainPanel: TPanel;
  31. RepeatPanel: TPanel;
  32. RepeatLabel: TLabel;
  33. RepeatSpinEdit: TSpinEdit;
  34. SuccessPanel: TPanel;
  35. SpeedLabel: TLabel;
  36. SpeedPanel: TPanel;
  37. PriceLabel: TLabel;
  38. NameEdit: TEdit;
  39. NameLabel: TLabel;
  40. NamePanel: TPanel;
  41. IconPanel: TPanel;
  42. DescriptionPanel: TPanel;
  43. PricePanel: TPanel;
  44. PriceSpinEdit: TSpinEdit;
  45. ScopeComboBox: TComboBox;
  46. OccasionComboBox: TComboBox;
  47. ScopeLabel: TLabel;
  48. MainAdditionalSplitter: TSplitter;
  49. OccasionLabel: TLabel;
  50. OccasionPanel: TPanel;
  51. MainInvocationSplitter: TSplitter;
  52. InvocationScrollBox: TScrollBox;
  53. SpeedSpinEdit: TSpinEdit;
  54. SuccessLabel: TLabel;
  55. SuccessSpinEdit: TSpinEdit;
  56. TpGainLabel: TLabel;
  57. TpGainSpinEdit: TSpinEdit;
  58. TypePanel: TPanel;
  59. DbItemSelectorFrame: TItemSelectorFrame;
  60. GeneralSettingsGroupBox: TGroupBox;
  61. MainContentPanel: TPanel;
  62. SelectorContentSplitter: TSplitter;
  63. TypeComboBox: TComboBox;
  64. TypeLabel: TLabel;
  65. ScopePanel: TPanel;
  66. procedure AnimationComboBoxChange(Sender: TObject);
  67. procedure DescriptionMemoChange(Sender: TObject);
  68. procedure HitTypeComboBoxChange(Sender: TObject);
  69. procedure IconSpeedButtonClick(Sender: TObject);
  70. procedure Init; override;
  71. procedure IsConsummableCheckBoxChange(Sender: TObject);
  72. procedure NameEditChange(Sender: TObject);
  73. procedure OccasionComboBoxChange(Sender: TObject);
  74. procedure PriceSpinEditChange(Sender: TObject);
  75. procedure RepeatSpinEditChange(Sender: TObject);
  76. procedure ScopeComboBoxChange(Sender: TObject);
  77. procedure SpeedSpinEditChange(Sender: TObject);
  78. procedure SuccessSpinEditChange(Sender: TObject);
  79. procedure TpGainSpinEditChange(Sender: TObject);
  80. procedure TypeComboBoxChange(Sender: TObject);
  81. private
  82. IsLoading: Boolean;
  83. SelectedId: Integer;
  84. procedure LoadGeneralSettings;
  85. procedure LoadInvocationData;
  86. procedure LoadIcon(Item: TJSONObject);
  87. function GetSelectedItem: TJSONObject;
  88. procedure FillItemTypes;
  89. procedure FillAnimations;
  90. procedure ResizeMemo;
  91. public
  92. procedure LoadItemData(Id: Integer);
  93. procedure UpdateLanguage(Code: String); override;
  94. end;
  95. resourcestring
  96. ItemsHeader = 'Items';
  97. implementation
  98. uses
  99. languagehelper, comboboxhelper, constants, globals, iconselection, gamefont;
  100. {$R *.lfm}
  101. { TDatabaseItemsFrame }
  102. procedure TDatabaseItemsFrame.Init;
  103. begin
  104. IsLoading := True;
  105. DbItemSelectorFrame.SetData(Db.Items, @Db.ResizeItems, @LoadItemData);
  106. DbItemSelectorFrame.Title := ItemsHeader;
  107. DbItemSelectorFrame.JsonDataType := 'application/rpgmv-items';
  108. DbItemSelectorFrame.CreateElementCallback := @Db.CreateEmptyItem;
  109. FillItemTypes;
  110. FillScopeComboBox(ScopeComboBox);
  111. FillOccasionComboBox(OccasionComboBox);
  112. FillHitTypeComboBox(HitTypeComboBox);
  113. FillAnimations;
  114. ItemAdditionalDataFrame.SetDatabase(Db, siadtItem);
  115. ResizeMemo;
  116. LoadItemData(1);
  117. end;
  118. procedure TDatabaseItemsFrame.IsConsummableCheckBoxChange(Sender: TObject);
  119. var
  120. Item: TJSONObject = nil;
  121. begin
  122. if not IsLoading then
  123. Item := GetSelectedItem;
  124. if Item <> nil then
  125. Item.Booleans['consumable'] := IsConsummableCheckBox.Checked;
  126. end;
  127. procedure TDatabaseItemsFrame.DescriptionMemoChange(Sender: TObject);
  128. var
  129. Item: TJSONObject = nil;
  130. begin
  131. if not IsLoading then
  132. Item := GetSelectedItem;
  133. if Item <> nil then
  134. Item.Strings['description'] := DescriptionMemo.Text;
  135. end;
  136. procedure TDatabaseItemsFrame.AnimationComboBoxChange(Sender: TObject);
  137. var
  138. Item: TJSONObject = nil;
  139. begin
  140. if not IsLoading then
  141. Item := GetSelectedItem;
  142. if Item <> nil then
  143. Item.Integers['animationId'] := AnimationComboBox.ItemIndex - 1;
  144. end;
  145. procedure TDatabaseItemsFrame.HitTypeComboBoxChange(Sender: TObject);
  146. var
  147. Item: TJSONObject = nil;
  148. begin
  149. if not IsLoading then
  150. Item := GetSelectedItem;
  151. if Item <> nil then
  152. Item.Integers['hitType'] := HitTypeComboBox.ItemIndex;
  153. end;
  154. procedure TDatabaseItemsFrame.IconSpeedButtonClick(Sender: TObject);
  155. var
  156. Item: TJSONObject;
  157. IconIndex: Integer = 0;
  158. IconIndexJson: TJSONNumber;
  159. begin
  160. Item := GetSelectedItem;
  161. if (Item <> nil) and Item.Find('iconIndex', IconIndexJson) then
  162. IconIndex := IconIndexJson.AsInteger;
  163. if IconSelectionForm.ShowIconSelection(IconIndex) then begin
  164. Item.Integers['iconIndex'] := IconSelectionForm.SelectedIconId;
  165. LoadIcon(Item);
  166. end;
  167. end;
  168. procedure TDatabaseItemsFrame.NameEditChange(Sender: TObject);
  169. var
  170. Item: TJSONObject = nil;
  171. begin
  172. if not IsLoading then
  173. Item := GetSelectedItem;
  174. if Item <> nil then begin
  175. Item.Strings['name'] := NameEdit.Text;
  176. DbItemSelectorFrame.RefreshSelectedName;
  177. end;
  178. end;
  179. procedure TDatabaseItemsFrame.OccasionComboBoxChange(Sender: TObject);
  180. var
  181. Item: TJSONObject = nil;
  182. begin
  183. if not IsLoading then
  184. Item := GetSelectedItem;
  185. if Item <> nil then
  186. Item.Integers['occasion'] := OccasionComboBox.ItemIndex;
  187. end;
  188. procedure TDatabaseItemsFrame.PriceSpinEditChange(Sender: TObject);
  189. var
  190. Item: TJSONObject = nil;
  191. begin
  192. if not IsLoading then
  193. Item := GetSelectedItem;
  194. if Item <> nil then
  195. Item.Integers['price'] := PriceSpinEdit.Value;
  196. end;
  197. procedure TDatabaseItemsFrame.RepeatSpinEditChange(Sender: TObject);
  198. var
  199. Item: TJSONObject = nil;
  200. begin
  201. if not IsLoading then
  202. Item := GetSelectedItem;
  203. if Item <> nil then
  204. Item.Integers['repeats'] := RepeatSpinEdit.Value;
  205. end;
  206. procedure TDatabaseItemsFrame.ScopeComboBoxChange(Sender: TObject);
  207. var
  208. Item: TJSONObject = nil;
  209. begin
  210. if not IsLoading then
  211. Item := GetSelectedItem;
  212. if Item <> nil then
  213. Item.Integers['scope'] := ScopeComboBox.ItemIndex;
  214. end;
  215. procedure TDatabaseItemsFrame.SpeedSpinEditChange(Sender: TObject);
  216. var
  217. Item: TJSONObject = nil;
  218. begin
  219. if not IsLoading then
  220. Item := GetSelectedItem;
  221. if Item <> nil then
  222. Item.Integers['speed'] := SpeedSpinEdit.Value;
  223. end;
  224. procedure TDatabaseItemsFrame.SuccessSpinEditChange(Sender: TObject);
  225. var
  226. Item: TJSONObject = nil;
  227. begin
  228. if not IsLoading then
  229. Item := GetSelectedItem;
  230. if Item <> nil then
  231. Item.Integers['successRate'] := SuccessSpinEdit.Value;
  232. end;
  233. procedure TDatabaseItemsFrame.TpGainSpinEditChange(Sender: TObject);
  234. var
  235. Item: TJSONObject = nil;
  236. begin
  237. if not IsLoading then
  238. Item := GetSelectedItem;
  239. if Item <> nil then
  240. Item.Integers['tpGain'] := TpGainSpinEdit.Value;
  241. end;
  242. procedure TDatabaseItemsFrame.TypeComboBoxChange(Sender: TObject);
  243. var
  244. Item: TJSONObject = nil;
  245. begin
  246. if not IsLoading then
  247. Item := GetSelectedItem;
  248. if Item <> nil then
  249. Item.Integers['itypeId'] := TypeComboBox.ItemIndex + 1;
  250. end;
  251. procedure TDatabaseItemsFrame.LoadGeneralSettings;
  252. var
  253. Item: TJSONObject;
  254. ItemName: String = '';
  255. Description: String = '';
  256. ItemType: Integer = 1;
  257. Price: Integer = 0;
  258. Occasion: Integer = 0;
  259. Scope: Integer = 0;
  260. IsConsumable: Boolean;
  261. ItemTypeJson, PriceJson, ScopeJson, OccasionJson: TJSONNumber;
  262. ItemNameJson, DescriptionJson: TJSONString;
  263. IsConsumableJson: TJSONBoolean;
  264. begin
  265. Item := GetSelectedItem;
  266. if Item = nil then
  267. Exit;
  268. if (Item <> nil) and Item.Find('name', ItemNameJson) then
  269. ItemName := ItemNameJson.AsString;
  270. NameEdit.Text := ItemName;
  271. LoadIcon(Item);
  272. if (Item <> nil) and Item.Find('description', DescriptionJson) then
  273. Description := DescriptionJson.AsString;
  274. DescriptionMemo.Text := Description;
  275. if (Item <> nil) and Item.Find('itypeId', ItemTypeJson) then
  276. ItemType := ItemTypeJson.AsInteger;
  277. TypeComboBox.ItemIndex := ItemType - 1;
  278. if (Item <> nil) and Item.Find('price', PriceJson) then
  279. Price := PriceJson.AsInteger;
  280. PriceSpinEdit.Value := Price;
  281. if (Item <> nil) and Item.Find('consumable', IsConsumableJson) then
  282. IsConsumable := IsConsumableJson.AsBoolean;
  283. IsConsummableCheckBox.Checked := IsConsumable;
  284. if (Item <> nil) and Item.Find('scope', ScopeJson) then
  285. Scope := ScopeJson.AsInteger;
  286. ScopeComboBox.ItemIndex := Scope;
  287. if (Item <> nil) and Item.Find('occasion', OccasionJson) then
  288. Occasion := OccasionJson.AsInteger;
  289. OccasionComboBox.ItemIndex := Occasion;
  290. end;
  291. procedure TDatabaseItemsFrame.LoadInvocationData;
  292. var
  293. Item: TJSONObject;
  294. Speed: Integer = 0;
  295. Success: Integer = 0;
  296. Repeats: Integer = 0;
  297. TpGain: Integer = 0;
  298. HitType: Integer = 0;
  299. AnimationId: Integer = 0;
  300. SpeedJson, SuccessJson, RepeatsJson, TpGainJson, AnimationIdJson, HitTypeJson:
  301. TJSONNumber;
  302. begin
  303. Item := GetSelectedItem;
  304. if Item = nil then
  305. Exit;
  306. if (Item <> nil) and Item.Find('speed', SpeedJson) then
  307. Speed := SpeedJson.AsInteger;
  308. SpeedSpinEdit.Value := Speed;
  309. if (Item <> nil) and Item.Find('successRate', SuccessJson) then
  310. Success := SuccessJson.AsInteger;
  311. SuccessSpinEdit.Value := Success;
  312. if (Item <> nil) and Item.Find('repeats', RepeatsJson) then
  313. Repeats := RepeatsJson.AsInteger;
  314. RepeatSpinEdit.Value := Repeats;
  315. if (Item <> nil) and Item.Find('tpGain', TpGainJson) then
  316. TpGain := TpGainJson.AsInteger;
  317. TpGainSpinEdit.Value := TpGain;
  318. if (Item <> nil) and Item.Find('hitType', HitTypeJson) then
  319. HitType := HitTypeJson.AsInteger;
  320. HitTypeComboBox.ItemIndex := HitType;
  321. if (Item <> nil) and Item.Find('animationId', AnimationIdJson) then
  322. AnimationId := AnimationIdJson.AsInteger;
  323. AnimationComboBox.ItemIndex := AnimationId;
  324. end;
  325. procedure TDatabaseItemsFrame.LoadIcon(Item: TJSONObject);
  326. var
  327. IconId: Integer = 0;
  328. IconIdJson: TJSONNumber;
  329. IconBitmap: TBitmap;
  330. begin
  331. if (Item <> nil) and Item.Find('iconIndex', IconIdJson) then
  332. IconId := IconIdJson.AsInteger;
  333. IconIdLabel.Caption := IntToStr(IconId);
  334. if IconId <> 0 then begin
  335. IconBitmap := Game.GetIcon(IconId);
  336. IconSpeedButton.Glyph.Assign(IconBitmap);
  337. IconBitmap.Free;
  338. end else
  339. IconSpeedButton.Glyph.Clear;
  340. end;
  341. function TDatabaseItemsFrame.GetSelectedItem: TJSONObject;
  342. begin
  343. GetSelectedItem := nil;
  344. if Db.Items = nil then
  345. Exit;
  346. if (SelectedId >= 1) and (SelectedId < Db.Items.Count) then
  347. GetSelectedItem := Db.Items.Objects[SelectedId];
  348. end;
  349. procedure TDatabaseItemsFrame.FillItemTypes;
  350. begin
  351. with TypeComboBox.Items do begin
  352. BeginUpdate;
  353. Clear;
  354. Add(ItemTypeNormal);
  355. Add(ItemTypeKey);
  356. Add(ItemTypeHiddenA);
  357. Add(ItemTypeHiddenB);
  358. EndUpdate;
  359. end;
  360. end;
  361. procedure TDatabaseItemsFrame.FillAnimations;
  362. //TODO: unify this with code from Database_Skills
  363. var
  364. I: Integer;
  365. begin
  366. if (Db = nil) or (Db.Animations = nil) then
  367. Exit;
  368. with AnimationComboBox.items do begin
  369. BeginUpdate;
  370. Clear;
  371. Append(AnimationNormalAttack);
  372. Append(AnimationNone);
  373. for I := 1 to Db.Animations.Count -1 do begin
  374. Append(Db.GetAnimName(I));
  375. end;
  376. EndUpdate;
  377. end;
  378. end;
  379. procedure TDatabaseItemsFrame.ResizeMemo;
  380. begin
  381. DescriptionMemo.Font.BeginUpdate;
  382. DescriptionMemo.Font.Name := GetMonospacedFontName;
  383. DescriptionMemo.Font.Size := GetMonospacedFontSize;
  384. DescriptionMemo.Font.EndUpdate;
  385. ResizeMemoForFont(DescriptionMemo, 55, 2);
  386. end;
  387. procedure TDatabaseItemsFrame.LoadItemData(Id: Integer);
  388. begin
  389. IsLoading := True;
  390. SelectedId := Id;
  391. LoadGeneralSettings;
  392. LoadInvocationData;
  393. ItemAdditionalDataFrame.SetItem(Id);
  394. IsLoading := False;
  395. end;
  396. procedure TDatabaseItemsFrame.UpdateLanguage(Code: String);
  397. begin
  398. inherited UpdateLanguage(Code);
  399. if IsLanguageRtl(Code) then begin
  400. GeneralSettingsFlowPanel.FlowStyle := fsRightLeftTopBottom;
  401. InvocationFlowPanel.FlowStyle := fsRightLeftTopBottom;
  402. end else begin
  403. GeneralSettingsFlowPanel.FlowStyle := fsLeftRightTopBottom;
  404. InvocationFlowPanel.FlowStyle := fsLeftRightTopBottom;
  405. end;
  406. end;
  407. end.