-
-
Notifications
You must be signed in to change notification settings - Fork 162
Adds new Processing sketch templates to enhance the user's initial coding experience in the Processing IDE. #1020
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
f727013
db4b6b9
0e5507a
089607d
7276460
152b14c
6ba547a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -586,9 +586,9 @@ public JMenu initDefaultFileMenu() { | |
| item.addActionListener(e -> handleNew()); | ||
| defaultFileMenu.add(item); | ||
|
|
||
| item = Toolkit.newJMenuItem(Language.text("menu.file.open"), 'O'); | ||
| item.addActionListener(e -> handleOpenPrompt()); | ||
| defaultFileMenu.add(item); | ||
| // item = Toolkit.newJMenuItem(Language.text("menu.file.open"), 'O'); | ||
| // item.addActionListener(e -> handleOpenPrompt()); | ||
|
||
| // defaultFileMenu.add(item); | ||
|
|
||
| item = Toolkit.newJMenuItemShift(Language.text("menu.file.sketchbook"), 'K'); | ||
| item.addActionListener(e -> showSketchbookFrame()); | ||
|
|
@@ -598,6 +598,10 @@ public JMenu initDefaultFileMenu() { | |
| item.addActionListener(e -> thinkDifferentExamples()); | ||
| defaultFileMenu.add(item); | ||
|
|
||
| item = Toolkit.newJMenuItem(Language.text("menu.file.templates"), 'T'); | ||
| //item.addActionListener(e -> handleTemplates()); | ||
|
||
| defaultFileMenu.add(item); | ||
|
|
||
| return defaultFileMenu; | ||
| } | ||
|
|
||
|
|
@@ -1095,16 +1099,15 @@ public void modeRemoved(Mode mode) { | |
| /** | ||
| * Create a new untitled document in a new sketch window. | ||
| */ | ||
| public void handleNew() { | ||
| // long t1 = System.currentTimeMillis(); | ||
| public Editor handleNew() { | ||
| try { | ||
| // In 0126, untitled sketches will begin in the temp folder, | ||
| // and then moved to a new location because Save will default to Save As. | ||
| //File sketchbookDir = getSketchbookFolder(); | ||
| File newbieDir = SketchName.nextFolder(untitledFolder); | ||
|
|
||
| // User was told to go outside or other problem happened inside naming. | ||
| if (newbieDir == null) return; | ||
| if (newbieDir == null) return null; | ||
|
|
||
| // Make the directory for the new sketch | ||
| if (!newbieDir.mkdirs()) { | ||
|
|
@@ -1124,16 +1127,16 @@ public void handleNew() { | |
| } | ||
|
|
||
| String path = newbieFile.getAbsolutePath(); | ||
| handleOpenUntitled(path); | ||
| return handleOpenUntitled(path); | ||
|
|
||
| } catch (IOException e) { | ||
| Messages.showTrace("That's new to me", | ||
| "A strange and unexplainable error occurred\n" + | ||
| "while trying to create a new sketch.", e, false); | ||
| "A strange and unexplainable error occurred\n" + | ||
| "while trying to create a new sketch.", e, false); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Prompt for a sketch to open, and open it in a new window. | ||
| */ | ||
|
|
@@ -1288,6 +1291,8 @@ private void openContribBundle(String path) { | |
| }); | ||
| } | ||
|
|
||
| // Open templates to start with any work | ||
|
||
|
|
||
|
|
||
| /** | ||
| * Return true if it's an obvious sketch folder: only .pde files, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,6 +39,7 @@ | |
| import java.util.Timer; | ||
| import java.util.TimerTask; | ||
| import java.util.stream.Collectors; | ||
| import java.nio.file.Files; | ||
|
|
||
| import javax.swing.*; | ||
| import javax.swing.border.EmptyBorder; | ||
|
|
@@ -722,6 +723,12 @@ protected JMenu buildFileMenu(JMenuItem[] exportItems) { | |
| item.addActionListener(e -> handleSaveAs()); | ||
| fileMenu.add(item); | ||
|
|
||
| JMenu templatesMenu = new JMenu(Language.text("menu.file.templates")); | ||
| System.out.println("templatesMenu"); | ||
| loadTemplates(templatesMenu); | ||
| fileMenu.add(templatesMenu); | ||
|
|
||
|
|
||
| if (exportItems != null) { | ||
| for (JMenuItem ei : exportItems) { | ||
| fileMenu.add(ei); | ||
|
|
@@ -761,6 +768,117 @@ protected JMenu buildFileMenu(JMenuItem[] exportItems) { | |
| } | ||
| return fileMenu; | ||
| } | ||
| // Load templates from both the repository and the user's Sketchbook folder | ||
| private void loadTemplates(JMenu templatesMenu) { | ||
| List<File> allTemplates = new ArrayList<>(); | ||
|
|
||
| // Load predefined templates from the repository | ||
| File repoTemplatesDir = Platform.getContentFile("lib/templates"); | ||
| System.out.println("repoTemplatesDir: " + repoTemplatesDir); | ||
| System.out.println("Loading templates from repository directory: " + repoTemplatesDir.getAbsolutePath()); | ||
| addTemplatesFromDirectory(repoTemplatesDir, allTemplates); | ||
|
|
||
| // Load user-defined templates from the Sketchbook folder | ||
| File userTemplatesDir = new File(Base.getSketchbookFolder(), "templates"); | ||
|
|
||
| System.out.println("Loading templates from user sketchbook directory: " + userTemplatesDir.getAbsolutePath()); | ||
|
||
| addTemplatesFromDirectory(userTemplatesDir, allTemplates); | ||
|
|
||
| // Print all templates to the console | ||
| System.out.println("All loaded templates:"); | ||
| System.out.println(allTemplates.size()); | ||
| for (File template : allTemplates) { | ||
| System.out.println("Template: " + template.getAbsolutePath()); | ||
| } | ||
|
|
||
| // Add all templates to the menu | ||
| addTemplatesToMenu(allTemplates, templatesMenu); | ||
| } | ||
|
|
||
| private void addTemplatesFromDirectory(File directory, List<File> allTemplates) { | ||
| if (directory.exists() && directory.isDirectory()) { | ||
| File[] templateFiles = directory.listFiles(); | ||
| if (templateFiles != null) { | ||
| for (File templateFile : templateFiles) { | ||
| System.out.println("Found template file: " + templateFile.getAbsolutePath()); | ||
| allTemplates.add(templateFile); | ||
| } | ||
| } else { | ||
| System.out.println("No template files found in directory: " + directory.getAbsolutePath()); | ||
| } | ||
| } else { | ||
| System.out.println("Directory does not exist or is not a directory: " + directory.getAbsolutePath()); | ||
| } | ||
| } | ||
|
|
||
| // Add templates to the menu | ||
| // Add templates to the menu | ||
| private void addTemplatesToMenu(List<File> templates, JMenu templatesMenu) { | ||
| templatesMenu.removeAll(); // Clear existing menu items | ||
| for (File templateFile : templates) { | ||
| String templateName; | ||
| if (templateFile.getName().toLowerCase().endsWith(".pde")) { | ||
| templateName = templateFile.getName().replace(".pde", ""); | ||
| } else { | ||
| templateName = templateFile.getName(); | ||
| } | ||
| JMenuItem templateItem = new JMenuItem(templateName); | ||
| templateItem.addActionListener(e -> { | ||
| try { | ||
| String templateCode; | ||
| if (templateFile.getName().toLowerCase().endsWith(".pde")) { | ||
| templateCode = new String(Files.readAllBytes(templateFile.toPath())); | ||
| System.out.println("Template code read from file: " + templateCode); | ||
| } else { | ||
| templateCode = ""; | ||
| // System.out.println("Addtemplatestomenu--suserdefned"+templateFile);// Or any other action needed for non-.pde files | ||
| System.out.println("Non-.pde file selected: " + templateFile.getAbsolutePath()); | ||
| } | ||
|
|
||
| Editor newEditor = base.handleNew(); | ||
| if (newEditor != null) { | ||
| System.out.println("New editor created. Inserting template code."); | ||
|
||
| newEditor.insertText(templateCode); | ||
| } else { | ||
| System.err.println("Failed to create new editor."); | ||
| } | ||
| } catch (IOException ex) { | ||
| ex.printStackTrace(); | ||
| } | ||
| }); | ||
| templatesMenu.add(templateItem); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| // Method to save user-defined templates in the sketchbook/templates directory | ||
| // public void saveUserTemplate(String templateName, String templateCode) { | ||
| // File userTemplatesDir = new File(Base.getSketchbookFolder(), "templates"); | ||
| // if (!userTemplatesDir.exists()) { | ||
| // userTemplatesDir.mkdirs(); | ||
| // } | ||
| // | ||
| // File templateFile = new File(userTemplatesDir, templateName + ".pde"); | ||
| // try { | ||
| // Files.write(templateFile.toPath(), templateCode.getBytes()); | ||
| // System.out.println("User template saved: " + templateFile.getAbsolutePath()); | ||
| // | ||
| // // Reload templates after saving a new one | ||
| // JMenu templatesMenu = new JMenu(Language.text("menu.file.templates")); | ||
| // loadTemplates(templatesMenu); | ||
| // | ||
| // // Update the File menu with the new templates menu | ||
| // JMenu fileMenu = buildFileMenu(null); | ||
| // fileMenu.add(templatesMenu); | ||
| // } catch (IOException e) { | ||
| // e.printStackTrace(); | ||
| // } | ||
| // } | ||
| // Add this method to insert template code into the editor | ||
| private void insertTemplateCode(String templateCode) { | ||
| //textarea.setText(""); | ||
| insertText(templateCode); | ||
| } | ||
|
|
||
|
|
||
| protected JMenu buildEditMenu() { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package processing.app.ui; | ||
|
|
||
| public class Template { | ||
|
||
|
|
||
| // New templates | ||
| public static final String animationSketchCode = | ||
| "void setup() {\n" + | ||
| " size(500, 500);\n" + | ||
| " // write code that will be called once in this function\n" + | ||
| "}\n\n" + | ||
| "void draw() {\n" + | ||
| " // write code that will be called for every frame here\n" + | ||
| "}"; | ||
|
|
||
| public static final String interactiveSketchCode = | ||
| "void setup() {\n" + | ||
| " size(500, 500);\n" + | ||
| " // write code that will be called once in this function\n" + | ||
| "}\n\n" + | ||
| "void draw() {\n" + | ||
| " // write code that will be called for every frame here\n" + | ||
| "}\n\n" + | ||
| "void mousePressed() {\n" + | ||
| " ellipse(mouseX, mouseY, 20, 20);\n" + | ||
| " // write code that will run when you click\n" + | ||
| "}"; | ||
|
|
||
| public static final String fullscreenSketchCode = | ||
| "void setup() {\n" + | ||
| " fullScreen(); // create a fullscreen canvas\n" + | ||
| "}\n\n" + | ||
| "void draw() {\n" + | ||
| " circle(width / 2, height / 2, height * 0.5);\n" + | ||
| "}"; | ||
|
|
||
| public static final String resizeableSketchCode = | ||
| "void setup() {\n" + | ||
| " size(500, 500);\n" + | ||
| " windowResizable(true);\n" + | ||
| " // allow the window to be resized\n" + | ||
| "}\n\n" + | ||
| "void draw() {\n" + | ||
| " circle(width / 2, height / 2, min(width, height) * 0.5);\n" + | ||
| " // draw a circle that resizes with the window\n" + | ||
| "}\n\n" + | ||
| "void windowResized() {\n" + | ||
| " println(\"Window resized to: \" + width + \"x\" + height);\n" + | ||
| " // this function is called whenever the window is resized\n" + | ||
| "}"; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ menu.file.recent = Open Recent | |
| menu.file.sketchbook = Sketchbook... | ||
| menu.file.sketchbook.empty = Empty Sketchbook | ||
| menu.file.examples = Examples... | ||
| menu.file.templates=Templates | ||
|
||
| menu.file.close = Close | ||
| menu.file.save = Save | ||
| menu.file.save_as = Save As... | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| // Basic Template | ||
| void setup() { | ||
|
||
| size(800, 600); | ||
| background(255); | ||
| } | ||
|
|
||
| void draw() { | ||
| // Drawing code here | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| // Shape Drawing Template | ||
| void setup() { | ||
| size(800, 600); | ||
| background(255); | ||
| } | ||
|
|
||
| void draw() { | ||
| fill(150, 0, 0); | ||
| rect(100, 100, 200, 200); | ||
|
|
||
| fill(0, 150, 0); | ||
| ellipse(400, 300, 150, 150); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| // Animation Template | ||
| float x = 0; | ||
| float speed = 2; | ||
|
|
||
| void setup() { | ||
| size(800, 600); | ||
| background(255); | ||
| } | ||
|
|
||
| void draw() { | ||
| background(255); | ||
| ellipse(x, height/2, 50, 50); | ||
| x += speed; | ||
| if (x > width || x < 0) { | ||
| speed *= -1; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // Interactive Template | ||
| void setup() { | ||
| size(800, 600); | ||
| background(255); | ||
| } | ||
|
|
||
| void draw() { | ||
| background(255); | ||
| fill(0, 0, 150); | ||
| ellipse(mouseX, mouseY, 50, 50); | ||
| } | ||
|
|
||
| void mousePressed() { | ||
| background(0, 255, 0); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just realised that the file->open menu item certainly should not be deleted here