git.haldean.org ana / d017f0d
a bunch of ui improvements, starting to hook up glfw callbacks Haldean Brown 2 years ago
6 changed file(s) with 153 addition(s) and 35 deletion(s). Raw diff Collapse all Expand all
66 src/ui.cpp \
77 src/viewport.cpp \
88 lib/imgui/imgui.cpp \
9 lib/imgui/imgui_demo.cpp \
910 lib/imgui/imgui_draw.cpp \
1011 lib/imgui/imgui_widgets.cpp \
1112 lib/imgui/examples/imgui_impl_opengl3.cpp \
1516 deps = $(patsubst %.cpp,%.d,$(c_sources))
1617
1718 CXXFLAGS += -DIMGUI_IMPL_OPENGL_LOADER_GLEW
19 CXXFLAGS += -DIMGUI_DISABLE_OBSOLETE_FUNCTIONS
1820 CXXFLAGS += -I lib/imgui
1921 CXXFLAGS += -I lib/imgui/examples
2022 CXXFLAGS += -I lib/glew-2.1.0/include
2123 CXXFLAGS += -I lib/glfw-3.2.1.bin.WIN64/include
2224 CXXFLAGS += -I lib/glm
2325 CXXFLAGS += -MD
26 CXXFLAGS += -ggdb
2427
2528 LDFLAGS += lib\glew-2.1.0\lib\Release\x64\glew32.lib
2629 LDFLAGS += lib\glfw-3.2.1.bin.WIN64\lib-mingw-w64\libglfw3.a
2831 LDFLAGS += -lgdi32
2932 LDFLAGS += -lglu32
3033 LDFLAGS += -lshell32
34 LDFLAGS += -ggdb
3135
3236 dist: build/ana.exe build/glew32.dll
3337
34 build/glew32.dll: lib/glew-2.1.0/bin/Release/x64/glew32.dll
38 build:
39 mkdir build
40
41 build/glew32.dll: lib/glew-2.1.0/bin/Release/x64/glew32.dll build
3542 cp $< $@
3643
37 build/ana.exe: $(objects)
38 mkdir -p build
44 build/ana.exe: $(objects) build
3945 $(CXX) $(objects) $(LDFLAGS) -o $@
4046
4147 XD = tool/xd.exe
1313 #include "ui.hpp"
1414 #include "viewport.hpp"
1515
16 static ana::viewport viewport;
17
1618 void error_callback(int error, const char* description) {
1719 std::cerr << "[E] glfw error: " << description << std::endl;
20 }
21
22 void scroll_callback(GLFWwindow *w, double xoff, double yoff) {
23 ImGuiIO &io = ImGui::GetIO();
24 if (!io.WantCaptureMouse && yoff != 0) {
25 viewport.view_step(yoff);
26 }
27 }
28
29 void mouse_button_callback(GLFWwindow *w, int button, int action, int mods) {
30 ImGuiIO &io = ImGui::GetIO();
31 if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS && !io.WantCaptureMouse) {
32 std::cout << "start" << std::endl;
33 }
34 }
35
36 void framebuffer_size_callback(GLFWwindow* w, int width, int height) {
37 glViewport(0, 0, width, height);
38 viewport.set_aspect(width, height);
39 glfwSwapBuffers(w);
1840 }
1941
2042 int main(void) {
4264 return EXIT_FAILURE;
4365 }
4466
67 glfwSetMouseButtonCallback(window, mouse_button_callback);
68 glfwSetScrollCallback(window, scroll_callback);
69 glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
70
4571 if (!ana::shaders::init())
4672 return EXIT_FAILURE;
4773
5278 nodes.nodes.push_back(ana::node{glm::vec2(0, 0), glm::vec2(0.1, 0.2), "hello"});
5379 nodes.nodes.push_back(ana::node{glm::vec2(-0.5, -0.5), glm::vec2(0.2, 0.2), "hello"});
5480
55 ana::viewport viewport;
56
5781 ImGui::CreateContext();
5882 ana::ui::init_imgui();
5983
6084 ImGui_ImplGlfw_InitForOpenGL(window, true);
6185 ImGui_ImplOpenGL3_Init("#version 410");
6286
87 glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
88
6389 while (!glfwWindowShouldClose(window)) {
6490 glfwPollEvents();
91
6592 ImGui_ImplOpenGL3_NewFrame();
6693 ImGui_ImplGlfw_NewFrame();
6794 ImGui::NewFrame();
6895
69 ImGui::Begin("Hello");
70 if (ImGui::Button("Hello?")) {
71 std::cout << "Hello!" << std::endl;
72 }
73 ImGui::End();
96 ImGui::ShowDemoWindow();
7497
75 //ImGui_ImplSDL2_NewFrame(window);
7698 /*
7799 SDL_Event ev;
78100 while (SDL_PollEvent(&ev)) {
82104 if (ev.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
83105 glViewport(0, 0, ev.window.data1, ev.window.data2);
84106 viewport.set_aspect(ev.window.data1, ev.window.data2);
85 }
86 break;
87
88 case SDL_MOUSEWHEEL:
89 if (!io.WantCaptureMouse && ev.wheel.y != 0) {
90 viewport.view_step(ev.wheel.y);
91107 }
92108 break;
93109
101117 */
102118 ImGui::Render();
103119
104 int display_w, display_h;
105
106 glfwMakeContextCurrent(window);
107 glfwGetFramebufferSize(window, &display_w, &display_h);
108 glViewport(0, 0, display_w, display_h);
109
110 glClearColor(0.15f, 0.12f, 0.1f, 1.0f);
111120 glClear(GL_COLOR_BUFFER_BIT);
112
113121 ana::shaders::draw(viewport, nodes);
114122 ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
115
116123 glfwMakeContextCurrent(window);
117124 glfwSwapBuffers(window);
118125 }
00 #include <stddef.h>
11
2 extern unsigned char *font_data;
2 extern const unsigned char *font_data;
33 extern const size_t font_data_len;
00 #include "font.hpp"
11 #include "ui.hpp"
22 #include "imgui.h"
3 #include <cstdlib>
34
45 namespace ana {
56 namespace ui {
67
7 void init_imgui() {
8 ImGuiIO &io = ImGui::GetIO();
9 io.Fonts->AddFontFromMemoryTTF(font_data, font_data_len, 16);
10 }
8 static ImVec4 base = ImVec4(0.502f, 0.075f, 0.256f, 1.0f);
9 static ImVec4 bg = ImVec4(0.200f, 0.220f, 0.270f, 1.0f);
10 static ImVec4 text = ImVec4(0.860f, 0.930f, 0.890f, 1.0f);
11 static float high_val = 0.8f;
12 static float mid_val = 0.5f;
13 static float low_val = 0.3f;
14 static float window_offset = -0.2f;
15
16 inline ImVec4 make_high(float alpha) {
17 ImVec4 res(0, 0, 0, alpha);
18 ImGui::ColorConvertRGBtoHSV(base.x, base.y, base.z, res.x, res.y, res.z);
19 res.z = high_val;
20 ImGui::ColorConvertHSVtoRGB(res.x, res.y, res.z, res.x, res.y, res.z);
21 return res;
22 }
23
24 inline ImVec4 make_mid(float alpha) {
25 ImVec4 res(0, 0, 0, alpha);
26 ImGui::ColorConvertRGBtoHSV(base.x, base.y, base.z, res.x, res.y, res.z);
27 res.z = mid_val;
28 ImGui::ColorConvertHSVtoRGB(res.x, res.y, res.z, res.x, res.y, res.z);
29 return res;
30 }
31
32 inline ImVec4 make_low(float alpha) {
33 ImVec4 res(0, 0, 0, alpha);
34 ImGui::ColorConvertRGBtoHSV(base.x, base.y, base.z, res.x, res.y, res.z);
35 res.z = low_val;
36 ImGui::ColorConvertHSVtoRGB(res.x, res.y, res.z, res.x, res.y, res.z);
37 return res;
38 }
39
40 inline ImVec4 make_bg(float alpha, float offset = 0.f) {
41 ImVec4 res(0, 0, 0, alpha);
42 ImGui::ColorConvertRGBtoHSV(bg.x, bg.y, bg.z, res.x, res.y, res.z);
43 res.z += offset;
44 ImGui::ColorConvertHSVtoRGB(res.x, res.y, res.z, res.x, res.y, res.z);
45 return res;
46 }
47
48 inline ImVec4 make_text(float alpha) {
49 return ImVec4(text.x, text.y, text.z, alpha);
50 }
51
52 void init_imgui() {
53 ImGuiIO &io = ImGui::GetIO();
54 void *data = malloc(font_data_len);
55 memcpy(data, &font_data, font_data_len);
56 io.Fonts->AddFontFromMemoryTTF(data, font_data_len, 24);
57
58 io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
59
60 ImGuiStyle &style = ImGui::GetStyle();
61 style.FrameRounding = 3.0f;
62 style.GrabRounding = 2.0f;
63
64 auto &colors = style.Colors;
65 colors[ImGuiCol_Text] = ImVec4(1.00f, 0.90f, 0.92f, 0.78f);
66 colors[ImGuiCol_TextDisabled] = ImVec4(1.00f, 0.90f, 0.92f, 0.28f);
67 colors[ImGuiCol_WindowBg] = ImVec4(0.06f, 0.04f, 0.04f, 1.00f);
68 colors[ImGuiCol_ChildBg] = ImVec4(0.14f, 0.08f, 0.09f, 0.58f);
69 colors[ImGuiCol_PopupBg] = ImVec4(0.14f, 0.08f, 0.09f, 0.90f);
70 colors[ImGuiCol_Border] = ImVec4(0.09f, 0.05f, 0.06f, 0.60f);
71 colors[ImGuiCol_BorderShadow] = ImVec4(0.14f, 0.08f, 0.09f, 0.00f);
72 colors[ImGuiCol_FrameBg] = ImVec4(0.14f, 0.08f, 0.09f, 1.00f);
73 colors[ImGuiCol_FrameBgHovered] = ImVec4(0.60f, 0.10f, 0.20f, 0.78f);
74 colors[ImGuiCol_FrameBgActive] = ImVec4(0.60f, 0.10f, 0.20f, 1.00f);
75 colors[ImGuiCol_TitleBg] = ImVec4(0.23f, 0.04f, 0.08f, 1.00f);
76 colors[ImGuiCol_TitleBgActive] = ImVec4(0.76f, 0.13f, 0.26f, 1.00f);
77 colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.14f, 0.08f, 0.09f, 0.75f);
78 colors[ImGuiCol_MenuBarBg] = ImVec4(0.14f, 0.08f, 0.09f, 0.47f);
79 colors[ImGuiCol_ScrollbarBg] = ImVec4(0.14f, 0.08f, 0.09f, 1.00f);
80 colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.23f, 0.04f, 0.08f, 1.00f);
81 colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.60f, 0.10f, 0.20f, 0.78f);
82 colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.60f, 0.10f, 0.20f, 1.00f);
83 colors[ImGuiCol_CheckMark] = ImVec4(0.76f, 0.13f, 0.26f, 1.00f);
84 colors[ImGuiCol_SliderGrab] = ImVec4(0.24f, 0.14f, 0.16f, 1.00f);
85 colors[ImGuiCol_SliderGrabActive] = ImVec4(0.76f, 0.13f, 0.26f, 1.00f);
86 colors[ImGuiCol_Button] = ImVec4(0.34f, 0.20f, 0.23f, 1.00f);
87 colors[ImGuiCol_ButtonHovered] = ImVec4(0.60f, 0.10f, 0.20f, 1.00f);
88 colors[ImGuiCol_ButtonActive] = ImVec4(0.76f, 0.13f, 0.26f, 1.00f);
89 colors[ImGuiCol_Header] = ImVec4(0.60f, 0.10f, 0.20f, 0.76f);
90 colors[ImGuiCol_HeaderHovered] = ImVec4(0.60f, 0.10f, 0.20f, 0.86f);
91 colors[ImGuiCol_HeaderActive] = ImVec4(0.76f, 0.13f, 0.26f, 1.00f);
92 colors[ImGuiCol_Separator] = ImVec4(0.43f, 0.43f, 0.50f, 0.50f);
93 colors[ImGuiCol_SeparatorHovered] = ImVec4(0.10f, 0.40f, 0.75f, 0.78f);
94 colors[ImGuiCol_SeparatorActive] = ImVec4(0.10f, 0.40f, 0.75f, 1.00f);
95 colors[ImGuiCol_ResizeGrip] = ImVec4(0.47f, 0.77f, 0.83f, 0.04f);
96 colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.60f, 0.10f, 0.20f, 0.78f);
97 colors[ImGuiCol_ResizeGripActive] = ImVec4(0.60f, 0.10f, 0.20f, 1.00f);
98 colors[ImGuiCol_Tab] = ImVec4(0.14f, 0.08f, 0.09f, 0.40f);
99 colors[ImGuiCol_TabHovered] = ImVec4(0.76f, 0.13f, 0.26f, 1.00f);
100 colors[ImGuiCol_TabActive] = ImVec4(0.60f, 0.10f, 0.20f, 1.00f);
101 colors[ImGuiCol_TabUnfocused] = ImVec4(0.14f, 0.08f, 0.09f, 0.40f);
102 colors[ImGuiCol_TabUnfocusedActive] = ImVec4(0.14f, 0.08f, 0.09f, 0.70f);
103 colors[ImGuiCol_DockingPreview] = ImVec4(0.76f, 0.13f, 0.26f, 0.30f);
104 colors[ImGuiCol_DockingEmptyBg] = ImVec4(0.20f, 0.20f, 0.20f, 1.00f);
105 colors[ImGuiCol_PlotLines] = ImVec4(1.00f, 0.90f, 0.92f, 0.63f);
106 colors[ImGuiCol_PlotLinesHovered] = ImVec4(0.60f, 0.10f, 0.20f, 1.00f);
107 colors[ImGuiCol_PlotHistogram] = ImVec4(1.00f, 0.90f, 0.92f, 0.63f);
108 colors[ImGuiCol_PlotHistogramHovered] = ImVec4(0.60f, 0.10f, 0.20f, 1.00f);
109 colors[ImGuiCol_TextSelectedBg] = ImVec4(0.60f, 0.10f, 0.20f, 0.43f);
110 colors[ImGuiCol_DragDropTarget] = ImVec4(1.00f, 1.00f, 0.00f, 0.90f);
111 colors[ImGuiCol_NavHighlight] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);
112 colors[ImGuiCol_NavWindowingHighlight] = ImVec4(1.00f, 1.00f, 1.00f, 0.70f);
113 colors[ImGuiCol_NavWindowingDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.20f);
114 colors[ImGuiCol_ModalWindowDimBg] = ImVec4(0.14f, 0.08f, 0.09f, 0.73f);
115 }
11116
12117 }
13118 }
11
22 namespace ana {
33 namespace ui {
4 void init_imgui();
4 void init_imgui();
55 }
66 }
372372 char cout[80];
373373 unsigned long len = 0;
374374
375 fprintf(out, "#include <stddef.h>\n");
375 fprintf(out, "#include <stddef.h>\n\n");
376376 fprintf(out, "unsigned char %s[] = {\n",
377377 clabel[0] == EOS ? "xd_data" : clabel);
378378 strcpy(cout, " ");
383383 fprintf(out, "%s\n", cout);
384384 strcpy(cout, " ");
385385 }
386 sprintf(cout + strlen(cout), "%d,", b);
386 sprintf(cout + strlen(cout), "0x%02X,", b);
387387 }
388388 if (strlen(cout) > 4) {
389389 cout[strlen(cout) - 1] = EOS; /* Strip trailing comma */
390390 fprintf(out, "%s\n", cout);
391391 }
392392 fprintf(out, "};\n");
393 fprintf(out, "size_t %s_len = %ul;\n", clabel[0] == EOS ? "xd_data" : clabel, len);
393 fprintf(out, "size_t %s_len = %lu;\n", clabel[0] == EOS ? "xd_data" : clabel, len);
394394 } else {
395395 while ((b = getc(in)) != EOF) {
396396 if (bp >= bytesperline) {