diff options
Diffstat (limited to 'src/livecode/context.cpp')
| -rw-r--r-- | src/livecode/context.cpp | 116 |
1 files changed, 82 insertions, 34 deletions
diff --git a/src/livecode/context.cpp b/src/livecode/context.cpp index a4b59e9b0..a22191400 100644 --- a/src/livecode/context.cpp +++ b/src/livecode/context.cpp @@ -9,7 +9,7 @@ * Released under GNU GPL v2+, read the file 'COPYING' for more information. */ -#include <giomm/file.h> +#include <memory> #include "svg/stringstream.h" #include "svg/svg-color.h" @@ -23,12 +23,56 @@ namespace Inkscape { namespace Livecode { +Script::Script(JanetTable *env, std::string const &path) + : env(env) + , function(nullptr) + , file(Gio::File::create_for_path(path)) + , monitor(file->monitor_file()) +{ + monitor->signal_changed().connect(sigc::mem_fun(this, &Script::file_changed)); + reload(); +} + +void Script::frame() { + if (function) { + Janet result; + janet_pcall(function, 0, NULL, &result, NULL); + } +} + +void Script::commit() { +} + +void Script::reload() { + try { + uint8_t *data; + size_t length; + file->load_contents((char *&)data, (gsize &)length); + + Janet result; + janet_dobytes(env, data, length, file->get_path().c_str(), &result); + if (janet_checktype(result, JANET_FUNCTION)) { + function = janet_unwrap_function(result); + } else { + g_message("Janet script didn't return a function"); + } + } catch (...) { + g_message("error loading file"); + } +} + +void Script::file_changed(const Glib::RefPtr<Gio::File>& file, + const Glib::RefPtr<Gio::File>& other_file, + Gio::FileMonitorEvent event) +{ + reload(); +} + Context::Context(SPDesktop *desktop) : desktop(desktop) + , _mouse(*this) , doc_root(nullptr) , ui_root(nullptr) - , _mouse(*this) - , script_function(nullptr) { janet_init(); @@ -122,8 +166,8 @@ Inkscape::XML::Node *Context::make_arrow(Geom::Point const &from, Geom::Point co Geom::Point back = from - to; back.normalize(); - Geom::Point const cross_a = back.cw() + back * 2.0; - Geom::Point const cross_b = back.cw() + back * 2.0; + Geom::Point const cross_a = back.cw() * 5.0 + back * 10.0; + Geom::Point const cross_b = back.ccw() * 5.0 + back * 10.0; gchar* d = g_strdup_printf("M %f,%f %f,%f l %f,%f M %f,%f l %f,%f", from.x(), from.y(), @@ -134,14 +178,12 @@ Inkscape::XML::Node *Context::make_arrow(Geom::Point const &from, Geom::Point co repr->setAttribute("d", d); g_free(d); - Glib::ustring css_str; if (css) { + Glib::ustring css_str; sp_repr_css_write_string(css, css_str); sp_repr_css_attr_unref(css); - } else { - css_str = "stroke: #000000;"; + repr->setAttribute("style", css_str.c_str()); } - repr->setAttribute("style", css_str.c_str()); return repr; } @@ -200,7 +242,12 @@ bool Context::input_line(Glib::ustring const &id, Geom::Point *p1, Geom::Point * if (input_point(id + "_p2", p2)) { change = true; } - draw_doc("", make_line(*p1, *p2)); + + SPCSSAttr *css = sp_repr_css_attr_new(); + sp_repr_css_set_property(css, "stroke-width", "1"); + sp_repr_css_set_property(css, "stroke", "#000000"); + sp_repr_css_set_property(css, "fill", "none"); + draw_doc("", make_line(*p1, *p2, css)); return change; } @@ -212,7 +259,12 @@ bool Context::input_arrow(Glib::ustring const &id, Geom::Point *from, Geom::Poin if (input_point(id + "_to", to)) { change = true; } - draw_doc("", make_line(*from, *to)); + + SPCSSAttr *css = sp_repr_css_attr_new(); + sp_repr_css_set_property(css, "stroke-width", "1"); + sp_repr_css_set_property(css, "stroke", "#000000"); + sp_repr_css_set_property(css, "fill", "none"); + draw_doc("", make_arrow(*from, *to, css)); return change; } @@ -243,6 +295,16 @@ void Context::push_event(GdkEvent *event) { _mouse.push_event(event); } +void Context::frame() { + setup_frame(); + + if (_script) { + _script->frame(); + } + + finish_frame(); +} + void Context::setup_frame() { hot = ""; @@ -267,27 +329,6 @@ void Context::setup_frame() { ui_root = SP_ITEM(desktop->currentLayer()->appendChildRepr(rui)); ui_root->doWriteTransform(ui2doc(), nullptr, true); Inkscape::GC::release(rui); - - if (script_file) { - try { - char *data; - gsize length; - script_file->load_contents(data, length); - - Janet result; - janet_dobytes(env, (uint8_t *)data, length, script_file->get_path().c_str(), &result); - if (janet_checktype(result, JANET_FUNCTION)) { - script_function = janet_unwrap_function(result); - } - } catch (...) { - g_message("error loading file"); - } - } - - if (script_function) { - Janet result; - janet_pcall(script_function, 0, NULL, &result, NULL); - } } void Context::finish_frame() { @@ -297,8 +338,15 @@ void Context::finish_frame() { } void Context::load_script(Glib::ustring const &path) { - g_message("loading file %s", path.c_str()); - script_file = Gio::File::create_for_path(path); + g_message("loading script %s", path.c_str()); + _script = nullptr; + + try { + _script.reset(new Script(env, path)); + g_message("loaded."); + } catch (...) { + g_message("error creating script"); + } } void Context::draw_doc(Glib::ustring const &id, Inkscape::XML::Node *repr) { |
