diff options
Diffstat (limited to '')
| -rw-r--r-- | src/livecode/context.cpp (renamed from src/livecode/api.cpp) | 74 |
1 files changed, 56 insertions, 18 deletions
diff --git a/src/livecode/api.cpp b/src/livecode/context.cpp index 4617968c2..a4b59e9b0 100644 --- a/src/livecode/api.cpp +++ b/src/livecode/context.cpp @@ -1,6 +1,6 @@ // SPDX-License-Identifier: GPL-2.0-or-later /* - * API for the livecoding script language + * Context for the livecoding script language * * Authors: * Sol Bekic <s+inkscape@s-ol.nu> @@ -9,26 +9,38 @@ * Released under GNU GPL v2+, read the file 'COPYING' for more information. */ +#include <giomm/file.h> + #include "svg/stringstream.h" #include "svg/svg-color.h" #include "svg/svg.h" #include "document.h" #include "display/sp-canvas-item.h" -#include "livecode/api.h" +#include "livecode/context.h" +#include "livecode/api/api.h" namespace Inkscape { namespace Livecode { -API::API(SPDesktop *desktop) +Context::Context(SPDesktop *desktop) : desktop(desktop) , doc_root(nullptr) , ui_root(nullptr) , _mouse(*this) + , script_function(nullptr) { + janet_init(); + + env = janet_core_env(NULL); + janet_lib_context(env, *this); + janet_lib_input(env); + janet_lib_geom_point(env); } -API::~API() { +Context::~Context() { + janet_deinit(); + if (doc_root) { doc_root->deleteObject(true, true); doc_root = nullptr; @@ -40,7 +52,7 @@ API::~API() { } } -Inkscape::XML::Node *API::make_rect(Geom::Rect const &rect, SPCSSAttr *css) { +Inkscape::XML::Node *Context::make_rect(Geom::Rect const &rect, SPCSSAttr *css) { SPDocument *doc = desktop->getDocument(); Inkscape::XML::Document *xml_doc = doc->getReprDoc(); @@ -61,7 +73,7 @@ Inkscape::XML::Node *API::make_rect(Geom::Rect const &rect, SPCSSAttr *css) { return repr; } -Inkscape::XML::Node *API::make_line(Geom::Point const &p1, Geom::Point const &p2, SPCSSAttr *css) { +Inkscape::XML::Node *Context::make_line(Geom::Point const &p1, Geom::Point const &p2, SPCSSAttr *css) { SPDocument *doc = desktop->getDocument(); Inkscape::XML::Document *xml_doc = doc->getReprDoc(); @@ -82,7 +94,7 @@ Inkscape::XML::Node *API::make_line(Geom::Point const &p1, Geom::Point const &p2 return repr; } -Inkscape::XML::Node *API::make_path(Glib::ustring d, Geom::Point const &pos, SPCSSAttr *css) { +Inkscape::XML::Node *Context::make_path(Glib::ustring d, Geom::Point const &pos, SPCSSAttr *css) { SPDocument *doc = desktop->getDocument(); Inkscape::XML::Document *xml_doc = doc->getReprDoc(); @@ -102,7 +114,7 @@ Inkscape::XML::Node *API::make_path(Glib::ustring d, Geom::Point const &pos, SPC return repr; } -Inkscape::XML::Node *API::make_arrow(Geom::Point const &from, Geom::Point const &to, SPCSSAttr *css) { +Inkscape::XML::Node *Context::make_arrow(Geom::Point const &from, Geom::Point const &to, SPCSSAttr *css) { SPDocument *doc = desktop->getDocument(); Inkscape::XML::Document *xml_doc = doc->getReprDoc(); @@ -145,7 +157,7 @@ SPCSSAttr *handle_style() { } } -bool API::input_point(Glib::ustring const &id, Geom::Point *point) { +bool Context::input_point(Glib::ustring const &id, Geom::Point *point) { auto const half_handle_size = Geom::Point(2, 2); auto ui_point = *point * desktop->w2d().inverse(); Geom::Rect const rect(ui_point - half_handle_size, ui_point + half_handle_size); @@ -180,7 +192,7 @@ bool API::input_point(Glib::ustring const &id, Geom::Point *point) { return change; } -bool API::input_line(Glib::ustring const &id, Geom::Point *p1, Geom::Point *p2) { +bool Context::input_line(Glib::ustring const &id, Geom::Point *p1, Geom::Point *p2) { bool change = false; if (input_point(id + "_p1", p1)) { change = true; @@ -192,7 +204,7 @@ bool API::input_line(Glib::ustring const &id, Geom::Point *p1, Geom::Point *p2) return change; } -bool API::input_arrow(Glib::ustring const &id, Geom::Point *from, Geom::Point *to) { +bool Context::input_arrow(Glib::ustring const &id, Geom::Point *from, Geom::Point *to) { bool change = false; if (input_point(id + "_from", from)) { change = true; @@ -204,7 +216,7 @@ bool API::input_arrow(Glib::ustring const &id, Geom::Point *from, Geom::Point *t return change; } -bool API::input_rect(Glib::ustring const &id, Geom::Rect *rect) { +bool Context::input_rect(Glib::ustring const &id, Geom::Rect *rect) { Geom::Point min = rect->min(); Geom::Point max = rect->max(); @@ -223,15 +235,15 @@ bool API::input_rect(Glib::ustring const &id, Geom::Rect *rect) { return change; } -Mouse const &API::mouse() { +Mouse const &Context::mouse() { return _mouse; } -void API::push_event(GdkEvent *event) { +void Context::push_event(GdkEvent *event) { _mouse.push_event(event); } -void API::setup_frame() { +void Context::setup_frame() { hot = ""; if (doc_root) { @@ -255,20 +267,46 @@ void API::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 API::finish_frame() { +void Context::finish_frame() { doc_root->updateRepr(); ui_root->updateRepr(); _mouse.finish_frame(); } -void API::draw_doc(Glib::ustring const &id, Inkscape::XML::Node *repr) { +void Context::load_script(Glib::ustring const &path) { + g_message("loading file %s", path.c_str()); + script_file = Gio::File::create_for_path(path); +} + +void Context::draw_doc(Glib::ustring const &id, Inkscape::XML::Node *repr) { repr->setAttribute("inkscape:livecode-id", id); doc_root->appendChildRepr(repr); Inkscape::GC::release(repr); } -void API::draw_ui(Glib::ustring const &id, Inkscape::XML::Node *repr) { +void Context::draw_ui(Glib::ustring const &id, Inkscape::XML::Node *repr) { repr->setAttribute("inkscape:livecode-id", id); ui_root->appendChildRepr(repr); Inkscape::GC::release(repr); |
