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.

834 lines
26KB

  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 parent, 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. uiAppend(parent, item);
  190. return item;
  191. }
  192. void demohandler(int item, UIevent event) {
  193. const UIButtonData *data = (const UIButtonData *)uiGetData(item);
  194. printf("clicked: %lld %s\n", uiGetHandle(item), data->label);
  195. }
  196. int button(int parent, UIhandle handle, int iconid, const char *label,
  197. UIhandler handler) {
  198. // create new ui item
  199. int item = uiItem();
  200. // set persistent handle for item that is used
  201. // to track activity over time
  202. uiSetHandle(item, handle);
  203. // set size of wiget; horizontal size is dynamic, vertical is fixed
  204. uiSetSize(item, 0, BND_WIDGET_HEIGHT);
  205. // attach event handler e.g. demohandler above
  206. uiSetHandler(item, handler, UI_BUTTON0_HOT_UP);
  207. // store some custom data with the button that we use for styling
  208. UIButtonData *data = (UIButtonData *)uiAllocData(item, sizeof(UIButtonData));
  209. data->head.subtype = ST_BUTTON;
  210. data->iconid = iconid;
  211. data->label = label;
  212. uiAppend(parent, item);
  213. return item;
  214. }
  215. void checkhandler(int item, UIevent event) {
  216. const UICheckData *data = (const UICheckData *)uiGetData(item);
  217. *data->option = !(*data->option);
  218. }
  219. int check(int parent, UIhandle handle, const char *label, int *option) {
  220. // create new ui item
  221. int item = uiItem();
  222. // set persistent handle for item that is used
  223. // to track activity over time
  224. uiSetHandle(item, handle);
  225. // set size of wiget; horizontal size is dynamic, vertical is fixed
  226. uiSetSize(item, 0, BND_WIDGET_HEIGHT);
  227. // attach event handler e.g. demohandler above
  228. uiSetHandler(item, checkhandler, UI_BUTTON0_DOWN);
  229. // store some custom data with the button that we use for styling
  230. UICheckData *data = (UICheckData *)uiAllocData(item, sizeof(UICheckData));
  231. data->head.subtype = ST_CHECK;
  232. data->label = label;
  233. data->option = option;
  234. uiAppend(parent, item);
  235. return item;
  236. }
  237. // simple logic for a slider
  238. // starting offset of the currently active slider
  239. static float sliderstart = 0.0;
  240. // event handler for slider (same handler for all sliders)
  241. void sliderhandler(int item, UIevent event) {
  242. // retrieve the custom data we saved with the slider
  243. UISliderData *data = (UISliderData *)uiGetData(item);
  244. switch(event) {
  245. default: break;
  246. case UI_BUTTON0_DOWN: {
  247. // button was pressed for the first time; capture initial
  248. // slider value.
  249. sliderstart = *data->progress;
  250. } break;
  251. case UI_BUTTON0_CAPTURE: {
  252. // called for every frame that the button is pressed.
  253. // get the delta between the click point and the current
  254. // mouse position
  255. UIvec2 pos = uiGetCursorStartDelta();
  256. // get the items layouted rectangle
  257. UIrect rc = uiGetRect(item);
  258. // calculate our new offset and clamp
  259. float value = sliderstart + ((float)pos.x / (float)rc.w);
  260. value = (value<0)?0:(value>1)?1:value;
  261. // assign the new value
  262. *data->progress = value;
  263. } break;
  264. }
  265. }
  266. int slider(int parent, UIhandle handle, const char *label, float *progress) {
  267. // create new ui item
  268. int item = uiItem();
  269. // set persistent handle for item that is used
  270. // to track activity over time
  271. uiSetHandle(item, handle);
  272. // set size of wiget; horizontal size is dynamic, vertical is fixed
  273. uiSetSize(item, 0, BND_WIDGET_HEIGHT);
  274. // attach our slider event handler and capture two classes of events
  275. uiSetHandler(item, sliderhandler,
  276. UI_BUTTON0_DOWN | UI_BUTTON0_CAPTURE);
  277. // store some custom data with the button that we use for styling
  278. // and logic, e.g. the pointer to the data we want to alter.
  279. UISliderData *data = (UISliderData *)uiAllocData(item, sizeof(UISliderData));
  280. data->head.subtype = ST_SLIDER;
  281. data->label = label;
  282. data->progress = progress;
  283. uiAppend(parent, item);
  284. return item;
  285. }
  286. void textboxhandler(int item, UIevent event) {
  287. UITextData *data = (UITextData *)uiGetData(item);
  288. switch(event) {
  289. default: break;
  290. case UI_BUTTON0_DOWN: {
  291. uiFocus(item);
  292. } break;
  293. case UI_KEY_DOWN: {
  294. unsigned int key = uiGetKey();
  295. switch(key) {
  296. default: break;
  297. case GLFW_KEY_BACKSPACE: {
  298. int size = strlen(data->text);
  299. if (!size) return;
  300. data->text[size-1] = 0;
  301. } break;
  302. case GLFW_KEY_ENTER: {
  303. uiFocus(-1);
  304. } break;
  305. }
  306. } break;
  307. case UI_CHAR: {
  308. unsigned int key = uiGetKey();
  309. if ((key > 255)||(key < 32)) return;
  310. int size = strlen(data->text);
  311. if (size >= (data->maxsize-1)) return;
  312. data->text[size] = (char)key;
  313. } break;
  314. }
  315. }
  316. int textbox(int parent, UIhandle handle, char *text, int maxsize) {
  317. int item = uiItem();
  318. uiSetHandle(item, handle);
  319. uiSetSize(item, 0, BND_WIDGET_HEIGHT);
  320. uiSetHandler(item, textboxhandler,
  321. UI_BUTTON0_DOWN | UI_KEY_DOWN | UI_CHAR);
  322. // store some custom data with the button that we use for styling
  323. // and logic, e.g. the pointer to the data we want to alter.
  324. UITextData *data = (UITextData *)uiAllocData(item, sizeof(UITextData));
  325. data->head.subtype = ST_TEXT;
  326. data->text = text;
  327. data->maxsize = maxsize;
  328. uiAppend(parent, item);
  329. return item;
  330. }
  331. // simple logic for a radio button
  332. void radiohandler(int item, UIevent event) {
  333. UIRadioData *data = (UIRadioData *)uiGetData(item);
  334. *data->value = uiGetChildId(item);
  335. }
  336. int radio(int parent, UIhandle handle, int iconid, const char *label, int *value) {
  337. int item = uiItem();
  338. uiSetHandle(item, handle);
  339. uiSetSize(item, label?0:BND_TOOL_WIDTH, BND_WIDGET_HEIGHT);
  340. UIRadioData *data = (UIRadioData *)uiAllocData(item, sizeof(UIRadioData));
  341. data->head.subtype = ST_RADIO;
  342. data->iconid = iconid;
  343. data->label = label;
  344. data->value = value;
  345. uiSetHandler(item, radiohandler, UI_BUTTON0_DOWN);
  346. uiAppend(parent, item);
  347. return item;
  348. }
  349. void columnhandler(int parent, UIevent event) {
  350. int item = uiLastChild(parent);
  351. int last = uiPrevSibling(item);
  352. // mark the new item as positioned under the previous item
  353. uiSetRelToTop(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. }
  359. int panel() {
  360. int item = uiItem();
  361. UIData *data = (UIData *)uiAllocData(item, sizeof(UIData));
  362. data->subtype = ST_PANEL;
  363. return item;
  364. }
  365. int column(int parent) {
  366. int item = uiItem();
  367. uiSetHandler(item, columnhandler, UI_APPEND);
  368. uiAppend(parent, item);
  369. return item;
  370. }
  371. void vgrouphandler(int parent, UIevent event) {
  372. int item = uiLastChild(parent);
  373. int last = uiPrevSibling(item);
  374. // mark the new item as positioned under the previous item
  375. uiSetRelToTop(item, last);
  376. // fill parent horizontally, anchor to previous item vertically
  377. uiSetLayout(item, UI_HFILL|UI_TOP);
  378. // if not the first item, add a margin
  379. uiSetMargins(item, 0, (last < 0)?0:-2, 0, 0);
  380. }
  381. int vgroup(int parent) {
  382. int item = uiItem();
  383. UIData *data = (UIData *)uiAllocData(item, sizeof(UIData));
  384. data->subtype = ST_COLUMN;
  385. uiSetHandler(item, vgrouphandler, UI_APPEND);
  386. uiAppend(parent, item);
  387. return item;
  388. }
  389. void hgrouphandler(int parent, UIevent event) {
  390. int item = uiLastChild(parent);
  391. int last = uiPrevSibling(item);
  392. uiSetRelToLeft(item, last);
  393. if (last > 0)
  394. uiSetRelToRight(last, item);
  395. uiSetLayout(item, UI_LEFT|UI_RIGHT);
  396. uiSetMargins(item, (last < 0)?0:-1, 0, 0, 0);
  397. }
  398. int hgroup(int parent) {
  399. int item = uiItem();
  400. UIData *data = (UIData *)uiAllocData(item, sizeof(UIData));
  401. data->subtype = ST_ROW;
  402. uiSetHandler(item, hgrouphandler, UI_APPEND);
  403. uiAppend(parent, item);
  404. return item;
  405. }
  406. void rowhandler(int parent, UIevent event) {
  407. int item = uiLastChild(parent);
  408. int last = uiPrevSibling(item);
  409. uiSetRelToLeft(item, last);
  410. if (last > 0)
  411. uiSetRelToRight(last, item);
  412. uiSetLayout(item, UI_LEFT|UI_RIGHT);
  413. uiSetMargins(item, (last < 0)?0:8, 0, 0, 0);
  414. }
  415. int row(int parent) {
  416. int item = uiItem();
  417. uiSetHandler(item, rowhandler, UI_APPEND);
  418. uiAppend(parent, item);
  419. return item;
  420. }
  421. void draw(NVGcontext *vg, float w, float h) {
  422. bndBackground(vg, 0, 0, w, h);
  423. int x = 10;
  424. int y = 10;
  425. bndToolButton(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_DEFAULT,
  426. BND_ICONID(6,3),"Default");
  427. y += 25;
  428. bndToolButton(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_HOVER,
  429. BND_ICONID(6,3),"Hovered");
  430. y += 25;
  431. bndToolButton(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_ACTIVE,
  432. BND_ICONID(6,3),"Active");
  433. y += 40;
  434. bndRadioButton(vg,x,y,80,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_DEFAULT,
  435. -1,"Default");
  436. y += 25;
  437. bndRadioButton(vg,x,y,80,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_HOVER,
  438. -1,"Hovered");
  439. y += 25;
  440. bndRadioButton(vg,x,y,80,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_ACTIVE,
  441. -1,"Active");
  442. y += 25;
  443. bndLabel(vg,x,y,120,BND_WIDGET_HEIGHT,-1,"Label:");
  444. y += BND_WIDGET_HEIGHT;
  445. bndChoiceButton(vg,x,y,80,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_DEFAULT,
  446. -1, "Default");
  447. y += 25;
  448. bndChoiceButton(vg,x,y,80,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_HOVER,
  449. -1, "Hovered");
  450. y += 25;
  451. bndChoiceButton(vg,x,y,80,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_ACTIVE,
  452. -1, "Active");
  453. y += 25;
  454. int ry = y;
  455. int rx = x;
  456. y = 10;
  457. x += 130;
  458. bndOptionButton(vg,x,y,120,BND_WIDGET_HEIGHT,BND_DEFAULT,"Default");
  459. y += 25;
  460. bndOptionButton(vg,x,y,120,BND_WIDGET_HEIGHT,BND_HOVER,"Hovered");
  461. y += 25;
  462. bndOptionButton(vg,x,y,120,BND_WIDGET_HEIGHT,BND_ACTIVE,"Active");
  463. y += 40;
  464. bndNumberField(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_DOWN,BND_DEFAULT,
  465. "Top","100");
  466. y += BND_WIDGET_HEIGHT-2;
  467. bndNumberField(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_ALL,BND_DEFAULT,
  468. "Center","100");
  469. y += BND_WIDGET_HEIGHT-2;
  470. bndNumberField(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_TOP,BND_DEFAULT,
  471. "Bottom","100");
  472. int mx = x-30;
  473. int my = y-12;
  474. int mw = 120;
  475. bndMenuBackground(vg,mx,my,mw,120,BND_CORNER_TOP);
  476. bndMenuLabel(vg,mx,my,mw,BND_WIDGET_HEIGHT,-1,"Menu Title");
  477. my += BND_WIDGET_HEIGHT-2;
  478. bndMenuItem(vg,mx,my,mw,BND_WIDGET_HEIGHT,BND_DEFAULT,
  479. BND_ICONID(17,3),"Default");
  480. my += BND_WIDGET_HEIGHT-2;
  481. bndMenuItem(vg,mx,my,mw,BND_WIDGET_HEIGHT,BND_HOVER,
  482. BND_ICONID(18,3),"Hovered");
  483. my += BND_WIDGET_HEIGHT-2;
  484. bndMenuItem(vg,mx,my,mw,BND_WIDGET_HEIGHT,BND_ACTIVE,
  485. BND_ICONID(19,3),"Active");
  486. y = 10;
  487. x += 130;
  488. int ox = x;
  489. bndNumberField(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_DEFAULT,
  490. "Default","100");
  491. y += 25;
  492. bndNumberField(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_HOVER,
  493. "Hovered","100");
  494. y += 25;
  495. bndNumberField(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_ACTIVE,
  496. "Active","100");
  497. y += 40;
  498. bndRadioButton(vg,x,y,60,BND_WIDGET_HEIGHT,BND_CORNER_RIGHT,BND_DEFAULT,
  499. -1,"One");
  500. x += 60-1;
  501. bndRadioButton(vg,x,y,60,BND_WIDGET_HEIGHT,BND_CORNER_ALL,BND_DEFAULT,
  502. -1,"Two");
  503. x += 60-1;
  504. bndRadioButton(vg,x,y,60,BND_WIDGET_HEIGHT,BND_CORNER_ALL,BND_DEFAULT,
  505. -1,"Three");
  506. x += 60-1;
  507. bndRadioButton(vg,x,y,60,BND_WIDGET_HEIGHT,BND_CORNER_LEFT,BND_ACTIVE,
  508. -1,"Butts");
  509. x = ox;
  510. y += 40;
  511. float progress_value = fmodf(glfwGetTime()/10.0,1.0);
  512. char progress_label[32];
  513. sprintf(progress_label, "%d%%", int(progress_value*100+0.5f));
  514. bndSlider(vg,x,y,240,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_DEFAULT,
  515. progress_value,"Default",progress_label);
  516. y += 25;
  517. bndSlider(vg,x,y,240,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_HOVER,
  518. progress_value,"Hovered",progress_label);
  519. y += 25;
  520. bndSlider(vg,x,y,240,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_ACTIVE,
  521. progress_value,"Active",progress_label);
  522. int rw = x+240-rx;
  523. float s_offset = sinf(glfwGetTime()/2.0)*0.5+0.5;
  524. float s_size = cosf(glfwGetTime()/3.11)*0.5+0.5;
  525. bndScrollBar(vg,rx,ry,rw,BND_SCROLLBAR_HEIGHT,BND_DEFAULT,s_offset,s_size);
  526. ry += 20;
  527. bndScrollBar(vg,rx,ry,rw,BND_SCROLLBAR_HEIGHT,BND_HOVER,s_offset,s_size);
  528. ry += 20;
  529. bndScrollBar(vg,rx,ry,rw,BND_SCROLLBAR_HEIGHT,BND_ACTIVE,s_offset,s_size);
  530. const char edit_text[] = "The quick brown fox";
  531. int textlen = strlen(edit_text)+1;
  532. int t = int(glfwGetTime()*2);
  533. int idx1 = (t/textlen)%textlen;
  534. int idx2 = idx1 + (t%(textlen-idx1));
  535. ry += 25;
  536. bndTextField(vg,rx,ry,240,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_DEFAULT,
  537. -1, edit_text, idx1, idx2);
  538. ry += 25;
  539. bndTextField(vg,rx,ry,240,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_HOVER,
  540. -1, edit_text, idx1, idx2);
  541. ry += 25;
  542. bndTextField(vg,rx,ry,240,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_ACTIVE,
  543. -1, edit_text, idx1, idx2);
  544. rx += rw + 20;
  545. ry = 10;
  546. bndScrollBar(vg,rx,ry,BND_SCROLLBAR_WIDTH,240,BND_DEFAULT,s_offset,s_size);
  547. rx += 20;
  548. bndScrollBar(vg,rx,ry,BND_SCROLLBAR_WIDTH,240,BND_HOVER,s_offset,s_size);
  549. rx += 20;
  550. bndScrollBar(vg,rx,ry,BND_SCROLLBAR_WIDTH,240,BND_ACTIVE,s_offset,s_size);
  551. x = ox;
  552. y += 40;
  553. bndToolButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_RIGHT,
  554. BND_DEFAULT,BND_ICONID(0,10),NULL);
  555. x += BND_TOOL_WIDTH-1;
  556. bndToolButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
  557. BND_DEFAULT,BND_ICONID(1,10),NULL);
  558. x += BND_TOOL_WIDTH-1;
  559. bndToolButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
  560. BND_DEFAULT,BND_ICONID(2,10),NULL);
  561. x += BND_TOOL_WIDTH-1;
  562. bndToolButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
  563. BND_DEFAULT,BND_ICONID(3,10),NULL);
  564. x += BND_TOOL_WIDTH-1;
  565. bndToolButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
  566. BND_DEFAULT,BND_ICONID(4,10),NULL);
  567. x += BND_TOOL_WIDTH-1;
  568. bndToolButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_LEFT,
  569. BND_DEFAULT,BND_ICONID(5,10),NULL);
  570. x += BND_TOOL_WIDTH-1;
  571. x += 5;
  572. bndRadioButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_RIGHT,
  573. BND_DEFAULT,BND_ICONID(0,11),NULL);
  574. x += BND_TOOL_WIDTH-1;
  575. bndRadioButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
  576. BND_DEFAULT,BND_ICONID(1,11),NULL);
  577. x += BND_TOOL_WIDTH-1;
  578. bndRadioButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
  579. BND_DEFAULT,BND_ICONID(2,11),NULL);
  580. x += BND_TOOL_WIDTH-1;
  581. bndRadioButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
  582. BND_DEFAULT,BND_ICONID(3,11),NULL);
  583. x += BND_TOOL_WIDTH-1;
  584. bndRadioButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
  585. BND_ACTIVE,BND_ICONID(4,11),NULL);
  586. x += BND_TOOL_WIDTH-1;
  587. bndRadioButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_LEFT,
  588. BND_DEFAULT,BND_ICONID(5,11),NULL);
  589. // some OUI stuff
  590. // some persistent variables for demonstration
  591. static int enum1 = 0;
  592. static float progress1 = 0.25f;
  593. static float progress2 = 0.75f;
  594. static int option1 = 1;
  595. static int option2 = 0;
  596. static int option3 = 0;
  597. uiClear();
  598. int root = panel();
  599. // position root element
  600. uiSetLayout(0,UI_LEFT|UI_TOP);
  601. uiSetMargins(0,600,10,0,0);
  602. uiSetSize(0,250,400);
  603. int col = column(root);
  604. uiSetMargins(col, 10, 10, 10, 10);
  605. uiSetLayout(col, UI_TOP|UI_HFILL);
  606. button(col, 1, BND_ICONID(6,3), "Item 1", demohandler);
  607. button(col, 2, BND_ICONID(6,3), "Item 2", demohandler);
  608. {
  609. int h = hgroup(col);
  610. radio(h, 3, BND_ICONID(6,3), "Item 3.0", &enum1);
  611. radio(h, 4, BND_ICONID(0,10), NULL, &enum1);
  612. radio(h, 5, BND_ICONID(1,10), NULL, &enum1);
  613. radio(h, 6, BND_ICONID(6,3), "Item 3.3", &enum1);
  614. }
  615. {
  616. int rows = row(col);
  617. int coll = vgroup(rows);
  618. label(coll, -1, "Items 4.0:");
  619. coll = vgroup(coll);
  620. button(coll, 7, BND_ICONID(6,3), "Item 4.0.0", demohandler);
  621. button(coll, 8, BND_ICONID(6,3), "Item 4.0.1", demohandler);
  622. int colr = vgroup(rows);
  623. uiSetFrozen(colr, option1);
  624. label(colr, -1, "Items 4.1:");
  625. colr = vgroup(colr);
  626. slider(colr, 9, "Item 4.1.0", &progress1);
  627. slider(colr,10, "Item 4.1.1", &progress2);
  628. }
  629. button(col, 11, BND_ICONID(6,3), "Item 5", NULL);
  630. check(col, 12, "Frozen", &option1);
  631. check(col, 13, "Item 7", &option2);
  632. check(col, 14, "Item 8", &option3);
  633. static char textbuffer[32] = "click and edit";
  634. textbox(col, (UIhandle)textbuffer, textbuffer, 32);
  635. uiLayout();
  636. drawUI(vg, 0, 0, 0);
  637. uiProcess();
  638. }
  639. ////////////////////////////////////////////////////////////////////////////////
  640. void errorcb(int error, const char* desc)
  641. {
  642. printf("GLFW error %d: %s\n", error, desc);
  643. }
  644. static void mousebutton(GLFWwindow *window, int button, int action, int mods) {
  645. NVG_NOTUSED(window);
  646. NVG_NOTUSED(mods);
  647. uiSetButton(button, (action==GLFW_PRESS)?1:0);
  648. }
  649. static void cursorpos(GLFWwindow *window, double x, double y) {
  650. NVG_NOTUSED(window);
  651. uiSetCursor((int)x,(int)y);
  652. }
  653. static void charevent(GLFWwindow *window, unsigned int value) {
  654. NVG_NOTUSED(window);
  655. uiSetChar(value);
  656. }
  657. static void key(GLFWwindow* window, int key, int scancode, int action, int mods)
  658. {
  659. NVG_NOTUSED(scancode);
  660. NVG_NOTUSED(mods);
  661. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  662. glfwSetWindowShouldClose(window, GL_TRUE);
  663. uiSetKey(key, mods, action);
  664. }
  665. int main()
  666. {
  667. GLFWwindow* window;
  668. struct NVGcontext* vg = NULL;
  669. UIcontext *uictx;
  670. uictx = uiCreateContext();
  671. uiMakeCurrent(uictx);
  672. if (!glfwInit()) {
  673. printf("Failed to init GLFW.");
  674. return -1;
  675. }
  676. glfwSetErrorCallback(errorcb);
  677. #ifndef _WIN32 // don't require this on win32, and works with more cards
  678. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  679. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  680. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  681. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  682. #endif
  683. glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, 1);
  684. window = glfwCreateWindow(1000, 600, "OUI Blendish Demo", NULL, NULL);
  685. if (!window) {
  686. glfwTerminate();
  687. return -1;
  688. }
  689. glfwSetKeyCallback(window, key);
  690. glfwSetCharCallback(window, charevent);
  691. glfwSetCursorPosCallback(window, cursorpos);
  692. glfwSetMouseButtonCallback(window, mousebutton);
  693. glfwMakeContextCurrent(window);
  694. #ifdef NANOVG_GLEW
  695. glewExperimental = GL_TRUE;
  696. if(glewInit() != GLEW_OK) {
  697. printf("Could not init glew.\n");
  698. return -1;
  699. }
  700. // GLEW generates GL error because it calls glGetString(GL_EXTENSIONS), we'll consume it here.
  701. glGetError();
  702. #endif
  703. vg = nvgCreateGL3(NVG_ANTIALIAS | NVG_STENCIL_STROKES);
  704. if (vg == NULL) {
  705. printf("Could not init nanovg.\n");
  706. return -1;
  707. }
  708. init(vg);
  709. glfwSwapInterval(0);
  710. glfwSetTime(0);
  711. while (!glfwWindowShouldClose(window))
  712. {
  713. double mx, my;
  714. int winWidth, winHeight;
  715. int fbWidth, fbHeight;
  716. float pxRatio;
  717. glfwGetCursorPos(window, &mx, &my);
  718. glfwGetWindowSize(window, &winWidth, &winHeight);
  719. glfwGetFramebufferSize(window, &fbWidth, &fbHeight);
  720. // Calculate pixel ration for hi-dpi devices.
  721. pxRatio = (float)fbWidth / (float)winWidth;
  722. // Update and render
  723. glViewport(0, 0, fbWidth, fbHeight);
  724. glClearColor(0,0,0,1);
  725. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
  726. nvgBeginFrame(vg, winWidth, winHeight, pxRatio);
  727. draw(vg, winWidth, winHeight);
  728. nvgEndFrame(vg);
  729. glfwSwapBuffers(window);
  730. glfwPollEvents();
  731. }
  732. uiDestroyContext(uictx);
  733. nvgDeleteGL3(vg);
  734. glfwTerminate();
  735. return 0;
  736. }