database_armours.pas 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. unit database_armours;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls,
  6. Buttons, Spin, Menus, database_base, itemselectorframeunit,
  7. paramchangesframeunit, traitlist, fpjson;
  8. type
  9. { TDatabaseArmoursFrame }
  10. TDatabaseArmoursFrame = class(TDatabaseBaseFrame)
  11. AdditionalDataPanel: TPanel;
  12. CopyMenuItem: TMenuItem;
  13. CutMenuItem: TMenuItem;
  14. DeleteMenuItem: TMenuItem;
  15. EditMenuItem: TMenuItem;
  16. EquipTypeComboBox: TComboBox;
  17. AnimationLabel: TLabel;
  18. AnimationPanel: TPanel;
  19. ArmourSelectorFrame: TItemSelectorFrame;
  20. DescriptionLabel: TLabel;
  21. DescriptionMemo: TMemo;
  22. DescriptionPanel: TPanel;
  23. GeneralSettingsFlowPanel: TFlowPanel;
  24. GeneralSettingsGroupBox: TGroupBox;
  25. IconIdLabel: TLabel;
  26. IconLabel: TLabel;
  27. IconPanel: TPanel;
  28. IconSpeedButton: TSpeedButton;
  29. MainContentScrollBox: TScrollBox;
  30. NameEdit: TEdit;
  31. NameLabel: TLabel;
  32. NamePanel: TPanel;
  33. NotesGroupBox: TGroupBox;
  34. NotesMemo: TMemo;
  35. ParamChangesFlowPanel: TFlowPanel;
  36. ParameterChangesGroupBox: TGroupBox;
  37. PasteMenuItem: TMenuItem;
  38. PriceLabel: TLabel;
  39. PricePanel: TPanel;
  40. PriceSpinEdit: TSpinEdit;
  41. SelectorContentSplitter: TSplitter;
  42. MainAdditionalContentSplitter: TSplitter;
  43. TraitsGroupBox: TGroupBox;
  44. TraitsListBox: TListBox;
  45. TraitsNotesSplitter: TSplitter;
  46. TypeComboBox: TComboBox;
  47. TypeLabel: TLabel;
  48. TypePanel: TPanel;
  49. ArmourParamChangesFrame: TParamChangesFrame;
  50. ArmourTraitListFrame: TTraitListFrame;
  51. procedure AnimationPanelClick(Sender: TObject);
  52. procedure DescriptionMemoChange(Sender: TObject);
  53. procedure IconSpeedButtonClick(Sender: TObject);
  54. procedure Init; override;
  55. procedure NameEditChange(Sender: TObject);
  56. procedure NotesMemoChange(Sender: TObject);
  57. procedure PriceSpinEditChange(Sender: TObject);
  58. procedure TypeComboBoxChange(Sender: TObject);
  59. private
  60. IsLoading: Boolean;
  61. SelectedId: Integer;
  62. procedure ResizeMemo;
  63. procedure LoadGeneralSettings(Armour: TJSONObject);
  64. procedure LoadParamChanges(Armour: TJSONObject);
  65. procedure LoadAdditionalSettings(Armour: TJSONObject);
  66. procedure LoadIcon(Armour: TJSONObject);
  67. function GetSelectedArmour: TJSONObject;
  68. public
  69. procedure LoadArmourData(Id: Integer);
  70. procedure UpdateLanguage(Code: String); override;
  71. end;
  72. resourcestring
  73. ArmoursHeader = 'Armours';
  74. implementation
  75. uses
  76. globals, gamefont, languagehelper, comboboxhelper, iconselection;
  77. {$R *.lfm}
  78. { TDatabaseArmoursFrame }
  79. procedure TDatabaseArmoursFrame.Init;
  80. begin
  81. IsLoading := True;
  82. ArmourSelectorFrame.SetData(Db.Armours, @Db.ResizeArmours, @LoadArmourData);
  83. ArmourSelectorFrame.Title := ArmoursHeader;
  84. ArmourSelectorFrame.JsonDataType := 'application/rpgmv-armors';
  85. ArmourSelectorFrame.CreateElementCallback := @Db.CreateEmptyArmour;
  86. FillArmourTypeComboBox(TypeComboBox, Db);
  87. FillArmourEquipTypeComboBox(EquipTypeComboBox, Db);
  88. ArmourParamChangesFrame.SetDatabase(Db);
  89. ArmourParamChangesFrame.AutoSize := True;
  90. ArmourTraitListFrame.SetDatabase(Db);
  91. ResizeMemo;
  92. LoadArmourData(1);
  93. end;
  94. procedure TDatabaseArmoursFrame.DescriptionMemoChange(Sender: TObject);
  95. var
  96. Armour: TJSONObject = nil;
  97. begin
  98. if not IsLoading then
  99. Armour := GetSelectedArmour;
  100. if Armour <> nil then
  101. Armour.Strings['description'] := DescriptionMemo.Text;
  102. end;
  103. procedure TDatabaseArmoursFrame.IconSpeedButtonClick(Sender: TObject);
  104. var
  105. Armour: TJSONObject = nil;
  106. IconIndex: Integer = 0;
  107. IconIndexJson: TJSONNumber;
  108. begin
  109. Armour := GetSelectedArmour;
  110. if (Armour <> nil) and Armour.Find('iconIndex', IconIndexJson) then
  111. IconIndex := IconIndexJson.AsInteger;
  112. if IconSelectionForm.ShowIconSelection(IconIndex) then begin
  113. Armour.Integers['iconIndex'] := IconSelectionForm.SelectedIconId;
  114. LoadIcon(Armour);
  115. end;
  116. end;
  117. procedure TDatabaseArmoursFrame.AnimationPanelClick(Sender: TObject);
  118. var
  119. Armour: TJSONObject = nil;
  120. begin
  121. if not IsLoading then
  122. Armour := GetSelectedArmour;
  123. if Armour <> nil then
  124. Armour.Integers['etypeId'] := TypeComboBox.ItemIndex + 2;
  125. end;
  126. procedure TDatabaseArmoursFrame.NameEditChange(Sender: TObject);
  127. var
  128. Armour: TJSONObject = nil;
  129. begin
  130. if IsLoading then
  131. Exit;
  132. Armour := GetSelectedArmour;
  133. if Armour <> nil then begin
  134. Armour.Strings['name'] := NameEdit.Text;
  135. ArmourSelectorFrame.RefreshSelectedName;
  136. end;
  137. end;
  138. procedure TDatabaseArmoursFrame.NotesMemoChange(Sender: TObject);
  139. var
  140. Armour: TJSONObject = nil;
  141. begin
  142. if not IsLoading then
  143. Armour := GetSelectedArmour;
  144. if Armour <> nil then
  145. Armour.Strings['note'] := NotesMemo.Text;
  146. end;
  147. procedure TDatabaseArmoursFrame.PriceSpinEditChange(Sender: TObject);
  148. var
  149. Armour: TJSONObject = nil;
  150. begin
  151. if not IsLoading then
  152. Armour := GetSelectedArmour;
  153. if Armour <> nil then
  154. Armour.Integers['price'] := PriceSpinEdit.Value
  155. end;
  156. procedure TDatabaseArmoursFrame.TypeComboBoxChange(Sender: TObject);
  157. var
  158. Armour: TJSONObject = nil;
  159. begin
  160. if not IsLoading then
  161. Armour := GetSelectedArmour;
  162. if Armour <> nil then
  163. Armour.Integers['atypeId'] := TypeComboBox.ItemIndex + 1
  164. end;
  165. procedure TDatabaseArmoursFrame.ResizeMemo;
  166. begin
  167. DescriptionMemo.Font.BeginUpdate;
  168. DescriptionMemo.Font.Name := GetMonospacedFontName;
  169. DescriptionMemo.Font.Size := GetMonospacedFontSize;
  170. DescriptionMemo.Font.EndUpdate;
  171. ResizeMemoForFont(DescriptionMemo, 55, 2)
  172. end;
  173. procedure TDatabaseArmoursFrame.LoadGeneralSettings(Armour: TJSONObject);
  174. var
  175. ArmourName: String = '';
  176. Description: String = '';
  177. ArmourTypeId: Integer = 0;
  178. Price: Integer = 0;
  179. EquipmentTypeId: Integer = 0;
  180. NameJson, DescriptionJson: TJSONString;
  181. ArmourTypeIdJson, PriceJson, EquipmentTypeIdJson: TJSONNumber;
  182. begin
  183. if (Armour <> nil) and Armour.Find('name', NameJson) then
  184. ArmourName := NameJson.AsString;
  185. NameEdit.Text := ArmourName;
  186. LoadIcon(Armour);
  187. if (Armour <> nil) and Armour.Find('description', DescriptionJson) then
  188. Description := DescriptionJson.AsString;
  189. DescriptionMemo.Text := Description;
  190. if (Armour <> nil) and Armour.Find('atypeId', ArmourTypeIdJson) then
  191. ArmourTypeId := ArmourTypeIdJson.AsInteger;
  192. TypeComboBox.ItemIndex := ArmourTypeId - 1;
  193. if (Armour <> nil) and Armour.Find('price', PriceJson) then
  194. Price := PriceJson.AsInteger;
  195. PriceSpinEdit.Value := Price;
  196. if (Armour <> nil) and Armour.Find('etypeId', EquipmentTypeIdJson) then
  197. EquipmentTypeId := EquipmentTypeIdJson.AsInteger;
  198. EquipTypeComboBox.ItemIndex := EquipmentTypeId - 2;
  199. end;
  200. procedure TDatabaseArmoursFrame.LoadParamChanges(Armour: TJSONObject);
  201. var
  202. Params: TJSONArray;
  203. begin
  204. if (Armour <> nil) and Armour.Find('params', Params) then
  205. ArmourParamChangesFrame.SetEditedParams(Params)
  206. else
  207. ArmourParamChangesFrame.SetEditedParams(nil);
  208. end;
  209. procedure TDatabaseArmoursFrame.LoadAdditionalSettings(Armour: TJSONObject);
  210. var
  211. Notes: String = '';
  212. NotesJson: TJSONString;
  213. Traits: TJSONArray;
  214. begin
  215. if (Armour <> nil) and Armour.Find('traits', Traits) then
  216. ArmourTraitListFrame.SetTraitList(Traits)
  217. else
  218. ArmourTraitListFrame.SetTraitList(nil);
  219. if (Armour <> nil) and Armour.Find('note', NotesJson) then
  220. Notes := NotesJson.AsString;
  221. NotesMemo.Text := Notes;
  222. end;
  223. procedure TDatabaseArmoursFrame.LoadIcon(Armour: TJSONObject);
  224. var
  225. IconId: Integer = 0;
  226. IconIdJson: TJSONNumber;
  227. IconBitmap: TBitmap;
  228. begin
  229. if (Armour <> nil) and Armour.Find('iconIndex', IconIdJson) then
  230. IconId := IconIdJson.AsInteger;
  231. IconIdLabel.Caption := IntToStr(IconId);
  232. if IconId <> 0 then begin
  233. IconBitmap := Game.GetIcon(IconId);
  234. IconSpeedButton.Glyph.Assign(IconBitmap);
  235. IconBitmap.Free;
  236. end else
  237. IconSpeedButton.Glyph.Clear;
  238. end;
  239. function TDatabaseArmoursFrame.GetSelectedArmour: TJSONObject;
  240. begin
  241. GetSelectedArmour := nil;
  242. if Db.Weapons <> nil then
  243. if (SelectedId >= 1) and (SelectedId <= Db.Armours.Count -1) then
  244. GetSelectedArmour := Db.Armours.Objects[SelectedId];
  245. end;
  246. procedure TDatabaseArmoursFrame.LoadArmourData(Id: Integer);
  247. var
  248. Armour: TJSONObject;
  249. begin
  250. IsLoading := True;
  251. SelectedId := Id;
  252. Armour := GetSelectedArmour;
  253. LoadGeneralSettings(Armour);
  254. LoadParamChanges(Armour);
  255. LoadAdditionalSettings(Armour);
  256. IsLoading := False;
  257. end;
  258. procedure TDatabaseArmoursFrame.UpdateLanguage(Code: String);
  259. begin
  260. inherited UpdateLanguage(Code);
  261. if IsLanguageRtl(Code) then begin
  262. GeneralSettingsFlowPanel.FlowStyle := fsRightLeftTopBottom;
  263. end else begin
  264. GeneralSettingsFlowPanel.FlowStyle := fsLeftRightTopBottom;
  265. end;
  266. ArmourParamChangesFrame.UpdateLanguage(Code);
  267. end;
  268. end.