Skip to content

Commit

Permalink
Merge pull request #12573 from poke1024/macostouchpad
Browse files Browse the repository at this point in the history
Native pan and zoom for macOS + InputEventGesture
  • Loading branch information
akien-mga authored Nov 21, 2017
2 parents ec4d467 + 80ad8af commit 5a23136
Show file tree
Hide file tree
Showing 21 changed files with 551 additions and 135 deletions.
90 changes: 82 additions & 8 deletions core/os/input_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,14 @@ bool InputEventWithModifiers::get_command() const {
return command;
}

void InputEventWithModifiers::set_modifiers_from_event(const InputEventWithModifiers *event) {

set_alt(event->get_alt());
set_shift(event->get_shift());
set_control(event->get_control());
set_metakey(event->get_metakey());
}

void InputEventWithModifiers::_bind_methods() {

ClassDB::bind_method(D_METHOD("set_alt", "enable"), &InputEventWithModifiers::set_alt);
Expand Down Expand Up @@ -436,10 +444,7 @@ Ref<InputEvent> InputEventMouseButton::xformed_by(const Transform2D &p_xform, co
mb->set_id(get_id());
mb->set_device(get_device());

mb->set_alt(get_alt());
mb->set_shift(get_shift());
mb->set_control(get_control());
mb->set_metakey(get_metakey());
mb->set_modifiers_from_event(this);

mb->set_position(l);
mb->set_global_position(g);
Expand Down Expand Up @@ -555,10 +560,7 @@ Ref<InputEvent> InputEventMouseMotion::xformed_by(const Transform2D &p_xform, co
mm->set_id(get_id());
mm->set_device(get_device());

mm->set_alt(get_alt());
mm->set_shift(get_shift());
mm->set_control(get_control());
mm->set_metakey(get_metakey());
mm->set_modifiers_from_event(this);

mm->set_position(l);
mm->set_global_position(g);
Expand Down Expand Up @@ -930,3 +932,75 @@ void InputEventAction::_bind_methods() {
InputEventAction::InputEventAction() {
pressed = false;
}
/////////////////////////////

void InputEventGesture::set_position(const Vector2 &p_pos) {

pos = p_pos;
}

Vector2 InputEventGesture::get_position() const {

return pos;
}
/////////////////////////////

void InputEventMagnifyGesture::set_factor(real_t p_factor) {

factor = p_factor;
}

real_t InputEventMagnifyGesture::get_factor() const {

return factor;
}

Ref<InputEvent> InputEventMagnifyGesture::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {

Ref<InputEventMagnifyGesture> ev;
ev.instance();

ev->set_id(get_id());
ev->set_device(get_device());
ev->set_modifiers_from_event(this);

ev->set_position(p_xform.xform(get_position() + p_local_ofs));
ev->set_factor(get_factor());

return ev;
}

InputEventMagnifyGesture::InputEventMagnifyGesture() {

factor = 1.0;
}
/////////////////////////////

void InputEventPanGesture::set_delta(const Vector2 &p_delta) {

delta = p_delta;
}

Vector2 InputEventPanGesture::get_delta() const {
return delta;
}

Ref<InputEvent> InputEventPanGesture::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const {

Ref<InputEventPanGesture> ev;
ev.instance();

ev->set_id(get_id());
ev->set_device(get_device());
ev->set_modifiers_from_event(this);

ev->set_position(p_xform.xform(get_position() + p_local_ofs));
ev->set_delta(get_delta());

return ev;
}

InputEventPanGesture::InputEventPanGesture() {

delta = Vector2(0, 0);
}
40 changes: 40 additions & 0 deletions core/os/input_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ class InputEventWithModifiers : public InputEvent {
void set_command(bool p_enabled);
bool get_command() const;

void set_modifiers_from_event(const InputEventWithModifiers *event);

InputEventWithModifiers();
};

Expand Down Expand Up @@ -468,4 +470,42 @@ class InputEventAction : public InputEvent {
InputEventAction();
};

class InputEventGesture : public InputEventWithModifiers {

GDCLASS(InputEventGesture, InputEventWithModifiers)

Vector2 pos;

public:
void set_position(const Vector2 &p_pos);
Vector2 get_position() const;
};

class InputEventMagnifyGesture : public InputEventGesture {

GDCLASS(InputEventMagnifyGesture, InputEventGesture)
real_t factor;

public:
void set_factor(real_t p_factor);
real_t get_factor() const;

virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const;

InputEventMagnifyGesture();
};

class InputEventPanGesture : public InputEventGesture {

GDCLASS(InputEventPanGesture, InputEventGesture)
Vector2 delta;

public:
void set_delta(const Vector2 &p_delta);
Vector2 get_delta() const;

virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const;

InputEventPanGesture();
};
#endif
12 changes: 12 additions & 0 deletions editor/animation_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2889,6 +2889,18 @@ void AnimationKeyEditor::_track_editor_gui_input(const Ref<InputEvent> &p_input)
}
}
}

Ref<InputEventMagnifyGesture> magnify_gesture = p_input;
if (magnify_gesture.is_valid()) {
zoom->set_value(zoom->get_value() * magnify_gesture->get_factor());
}

Ref<InputEventPanGesture> pan_gesture = p_input;
if (pan_gesture.is_valid()) {

h_scroll->set_value(h_scroll->get_value() - h_scroll->get_page() * pan_gesture->get_delta().x / 8);
v_scroll->set_value(v_scroll->get_value() - v_scroll->get_page() * pan_gesture->get_delta().y / 8);
}
}

void AnimationKeyEditor::_notification(int p_what) {
Expand Down
38 changes: 33 additions & 5 deletions editor/code_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,23 @@ void CodeTextEditor::_text_editor_gui_input(const Ref<InputEvent> &p_event) {
}
}

Ref<InputEventMagnifyGesture> magnify_gesture = p_event;
if (magnify_gesture.is_valid()) {

Ref<DynamicFont> font = text_editor->get_font("font");

if (font.is_valid()) {
if (font->get_size() != (int)font_size) {
font_size = font->get_size();
}

font_size *= powf(magnify_gesture->get_factor(), 0.25);

_add_font_size((int)font_size - font->get_size());
}
return;
}

Ref<InputEventKey> k = p_event;

if (k.is_valid()) {
Expand All @@ -999,14 +1016,15 @@ void CodeTextEditor::_text_editor_gui_input(const Ref<InputEvent> &p_event) {

void CodeTextEditor::_zoom_in() {
font_resize_val += EDSCALE;

if (font_resize_timer->get_time_left() == 0)
font_resize_timer->start();
_zoom_changed();
}

void CodeTextEditor::_zoom_out() {
font_resize_val -= EDSCALE;
_zoom_changed();
}

void CodeTextEditor::_zoom_changed() {
if (font_resize_timer->get_time_left() == 0)
font_resize_timer->start();
}
Expand Down Expand Up @@ -1067,16 +1085,25 @@ void CodeTextEditor::_complete_request() {

void CodeTextEditor::_font_resize_timeout() {

if (_add_font_size(font_resize_val)) {
font_resize_val = 0;
}
}

bool CodeTextEditor::_add_font_size(int p_delta) {

Ref<DynamicFont> font = text_editor->get_font("font");

if (font.is_valid()) {
int new_size = CLAMP(font->get_size() + font_resize_val, 8 * EDSCALE, 96 * EDSCALE);
int new_size = CLAMP(font->get_size() + p_delta, 8 * EDSCALE, 96 * EDSCALE);
if (new_size != font->get_size()) {
EditorSettings::get_singleton()->set("interface/editor/source_font_size", new_size / EDSCALE);
font->set_size(new_size);
}

font_resize_val = 0;
return true;
} else {
return false;
}
}

Expand Down Expand Up @@ -1285,6 +1312,7 @@ CodeTextEditor::CodeTextEditor() {
code_complete_timer->connect("timeout", this, "_code_complete_timer_timeout");

font_resize_val = 0;
font_size = -1;
font_resize_timer = memnew(Timer);
add_child(font_resize_timer);
font_resize_timer->set_one_shot(true);
Expand Down
3 changes: 3 additions & 0 deletions editor/code_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ class CodeTextEditor : public VBoxContainer {

Timer *font_resize_timer;
int font_resize_val;
real_t font_size;

Label *error;

Expand All @@ -212,10 +213,12 @@ class CodeTextEditor : public VBoxContainer {
void _update_font();
void _complete_request();
void _font_resize_timeout();
bool _add_font_size(int p_delta);

void _text_editor_gui_input(const Ref<InputEvent> &p_event);
void _zoom_in();
void _zoom_out();
void _zoom_changed();
void _reset_zoom();

CodeTextEditorCodeCompleteFunc code_complete_func;
Expand Down
16 changes: 16 additions & 0 deletions editor/plugins/canvas_item_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1442,6 +1442,22 @@ void CanvasItemEditor::_gui_input_viewport(const Ref<InputEvent> &p_event) {
}
}

Ref<InputEventMagnifyGesture> magnify_gesture = p_event;
if (magnify_gesture.is_valid()) {

_zoom_on_position(zoom * magnify_gesture->get_factor(), magnify_gesture->get_position());
return;
}

Ref<InputEventPanGesture> pan_gesture = p_event;
if (pan_gesture.is_valid()) {

const Vector2 delta = (int(EditorSettings::get_singleton()->get("editors/2d/pan_speed")) / zoom) * pan_gesture->get_delta();
h_scroll->set_value(h_scroll->get_value() + delta.x);
v_scroll->set_value(v_scroll->get_value() + delta.y);
return;
}

Ref<InputEventMouseButton> b = p_event;
if (b.is_valid()) {
// Button event
Expand Down
13 changes: 13 additions & 0 deletions editor/plugins/polygon_2d_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,19 @@ void Polygon2DEditor::_uv_input(const Ref<InputEvent> &p_input) {
uv_edit_draw->update();
}
}

Ref<InputEventMagnifyGesture> magnify_gesture = p_input;
if (magnify_gesture.is_valid()) {

uv_zoom->set_value(uv_zoom->get_value() * magnify_gesture->get_factor());
}

Ref<InputEventPanGesture> pan_gesture = p_input;
if (pan_gesture.is_valid()) {

uv_hscroll->set_value(uv_hscroll->get_value() + uv_hscroll->get_page() * pan_gesture->get_delta().x / 8);
uv_vscroll->set_value(uv_vscroll->get_value() + uv_vscroll->get_page() * pan_gesture->get_delta().y / 8);
}
}

void Polygon2DEditor::_uv_scroll_changed(float) {
Expand Down
Loading

0 comments on commit 5a23136

Please sign in to comment.