diff --git a/include/common/viewport_handler.hpp b/include/common/viewport_handler.hpp index 623a5fd6..6904ce4a 100644 --- a/include/common/viewport_handler.hpp +++ b/include/common/viewport_handler.hpp @@ -73,7 +73,7 @@ struct ViewportHandler void reset() { - state.zoom = 1.01f; + state.zoom = 1.0f; setFocus(state.center); } diff --git a/include/editor/colony_creator/colony_tool.hpp b/include/editor/colony_creator/colony_tool.hpp index f8be7bb6..ff829b13 100644 --- a/include/editor/colony_creator/colony_tool.hpp +++ b/include/editor/colony_creator/colony_tool.hpp @@ -33,7 +33,9 @@ struct ColonyTool : GUI::Container color_button->setWidth(30.0f); top_zone->addItem(color_button, "colony_color_button"); - auto to_focus_button = create("Focus", [](){}); + auto to_focus_button = create("Focus", [this](){ + control_state.requestFocus(colony.base.position, 2.0f); + }); to_focus_button->setHeight(20.0f); to_focus_button->setWidth(40.0f); top_zone->addItem(to_focus_button); diff --git a/include/editor/control_state.hpp b/include/editor/control_state.hpp index cc7306dc..053cce44 100644 --- a/include/editor/control_state.hpp +++ b/include/editor/control_state.hpp @@ -1,6 +1,7 @@ #pragma once #include #include +#include "editor/transition.hpp" struct ControlState @@ -8,7 +9,16 @@ struct ControlState using ViewAction = std::function; using Action = std::function; + Action action = nullptr; ViewAction view_action = nullptr; + // Special commands + bool focus_requested = false; + trn::Transition focus; + trn::Transition zoom = 1.0f; + + ControlState() + { + } void executeViewAction(sf::Vector2f mouse_world_position) { @@ -16,4 +26,18 @@ struct ControlState view_action(mouse_world_position); } } + + void executeAction(sf::Vector2f mouse_world_position) + { + if (action) { + action(); + } + } + + void requestFocus(sf::Vector2f position, float zoom_level = 1.0f) + { + focus_requested = true; + focus = position; + zoom = zoom_level; + } }; diff --git a/include/editor/transition.hpp b/include/editor/transition.hpp index 17be0113..f3ff7442 100644 --- a/include/editor/transition.hpp +++ b/include/editor/transition.hpp @@ -17,7 +17,7 @@ class Transition , m_target_value() , m_delta() , m_start_time(std::chrono::steady_clock::now()) - , m_speed(0.0f) + , m_speed(1.0f) { updateDelta(); } @@ -82,8 +82,9 @@ class Transition void setValueInstant(const T& value) { + m_start_value = value; m_current_value = value; - m_target_value = value; + m_target_value = value; updateDelta(); } @@ -99,7 +100,7 @@ class Transition updateDelta(); } -private: +public: T m_start_value; T m_target_value; T m_delta; @@ -135,6 +136,7 @@ class Transition { m_start_value = m_current_value; m_start_time = std::chrono::steady_clock::now(); + m_last_access = m_start_time; updateDelta(); } diff --git a/include/editor/world_view.hpp b/include/editor/world_view.hpp index b94ad12b..9f78457d 100644 --- a/include/editor/world_view.hpp +++ b/include/editor/world_view.hpp @@ -24,13 +24,22 @@ struct WorldView : GUI::Item void initializeEventCallbacks() override { - addEventCallback(sf::Event::MouseWheelScrolled, [&](sfev::CstEv e){simulation.renderer.vp_handler.wheelZoom(e.mouseWheelScroll.delta);}); - addKeyPressedCallback(sf::Keyboard::R, [&](sfev::CstEv e){simulation.renderer.vp_handler.reset();}); + addEventCallback(sf::Event::MouseWheelScrolled, [&](sfev::CstEv e){ + simulation.renderer.vp_handler.wheelZoom(e.mouseWheelScroll.delta); + control_state.focus_requested = false; + control_state.zoom.setValueInstant(simulation.renderer.vp_handler.state.zoom); + }); + addKeyPressedCallback(sf::Keyboard::R, [&](sfev::CstEv e){ + control_state.focus_requested = true; + control_state.zoom = 1.0f; + control_state.focus = simulation.renderer.vp_handler.state.center; + }); } void onClick(sf::Vector2f relative_click_position, sf::Mouse::Button button) override { if (button == sf::Mouse::Left) { + control_state.focus_requested = false; simulation.renderer.vp_handler.click(relative_click_position); } else if (button == sf::Mouse::Right) { control_state.executeViewAction(simulation.renderer.vp_handler.getMouseWorldPosition()); @@ -41,6 +50,8 @@ struct WorldView : GUI::Item { if (button == sf::Mouse::Left) { simulation.renderer.vp_handler.unclick(); + control_state.focus_requested = false; + control_state.focus.setValueInstant(simulation.renderer.vp_handler.state.offset); } } @@ -51,6 +62,10 @@ struct WorldView : GUI::Item void render(sf::RenderTarget& target) override { + if (control_state.focus_requested) { + simulation.renderer.vp_handler.setFocus(control_state.focus); + simulation.renderer.vp_handler.setZoom(control_state.zoom); + } simulation.render(target); }