-
Notifications
You must be signed in to change notification settings - Fork 3
/
files.pde
111 lines (93 loc) · 3.48 KB
/
files.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// =============================================================================
// Globals, logic, and event handlers related to loading/saving image files
// =============================================================================
// Globals =====================================================================
// Base image file name, used for default save name
String imgFile;
// Constants ===================================================================
// File extension of the output image
String outputImgExt = ".png";
// Helper Methods ==============================================================
// Loading ---------------------------------------------------------------------
/**
* Returns the name of a file with the extension removed
*/
String getBaseFileName(File f) {
String name = f.getName();
// Return name stripping last . followed by 1 or more chars
return name.replaceFirst("[.][^.]+$", "");
}
/**
* Callback function for selectInput() when loading a file
*/
void imageFileSelected(File selection) {
if (selection != null) {
println("Loading...");
loadImageFile(selection.getAbsolutePath(), getBaseFileName(selection));
println("Image loaded.");
println("");
// Reset UI and configs
resetShift();
}
}
/**
* Load an image file. Sets global variables and updates window size
* accordingly.
* @param path Full path to the image file
* @param name Name of file without extension. Used for default filename when
* saving
*/
void loadImageFile(String path, String name) {
// Load image objects
imgManager.loadImageFile(path);
// Update window size
windowManager.updateWindowDimensions(imgManager.sourceImg);
// TODO UPDATE CONTROLS WINDOW?
// Update imgFile (for default output name)
imgFile = name;
// Reset steps string
stepManager.resetSteps();
// Update managers
xShiftManager.setImgDimension(imgManager.imgWidth);
yShiftManager.setImgDimension(imgManager.imgHeight);
// Redraw preview
windowManager.previewImgUpdated();
}
// Saving ----------------------------------------------------------------------
/**
* Returns an output file path based on source image name and sketch steps.
* @return File name with the image file and sketch steps. Parent directory is
* not specified, so selectOutput() should default to last opened directory
* (depending on OS)
*/
String defaultOutputFilename() {
// Append steps to file name
String filename = imgFile + stepManager.stepsToString();
// Max filename of 255 characters
filename = truncateString(filename, 255 - outputImgExt.length());
return filename + outputImgExt;
}
/**
* Callback function for selectOutput() when saving result
*/
void outFileSelected(File selection) {
if (selection != null) {
println("Saving...");
String outputFile = selection.getAbsolutePath();
// Save previewImg so preview matches output file
imgManager.savePreviewImg(outputFile);
// Print output
println("Result saved:");
println(INDENT + outputFile);
println("");
}
}
// Event Handlers ==============================================================
// Load Button -----------------------------------------------------------------
public void loadBtn_click(GButton source, GEvent event) {
selectInput("Load image:", "imageFileSelected");
}
// Save Button -----------------------------------------------------------------
public void saveBtn_click(GButton source, GEvent event) {
selectOutput("Save as:", "outFileSelected", new File(defaultOutputFilename()));
}