You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

886 lines
27KB

  1. //
  2. // based on NanoVG's example code by Mikko Mononen
  3. #include <stdio.h>
  4. #ifdef NANOVG_GLEW
  5. # include <GL/glew.h>
  6. #endif
  7. #ifdef __APPLE__
  8. # define GLFW_INCLUDE_GLCOREARB
  9. #endif
  10. #include <GLFW/glfw3.h>
  11. #include "nanovg.h"
  12. #define NANOVG_GL3_IMPLEMENTATION
  13. #include "nanovg_gl.h"
  14. #define BLENDISH_IMPLEMENTATION
  15. #include "blendish.h"
  16. #define OUI_IMPLEMENTATION
  17. #include "oui.h"
  18. ////////////////////////////////////////////////////////////////////////////////
  19. typedef enum {
  20. // label
  21. ST_LABEL = 0,
  22. // button
  23. ST_BUTTON = 1,
  24. // radio button
  25. ST_RADIO = 2,
  26. // progress slider
  27. ST_SLIDER = 3,
  28. // column
  29. ST_COLUMN = 4,
  30. // row
  31. ST_ROW = 5,
  32. // check button
  33. ST_CHECK = 6,
  34. // panel
  35. ST_PANEL = 7,
  36. // text
  37. ST_TEXT = 8,
  38. } SubType;
  39. typedef struct {
  40. int subtype;
  41. } UIData;
  42. typedef struct {
  43. UIData head;
  44. int iconid;
  45. const char *label;
  46. } UIButtonData;
  47. typedef struct {
  48. UIData head;
  49. const char *label;
  50. int *option;
  51. } UICheckData;
  52. typedef struct {
  53. UIData head;
  54. int iconid;
  55. const char *label;
  56. int *value;
  57. } UIRadioData;
  58. typedef struct {
  59. UIData head;
  60. const char *label;
  61. float *progress;
  62. } UISliderData;
  63. typedef struct {
  64. UIData head;
  65. char *text;
  66. int maxsize;
  67. } UITextData;
  68. ////////////////////////////////////////////////////////////////////////////////
  69. void init(NVGcontext *vg) {
  70. bndSetFont(nvgCreateFont(vg, "system", "../DejaVuSans.ttf"));
  71. bndSetIconImage(nvgCreateImage(vg, "../blender_icons16.png"));
  72. }
  73. // calculate which corners are sharp for an item, depending on whether
  74. // the container the item is in has negative spacing, and the item
  75. // is first or last element in a sequence of 2 or more elements.
  76. int cornerFlags(int item) {
  77. int parent = uiParent(item);
  78. int numkids = uiGetChildCount(parent);
  79. if (numkids < 2) return BND_CORNER_NONE;
  80. const UIData *head = (const UIData *)uiGetData(parent);
  81. if (head) {
  82. int numid = uiGetChildId(item);
  83. switch(head->subtype) {
  84. case ST_COLUMN: {
  85. if (!numid) return BND_CORNER_DOWN;
  86. else if (numid == numkids-1) return BND_CORNER_TOP;
  87. else return BND_CORNER_ALL;
  88. } break;
  89. case ST_ROW: {
  90. if (!numid) return BND_CORNER_RIGHT;
  91. else if (numid == numkids-1) return BND_CORNER_LEFT;
  92. else return BND_CORNER_ALL;
  93. } break;
  94. default: break;
  95. }
  96. }
  97. return BND_CORNER_NONE;
  98. }
  99. void testrect(NVGcontext *vg, UIrect rect) {
  100. #if 0
  101. nvgBeginPath(vg);
  102. nvgRect(vg,rect.x+0.5,rect.y+0.5,rect.w-1,rect.h-1);
  103. nvgStrokeColor(vg,nvgRGBf(1,0,0));
  104. nvgStrokeWidth(vg,1);
  105. nvgStroke(vg);
  106. #endif
  107. }
  108. void drawUI(NVGcontext *vg, int item, int x, int y) {
  109. const UIData *head = (const UIData *)uiGetData(item);
  110. UIrect rect = uiGetRect(item);
  111. rect.x += x;
  112. rect.y += y;
  113. if (uiGetState(item) == UI_FROZEN) {
  114. nvgGlobalAlpha(vg, BND_DISABLED_ALPHA);
  115. }
  116. if (head) {
  117. switch(head->subtype) {
  118. default: {
  119. testrect(vg,rect);
  120. } break;
  121. case ST_PANEL: {
  122. bndBevel(vg,rect.x,rect.y,rect.w,rect.h);
  123. } break;
  124. case ST_LABEL: {
  125. assert(head);
  126. const UIButtonData *data = (UIButtonData*)head;
  127. bndLabel(vg,rect.x,rect.y,rect.w,rect.h,
  128. data->iconid,data->label);
  129. } break;
  130. case ST_BUTTON: {
  131. const UIButtonData *data = (UIButtonData*)head;
  132. bndToolButton(vg,rect.x,rect.y,rect.w,rect.h,
  133. cornerFlags(item),(BNDwidgetState)uiGetState(item),
  134. data->iconid,data->label);
  135. } break;
  136. case ST_CHECK: {
  137. const UICheckData *data = (UICheckData*)head;
  138. BNDwidgetState state = (BNDwidgetState)uiGetState(item);
  139. if (*data->option)
  140. state = BND_ACTIVE;
  141. bndOptionButton(vg,rect.x,rect.y,rect.w,rect.h, state,
  142. data->label);
  143. } break;
  144. case ST_RADIO:{
  145. const UIRadioData *data = (UIRadioData*)head;
  146. BNDwidgetState state = (BNDwidgetState)uiGetState(item);
  147. if (*data->value == uiGetChildId(item))
  148. state = BND_ACTIVE;
  149. bndRadioButton(vg,rect.x,rect.y,rect.w,rect.h,
  150. cornerFlags(item),state,
  151. data->iconid,data->label);
  152. } break;
  153. case ST_SLIDER:{
  154. const UISliderData *data = (UISliderData*)head;
  155. BNDwidgetState state = (BNDwidgetState)uiGetState(item);
  156. static char value[32];
  157. sprintf(value,"%.0f%%",(*data->progress)*100.0f);
  158. bndSlider(vg,rect.x,rect.y,rect.w,rect.h,
  159. cornerFlags(item),state,
  160. *data->progress,data->label,value);
  161. } break;
  162. case ST_TEXT: {
  163. const UITextData *data = (UITextData*)head;
  164. BNDwidgetState state = (BNDwidgetState)uiGetState(item);
  165. int idx = strlen(data->text);
  166. bndTextField(vg,rect.x,rect.y,rect.w,rect.h,
  167. cornerFlags(item),state, -1, data->text, idx, idx);
  168. } break;
  169. }
  170. } else {
  171. testrect(vg,rect);
  172. }
  173. int kid = uiFirstChild(item);
  174. while (kid > 0) {
  175. drawUI(vg, kid, rect.x, rect.y);
  176. kid = uiNextSibling(kid);
  177. }
  178. if (uiGetState(item) == UI_FROZEN) {
  179. nvgGlobalAlpha(vg, 1.0);
  180. }
  181. }
  182. int label(int iconid, const char *label) {
  183. int item = uiItem();
  184. uiSetSize(item, 0, BND_WIDGET_HEIGHT);
  185. UIButtonData *data = (UIButtonData *)uiAllocData(item, sizeof(UIButtonData));
  186. data->head.subtype = ST_LABEL;
  187. data->iconid = iconid;
  188. data->label = label;
  189. return item;
  190. }
  191. void demohandler(int item, UIevent event) {
  192. const UIButtonData *data = (const UIButtonData *)uiGetData(item);
  193. printf("clicked: %lld %s\n", uiGetHandle(item), data->label);
  194. }
  195. int button(UIhandle handle, int iconid, const char *label,
  196. UIhandler handler) {
  197. // create new ui item
  198. int item = uiItem();
  199. // set persistent handle for item that is used
  200. // to track activity over time
  201. uiSetHandle(item, handle);
  202. // set size of wiget; horizontal size is dynamic, vertical is fixed
  203. uiSetSize(item, 0, BND_WIDGET_HEIGHT);
  204. // attach event handler e.g. demohandler above
  205. uiSetHandler(item, handler, UI_BUTTON0_HOT_UP);
  206. // store some custom data with the button that we use for styling
  207. UIButtonData *data = (UIButtonData *)uiAllocData(item, sizeof(UIButtonData));
  208. data->head.subtype = ST_BUTTON;
  209. data->iconid = iconid;
  210. data->label = label;
  211. return item;
  212. }
  213. void checkhandler(int item, UIevent event) {
  214. const UICheckData *data = (const UICheckData *)uiGetData(item);
  215. *data->option = !(*data->option);
  216. }
  217. int check(UIhandle handle, const char *label, int *option) {
  218. // create new ui item
  219. int item = uiItem();
  220. // set persistent handle for item that is used
  221. // to track activity over time
  222. uiSetHandle(item, handle);
  223. // set size of wiget; horizontal size is dynamic, vertical is fixed
  224. uiSetSize(item, 0, BND_WIDGET_HEIGHT);
  225. // attach event handler e.g. demohandler above
  226. uiSetHandler(item, checkhandler, UI_BUTTON0_DOWN);
  227. // store some custom data with the button that we use for styling
  228. UICheckData *data = (UICheckData *)uiAllocData(item, sizeof(UICheckData));
  229. data->head.subtype = ST_CHECK;
  230. data->label = label;
  231. data->option = option;
  232. return item;
  233. }
  234. // simple logic for a slider
  235. // starting offset of the currently active slider
  236. static float sliderstart = 0.0;
  237. // event handler for slider (same handler for all sliders)
  238. void sliderhandler(int item, UIevent event) {
  239. // retrieve the custom data we saved with the slider
  240. UISliderData *data = (UISliderData *)uiGetData(item);
  241. switch(event) {
  242. default: break;
  243. case UI_BUTTON0_DOWN: {
  244. // button was pressed for the first time; capture initial
  245. // slider value.
  246. sliderstart = *data->progress;
  247. } break;
  248. case UI_BUTTON0_CAPTURE: {
  249. // called for every frame that the button is pressed.
  250. // get the delta between the click point and the current
  251. // mouse position
  252. UIvec2 pos = uiGetCursorStartDelta();
  253. // get the items layouted rectangle
  254. UIrect rc = uiGetRect(item);
  255. // calculate our new offset and clamp
  256. float value = sliderstart + ((float)pos.x / (float)rc.w);
  257. value = (value<0)?0:(value>1)?1:value;
  258. // assign the new value
  259. *data->progress = value;
  260. } break;
  261. }
  262. }
  263. int slider(UIhandle handle, const char *label, float *progress) {
  264. // create new ui item
  265. int item = uiItem();
  266. // set persistent handle for item that is used
  267. // to track activity over time
  268. uiSetHandle(item, handle);
  269. // set size of wiget; horizontal size is dynamic, vertical is fixed
  270. uiSetSize(item, 0, BND_WIDGET_HEIGHT);
  271. // attach our slider event handler and capture two classes of events
  272. uiSetHandler(item, sliderhandler,
  273. UI_BUTTON0_DOWN | UI_BUTTON0_CAPTURE);
  274. // store some custom data with the button that we use for styling
  275. // and logic, e.g. the pointer to the data we want to alter.
  276. UISliderData *data = (UISliderData *)uiAllocData(item, sizeof(UISliderData));
  277. data->head.subtype = ST_SLIDER;
  278. data->label = label;
  279. data->progress = progress;
  280. return item;
  281. }
  282. void textboxhandler(int item, UIevent event) {
  283. UITextData *data = (UITextData *)uiGetData(item);
  284. switch(event) {
  285. default: break;
  286. case UI_BUTTON0_DOWN: {
  287. uiFocus(item);
  288. } break;
  289. case UI_KEY_DOWN: {
  290. unsigned int key = uiGetKey();
  291. switch(key) {
  292. default: break;
  293. case GLFW_KEY_BACKSPACE: {
  294. int size = strlen(data->text);
  295. if (!size) return;
  296. data->text[size-1] = 0;
  297. } break;
  298. case GLFW_KEY_ENTER: {
  299. uiFocus(-1);
  300. } break;
  301. }
  302. } break;
  303. case UI_CHAR: {
  304. unsigned int key = uiGetKey();
  305. if ((key > 255)||(key < 32)) return;
  306. int size = strlen(data->text);
  307. if (size >= (data->maxsize-1)) return;
  308. data->text[size] = (char)key;
  309. } break;
  310. }
  311. }
  312. int textbox(UIhandle handle, char *text, int maxsize) {
  313. int item = uiItem();
  314. uiSetHandle(item, handle);
  315. uiSetSize(item, 0, BND_WIDGET_HEIGHT);
  316. uiSetHandler(item, textboxhandler,
  317. UI_BUTTON0_DOWN | UI_KEY_DOWN | UI_CHAR);
  318. // store some custom data with the button that we use for styling
  319. // and logic, e.g. the pointer to the data we want to alter.
  320. UITextData *data = (UITextData *)uiAllocData(item, sizeof(UITextData));
  321. data->head.subtype = ST_TEXT;
  322. data->text = text;
  323. data->maxsize = maxsize;
  324. return item;
  325. }
  326. // simple logic for a radio button
  327. void radiohandler(int item, UIevent event) {
  328. UIRadioData *data = (UIRadioData *)uiGetData(item);
  329. *data->value = uiGetChildId(item);
  330. }
  331. int radio(UIhandle handle, int iconid, const char *label, int *value) {
  332. int item = uiItem();
  333. uiSetHandle(item, handle);
  334. uiSetSize(item, label?0:BND_TOOL_WIDTH, BND_WIDGET_HEIGHT);
  335. UIRadioData *data = (UIRadioData *)uiAllocData(item, sizeof(UIRadioData));
  336. data->head.subtype = ST_RADIO;
  337. data->iconid = iconid;
  338. data->label = label;
  339. data->value = value;
  340. uiSetHandler(item, radiohandler, UI_BUTTON0_DOWN);
  341. return item;
  342. }
  343. int panel() {
  344. int item = uiItem();
  345. UIData *data = (UIData *)uiAllocData(item, sizeof(UIData));
  346. data->subtype = ST_PANEL;
  347. return item;
  348. }
  349. int column_append(int parent, int item) {
  350. int last = uiLastChild(parent);
  351. uiAppend(parent, item);
  352. // mark the new item as positioned under the previous item
  353. uiSetBelow(item, last);
  354. // fill parent horizontally, anchor to previous item vertically
  355. uiSetLayout(item, UI_HFILL|UI_TOP);
  356. // if not the first item, add a margin of 1
  357. uiSetMargins(item, 0, (last < 0)?0:1, 0, 0);
  358. return item;
  359. }
  360. int column() {
  361. int item = uiItem();
  362. return item;
  363. }
  364. int vgroup_append(int parent, int item) {
  365. int last = uiLastChild(parent);
  366. uiAppend(parent, item);
  367. // mark the new item as positioned under the previous item
  368. uiSetBelow(item, last);
  369. // fill parent horizontally, anchor to previous item vertically
  370. uiSetLayout(item, UI_HFILL|UI_TOP);
  371. // if not the first item, add a margin
  372. uiSetMargins(item, 0, (last < 0)?0:-2, 0, 0);
  373. return item;
  374. }
  375. int vgroup() {
  376. int item = uiItem();
  377. UIData *data = (UIData *)uiAllocData(item, sizeof(UIData));
  378. data->subtype = ST_COLUMN;
  379. return item;
  380. }
  381. int hgroup_append(int parent, int item) {
  382. int last = uiLastChild(parent);
  383. uiAppend(parent, item);
  384. uiSetRightTo(item, last);
  385. if (last > 0)
  386. uiSetLeftTo(last, item);
  387. uiSetLayout(item, UI_LEFT|UI_RIGHT);
  388. uiSetMargins(item, (last < 0)?0:-1, 0, 0, 0);
  389. return item;
  390. }
  391. int hgroup() {
  392. int item = uiItem();
  393. UIData *data = (UIData *)uiAllocData(item, sizeof(UIData));
  394. data->subtype = ST_ROW;
  395. return item;
  396. }
  397. int row_append(int parent, int item) {
  398. int last = uiLastChild(parent);
  399. uiAppend(parent, item);
  400. uiSetRightTo(item, last);
  401. if (last > 0)
  402. uiSetLeftTo(last, item);
  403. uiSetLayout(item, UI_LEFT|UI_RIGHT);
  404. uiSetMargins(item, (last < 0)?0:8, 0, 0, 0);
  405. return item;
  406. }
  407. int row() {
  408. int item = uiItem();
  409. return item;
  410. }
  411. void draw_noodles(NVGcontext *vg, int x, int y) {
  412. int w = 200;
  413. int s = 70;
  414. bndNodeBackground(vg, x+w, y-50, 100, 200, BND_DEFAULT, BND_ICONID(6,3),
  415. "Default", nvgRGBf(0.392f,0.392f,0.392f));
  416. bndNodeBackground(vg, x+w+120, y-50, 100, 200, BND_HOVER, BND_ICONID(6,3),
  417. "Hover", nvgRGBf(0.392f,0.392f,0.392f));
  418. bndNodeBackground(vg, x+w+240, y-50, 100, 200, BND_ACTIVE, BND_ICONID(6,3),
  419. "Active", nvgRGBf(0.392f,0.392f,0.392f));
  420. for (int i = 0; i < 9; ++i) {
  421. int a = i%3;
  422. int b = i/3;
  423. bndNodeWire(vg, x, y+s*a, x+w, y+s*b, (BNDwidgetState)a, (BNDwidgetState)b);
  424. }
  425. bndNodePort(vg, x, y, BND_DEFAULT, nvgRGBf(0.5f, 0.5f, 0.5f));
  426. bndNodePort(vg, x+w, y, BND_DEFAULT, nvgRGBf(0.5f, 0.5f, 0.5f));
  427. bndNodePort(vg, x, y+s, BND_HOVER, nvgRGBf(0.5f, 0.5f, 0.5f));
  428. bndNodePort(vg, x+w, y+s, BND_HOVER, nvgRGBf(0.5f, 0.5f, 0.5f));
  429. bndNodePort(vg, x, y+2*s, BND_ACTIVE, nvgRGBf(0.5f, 0.5f, 0.5f));
  430. bndNodePort(vg, x+w, y+2*s, BND_ACTIVE, nvgRGBf(0.5f, 0.5f, 0.5f));
  431. }
  432. static void roothandler(int parent, UIevent event) {
  433. switch(event) {
  434. default: break;
  435. case UI_SCROLL: {
  436. UIvec2 pos = uiGetScroll();
  437. printf("scroll! %d %d\n", pos.x, pos.y);
  438. } break;
  439. case UI_BUTTON0_DOWN: {
  440. printf("%d clicks\n", uiGetClicks());
  441. } break;
  442. }
  443. }
  444. void draw(NVGcontext *vg, float w, float h) {
  445. bndBackground(vg, 0, 0, w, h);
  446. bndSplitterWidgets(vg, 0, 0, w, h);
  447. int x = 10;
  448. int y = 10;
  449. bndToolButton(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_DEFAULT,
  450. BND_ICONID(6,3),"Default");
  451. y += 25;
  452. bndToolButton(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_HOVER,
  453. BND_ICONID(6,3),"Hovered");
  454. y += 25;
  455. bndToolButton(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_ACTIVE,
  456. BND_ICONID(6,3),"Active");
  457. y += 40;
  458. bndRadioButton(vg,x,y,80,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_DEFAULT,
  459. -1,"Default");
  460. y += 25;
  461. bndRadioButton(vg,x,y,80,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_HOVER,
  462. -1,"Hovered");
  463. y += 25;
  464. bndRadioButton(vg,x,y,80,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_ACTIVE,
  465. -1,"Active");
  466. y += 25;
  467. bndLabel(vg,x,y,120,BND_WIDGET_HEIGHT,-1,"Label:");
  468. y += BND_WIDGET_HEIGHT;
  469. bndChoiceButton(vg,x,y,80,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_DEFAULT,
  470. -1, "Default");
  471. y += 25;
  472. bndChoiceButton(vg,x,y,80,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_HOVER,
  473. -1, "Hovered");
  474. y += 25;
  475. bndChoiceButton(vg,x,y,80,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_ACTIVE,
  476. -1, "Active");
  477. y += 25;
  478. int ry = y;
  479. int rx = x;
  480. y = 10;
  481. x += 130;
  482. bndOptionButton(vg,x,y,120,BND_WIDGET_HEIGHT,BND_DEFAULT,"Default");
  483. y += 25;
  484. bndOptionButton(vg,x,y,120,BND_WIDGET_HEIGHT,BND_HOVER,"Hovered");
  485. y += 25;
  486. bndOptionButton(vg,x,y,120,BND_WIDGET_HEIGHT,BND_ACTIVE,"Active");
  487. y += 40;
  488. bndNumberField(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_DOWN,BND_DEFAULT,
  489. "Top","100");
  490. y += BND_WIDGET_HEIGHT-2;
  491. bndNumberField(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_ALL,BND_DEFAULT,
  492. "Center","100");
  493. y += BND_WIDGET_HEIGHT-2;
  494. bndNumberField(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_TOP,BND_DEFAULT,
  495. "Bottom","100");
  496. int mx = x-30;
  497. int my = y-12;
  498. int mw = 120;
  499. bndMenuBackground(vg,mx,my,mw,120,BND_CORNER_TOP);
  500. bndMenuLabel(vg,mx,my,mw,BND_WIDGET_HEIGHT,-1,"Menu Title");
  501. my += BND_WIDGET_HEIGHT-2;
  502. bndMenuItem(vg,mx,my,mw,BND_WIDGET_HEIGHT,BND_DEFAULT,
  503. BND_ICONID(17,3),"Default");
  504. my += BND_WIDGET_HEIGHT-2;
  505. bndMenuItem(vg,mx,my,mw,BND_WIDGET_HEIGHT,BND_HOVER,
  506. BND_ICONID(18,3),"Hovered");
  507. my += BND_WIDGET_HEIGHT-2;
  508. bndMenuItem(vg,mx,my,mw,BND_WIDGET_HEIGHT,BND_ACTIVE,
  509. BND_ICONID(19,3),"Active");
  510. y = 10;
  511. x += 130;
  512. int ox = x;
  513. bndNumberField(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_DEFAULT,
  514. "Default","100");
  515. y += 25;
  516. bndNumberField(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_HOVER,
  517. "Hovered","100");
  518. y += 25;
  519. bndNumberField(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_ACTIVE,
  520. "Active","100");
  521. y += 40;
  522. bndRadioButton(vg,x,y,60,BND_WIDGET_HEIGHT,BND_CORNER_RIGHT,BND_DEFAULT,
  523. -1,"One");
  524. x += 60-1;
  525. bndRadioButton(vg,x,y,60,BND_WIDGET_HEIGHT,BND_CORNER_ALL,BND_DEFAULT,
  526. -1,"Two");
  527. x += 60-1;
  528. bndRadioButton(vg,x,y,60,BND_WIDGET_HEIGHT,BND_CORNER_ALL,BND_DEFAULT,
  529. -1,"Three");
  530. x += 60-1;
  531. bndRadioButton(vg,x,y,60,BND_WIDGET_HEIGHT,BND_CORNER_LEFT,BND_ACTIVE,
  532. -1,"Butts");
  533. x = ox;
  534. y += 40;
  535. float progress_value = fmodf(glfwGetTime()/10.0,1.0);
  536. char progress_label[32];
  537. sprintf(progress_label, "%d%%", int(progress_value*100+0.5f));
  538. bndSlider(vg,x,y,240,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_DEFAULT,
  539. progress_value,"Default",progress_label);
  540. y += 25;
  541. bndSlider(vg,x,y,240,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_HOVER,
  542. progress_value,"Hovered",progress_label);
  543. y += 25;
  544. bndSlider(vg,x,y,240,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_ACTIVE,
  545. progress_value,"Active",progress_label);
  546. int rw = x+240-rx;
  547. float s_offset = sinf(glfwGetTime()/2.0)*0.5+0.5;
  548. float s_size = cosf(glfwGetTime()/3.11)*0.5+0.5;
  549. bndScrollBar(vg,rx,ry,rw,BND_SCROLLBAR_HEIGHT,BND_DEFAULT,s_offset,s_size);
  550. ry += 20;
  551. bndScrollBar(vg,rx,ry,rw,BND_SCROLLBAR_HEIGHT,BND_HOVER,s_offset,s_size);
  552. ry += 20;
  553. bndScrollBar(vg,rx,ry,rw,BND_SCROLLBAR_HEIGHT,BND_ACTIVE,s_offset,s_size);
  554. const char edit_text[] = "The quick brown fox";
  555. int textlen = strlen(edit_text)+1;
  556. int t = int(glfwGetTime()*2);
  557. int idx1 = (t/textlen)%textlen;
  558. int idx2 = idx1 + (t%(textlen-idx1));
  559. ry += 25;
  560. bndTextField(vg,rx,ry,240,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_DEFAULT,
  561. -1, edit_text, idx1, idx2);
  562. ry += 25;
  563. bndTextField(vg,rx,ry,240,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_HOVER,
  564. -1, edit_text, idx1, idx2);
  565. ry += 25;
  566. bndTextField(vg,rx,ry,240,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_ACTIVE,
  567. -1, edit_text, idx1, idx2);
  568. draw_noodles(vg, 20, ry+50);
  569. rx += rw + 20;
  570. ry = 10;
  571. bndScrollBar(vg,rx,ry,BND_SCROLLBAR_WIDTH,240,BND_DEFAULT,s_offset,s_size);
  572. rx += 20;
  573. bndScrollBar(vg,rx,ry,BND_SCROLLBAR_WIDTH,240,BND_HOVER,s_offset,s_size);
  574. rx += 20;
  575. bndScrollBar(vg,rx,ry,BND_SCROLLBAR_WIDTH,240,BND_ACTIVE,s_offset,s_size);
  576. x = ox;
  577. y += 40;
  578. bndToolButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_RIGHT,
  579. BND_DEFAULT,BND_ICONID(0,10),NULL);
  580. x += BND_TOOL_WIDTH-1;
  581. bndToolButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
  582. BND_DEFAULT,BND_ICONID(1,10),NULL);
  583. x += BND_TOOL_WIDTH-1;
  584. bndToolButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
  585. BND_DEFAULT,BND_ICONID(2,10),NULL);
  586. x += BND_TOOL_WIDTH-1;
  587. bndToolButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
  588. BND_DEFAULT,BND_ICONID(3,10),NULL);
  589. x += BND_TOOL_WIDTH-1;
  590. bndToolButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
  591. BND_DEFAULT,BND_ICONID(4,10),NULL);
  592. x += BND_TOOL_WIDTH-1;
  593. bndToolButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_LEFT,
  594. BND_DEFAULT,BND_ICONID(5,10),NULL);
  595. x += BND_TOOL_WIDTH-1;
  596. x += 5;
  597. bndRadioButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_RIGHT,
  598. BND_DEFAULT,BND_ICONID(0,11),NULL);
  599. x += BND_TOOL_WIDTH-1;
  600. bndRadioButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
  601. BND_DEFAULT,BND_ICONID(1,11),NULL);
  602. x += BND_TOOL_WIDTH-1;
  603. bndRadioButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
  604. BND_DEFAULT,BND_ICONID(2,11),NULL);
  605. x += BND_TOOL_WIDTH-1;
  606. bndRadioButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
  607. BND_DEFAULT,BND_ICONID(3,11),NULL);
  608. x += BND_TOOL_WIDTH-1;
  609. bndRadioButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
  610. BND_ACTIVE,BND_ICONID(4,11),NULL);
  611. x += BND_TOOL_WIDTH-1;
  612. bndRadioButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_LEFT,
  613. BND_DEFAULT,BND_ICONID(5,11),NULL);
  614. // some OUI stuff
  615. // some persistent variables for demonstration
  616. static int enum1 = 0;
  617. static float progress1 = 0.25f;
  618. static float progress2 = 0.75f;
  619. static int option1 = 1;
  620. static int option2 = 0;
  621. static int option3 = 0;
  622. uiClear();
  623. int root = panel();
  624. // position root element
  625. uiSetLayout(0,UI_LEFT|UI_TOP);
  626. uiSetMargins(0,600,10,0,0);
  627. uiSetSize(0,250,400);
  628. uiSetSelfHandle(root);
  629. uiSetHandler(root, roothandler, UI_SCROLL|UI_BUTTON0_DOWN);
  630. int col = column();
  631. uiAppend(root, col);
  632. uiSetMargins(col, 10, 10, 10, 10);
  633. uiSetLayout(col, UI_TOP|UI_HFILL);
  634. column_append(col, button(1, BND_ICONID(6,3), "Item 1", demohandler));
  635. column_append(col, button(2, BND_ICONID(6,3), "Item 2", demohandler));
  636. {
  637. int h = column_append(col, hgroup());
  638. hgroup_append(h, radio(3, BND_ICONID(6,3), "Item 3.0", &enum1));
  639. hgroup_append(h, radio(4, BND_ICONID(0,10), NULL, &enum1));
  640. hgroup_append(h, radio(5, BND_ICONID(1,10), NULL, &enum1));
  641. hgroup_append(h, radio(6, BND_ICONID(6,3), "Item 3.3", &enum1));
  642. }
  643. {
  644. int rows = column_append(col, row());
  645. int coll = row_append(rows, vgroup());
  646. vgroup_append(coll, label(-1, "Items 4.0:"));
  647. coll = vgroup_append(coll, vgroup());
  648. vgroup_append(coll, button(7, BND_ICONID(6,3), "Item 4.0.0", demohandler));
  649. vgroup_append(coll, button(8, BND_ICONID(6,3), "Item 4.0.1", demohandler));
  650. int colr = row_append(rows, vgroup());
  651. uiSetFrozen(colr, option1);
  652. vgroup_append(colr, label(-1, "Items 4.1:"));
  653. colr = vgroup_append(colr, vgroup());
  654. vgroup_append(colr, slider(9, "Item 4.1.0", &progress1));
  655. vgroup_append(colr, slider(10, "Item 4.1.1", &progress2));
  656. }
  657. column_append(col, button(11, BND_ICONID(6,3), "Item 5", NULL));
  658. column_append(col, check(12, "Frozen", &option1));
  659. column_append(col, check(13, "Item 7", &option2));
  660. column_append(col, check(14, "Item 8", &option3));
  661. static char textbuffer[32] = "click and edit";
  662. column_append(col, textbox((UIhandle)textbuffer, textbuffer, 32));
  663. uiLayout();
  664. drawUI(vg, 0, 0, 0);
  665. UIvec2 cursor = uiGetCursor();
  666. cursor.x -= w/2;
  667. cursor.y -= h/2;
  668. if (abs(cursor.x) > (w/4)) {
  669. bndJoinAreaOverlay(vg, 0, 0, w, h, 0, (cursor.x > 0));
  670. } else if (abs(cursor.y) > (h/4)) {
  671. bndJoinAreaOverlay(vg, 0, 0, w, h, 1, (cursor.y > 0));
  672. }
  673. uiProcess((int)(glfwGetTime()*1000.0));
  674. }
  675. ////////////////////////////////////////////////////////////////////////////////
  676. void errorcb(int error, const char* desc)
  677. {
  678. printf("GLFW error %d: %s\n", error, desc);
  679. }
  680. static void mousebutton(GLFWwindow *window, int button, int action, int mods) {
  681. NVG_NOTUSED(window);
  682. NVG_NOTUSED(mods);
  683. uiSetButton(button, (action==GLFW_PRESS)?1:0);
  684. }
  685. static void cursorpos(GLFWwindow *window, double x, double y) {
  686. NVG_NOTUSED(window);
  687. uiSetCursor((int)x,(int)y);
  688. }
  689. static void scrollevent(GLFWwindow *window, double x, double y) {
  690. NVG_NOTUSED(window);
  691. uiSetScroll((int)x, (int)y);
  692. }
  693. static void charevent(GLFWwindow *window, unsigned int value) {
  694. NVG_NOTUSED(window);
  695. uiSetChar(value);
  696. }
  697. static void key(GLFWwindow* window, int key, int scancode, int action, int mods)
  698. {
  699. NVG_NOTUSED(scancode);
  700. NVG_NOTUSED(mods);
  701. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  702. glfwSetWindowShouldClose(window, GL_TRUE);
  703. uiSetKey(key, mods, action);
  704. }
  705. int main()
  706. {
  707. GLFWwindow* window;
  708. struct NVGcontext* vg = NULL;
  709. UIcontext *uictx;
  710. uictx = uiCreateContext();
  711. uiMakeCurrent(uictx);
  712. if (!glfwInit()) {
  713. printf("Failed to init GLFW.");
  714. return -1;
  715. }
  716. glfwSetErrorCallback(errorcb);
  717. #ifndef _WIN32 // don't require this on win32, and works with more cards
  718. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  719. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  720. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  721. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  722. #endif
  723. glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, 1);
  724. window = glfwCreateWindow(1000, 600, "OUI Blendish Demo", NULL, NULL);
  725. if (!window) {
  726. glfwTerminate();
  727. return -1;
  728. }
  729. glfwSetKeyCallback(window, key);
  730. glfwSetCharCallback(window, charevent);
  731. glfwSetCursorPosCallback(window, cursorpos);
  732. glfwSetMouseButtonCallback(window, mousebutton);
  733. glfwSetScrollCallback(window, scrollevent);
  734. glfwMakeContextCurrent(window);
  735. #ifdef NANOVG_GLEW
  736. glewExperimental = GL_TRUE;
  737. if(glewInit() != GLEW_OK) {
  738. printf("Could not init glew.\n");
  739. return -1;
  740. }
  741. // GLEW generates GL error because it calls glGetString(GL_EXTENSIONS), we'll consume it here.
  742. glGetError();
  743. #endif
  744. vg = nvgCreateGL3(NVG_ANTIALIAS | NVG_STENCIL_STROKES);
  745. if (vg == NULL) {
  746. printf("Could not init nanovg.\n");
  747. return -1;
  748. }
  749. init(vg);
  750. glfwSwapInterval(0);
  751. glfwSetTime(0);
  752. while (!glfwWindowShouldClose(window))
  753. {
  754. double mx, my;
  755. int winWidth, winHeight;
  756. int fbWidth, fbHeight;
  757. float pxRatio;
  758. glfwGetCursorPos(window, &mx, &my);
  759. glfwGetWindowSize(window, &winWidth, &winHeight);
  760. glfwGetFramebufferSize(window, &fbWidth, &fbHeight);
  761. // Calculate pixel ration for hi-dpi devices.
  762. pxRatio = (float)fbWidth / (float)winWidth;
  763. // Update and render
  764. glViewport(0, 0, fbWidth, fbHeight);
  765. glClearColor(0,0,0,1);
  766. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
  767. nvgBeginFrame(vg, winWidth, winHeight, pxRatio);
  768. draw(vg, winWidth, winHeight);
  769. nvgEndFrame(vg);
  770. glfwSwapBuffers(window);
  771. glfwPollEvents();
  772. }
  773. uiDestroyContext(uictx);
  774. nvgDeleteGL3(vg);
  775. glfwTerminate();
  776. return 0;
  777. }