Max 5 API Reference
00001 /** 00002 @file 00003 uisimp - a very simple ui object - step 4 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 typedef struct _uisimp 00017 { 00018 t_jbox j_box; 00019 long j_mouse_is_down; 00020 int j_mouse_counter; 00021 } t_uisimp; 00022 00023 void uisimp_assist(t_uisimp *x, void *b, long m, long a, char *s); 00024 t_uisimp* uisimp_new(t_symbol *s, long argc, t_atom *argv); 00025 void uisimp_int(t_uisimp *x, long n); 00026 void uisimp_free(t_uisimp *x); 00027 00028 void uisimp_paint(t_uisimp *x, t_object *view); 00029 00030 void uisimp_mousedrag(t_uisimp *x, t_object *patcherview, t_pt pt, long modifiers); 00031 void uisimp_mousedown(t_uisimp *x, t_object *patcherview, t_pt pt, long modifiers); 00032 void uisimp_mouseup(t_uisimp *x, t_object *patcherview, t_pt pt, long modifiers); 00033 00034 void uisimp_bang(t_uisimp *x); 00035 00036 int main(void) 00037 { 00038 t_class *c; 00039 00040 c = class_new("uisimp2", 00041 (method)uisimp_new, 00042 (method)uisimp_free, 00043 sizeof(t_uisimp), 00044 (method)NULL, 00045 A_GIMME, 00046 0L); 00047 00048 c->c_flags |= CLASS_FLAG_NEWDICTIONARY; 00049 00050 jbox_initclass(c, JBOX_COLOR | JBOX_FIXWIDTH | JBOX_FONTATTR); 00051 00052 class_addmethod(c, (method) uisimp_paint, "paint", A_CANT, 0); 00053 class_addmethod(c, (method) uisimp_int, "int", A_LONG, 0); 00054 00055 class_addmethod(c, (method) uisimp_mousedown, "mousedown", A_CANT, 0); 00056 class_addmethod(c, (method) uisimp_mousedrag, "mousedrag", A_CANT, 0); 00057 class_addmethod(c, (method) uisimp_mouseup, "mouseup", A_CANT, 0); 00058 00059 class_addmethod(c, (method) uisimp_assist, "assist", A_CANT, 0); 00060 class_addmethod(c, (method) jbox_notify, "notify", A_CANT, 0); 00061 00062 CLASS_ATTR_DEFAULT(c, "patching_rect", 0, "0 0 50 50"); 00063 CLASS_ATTR_DEFAULT(c, "color", 0, "0.8 0.5 0.2 1"); 00064 00065 s_uisimp_class = c; 00066 class_register(CLASS_BOX, s_uisimp_class); 00067 return 0; 00068 } 00069 00070 void uisimp_assist(t_uisimp *x, void *b, long m, long a, char *s) 00071 { 00072 if (m == ASSIST_INLET) 00073 sprintf(s,"bang Says \"bang\""); 00074 } 00075 00076 t_uisimp* uisimp_new(t_symbol *s, long argc, t_atom *argv) 00077 { 00078 t_uisimp* x = NULL; 00079 t_max_err err = MAX_ERR_GENERIC; 00080 t_dictionary *d; 00081 long flags; 00082 00083 if (!(d=object_dictionaryarg(argc,argv))) 00084 return NULL; 00085 00086 x = (t_uisimp*) object_alloc(s_uisimp_class); 00087 flags = 0 00088 | JBOX_DRAWFIRSTIN 00089 // | JBOX_NODRAWBOX 00090 | JBOX_DRAWINLAST 00091 | JBOX_TRANSPARENT 00092 // | JBOX_NOGROW 00093 // | JBOX_GROWY 00094 | JBOX_GROWBOTH 00095 // | JBOX_HILITE 00096 // | JBOX_BACKGROUND 00097 // | JBOX_TEXTFIELD 00098 | JBOX_DRAWBACKGROUND 00099 | JBOX_DEFAULTNAMES 00100 ; 00101 00102 err = jbox_new(&x->j_box, flags, argc, argv); 00103 00104 x->j_box.b_firstin = (t_object*) x; 00105 00106 attr_dictionary_process(x, d); 00107 jbox_ready(&x->j_box); 00108 00109 return x; 00110 } 00111 00112 void uisimp_int(t_uisimp *x, long n) 00113 { 00114 x->j_mouse_counter = n; 00115 jbox_redraw((t_jbox *)x); 00116 } 00117 00118 void uisimp_free(t_uisimp *x) 00119 { 00120 jbox_free(&x->j_box); 00121 } 00122 00123 void uisimp_paint(t_uisimp *x, t_object *view) 00124 { 00125 t_jgraphics *g; 00126 t_rect rect; 00127 t_jrgba rgba, textcolor; 00128 t_jfont *jf; 00129 t_jtextlayout *jtl; 00130 char text[16]; 00131 00132 g = (t_jgraphics*) patcherview_get_jgraphics(view); 00133 jbox_get_rect_for_view(&x->j_box.b_ob, view, &rect); 00134 00135 jgraphics_rectangle(g, 3, 3, rect.width-6., rect.height-6.); 00136 if (x->j_mouse_is_down) { 00137 jbox_get_color((t_object *)x, &rgba); 00138 jgraphics_set_source_jrgba(g, &rgba); 00139 } else 00140 jgraphics_set_source_rgba(g, 0, 0, 0, 1.0); 00141 jgraphics_fill(g); 00142 // draw counter 00143 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)); 00144 jtl = jtextlayout_create(); 00145 sprintf(text,"%d",x->j_mouse_counter); 00146 00147 jtextlayout_set(jtl, text, jf, 5, 5, rect.width - 10, rect.height- 10, JGRAPHICS_TEXT_JUSTIFICATION_CENTERED, JGRAPHICS_TEXTLAYOUT_NOWRAP); 00148 textcolor.red = textcolor.green = textcolor.blue = 1; 00149 textcolor.alpha = 1; 00150 jtextlayout_settextcolor(jtl, &textcolor); 00151 jtextlayout_draw(jtl, g); 00152 jtextlayout_destroy(jtl); 00153 jfont_destroy(jf); 00154 } 00155 00156 void uisimp_bang(t_uisimp *x) 00157 { 00158 object_post((t_object *)x, "bang!"); 00159 } 00160 00161 void uisimp_mousedrag(t_uisimp *x, t_object *patcherview, t_pt pt, long modifiers) 00162 { 00163 x->j_mouse_counter++; 00164 jbox_redraw((t_jbox *)x); 00165 } 00166 00167 void uisimp_mousedown(t_uisimp *x, t_object *patcherview, t_pt pt, long modifiers) 00168 { 00169 x->j_mouse_is_down = true; 00170 x->j_mouse_counter = 0; 00171 jbox_redraw((t_jbox *)x); 00172 } 00173 00174 void uisimp_mouseup(t_uisimp *x, t_object *patcherview, t_pt pt, long modifiers) 00175 { 00176 x->j_mouse_is_down = false; 00177 jbox_redraw((t_jbox *)x); 00178 }
Copyright © 2008, Cycling '74