databaseformunit.pas 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. unit databaseformunit;
  2. {$mode objfpc}{$H+}{$MACRO ON}
  3. interface
  4. uses
  5. Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls,
  6. Buttons, database, LCLTranslator, languagehelper,
  7. { Frames for individual database pages: }
  8. database_base, database_actors,
  9. database_terms, database_classes, database_skills, database_items,
  10. database_weapons, database_armours, database_enemies, database_troops,
  11. database_states, database_tilesets, database_cevs, database_system,
  12. database_start, database_types, database_svbattle;
  13. type
  14. { TDatabaseForm }
  15. TDatabaseFormPageId = (
  16. dfpiNoFrame = -1,
  17. dfpiActors = 0,
  18. dfpiClasses = 1,
  19. dfpiSkills = 2,
  20. dfpiItems = 3,
  21. dfpiWeapons = 4,
  22. dfpiArmours = 5,
  23. dfpiEnemies = 6,
  24. dfpiTroops = 7,
  25. dfpiStates = 8,
  26. dfpiAnimations = 9,
  27. dfpiTilesets = 10,
  28. dfpiCommonEvents = 11,
  29. dfpiSystem = 12,
  30. dfpiStart = 13,
  31. dfpiSvBattle = 14,
  32. dfpiAudio = 15,
  33. dfpiTypes = 16,
  34. dfpiTerms = 17
  35. );
  36. TDbFrameClass = class of TDatabaseBaseFrame;
  37. TDatabasePageDesc = record
  38. Id: TDatabaseFormPageId;
  39. Title: String;
  40. Frame: TDbFrameClass;
  41. end;
  42. TDatabaseForm = class(TForm)
  43. ApplyButton: TButton;
  44. OkBitBtn: TBitBtn;
  45. CancelBitBtn: TBitBtn;
  46. PageListBox: TListBox;
  47. PageSelectionLabel: TLabel;
  48. PageSelectionPanel: TPanel;
  49. PageSelectorSplitter: TSplitter;
  50. DecisionButtonsPanel: TPanel;
  51. PageFrame: TDatabaseBaseFrame;
  52. procedure ApplyButtonClick(Sender: TObject);
  53. procedure CancelButtonClick(Sender: TObject);
  54. procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
  55. procedure FormCreate(Sender: TObject);
  56. procedure OkButtonClick(Sender: TObject);
  57. procedure PageListBoxSelectionChange(Sender: TObject; User: boolean);
  58. function GetCurrentFrameType: TDatabaseFormPageId;
  59. function CreatePageFrame(PageId: TDatabaseFormPageId): TDatabaseBaseFrame;
  60. private
  61. EditedDb: TDatabase;
  62. procedure SetUpPages;
  63. public
  64. function ShowModal: Integer; override;
  65. procedure UpdateLanguage(Code: String);
  66. end;
  67. const
  68. DbPageData: array[0..17] of TDatabasePageDesc = (
  69. (Id: dfpiActors; Title: ActorsHeader; Frame: TDatabaseActorsFrame),
  70. (Id: dfpiClasses; Title: ClassesHeader; Frame: TDatabaseClassesFrame),
  71. (Id: dfpiSkills; Title: SkillsHeader; Frame: TDatabaseSkillsFrame),
  72. (Id: dfpiItems; Title: ItemsHeader; Frame: TDatabaseItemsFrame),
  73. (Id: dfpiWeapons; Title: WeaponsHeader; Frame: TDatabaseWeaponsFrame),
  74. (Id: dfpiArmours; Title: ArmoursHeader; Frame: TDatabaseArmoursFrame),
  75. (Id: dfpiEnemies; Title: EnemiesHeader; Frame: TDatabaseEnemiesFrame),
  76. (Id: dfpiTroops; Title: TroopsHeader; Frame: TDatabaseTroopsFrame),
  77. (Id: dfpiStates; Title: StatesHeader; Frame: TDatabaseStatesFrame),
  78. (Id: dfpiAnimations; Title: 'Animations'; Frame: nil),
  79. (Id: dfpiTilesets; Title: TilesetsHeader; Frame: TDatabaseTilesetsFrame),
  80. (Id: dfpiCommonEvents; Title: CevsHeader; Frame: TDatabaseCevsFrame),
  81. (Id: dfpiSystem; Title: SystemHeader; Frame: TDatabaseSystemFrame),
  82. (Id: dfpiStart; Title: StartHeader; Frame: TDatabaseStartFrame),
  83. (Id: dfpiSvBattle; Title: SvBattleHeader; Frame: TDatabaseSvBattleFrame),
  84. (Id: dfpiAudio; Title: 'Audio'; Frame: nil),
  85. (Id: dfpiTypes; Title: TypesHeader; Frame: TDatabaseTypesFrame),
  86. (Id: dfpiTerms; Title: 'Terms'; Frame: TDatabaseTermsFrame)
  87. );
  88. var
  89. DatabaseForm: TDatabaseForm;
  90. implementation
  91. uses Math, globals;
  92. {$R *.lfm}
  93. { TDatabaseForm }
  94. procedure TDatabaseForm.OkButtonClick(Sender: TObject);
  95. begin
  96. Game.Db.SetData(EditedDb);
  97. Game.DbIsDirty := True;
  98. EditedDb.Free;
  99. Close;
  100. end;
  101. function TDatabaseForm.GetCurrentFrameType: TDatabaseFormPageId;
  102. var
  103. I: Integer;
  104. begin
  105. GetCurrentFrameType := dfpiNoFrame;
  106. if PageFrame = nil then
  107. Exit;
  108. for I := Low(DbPageData) to High(DbPageData) do begin
  109. if PageFrame is DbPageData[I].Frame then begin
  110. GetCurrentFrameType := DbPageData[I].Id;
  111. Exit
  112. end;
  113. end;
  114. end;
  115. procedure TDatabaseForm.PageListBoxSelectionChange(Sender: TObject;
  116. User: boolean);
  117. var
  118. SelectedPage: TDatabaseFormPageId;
  119. begin
  120. SelectedPage := TDatabaseFormPageId(PageListBox.ItemIndex);
  121. if (EditedDb <> nil) and (SelectedPage <> GetCurrentFrameType) then begin
  122. if PageFrame <> nil then
  123. PageFrame.Free;
  124. PageFrame := CreatePageFrame(SelectedPage);
  125. end;
  126. end;
  127. function TDatabaseForm.CreatePageFrame(PageId: TDatabaseFormPageId):
  128. TDatabaseBaseFrame;
  129. var
  130. PageDesc: TDatabasePageDesc;
  131. Page: TDatabaseBaseFrame;
  132. PageClass: TDbFrameClass;
  133. begin
  134. Page := nil;
  135. if (Integer(PageId) >= Low(DbPageData)) and (Integer(PageId) <= High(DbPageData)) then begin
  136. PageDesc := DbPageData[Integer(PageId)];
  137. PageClass := PageDesc.Frame;
  138. if PageClass <> nil then
  139. Page := PageClass.Create(Self);
  140. end;
  141. if Page <> nil then begin
  142. Page.Visible := False;
  143. Page.DoubleBuffered := True;
  144. Page.Parent := Self;
  145. Page.Align := alClient;
  146. Page.TabOrder := 1;
  147. Page.HandleNeeded;
  148. Page.SetDatabase(EditedDb);
  149. Page.UpdateLanguage(EditorLanguage);
  150. Page.Visible := True
  151. end;
  152. CreatePageFrame := Page;
  153. end;
  154. procedure TDatabaseForm.SetUpPages;
  155. var
  156. I: Integer;
  157. begin
  158. PageListBox.Items.BeginUpdate;
  159. for I := Low(DbPageData) to High(DbPageData) do begin
  160. if PageListBox.Items.Count <= I then
  161. PageListBox.Items.Add(DbPageData[I].Title)
  162. else
  163. PageListBox.Items[I] := DbPageData[I].Title
  164. end;
  165. PageListBox.Items.EndUpdate;
  166. end;
  167. procedure TDatabaseForm.CancelButtonClick(Sender: TObject);
  168. begin
  169. EditedDb.Free;
  170. Close;
  171. end;
  172. procedure TDatabaseForm.FormClose(Sender: TObject;
  173. var CloseAction: TCloseAction);
  174. begin
  175. if PageFrame <> nil then begin
  176. PageFrame.Free;
  177. PageFrame := nil;
  178. end;
  179. end;
  180. procedure TDatabaseForm.FormCreate(Sender: TObject);
  181. procedure SwapButtons(A, B: TCustomButton);
  182. var
  183. Tmp: Integer;
  184. begin
  185. Tmp := A.Left;
  186. A.Left := B.Left;
  187. B.Left := Tmp;
  188. Tmp := A.TabOrder;
  189. A.TabOrder := B.TabOrder;
  190. B.TabOrder := Tmp;
  191. end;
  192. begin
  193. (* Make sure our window is not larger than screen.
  194. Ideally we should use a special layout compact for smaller screens *)
  195. Width := Min(Screen.Width - 20, Width);
  196. Height := Min(Screen.Height - 20, Height);
  197. { On Gnome, the button order is: Apply Cancel OK
  198. On Windows, the button oder is: OK Cancel Apply
  199. On KDE, the button order is: OK Apply Cancel
  200. On Mac, the button order is: Cancel Ok (we don't support Mac so just treat it as a GTK+ variant)}
  201. {$IF Defined(LCLGtk) or Defined(LCLGtk2) or Defined(LCLGtk3) or Defined(LCLCarbon) or Defined(LCLCocoa)}
  202. SwapButtons(OkBitBtn, ApplyButton);
  203. {$ELSEIF Defined(LCLQt) or Defined(LCLQt5)}
  204. SwapButtons(CancelBitBtn, ApplyButton);
  205. {$ENDIF}
  206. SetUpPages;
  207. PageListBox.ItemIndex := 0;
  208. end;
  209. procedure TDatabaseForm.ApplyButtonClick(Sender: TObject);
  210. begin
  211. Game.Db.SetData(EditedDb);
  212. Game.DbIsDirty := True;
  213. end;
  214. function TDatabaseForm.ShowModal: Integer;
  215. begin
  216. if Game.Db <> nil then begin
  217. EditedDb := TDatabase.Create;
  218. EditedDb.SetData(Game.Db);
  219. if PageFrame <> nil then
  220. PageFrame.Free;
  221. PageFrame := CreatePageFrame(TDatabaseFormPageId(PageListBox.ItemIndex));
  222. Result := inherited;
  223. end
  224. else begin
  225. Result := 0;
  226. end;
  227. end;
  228. procedure TDatabaseForm.UpdateLanguage(Code: String);
  229. begin
  230. SetUpPages;
  231. end;
  232. end.