Created
May 6, 2017 02:28
-
-
Save OlegJakushkin/ead8fa88ed3cfff633fba56f4272b1bf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SDL.h> | |
#include <GLFW/glfw3.h> | |
#include <imgui/imgui.h> | |
#include <imgui/imgui_impl_glfw.h> | |
#include <vector> | |
#include<time.h> | |
// Model | |
struct Line { | |
ImVec2 from, to; | |
unsigned int color; | |
}; | |
struct Image { | |
std::vector<Line> lines; | |
}; | |
//UI | |
int main(int, char**) { | |
srand(time(0)); | |
// Setup window | |
glfwSetErrorCallback(error_callback); | |
if (!glfwInit()) { | |
exit(1); | |
} | |
auto w=1280, h=720; | |
auto window = glfwCreateWindow(w, h, "ImGui OpenGL2 example", NULL, NULL); | |
glfwMakeContextCurrent(window); | |
// Setup ImGui binding | |
ImGui_ImplGlfw_Init(window, true); | |
ImVec4 clear_color = ImColor(114, 144, 154); | |
auto myColor = ImColor(rand()%255, rand()%200, rand()%200); | |
auto opened = true; | |
Image image; | |
Line current; | |
current.color = myColor; | |
auto adding_line = false; | |
// Main loop | |
while (!glfwWindowShouldClose(window)) { | |
glfwPollEvents(); | |
ImGui_ImplGlfw_NewFrame(); | |
ImGui::SetNextWindowSize(ImVec2(w-30,h-30), ImGuiSetCond_FirstUseEver); | |
if (!ImGui::Begin("Example: Custom Rendering",&opened)) { | |
ImGui::End(); | |
return 0; | |
} | |
if (image.lines.size() > 0) { | |
ImGui::SameLine(); | |
if (ImGui::Button("Undo")) { | |
image.lines.pop_back(); | |
} | |
} | |
ImGui::Text("Your Color"); | |
ImGui::SameLine(); | |
ImGui::PushStyleColor(ImGuiCol_Button, myColor); | |
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, myColor); | |
ImGui::PushStyleColor(ImGuiCol_ButtonActive, myColor); | |
ImGui::Button("------"); | |
ImGui::PopStyleColor(3); | |
ImGui::Text("Left-click and drag to add lines"); | |
ImGui::SameLine(); | |
ImGui::Text("Right-click to undo"); | |
auto draw_list = ImGui::GetWindowDrawList(); | |
auto canvas_pos = ImGui::GetCursorScreenPos(); // ImDrawList API uses screen coordinates! | |
auto canvas_size = ImVec2(ImMax(50.0f,ImGui::GetWindowContentRegionMax().x-ImGui::GetCursorPos().x), | |
ImMax(50.0f,ImGui::GetWindowContentRegionMax().y-ImGui::GetCursorPos().y)); // Resize canvas what's available | |
draw_list->AddRect(canvas_pos, ImVec2(canvas_pos.x + canvas_size.x, canvas_pos.y + canvas_size.y), 0xFFFFFFFF); | |
auto adding_preview = false; | |
ImGui::InvisibleButton("canvas", canvas_size); | |
if (ImGui::IsItemHovered()) { | |
auto mouse_pos_in_canvas = ImVec2(ImGui::GetIO().MousePos.x - canvas_pos.x, ImGui::GetIO().MousePos.y - canvas_pos.y); | |
if (!adding_line && ImGui::GetIO().MouseClicked[0]) { | |
current.from = mouse_pos_in_canvas; | |
adding_line = true; | |
} | |
if (adding_line) { | |
adding_preview = true; | |
current.to = mouse_pos_in_canvas; | |
image.lines.push_back(current); // will be removed if preview_mode before next frame | |
if (!ImGui::GetIO().MouseDown[0]) { | |
adding_line = adding_preview = false; | |
} | |
} | |
if (ImGui::GetIO().MouseClicked[1] && !image.lines.empty()) { | |
adding_line = false; | |
image.lines.pop_back(); | |
} | |
} | |
draw_list->PushClipRect(ImVec4(canvas_pos.x, | |
canvas_pos.y, | |
canvas_pos.x+canvas_size.x, | |
canvas_pos.y+canvas_size.y)); // clip lines within the canvas (if we resize it, etc.) | |
for (auto l : image.lines) { | |
draw_list->AddLine(canvas_pos + l.from, | |
canvas_pos + l.to, l.color); | |
} | |
draw_list->PopClipRect(); | |
if (adding_preview) { | |
image.lines.pop_back(); | |
} | |
ImGui::End(); | |
// Rendering | |
glViewport(0, 0, static_cast<int>(ImGui::GetIO().DisplaySize.x), | |
static_cast<int>(ImGui::GetIO().DisplaySize.y)); | |
glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w); | |
glClear(GL_COLOR_BUFFER_BIT); | |
ImGui::Render(); | |
glfwSwapBuffers(window); | |
} | |
// Cleanup | |
ImGui_ImplGlfw_Shutdown(); | |
glfwTerminate(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment