summaryrefslogtreecommitdiffstats
path: root/src/livecode/context.h
blob: 4ebb73609bec242ea017a2c9670ba9d87e5b0883 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// SPDX-License-Identifier: GPL-2.0-or-later
#ifndef INK_LIVECODE_API_H
#define INK_LIVECODE_API_H

/*
 * Context for the livecoding script language
 *
 * Authors:
 *   Sol Bekic <s+inkscape@s-ol.nu>
 * Copyright (C) 2019 Authors
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include <cstddef>
#include <2geom/point.h>
#include <2geom/rect.h>
#include <gdk/gdk.h>
#include <giomm/file.h>
#include <giomm/filemonitor.h>
#include <janet.h>

#include "xml/repr.h"
#include "desktop.h"
#include "livecode/input.h"


class SPDocument;
class SPItem;

namespace Inkscape {
namespace Livecode {

class Mouse;

class Script {
public:
    Script(JanetTable *env, std::string const &path);

    void frame();
    void commit();

private:
    JanetTable *env;
    JanetFunction *function;
    Glib::RefPtr<Gio::File> file;
    Glib::RefPtr<Gio::FileMonitor> monitor;

    void reload();
    void file_changed(const Glib::RefPtr<Gio::File>& file,
                      const Glib::RefPtr<Gio::File>& other_file,
                      Gio::FileMonitorEvent event);
};

class Context {
public:
    Context(SPDesktop *desktop);
    ~Context();

    Inkscape::XML::Node *make_rect(Geom::Rect const &rect, SPCSSAttr *css = nullptr);
    Inkscape::XML::Node *make_line(Geom::Point const &p1, Geom::Point const &p2, SPCSSAttr *css = nullptr);
    Inkscape::XML::Node *make_path(Glib::ustring d, Geom::Point const &pos = Geom::Point(), SPCSSAttr *css = nullptr);
    Inkscape::XML::Node *make_arrow(Geom::Point const &from, Geom::Point const &to, SPCSSAttr *css = nullptr);
   
    bool input_point(Glib::ustring const &id, Geom::Point *point);
    bool input_line(Glib::ustring const &id, Geom::Point *p1, Geom::Point *p2);
    bool input_arrow(Glib::ustring const &id, Geom::Point *from, Geom::Point *to);
    bool input_rect(Glib::ustring const &id, Geom::Rect *rect);

    void draw_doc(Glib::ustring const &id, Inkscape::XML::Node *item);
    void draw_ui(Glib::ustring const &id, Inkscape::XML::Node *item);

    void push_event(GdkEvent *event);
    void frame();

    inline Geom::Affine ui2dt() const {
      return doc_root->i2doc_affine();
    }
    inline Geom::Affine dt2ui() const {
      return ui2dt().inverse();
    }

    inline Geom::Affine ui2doc() const {
      return desktop->w2d();
    }
    inline Geom::Affine doc2ui() const {
      return ui2doc().inverse();
    }
    inline Geom::Affine ui2doc_vec() const {
      return desktop->w2d().withoutTranslation();
    }
    inline Geom::Affine doc2ui_vec() const {
      return ui2doc_vec().inverse();
    }

    Mouse const &mouse();
    void load_script(Glib::ustring const &path);

private:
    void setup_frame();
    void finish_frame();

    SPDesktop *desktop;
    SPItem *doc_root, *ui_root;

    JanetTable *env;

    std::unique_ptr<Script> _script;
    Mouse _mouse;

    Glib::ustring hot, active;
    std::vector<SPItem *> drawn_items;
};

}
}

#endif // INK_LIVECODE_API_H

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :