shithub: neindaw

ref: fdd7d0be76013a747dd2c9f3bf73310ffc0f0705
dir: /cfg/cfg.c/

View raw version
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <mouse.h>
#include <keyboard.h>
#include <thread.h>
#include "common.h"
#include "nk.h"

typedef struct UI UI;

struct UI {
	UItype type;
	const char *label;
	int index;

	double value;
	double init;
	double min;
	double max;
	double step;

	int ctl;

	char *path;
	UI *child;
	int numchild;
};

int mainstacksize = 32768;

static Mousectl *mctl;
static Keyboardctl *kctl;
static Image *cola, *colb;
static Font *f;
static char **dirs;
static UI topui;
static struct nk_context nk;

static void
usage(void)
{
	print("usage: %s [DIR]...\n", argv0);
	threadexitsall("usage");
}

void
threadmain(int argc, char **argv)
{
	Rune key;
	Mouse m;
	Alt a[] = {
		{ nil, &m, CHANRCV },
		{ nil, nil, CHANRCV },
		{ nil, &key, CHANRCV },
		{ nil, nil, CHANEND },
	};
	char *curdir[] = {".", nil};
	int oldbuttons, x, entering;

	ARGBEGIN{
	default:
		usage();
	}ARGEND

	dirs = *argv == nil ? curdir : argv;
	memset(&topui, 0, sizeof(topui));
	topui.type = -1; /* that one is special */

	if (initdraw(nil, nil, "daw/cfg") < 0)
		sysfatal("initdraw: %r");
	if ((mctl = initmouse(nil, screen)) == nil)
		sysfatal("initmouse: %r");
	if ((kctl = initkeyboard(nil)) == nil)
		sysfatal("initkeyboard: %r");

	a[0].c = mctl->c;
	a[1].c = mctl->resizec;
	a[2].c = kctl->c;

	f = display->defaultfont;
	cola = display->white;
	colb = display->black;
	srand(time(0));
	threadsetname("daw/cfg");

	if (!nk_init_p9(&nk))
		sysfatal("nk_init_p9 failed");

	oldbuttons = 0;
	entering = 1;
	for (;;) {
		nk_glyph g;
		enum nk_keys nkey;

		nk_input_begin(&nk);

		if (entering == 0) {
			switch (alt(a)) {
			case 0: /* mouse */
				m.xy.x -= screen->r.min.x;
				m.xy.y -= screen->r.min.y;
				nk_input_motion(&nk, m.xy.x, m.xy.y);
				if ((x = (m.buttons & 1)) != (oldbuttons & 1))
					nk_input_button(&nk, NK_BUTTON_LEFT, m.xy.x, m.xy.y, x);
				else if ((x = (m.buttons & 2)) != (oldbuttons & 2))
					nk_input_button(&nk, NK_BUTTON_MIDDLE, m.xy.x, m.xy.y, x);
				else if ((x = (m.buttons & 4)) != (oldbuttons & 4))
					nk_input_button(&nk, NK_BUTTON_RIGHT, m.xy.x, m.xy.y, x);
				oldbuttons = m.buttons;
				break;

			case 1: /* resize */
				getwindow(display, Refnone);
				break;

			case 2: /* keyboard */
				nkey = -1;
				switch (key) {
				case 'q': goto end;
				case Kbs: nkey = NK_KEY_BACKSPACE; break;
				case Kdel: nkey = NK_KEY_DEL; break;
				case Kleft: nkey = NK_KEY_LEFT; break;
				case Kright: nkey = NK_KEY_RIGHT; break;
				case '\n': nkey = NK_KEY_ENTER; break;
				default:
					if (runetochar(g, &key) > 0)
						nk_input_glyph(&nk, g);
					break;
				}
				if (nkey >= 0) {
					nk_input_key(&nk, nkey, 1);
					nk_input_key(&nk, nkey, 0);
				}
			}
		} else {
			entering = 0;
		}

		nk_input_end(&nk);

		/* that's just a test */
		if (nk_begin_titled(&nk, "daw/cfg", "daw/cfg", nk_rect(0, 0, Dx(screen->r), Dy(screen->r)), 0)) {
			static int d = 20;
			nk_layout_row_template_begin(&nk, 0);
			nk_layout_row_template_push_static(&nk, stringwidth(display->defaultfont, "Some kind of slider?"));
			nk_layout_row_template_push_variable(&nk, 100);
			nk_layout_row_template_push_static(&nk, stringwidth(display->defaultfont, "Button")*2);
			nk_layout_row_template_end(&nk);
			nk_label(&nk, "Some kind of slider?", NK_TEXT_LEFT);
			nk_slider_int(&nk, 0, &d, 100, 1);
			nk_button_label(&nk, "Button");
		}
		nk_end(&nk);

		nk_redraw_p9(&nk, screen);
		nk_clear(&nk);
	}

end:
	closemouse(mctl);
	closekeyboard(kctl);
	threadexitsall(nil);
}