Debian Codegraph @

tools/castle-editor/code/formnewproject.pas

raw
1 {
2 Copyright 2018-2024 Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14 }
15
16 { New project form (@link(TNewProjectForm)). }
17 unit FormNewProject;
18
19 {$mode objfpc}{$H+}
20
21 interface
22
23 uses
24 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
25 Buttons, ButtonPanel, StdCtrls, EditBtn, LCLType;
26
27 type
28 { Determine new project settings. }
29 TNewProjectForm = class(TForm)
30 ButtonPanel1: TButtonPanel;
31 ButtonTemplate2d: TSpeedButton;
32 EditLocation: TDirectoryEdit;
33 EditViewName: TEdit;
34 EditProjectName: TEdit;
35 EditProjectCaption: TEdit;
36 GroupProjectTemplate: TGroupBox;
37 LabelViewName: TLabel;
38 LabelProjectLocation: TLabel;
39 LabelProjectCaption: TLabel;
40 LabelTitle: TLabel;
41 LabelProjectName: TLabel;
42 ButtonTemplateEmpty: TSpeedButton;
43 ButtonTemplate3dModelViewer: TSpeedButton;
44 ButtonTemplate3dFps: TSpeedButton;
45 procedure EditProjectNameUTF8KeyPress(Sender: TObject;
46 var UTF8Key: TUTF8Char);
47 procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);
48 procedure FormShow(Sender: TObject);
49 procedure ButtonTemplateClick(Sender: TObject);
50 private
51 procedure AdjustViewNameUi;
52 public
53
54 end;
55
56 var
57 NewProjectForm: TNewProjectForm;
58
59 implementation
60
61 {$R *.lfm}
62
63 uses {$ifdef MSWINDOWS} WinDirs, {$endif}
64 LazFileUtils,
65 CastleUriUtils, CastleConfig, CastleUtils, CastleStringUtils,
66 EditorUtils;
67
68 const
69 AlphaNum = ['a'..'z', 'A'..'Z', '0'..'9'];
70 ValidProjectNameChars = AlphaNum + ['_', '-'];
71 InvalidProjectNameChars = AllChars - ValidProjectNameChars;
72
73 procedure TNewProjectForm.FormShow(Sender: TObject);
74
75 function DefaultProjectsParentDir: String;
76 begin
77 Result :=
78 {$ifdef MSWINDOWS}
79 // get "Documents" dir
80 GetWindowsSpecialDir(CSIDL_PERSONAL);
81 {$else}
82 GetUserDir;
83 {$endif}
84 end;
85
86 { We used to base form height on whether EditViewName is available,
87 but this
88 - causes ugly jump in window on GTK2 when switching templates
89 - for user it is not obvious why EditViewName appears/disappears.
90 Now we keep form height constant during operation,
91 and when EditViewName is not available - we explain it. }
92 procedure AdjustFormSize;
93 const
94 ButtonsMargin = 8;
95 begin
96 ClientHeight := EditViewName.Top + EditViewName.Height + ButtonsMargin + ButtonPanel1.Height;
97 end;
98
99 var
100 DefaultNewProjectDir, NewProjectDir: String;
101 begin
102 { Initialize everything to default values }
103
104 ButtonTemplateEmpty.Down := true;
105
106 DefaultNewProjectDir := InclPathDelim(DefaultProjectsParentDir) +
107 'Castle Game Engine Projects';
108 NewProjectDir := UserConfig.GetValue('new_project/default_dir', DefaultNewProjectDir);
109 EditLocation.Directory := NewProjectDir;
110
111 EditProjectName.Text := 'my-new-project';
112 EditProjectCaption.Text := 'My New Project';
113 EditViewName.Text := 'Main';
114
115 AdjustViewNameUi;
116 AdjustFormSize;
117 end;
118
119 procedure TNewProjectForm.AdjustViewNameUi;
120 var
121 ViewNameConfigurable: Boolean;
122 begin
123 ViewNameConfigurable := ButtonTemplateEmpty.Down or ButtonTemplate3dModelViewer.Down;
124 LabelViewName.Enabled := ViewNameConfigurable;
125 EditViewName.Enabled := ViewNameConfigurable;
126 if ViewNameConfigurable then
127 LabelViewName.Caption := 'Main View Name (determines name of the initial design and Pascal unit)'
128 else
129 LabelViewName.Caption := 'Main View Name (not customizable, as this template has multiple views)';
130 end;
131
132 procedure TNewProjectForm.ButtonTemplateClick(Sender: TObject);
133 begin
134 (Sender as TSpeedButton).Down := true;
135 AdjustViewNameUi;
136 end;
137
138 procedure TNewProjectForm.FormCloseQuery(Sender: TObject; var CanClose: boolean);
139 var
140 ProjectDir: String;
141 InvalidIndex: Integer;
142 ProjectName, ProjectLocation: TCaption;
143 begin
144 if ModalResult = mrOK then
145 begin
146 ProjectName := EditProjectName.Text;
147 ProjectLocation := EditLocation.Directory;
148
149 if ProjectName = '' then
150 begin
151 ErrorBox('Project name cannot be empty.');
152 CanClose := false;
153 Exit;
154 end;
155
156 InvalidIndex := CharsPos(InvalidProjectNameChars, ProjectName);
157 if InvalidIndex <> 0 then
158 begin
159 ErrorBox(Format('Project name contains invalid character "%s".' + NL +
160 NL +
161 'The internal project name is used with various tools, in various contexts, and thus it is limited to alphanumeric characters plus underscore ("_") and hyphen ("-").' + NL +
162 NL +
163 'Note that this is only an internal project name. The user-visible "Project Caption" has no such limitations.',
164 [SReadableForm(ProjectName[InvalidIndex])]));
165 CanClose := false;
166 Exit;
167 end;
168
169 if ProjectLocation = '' then
170 begin
171 ErrorBox('No Project Location chosen.');
172 CanClose := false;
173 Exit;
174 end;
175
176 ProjectDir := InclPathDelim(ProjectLocation) + ProjectName;
177 if DirectoryExists(ProjectDir) then
178 begin
179 ErrorBox(Format('Directory "%s" already exists, cannot create a project there. Please pick a project name that does not correspond to an already-existing directory.',
180 [ProjectDir]));
181 CanClose := false;
182 Exit;
183 end;
184
185 if EditViewName.Visible and not IsValidIdent(EditViewName.Text) then
186 begin
187 ErrorBox(Format('View name "%s" is not a valid Pascal identifier',
188 [EditViewName.Text]));
189 CanClose := false;
190 Exit;
191 end;
192
193 UserConfig.SetValue('new_project/default_dir', ProjectLocation);
194 end;
195 end;
196
197 procedure TNewProjectForm.EditProjectNameUTF8KeyPress(Sender: TObject;
198 var UTF8Key: TUTF8Char);
199 const
200 { Although these chars are not allowed in project name,
201 but they are allowed by this routine, as they allow to edit the text
202 (e.g. delete by backspace, cut/copy/paste by Ctrl+X/C/V). }
203 ControlChars = [CtrlA .. CtrlZ] +
204 { actually this is already in CtrlA .. CtrlZ, but seemed more obvious to list it explicitly. }
205 [CharBackspace];
206 begin
207 if (Length(UTF8Key) <> 1) or
208 ( (UTF8Key[1] in InvalidProjectNameChars) and
209 (not (UTF8Key[1] in ControlChars))
210 ) then
211 UTF8Key := '';
212 end;
213
214 end.