Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added keyboard controls to vsg::Trackball, a vsg::Keyboard class for tracking keyboard state and FocusIn/Out events #816

Merged
merged 25 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
2f2b04e
Initial support for movement controlled by keys
robertosfield May 5, 2023
61fc0c1
Merge branch 'master' into TrackballKeys
robertosfield May 5, 2023
81f62bf
Merge branch 'master' into TrackballKeys
robertosfield May 12, 2023
6be18aa
Implemented turn left/right.
robertosfield May 12, 2023
a043ecc
Experiment with tracking holding keyboard keys
robertosfield May 13, 2023
b8a0ec7
Merge branch 'master' into TrackballKeys
robertosfield May 14, 2023
06d78b5
Added experimental update of camera position using key controls.
robertosfield May 14, 2023
eecadee
Moved keyboard state tracking into decidated Keyboard class
robertosfield May 15, 2023
e5dfd87
Improved support for handling keyboard focus
robertosfield May 15, 2023
30b5535
Added look up/down and forwrd/back support with 'w', 's', 'i' and 'o'…
robertosfield May 15, 2023
364a798
Test build of WM_SETFOCUS & WM_KILLFOCUS
robertosfield May 16, 2023
907e247
Added FocusInEvent and FocusOutEvent for tracking when a window gain/…
robertosfield May 16, 2023
5f7b64f
Added FocusInEvent/FocusOutEvent suppprt to Xcb_Window and Win32_Window.
robertosfield May 16, 2023
d7755f1
Added use of FocusInEvent/FocusOutEvent to Trackball
robertosfield May 16, 2023
e21f726
Moved Keyboard class into into own header
robertosfield May 16, 2023
cdf26b8
Ran clang-format
robertosfield May 16, 2023
788225a
Added { } to fix build
robertosfield May 16, 2023
20f7b54
Added user controllable keys for controlling motion
robertosfield May 16, 2023
16a1751
Resutrctured the keyboard movement implementation
robertosfield May 16, 2023
ecd23ae
Ran clang-format
robertosfield May 17, 2023
68fe636
Added bool method to vec/plane/quat class to make it easier to test i…
robertosfield May 17, 2023
b30c90d
Added inertial effect to Trackball keyboard controls.
robertosfield May 17, 2023
f08409d
Added roll with q and e keys
robertosfield May 17, 2023
8b4dee7
Ran clang-format
robertosfield May 17, 2023
0267c32
Merge branch 'master' into TrackballKeys
robertosfield May 17, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/vsg/all.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
#include <vsg/ui/CollectEvents.h>
#include <vsg/ui/FrameStamp.h>
#include <vsg/ui/KeyEvent.h>
#include <vsg/ui/Keyboard.h>
#include <vsg/ui/PlayEvents.h>
#include <vsg/ui/PointerEvent.h>
#include <vsg/ui/PrintEvents.h>
Expand Down
47 changes: 44 additions & 3 deletions include/vsg/app/Trackball.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
#include <vsg/app/Camera.h>
#include <vsg/app/EllipsoidModel.h>
#include <vsg/maths/transform.h>
#include <vsg/ui/ApplicationEvent.h>
#include <vsg/ui/KeyEvent.h>
#include <vsg/ui/Keyboard.h>
#include <vsg/ui/PointerEvent.h>
#include <vsg/ui/ScrollWheelEvent.h>
#include <vsg/ui/TouchEvent.h>
Expand All @@ -37,6 +36,9 @@ namespace vsg
dvec3 tbc(PointerEvent& event);

void apply(KeyPressEvent& keyPress) override;
void apply(KeyReleaseEvent& keyRelease) override;
void apply(FocusInEvent& focusIn) override;
void apply(FocusOutEvent& focusOut) override;
void apply(ButtonPressEvent& buttonPress) override;
void apply(ButtonReleaseEvent& buttonRelease) override;
void apply(MoveEvent& moveEvent) override;
Expand Down Expand Up @@ -81,6 +83,42 @@ namespace vsg
/// container that maps key symbol bindings with the Viewpoint that should move the LookAt to when pressed.
std::map<KeySymbol, Viewpoint> keyViewpointMap;

/// Key that turns the view left around the eye points
KeySymbol turnLeftKey = KEY_a;

/// Key that turns the view right around the eye points
KeySymbol turnRightKey = KEY_d;

/// Key that pitches up the view around the eye point
KeySymbol pitchUpKey = KEY_w;

/// Key that pitches down the view around the eye point
KeySymbol pitchDownKey = KEY_s;

/// Key that rools the view anti-clockwise/left
KeySymbol rollLeftKey = KEY_q;

/// Key that rolls the view clockwise/right
KeySymbol rollRightKey = KEY_e;

/// Key that moves the view forward
KeySymbol moveForwardKey = KEY_o;

/// Key that moves the view backwards
KeySymbol moveBackwardKey = KEY_i;

/// Key that moves the view left
KeySymbol moveLeftKey = KEY_Left;

/// Key that moves the view right
KeySymbol moveRightKey = KEY_Right;

/// Key that moves the view upward
KeySymbol moveUpKey = KEY_Up;

/// Key that moves the view downard
KeySymbol moveDownKey = KEY_Down;

/// Button mask value used to enable panning of the view, defaults to left mouse button
ButtonMask rotateButtonMask = BUTTON_MASK_1;

Expand All @@ -104,7 +142,8 @@ namespace vsg
ref_ptr<LookAt> _lookAt;
ref_ptr<EllipsoidModel> _ellipsoidModel;

bool _hasFocus = false;
bool _hasKeyboardFocus = false;
bool _hasPointerFocus = false;
bool _lastPointerEventWithinRenderArea = false;

enum UpdateMode
Expand All @@ -131,6 +170,8 @@ namespace vsg
ref_ptr<LookAt> _endLookAt;
std::map<uint32_t, ref_ptr<TouchEvent>> _previousTouches;

ref_ptr<Keyboard> _keyboard;

double _animationDuration = 0.0;
};
VSG_type_name(vsg::Trackball);
Expand Down
1 change: 0 additions & 1 deletion include/vsg/app/WindowResizeHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ namespace vsg
class VSG_DECLSPEC UpdateGraphicsPipelines : public vsg::Inherit<vsg::Visitor, UpdateGraphicsPipelines>
{
public:

UpdateGraphicsPipelines();

vsg::ref_ptr<vsg::Context> context;
Expand Down
4 changes: 4 additions & 0 deletions include/vsg/core/ConstVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ namespace vsg
class ExposeWindowEvent;
class ConfigureWindowEvent;
class CloseWindowEvent;
class FocusInEvent;
class FocusOutEvent;
class KeyEvent;
class KeyPressEvent;
class KeyReleaseEvent;
Expand Down Expand Up @@ -356,6 +358,8 @@ namespace vsg
virtual void apply(const ExposeWindowEvent&);
virtual void apply(const ConfigureWindowEvent&);
virtual void apply(const CloseWindowEvent&);
virtual void apply(const FocusInEvent&);
virtual void apply(const FocusOutEvent&);
virtual void apply(const KeyEvent&);
virtual void apply(const KeyPressEvent&);
virtual void apply(const KeyReleaseEvent&);
Expand Down
4 changes: 4 additions & 0 deletions include/vsg/core/Visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ namespace vsg
class ExposeWindowEvent;
class ConfigureWindowEvent;
class CloseWindowEvent;
class FocusInEvent;
class FocusOutEvent;
class KeyEvent;
class KeyPressEvent;
class KeyReleaseEvent;
Expand Down Expand Up @@ -356,6 +358,8 @@ namespace vsg
virtual void apply(ExposeWindowEvent&);
virtual void apply(ConfigureWindowEvent&);
virtual void apply(CloseWindowEvent&);
virtual void apply(FocusInEvent&);
virtual void apply(FocusOutEvent&);
virtual void apply(KeyEvent&);
virtual void apply(KeyPressEvent&);
virtual void apply(KeyReleaseEvent&);
Expand Down
2 changes: 2 additions & 0 deletions include/vsg/maths/plane.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ namespace vsg

bool valid() const { return n.x != 0.0 && n.y != 0.0 && n.z != 0.0; }

explicit operator bool() const noexcept { return valid(); }

T* data() { return value; }
const T* data() const { return value; }
};
Expand Down
2 changes: 2 additions & 0 deletions include/vsg/maths/quat.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ namespace vsg
z = axis.z * sinhalfangle * inversenorm;
w = coshalfangle;
}

explicit operator bool() const noexcept { return value[0] != 0.0 || value[1] != 0.0 || value[2] != 0.0 || value[3] != 0.0; }
};

using quat = t_quat<float>; /// float quaternion
Expand Down
2 changes: 2 additions & 0 deletions include/vsg/maths/vec2.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ namespace vsg
}
return *this;
}

explicit operator bool() const noexcept { return value[0] != 0.0 || value[1] != 0.0; }
};

using vec2 = t_vec2<float>; // float 2D vector
Expand Down
2 changes: 2 additions & 0 deletions include/vsg/maths/vec3.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ namespace vsg
}
return *this;
}

explicit operator bool() const noexcept { return value[0] != 0.0 || value[1] != 0.0 || value[2] != 0.0; }
};

using vec3 = t_vec3<float>; // float 3D vector
Expand Down
2 changes: 2 additions & 0 deletions include/vsg/maths/vec4.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ namespace vsg
}
return *this;
}

explicit operator bool() const noexcept { return value[0] != 0.0 || value[1] != 0.0 || value[2] != 0.0 || value[3] != 0.0; }
};

using vec4 = t_vec4<float>; // float 4D vector
Expand Down
49 changes: 49 additions & 0 deletions include/vsg/ui/Keyboard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#pragma once

/* <editor-fold desc="MIT License">

Copyright(c) 2023 Robert Osfield

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

</editor-fold> */

#include <vsg/ui/ApplicationEvent.h>
#include <vsg/ui/KeyEvent.h>

namespace vsg
{

/// Keyboard tracks keyboard events to maintain the key pressed state and how long the key has been hel for
class VSG_DECLSPEC Keyboard : public Inherit<Visitor, Keyboard>
{
public:
void apply(KeyPressEvent& keyPress) override;
void apply(KeyReleaseEvent& keyRelease) override;
void apply(FocusInEvent& focusIn) override;
void apply(FocusOutEvent& focusOut) override;

struct KeyHistory
{
vsg::time_point timeOfFirstKeyPress = {};
vsg::time_point timeOfLastKeyPress = {};
vsg::time_point timeOfKeyRelease = {};
bool handled = false;
};

std::map<KeySymbol, KeyHistory> keyState;

/// return true if key is currently pressed
bool pressed(KeySymbol key, bool ignore_handled_keys = true);

/// return a pair of times, the first is the time, in seconds, since the key was first pressed and the second is the time, in secnds, since it was released.
/// if the key hasn't been pressed then then first value will be < 0.0, if the key is still pressed then the second value will be 0.0.
std::pair<double, double> times(KeySymbol key, bool ignore_handled_keys = true);
};
VSG_type_name(vsg::Keyboard);

} // namespace vsg
22 changes: 22 additions & 0 deletions include/vsg/ui/WindowEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,26 @@ namespace vsg
};
VSG_type_name(vsg::CloseWindowEvent);

/// FocusInEvent represents a window aquiring focus event.
class FocusInEvent : public Inherit<WindowEvent, FocusInEvent>
{
public:
FocusInEvent() {}

FocusInEvent(Window* in_window, time_point in_time) :
Inherit(in_window, in_time) {}
};
VSG_type_name(vsg::FocusInEvent);

/// FocusOutEvent represents a window loosing focus event.
class FocusOutEvent : public Inherit<WindowEvent, FocusOutEvent>
{
public:
FocusOutEvent() {}

FocusOutEvent(Window* in_window, time_point in_time) :
Inherit(in_window, in_time) {}
};
VSG_type_name(vsg::FocusOutEvent);

} // namespace vsg
1 change: 1 addition & 0 deletions src/vsg/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ set(SOURCES
ui/ShiftEventTime.cpp
ui/PlayEvents.cpp
ui/PrintEvents.cpp
ui/Keyboard.cpp

vk/CommandBuffer.cpp
vk/CommandPool.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/vsg/app/CompileTraversal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ void CompileTraversal::apply(View& view)
{
// if context is associated with a view make sure we only apply it if it matches with view, oherwsie we skip this context
auto context_view = context->view.ref_ptr();
if (context_view && context_view.get()!=&view) continue;
if (context_view && context_view.get() != &view) continue;

context->viewID = view.viewID;
context->viewDependentState = view.viewDependentState.get();
Expand Down
Loading