ref: 4a2e2bfe6c3993ed29c593adfb9069f7c50b9ced
parent: b69fdfbf30beb998c1817784e748ae78806becf8
author: rodri <rgl@antares-labs.eu>
date: Wed Jun 15 15:59:48 EDT 2022
began work on a VModel editor.
--- /dev/null
+++ b/vmodeled/main.c
@@ -1,0 +1,142 @@
+#include <u.h>
+#include <libc.h>
+#include <thread.h>
+#include <draw.h>
+#include <mouse.h>
+#include <keyboard.h>
+#include "../libgeometry/geometry.h"
+
+RFrame worldrf;
+
+void resized(void);
+
+Point
+toscreen(Point2 p)
+{
+ p = invrframexform(p, worldrf);
+ return Pt(p.x,p.y);
+}
+
+Point2
+fromscreen(Point p)
+{
+ return rframexform(Pt2(p.x,p.y,1), worldrf);
+}
+
+void
+redraw(void)
+{
+ lockdisplay(display);
+ draw(screen, screen->r, display->black, nil, ZP);
+ line(screen, toscreen(Pt2(0,512,1)), toscreen(Pt2(0,-512,1)), 0, 0, 0, display->white, ZP);
+ line(screen, toscreen(Pt2(512,0,1)), toscreen(Pt2(-512,0,1)), 0, 0, 0, display->white, ZP);
+ flushimage(display, 1);
+ unlockdisplay(display);
+}
+
+void
+rmb(Mousectl *, Keyboardctl *)
+{
+}
+
+void
+lmb(Mousectl *mc, Keyboardctl *)
+{
+ Point2 mpos;
+
+ mpos = fromscreen(mc->xy);
+ fprint(2, "%v\n", mpos);
+}
+
+void
+mouse(Mousectl *mc, Keyboardctl *kc)
+{
+ if((mc->buttons&1) != 0)
+ lmb(mc, kc);
+ if((mc->buttons&4) != 0)
+ rmb(mc, kc);
+}
+
+void
+key(Rune r)
+{
+ switch(r){
+ case Kdel:
+ case 'q':
+ threadexitsall(nil);
+ }
+}
+
+void
+usage(void)
+{
+ fprint(2, "usage: %s\n", argv0);
+ exits("usage");
+}
+
+void
+threadmain(int argc, char *argv[])
+{
+ Mousectl *mc;
+ Keyboardctl *kc;
+ Rune r;
+
+ GEOMfmtinstall();
+ ARGBEGIN{
+ default: usage();
+ }ARGEND;
+ if(argc > 0)
+ usage();
+
+ if(newwindow(nil) < 0)
+ sysfatal("newwindow: %r");
+ if(initdraw(nil, nil, nil) < 0)
+ sysfatal("initdraw: %r");
+ if((mc = initmouse(nil, screen)) == nil)
+ sysfatal("initmouse: %r");
+ if((kc = initkeyboard(nil)) == nil)
+ sysfatal("initkeyboard: %r");
+
+ worldrf.p = Pt2(screen->r.min.x+Dx(screen->r)/2,screen->r.max.y-Dy(screen->r)/2,1);
+ worldrf.bx = Vec2(1, 0);
+ worldrf.by = Vec2(0,-1);
+
+ display->locking = 1;
+ unlockdisplay(display);
+ redraw();
+
+ for(;;){
+ enum { MOUSE, RESIZE, KEYBOARD };
+ Alt a[] = {
+ {mc->c, &mc->Mouse, CHANRCV},
+ {mc->resizec, nil, CHANRCV},
+ {kc->c, &r, CHANRCV},
+ {nil, nil, CHANEND}
+ };
+
+ switch(alt(a)){
+ case MOUSE:
+ mouse(mc, kc);
+ break;
+ case RESIZE:
+ resized();
+ break;
+ case KEYBOARD:
+ key(r);
+ break;
+ }
+
+ redraw();
+ }
+}
+
+void
+resized(void)
+{
+ lockdisplay(display);
+ if(getwindow(display, Refnone) < 0)
+ sysfatal("couldn't resize");
+ unlockdisplay(display);
+ worldrf.p = Pt2(screen->r.min.x+Dx(screen->r)/2,screen->r.max.y-Dy(screen->r)/2,1);
+ redraw();
+}
--- /dev/null
+++ b/vmodeled/mkfile
@@ -1,0 +1,24 @@
+</$objtype/mkfile
+
+BIN=/$objtype/bin/musw
+TARG=vmodeled
+OFILES=\
+ main.$O\
+
+HFILES=\
+ ../libgeometry/geometry.h\
+
+LIB=\
+ ../libgeometry/libgeometry.a$O\
+
+CFLAGS=$CFLAGS
+
+</sys/src/cmd/mkone
+
+../libgeometry/libgeometry.a$O:
+ cd ../libgeometry
+ mk install
+
+clean nuke:V:
+ rm -f *.[$OS] [$OS].??* $TARG
+ @{cd ../libgeometry; mk $target}