GLS.TextureImageEditors.pas 6.8 KB

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