Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions app/src/processing/app/Base.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Copy link
Copy Markdown
Member

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

defaultFileMenu.add(item);
// item = Toolkit.newJMenuItem(Language.text("menu.file.open"), 'O');
// item.addActionListener(e -> handleOpenPrompt());
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please clean up any comments and unused code left in your Pull Request

// defaultFileMenu.add(item);

item = Toolkit.newJMenuItemShift(Language.text("menu.file.sketchbook"), 'K');
item.addActionListener(e -> showSketchbookFrame());
Expand All @@ -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());
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more comment

defaultFileMenu.add(item);

return defaultFileMenu;
}

Expand Down Expand Up @@ -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()) {
Expand All @@ -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.
*/
Expand Down Expand Up @@ -1288,6 +1291,8 @@ private void openContribBundle(String path) {
});
}

// Open templates to start with any work
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another



/**
* Return true if it's an obvious sketch folder: only .pde files,
Expand Down
118 changes: 118 additions & 0 deletions app/src/processing/app/ui/Editor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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());
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please replace all occurrences of System.out.println with Messages.Log, this will ensure that only people running the PDE in DEBUG mode will see this message

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.");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please only print something when something is wrong. This will just create noise in the log

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() {
Expand Down
50 changes: 50 additions & 0 deletions app/src/processing/app/ui/Template.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package processing.app.ui;

public class Template {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer used correct?


// 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" +
"}";
}
1 change: 1 addition & 0 deletions build/shared/lib/languages/PDE.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this should be menu.file.templates=New from Template

cc @Stefterv

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay sir

menu.file.close = Close
menu.file.save = Save
menu.file.save_as = Save As...
Expand Down
9 changes: 9 additions & 0 deletions build/shared/lib/templates/Template1.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Basic Template
void setup() {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and @SableRaf named these in his original suggestion, so please change the file names to those of something else more appropriate

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure

size(800, 600);
background(255);
}

void draw() {
// Drawing code here
}
13 changes: 13 additions & 0 deletions build/shared/lib/templates/Template2.pde
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);
}
17 changes: 17 additions & 0 deletions build/shared/lib/templates/Template3.pde
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;
}
}
15 changes: 15 additions & 0 deletions build/shared/lib/templates/Template4.pde
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);
}