git.s-ol.nu inkscape / 1b69cd0
more janet bindings s-ol 3 years ago
7 changed file(s) with 165 addition(s) and 64 deletion(s). Raw diff Collapse all Expand all
2020
2121 Janet janet_wrap_point(Geom::Point const &x);
2222 Janet janet_wrap_point(Geom::IntPoint const &x);
23 Geom::Point janet_unwrap_point(Janet x);
23 Geom::Point &janet_unwrap_point(Janet x);
2424
2525 /* input.cpp */
2626 void janet_lib_input(JanetTable *env);
1212 return *_context;
1313 }
1414
15 extern "C" Janet cfun_input_point(int32_t argc, Janet *argv) {
16 janet_fixarity(argc, 2);
17 ctx().input_point(janet_getcstring(argv, 0),
18 &janet_unwrap_point(argv[1]));
19 return janet_wrap_nil();
20 }
21
22 extern "C" Janet cfun_input_line(int32_t argc, Janet *argv) {
23 janet_fixarity(argc, 3);
24 ctx().input_line(janet_getcstring(argv, 0),
25 &janet_unwrap_point(argv[1]),
26 &janet_unwrap_point(argv[2]));
27 return janet_wrap_nil();
28 }
29
30 extern "C" Janet cfun_input_arrow(int32_t argc, Janet *argv) {
31 janet_fixarity(argc, 3);
32 ctx().input_arrow(janet_getcstring(argv, 0),
33 &janet_unwrap_point(argv[1]),
34 &janet_unwrap_point(argv[2]));
35 return janet_wrap_nil();
36 }
37
38 const JanetReg it_cfuns[] = {
39 {
40 "input/point", cfun_input_point,
41 "(input/point id p)\n\nMake p modifiable via a handle.\n"
42 "This function mutates p."
43 },
44 {
45 "input/line", cfun_input_line,
46 "(input/line id p1 p2)\n\nMake a modifiable line between p1 and p2\n"
47 "This function mutates p1 and p2."
48 },
49 {
50 "input/arrow", cfun_input_arrow,
51 "(input/arrow id from to)\n\nMake a modifiable arrow between from and to\n"
52 "This function mutates from and to."
53 },
54 {NULL, NULL, NULL}
55 };
56
1557 void janet_lib_context(JanetTable *env, Context &context) {
1658 _context = &context;
59 janet_cfuns(env, NULL, it_cfuns);
1760 }
1861
1962 }
105105
106106
107107 void janet_lib_input(JanetTable *env) {
108 janet_printf("yoloading");
108109 janet_cfuns(env, NULL, it_cfuns);
109110 }
110111
99
1010 extern "C" void geom_point_tostring(void *p, JanetBuffer *buffer) {
1111 std::stringstream stream;
12 stream << "<2geom/point " << *static_cast<Geom::Point *>(p) << ">";
12 stream << "<geom/point " << *static_cast<Geom::Point *>(p) << ">";
1313 janet_buffer_push_cstring(buffer, stream.str().c_str());
1414 }
1515
1616
1717 const JanetAbstractType geom_point_type = {
18 "2geom/point",
18 "geom/point",
1919 NULL,
2020 NULL,
2121 geom_point_get,
3838 return janet_wrap_abstract(box);
3939 }
4040
41 Geom::Point janet_unwrap_point(Janet x) {
41 Geom::Point &janet_unwrap_point(Janet x) {
4242 if (janet_checktype(x, JANET_ABSTRACT)) {
4343 void *abst = janet_unwrap_abstract(x);
4444 if (janet_abstract_type(abst) == &geom_point_type)
5656 } else if (janet_checktype(x, JANET_NUMBER)) {
5757 double val = janet_unwrap_number(x);
5858 return Geom::Point(val, val);
59 }
60
61 janet_panic("expected a geom/point");
62 }
63
64
65 Geom::Point& janet_unwrap_point_ref(Janet x) {
66 if (janet_checktype(x, JANET_ABSTRACT)) {
67 void *abst = janet_unwrap_abstract(x);
68 if (janet_abstract_type(abst) == &geom_point_type)
69 return *(Geom::Point *)abst;
7059 }
7160
7261 janet_panic("expected a geom/point");
8877
8978 const JanetReg it_cfuns[] = {
9079 {
91 "point", cfun_geom_point_new,
80 "geom/point", cfun_geom_point_new,
9281 "(geom/point x y)\n\nCreate a Point from x/y coordinate values."
9382 },
9483 {NULL, NULL, NULL}
176165
177166 static Janet cfun_geom_point_eq(int32_t argc, Janet *argv) {
178167 janet_fixarity(argc, 2);
179 Geom::Point& v1 = janet_unwrap_point_ref(argv[0]);
180 Geom::Point& v2 = janet_unwrap_point_ref(argv[1]);
168 Geom::Point &v1 = janet_unwrap_point(argv[0]);
169 Geom::Point &v2 = janet_unwrap_point(argv[1]);
181170 return janet_wrap_boolean(v1 == v2);
182171 }
183172
184173 static Janet cfun_geom_point_lt(int32_t argc, Janet *argv) {
185174 janet_fixarity(argc, 2);
186 Geom::Point& v1 = janet_unwrap_point_ref(argv[0]);
187 Geom::Point& v2 = janet_unwrap_point_ref(argv[1]);
175 Geom::Point &v1 = janet_unwrap_point(argv[0]);
176 Geom::Point &v2 = janet_unwrap_point(argv[1]);
188177 return janet_wrap_boolean(v1 < v2);
189178 }
190179
88 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
99 */
1010
11 #include <giomm/file.h>
11 #include <memory>
1212
1313 #include "svg/stringstream.h"
1414 #include "svg/svg-color.h"
2222 namespace Inkscape {
2323 namespace Livecode {
2424
25 Script::Script(JanetTable *env, std::string const &path)
26 : env(env)
27 , function(nullptr)
28 , file(Gio::File::create_for_path(path))
29 , monitor(file->monitor_file())
30 {
31 monitor->signal_changed().connect(sigc::mem_fun(this, &Script::file_changed));
32 reload();
33 }
34
35 void Script::frame() {
36 if (function) {
37 Janet result;
38 janet_pcall(function, 0, NULL, &result, NULL);
39 }
40 }
41
42 void Script::commit() {
43 }
44
45 void Script::reload() {
46 try {
47 uint8_t *data;
48 size_t length;
49 file->load_contents((char *&)data, (gsize &)length);
50
51 Janet result;
52 janet_dobytes(env, data, length, file->get_path().c_str(), &result);
53 if (janet_checktype(result, JANET_FUNCTION)) {
54 function = janet_unwrap_function(result);
55 } else {
56 g_message("Janet script didn't return a function");
57 }
58 } catch (...) {
59 g_message("error loading file");
60 }
61 }
62
63 void Script::file_changed(const Glib::RefPtr<Gio::File>& file,
64 const Glib::RefPtr<Gio::File>& other_file,
65 Gio::FileMonitorEvent event)
66 {
67 reload();
68 }
69
2570 Context::Context(SPDesktop *desktop)
2671 : desktop(desktop)
72 , _mouse(*this)
2773 , doc_root(nullptr)
2874 , ui_root(nullptr)
29 , _mouse(*this)
30 , script_function(nullptr)
3175 {
3276 janet_init();
3377
121165
122166 Geom::Point back = from - to;
123167 back.normalize();
124 Geom::Point const cross_a = back.cw() + back * 2.0;
125 Geom::Point const cross_b = back.cw() + back * 2.0;
168 Geom::Point const cross_a = back.cw() * 5.0 + back * 10.0;
169 Geom::Point const cross_b = back.ccw() * 5.0 + back * 10.0;
126170
127171 gchar* d = g_strdup_printf("M %f,%f %f,%f l %f,%f M %f,%f l %f,%f",
128172 from.x(), from.y(),
133177 repr->setAttribute("d", d);
134178 g_free(d);
135179
136 Glib::ustring css_str;
137180 if (css) {
181 Glib::ustring css_str;
138182 sp_repr_css_write_string(css, css_str);
139183 sp_repr_css_attr_unref(css);
140 } else {
141 css_str = "stroke: #000000;";
142 }
143 repr->setAttribute("style", css_str.c_str());
184 repr->setAttribute("style", css_str.c_str());
185 }
144186
145187 return repr;
146188 }
199241 if (input_point(id + "_p2", p2)) {
200242 change = true;
201243 }
202 draw_doc("", make_line(*p1, *p2));
244
245 SPCSSAttr *css = sp_repr_css_attr_new();
246 sp_repr_css_set_property(css, "stroke-width", "1");
247 sp_repr_css_set_property(css, "stroke", "#000000");
248 sp_repr_css_set_property(css, "fill", "none");
249 draw_doc("", make_line(*p1, *p2, css));
203250 return change;
204251 }
205252
211258 if (input_point(id + "_to", to)) {
212259 change = true;
213260 }
214 draw_doc("", make_line(*from, *to));
261
262 SPCSSAttr *css = sp_repr_css_attr_new();
263 sp_repr_css_set_property(css, "stroke-width", "1");
264 sp_repr_css_set_property(css, "stroke", "#000000");
265 sp_repr_css_set_property(css, "fill", "none");
266 draw_doc("", make_arrow(*from, *to, css));
215267 return change;
216268 }
217269
242294 _mouse.push_event(event);
243295 }
244296
297 void Context::frame() {
298 setup_frame();
299
300 if (_script) {
301 _script->frame();
302 }
303
304 finish_frame();
305 }
306
245307 void Context::setup_frame() {
246308 hot = "";
247309
266328 ui_root = SP_ITEM(desktop->currentLayer()->appendChildRepr(rui));
267329 ui_root->doWriteTransform(ui2doc(), nullptr, true);
268330 Inkscape::GC::release(rui);
269
270 if (script_file) {
271 try {
272 char *data;
273 gsize length;
274 script_file->load_contents(data, length);
275
276 Janet result;
277 janet_dobytes(env, (uint8_t *)data, length, script_file->get_path().c_str(), &result);
278 if (janet_checktype(result, JANET_FUNCTION)) {
279 script_function = janet_unwrap_function(result);
280 }
281 } catch (...) {
282 g_message("error loading file");
283 }
284 }
285
286 if (script_function) {
287 Janet result;
288 janet_pcall(script_function, 0, NULL, &result, NULL);
289 }
290331 }
291332
292333 void Context::finish_frame() {
296337 }
297338
298339 void Context::load_script(Glib::ustring const &path) {
299 g_message("loading file %s", path.c_str());
300 script_file = Gio::File::create_for_path(path);
340 g_message("loading script %s", path.c_str());
341 _script = nullptr;
342
343 try {
344 _script.reset(new Script(env, path));
345 g_message("loaded.");
346 } catch (...) {
347 g_message("error creating script");
348 }
301349 }
302350
303351 void Context::draw_doc(Glib::ustring const &id, Inkscape::XML::Node *repr) {
1515 #include <2geom/point.h>
1616 #include <2geom/rect.h>
1717 #include <gdk/gdk.h>
18 #include <giomm/file.h>
19 #include <giomm/filemonitor.h>
1820 #include <janet.h>
1921
2022 #include "xml/repr.h"
2527 class SPDocument;
2628 class SPItem;
2729
28 namespace Gio {
29 class File;
30 };
31
3230 namespace Inkscape {
3331 namespace Livecode {
3432
3533 class Mouse;
34
35 class Script {
36 public:
37 Script(JanetTable *env, std::string const &path);
38
39 void frame();
40 void commit();
41
42 private:
43 JanetTable *env;
44 JanetFunction *function;
45 Glib::RefPtr<Gio::File> file;
46 Glib::RefPtr<Gio::FileMonitor> monitor;
47
48 void reload();
49 void file_changed(const Glib::RefPtr<Gio::File>& file,
50 const Glib::RefPtr<Gio::File>& other_file,
51 Gio::FileMonitorEvent event);
52 };
3653
3754 class Context {
3855 public:
5370 void draw_ui(Glib::ustring const &id, Inkscape::XML::Node *item);
5471
5572 void push_event(GdkEvent *event);
56 void setup_frame();
57 void finish_frame();
73 void frame();
5874
5975 inline Geom::Affine ui2dt() const {
6076 return doc_root->i2doc_affine();
8096 void load_script(Glib::ustring const &path);
8197
8298 private:
99 void setup_frame();
100 void finish_frame();
101
83102 SPDesktop *desktop;
84103 SPItem *doc_root, *ui_root;
85104
86105 JanetTable *env;
87 JanetFunction *script_function;
88 Glib::RefPtr<Gio::File> script_file;
89106
107 std::unique_ptr<Script> _script;
90108 Mouse _mouse;
91109
92110 Glib::ustring hot, active;
9999
100100 bool LivecodeTool::handle_tick(Glib::RefPtr<Gdk::FrameClock> const &frame_clock)
101101 {
102 context.setup_frame();
102 context.frame();
103103
104 /*
104105 context.input_point("p", &p);
105106 context.input_arrow("a->b", &a, &b);
106107 context.input_rect("rect", &rect);
107108
108109 context.finish_frame();
110 */
109111 return true;
110112 }
111113