-
Notifications
You must be signed in to change notification settings - Fork 1
/
interface-manager.cc
147 lines (130 loc) · 4.42 KB
/
interface-manager.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
// Copyright(c) Leonardo Romor <[email protected]>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation version 2.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://gnu.org/licenses/gpl-2.0.txt>
#include "interface-manager.h"
#include <cmath>
#define XK_MISCELLANY
#include <X11/Xlib.h>
#include <X11/keysymdef.h>
#include <X11/XKBlib.h>
#include "input/gamepad.h"
InterfaceManager::InterfaceManager(Display *display, Window window,
Scene *scene, Camera *camera, XInput2 *xinput2,
Gamepad *gamepad)
: display_(display), window_(window), scene_(scene),
camera_(camera), xinput2_(xinput2), gamepad_(gamepad),
gamepad_state_({}), mouse_state_(0), exit_(false) {
// Register events for gamepad
if (gamepad)
gamepad->SetEventCallback(
[&](const struct EventData &data) {
const float value = (std::fabs(data.value) < 5e-2) ? 0 : data.value;
switch(data.type) {
case EventType::kAxisLeftX:
gamepad_state_.axis_left_x_ = value;
break;
case EventType::kAxisLeftY:
gamepad_state_.axis_left_y_ = value;
break;
case EventType::kAxisRightX:
gamepad_state_.axis_right_x_ = value;
break;
case EventType::kAxisRightY:
gamepad_state_.axis_right_y_ = value;
break;
default: break;
}
});
// Register mouse event callbacks
xinput2->OnButtonEvent([&](XInput2 *xinput2,
const XInput2::KeyButtonEventType e,
const unsigned int b) {
if (e == XInput2::KeyButtonEventType::kButtonReleased)
DisableDragMode();
else {
switch (b) {
case Button1:
EnableDragMode1();
break;
case Button3:
EnableDragMode2();
break;
}
}
});
xinput2->OnPointerMotionEvent([&](
XInput2 *xinput2,
double dx, double dy) {
DispatchPointerMotionEvent(dx, dy);
});
xinput2->OnWheelEvent([&](XInput2 *xinput2, float dz) {
DispatchWheelEvent(dz);
});
xinput2->OnKeyEvent([&](XInput2 *xinput2,
const XInput2::KeyButtonEventType e,
const unsigned int b) {
DispatchKeyPressEvent(b);
});
}
void InterfaceManager::UpdateScene(double dt) {
camera_->FirstPersonControlMove(gamepad_state_.axis_left_x_ * dt, gamepad_state_.axis_left_y_ * dt);
camera_->FirstPersonControlRotate(gamepad_state_.axis_right_x_ * dt, gamepad_state_.axis_right_y_ * dt);
}
void InterfaceManager::EnableDragMode1() {
if (mouse_state_ == IDLE) {
xinput2_->GrabPointerDevice();
mouse_state_ = DRAG_MODE1;
}
}
void InterfaceManager::EnableDragMode2() {
if (mouse_state_ == IDLE) {
xinput2_->GrabPointerDevice();
mouse_state_ = DRAG_MODE2;
}
}
void InterfaceManager::DisableDragMode() {
if (mouse_state_ != IDLE) {
xinput2_->UngrabPointerDevice();
mouse_state_ = IDLE;
}
}
void InterfaceManager::DispatchPointerMotionEvent(double dx, double dy) {
XWindowAttributes attrs;
const float kPanSpeed = 5.0f;
const float kRotateSpeed = 2.0f;
XGetWindowAttributes(display_, window_, &attrs);
const unsigned int width = attrs.width;
const unsigned int height = attrs.height;
const int minor_axis = std::min(width, height) / 2;
const float dside = dx / minor_axis;
const float dup = -dy / minor_axis;
switch (mouse_state_) {
case DRAG_MODE1:
camera_->TrackballControlRotate(dside * kRotateSpeed, dup * kRotateSpeed);
break;
case DRAG_MODE2:
camera_->TrackballControlPan(dside * kPanSpeed, dup * kPanSpeed);
break;
}
}
void InterfaceManager::DispatchWheelEvent(float dz) {
camera_->TrackballControlZoom(dz);
}
void InterfaceManager::DispatchKeyPressEvent(unsigned int keysim) {
switch (keysim) {
case XK_Escape:
exit_ = true;
break;
}
}