Implemented scrolling factor for smooth trackpad scrolling

Working platforms platform: OSX, Windows.
Support for almost all ui elements, including project list.

Ported from 304a1f5b5a (#7864).
Fixes #492 and #3913.
This commit is contained in:
Sean Bohan 2017-05-13 12:02:26 +08:00
parent e7328fe5a0
commit ee670f3724
15 changed files with 111 additions and 67 deletions

View File

@ -203,6 +203,7 @@ struct InputEventMouse {
struct InputEventMouseButton : public InputEventMouse {
double factor;
int button_index;
bool pressed; //otherwise released
bool doubleclick; //last even less than doubleclick time
@ -287,7 +288,10 @@ struct InputEvent {
InputEvent xform_by(const Matrix32 &p_xform) const;
bool operator==(const InputEvent &p_event) const;
operator String() const;
InputEvent() { zeromem(this, sizeof(InputEvent)); }
InputEvent() {
zeromem(this, sizeof(InputEvent));
mouse_button.factor = 1;
}
};
#endif

View File

@ -1834,7 +1834,7 @@ void AnimationKeyEditor::_track_editor_input_event(const InputEvent &p_input) {
if (mb.mod.command) {
zoom->set_val(zoom->get_val() + zoom->get_step());
} else {
v_scroll->set_val(v_scroll->get_val() - v_scroll->get_page() / 8);
v_scroll->set_val(v_scroll->get_val() - v_scroll->get_page() * mb.factor / 8);
}
}
@ -1843,10 +1843,19 @@ void AnimationKeyEditor::_track_editor_input_event(const InputEvent &p_input) {
if (mb.mod.command) {
zoom->set_val(zoom->get_val() - zoom->get_step());
} else {
v_scroll->set_val(v_scroll->get_val() + v_scroll->get_page() / 8);
v_scroll->set_val(v_scroll->get_val() + v_scroll->get_page() * mb.factor / 8);
}
}
if (mb.button_index == BUTTON_WHEEL_RIGHT && mb.pressed) {
h_scroll->set_val(h_scroll->get_val() - h_scroll->get_page() * mb.factor / 8);
}
if (mb.button_index == BUTTON_WHEEL_LEFT && mb.pressed) {
v_scroll->set_val(v_scroll->get_val() + v_scroll->get_page() * mb.factor / 8);
}
if (mb.button_index == BUTTON_RIGHT && mb.pressed) {
Point2 mpos = Point2(mb.x, mb.y) - ofs;

View File

@ -1041,7 +1041,7 @@ void CanvasItemEditor::_viewport_input_event(const InputEvent &p_event) {
return;
float prev_zoom = zoom;
zoom = zoom * 0.95;
zoom = zoom * (1 - (0.05 * b.factor));
{
Point2 ofs(b.x, b.y);
ofs = ofs / prev_zoom - ofs / zoom;
@ -1059,7 +1059,7 @@ void CanvasItemEditor::_viewport_input_event(const InputEvent &p_event) {
return;
float prev_zoom = zoom;
zoom = zoom * (1.0 / 0.95);
zoom = zoom * ((0.95 + (0.05 * b.factor)) / 0.95);
{
Point2 ofs(b.x, b.y);
ofs = ofs / prev_zoom - ofs / zoom;

View File

@ -523,10 +523,10 @@ void Polygon2DEditor::_uv_input(const InputEvent &p_input) {
} else if (mb.button_index == BUTTON_WHEEL_UP && mb.pressed) {
uv_zoom->set_val(uv_zoom->get_val() / 0.9);
uv_zoom->set_val(uv_zoom->get_val() / (1 - (0.1 * mb.factor)));
} else if (mb.button_index == BUTTON_WHEEL_DOWN && mb.pressed) {
uv_zoom->set_val(uv_zoom->get_val() * 0.9);
uv_zoom->set_val(uv_zoom->get_val() * (1 - (0.1 * mb.factor)));
}
} else if (p_input.type == InputEvent::MOUSE_MOTION) {

View File

@ -519,12 +519,12 @@ bool GridMapEditor::forward_spatial_input_event(Camera *p_camera, const InputEve
if (p_event.mouse_button.button_index == BUTTON_WHEEL_UP && (p_event.mouse_button.mod.command || p_event.mouse_button.mod.shift)) {
if (p_event.mouse_button.pressed)
floor->set_val(floor->get_val() + 1);
floor->set_val(floor->get_val() + p_event.mouse_button.factor);
return true; //eaten
} else if (p_event.mouse_button.button_index == BUTTON_WHEEL_DOWN && (p_event.mouse_button.mod.command || p_event.mouse_button.mod.shift)) {
if (p_event.mouse_button.pressed)
floor->set_val(floor->get_val() - 1);
floor->set_val(floor->get_val() - p_event.mouse_button.factor);
return true;
}

View File

@ -786,6 +786,22 @@ static int translateKey(unsigned int key) {
*/
}
inline void sendScrollEvent(int button, double factor) {
InputEvent ev;
ev.type = InputEvent::MOUSE_BUTTON;
ev.mouse_button.button_index = button;
ev.mouse_button.factor = factor;
ev.mouse_button.pressed = true;
ev.mouse_button.x = mouse_x;
ev.mouse_button.y = mouse_y;
ev.mouse_button.global_x = mouse_x;
ev.mouse_button.global_y = mouse_y;
ev.mouse_button.button_mask = button_mask;
OS_OSX::singleton->push_input(ev);
ev.mouse_button.pressed = false;
OS_OSX::singleton->push_input(ev);
}
- (void)scrollWheel:(NSEvent *)event {
double deltaX, deltaY;
@ -796,47 +812,21 @@ static int translateKey(unsigned int key) {
deltaY = [event scrollingDeltaY];
if ([event hasPreciseScrollingDeltas]) {
deltaX *= 0.1;
deltaY *= 0.1;
deltaX *= 0.03;
deltaY *= 0.03;
}
} else {
} else
#endif // MAC_OS_X_VERSION_MAX_ALLOWED
{
deltaX = [event deltaX];
deltaY = [event deltaY];
}
#else
deltaX = [event deltaX];
deltaY = [event deltaY];
#endif /*MAC_OS_X_VERSION_MAX_ALLOWED*/
if (fabs(deltaY)) {
InputEvent ev;
ev.type = InputEvent::MOUSE_BUTTON;
ev.mouse_button.button_index = deltaY > 0 ? BUTTON_WHEEL_UP : BUTTON_WHEEL_DOWN;
ev.mouse_button.pressed = true;
ev.mouse_button.x = mouse_x;
ev.mouse_button.y = mouse_y;
ev.mouse_button.global_x = mouse_x;
ev.mouse_button.global_y = mouse_y;
ev.mouse_button.button_mask = button_mask;
OS_OSX::singleton->push_input(ev);
ev.mouse_button.pressed = false;
OS_OSX::singleton->push_input(ev);
}
if (fabs(deltaX)) {
InputEvent ev;
ev.type = InputEvent::MOUSE_BUTTON;
ev.mouse_button.button_index = deltaX < 0 ? BUTTON_WHEEL_RIGHT : BUTTON_WHEEL_LEFT;
ev.mouse_button.pressed = true;
ev.mouse_button.x = mouse_x;
ev.mouse_button.y = mouse_y;
ev.mouse_button.global_x = mouse_x;
ev.mouse_button.global_y = mouse_y;
ev.mouse_button.button_mask = button_mask;
OS_OSX::singleton->push_input(ev);
ev.mouse_button.pressed = false;
OS_OSX::singleton->push_input(ev);
sendScrollEvent(0 > deltaX ? BUTTON_WHEEL_RIGHT : BUTTON_WHEEL_LEFT, fabs(deltaX * 0.3));
}
if (fabs(deltaY)) {
sendScrollEvent(0 < deltaY ? BUTTON_WHEEL_UP : BUTTON_WHEEL_DOWN, fabs(deltaY * 0.3));
}
}
@ -1648,7 +1638,6 @@ void OS_OSX::push_input(const InputEvent &p_event) {
InputEvent ev = p_event;
ev.ID = last_id++;
//print_line("EV: "+String(ev));
input->parse_input_event(ev);
}

View File

@ -508,10 +508,14 @@ LRESULT OS_Windows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
if (!motion)
return 0;
if (motion < 0)
if (motion < 0) {
mb.button_index = BUTTON_WHEEL_LEFT;
else
mb.factor = fabs((double)motion / (double)WHEEL_DELTA);
}
else {
mb.button_index = BUTTON_WHEEL_RIGHT;
mb.factor = fabs((double)motion / (double)WHEEL_DELTA);
}
} break;
/*
case WM_XBUTTONDOWN: {

View File

@ -717,6 +717,18 @@ void GraphEdit::_input_event(const InputEvent &p_ev) {
//too difficult to get right
//set_zoom(zoom/ZOOM_SCALE);
}
if (b.button_index == BUTTON_WHEEL_UP) {
h_scroll->set_val(h_scroll->get_val() - h_scroll->get_page() * b.factor / 8);
}
if (b.button_index == BUTTON_WHEEL_DOWN) {
h_scroll->set_val(h_scroll->get_val() + h_scroll->get_page() * b.factor / 8);
}
if (b.button_index == BUTTON_WHEEL_RIGHT) {
v_scroll->set_val(v_scroll->get_val() + v_scroll->get_page() * b.factor / 8);
}
if (b.button_index == BUTTON_WHEEL_LEFT) {
v_scroll->set_val(v_scroll->get_val() - v_scroll->get_page() * b.factor / 8);
}
}
if (p_ev.type == InputEvent::KEY && p_ev.key.scancode == KEY_D && p_ev.key.pressed && p_ev.key.mod.command) {

View File

@ -519,10 +519,14 @@ void ItemList::_input_event(const InputEvent &p_event) {
}
if (p_event.type == InputEvent::MOUSE_BUTTON && p_event.mouse_button.button_index == BUTTON_WHEEL_UP && p_event.mouse_button.pressed) {
scroll_bar->set_val(scroll_bar->get_val() - scroll_bar->get_page() * p_event.mouse_button.factor / 8);
scroll_bar->set_val(scroll_bar->get_val() - scroll_bar->get_page() / 8);
}
if (p_event.type == InputEvent::MOUSE_BUTTON && p_event.mouse_button.button_index == BUTTON_WHEEL_DOWN && p_event.mouse_button.pressed) {
scroll_bar->set_val(scroll_bar->get_val() + scroll_bar->get_page() * p_event.mouse_button.factor / 8);
scroll_bar->set_val(scroll_bar->get_val() + scroll_bar->get_page() / 8);
}

View File

@ -271,7 +271,7 @@ void PopupMenu::_input_event(const InputEvent &p_event) {
Point2 pos = get_pos();
int s = (vseparation + font->get_height()) * 3;
pos.y -= s;
pos.y -= (s * b.factor);
set_pos(pos);
//update hover
@ -291,7 +291,7 @@ void PopupMenu::_input_event(const InputEvent &p_event) {
Point2 pos = get_pos();
int s = (vseparation + font->get_height()) * 3;
pos.y += s;
pos.y += (s * b.factor);
set_pos(pos);
//update hover

View File

@ -782,12 +782,14 @@ void RichTextLabel::_input_event(InputEvent p_event) {
if (b.button_index == BUTTON_WHEEL_UP) {
if (scroll_active)
vscroll->set_val(vscroll->get_val() - vscroll->get_page() / 8);
vscroll->set_val(vscroll->get_val() - vscroll->get_page() * b.factor * 0.5 / 8);
}
if (b.button_index == BUTTON_WHEEL_DOWN) {
if (scroll_active)
vscroll->set_val(vscroll->get_val() + vscroll->get_page() / 8);
vscroll->set_val(vscroll->get_val() + vscroll->get_page() * b.factor * 0.5 / 8);
}
} break;
case InputEvent::KEY: {

View File

@ -85,20 +85,32 @@ void ScrollContainer::_input_event(const InputEvent &p_input_event) {
const InputEventMouseButton &mb = p_input_event.mouse_button;
if (mb.button_index == BUTTON_WHEEL_UP && mb.pressed) {
// only horizontal is enabled, scroll horizontally
if (h_scroll->is_visible() && !v_scroll->is_visible()) {
// only horizontal is enabled, scroll horizontally
h_scroll->set_val(h_scroll->get_val() - h_scroll->get_page() / 8);
h_scroll->set_val(h_scroll->get_val() - h_scroll->get_page() / 8 * mb.factor);
} else if (v_scroll->is_visible()) {
v_scroll->set_val(v_scroll->get_val() - v_scroll->get_page() / 8);
v_scroll->set_val(v_scroll->get_val() - v_scroll->get_page() / 8 * mb.factor);
}
}
if (mb.button_index == BUTTON_WHEEL_DOWN && mb.pressed) {
// only horizontal is enabled, scroll horizontally
if (h_scroll->is_visible() && !v_scroll->is_visible()) {
// only horizontal is enabled, scroll horizontally
h_scroll->set_val(h_scroll->get_val() + h_scroll->get_page() / 8);
h_scroll->set_val(h_scroll->get_val() + h_scroll->get_page() / 8 * mb.factor);
} else if (v_scroll->is_visible()) {
v_scroll->set_val(v_scroll->get_val() + v_scroll->get_page() / 8);
v_scroll->set_val(v_scroll->get_val() + v_scroll->get_page() / 8 * mb.factor);
}
}
if (mb.button_index == BUTTON_WHEEL_LEFT && mb.pressed) {
if (h_scroll->is_visible()) {
h_scroll->set_val(h_scroll->get_val() - h_scroll->get_page() * mb.factor / 8);
}
}
if (mb.button_index == BUTTON_WHEEL_RIGHT && mb.pressed) {
if (h_scroll->is_visible()) {
h_scroll->set_val(h_scroll->get_val() + h_scroll->get_page() * mb.factor / 8);
}
}

View File

@ -113,13 +113,15 @@ void SpinBox::_input_event(const InputEvent &p_event) {
} break;
case BUTTON_WHEEL_UP: {
if (line_edit->has_focus()) {
set_val(get_val() + get_step());
set_val(get_val() + get_step() * mb.factor);
accept_event();
}
} break;
case BUTTON_WHEEL_DOWN: {
if (line_edit->has_focus()) {
set_val(get_val() - get_step());
set_val(get_val() - get_step() * mb.factor);
accept_event();
}
} break;

View File

@ -333,7 +333,10 @@ void TextEdit::_update_scrollbars() {
v_scroll->show();
v_scroll->set_max(total_rows);
v_scroll->set_page(visible_rows);
v_scroll->set_val(cursor.line_ofs);
if (fabs(v_scroll->get_val() - (double)cursor.line_ofs) >= 1) {
v_scroll->set_val(cursor.line_ofs);
}
} else {
cursor.line_ofs = 0;
@ -345,7 +348,9 @@ void TextEdit::_update_scrollbars() {
h_scroll->show();
h_scroll->set_max(total_width);
h_scroll->set_page(visible_width);
h_scroll->set_val(cursor.x_ofs);
if (fabs(h_scroll->get_val() - (double)cursor.x_ofs) >= 1) {
h_scroll->set_val(cursor.x_ofs);
}
} else {
@ -1431,17 +1436,18 @@ void TextEdit::_input_event(const InputEvent &p_input_event) {
}
if (mb.pressed) {
if (mb.button_index == BUTTON_WHEEL_UP && !mb.mod.command) {
v_scroll->set_val(v_scroll->get_val() - 3);
v_scroll->set_val(v_scroll->get_val() - (3 * mb.factor));
}
if (mb.button_index == BUTTON_WHEEL_DOWN && !mb.mod.command) {
v_scroll->set_val(v_scroll->get_val() + 3);
v_scroll->set_val(v_scroll->get_val() + (3 * mb.factor));
}
if (mb.button_index == BUTTON_WHEEL_LEFT) {
h_scroll->set_val(h_scroll->get_val() - 3);
h_scroll->set_val(h_scroll->get_val() - (100 * mb.factor));
}
if (mb.button_index == BUTTON_WHEEL_RIGHT) {
h_scroll->set_val(h_scroll->get_val() + 3);
h_scroll->set_val(h_scroll->get_val() + (100 * mb.factor));
}
if (mb.button_index == BUTTON_LEFT) {

View File

@ -2299,11 +2299,11 @@ void Tree::_input_event(InputEvent p_event) {
} break;
case BUTTON_WHEEL_UP: {
v_scroll->set_val(v_scroll->get_val() - v_scroll->get_page() / 8);
v_scroll->set_val(v_scroll->get_val() - v_scroll->get_page() * b.factor / 8);
} break;
case BUTTON_WHEEL_DOWN: {
v_scroll->set_val(v_scroll->get_val() + v_scroll->get_page() / 8);
v_scroll->set_val(v_scroll->get_val() + v_scroll->get_page() * b.factor / 8);
} break;
}