Max 5 API Reference
00001 /** 00002 @file 00003 uisimp - a very simple ui object - step 6 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 t_pt j_absolute_position; 00031 } t_uisimp; 00032 00033 00034 00035 void uisimp_assist(t_uisimp *x, void *b, long m, long a, char *s); 00036 t_uisimp* uisimp_new(t_symbol *s, long argc, t_atom *argv); 00037 void uisimp_int(t_uisimp *x, long n); 00038 void uisimp_free(t_uisimp *x); 00039 00040 void uisimp_paint(t_uisimp *x, t_object *view); 00041 00042 void uisimp_mousedragdelta(t_uisimp *x, t_object *patcherview, t_pt pt, long modifiers); 00043 void uisimp_mousedown(t_uisimp *x, t_object *patcherview, t_pt pt, long modifiers); 00044 void uisimp_mouseup(t_uisimp *x, t_object *patcherview, t_pt pt, long modifiers); 00045 00046 int main(void) 00047 { 00048 t_class *c; 00049 00050 c = class_new("uisimp4", 00051 (method)uisimp_new, 00052 (method)uisimp_free, 00053 sizeof(t_uisimp), 00054 (method)NULL, 00055 A_GIMME, 00056 0L); 00057 00058 c->c_flags |= CLASS_FLAG_NEWDICTIONARY; 00059 00060 jbox_initclass(c, JBOX_COLOR | JBOX_FIXWIDTH | JBOX_FONTATTR); 00061 00062 class_addmethod(c, (method) uisimp_paint, "paint", A_CANT, 0); 00063 class_addmethod(c, (method) uisimp_int, "int", A_LONG, 0); 00064 00065 class_addmethod(c, (method) uisimp_mousedown, "mousedown", A_CANT, 0); 00066 class_addmethod(c, (method) uisimp_mousedragdelta, "mousedragdelta", A_CANT, 0); 00067 class_addmethod(c, (method) uisimp_mouseup, "mouseup", A_CANT, 0); 00068 00069 class_addmethod(c, (method) uisimp_assist, "assist", A_CANT, 0); 00070 class_addmethod(c, (method) jbox_notify, "notify", A_CANT, 0); 00071 00072 CLASS_ATTR_DEFAULT(c, "patching_rect", 0, "0 0 60 60"); 00073 CLASS_ATTR_DEFAULT(c, "color", 0, "0.8 0.5 0.2 1"); 00074 00075 CLASS_ATTR_LONG(c,"inset",0, t_uisimp, j_inset); 00076 CLASS_ATTR_LABEL(c,"inset",0,"Filled Area Inset"); 00077 CLASS_ATTR_CATEGORY(c,"inset",0,"Tweaks"); 00078 CLASS_ATTR_DEFAULT_SAVE_PAINT(c,"inset",0,"3"); 00079 00080 CLASS_ATTR_CHAR(c,"reset",0, t_uisimp, j_reset); 00081 CLASS_ATTR_STYLE_LABEL(c,"reset",0,"onoff","Reset Counter on Click"); 00082 CLASS_ATTR_CATEGORY(c,"reset",0,"Tweaks"); 00083 CLASS_ATTR_DEFAULT_SAVE(c,"reset",0,"1"); 00084 00085 CLASS_ATTR_LONG(c,"shape",0, t_uisimp, j_shape); 00086 CLASS_ATTR_LABEL(c,"shape", 0, "Fill Shape"); 00087 CLASS_ATTR_CATEGORY(c,"shape",0,"Shape"); 00088 CLASS_ATTR_ENUMINDEX(c,"shape", 0, "Square Circle Angle"); 00089 CLASS_ATTR_DEFAULT_SAVE_PAINT(c,"shape",0,"0"); 00090 00091 s_uisimp_class = c; 00092 class_register(CLASS_BOX, s_uisimp_class); 00093 return 0; 00094 } 00095 00096 void uisimp_assist(t_uisimp *x, void *b, long m, long a, char *s) 00097 { 00098 if (m == ASSIST_INLET) 00099 sprintf(s,"bang Says \"bang\""); 00100 } 00101 00102 t_uisimp* uisimp_new(t_symbol *s, long argc, t_atom *argv) 00103 { 00104 t_uisimp* x = NULL; 00105 t_max_err err = MAX_ERR_GENERIC; 00106 t_dictionary *d; 00107 long flags; 00108 00109 if (!(d=object_dictionaryarg(argc,argv))) 00110 return NULL; 00111 00112 x = (t_uisimp*) object_alloc(s_uisimp_class); 00113 flags = 0 00114 | JBOX_DRAWFIRSTIN 00115 // | JBOX_NODRAWBOX 00116 | JBOX_DRAWINLAST 00117 | JBOX_TRANSPARENT 00118 // | JBOX_NOGROW 00119 // | JBOX_GROWY 00120 | JBOX_GROWBOTH 00121 // | JBOX_HILITE 00122 // | JBOX_BACKGROUND 00123 // | JBOX_TEXTFIELD 00124 | JBOX_DRAWBACKGROUND 00125 | JBOX_DEFAULTNAMES 00126 | JBOX_MOUSEDRAGDELTA 00127 ; 00128 00129 err = jbox_new(&x->j_box, flags, argc, argv); 00130 00131 x->j_box.b_firstin = (t_object*) x; 00132 00133 attr_dictionary_process(x, d); 00134 jbox_ready(&x->j_box); 00135 00136 return x; 00137 } 00138 00139 void uisimp_int(t_uisimp *x, long n) 00140 { 00141 x->j_mouse_counter = n; 00142 jbox_redraw((t_jbox *)x); 00143 } 00144 00145 void uisimp_free(t_uisimp *x) 00146 { 00147 jbox_free(&x->j_box); 00148 } 00149 00150 void uisimp_paint(t_uisimp *x, t_object *view) 00151 { 00152 t_jgraphics *g; 00153 t_rect rect; 00154 t_jrgba rgba, textcolor; 00155 t_jfont *jf; 00156 t_jtextlayout *jtl; 00157 char text[16]; 00158 long inset = x->j_inset; 00159 00160 g = (t_jgraphics*) patcherview_get_jgraphics(view); 00161 jbox_get_rect_for_view(&x->j_box.b_ob, view, &rect); 00162 00163 if (x->j_shape == EXAMP_SQUARE) 00164 jgraphics_rectangle(g, inset, inset, rect.width - (inset * 2), rect.height - (inset * 2)); 00165 else if (x->j_shape == EXAMP_CIRCLE) { 00166 jgraphics_arc(g, rect.width * .5, rect.height * .5, (rect.width * .5) - (inset * 2), 0, JGRAPHICS_2PI); 00167 jgraphics_close_path(g); 00168 } else if (x->j_shape == EXAMP_ANGLE) { 00169 jgraphics_move_to(g, inset * 2, inset); 00170 jgraphics_line_to(g, rect.width - (inset * 2), inset); 00171 jgraphics_line_to(g, rect.width - (inset * 2), inset * 2); 00172 jgraphics_line_to(g, rect.width - (inset * 2), rect.height - (inset * 2)); 00173 jgraphics_line_to(g, rect.width - (inset * 2), rect.height - inset); 00174 jgraphics_line_to(g, inset * 2, rect.height - (inset * 2)); 00175 jgraphics_line_to(g, inset, rect.height - (inset * 3)); 00176 jgraphics_line_to(g, inset, inset * 2); 00177 jgraphics_line_to(g, inset * 2, inset); 00178 jgraphics_close_path(g); 00179 } 00180 00181 if (x->j_mouse_is_down) { 00182 jbox_get_color((t_object *)x, &rgba); 00183 jgraphics_set_source_jrgba(g, &rgba); 00184 } else 00185 jgraphics_set_source_rgba(g, 0, 0, 0, 1.0); 00186 jgraphics_fill(g); 00187 // draw counter 00188 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)); 00189 jtl = jtextlayout_create(); 00190 sprintf(text,"%d",x->j_mouse_counter); 00191 00192 jtextlayout_set(jtl, text, jf, 5, 5, rect.width - 10, rect.height- 10, JGRAPHICS_TEXT_JUSTIFICATION_CENTERED, JGRAPHICS_TEXTLAYOUT_NOWRAP); 00193 textcolor.red = textcolor.green = textcolor.blue = 1; 00194 textcolor.alpha = 1; 00195 jtextlayout_settextcolor(jtl, &textcolor); 00196 jtextlayout_draw(jtl, g); 00197 jtextlayout_destroy(jtl); 00198 jfont_destroy(jf); 00199 } 00200 00201 void uisimp_mousedragdelta(t_uisimp *x, t_object *patcherview, t_pt pt, long modifiers) 00202 { 00203 t_rect rect; 00204 jbox_get_rect_for_view((t_object *)x, patcherview, &rect); 00205 rect.x += pt.x; 00206 rect.y += pt.y; 00207 jbox_set_rect_for_view((t_object *)x, patcherview, &rect); 00208 } 00209 00210 void uisimp_mousedown(t_uisimp *x, t_object *patcherview, t_pt pt, long modifiers) 00211 { 00212 x->j_absolute_position = pt; 00213 00214 x->j_mouse_is_down = true; 00215 if (x->j_reset) 00216 x->j_mouse_counter = 0; 00217 jbox_redraw((t_jbox *)x); 00218 } 00219 00220 void uisimp_mouseup(t_uisimp *x, t_object *patcherview, t_pt pt, long modifiers) 00221 { 00222 t_rect rect; 00223 jbox_get_rect_for_view((t_object *)x, patcherview, &rect); 00224 jmouse_setposition_view(patcherview, rect.x + x->j_absolute_position.x, rect.y + x->j_absolute_position.y); 00225 x->j_mouse_is_down = false; 00226 jbox_redraw((t_jbox *)x); 00227 }
Copyright © 2008, Cycling '74