diff --git a/src/gooey.cpp b/src/gooey.cpp index cc40c24..93458d6 100644 --- a/src/gooey.cpp +++ b/src/gooey.cpp @@ -12,16 +12,21 @@ #include "examples/imgui_impl_opengl3.h" #include "camera.h" +#include "entity.h" #include "hexgrid.h" -#include "util.h" #include "gooey.h" +#include "util.h" #include "scene_loader.h" +#define GOOEY_BG_ALPHA 0.3f + // forward declarations -void renderRendererWindow(render_state* rs, ImGuiWindowFlags window_flags = 0); -void renderHexgridWindow(hexgrid& grid, ImGuiWindowFlags window_flags = 0); +void renderMenuBar(); +void renderRendererWindow(render_state* rs, ImGuiWindowFlags window_flags); +void renderHexgridWindow(hexgrid& grid, ImGuiWindowFlags window_flags); void renderHexInfo(hex_info* hxi); +void renderGSWindow(game_state* gs, ImGuiWindowFlags window_flags); bool @@ -62,11 +67,49 @@ gooRender(game_state* gs, render_state* rs) { assert(gs != nullptr && rs != nullptr); - ImGuiIO& io = ImGui::GetIO(); ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplSDL2_NewFrame(rs->handles.window); ImGui::NewFrame(); + ImGuiWindowFlags window_flags = 0; + window_flags |= ImGuiWindowFlags_NoScrollbar; + window_flags |= ImGuiWindowFlags_NoMove; + window_flags |= ImGuiWindowFlags_NoResize; + window_flags |= ImGuiWindowFlags_NoCollapse; + + // v2i rs->viewport_dims; + + uint top_pad = 20; + renderMenuBar(); + + uint rw_width = 250; + uint rw_height = 260; + ImGui::SetNextWindowPos(ImVec2(rs->viewport_dims.x - rw_width, top_pad)); + ImGui::SetNextWindowSize(ImVec2(rw_width, rw_height)); + renderRendererWindow(rs, window_flags); + + uint ew_height = 250; + ImGui::SetNextWindowPos(ImVec2(0, top_pad)); + ImGui::SetNextWindowSize(ImVec2(350, ew_height)); + renderGSWindow(gs, window_flags); + + uint hgw_height = 370; + ImGui::SetNextWindowPos(ImVec2(0, top_pad + ew_height)); + ImGui::SetNextWindowSize(ImVec2(350, hgw_height)); + renderHexgridWindow(gs->grid, window_flags); + + ImGui::Render(); + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); +} + + +// internal + +void +renderMenuBar() +{ + ImGuiIO& io = ImGui::GetIO(); + ImGui::BeginMainMenuBar(); if (ImGui::BeginMenu("Test")) { if (ImGui::MenuItem("Test 1")) {} @@ -86,31 +129,15 @@ gooRender(game_state* gs, render_state* rs) ImGui::SameLine(0, 800); ImGui::Text("%.3f ms/frame, (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate); ImGui::EndMainMenuBar(); - - ImGuiWindowFlags window_flags = 0; - window_flags |= ImGuiWindowFlags_NoScrollbar; - window_flags |= ImGuiWindowFlags_NoMove; - window_flags |= ImGuiWindowFlags_NoResize; - window_flags |= ImGuiWindowFlags_NoCollapse; - - renderRendererWindow(rs, window_flags); - renderHexgridWindow(gs->grid, window_flags); - - ImGui::Render(); - ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); } - -// internal - void renderRendererWindow(render_state* rs, ImGuiWindowFlags window_flags) { - ImGui::SetNextWindowPos(ImVec2(0, 20)); - ImGui::SetNextWindowSize(ImVec2(250, 250)); - ImGui::SetNextWindowBgAlpha(0.3f); + ImGui::SetNextWindowBgAlpha(GOOEY_BG_ALPHA); ImGui::Begin("Renderer", nullptr, window_flags); + ImGui::Text("viewport dims: %i, %i", rs->viewport_dims.x, rs->viewport_dims.y); ImGui::Checkbox("Debug Render", &rs->is_debug_draw); if (ImGui::CollapsingHeader("Camera Position", ImGuiTreeNodeFlags_DefaultOpen)) { @@ -132,12 +159,29 @@ renderRendererWindow(render_state* rs, ImGuiWindowFlags window_flags) ImGui::End(); } +void +renderGSWindow(game_state* gs, ImGuiWindowFlags window_flags) +{ + ImGui::SetNextWindowBgAlpha(GOOEY_BG_ALPHA); + ImGui::Begin("Game State", nullptr, window_flags); + + ImGui::Text("Select Mode"); + if (ImGui::RadioButton("Hex", (gs->select_mode == HEX_SELECT))) + gs->select_mode = HEX_SELECT; + if (ImGui::RadioButton("Entity", (gs->select_mode == ENTITY_SELECT))) + gs->select_mode = ENTITY_SELECT; + + if (gs->selected_entity) { + // TODO: need to implement entity selection + } + + ImGui::End(); +} + void renderHexgridWindow(hexgrid& grid, ImGuiWindowFlags window_flags) { - ImGui::SetNextWindowPos(ImVec2(0,270)); - ImGui::SetNextWindowSize(ImVec2(350, 370)); - ImGui::SetNextWindowBgAlpha(0.3f); + ImGui::SetNextWindowBgAlpha(GOOEY_BG_ALPHA); ImGui::Begin("Hexgrid", nullptr, window_flags); // NOTE: only show file dialog for hash_map grid type diff --git a/src/hexgame.cpp b/src/hexgame.cpp index 5425f54..313bd0b 100644 --- a/src/hexgame.cpp +++ b/src/hexgame.cpp @@ -109,6 +109,7 @@ init() g_game_state->grid.draw_mode = NONE; // TODO: maybe add this as an option to scene json? g_game_state->grid.normal = v3f(0.f, 0.f, 1.f); + g_game_state->select_mode = ENTITY_SELECT; // init global render state g_render_state = UTIL_ALLOC(1, render_state); @@ -162,7 +163,7 @@ mapMouseToViewport(int32 x, int32 y) } void -startDrawHelper(int32 x, int32 y) +startHexDrawHelper(int32 x, int32 y) { hgResetHexes(g_game_state->grid); v2i dims = g_render_state->viewport_dims; @@ -175,7 +176,7 @@ startDrawHelper(int32 x, int32 y) } void -handleMouseDown(SDL_MouseButtonEvent &e) +handleHexMouseDown(SDL_MouseButtonEvent& e) { game_state* gs = g_game_state; render_state* rs = g_render_state; @@ -184,13 +185,13 @@ handleMouseDown(SDL_MouseButtonEvent &e) switch (gs->grid.draw_mode) { case FILL: - startDrawHelper(coords.x, coords.y); + startHexDrawHelper(coords.x, coords.y); break; case LINE: - startDrawHelper(coords.x, coords.y); + startHexDrawHelper(coords.x, coords.y); break; case CONE_FILL: - startDrawHelper(coords.x, coords.y); + startHexDrawHelper(coords.x, coords.y); break; case PATHFINDING: break; @@ -223,7 +224,7 @@ handleMouseDown(SDL_MouseButtonEvent &e) } void -handleMouseMove(SDL_MouseMotionEvent &e) +handleHexMouseMove(SDL_MouseMotionEvent& e) { game_state* gs = g_game_state; render_state* rs = g_render_state; @@ -243,7 +244,7 @@ handleMouseMove(SDL_MouseMotionEvent &e) break; case PATHFINDING: break; - case ADD_HEXES: { + case ADD_HEXES: if (gs->grid.is_selecting) { v3f ray = cameraCreateRay(rs->cam, coords, dims); v3f xsect; @@ -251,8 +252,7 @@ handleMouseMove(SDL_MouseMotionEvent &e) hgAddHex(gs->grid, xsect, rs->filled_hex_render_group, rs->hex_line_render_group); } break; - } - case REMOVE_HEXES: { + case REMOVE_HEXES: if (gs->grid.is_selecting) { v3f ray = cameraCreateRay(rs->cam, coords, dims); v3f xsect; @@ -260,13 +260,63 @@ handleMouseMove(SDL_MouseMotionEvent &e) hgRemoveHex(gs->grid, xsect, rs->filled_hex_render_group, rs->hex_line_render_group); } break; - } case NONE: default: break; } } +void +handleEntityMouseDown(SDL_MouseButtonEvent& e) +{ + LOG(DEBUG) << "hey\n"; +} + +void +handleMouseDown(SDL_MouseButtonEvent& e, bool gooey_wants) +{ + game_state* gs = g_game_state; + + if (!gooey_wants) { + if (e.button == SDL_BUTTON_LEFT) { + if (gs->select_mode == HEX_SELECT) { + gs->grid.is_selecting = true; + handleHexMouseDown(e); + } else if (gs->select_mode == ENTITY_SELECT) { + handleEntityMouseDown(e); + } + } else if (e.button == SDL_BUTTON_RIGHT) { + gs->is_camera_rotate = true; + } + } +} + +void +handleMouseUp(SDL_MouseButtonEvent& e, bool gooey_wants) +{ + game_state* gs = g_game_state; + + if (!gooey_wants) { + if (e.button == SDL_BUTTON_LEFT && gs->select_mode == HEX_SELECT) + gs->grid.is_selecting = false; + else if (e.button == SDL_BUTTON_RIGHT) + gs->is_camera_rotate = false; + } +} + +void +handleMouseMove(SDL_MouseMotionEvent& e, bool gooey_wants) +{ + game_state* gs = g_game_state; + + if (!gooey_wants) { + if (gs->select_mode == HEX_SELECT && gs->grid.is_selecting) + handleHexMouseMove(e); + else if(gs->is_camera_rotate) + cameraRotate(g_render_state->cam, e.xrel, e.yrel); + } +} + bool processSDLEvents() { @@ -274,9 +324,6 @@ processSDLEvents() bool run = true; while (SDL_PollEvent(&e)) { - // let gooey have event - // TODO: need to check for both io.WantCaptureKeyboard and io.WantCaptureMouse - // to fix bug with 'ESC' not passing through while in imgui bool gooey_wants = gooProcessEvent(e); game_state* gs = g_game_state; @@ -310,28 +357,13 @@ processSDLEvents() } break; case SDL_MOUSEBUTTONDOWN: - if (!gooey_wants) { - if (e.button.button == SDL_BUTTON_LEFT) { - gs->grid.is_selecting = true; - handleMouseDown(e.button); - } else if (e.button.button == SDL_BUTTON_RIGHT) { - gs->is_camera_rotate = true; - } - } + handleMouseDown(e.button, gooey_wants); break; case SDL_MOUSEBUTTONUP: - if (!gooey_wants) { - if (e.button.button == SDL_BUTTON_LEFT) - gs->grid.is_selecting = false; - else if (e.button.button == SDL_BUTTON_RIGHT) - gs->is_camera_rotate = false; - } + handleMouseUp(e.button, gooey_wants); break; case SDL_MOUSEMOTION: - if (!gooey_wants && gs->grid.is_selecting) - handleMouseMove(e.motion); - else if(!gooey_wants && gs->is_camera_rotate) - cameraRotate(g_render_state->cam, e.motion.xrel, e.motion.yrel); + handleMouseMove(e.motion, gooey_wants); break; default: break; diff --git a/src/hexgame.h b/src/hexgame.h index a9b6167..bf4b5ee 100644 --- a/src/hexgame.h +++ b/src/hexgame.h @@ -7,6 +7,11 @@ #include "hexgrid.h" #include "util.h" +enum game_select_mode +{ + HEX_SELECT, + ENTITY_SELECT +}; struct game_state { @@ -14,6 +19,10 @@ struct game_state uint32 entity_count; Entity* entities; + // testing entity selection + game_select_mode select_mode; + Entity* selected_entity; + // camera movement controls bool is_camera_rotate; bool is_moveforward; diff --git a/src/renderer.cpp b/src/renderer.cpp index 1b43594..6e73c21 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -25,7 +25,7 @@ #define CLEAR_COL_G 55.f / 255.f #define CLEAR_COL_B 55.f / 255.f #define CLEAR_COL_A 1.f -#define USE_SECOND_MONITOR 1 +#define USE_SECOND_MONITOR 0 // TODO: add this to json scene properties and render_state util_RGBA g_clear_col = {1.f,1.f,1.f,1.f};