Skip to content
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

Save a location of directory on loading/saving ISFs #12

Merged
merged 5 commits into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
39 changes: 39 additions & 0 deletions Headers/AEUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,43 @@ void copyConvertStringLiteralIntoUTF16(const wchar_t* inputString, A_UTF16Char*
#endif
}

PF_Err getStringPersistentData(PF_InData* in_data,
const A_char* sectionKey,
const A_char* valueKey,
const std::string& defaultValue,
std::string* value) {
PF_Err err = PF_Err_NONE;

AEGP_SuiteHandler suites(in_data->pica_basicP);

AEGP_PersistentBlobH blobH;
ERR(suites.PersistentDataSuite4()->AEGP_GetApplicationBlob(AEGP_PersistentType_MACHINE_SPECIFIC, &blobH));

char charStr[1024];
A_u_long charSize = 0;

ERR(suites.PersistentDataSuite4()->AEGP_GetString(blobH, sectionKey, valueKey, defaultValue.c_str(), 1024, charStr,
&charSize));

(*value) = std::string(charStr, charSize);

return err;
}

PF_Err setStringPersistentData(PF_InData* in_data,
const A_char* sectionKey,
const A_char* valueKey,
const std::string& value) {
PF_Err err = PF_Err_NONE;

AEGP_SuiteHandler suites(in_data->pica_basicP);

AEGP_PersistentBlobH blobH;
ERR(suites.PersistentDataSuite4()->AEGP_GetApplicationBlob(AEGP_PersistentType_MACHINE_SPECIFIC, &blobH));

ERR(suites.PersistentDataSuite3()->AEGP_SetString(blobH, sectionKey, valueKey, value.c_str()));

return err;
}

} // namespace AEUtil
10 changes: 10 additions & 0 deletions Headers/AEUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ PF_Err getColorParam(PF_InData* in_data, PF_OutData* out_data, int paramIndex, P
PF_Err getEffectName(AEGP_PluginID aegpId, PF_InData* in_data, std::string* name);
PF_Err setEffectName(AEGP_PluginID aegpId, PF_InData* in_data, const std::string& name);

PF_Err getStringPersistentData(PF_InData* in_data,
const A_char* sectionKey,
const A_char* valueKey,
const std::string& defaultValue,
std::string* value);
PF_Err setStringPersistentData(PF_InData* in_data,
const A_char* sectionKey,
const A_char* valueKey,
const std::string& value);

// Other AE-specific utils
void copyConvertStringLiteralIntoUTF16(const wchar_t* inputString, A_UTF16Char* destination);

Expand Down
5 changes: 5 additions & 0 deletions Headers/MiscUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,8 @@ std::string getBasename(const std::string& path) {
std::filesystem::path p(path);
return p.stem();
}

std::string getDirname(const std::string& path) {
std::filesystem::path p(path);
return p.parent_path().string();
}
2 changes: 2 additions & 0 deletions Headers/MiscUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ int findIndex(std::vector<T> vals, T target) {
void setBitFlag(int flag, bool value, int* target);

std::string getBasename(const std::string& path);

std::string getDirname(const std::string& path);
11 changes: 9 additions & 2 deletions Headers/SystemUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

namespace SystemUtil {

std::string openFileDialog(const std::vector<std::string>& fileTypes) {
std::string openFileDialog(const std::vector<std::string>& fileTypes,
const std::string& directory,
const std::string& title) {
std::string path;

id nsFileTypes = [NSMutableArray new];
Expand All @@ -19,6 +21,9 @@ std::string openFileDialog(const std::vector<std::string>& fileTypes) {
}

NSOpenPanel* panel = [NSOpenPanel openPanel];

[panel setMessage:[NSString stringWithUTF8String:title.c_str()]];
[panel setDirectoryURL:[NSURL fileURLWithPath:[NSString stringWithUTF8String:directory.c_str()]]];
[panel setAllowsMultipleSelection:NO];
[panel setCanChooseDirectories:NO];
[panel setCanChooseFiles:YES];
Expand All @@ -36,12 +41,14 @@ std::string openFileDialog(const std::vector<std::string>& fileTypes) {
return path;
}

std::string saveFileDialog(const std::string& filename) {
std::string saveFileDialog(const std::string& filename, const std::string& directory, const std::string& title) {
std::string path;

NSString* nsFilename = [NSString stringWithCString:filename.c_str() encoding:NSUTF8StringEncoding];

NSSavePanel* panel = [NSSavePanel savePanel];
[panel setMessage:[NSString stringWithUTF8String:title.c_str()]];
[panel setDirectoryURL:[NSURL fileURLWithPath:[NSString stringWithUTF8String:directory.c_str()]]];
[panel setCanCreateDirectories:YES];
[panel setNameFieldStringValue:nsFilename];
[panel setFloatingPanel:YES];
Expand Down
8 changes: 6 additions & 2 deletions Headers/SystemUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@

namespace SystemUtil {

std::string openFileDialog(const std::vector<std::string>& fileTypes);
std::string saveFileDialog(const std::string& filename);
std::string openFileDialog(const std::vector<std::string>& fileTypes,
const std::string& directory = "",
const std::string& title = "");
std::string saveFileDialog(const std::string& filename,
const std::string& directory = "",
const std::string& title = "");

std::string readTextFile(const std::string& path);
bool writeTextFile(const std::string& path, const std::string& text);
Expand Down
6 changes: 6 additions & 0 deletions ISF4AE.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,16 @@ typedef unsigned short PixelType;
#include "OGL.h"
#include "WeakMap.hpp"

#include "Config.h"

// Magic numbers
#define ARB_REFCON (void*)0xDEADBEEFDEADBEEF
#define PI 3.14159265358979323846

#ifdef AE_OS_MAC
#define DEFAULT_ISF_DIRECTORY "/Library/Graphics/ISF"
#endif

// Parameter indices
enum {
Param_Input = 0,
Expand Down
13 changes: 9 additions & 4 deletions ISF4AE_CommandSelector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
#include "AEFX_SuiteHelper.h"
#include "Smart_Utils.h"

#include "Config.h"

#include "AEUtil.h"
#include "SystemUtil.h"

Expand Down Expand Up @@ -581,11 +579,18 @@ static PF_Err UserChangedParam(PF_InData* in_data,
// Load a shader
std::vector<std::string> fileTypes = {"fs", "txt", "frag", "glsl"};

std::string srcPath = SystemUtil::openFileDialog(fileTypes);
std::string isfDirectory = "";
ERR(AEUtil::getStringPersistentData(in_data, CONFIG_MATCH_NAME, "ISF Directory", DEFAULT_ISF_DIRECTORY,
&isfDirectory));

std::string srcPath = SystemUtil::openFileDialog(fileTypes, isfDirectory, "Open ISF File");

if (!srcPath.empty()) {
if (!err && !srcPath.empty()) {
std::string isfCode = SystemUtil::readTextFile(srcPath);

isfDirectory = getDirname(srcPath);
ERR(AEUtil::setStringPersistentData(in_data, CONFIG_MATCH_NAME, "ISF Directory", isfDirectory));

if (!isfCode.empty()) {
params[Param_ISF]->uu.change_flags |= PF_ChangeFlag_CHANGED_VALUE;

Expand Down
9 changes: 8 additions & 1 deletion ISF4AE_UtilFunc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,16 +182,23 @@ PF_Err saveISF(PF_InData* in_data, PF_OutData* out_data) {

auto* isf = reinterpret_cast<ParamArbIsf*>(*paramIsf.u.arb_d.value);

std::string isfDirectory = "";
ERR(AEUtil::getStringPersistentData(in_data, CONFIG_MATCH_NAME, "ISF Directory", DEFAULT_ISF_DIRECTORY,
&isfDirectory));

// Set name of an effect instance as default file name
std::string effectName = isf->name;

// Then confirm a destination path and save it
std::string dstPath = SystemUtil::saveFileDialog(effectName + ".fs");
std::string dstPath = SystemUtil::saveFileDialog(effectName + ".fs", isfDirectory, "Save ISF File");

if (!err && !dstPath.empty()) {
std::string isfCode = isf->desc->scene->getFragCode();

SystemUtil::writeTextFile(dstPath, isfCode);

isfDirectory = getDirname(dstPath);
ERR(AEUtil::setStringPersistentData(in_data, CONFIG_MATCH_NAME, "ISF Directory", isfDirectory));
}

ERR2(PF_CHECKIN_PARAM(in_data, &paramIsf));
Expand Down