Skip to content

Commit

Permalink
Add event for viewport limit updates
Browse files Browse the repository at this point in the history
  • Loading branch information
albin-johansson committed Jun 25, 2024
1 parent 880c09d commit 0627575
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
8 changes: 8 additions & 0 deletions source/core/inc/tactile/core/event/viewport_event_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class EventDispatcher;

struct OffsetViewportEvent;
struct UpdateViewportSizeEvent;
struct UpdateViewportLimitsEvent;
struct IncreaseViewportZoomEvent;
struct DecreaseViewportZoomEvent;
struct ResetViewportZoomEvent;
Expand Down Expand Up @@ -56,6 +57,13 @@ class ViewportEventHandler final
*/
void on_update_viewport_size(const UpdateViewportSizeEvent& event);

/**
* Updates the limits of a viewport.
*
* \param event The associated event.
*/
void on_update_viewport_limits(const UpdateViewportLimitsEvent& event);

/**
* Increases the zoom of a viewport.
*
Expand Down
15 changes: 15 additions & 0 deletions source/core/inc/tactile/core/event/viewport_events.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ struct UpdateViewportSizeEvent final
Float2 new_size;
};

/**
* Event for changing the limits of a viewport in the current document.
*/
struct UpdateViewportLimitsEvent final
{
/** The target viewport. */
EntityID viewport_entity;

/** The new minimum position. */
Float2 min_pos;

/** The new maximum position. */
Float2 max_pos;
};

/**
* Event for increasing the zoom of a viewport in the current document.
*/
Expand Down
23 changes: 23 additions & 0 deletions source/core/src/tactile/core/event/viewport_event_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ void ViewportEventHandler::install(EventDispatcher& dispatcher)
// clang-format off
dispatcher.bind<OffsetViewportEvent, &Self::on_offset_viewport>(this);
dispatcher.bind<UpdateViewportSizeEvent, &Self::on_update_viewport_size>(this);
dispatcher.bind<UpdateViewportLimitsEvent, &Self::on_update_viewport_limits>(this);
dispatcher.bind<IncreaseViewportZoomEvent, &Self::on_increase_viewport_zoom>(this);
dispatcher.bind<DecreaseViewportZoomEvent, &Self::on_decrease_viewport_zoom>(this);
dispatcher.bind<ResetViewportZoomEvent, &Self::on_reset_viewport_zoom>(this);
Expand Down Expand Up @@ -75,6 +76,28 @@ void ViewportEventHandler::on_update_viewport_size(
}
}

void ViewportEventHandler::on_update_viewport_limits(
const UpdateViewportLimitsEvent& event)
{
if (mModel->get_settings().log_verbose_events) {
TACTILE_LOG_TRACE(
"UpdateViewportLimitsEvent(viewport: {}, min: {}, max: {})",
entity_to_string(event.viewport_entity),
event.min_pos,
event.max_pos);
}

if (auto* document = mModel->get_current_document()) {
auto& registry = document->get_registry();
auto& limits = registry.get<CViewportLimits>(event.viewport_entity);
limits.min_pos = event.min_pos;
limits.max_pos = event.max_pos;

auto& viewport = registry.get<CViewport>(event.viewport_entity);
translate_viewport(viewport, Float2 {0, 0}, &limits);
}
}

void ViewportEventHandler::on_increase_viewport_zoom(
const IncreaseViewportZoomEvent& event)
{
Expand Down

0 comments on commit 0627575

Please sign in to comment.