diff --git a/src/dawn/display/RenderPipeline.cpp b/src/dawn/display/RenderPipeline.cpp index 834f732b..18edc24c 100644 --- a/src/dawn/display/RenderPipeline.cpp +++ b/src/dawn/display/RenderPipeline.cpp @@ -134,21 +134,7 @@ void RenderPipeline::renderSceneCamera(Scene *scene, Camera *camera) { auto canvas = *itCanvas; glm::mat4 projection; glm::mat4 view; - - switch(canvas->drawType) { - case UI_DRAW_TYPE_WORLD_ABSOLUTE: - projection = camera->getProjection(); - view = camera->transform->getWorldTransform(); - break; - - case UI_DRAW_TYPE_WORLD_CAMERA_RELATIVE: - projection = glm::ortho(0.0f, renderTarget->getWidth(), renderTarget->getHeight(), 0.0f); - view = glm::mat4(1.0f); - break; - - default: - assertUnreachable(); - } + canvas->getProjectionAndView(&projection, &view); auto renderables = canvas->item->findChildrenDeep(); auto itChild = renderables.begin(); diff --git a/src/dawn/scene/components/ui/UICanvas.cpp b/src/dawn/scene/components/ui/UICanvas.cpp index bd362fe6..169bcf7b 100644 --- a/src/dawn/scene/components/ui/UICanvas.cpp +++ b/src/dawn/scene/components/ui/UICanvas.cpp @@ -36,6 +36,31 @@ float_t UICanvas::getContentHeight() { return this->getHeight(); } +void UICanvas::getProjectionAndView(glm::mat4 *proj, glm::mat4 *view) { + assertNotNull(proj); + assertNotNull(view); + + switch(this->drawType) { + case UI_DRAW_TYPE_WORLD_ABSOLUTE: + *proj = camera->getProjection(); + *view = camera->transform->getWorldTransform(); + break; + + case UI_DRAW_TYPE_WORLD_CAMERA_RELATIVE: + *proj = glm::ortho( + 0.0f, + camera->getRenderTarget()->getWidth(), + camera->getRenderTarget()->getHeight(), + 0.0f + ); + *view = glm::mat4(1.0f); + break; + + default: + assertUnreachable(); + } +} + void UICanvas::onStart() { if(camera == nullptr) camera = getScene()->findComponent(); diff --git a/src/dawn/scene/components/ui/UICanvas.hpp b/src/dawn/scene/components/ui/UICanvas.hpp index 5b3fb25e..2143ad5a 100644 --- a/src/dawn/scene/components/ui/UICanvas.hpp +++ b/src/dawn/scene/components/ui/UICanvas.hpp @@ -64,7 +64,7 @@ namespace Dawn { //======================================================================// StateProperty camera; - enum UIDrawType drawType = UI_DRAW_TYPE_WORLD_ABSOLUTE; + enum UIDrawType drawType = UI_DRAW_TYPE_WORLD_CAMERA_RELATIVE; /** * Constructs the UI Canvas Scene Item Component. @@ -73,6 +73,14 @@ namespace Dawn { */ UICanvas(SceneItem *item); + /** + * Return the type of projection and view matrixes are used. + * + * @param proj Projection matrix output. + * @param view View matrix ouput. + */ + void getProjectionAndView(glm::mat4 *proj, glm::mat4 *view); + float_t getWidth() override; float_t getHeight() override; float_t getContentWidth() override; diff --git a/src/dawn/scene/components/ui/menu/UIMenuController.cpp b/src/dawn/scene/components/ui/menu/UIMenuController.cpp index 7020fbb7..2d58fd8b 100644 --- a/src/dawn/scene/components/ui/menu/UIMenuController.cpp +++ b/src/dawn/scene/components/ui/menu/UIMenuController.cpp @@ -54,6 +54,7 @@ void UIMenuController::onStart() { menuY = mathClamp(menuY-1, 0, rows); break; case INPUT_BIND_ACCEPT: + case INPUT_BIND_MOUSE_CLICK: eventItemSelected.invoke(menuX, menuY); break; case INPUT_BIND_CANCEL: diff --git a/src/dawn/scene/components/ui/menu/UIMenuController.hpp b/src/dawn/scene/components/ui/menu/UIMenuController.hpp index 2bf759d3..47f00f01 100644 --- a/src/dawn/scene/components/ui/menu/UIMenuController.hpp +++ b/src/dawn/scene/components/ui/menu/UIMenuController.hpp @@ -22,10 +22,7 @@ namespace Dawn { StateEvent eventItemSelected; StateEvent<> eventMenuCancel; - void moveRelative(int32_t x, int32_t y); - UIMenuController(SceneItem *item); - - void onStart(); + void onStart() override; }; } \ No newline at end of file diff --git a/src/dawn/scene/components/ui/menu/UISimpleMenu.cpp b/src/dawn/scene/components/ui/menu/UISimpleMenu.cpp index 6acaf14f..71bebe97 100644 --- a/src/dawn/scene/components/ui/menu/UISimpleMenu.cpp +++ b/src/dawn/scene/components/ui/menu/UISimpleMenu.cpp @@ -62,19 +62,32 @@ void UISimpleMenu::onStart() { assertNotNull(canvas->camera); if(!this->menu->active) return; + // Mouse in screen space. auto mouse = getGame()->inputManager.getAxis2D(INPUT_BIND_MOUSE_X, INPUT_BIND_MOUSE_Y); - mouse *= 2.0f; - mouse -= glm::vec2(1, 1); - - struct Ray3D ray; - ray.origin = canvas->camera->transform->getWorldPosition(); - ray.direction = canvas->camera->getRayDirectionFromScreenSpace(mouse); switch(canvas->drawType) { case UI_DRAW_TYPE_WORLD_CAMERA_RELATIVE: - break; - - case UI_DRAW_TYPE_WORLD_ABSOLUTE: + case UI_DRAW_TYPE_WORLD_ABSOLUTE: { + + // Calculate ray + struct Ray3D ray; + switch(canvas->drawType) { + case UI_DRAW_TYPE_WORLD_ABSOLUTE: + mouse *= 2.0f; + mouse -= glm::vec2(1, 1); + ray.origin = canvas->camera->transform->getWorldPosition(); + ray.direction = canvas->camera->getRayDirectionFromScreenSpace(mouse); + break; + + case UI_DRAW_TYPE_WORLD_CAMERA_RELATIVE: + RenderTarget *rt = canvas->camera->getRenderTarget(); + mouse *= glm::vec2(rt->getWidth(), rt->getHeight()); + ray.origin = glm::vec3(mouse, 5); + ray.direction = glm::vec3(0, 0, -10); + break; + } + + // Find mouse item auto itItems = menuItems.begin(); while(itItems != menuItems.end()) { auto item = *itItems; @@ -90,7 +103,7 @@ void UISimpleMenu::onStart() { glm::vec3 normal; float_t distance; - // TODO: Include Z axis. + // TODO: Include Z axis for determining which item is hovered. if(!raytestQuad( ray, glm::vec2(0, 0), size, @@ -105,6 +118,11 @@ void UISimpleMenu::onStart() { break; } break; + } + + default: { + assertUnreachable(); + } } }, getScene()->eventSceneUnpausedUpdate); } \ No newline at end of file diff --git a/src/dawn/scene/components/ui/menu/UISimpleMenu.hpp b/src/dawn/scene/components/ui/menu/UISimpleMenu.hpp index ff96bb43..db765561 100644 --- a/src/dawn/scene/components/ui/menu/UISimpleMenu.hpp +++ b/src/dawn/scene/components/ui/menu/UISimpleMenu.hpp @@ -17,7 +17,6 @@ namespace Dawn { public: UISimpleMenu(SceneItem *item); - std::vector getDependencies() override; void onStart() override; }; diff --git a/src/dawn/scene/components/ui/menu/UISimpleMenuItem.hpp b/src/dawn/scene/components/ui/menu/UISimpleMenuItem.hpp index df456fa3..fc73c527 100644 --- a/src/dawn/scene/components/ui/menu/UISimpleMenuItem.hpp +++ b/src/dawn/scene/components/ui/menu/UISimpleMenuItem.hpp @@ -12,14 +12,19 @@ namespace Dawn { StateEvent<> eventHoveredOn; StateEvent<> eventHoveredOff; StateEvent<> eventSelected; - // May make these into either state events or make a new event. - int32_t menuX; - int32_t menuY; - + int32_t menuX = 0; + int32_t menuY = 0; UIComponent *uiComponent = nullptr; UISimpleMenuItem(SceneItem *item); + + /** + * Returns the UI component that this simple UI menu item uses to figure + * out if it is being hovered over currently or not. + * + * @return UI Component to use for highlighting / hovering. + */ virtual UIComponent * getComponentForHighlighting(); }; } \ No newline at end of file diff --git a/src/dawntictactoe/scenes/TicTacToeScene.hpp b/src/dawntictactoe/scenes/TicTacToeScene.hpp index 855fb3bc..68b96ea9 100644 --- a/src/dawntictactoe/scenes/TicTacToeScene.hpp +++ b/src/dawntictactoe/scenes/TicTacToeScene.hpp @@ -24,7 +24,7 @@ namespace Dawn { void stage() override { camera = Camera::create(this); // camera->transform->lookAt(glm::vec3(0, 0, 8), glm::vec3(0, 0, 0)); - camera->transform->lookAt(glm::vec3(3, 3, 3), glm::vec3(0, 0, 0)); + camera->transform->lookAt(glm::vec3(32, 32, 32), glm::vec3(0, 0, 0)); float_t s = 2.0f; camera->orthoTop = s; @@ -59,8 +59,7 @@ namespace Dawn { label->font = &this->game->assetManager.get("truetype_bizudp")->font; label->text = "Item " + std::to_string(x) + ", " + std::to_string(y); - // label->fontSize = 36.0f; - label->fontSize = 1.0f; + label->fontSize = 36.0f; labelItem->transform.setParent(canvas->transform); label->alignment = glm::vec4(label->fontSize * 8 * x, label->fontSize * 1.5f * y, 350, 100); @@ -69,7 +68,6 @@ namespace Dawn { labelMenuItem->menuX = x; labelMenuItem->menuY = y; - useEvent(std::bind([&](UISimpleMenuItem *itm){ std::cout << "Hover on " << std::to_string(itm->menuX) << ", " << std::to_string(itm->menuY) << std::endl; }, labelMenuItem), labelMenuItem->eventHoveredOn);