Rectangle imager(Image *i) { Point p1, p2; p1 = addpt(divpt(subpt(i->r.max, i->r.min), 2), i->r.min); p2 = addpt(divpt(subpt(screen->clipr.max, screen->clipr.min), 2), screen->clipr.min); return rectaddpt(i->r, subpt(p2, p1)); }
void qball(Rectangle r, Mouse *m, Quaternion *result, void (*redraw)(void), Quaternion *ap){ Quaternion q, down; Point rad; axis=ap; ctlcen=divpt(addpt(r.min, r.max), 2); rad=divpt(subpt(r.max, r.min), 2); ctlrad=(rad.x<rad.y?rad.x:rad.y)-BORDER; down=qinv(mouseq(m->xy)); q=*result; for(;;){ *m=emouse(); if(!m->buttons) break; *result=qmul(q, qmul(down, mouseq(m->xy))); (*redraw)(); } }
static void _bezsplinepts(Plist *l, Point *pt, int npt) { Point *p, *ep; Point a, b, c, d; int periodic; if(npt<3) return; ep = &pt[npt-3]; periodic = eqpt(pt[0], ep[2]); if(periodic){ a = divpt(addpt(ep[1], pt[0]), 2); b = divpt(addpt(ep[1], mulpt(pt[0], 5)), 6); c = divpt(addpt(mulpt(pt[0], 5), pt[1]), 6); d = divpt(addpt(pt[0], pt[1]), 2); bpts(l, a, b, c, d); } for(p=pt; p<=ep; p++){ if(p==pt && !periodic){ a = p[0]; b = divpt(addpt(p[0], mulpt(p[1], 2)), 3); } else{ a = divpt(addpt(p[0], p[1]), 2); b = divpt(addpt(p[0], mulpt(p[1], 5)), 6); } if(p==ep && !periodic){ c = divpt(addpt(mulpt(p[1], 2), p[2]), 3); d = p[2]; } else{ c = divpt(addpt(mulpt(p[1], 5), p[2]), 6); d = divpt(addpt(p[1], p[2]), 2); } bpts(l, a, b, c, d); } appendpt(l, d); }
void rest(void) { int i; Point pt; pt = divpt(subpt(pos, rboard.min), pcsz); for(i=0; i<N; i++) { pt.x += piece->d[i].x; pt.y += piece->d[i].y; board[pt.y][pt.x] = piece->tx+16; } }
/* * Convert cubic Bezier curve control points to polyline * vertices. Leaves the last vertex off, so you can continue * with another curve. */ static void bpts1(Plist *l, Point p0, Point p1, Point p2, Point p3, int scale) { Point p01, p12, p23, p012, p123, p0123; Point tp0, tp1, tp2, tp3; tp0=divpt(p0, scale); tp1=divpt(p1, scale); tp2=divpt(p2, scale); tp3=divpt(p3, scale); if(psdist(tp1, tp0, tp3)<=1 && psdist(tp2, tp0, tp3)<=1){ appendpt(l, tp0); appendpt(l, tp1); appendpt(l, tp2); } else{ /* * if scale factor is getting too big for comfort, * rescale now & concede the rounding error */ if(scale>(1<<12)){ p0=tp0; p1=tp1; p2=tp2; p3=tp3; scale=1; } p01=addpt(p0, p1); p12=addpt(p1, p2); p23=addpt(p2, p3); p012=addpt(p01, p12); p123=addpt(p12, p23); p0123=addpt(p012, p123); bpts1(l, mulpt(p0, 8), mulpt(p01, 4), mulpt(p012, 2), p0123, scale*8); bpts1(l, p0123, mulpt(p123, 2), mulpt(p23, 4), mulpt(p3, 8), scale*8); } }
static int psdist(Point p, Point a, Point b) { int num, den; p = subpt(p, a); b = subpt(b, a); num = p.x*b.x + p.y*b.y; if(num <= 0) return normsq(p); den = normsq(b); if(num >= den) return normsq(subpt(b, p)); return normsq(subpt(divpt(mulpt(b, num), den), p)); }
static void reshape(void) { // int dy12; // if (needresize) { // sqwid = Dx(screen->r) / (1 + bdp->cols + 1); // dy12 = Dy(screen->r) / (1 + bdp->rows + 1 + 2); // if (sqwid > dy12) // sqwid = dy12; // recompute(bdp, sqwid); // } sleep(1000); needresize = 0; cen = divpt(subpt(addpt(screen->r.min, screen->r.max), Pt(NLIFE * PX, NLIFE * PX)), 2); redraw(); flushimage(display, 1); }
void main(int argc, char *argv[]) { int delay = 1000; setrules(".d.d..b..d.d.d.d.d"); /* regular rules */ ARGBEGIN { case '3': setrules(".d.d.db.b..d.d.d.d"); break; /* 34-life */ case 'o': setrules(".d.d.db.b.b..d.d.d"); break; /* lineosc? */ case 'r': /* rules from cmdline */ setrules(EARGF(usage())); break; default: usage(); } ARGEND if (argc != 1) usage(); initdraw(g9err, 0, argv0); einit(Emouse|Ekeyboard); /* implies rawon() */ cen = divpt(subpt(addpt(screen->r.min, screen->r.max), Pt(NLIFE * PX, NLIFE * PX)), 2); box = allocimage(display, Rect(0, 0, BX, BX), RGB24, 1, DBlack); assert(box != nil); redraw(); readlife(argv[0]); do { flushimage(display, 1); idle(); sleep(delay); idle(); } while (generate()); exits(nil); }
void redraw(Image *screen) { static int tm, ntm; static Rectangle r; static Point c; static int rad; static Image *im; int i; int anghr, angmin; static Tm tms; static Tm ntms; ntm = time(0); if(ntm == tm && eqrect(screen->r, r)) return; ntms = *localtime(ntm); anghr = 90-(ntms.hour*5 + ntms.min/12)*6; angmin = 90-ntms.min*6; tm = ntm; tms = ntms; r = screen->r; c = divpt(addpt(r.min, r.max), 2); rad = Dx(r) < Dy(r) ? Dx(r) : Dy(r); rad /= 2; rad -= 8; draw(screen, screen->r, back, nil, ZP); for(i=0; i<12; i++) fillellipse(screen, circlept(c, rad, i*(360/12)), 2, 2, dots, ZP); line(screen, c, circlept(c, (rad*3)/4, angmin), 0, 0, 1, minhand, ZP); line(screen, c, circlept(c, rad/2, anghr), 0, 0, 1, hrhand, ZP); flushimage(display, 1); }
int menuhit(int but, Mousectl *mc, Menu *menu, Screen *scr) { int i, nitem, nitemdrawn, maxwid, lasti, off, noff, wid, screenitem; int scrolling; Rectangle r, menur, sc, textr, scrollr; Image *b, *save, *backup; Point pt; char *item; if(back == nil) menucolors(); sc = screen->clipr; replclipr(screen, 0, screen->r); maxwid = 0; for(nitem = 0; item = menu->item? menu->item[nitem] : (*menu->gen)(nitem); nitem++){ i = stringwidth(font, item); if(i > maxwid) maxwid = i; } if(menu->lasthit<0 || menu->lasthit>=nitem) menu->lasthit = 0; screenitem = (Dy(screen->r)-10)/(font->height+Vspacing); if(nitem>Maxunscroll || nitem>screenitem){ scrolling = 1; nitemdrawn = Nscroll; if(nitemdrawn > screenitem) nitemdrawn = screenitem; wid = maxwid + Gap + Scrollwid; off = menu->lasthit - nitemdrawn/2; if(off < 0) off = 0; if(off > nitem-nitemdrawn) off = nitem-nitemdrawn; lasti = menu->lasthit-off; }else{ scrolling = 0; nitemdrawn = nitem; wid = maxwid; off = 0; lasti = menu->lasthit; } r = insetrect(Rect(0, 0, wid, nitemdrawn*(font->height+Vspacing)), -Margin); r = rectsubpt(r, Pt(wid/2, lasti*(font->height+Vspacing)+font->height/2)); r = rectaddpt(r, mc->m.xy); pt = ZP; if(r.max.x>screen->r.max.x) pt.x = screen->r.max.x-r.max.x; if(r.max.y>screen->r.max.y) pt.y = screen->r.max.y-r.max.y; if(r.min.x<screen->r.min.x) pt.x = screen->r.min.x-r.min.x; if(r.min.y<screen->r.min.y) pt.y = screen->r.min.y-r.min.y; menur = rectaddpt(r, pt); textr.max.x = menur.max.x-Margin; textr.min.x = textr.max.x-maxwid; textr.min.y = menur.min.y+Margin; textr.max.y = textr.min.y + nitemdrawn*(font->height+Vspacing); if(scrolling){ scrollr = insetrect(menur, Border); scrollr.max.x = scrollr.min.x+Scrollwid; }else scrollr = Rect(0, 0, 0, 0); if(scr){ b = allocwindow(scr, menur, Refbackup, DWhite); if(b == nil) b = screen; backup = nil; }else{ b = screen; backup = allocimage(display, menur, screen->chan, 0, -1); if(backup) draw(backup, menur, screen, nil, menur.min); } draw(b, menur, back, nil, ZP); border(b, menur, Blackborder, bord, ZP); save = allocimage(display, menurect(textr, 0), screen->chan, 0, -1); r = menurect(textr, lasti); moveto(mc, divpt(addpt(r.min, r.max), 2)); menupaint(b, menu, textr, off, nitemdrawn); if(scrolling) menuscrollpaint(b, scrollr, off, nitem, nitemdrawn); while(mc->m.buttons & (1<<(but-1))){ lasti = menuscan(b, menu, but, mc, textr, off, lasti, save); if(lasti >= 0) break; while(!ptinrect(mc->m.xy, textr) && (mc->m.buttons & (1<<(but-1)))){ if(scrolling && ptinrect(mc->m.xy, scrollr)){ noff = ((mc->m.xy.y-scrollr.min.y)*nitem)/Dy(scrollr); noff -= nitemdrawn/2; if(noff < 0) noff = 0; if(noff > nitem-nitemdrawn) noff = nitem-nitemdrawn; if(noff != off){ off = noff; menupaint(b, menu, textr, off, nitemdrawn); menuscrollpaint(b, scrollr, off, nitem, nitemdrawn); } } readmouse(mc); } } if(b != screen) freeimage(b); if(backup){ draw(screen, menur, backup, nil, menur.min); freeimage(backup); } freeimage(save); replclipr(screen, 0, sc); flushimage(display, 1); if(lasti >= 0){ menu->lasthit = lasti+off; return menu->lasthit; } return -1; }
int menuhit(int but, Mouse *m, Menu *menu) { int i, nitem, nitemdrawn, maxwid, lasti, off, noff, wid, screenitem; bool scrolling; Rectangle r, menur, sc, textr, scrollr; Bitmap *b; Point pt; char *item; extern unsigned int cursor; unsigned int oldcursor = cursor; cursorswitch(ArrowCursor); sc = screen.clipr; clipr(&screen, screen.r); maxwid = 0; for(nitem = 0; (item = menu->item? menu->item[nitem] : (*menu->gen)(nitem)); nitem++){ i = strwidth(font, item); if(i > maxwid) maxwid = i; } if(menu->lasthit<0 || menu->lasthit>=nitem) menu->lasthit = 0; screenitem = (Dy(screen.r)-10)/(fontheight()+Vspacing); if(nitem>Maxunscroll || nitem>screenitem){ scrolling = true; nitemdrawn = Nscroll; if(nitemdrawn > screenitem) nitemdrawn = screenitem; wid = maxwid + Gap + Scrollwid; off = menu->lasthit - nitemdrawn/2; if(off < 0) off = 0; if(off > nitem-nitemdrawn) off = nitem-nitemdrawn; lasti = menu->lasthit-off; }else{ scrolling = false; nitemdrawn = nitem; wid = maxwid; off = 0; lasti = menu->lasthit; } r = inset(Rect(0, 0, wid, nitemdrawn*(fontheight()+Vspacing)), -Margin); r = rsubp(r, Pt(wid/2, lasti*(fontheight()+Vspacing)+fontheight()/2)); r = raddp(r, m->xy); pt = Pt(0, 0); if(r.max.x>screen.r.max.x) pt.x = screen.r.max.x-r.max.x; if(r.max.y>screen.r.max.y) pt.y = screen.r.max.y-r.max.y; if(r.min.x<screen.r.min.x) pt.x = screen.r.min.x-r.min.x; if(r.min.y<screen.r.min.y) pt.y = screen.r.min.y-r.min.y; menur = raddp(r, pt); textr.max.x = menur.max.x-Margin; textr.min.x = textr.max.x-maxwid; textr.min.y = menur.min.y+Margin; textr.max.y = textr.min.y + nitemdrawn*(fontheight()+Vspacing); if(scrolling){ scrollr = inset(menur, Border); scrollr.max.x = scrollr.min.x+Scrollwid; }else scrollr = Rect(0, 0, 0, 0); b = balloc(menur, screen.ldepth); if(b == 0) b = &screen; bitblt(b, menur.min, &screen, menur, S); bitblt(&screen, menur.min, &screen, menur, 0); border(&screen, menur, Blackborder, F, _bgpixel); r = menurect(textr, lasti); cursorset(divpt(add(r.min, r.max), 2)); menupaint(menu, textr, off, nitemdrawn); if(scrolling) menuscrollpaint(scrollr, off, nitem, nitemdrawn); r = menurect(textr, lasti); cursorset(divpt(add(r.min, r.max), 2)); menupaint(menu, textr, off, nitemdrawn); if(scrolling) menuscrollpaint(scrollr, off, nitem, nitemdrawn); while(m->buttons & (1<<(but-1))){ lasti = menuscan(but, m, textr, lasti); if(lasti >= 0) break; while(!ptinrect(m->xy, textr) && (m->buttons & (1<<(but-1)))){ if(scrolling && ptinrect(m->xy, scrollr)){ noff = ((m->xy.y-scrollr.min.y)*nitem)/Dy(scrollr); noff -= nitemdrawn/2; if(noff < 0) noff = 0; if(noff > nitem-nitemdrawn) noff = nitem-nitemdrawn; if(noff != off){ off = noff; menupaint(menu, textr, off, nitemdrawn); menuscrollpaint(scrollr, off, nitem, nitemdrawn); } } *m = emouse(); } } bitblt(&screen, menur.min, b, menur, S); if(b != &screen) bfree(b); clipr(&screen, sc); if(lasti >= 0){ menu->lasthit = lasti+off; return cursorswitch(oldcursor), menu->lasthit; } cursorswitch(oldcursor); return -1; }
int pl_hitpopup(Panel *g, Mouse *m){ Panel *p; Point d; Popup *pp; pp=g->data; if(g->state==UP){ switch(m->buttons&7){ case 0: p=g->child; break; case 1: p=pp->pop[0]; g->state=DOWN1; break; case 2: p=pp->pop[1]; g->state=DOWN2; break; case 4: p=pp->pop[2]; g->state=DOWN3; break; default: p=0; break; } if(p==0){ p=g->child; g->state=DOWN; } else if(g->state!=UP){ plpack(p, screen->clipr); if(p->lastmouse) d=subpt(m->xy, divpt(addpt(p->lastmouse->r.min, p->lastmouse->r.max), 2)); else d=subpt(m->xy, divpt(addpt(p->r.min, p->r.max), 2)); if(p->r.min.x+d.x<g->r.min.x) d.x=g->r.min.x-p->r.min.x; if(p->r.max.x+d.x>g->r.max.x) d.x=g->r.max.x-p->r.max.x; if(p->r.min.y+d.y<g->r.min.y) d.y=g->r.min.y-p->r.min.y; if(p->r.max.y+d.y>g->r.max.y) d.y=g->r.max.y-p->r.max.y; plmove(p, d); pp->save=allocimage(display, p->r, g->b->chan, 0, DNofill); if(pp->save!=0) draw(pp->save, p->r, g->b, 0, p->r.min); pl_invis(p, 0); pldraw(p, g->b); } } else{ switch(g->state){ default: SET(p); break; /* can't happen! */ case DOWN1: p=pp->pop[0]; break; case DOWN2: p=pp->pop[1]; break; case DOWN3: p=pp->pop[2]; break; case DOWN: p=g->child; break; } if((m->buttons&7)==0){ if(g->state!=DOWN){ if(pp->save!=0){ draw(g->b, p->r, pp->save, 0, p->r.min); flushimage(display, 1); freeimage(pp->save); pp->save=0; } pl_invis(p, 1); } g->state=UP; } } plmouse(p, m); if((m->buttons&7)==0) g->state=UP; return (m->buttons&7)!=0; }
void colmousebut(Column *c) { moveto(mousectl, divpt(addpt(c->tag.scrollr.min, c->tag.scrollr.max), 2)); }