Max 5 API Reference
00001 /* 00002 uisimp -- fancier version of a toggle 00003 */ 00004 00005 #include "ext.h" // standard Max include, always required 00006 #include "ext_obex.h" // required for new style Max object 00007 #include "jpatcher_api.h" 00008 #include "jgraphics.h" 00009 00010 typedef struct _uisimp 00011 { 00012 t_jbox u_box; // header for UI objects 00013 void *u_out; // outlet pointer 00014 long u_state; // state (1 or 0) 00015 char u_mouseover; // is mouse over the object 00016 char u_mousedowninside; // is mouse down within the object 00017 char u_trackmouse; // if non-zero, track mouse when button not down 00018 t_jrgba u_outline; 00019 t_jrgba u_check; 00020 t_jrgba u_background; 00021 t_jrgba u_hilite; 00022 } t_uisimp; 00023 00024 void *uisimp_new(t_symbol *s, long argc, t_atom *argv); 00025 void uisimp_free(t_uisimp *x); 00026 void uisimp_assist(t_uisimp *x, void *b, long m, long a, char *s); 00027 void uisimp_paint(t_uisimp *x, t_object *patcherview); 00028 void uisimp_getdrawparams(t_uisimp *x, t_object *patcherview, t_jboxdrawparams *params); 00029 void uisimp_mousedown(t_uisimp *x, t_object *patcherview, t_pt pt, long modifiers); 00030 void uisimp_mousedrag(t_uisimp *x, t_object *patcherview, t_pt pt, long modifiers); 00031 void uisimp_mouseup(t_uisimp *x, t_object *patcherview, t_pt pt, long modifiers); 00032 void uisimp_mouseenter(t_uisimp *x, t_object *patcherview, t_pt pt, long modifiers); 00033 void uisimp_mouseleave(t_uisimp *x, t_object *patcherview, t_pt pt, long modifiers); 00034 void uisimp_mousemove(t_uisimp *x, t_object *patcherview, t_pt pt, long modifiers); 00035 void uisimp_bang(t_uisimp *x); 00036 void uisimp_int(t_uisimp *x, long n); 00037 00038 00039 static t_class *s_uisimp_class; 00040 00041 int main(void) 00042 { 00043 t_class *c; 00044 00045 c = class_new("uisimp", (method)uisimp_new, (method)uisimp_free, sizeof(t_uisimp), 0L, A_GIMME, 0); 00046 00047 c->c_flags |= CLASS_FLAG_NEWDICTIONARY; 00048 jbox_initclass(c, JBOX_FIXWIDTH | JBOX_COLOR); 00049 00050 class_addmethod(c, (method)uisimp_paint, "paint", A_CANT, 0); 00051 class_addmethod(c, (method)uisimp_mousedown, "mousedown", A_CANT, 0); 00052 class_addmethod(c, (method)uisimp_mousedrag, "mousedrag", A_CANT, 0); 00053 class_addmethod(c, (method)uisimp_mouseup, "mouseup", A_CANT, 0); 00054 class_addmethod(c, (method)uisimp_mouseenter, "mouseenter", A_CANT, 0); 00055 class_addmethod(c, (method)uisimp_mouseleave, "mouseleave", A_CANT, 0); 00056 class_addmethod(c, (method)uisimp_mousemove, "mousemove", A_CANT, 0); 00057 class_addmethod(c, (method)uisimp_assist, "assist", A_CANT, 0); 00058 00059 // messages for state setting / retrieval 00060 00061 class_addmethod(c, (method)uisimp_int, "int", A_LONG, 0); 00062 class_addmethod(c, (method)uisimp_bang, "bang", 0); 00063 00064 // attributes 00065 00066 CLASS_ATTR_CHAR(c, "trackmouse", 0, t_uisimp, u_trackmouse); 00067 CLASS_ATTR_STYLE_LABEL(c, "trackmouse", 0, "onoff", "Track Mouse"); 00068 CLASS_ATTR_SAVE(c, "trackmouse", 0); 00069 CLASS_ATTR_CATEGORY(c, "trackmouse", 0, "Behavior"); 00070 00071 CLASS_STICKY_ATTR(c, "category", 0, "Color"); 00072 CLASS_ATTR_RGBA(c, "bgcolor", 0, t_uisimp, u_background); 00073 CLASS_ATTR_DEFAULTNAME_SAVE_PAINT(c, "bgcolor", 0, "1. 1. 1. 1."); 00074 CLASS_ATTR_STYLE_LABEL(c,"bgcolor",0,"rgba","Background Color"); 00075 00076 CLASS_ATTR_RGBA(c, "bordercolor", 0, t_uisimp, u_outline); 00077 CLASS_ATTR_DEFAULTNAME_SAVE_PAINT(c, "bordercolor", 0, "0.5 0.5 0.5 1."); 00078 CLASS_ATTR_STYLE_LABEL(c,"bordercolor",0,"rgba","Border Color"); 00079 00080 CLASS_ATTR_RGBA(c, "hilitecolor", 0, t_uisimp, u_hilite); 00081 CLASS_ATTR_DEFAULTNAME_SAVE_PAINT(c, "hilitecolor", 0, "0.5 0.5 0.5 1."); 00082 CLASS_ATTR_STYLE_LABEL(c,"hilitecolor",0,"rgba","Hilite Color"); 00083 00084 //CLASS_ATTR_RGBA(c, "color", 0, t_uisimp, u_check); 00085 CLASS_ATTR_DEFAULTNAME_SAVE_PAINT(c, "color", 0, "0. 0. 0. 1."); 00086 CLASS_ATTR_STYLE_LABEL(c,"color",0,"rgba","Check Color"); 00087 00088 CLASS_STICKY_ATTR_CLEAR(c, "category"); 00089 00090 00091 CLASS_ATTR_DEFAULT(c,"patching_rect",0, "0. 0. 20. 20."); 00092 00093 class_register(CLASS_BOX, c); 00094 s_uisimp_class = c; 00095 00096 return 0; 00097 } 00098 00099 void uisimp_assist(t_uisimp *x, void *b, long m, long a, char *s) 00100 { 00101 if (m == 1) //inlet 00102 sprintf(s, "(signal) Audio Input"); 00103 } 00104 00105 void uisimp_paint(t_uisimp *x, t_object *patcherview) 00106 { 00107 t_rect rect; 00108 t_jgraphics *g = (t_jgraphics*) patcherview_get_jgraphics(patcherview); // obtain graphics context 00109 jbox_get_rect_for_view((t_object *)x, patcherview, &rect); 00110 00111 // paint outline 00112 jgraphics_set_source_jrgba(g, &x->u_outline); 00113 jgraphics_set_line_width(g, 1.); 00114 jgraphics_rectangle(g, 0., 0., rect.width, rect.height); 00115 jgraphics_stroke(g); 00116 00117 // paint "inner highlight" to indicate mouseover 00118 if (x->u_mouseover && !x->u_mousedowninside) { 00119 jgraphics_set_source_jrgba(g, &x->u_hilite); 00120 jgraphics_set_line_width(g, 1.); 00121 jgraphics_rectangle(g, 1., 1., rect.width - 2, rect.height - 2); 00122 jgraphics_stroke(g); 00123 } 00124 if (x->u_mousedowninside && !x->u_state) { // paint hilite color 00125 jgraphics_set_source_jrgba(g, &x->u_hilite); 00126 jgraphics_rectangle(g, 1., 1., rect.width - 2, rect.height - 2); 00127 jgraphics_fill(g); 00128 } 00129 00130 // paint "check" (actually a filled rectangle) if state is on 00131 if (x->u_state) { 00132 t_jrgba col; 00133 00134 jbox_get_color((t_object *)x, &col); 00135 jgraphics_set_source_jrgba(g, &col); 00136 if (x->u_mousedowninside) // make rect bigger if mouse is down and we are unchecking 00137 jgraphics_rectangle(g, 3., 3., rect.width - 6, rect.height - 6); 00138 else 00139 jgraphics_rectangle(g, 4., 4., rect.width - 8, rect.height - 8); 00140 jgraphics_fill(g); 00141 } 00142 } 00143 00144 // mouse interaction 00145 00146 void uisimp_mousedown(t_uisimp *x, t_object *patcherview, t_pt pt, long modifiers) 00147 { 00148 x->u_mousedowninside = true; // wouldn't get a click unless it was inside the box 00149 jbox_redraw((t_jbox *)x); 00150 } 00151 00152 void uisimp_mousedrag(t_uisimp *x, t_object *patcherview, t_pt pt, long modifiers) 00153 { 00154 t_rect rect; 00155 00156 // test to see if mouse is still inside the object 00157 jbox_get_rect_for_view((t_object *)x, patcherview, &rect); 00158 00159 // redraw if changed 00160 if (pt.x >= 0 && pt.x <= rect.width && pt.y >= 0 && pt.y <= rect.height) { 00161 if (!x->u_mousedowninside) { 00162 x->u_mousedowninside = true; 00163 jbox_redraw((t_jbox *)x); 00164 } 00165 } else { 00166 if (x->u_mousedowninside) { 00167 x->u_mousedowninside = false; 00168 jbox_redraw((t_jbox *)x); 00169 } 00170 } 00171 } 00172 00173 void uisimp_mouseup(t_uisimp *x, t_object *patcherview, t_pt pt, long modifiers) 00174 { 00175 if (x->u_mousedowninside) { 00176 x->u_state = !x->u_state; 00177 uisimp_bang(x); 00178 x->u_mousedowninside = false; 00179 jbox_redraw((t_jbox *)x); 00180 } 00181 } 00182 00183 void uisimp_mouseenter(t_uisimp *x, t_object *patcherview, t_pt pt, long modifiers) 00184 { 00185 x->u_mouseover = true; 00186 jbox_redraw((t_jbox *)x); 00187 } 00188 00189 void uisimp_mouseleave(t_uisimp *x, t_object *patcherview, t_pt pt, long modifiers) 00190 { 00191 x->u_mouseover = false; 00192 jbox_redraw((t_jbox *)x); 00193 } 00194 00195 void uisimp_mousemove(t_uisimp *x, t_object *patcherview, t_pt pt, long modifiers) 00196 { 00197 // nothing to do here, but could track mouse position inside object 00198 } 00199 00200 void uisimp_bang(t_uisimp *x) 00201 { 00202 outlet_int(x->u_out, x->u_state); 00203 } 00204 00205 void uisimp_int(t_uisimp *x, long n) 00206 { 00207 x->u_state = n? 1 : 0; 00208 uisimp_bang(x); 00209 jbox_redraw((t_jbox *)x); 00210 } 00211 00212 void uisimp_getdrawparams(t_uisimp *x, t_object *patcherview, t_jboxdrawparams *params) 00213 { 00214 params->d_bordercolor.alpha = 0; 00215 params->d_boxfillcolor.alpha = 0; 00216 } 00217 00218 void uisimp_free(t_uisimp *x) 00219 { 00220 jbox_free((t_jbox *)x); 00221 } 00222 00223 void *uisimp_new(t_symbol *s, long argc, t_atom *argv) 00224 { 00225 t_uisimp *x = NULL; 00226 t_dictionary *d = NULL; 00227 long boxflags; 00228 00229 if (!(d = object_dictionaryarg(argc,argv))) 00230 return NULL; 00231 00232 x = (t_uisimp *)object_alloc(s_uisimp_class); 00233 boxflags = 0 00234 | JBOX_DRAWFIRSTIN 00235 | JBOX_NODRAWBOX 00236 | JBOX_DRAWINLAST 00237 | JBOX_TRANSPARENT 00238 // | JBOX_NOGROW 00239 | JBOX_GROWY 00240 // | JBOX_GROWBOTH 00241 // | JBOX_HILITE 00242 // | JBOX_BACKGROUND 00243 | JBOX_DRAWBACKGROUND 00244 // | JBOX_NOFLOATINSPECTOR 00245 // | JBOX_TEXTFIELD 00246 // | JBOX_MOUSEDRAGDELTA 00247 // | JBOX_TEXTFIELD 00248 ; 00249 00250 jbox_new((t_jbox *)x, boxflags, argc, argv); 00251 x->u_box.b_firstin = (void *)x; 00252 x->u_mousedowninside = x->u_mouseover = x->u_state = 0; 00253 x->u_out = intout((t_object *)x); 00254 attr_dictionary_process(x,d); 00255 jbox_ready((t_jbox *)x); 00256 return x; 00257 }
Copyright © 2008, Cycling '74