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.

296 lines
9.0KB

  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. ////////////////////////////////////////////////////////////////////////////////
  17. void init(NVGcontext *vg) {
  18. bndSetFont(nvgCreateFont(vg, "system", "droidsans.ttf"));
  19. bndSetIconImage(nvgCreateImage(vg, "blender_icons.png"));
  20. }
  21. void draw(NVGcontext *vg, float w, float h) {
  22. bndBackground(vg, 0, 0, w, h);
  23. int x = 10;
  24. int y = 10;
  25. bndToolButton(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_DEFAULT,
  26. BND_ICONID(6,3),"Default");
  27. y += 25;
  28. bndToolButton(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_HOVER,
  29. BND_ICONID(6,3),"Hovered");
  30. y += 25;
  31. bndToolButton(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_ACTIVE,
  32. BND_ICONID(6,3),"Active");
  33. y += 40;
  34. bndRadioButton(vg,x,y,80,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_DEFAULT,
  35. -1,"Default");
  36. y += 25;
  37. bndRadioButton(vg,x,y,80,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_HOVER,
  38. -1,"Hovered");
  39. y += 25;
  40. bndRadioButton(vg,x,y,80,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_ACTIVE,
  41. -1,"Active");
  42. y += 25;
  43. bndLabel(vg,x,y,120,BND_WIDGET_HEIGHT,-1,"Label:");
  44. y += BND_WIDGET_HEIGHT;
  45. bndChoiceButton(vg,x,y,80,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_DEFAULT,
  46. -1, "Default");
  47. y += 25;
  48. bndChoiceButton(vg,x,y,80,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_HOVER,
  49. -1, "Hovered");
  50. y += 25;
  51. bndChoiceButton(vg,x,y,80,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_ACTIVE,
  52. -1, "Active");
  53. y += 25;
  54. int ry = y;
  55. int rx = x;
  56. y = 10;
  57. x += 130;
  58. bndOptionButton(vg,x,y,120,BND_WIDGET_HEIGHT,BND_DEFAULT,"Default");
  59. y += 25;
  60. bndOptionButton(vg,x,y,120,BND_WIDGET_HEIGHT,BND_HOVER,"Hovered");
  61. y += 25;
  62. bndOptionButton(vg,x,y,120,BND_WIDGET_HEIGHT,BND_ACTIVE,"Active");
  63. y += 40;
  64. bndNumberField(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_DOWN,BND_DEFAULT,
  65. "Top","100");
  66. y += BND_WIDGET_HEIGHT-2;
  67. bndNumberField(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_ALL,BND_DEFAULT,
  68. "Center","100");
  69. y += BND_WIDGET_HEIGHT-2;
  70. bndNumberField(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_TOP,BND_DEFAULT,
  71. "Bottom","100");
  72. int mx = x-30;
  73. int my = y-12;
  74. int mw = 120;
  75. bndMenuBackground(vg,mx,my,mw,120,BND_CORNER_TOP);
  76. bndMenuLabel(vg,mx,my,mw,BND_WIDGET_HEIGHT,-1,"Menu Title");
  77. my += BND_WIDGET_HEIGHT-2;
  78. bndMenuItem(vg,mx,my,mw,BND_WIDGET_HEIGHT,BND_DEFAULT,
  79. BND_ICONID(17,3),"Default");
  80. my += BND_WIDGET_HEIGHT-2;
  81. bndMenuItem(vg,mx,my,mw,BND_WIDGET_HEIGHT,BND_HOVER,
  82. BND_ICONID(18,3),"Hovered");
  83. my += BND_WIDGET_HEIGHT-2;
  84. bndMenuItem(vg,mx,my,mw,BND_WIDGET_HEIGHT,BND_ACTIVE,
  85. BND_ICONID(19,3),"Active");
  86. y = 10;
  87. x += 130;
  88. int ox = x;
  89. bndNumberField(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_DEFAULT,
  90. "Default","100");
  91. y += 25;
  92. bndNumberField(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_HOVER,
  93. "Hovered","100");
  94. y += 25;
  95. bndNumberField(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_ACTIVE,
  96. "Active","100");
  97. y += 40;
  98. bndRadioButton(vg,x,y,60,BND_WIDGET_HEIGHT,BND_CORNER_RIGHT,BND_DEFAULT,
  99. -1,"One");
  100. x += 60-1;
  101. bndRadioButton(vg,x,y,60,BND_WIDGET_HEIGHT,BND_CORNER_ALL,BND_DEFAULT,
  102. -1,"Two");
  103. x += 60-1;
  104. bndRadioButton(vg,x,y,60,BND_WIDGET_HEIGHT,BND_CORNER_ALL,BND_DEFAULT,
  105. -1,"Three");
  106. x += 60-1;
  107. bndRadioButton(vg,x,y,60,BND_WIDGET_HEIGHT,BND_CORNER_LEFT,BND_ACTIVE,
  108. -1,"Butts");
  109. x = ox;
  110. y += 40;
  111. float progress_value = fmodf(glfwGetTime()/10.0,1.0);
  112. char progress_label[32];
  113. sprintf(progress_label, "%d%%", int(progress_value*100+0.5f));
  114. bndSlider(vg,x,y,240,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_DEFAULT,
  115. progress_value,"Default",progress_label);
  116. y += 25;
  117. bndSlider(vg,x,y,240,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_HOVER,
  118. progress_value,"Hovered",progress_label);
  119. y += 25;
  120. bndSlider(vg,x,y,240,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_ACTIVE,
  121. progress_value,"Active",progress_label);
  122. int rw = x+240-rx;
  123. float s_offset = sinf(glfwGetTime()/2.0)*0.5+0.5;
  124. float s_size = cosf(glfwGetTime()/3.11)*0.5+0.5;
  125. bndScrollBar(vg,rx,ry,rw,BND_SCROLLBAR_HEIGHT,BND_DEFAULT,s_offset,s_size);
  126. ry += 20;
  127. bndScrollBar(vg,rx,ry,rw,BND_SCROLLBAR_HEIGHT,BND_HOVER,s_offset,s_size);
  128. ry += 20;
  129. bndScrollBar(vg,rx,ry,rw,BND_SCROLLBAR_HEIGHT,BND_ACTIVE,s_offset,s_size);
  130. rx += rw + 20;
  131. ry = 10;
  132. bndScrollBar(vg,rx,ry,BND_SCROLLBAR_WIDTH,240,BND_DEFAULT,s_offset,s_size);
  133. rx += 20;
  134. bndScrollBar(vg,rx,ry,BND_SCROLLBAR_WIDTH,240,BND_HOVER,s_offset,s_size);
  135. rx += 20;
  136. bndScrollBar(vg,rx,ry,BND_SCROLLBAR_WIDTH,240,BND_ACTIVE,s_offset,s_size);
  137. x = ox;
  138. y += 40;
  139. bndToolButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_RIGHT,
  140. BND_DEFAULT,BND_ICONID(0,10),NULL);
  141. x += BND_TOOL_WIDTH-1;
  142. bndToolButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
  143. BND_DEFAULT,BND_ICONID(1,10),NULL);
  144. x += BND_TOOL_WIDTH-1;
  145. bndToolButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
  146. BND_DEFAULT,BND_ICONID(2,10),NULL);
  147. x += BND_TOOL_WIDTH-1;
  148. bndToolButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
  149. BND_DEFAULT,BND_ICONID(3,10),NULL);
  150. x += BND_TOOL_WIDTH-1;
  151. bndToolButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
  152. BND_DEFAULT,BND_ICONID(4,10),NULL);
  153. x += BND_TOOL_WIDTH-1;
  154. bndToolButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_LEFT,
  155. BND_DEFAULT,BND_ICONID(5,10),NULL);
  156. x += BND_TOOL_WIDTH-1;
  157. x += 5;
  158. bndRadioButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_RIGHT,
  159. BND_DEFAULT,BND_ICONID(0,11),NULL);
  160. x += BND_TOOL_WIDTH-1;
  161. bndRadioButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
  162. BND_DEFAULT,BND_ICONID(1,11),NULL);
  163. x += BND_TOOL_WIDTH-1;
  164. bndRadioButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
  165. BND_DEFAULT,BND_ICONID(2,11),NULL);
  166. x += BND_TOOL_WIDTH-1;
  167. bndRadioButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
  168. BND_DEFAULT,BND_ICONID(3,11),NULL);
  169. x += BND_TOOL_WIDTH-1;
  170. bndRadioButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
  171. BND_ACTIVE,BND_ICONID(4,11),NULL);
  172. x += BND_TOOL_WIDTH-1;
  173. bndRadioButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_LEFT,
  174. BND_DEFAULT,BND_ICONID(5,11),NULL);
  175. }
  176. ////////////////////////////////////////////////////////////////////////////////
  177. void errorcb(int error, const char* desc)
  178. {
  179. printf("GLFW error %d: %s\n", error, desc);
  180. }
  181. static void key(GLFWwindow* window, int key, int scancode, int action, int mods)
  182. {
  183. NVG_NOTUSED(scancode);
  184. NVG_NOTUSED(mods);
  185. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  186. glfwSetWindowShouldClose(window, GL_TRUE);
  187. }
  188. int main()
  189. {
  190. GLFWwindow* window;
  191. struct NVGcontext* vg = NULL;
  192. if (!glfwInit()) {
  193. printf("Failed to init GLFW.");
  194. return -1;
  195. }
  196. glfwSetErrorCallback(errorcb);
  197. #ifndef _WIN32 // don't require this on win32, and works with more cards
  198. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  199. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  200. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  201. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  202. #endif
  203. glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, 1);
  204. window = glfwCreateWindow(1000, 600, "Blendish Demo", NULL, NULL);
  205. if (!window) {
  206. glfwTerminate();
  207. return -1;
  208. }
  209. glfwSetKeyCallback(window, key);
  210. glfwMakeContextCurrent(window);
  211. #ifdef NANOVG_GLEW
  212. glewExperimental = GL_TRUE;
  213. if(glewInit() != GLEW_OK) {
  214. printf("Could not init glew.\n");
  215. return -1;
  216. }
  217. // GLEW generates GL error because it calls glGetString(GL_EXTENSIONS), we'll consume it here.
  218. glGetError();
  219. #endif
  220. vg = nvgCreateGL3(NVG_ANTIALIAS | NVG_STENCIL_STROKES);
  221. if (vg == NULL) {
  222. printf("Could not init nanovg.\n");
  223. return -1;
  224. }
  225. init(vg);
  226. glfwSwapInterval(0);
  227. glfwSetTime(0);
  228. while (!glfwWindowShouldClose(window))
  229. {
  230. double mx, my;
  231. int winWidth, winHeight;
  232. int fbWidth, fbHeight;
  233. float pxRatio;
  234. glfwGetCursorPos(window, &mx, &my);
  235. glfwGetWindowSize(window, &winWidth, &winHeight);
  236. glfwGetFramebufferSize(window, &fbWidth, &fbHeight);
  237. // Calculate pixel ration for hi-dpi devices.
  238. pxRatio = (float)fbWidth / (float)winWidth;
  239. // Update and render
  240. glViewport(0, 0, fbWidth, fbHeight);
  241. glClearColor(0,0,0,1);
  242. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
  243. nvgBeginFrame(vg, winWidth, winHeight, pxRatio);
  244. draw(vg, winWidth, winHeight);
  245. nvgEndFrame(vg);
  246. glfwSwapBuffers(window);
  247. glfwPollEvents();
  248. }
  249. nvgDeleteGL3(vg);
  250. glfwTerminate();
  251. return 0;
  252. }