aboutsummaryrefslogtreecommitdiff
path: root/cursor.c
blob: 6cc0bfdb80b98725c2714199bd11b73ea57eb649 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <m_pd.h>
#include <g_canvas.h>

static t_symbol *button_symbol;
static t_symbol *motion_symbol;
static t_symbol *wheel_symbol;

//TODO add reset method for cursor icons, this should probably be done in pd.tk

// TODO figure out some way to get
//t_int hidio_instance_count;
/* this is used to test for the first instance to execute */
//double last_execute_time;

static t_class *cursor_class;

typedef struct _cursor
{
    t_object x_obj;
    t_symbol *receive_symbol;
    t_canvas *parent_canvas;
    t_outlet *data_outlet;
//    t_outlet *status_outlet; // not used (yet?)
    t_int optionc;
    char *optionv[];
} t_cursor;


/* idea from #tcl for a Tcl unbind
proc unbind {tag event script} {
     set bind ""
     foreach x [split [bind $tag $event] "\n"] {
         if {$x != $script} {lappend bind $x}
     }
     bind $tag $event {}
     foreach x $bind {bind $tag $event $x}
 }
*/

static void cursor_setmethod(t_cursor *x, t_symbol *s, int argc, t_atom *argv)
{
    sys_vgui("set cursor_%s \"%s\"\n", s->s_name, atom_getsymbol(argv)->s_name);
    canvas_setcursor(x->parent_canvas, 0); /* hack to refresh the cursor */
}

static void cursor_bang(t_cursor *x)
{
    sys_vgui("pd [concat %s motion [winfo pointerx .] [winfo pointery .] \\;]\n",
             x->receive_symbol->s_name);
}

/* TODO this needs some kind of working unbind to be useful, otherwise when an
 * instance is deleted, you get reams of "error: #123123 no such object"
 * messages in the Pd Window.
static void cursor_float(t_cursor *x, t_float f)
{
    if(f > 0)
    {
        sys_vgui("bind all <Motion> {+pd [concat %s motion %%x %%y \\;]}\n",
            x->receive_symbol->s_name);
    }
    else
    {
        // TODO figure out how to turn off this binding
        sys_vgui("bind all <Motion> {+pd [concat %s motion %%x %%y \\;]}\n",
            x->receive_symbol->s_name);
    }
}
*/

static void cursor_button_callback(t_cursor *x, t_float button, t_float state)
{
    t_atom output_atoms[2];
    
    SETFLOAT(output_atoms, button);
    SETFLOAT(output_atoms + 1, state);
    outlet_anything(x->data_outlet, button_symbol, 2, output_atoms);
}

static void cursor_motion_callback(t_cursor *x, t_float x_position, t_float y_position)
{
    t_atom output_atoms[2];
    
    SETFLOAT(output_atoms, x_position);
    SETFLOAT(output_atoms + 1, y_position);
    outlet_anything(x->data_outlet, motion_symbol, 2, output_atoms);
}

static void cursor_wheel_callback(t_cursor *x, t_float f)
{
    t_atom output_atom;
    
    SETFLOAT(&output_atom, f);
    outlet_anything(x->data_outlet, wheel_symbol, 1, &output_atom);
}

static void cursor_free(t_cursor *x)
{
    pd_unbind(&x->x_obj.ob_pd, x->receive_symbol);
    //TODO free the "bind all"
}

static void *cursor_new(void)
{
    char buf[MAXPDSTRING];
    t_cursor *x = (t_cursor *)pd_new(cursor_class);

    x->parent_canvas = canvas_getcurrent();

    sprintf(buf, "#%lx", (t_int)x);
    x->receive_symbol = gensym(buf);
    pd_bind(&x->x_obj.ob_pd, x->receive_symbol);
	x->data_outlet = outlet_new(&x->x_obj, 0);
//	x->status_outlet = outlet_new(&x->x_obj, 0);

/* not working yet
    sys_vgui("bind . <ButtonPress> {+pd [concat %s button %%b 1 \\;]}\n",
             x->receive_symbol->s_name);
    sys_vgui("bind . <ButtonRelease> {+pd [concat %s button %%b 0 \\;]}\n",
             x->receive_symbol->s_name);
    sys_vgui("bind . <MouseWheel> {+pd [concat %s wheel %%D \\;]}\n",
             x->receive_symbol->s_name);
*/

    return(x);
}

void cursor_setup(void)
{
    cursor_class = class_new(gensym("cursor"),
        (t_newmethod)cursor_new, (t_method)cursor_free,
        sizeof(t_cursor), 0, 0);

    class_addbang(cursor_class, (t_method)cursor_bang);
/*     class_addfloat(cursor_class, (t_method)cursor_float); */

    button_symbol = gensym("button");
    motion_symbol = gensym("motion");
    wheel_symbol = gensym("wheel");

    class_addmethod(cursor_class, (t_method)cursor_button_callback, 
                    button_symbol, A_DEFFLOAT, A_DEFFLOAT, 0);
    class_addmethod(cursor_class, (t_method)cursor_motion_callback, 
                    motion_symbol, A_DEFFLOAT, A_DEFFLOAT, 0);
    class_addmethod(cursor_class, (t_method)cursor_wheel_callback, 
                    wheel_symbol, A_DEFFLOAT, 0);

    /* methods for setting the cursor icon */
    class_addmethod(cursor_class, (t_method)cursor_setmethod, 
                    gensym("runmode_nothing"), A_GIMME, 0);
    class_addmethod(cursor_class, (t_method)cursor_setmethod, 
                    gensym("runmode_clickme"), A_GIMME, 0);
    class_addmethod(cursor_class, (t_method)cursor_setmethod, 
                    gensym("runmode_thicken"), A_GIMME, 0);
    class_addmethod(cursor_class, (t_method)cursor_setmethod, 
                    gensym("runmode_addpoint"), A_GIMME, 0);
    class_addmethod(cursor_class, (t_method)cursor_setmethod, 
                    gensym("editmode_nothing"), A_GIMME, 0);
    class_addmethod(cursor_class, (t_method)cursor_setmethod, 
                    gensym("editmode_connect"), A_GIMME, 0);
    class_addmethod(cursor_class, (t_method)cursor_setmethod, 
                    gensym("editmode_disconnect"), A_GIMME, 0);
}