doublebackgroundselection.pas 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. unit doublebackgroundselection;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls,
  6. ButtonPanel, BGRABitmap, BGRABitmapTypes;
  7. type
  8. { TDoubleBackgroundSelectionForm }
  9. TDoubleBackgroundType = (dbtBattle, dbtTitle);
  10. TDoubleBackgroundSelectionForm = class(TForm)
  11. Bg1ListBox: TListBox;
  12. Bg1Bg2Splitter: TSplitter;
  13. Bg2ListBox: TListBox;
  14. Bg2PreviewSplitter: TSplitter;
  15. ResultButtonPanel: TButtonPanel;
  16. PreviewPaintBox: TPaintBox;
  17. PreviewScrollBox: TScrollBox;
  18. procedure BgListBoxClick(Sender: TObject);
  19. procedure CancelButtonClick(Sender: TObject);
  20. procedure OKButtonClick(Sender: TObject);
  21. procedure PreviewPaintBoxPaint(Sender: TObject);
  22. private
  23. DoubleBgType: TDoubleBackgroundType;
  24. ResultWasAccepted: Boolean;
  25. Previews: array [1..2] of TBGRACustomBitmap;
  26. procedure FillListBox(Box: TListBox; Subfolder: String; CurrentValue: String);
  27. public
  28. SelectedBgs: array [1..2] of String;
  29. function ShowBattleBgSelection(Bg1, Bg2: String): Boolean;
  30. function ShowTitleBgSelection(Bg1, Bg2: String): Boolean;
  31. end;
  32. var
  33. DoubleBackgroundSelectionForm: TDoubleBackgroundSelectionForm;
  34. implementation
  35. uses
  36. filehelper, globals, constants;
  37. {$R *.lfm}
  38. { TDoubleBackgroundSelectionForm }
  39. procedure TDoubleBackgroundSelectionForm.BgListBoxClick(Sender: TObject);
  40. var
  41. BgType: Integer;
  42. Box: TListBox;
  43. NewBg: String;
  44. begin
  45. Box := (Sender as TListBox);
  46. BgType := Box.Tag;
  47. if Box.ItemIndex = 0 then
  48. NewBg := ''
  49. else
  50. NewBg := Box.Items[Box.ItemIndex];
  51. if NewBg <> SelectedBgs[BgType] then begin
  52. SelectedBgs[BgType] := NewBg;
  53. Previews[BgType].Free;
  54. Previews[BgType] := nil;
  55. if DoubleBgType = dbtBattle then
  56. Previews[BgType] := Game.GetScaledBattlebackImgBGRA(NewBg, BgType,
  57. PreviewPaintBox.Width / SCREEN_WIDTH)
  58. else if DoubleBgType = dbtTitle then
  59. Previews[BgType] := Game.GetScaledTitleImgBGRA(NewBg, BgType,
  60. PreviewPaintBox.Width / SCREEN_WIDTH);
  61. PreviewPaintBox.Refresh;
  62. end;
  63. end;
  64. procedure TDoubleBackgroundSelectionForm.CancelButtonClick(Sender: TObject);
  65. begin
  66. ResultWasAccepted := False;
  67. Close
  68. end;
  69. procedure TDoubleBackgroundSelectionForm.OKButtonClick(Sender: TObject);
  70. begin
  71. ResultWasAccepted := True;
  72. Close
  73. end;
  74. procedure TDoubleBackgroundSelectionForm.PreviewPaintBoxPaint(Sender: TObject);
  75. begin
  76. if Previews[1] <> nil then
  77. Previews[1].Draw(PreviewPaintBox.Canvas, 0, 0, True);
  78. if Previews[2] <> nil then
  79. Previews[2].Draw(PreviewPaintBox.Canvas, 0, 0, False);
  80. end;
  81. procedure TDoubleBackgroundSelectionForm.FillListBox(Box: TListBox;
  82. Subfolder: String; CurrentValue: String);
  83. var
  84. Path: String;
  85. FileName: String;
  86. Files: TFilenameList;
  87. PreselectedIndex: Integer = -1;
  88. begin
  89. Box.Items.BeginUpdate;
  90. try
  91. Box.Items.Clear;
  92. Box.Items.Add(NoneFilename);
  93. if (CurrentValue = '') then begin
  94. PreselectedIndex := Box.Items.Count - 1;
  95. end;
  96. Path := Game.GetImageSubfolderPath(Subfolder);
  97. Files := FillFilenameList(Path);
  98. try
  99. for FileName in Files do begin
  100. Box.Items.Add(FileName);
  101. if CurrentValue = FileName then begin
  102. PreselectedIndex := Box.Items.Count - 1;
  103. end;
  104. end;
  105. finally
  106. Files.Free;
  107. end;
  108. finally
  109. Box.Items.EndUpdate;
  110. end;
  111. Box.ItemIndex := PreselectedIndex;
  112. end;
  113. function TDoubleBackgroundSelectionForm.ShowBattleBgSelection(Bg1, Bg2: String
  114. ): Boolean;
  115. begin
  116. ResultWasAccepted := False;
  117. DoubleBgType := dbtBattle;
  118. FillListBox(Bg1ListBox, 'battlebacks1', Bg1);
  119. FillListBox(Bg2ListBox, 'battlebacks2', Bg2);
  120. if Bg1 <> '' then
  121. Previews[1] := Game.GetScaledBattlebackImgBGRA(Bg1, 1,
  122. PreviewPaintBox.Width / SCREEN_WIDTH);
  123. if Bg2 <> '' then
  124. Previews[2] := Game.GetScaledBattlebackImgBGRA(Bg2, 2,
  125. PreviewPaintBox.Width / SCREEN_WIDTH);
  126. ShowModal;
  127. ShowBattleBgSelection := ResultWasAccepted
  128. end;
  129. function TDoubleBackgroundSelectionForm.ShowTitleBgSelection(Bg1, Bg2: String
  130. ): Boolean;
  131. begin
  132. ResultWasAccepted := False;
  133. DoubleBgType := dbtTitle;
  134. FillListBox(Bg1ListBox, 'titles1', Bg1);
  135. FillListBox(Bg2ListBox, 'titles2', Bg2);
  136. if Bg1 <> '' then
  137. Previews[1] := Game.GetScaledTitleImgBGRA(Bg1, 1,
  138. PreviewPaintBox.Width / SCREEN_WIDTH);
  139. if Bg2 <> '' then
  140. Previews[2] := Game.GetScaledTitleImgBGRA(Bg2, 2,
  141. PreviewPaintBox.Width / SCREEN_WIDTH);
  142. ShowModal;
  143. ShowTitleBgSelection := ResultWasAccepted
  144. end;
  145. end.