Skip to content

Commit

Permalink
- added wasd keys to scroll around in viewport
Browse files Browse the repository at this point in the history
  • Loading branch information
christoph-hart committed Nov 12, 2024
1 parent 2636d04 commit 6090bd3
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 2 deletions.
97 changes: 96 additions & 1 deletion hi_tools/hi_standalone_components/ZoomableViewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ ZoomableViewport::ZoomableViewport(Component* n) :
vBar(true),
content(n),
dragScrollTimer(*this),
mouseWatcher(new MouseWatcher(*this))
mouseWatcher(new MouseWatcher(*this)),
scroller(*this)
{
sf.addScrollBarToAnimate(hBar);
sf.addScrollBarToAnimate(vBar);
Expand Down Expand Up @@ -1029,6 +1030,100 @@ void ZoomableViewport::setMouseWheelScrollEnabled(bool shouldBeEnabled)
mouseWheelScroll = shouldBeEnabled;
}

void ZoomableViewport::WASDScroller::timerCallback()
{
if(currentDelta.isOrigin())
{
currentVelocity *= JUCE_LIVE_CONSTANT_OFF(0.8f);

if(currentVelocity.getDistanceFromOrigin() < 0.01f)
{
currentVelocity = {};
stopTimer();
}
}
else
{
currentVelocity += currentDelta;

currentVelocity.x = jlimit(-1.0f, 1.0f, currentVelocity.x);
currentVelocity.y = jlimit(-1.0f, 1.0f, currentVelocity.y);
}

auto f = currentVelocity;

f *= JUCE_LIVE_CONSTANT_OFF(-0.1f);

auto x = parent.hBar.getCurrentRangeStart();
x += f.x;
parent.hBar.setCurrentRangeStart(x, sendNotificationAsync);

auto y = parent.vBar.getCurrentRangeStart();
y += f.y;
parent.vBar.setCurrentRangeStart(y, sendNotificationAsync);
}

void ZoomableViewport::WASDScroller::setDelta(Point<float> delta)
{
currentDelta = delta;

if(currentVelocity.isOrigin() && !delta.isOrigin())
startTimer(15);
}

bool ZoomableViewport::WASDScroller::checkWASD(const KeyPress& key)
{
if(key == KeyPress('w', ModifierKeys(), 'w'))
return true;
if(key == KeyPress('a', ModifierKeys(), 'a'))
return true;
if(key == KeyPress('s', ModifierKeys(), 's'))
return true;
if(key == KeyPress('d', ModifierKeys(), 'd'))
return true;

return false;
}

bool ZoomableViewport::WASDScroller::keyChangedWASD(bool isKeyDown, Component* c)
{
auto fc = c->getCurrentlyFocusedComponent();

if(checkFunction && checkFunction(fc)) // make better check function
{
auto u = KeyPress::isKeyCurrentlyDown('w');
auto d = KeyPress::isKeyCurrentlyDown('s');
auto l = KeyPress::isKeyCurrentlyDown('a');
auto r = KeyPress::isKeyCurrentlyDown('d');

auto turbo = ModifierKeys::getCurrentModifiers().isShiftDown();

Point<float> delta;

if(u)
delta.y += 1.0f;
if(d)
delta.y -= 1.0f;

if(l)
delta.x += 1.0f;
if(r)
delta.x -= 1.0f;

delta *= JUCE_LIVE_CONSTANT(0.1);
delta *= 0.05f;

if(turbo)
delta *= JUCE_LIVE_CONSTANT_OFF(3.0f);

setDelta(delta);

return !delta.isOrigin();
}

return false;
}

Point<float> ZoomableViewport::MouseWatcher::getDeltaAfterResize()
{
auto newGraphPos = parent.getLocalPoint(parent.content, graphMousePos);
Expand Down
40 changes: 39 additions & 1 deletion hi_tools/hi_standalone_components/ZoomableViewport.h
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,46 @@ struct ZoomableViewport : public Component,

std::function<void(Component*)> contentFunction;

bool keyPressed(const KeyPress& key) override
{
return WASDScroller::checkWASD(key);
}

bool keyStateChanged (bool isKeyDown) override
{
return scroller.keyChangedWASD(isKeyDown, getContent<Component>());
}

/** Set a function that will return true or false based on the currently focused component. */
void setEnableWASD(const std::function<bool(Component*)>& checkFunction)
{
scroller.checkFunction = checkFunction;
}

private:


struct WASDScroller: public juce::Timer
{
WASDScroller(ZoomableViewport& parent_):
parent(parent_)
{}

void timerCallback() override;

void setDelta(Point<float> delta);

static bool checkWASD(const KeyPress & key);

bool keyChangedWASD(bool isKeyDown, Component* c);

ZoomableViewport& parent;

std::function<bool(Component*)> checkFunction;
Point<float> currentVelocity;
Point<float> currentDelta;

} scroller;

float maxZoomFactor = 3.0f;

bool dragToScroll = false;
Expand Down

0 comments on commit 6090bd3

Please sign in to comment.