Skip to content

Commit f727013

Browse files
committed
Add new Processing sketch templates and update template handling in Editor
1 parent 81d5127 commit f727013

File tree

3 files changed

+96
-3
lines changed

3 files changed

+96
-3
lines changed

app/src/processing/app/Base.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -586,9 +586,9 @@ public JMenu initDefaultFileMenu() {
586586
item.addActionListener(e -> handleNew());
587587
defaultFileMenu.add(item);
588588

589-
item = Toolkit.newJMenuItem(Language.text("menu.file.open"), 'O');
590-
item.addActionListener(e -> handleOpenPrompt());
591-
defaultFileMenu.add(item);
589+
// item = Toolkit.newJMenuItem(Language.text("menu.file.open"), 'O');
590+
// item.addActionListener(e -> handleOpenPrompt());
591+
// defaultFileMenu.add(item);
592592

593593
item = Toolkit.newJMenuItemShift(Language.text("menu.file.sketchbook"), 'K');
594594
item.addActionListener(e -> showSketchbookFrame());
@@ -598,6 +598,10 @@ public JMenu initDefaultFileMenu() {
598598
item.addActionListener(e -> thinkDifferentExamples());
599599
defaultFileMenu.add(item);
600600

601+
item = Toolkit.newJMenuItem(Language.text("menu.file.templates"), 'T');
602+
//item.addActionListener(e -> handleTemplates());
603+
defaultFileMenu.add(item);
604+
601605
return defaultFileMenu;
602606
}
603607

@@ -1288,6 +1292,8 @@ private void openContribBundle(String path) {
12881292
});
12891293
}
12901294

1295+
// Open templates to start with any work
1296+
12911297

12921298
/**
12931299
* Return true if it's an obvious sketch folder: only .pde files,

app/src/processing/app/ui/Editor.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,13 @@ protected JMenu buildFileMenu(JMenuItem[] exportItems) {
722722
item.addActionListener(e -> handleSaveAs());
723723
fileMenu.add(item);
724724

725+
item = Toolkit.newJMenuItemShift(Language.text("menu.file.templates"), 'T');
726+
//JMenuItem finalItem = item;
727+
item.addActionListener(e -> handleTemplate());
728+
fileMenu.add(item);
729+
730+
731+
725732
if (exportItems != null) {
726733
for (JMenuItem ei : exportItems) {
727734
fileMenu.add(ei);
@@ -761,6 +768,36 @@ protected JMenu buildFileMenu(JMenuItem[] exportItems) {
761768
}
762769
return fileMenu;
763770
}
771+
// Define the handleTemplate method to show different templates
772+
private void handleTemplate() {
773+
// Create a popup menu for templates
774+
JPopupMenu templatesMenu = new JPopupMenu("Templates");
775+
776+
JMenuItem template1 = new JMenuItem("Animation Sketch");
777+
template1.addActionListener(e -> insertTemplateCode(Template.animationSketchCode));
778+
templatesMenu.add(template1);
779+
780+
JMenuItem template2 = new JMenuItem("Interactive Sketch");
781+
template2.addActionListener(e -> insertTemplateCode(Template.interactiveSketchCode));
782+
templatesMenu.add(template2);
783+
784+
JMenuItem template3 = new JMenuItem("Fullscreen Sketch");
785+
template3.addActionListener(e -> insertTemplateCode(Template.fullscreenSketchCode));
786+
templatesMenu.add(template3);
787+
788+
JMenuItem template4 = new JMenuItem("Resizeable Sketch");
789+
template4.addActionListener(e -> insertTemplateCode(Template.resizeableSketchCode));
790+
templatesMenu.add(template4);
791+
792+
// Show the popup menu at the location where the "Templates" menu item was clicked
793+
templatesMenu.show(this, 0, 0);
794+
}
795+
796+
// Add this method to insert template code into the editor
797+
private void insertTemplateCode(String templateCode) {
798+
textarea.setText("");
799+
insertText(templateCode);
800+
}
764801

765802

766803
protected JMenu buildEditMenu() {
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package processing.app.ui;
2+
3+
public class Template {
4+
5+
// New templates
6+
public static final String animationSketchCode =
7+
"void setup() {\n" +
8+
" size(500, 500);\n" +
9+
" // write code that will be called once in this function\n" +
10+
"}\n\n" +
11+
"void draw() {\n" +
12+
" // write code that will be called for every frame here\n" +
13+
"}";
14+
15+
public static final String interactiveSketchCode =
16+
"void setup() {\n" +
17+
" size(500, 500);\n" +
18+
" // write code that will be called once in this function\n" +
19+
"}\n\n" +
20+
"void draw() {\n" +
21+
" // write code that will be called for every frame here\n" +
22+
"}\n\n" +
23+
"void mousePressed() {\n" +
24+
" ellipse(mouseX, mouseY, 20, 20);\n" +
25+
" // write code that will run when you click\n" +
26+
"}";
27+
28+
public static final String fullscreenSketchCode =
29+
"void setup() {\n" +
30+
" fullScreen(); // create a fullscreen canvas\n" +
31+
"}\n\n" +
32+
"void draw() {\n" +
33+
" circle(width / 2, height / 2, height * 0.5);\n" +
34+
"}";
35+
36+
public static final String resizeableSketchCode =
37+
"void setup() {\n" +
38+
" size(500, 500);\n" +
39+
" windowResizable(true);\n" +
40+
" // allow the window to be resized\n" +
41+
"}\n\n" +
42+
"void draw() {\n" +
43+
" circle(width / 2, height / 2, min(width, height) * 0.5);\n" +
44+
" // draw a circle that resizes with the window\n" +
45+
"}\n\n" +
46+
"void windowResized() {\n" +
47+
" println(\"Window resized to: \" + width + \"x\" + height);\n" +
48+
" // this function is called whenever the window is resized\n" +
49+
"}";
50+
}

0 commit comments

Comments
 (0)