Skip to content

Commit

Permalink
Fix focus shit
Browse files Browse the repository at this point in the history
  • Loading branch information
johnBuffer committed Dec 4, 2021
1 parent bc964de commit a2d4367
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 7 deletions.
2 changes: 1 addition & 1 deletion include/common/viewport_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ struct ViewportHandler

void reset()
{
state.zoom = 1.01f;
state.zoom = 1.0f;
setFocus(state.center);
}

Expand Down
4 changes: 3 additions & 1 deletion include/editor/colony_creator/colony_tool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<GUI::Button>("Focus", [](){});
auto to_focus_button = create<GUI::Button>("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);
Expand Down
24 changes: 24 additions & 0 deletions include/editor/control_state.hpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,43 @@
#pragma once
#include <functional>
#include <SFML/System/Vector2.hpp>
#include "editor/transition.hpp"


struct ControlState
{
using ViewAction = std::function<void(sf::Vector2f)>;
using Action = std::function<void(void)>;

Action action = nullptr;
ViewAction view_action = nullptr;
// Special commands
bool focus_requested = false;
trn::Transition<sf::Vector2f> focus;
trn::Transition<float> zoom = 1.0f;

ControlState()
{
}

void executeViewAction(sf::Vector2f mouse_world_position)
{
if (view_action) {
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;
}
};
8 changes: 5 additions & 3 deletions include/editor/transition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
}

Expand All @@ -99,7 +100,7 @@ class Transition
updateDelta();
}

private:
public:
T m_start_value;
T m_target_value;
T m_delta;
Expand Down Expand Up @@ -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();
}

Expand Down
19 changes: 17 additions & 2 deletions include/editor/world_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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);
}
}

Expand All @@ -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);
}

Expand Down

0 comments on commit a2d4367

Please sign in to comment.