Max 5 API Reference
00001 /** 00002 @file 00003 uisimp - a very simple ui object - step 7 00004 00005 @ingroup examples 00006 */ 00007 00008 #include "ext.h" 00009 #include "ext_obex.h" 00010 #include "jpatcher_api.h" 00011 #include "jgraphics.h" 00012 00013 00014 t_class *s_uisimp_class = 0; 00015 00016 enum { 00017 EXAMP_SQUARE = 0, 00018 EXAMP_CIRCLE, 00019 EXAMP_ANGLE 00020 }; 00021 00022 typedef struct _uisimp 00023 { 00024 t_jbox j_box; 00025 long j_mouse_is_down; 00026 int j_mouse_counter; 00027 long j_inset; 00028 char j_reset; 00029 long j_shape; 00030 char j_has_focus; 00031 t_pt j_absolute_position; 00032 } t_uisimp; 00033 00034 00035 00036 void uisimp_assist(t_uisimp *x, void *b, long m, long a, char *s); 00037 t_uisimp* uisimp_new(t_symbol *s, long argc, t_atom *argv); 00038 void uisimp_int(t_uisimp *x, long n); 00039 void uisimp_free(t_uisimp *x); 00040 00041 void uisimp_paint(t_uisimp *x, t_object *view); 00042 00043 void uisimp_mousedragdelta(t_uisimp *x, t_object *patcherview, t_pt pt, long modifiers); 00044 void uisimp_mousedown(t_uisimp *x, t_object *patcherview, t_pt pt, long modifiers); 00045 void uisimp_mouseup(t_uisimp *x, t_object *patcherview, t_pt pt, long modifiers); 00046 void uisimp_getdrawparams(t_uisimp *x, t_object *patcherview, t_jboxdrawparams *params); 00047 long uisimp_key(t_uisimp *x, t_object *patcherview, long keycode, long modifiers, long textcharacter); 00048 void uisimp_focusgained(t_uisimp *x, t_object *patcherview); 00049 void uisimp_focuslost(t_uisimp *x, t_object *patcherview); 00050 00051 void uisimp_bang(t_uisimp *x); 00052 00053 int main(void) 00054 { 00055 t_class *c; 00056 00057 c = class_new("uisimp5", 00058 (method)uisimp_new, 00059 (method)uisimp_free, 00060 sizeof(t_uisimp), 00061 (method)NULL, 00062 A_GIMME, 00063 0L); 00064 00065 c->c_flags |= CLASS_FLAG_NEWDICTIONARY; 00066 00067 jbox_initclass(c, JBOX_COLOR | JBOX_FIXWIDTH | JBOX_FONTATTR); 00068 00069 class_addmethod(c, (method) uisimp_paint, "paint", A_CANT, 0); 00070 class_addmethod(c, (method) uisimp_int, "int", A_LONG, 0); 00071 00072 class_addmethod(c, (method) uisimp_mousedown, "mousedown", A_CANT, 0); 00073 class_addmethod(c, (method) uisimp_mousedragdelta, "mousedragdelta", A_CANT, 0); 00074 class_addmethod(c, (method) uisimp_mouseup, "mouseup", A_CANT, 0); 00075 class_addmethod(c, (method) uisimp_getdrawparams, "getdrawparams", A_CANT, 0); 00076 00077 class_addmethod(c, (method) uisimp_key, "key", A_CANT, 0); 00078 class_addmethod(c, (method) uisimp_focusgained, "focusgained", A_CANT, 0); 00079 class_addmethod(c, (method) uisimp_focuslost, "focuslost", A_CANT, 0); 00080 00081 class_addmethod(c, (method) uisimp_assist, "assist", A_CANT, 0); 00082 class_addmethod(c, (method) jbox_notify, "notify", A_CANT, 0); 00083 00084 CLASS_ATTR_DEFAULT(c, "patching_rect", 0, "0 0 60 60"); 00085 CLASS_ATTR_DEFAULT(c, "color", 0, "0.8 0.5 0.2 1"); 00086 00087 CLASS_ATTR_LONG(c,"inset",0, t_uisimp, j_inset); 00088 CLASS_ATTR_LABEL(c,"inset",0,"Filled Area Inset"); 00089 CLASS_ATTR_CATEGORY(c,"inset",0,"Tweaks"); 00090 CLASS_ATTR_DEFAULT_SAVE_PAINT(c,"inset",0,"3"); 00091 00092 CLASS_ATTR_CHAR(c,"reset",0, t_uisimp, j_reset); 00093 CLASS_ATTR_STYLE_LABEL(c,"reset",0,"onoff","Reset Counter on Click"); 00094 CLASS_ATTR_CATEGORY(c,"reset",0,"Tweaks"); 00095 CLASS_ATTR_DEFAULT_SAVE(c,"reset",0,"1"); 00096 00097 CLASS_ATTR_LONG(c,"shape",0, t_uisimp, j_shape); 00098 CLASS_ATTR_LABEL(c,"shape", 0, "Fill Shape"); 00099 CLASS_ATTR_CATEGORY(c,"shape",0,"Shape"); 00100 CLASS_ATTR_ENUMINDEX(c,"shape", 0, "Square Circle Angle"); 00101 CLASS_ATTR_DEFAULT_SAVE_PAINT(c,"shape",0,"0"); 00102 00103 s_uisimp_class = c; 00104 class_register(CLASS_BOX, s_uisimp_class); 00105 return 0; 00106 } 00107 00108 void uisimp_assist(t_uisimp *x, void *b, long m, long a, char *s) 00109 { 00110 if (m == ASSIST_INLET) 00111 sprintf(s,"bang Says \"bang\""); 00112 } 00113 00114 00115 t_uisimp* uisimp_new(t_symbol *s, long argc, t_atom *argv) 00116 { 00117 t_uisimp* x = NULL; 00118 t_max_err err = MAX_ERR_GENERIC; 00119 t_dictionary *d; 00120 long flags; 00121 00122 if (!(d=object_dictionaryarg(argc,argv))) 00123 return NULL; 00124 00125 x = (t_uisimp*) object_alloc(s_uisimp_class); 00126 flags = 0 00127 | JBOX_DRAWFIRSTIN 00128 // | JBOX_NODRAWBOX 00129 | JBOX_DRAWINLAST 00130 | JBOX_TRANSPARENT 00131 // | JBOX_NOGROW 00132 // | JBOX_GROWY 00133 | JBOX_GROWBOTH 00134 | JBOX_HILITE 00135 // | JBOX_BACKGROUND 00136 // | JBOX_TEXTFIELD 00137 | JBOX_DRAWBACKGROUND 00138 | JBOX_DEFAULTNAMES 00139 | JBOX_MOUSEDRAGDELTA 00140 ; 00141 00142 err = jbox_new(&x->j_box, flags, argc, argv); 00143 00144 x->j_box.b_firstin = (t_object*) x; 00145 00146 attr_dictionary_process(x, d); 00147 jbox_ready(&x->j_box); 00148 00149 return x; 00150 } 00151 00152 void uisimp_int(t_uisimp *x, long n) 00153 { 00154 x->j_mouse_counter = n; 00155 jbox_redraw((t_jbox *)x); 00156 } 00157 00158 void uisimp_free(t_uisimp *x) 00159 { 00160 jbox_free(&x->j_box); 00161 } 00162 00163 void uisimp_paint(t_uisimp *x, t_object *view) 00164 { 00165 t_jgraphics *g; 00166 t_rect rect; 00167 t_jrgba rgba, textcolor; 00168 t_jfont *jf; 00169 t_jtextlayout *jtl; 00170 char text[16]; 00171 long inset = x->j_inset; 00172 00173 g = (t_jgraphics*) patcherview_get_jgraphics(view); 00174 jbox_get_rect_for_view(&x->j_box.b_ob, view, &rect); 00175 00176 if (x->j_shape == EXAMP_SQUARE) 00177 jgraphics_rectangle(g, inset, inset, rect.width - (inset * 2), rect.height - (inset * 2)); 00178 else if (x->j_shape == EXAMP_CIRCLE) { 00179 jgraphics_arc(g, rect.width * .5, rect.height * .5, (rect.width * .5) - (inset * 2), 0, JGRAPHICS_2PI); 00180 jgraphics_close_path(g); 00181 } else if (x->j_shape == EXAMP_ANGLE) { 00182 jgraphics_move_to(g, inset * 2, inset); 00183 jgraphics_line_to(g, rect.width - (inset * 2), inset); 00184 jgraphics_line_to(g, rect.width - (inset * 2), inset * 2); 00185 jgraphics_line_to(g, rect.width - (inset * 2), rect.height - (inset * 2)); 00186 jgraphics_line_to(g, rect.width - (inset * 2), rect.height - inset); 00187 jgraphics_line_to(g, inset * 2, rect.height - (inset * 2)); 00188 jgraphics_line_to(g, inset, rect.height - (inset * 3)); 00189 jgraphics_line_to(g, inset, inset * 2); 00190 jgraphics_line_to(g, inset * 2, inset); 00191 jgraphics_close_path(g); 00192 } 00193 00194 if (x->j_mouse_is_down) { 00195 jbox_get_color((t_object *)x, &rgba); 00196 jgraphics_set_source_jrgba(g, &rgba); 00197 } else 00198 jgraphics_set_source_rgba(g, 0, 0, 0, 1.0); 00199 jgraphics_fill(g); 00200 // draw counter 00201 jf = jfont_create(jbox_get_fontname((t_object *)x)->s_name, jbox_get_font_slant((t_object *)x), jbox_get_font_weight((t_object *)x), jbox_get_fontsize((t_object *)x)); 00202 jtl = jtextlayout_create(); 00203 sprintf(text,"%d",x->j_mouse_counter); 00204 00205 jtextlayout_set(jtl, text, jf, 5, 5, rect.width - 10, rect.height- 10, JGRAPHICS_TEXT_JUSTIFICATION_CENTERED, JGRAPHICS_TEXTLAYOUT_NOWRAP); 00206 textcolor.red = textcolor.green = textcolor.blue = 1; 00207 textcolor.alpha = 1; 00208 jtextlayout_settextcolor(jtl, &textcolor); 00209 jtextlayout_draw(jtl, g); 00210 jtextlayout_destroy(jtl); 00211 jfont_destroy(jf); 00212 } 00213 00214 void uisimp_getdrawparams(t_uisimp *x, t_object *patcherview, t_jboxdrawparams *params) 00215 { 00216 if (x->j_has_focus) { 00217 params->d_bordercolor.red = 1; 00218 params->d_bordercolor.green = 0; 00219 params->d_bordercolor.blue = 0; 00220 params->d_bordercolor.alpha = 1; 00221 params->d_borderthickness = 2.; 00222 } 00223 } 00224 00225 void uisimp_bang(t_uisimp *x) 00226 { 00227 object_post((t_object *)x, "bang!"); 00228 } 00229 00230 void uisimp_mousedragdelta(t_uisimp *x, t_object *patcherview, t_pt pt, long modifiers) 00231 { 00232 t_rect rect; 00233 jbox_get_rect_for_view((t_object *)x, patcherview, &rect); 00234 rect.x += pt.x; 00235 rect.y += pt.y; 00236 jbox_set_rect_for_view((t_object *)x, patcherview, &rect); 00237 } 00238 00239 void uisimp_mousedown(t_uisimp *x, t_object *patcherview, t_pt pt, long modifiers) 00240 { 00241 x->j_absolute_position = pt; 00242 00243 x->j_mouse_is_down = true; 00244 if (x->j_reset) 00245 x->j_mouse_counter = 0; 00246 jbox_redraw((t_jbox *)x); 00247 } 00248 00249 void uisimp_mouseup(t_uisimp *x, t_object *patcherview, t_pt pt, long modifiers) 00250 { 00251 t_rect rect; 00252 jbox_get_rect_for_view((t_object *)x, patcherview, &rect); 00253 jmouse_setposition_view(patcherview, rect.x + x->j_absolute_position.x, rect.y + x->j_absolute_position.y); 00254 x->j_mouse_is_down = false; 00255 jbox_redraw((t_jbox *)x); 00256 } 00257 00258 long uisimp_key(t_uisimp *x, t_object *patcherview, long keycode, long modifiers, long textcharacter) 00259 { 00260 switch (textcharacter) { 00261 case '+': 00262 x->j_mouse_counter++; 00263 jbox_redraw((t_jbox *)x); 00264 break; 00265 case '-': 00266 x->j_mouse_counter--; 00267 jbox_redraw((t_jbox *)x); 00268 break; 00269 } 00270 00271 return 0; // returns 1 if you want to filter it from the key object 00272 } 00273 00274 void uisimp_focusgained(t_uisimp *x, t_object *patcherview) 00275 { 00276 post("You can now press \"+\" or \"-\""); 00277 x->j_has_focus = true; 00278 jbox_redraw((t_jbox *)x); 00279 } 00280 00281 void uisimp_focuslost(t_uisimp *x, t_object *patcherview) 00282 { 00283 post("You have lost the keyboard focus."); 00284 x->j_has_focus = false; 00285 jbox_redraw((t_jbox *)x); 00286 }
Copyright © 2008, Cycling '74