/
uires.c
217 lines (172 loc) · 5.52 KB
/
uires.c
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/**
@file
uires - demonstrate the use of resources
@ingroup examples
*/
#include "ext.h"
#include "ext_obex.h"
#include "jpatcher_api.h"
#include "jgraphics.h"
/**********************************************************************/
// Data Structures
typedef struct _uires {
t_jbox j_box;
char j_clicked;
char j_hover;
t_jsurface *j_idrewitmyself;
t_jsurface *j_idrewitmyselftongue;
} t_uires;
/**********************************************************************/
// Prototypes
void uires_initclass();
t_uires *uires_new(t_symbol *s, short argc, t_atom *argv);
void uires_free(t_uires *x);
void uires_load_surfaces(t_uires *x);
t_jsurface * uires_load_surface_from_name(t_uires *, char *name);
void uires_setmouse(t_uires *x, long which);
void uires_mousedown(t_uires *x, t_object *patcherview, t_pt pt, long modifiers);
void uires_mouseup(t_uires *x, t_object *patcherview, t_pt pt, long modifiers);
void uires_mouseenter(t_uires *x, t_object *patcherview, t_pt pt, long modifiers);
void uires_mouseleave(t_uires *x, t_object *patcherview, t_pt pt, long modifiers);
void uires_paint(t_uires *x, t_object *view);
/**********************************************************************/
// Globals and Statics
static t_class *s_uires_class = NULL;
/**********************************************************************/
// Class Definition and Life Cycle
void ext_main(void *moduleRef)
{
t_class *c;
c = class_new("uires",
(method)uires_new,
(method)uires_free,
sizeof(t_uires),
(method)NULL,
A_GIMME,
0L);
jbox_initclass(c, 0);
class_addmethod(c, (method) uires_mousedown, "mousedown", A_CANT, 0);
class_addmethod(c, (method) uires_mouseup, "mouseup", A_CANT, 0);
class_addmethod(c, (method) uires_mouseenter, "mouseenter", A_CANT, 0);
class_addmethod(c, (method) uires_mouseleave, "mouseleave", A_CANT, 0);
class_addmethod(c, (method) uires_paint, "paint", A_CANT, 0);
CLASS_ATTR_DEFAULT(c, "rect", 0, "0. 0. 150. 150.");
class_register(CLASS_BOX, c);
s_uires_class = c;
}
t_uires *uires_new(t_symbol *s, short argc, t_atom *argv)
{
t_uires *x = (t_uires *)object_alloc(s_uires_class);
if (x) {
long flags;
flags = 0
| JBOX_DRAWFIRSTIN
// | JBOX_NODRAWBOX
// | JBOX_DRAWINLAST
| JBOX_TRANSPARENT
// | JBOX_NOGROW
// | JBOX_GROWY
| JBOX_GROWBOTH
// | JBOX_IGNORELOCKCLICK
| JBOX_HILITE
// | JBOX_BACKGROUND
// | JBOX_NOFLOATINSPECTOR
// | JBOX_TEXTFIELD
;
jbox_new(&x->j_box, flags, argc, argv);
x->j_box.b_firstin = (t_object *) x;
// find the images in the search path, and load them.
uires_load_surfaces(x);
jbox_ready(&x->j_box);
}
return x;
}
void uires_load_surfaces(t_uires *x)
{
if (x->j_idrewitmyself == NULL) {
x->j_idrewitmyself = uires_load_surface_from_name(x, "idrewitmyself.png");
}
if (x->j_idrewitmyselftongue == NULL) {
x->j_idrewitmyselftongue = uires_load_surface_from_name(x, "idrewitmyselftongue.png");
}
}
t_jsurface * uires_load_surface_from_name(t_uires *x, char *name)
{
short path;
t_fourcc outtype;
char filename[MAX_FILENAME_CHARS];
strncpy_zero(filename, name, MAX_FILENAME_CHARS);
if (!locatefile_extended(filename, &path, &outtype, NULL, 0)) {
// found the image, get the t_jsurface* from the file
return jgraphics_image_surface_create_from_file(filename, path);
} else {
object_warn((t_object *)x, "Couldn't locate image (%s)", name);
return NULL;
}
}
void uires_free(t_uires *x)
{
if (x->j_idrewitmyself) {
jgraphics_surface_destroy(x->j_idrewitmyself);
x->j_idrewitmyself = NULL;
}
if (x->j_idrewitmyselftongue) {
jgraphics_surface_destroy(x->j_idrewitmyselftongue);
x->j_idrewitmyselftongue = NULL;
}
jbox_free(&x->j_box);
}
/**********************************************************************/
// Methods
void uires_mousedown(t_uires *x, t_object *patcherview, t_pt pt, long modifiers)
{
x->j_clicked = true;
jbox_redraw((t_jbox *)x);
}
void uires_mouseup(t_uires *x, t_object *patcherview, t_pt pt, long modifiers)
{
x->j_clicked = false;
jbox_redraw((t_jbox *)x);
}
void uires_mouseenter(t_uires *x, t_object *patcherview, t_pt pt, long modifiers)
{
x->j_hover = true;
jbox_redraw((t_jbox *)x);
}
void uires_mouseleave(t_uires *x, t_object *patcherview, t_pt pt, long modifiers)
{
x->j_hover = false;
jbox_redraw((t_jbox *)x);
}
void uires_paint(t_uires *x, t_object *view)
{
// paint the box grey
t_rect rect;
t_jgraphics *g;
g = (t_jgraphics *) patcherview_get_jgraphics(view);
// set up matrix
jbox_get_rect_for_view((t_object *) x, view, &rect);
jgraphics_set_source_rgba(g, 0, 0, 0, 1);
if (x->j_idrewitmyself && x->j_idrewitmyselftongue) {
t_rect src, dst;
src.x = src.y = 0.;
src.width = jgraphics_image_surface_get_width(x->j_idrewitmyself); // we know that both images
src.height = jgraphics_image_surface_get_height(x->j_idrewitmyself); // have the same dimensions.
dst.x = dst.y = 0.;
dst.width = rect.width;
dst.height = rect.height;
if (x->j_clicked)
jgraphics_image_surface_draw(g, x->j_idrewitmyselftongue, src, dst);
else
jgraphics_image_surface_draw(g, x->j_idrewitmyself, src, dst);
}
if (x->j_hover) {
jgraphics_set_source_rgba(g, 1., 1., 1., 1.);
jgraphics_arc(g, rect.width * 0.53, rect.height * 0.4, rect.width * 0.015, 0, JGRAPHICS_2PI);
jgraphics_fill(g);
jgraphics_arc(g, rect.width * 0.54, rect.height * 0.5, rect.width * 0.015, 0, JGRAPHICS_2PI);
jgraphics_fill(g);
jgraphics_arc(g, rect.width * 0.54, rect.height * 0.6, rect.width * 0.015, 0, JGRAPHICS_2PI);
jgraphics_fill(g);
}
}