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.

312 lines
9.6KB

  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_icons16.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. const char edit_text[] = "The quick brown fox";
  131. int textlen = strlen(edit_text)+1;
  132. int t = int(glfwGetTime()*2);
  133. int idx1 = (t/textlen)%textlen;
  134. int idx2 = idx1 + (t%(textlen-idx1));
  135. ry += 25;
  136. bndTextField(vg,rx,ry,240,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_DEFAULT,
  137. -1, edit_text, idx1, idx2);
  138. ry += 25;
  139. bndTextField(vg,rx,ry,240,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_HOVER,
  140. -1, edit_text, idx1, idx2);
  141. ry += 25;
  142. bndTextField(vg,rx,ry,240,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_ACTIVE,
  143. -1, edit_text, idx1, idx2);
  144. rx += rw + 20;
  145. ry = 10;
  146. bndScrollBar(vg,rx,ry,BND_SCROLLBAR_WIDTH,240,BND_DEFAULT,s_offset,s_size);
  147. rx += 20;
  148. bndScrollBar(vg,rx,ry,BND_SCROLLBAR_WIDTH,240,BND_HOVER,s_offset,s_size);
  149. rx += 20;
  150. bndScrollBar(vg,rx,ry,BND_SCROLLBAR_WIDTH,240,BND_ACTIVE,s_offset,s_size);
  151. x = ox;
  152. y += 40;
  153. bndToolButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_RIGHT,
  154. BND_DEFAULT,BND_ICONID(0,10),NULL);
  155. x += BND_TOOL_WIDTH-1;
  156. bndToolButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
  157. BND_DEFAULT,BND_ICONID(1,10),NULL);
  158. x += BND_TOOL_WIDTH-1;
  159. bndToolButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
  160. BND_DEFAULT,BND_ICONID(2,10),NULL);
  161. x += BND_TOOL_WIDTH-1;
  162. bndToolButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
  163. BND_DEFAULT,BND_ICONID(3,10),NULL);
  164. x += BND_TOOL_WIDTH-1;
  165. bndToolButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
  166. BND_DEFAULT,BND_ICONID(4,10),NULL);
  167. x += BND_TOOL_WIDTH-1;
  168. bndToolButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_LEFT,
  169. BND_DEFAULT,BND_ICONID(5,10),NULL);
  170. x += BND_TOOL_WIDTH-1;
  171. x += 5;
  172. bndRadioButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_RIGHT,
  173. BND_DEFAULT,BND_ICONID(0,11),NULL);
  174. x += BND_TOOL_WIDTH-1;
  175. bndRadioButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
  176. BND_DEFAULT,BND_ICONID(1,11),NULL);
  177. x += BND_TOOL_WIDTH-1;
  178. bndRadioButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
  179. BND_DEFAULT,BND_ICONID(2,11),NULL);
  180. x += BND_TOOL_WIDTH-1;
  181. bndRadioButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
  182. BND_DEFAULT,BND_ICONID(3,11),NULL);
  183. x += BND_TOOL_WIDTH-1;
  184. bndRadioButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
  185. BND_ACTIVE,BND_ICONID(4,11),NULL);
  186. x += BND_TOOL_WIDTH-1;
  187. bndRadioButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_LEFT,
  188. BND_DEFAULT,BND_ICONID(5,11),NULL);
  189. }
  190. ////////////////////////////////////////////////////////////////////////////////
  191. void errorcb(int error, const char* desc)
  192. {
  193. printf("GLFW error %d: %s\n", error, desc);
  194. }
  195. static void key(GLFWwindow* window, int key, int scancode, int action, int mods)
  196. {
  197. NVG_NOTUSED(scancode);
  198. NVG_NOTUSED(mods);
  199. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  200. glfwSetWindowShouldClose(window, GL_TRUE);
  201. }
  202. int main()
  203. {
  204. GLFWwindow* window;
  205. struct NVGcontext* vg = NULL;
  206. if (!glfwInit()) {
  207. printf("Failed to init GLFW.");
  208. return -1;
  209. }
  210. glfwSetErrorCallback(errorcb);
  211. #ifndef _WIN32 // don't require this on win32, and works with more cards
  212. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  213. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  214. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  215. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  216. #endif
  217. glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, 1);
  218. window = glfwCreateWindow(1000, 600, "Blendish Demo", NULL, NULL);
  219. if (!window) {
  220. glfwTerminate();
  221. return -1;
  222. }
  223. glfwSetKeyCallback(window, key);
  224. glfwMakeContextCurrent(window);
  225. #ifdef NANOVG_GLEW
  226. glewExperimental = GL_TRUE;
  227. if(glewInit() != GLEW_OK) {
  228. printf("Could not init glew.\n");
  229. return -1;
  230. }
  231. // GLEW generates GL error because it calls glGetString(GL_EXTENSIONS), we'll consume it here.
  232. glGetError();
  233. #endif
  234. vg = nvgCreateGL3(NVG_ANTIALIAS | NVG_STENCIL_STROKES);
  235. if (vg == NULL) {
  236. printf("Could not init nanovg.\n");
  237. return -1;
  238. }
  239. init(vg);
  240. glfwSwapInterval(0);
  241. glfwSetTime(0);
  242. while (!glfwWindowShouldClose(window))
  243. {
  244. double mx, my;
  245. int winWidth, winHeight;
  246. int fbWidth, fbHeight;
  247. float pxRatio;
  248. glfwGetCursorPos(window, &mx, &my);
  249. glfwGetWindowSize(window, &winWidth, &winHeight);
  250. glfwGetFramebufferSize(window, &fbWidth, &fbHeight);
  251. // Calculate pixel ration for hi-dpi devices.
  252. pxRatio = (float)fbWidth / (float)winWidth;
  253. // Update and render
  254. glViewport(0, 0, fbWidth, fbHeight);
  255. glClearColor(0,0,0,1);
  256. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
  257. nvgBeginFrame(vg, winWidth, winHeight, pxRatio);
  258. draw(vg, winWidth, winHeight);
  259. nvgEndFrame(vg);
  260. glfwSwapBuffers(window);
  261. glfwPollEvents();
  262. }
  263. nvgDeleteGL3(vg);
  264. glfwTerminate();
  265. return 0;
  266. }