GLS.TextureImageEditors.pas 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. //
  2. // The graphics platform GLScene https://github.com/glscene
  3. //
  4. unit GLS.TextureImageEditors;
  5. (* Standard texture image editors for standard texture image classes. *)
  6. interface
  7. {$I Scenario.inc}
  8. uses
  9. System.Classes,
  10. System.SysUtils,
  11. GLS.Texture,
  12. GLS.ProcTextures;
  13. type
  14. TGLTextureImageEditor = class(TObject)
  15. public
  16. (* Request to edit a textureImage.
  17. Returns True if changes have been made.
  18. This method may be invoked from the IDE or at run-time. *)
  19. class function Edit(aTexImage: TGLTextureImage): Boolean; virtual;
  20. end;
  21. TGLTextureImageEditorClass = class of TGLTextureImageEditor;
  22. TGLBlankTIE = class(TGLTextureImageEditor)
  23. public
  24. class function Edit(aTexImage: TGLTextureImage): Boolean; override;
  25. end;
  26. TGLPersistentTIE = class(TGLTextureImageEditor)
  27. public
  28. class function Edit(aTexImage: TGLTextureImage): Boolean; override;
  29. end;
  30. TGLPicFileTIE = class(TGLTextureImageEditor)
  31. public
  32. class function Edit(aTexImage: TGLTextureImage): Boolean; override;
  33. end;
  34. TGLProcTextureNoiseTIE = class(TGLTextureImageEditor)
  35. public
  36. class function Edit(aTexImage: TGLTextureImage): Boolean; override;
  37. end;
  38. // Invokes the editor for the given TGLTextureImage
  39. function EditGLTextureImage(aTexImage: TGLTextureImage): Boolean;
  40. procedure RegisterGLTextureImageEditor(aTexImageClass: TGLTextureImageClass;
  41. texImageEditor: TGLTextureImageEditorClass);
  42. procedure UnRegisterGLTextureImageEditor(texImageEditor: TGLTextureImageEditorClass);
  43. // ------------------------------------------------------------------------------
  44. implementation
  45. // ------------------------------------------------------------------------------
  46. uses
  47. GLS.Utils;
  48. var
  49. vTIEClass, vTIEEditor: TList;
  50. class function TGLTextureImageEditor.Edit(ATexImage: TGLTextureImage): Boolean;
  51. begin
  52. Result := True;
  53. end;
  54. function EditGLTextureImage(ATexImage: TGLTextureImage): Boolean;
  55. var
  56. i: Integer;
  57. editor: TGLTextureImageEditorClass;
  58. begin
  59. if Assigned(vTIEClass) then
  60. begin
  61. i := vTIEClass.IndexOf(Pointer(aTexImage.ClassType));
  62. if i >= 0 then
  63. begin
  64. editor := TGLTextureImageEditorClass(vTIEEditor[i]);
  65. Result := editor.Edit(aTexImage);
  66. Exit;
  67. end;
  68. end;
  69. InformationDlg(aTexImage.ClassName + ': editing not supported.');
  70. Result := False;
  71. end;
  72. procedure RegisterGLTextureImageEditor(ATexImageClass: TGLTextureImageClass;
  73. texImageEditor: TGLTextureImageEditorClass);
  74. begin
  75. if not Assigned(vTIEClass) then
  76. begin
  77. vTIEClass := TList.Create;
  78. vTIEEditor := TList.Create;
  79. end;
  80. vTIEClass.Add(Pointer(aTexImageClass));
  81. vTIEEditor.Add(texImageEditor);
  82. end;
  83. procedure UnRegisterGLTextureImageEditor(TexImageEditor: TGLTextureImageEditorClass);
  84. var
  85. i: Integer;
  86. begin
  87. if Assigned(vTIEClass) then
  88. begin
  89. i := vTIEEditor.IndexOf(texImageEditor);
  90. if i >= 0 then
  91. begin
  92. vTIEClass.Delete(i);
  93. vTIEEditor.Delete(i);
  94. end;
  95. end;
  96. end;
  97. // ------------------
  98. // ------------------ TGLBlankTIE ------------------
  99. // ------------------
  100. class function TGLBlankTIE.Edit(ATexImage: TGLTextureImage): Boolean;
  101. var
  102. p1, p2: Integer;
  103. buf, part: String;
  104. texImage: TGLBlankImage;
  105. begin
  106. texImage := (aTexImage as TGLBlankImage);
  107. if texImage.Depth = 0 then
  108. buf := InputDlg('Blank Image', 'Enter size', Format('%d x %d', [texImage.Width, texImage.Height]))
  109. else
  110. buf := InputDlg('Blank Image', 'Enter size', Format('%d x %d x %d', [texImage.Width, texImage.Height, texImage.Depth]));
  111. p1 := Pos('x', buf);
  112. if p1 > 0 then
  113. begin
  114. texImage.Width := StrToIntDef(Trim(Copy(buf, 1, p1 - 1)), 256);
  115. part := Copy(buf, p1 + 1, MaxInt);
  116. p2 := Pos('x', part);
  117. if p2 > 0 then
  118. begin
  119. texImage.Height := StrToIntDef(Trim(Copy(part, 1, p2 - 1)), 256);
  120. texImage.Depth := StrToIntDef(Trim(Copy(part, p2 + 1, MaxInt)), 1)
  121. end
  122. else
  123. begin
  124. texImage.Height := StrToIntDef(Trim(Copy(buf, p1 + 1, MaxInt)), 256);
  125. texImage.Depth := 0;
  126. end;
  127. Result := True;
  128. end
  129. else
  130. begin
  131. InformationDlg('Invalid size');
  132. Result := False;
  133. end;
  134. end;
  135. // ------------------
  136. // ------------------ TGLPersistentTIE ------------------
  137. // ------------------
  138. class function TGLPersistentTIE.Edit(aTexImage: TGLTextureImage): Boolean;
  139. var
  140. fName: String;
  141. begin
  142. fName := '';
  143. Result := OpenPictureDialog(fName);
  144. if Result then
  145. begin
  146. aTexImage.LoadFromFile(fName);
  147. aTexImage.NotifyChange(aTexImage);
  148. end;
  149. end;
  150. // ------------------
  151. // ------------------ TGLPicFileTIE ------------------
  152. // ------------------
  153. class function TGLPicFileTIE.Edit(aTexImage: TGLTextureImage): Boolean;
  154. var
  155. newName: String;
  156. texImage: TGLPicFileImage;
  157. begin
  158. { TODO : A better TGLPicFileImage.Edit is needed... }
  159. texImage := (aTexImage as TGLPicFileImage);
  160. newName := InputDlg('PicFile Image', 'Enter filename', texImage.PictureFileName);
  161. Result := (texImage.PictureFileName <> newName);
  162. if Result then
  163. texImage.PictureFileName := newName
  164. end;
  165. class function TGLProcTextureNoiseTIE.Edit(aTexImage: TGLTextureImage): Boolean;
  166. var
  167. p: Integer;
  168. buf: String;
  169. begin
  170. with aTexImage as TGLProcTextureNoise do
  171. begin
  172. buf := InputDlg(TGLProcTextureNoise.FriendlyName, 'Enter size', Format('%d x %d', [Width, Height]));
  173. p := Pos('x', buf);
  174. if p > 0 then
  175. begin
  176. Width := StrToIntDef(Trim(Copy(buf, 1, p - 1)), 256);
  177. Height := StrToIntDef(Trim(Copy(buf, p + 1, MaxInt)), 256);
  178. buf := InputDlg(TGLProcTextureNoise.FriendlyName, 'Minimum Cut', IntToStr(MinCut));
  179. MinCut := StrToIntDef(buf, 0);
  180. buf := InputDlg(TGLProcTextureNoise.FriendlyName, 'Noise Sharpness', FloatToStr(NoiseSharpness));
  181. //By PAL, this has to be StrToFloatDef using Windows locale because comes from a Dialog
  182. NoiseSharpness := StrToFloatDef(buf, 0.9);
  183. buf := InputDlg(TGLProcTextureNoise.FriendlyName, 'Random Seed', IntToStr(NoiseRandSeed));
  184. NoiseRandSeed := StrToIntDef(buf, 0);
  185. RandSeed := NoiseRandSeed;
  186. buf := InputDlg(TGLProcTextureNoise.FriendlyName, 'Generate Seamless Texture (0,1)', IntToStr(Ord(Seamless)));
  187. Seamless := (buf <> '0');
  188. Result := True;
  189. Invalidate;
  190. end
  191. else
  192. begin
  193. InformationDlg('Invalid size');
  194. Result := False;
  195. end;
  196. end;
  197. end;
  198. // ------------------------------------------------------------------
  199. initialization
  200. // ------------------------------------------------------------------
  201. RegisterGLTextureImageEditor(TGLBlankImage, TGLBlankTIE);
  202. RegisterGLTextureImageEditor(TGLPersistentImage, TGLPersistentTIE);
  203. RegisterGLTextureImageEditor(TGLPicFileImage, TGLPicFileTIE);
  204. RegisterGLTextureImageEditor(TGLProcTextureNoise, TGLProcTextureNoiseTIE);
  205. finalization
  206. UnRegisterGLTextureImageEditor(TGLBlankTIE);
  207. UnRegisterGLTextureImageEditor(TGLPersistentTIE);
  208. UnRegisterGLTextureImageEditor(TGLPicFileTIE);
  209. UnRegisterGLTextureImageEditor(TGLProcTextureNoiseTIE);
  210. FreeAndNil(vTIEClass);
  211. FreeAndNil(vTIEEditor);
  212. end.