Skip to content

Commit

Permalink
pdlua improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
timothyschoen committed Dec 22, 2023
1 parent ac97660 commit 0dc62fd
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 34 deletions.
4 changes: 1 addition & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
url = https://github.com/Wasted-Audio/heavylib.git
[submodule "Libraries/pd-lua"]
path = Libraries/pd-lua
url = https://github.com/agraef/pd-lua.git
url = https://github.com/timothyschoen/pd-lua
[submodule "Libraries/clap-juce-extensions"]
path = Libraries/clap-juce-extensions
url = https://github.com/free-audio/clap-juce-extensions.git
Expand Down Expand Up @@ -42,5 +42,3 @@
[submodule "Libraries/melatonin_blur"]
path = Libraries/melatonin_blur
url = https://github.com/sudara/melatonin_blur.git
[submodule "./Libraries/pd-lua"]
url = https://github.com/timothyschoen/pd-lua
2 changes: 1 addition & 1 deletion Libraries/pd-lua
Submodule pd-lua updated 4 files
+24 −0 pd.lua
+111 −53 pdlua.c
+28 −7 pdlua.h
+144 −89 pdlua_gfx.h
24 changes: 8 additions & 16 deletions Source/Objects/LuaObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ class LuaObject : public ObjectBase, public Timer {

cnv->zoomScale.addListener(this);
startTimerHz(60); // Check for paint messages at 60hz (but we only really repaint when needed)


}

~LuaObject()
Expand Down Expand Up @@ -348,7 +346,7 @@ class LuaObject : public ObjectBase, public Timer {
text.setJustification(Justification::topLeft);

TextLayout layout;
layout.createLayout(text, getWidth());
layout.createLayout(text, w);
layout.draw(*graphics, {x, y, w, layout.getHeight()});
}
break;
Expand Down Expand Up @@ -481,20 +479,14 @@ class LuaObject : public ObjectBase, public Timer {
&saveDialog, textEditor.get(), "", [this, newText, fileToOpen](int result) mutable {
if (result == 2) {
fileToOpen.replaceWithText(newText);
if(auto pdlua = ptr.get<t_pdlua>())
if(auto pdlua = ptr.get<t_pd>())
{
// TODO: what if there's no inlet?
if(pdlua->in) {
// Reload the lua script
pd_typedmess((t_pd*)pdlua->in, pd->generateSymbol("_reload"), 0, nullptr);

// Recreate this object
auto patch = cnv->patch.getPointer().get();
int size;
char const* text = pd::Interface::copy(patch, &size, { pdlua.cast<t_gobj>() });
pd::Interface::removeObjects(patch, { pdlua.cast<t_gobj>() });
pd::Interface::paste(patch, text);
}
// Reload the lua script
pd_typedmess(pdlua.get(), pd->generateSymbol("_reload"), 0, nullptr);

// Recreate this object
auto* patch = cnv->patch.getPointer().get();
pd::Interface::recreateTextObject(patch, pdlua.cast<t_gobj>());
}
textEditor.reset(nullptr);
cnv->performSynchronise();
Expand Down
9 changes: 7 additions & 2 deletions Source/Objects/ObjectBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -469,9 +469,14 @@ ObjectBase* ObjectBase::createGui(pd::WeakReference ptr, Object* parent)
if (auto checked = ptr.get<t_gobj>()) {
auto const name = hash(pd::Interface::getObjectClassName(checked.cast<t_pd>()));

if(parent->cnv->pd->isLuaGui(name))
if(parent->cnv->pd->isLuaClass(name))
{
return new LuaObject(ptr, parent);
if (checked.cast<t_pdlua>()->has_gui) {
return new LuaObject(ptr, parent);
}
else {
return new LuaTextObject(ptr, parent);
}
}
// check if object is a patcher object, or something else
if (!pd::Interface::checkObject(checked.get()) && name != hash("scalar")) {
Expand Down
10 changes: 5 additions & 5 deletions Source/Pd/Instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ void Instance::initialisePd(String& pdlua_version)
// Class prefix doesn't seem to work for pdlua
char vers[1000];
*vers = 0;
pd::Setup::initialisePdLua(extra.getFullPathName().getCharPointer(), vers, 1000, &registerLuaGui);
pd::Setup::initialisePdLua(extra.getFullPathName().getCharPointer(), vers, 1000, &registerLuaClass);
if (*vers)
pdlua_version = vers;

Expand Down Expand Up @@ -786,14 +786,14 @@ void Instance::clearObjectImplementationsForPatch(pd::Patch* p)
}
}

void Instance::registerLuaGui(t_object* object)
void Instance::registerLuaClass(const char* className)
{
luaGuis.insert(hash(object->te_g.g_pd->c_name->s_name));
luaClasses.insert(hash(className));
}

bool Instance::isLuaGui(hash32 objectNameHash)
bool Instance::isLuaClass(hash32 objectNameHash)
{
return luaGuis.contains(objectNameHash);
return luaClasses.contains(objectNameHash);
}

} // namespace pd
6 changes: 3 additions & 3 deletions Source/Pd/Instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ class Instance {
void unregisterWeakReference(void* ptr, pd_weak_reference const* ref);
void clearWeakReferences(void* ptr);

static void registerLuaGui(t_object* object);
bool isLuaGui(hash32 objectNameHash);
static void registerLuaClass(const char* object);
bool isLuaClass(hash32 objectNameHash);

virtual void receiveDSPState(bool dsp) { }

Expand Down Expand Up @@ -323,7 +323,7 @@ class Instance {

std::unique_ptr<FileChooser> openChooser;
std::atomic<bool> consoleMute;
static inline std::set<hash32> luaGuis = std::set<hash32>(); // Keep track of class names that correspond to pdlua gui objects
static inline std::set<hash32> luaClasses = std::set<hash32>(); // Keep track of class names that correspond to pdlua objects

protected:
struct internal;
Expand Down
13 changes: 13 additions & 0 deletions Source/Pd/Interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,19 @@ struct Interface {

return new_object;
}

// Can recreate any object of type t_text
static void recreateTextObject(t_canvas* cnv, t_gobj* obj)
{
if(auto* textObject = checkObject(obj))
{
char* text = nullptr;
int len = 0;

getObjectText(textObject, &text, &len);
renameObject(cnv, obj, text, len);
}
}

static void renameObject(t_canvas* cnv, t_gobj* obj, char const* buf, size_t bufsize)
{
Expand Down
6 changes: 3 additions & 3 deletions Source/Pd/Setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ void fm_tilde_setup();
#endif
void knob_setup();

void pdlua_setup(char const* datadir, char* vers, int vers_len, void(*register_gui_callback)(t_object*));
void pdlua_setup(char const* datadir, char* vers, int vers_len, void(*register_class_callback)(const char*));
}

namespace pd {
Expand Down Expand Up @@ -757,9 +757,9 @@ void* Setup::createReceiver(void* ptr, char const* s,
return x;
}

void Setup::initialisePdLua(char const* datadir, char* vers, int vers_len, void(*register_gui_callback)(t_object*))
void Setup::initialisePdLua(char const* datadir, char* vers, int vers_len, void(*register_class_callback)(const char*))
{
pdlua_setup(datadir, vers, vers_len, register_gui_callback);
pdlua_setup(datadir, vers, vers_len, register_class_callback);
}

void* Setup::createPrintHook(void* ptr, t_plugdata_printhook hook_print)
Expand Down
2 changes: 1 addition & 1 deletion Source/Pd/Setup.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct Setup {

void parseArguments(char const** args, size_t argc, t_namelist** sys_openlist, t_namelist** sys_messagelist);

static void initialisePdLua(char const* datadir, char* vers, int vers_len, void(*register_gui_callback)(t_object*));
static void initialisePdLua(char const* datadir, char* vers, int vers_len, void(*register_class_callback)(const char*));
static void initialiseELSE();
static void initialiseCyclone();

Expand Down

0 comments on commit 0dc62fd

Please sign in to comment.