Merge pull request #15728 from Zylann/fix_freelook_null_shortcut

Fix crash when freelook shortcuts are undefined
This commit is contained in:
Rémi Verschelde 2018-01-15 07:40:25 +01:00 committed by GitHub
commit cd62fd48f9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2017,6 +2017,20 @@ Point2i SpatialEditorViewport::_get_warped_mouse_motion(const Ref<InputEventMous
return relative; return relative;
} }
static bool is_shortcut_pressed(const String &p_path) {
Ref<ShortCut> shortcut = ED_GET_SHORTCUT(p_path);
if (shortcut.is_null()) {
return false;
}
InputEventKey *k = Object::cast_to<InputEventKey>(shortcut->get_shortcut().ptr());
if (k == NULL) {
return false;
}
const Input &input = *Input::get_singleton();
int scancode = k->get_scancode();
return input.is_key_pressed(scancode);
}
void SpatialEditorViewport::_update_freelook(real_t delta) { void SpatialEditorViewport::_update_freelook(real_t delta) {
if (!is_freelook_active()) { if (!is_freelook_active()) {
@ -2027,38 +2041,28 @@ void SpatialEditorViewport::_update_freelook(real_t delta) {
Vector3 right = camera->get_transform().basis.xform(Vector3(1, 0, 0)); Vector3 right = camera->get_transform().basis.xform(Vector3(1, 0, 0));
Vector3 up = camera->get_transform().basis.xform(Vector3(0, 1, 0)); Vector3 up = camera->get_transform().basis.xform(Vector3(0, 1, 0));
int key_left = Object::cast_to<InputEventKey>(ED_GET_SHORTCUT("spatial_editor/freelook_left")->get_shortcut().ptr())->get_scancode();
int key_right = Object::cast_to<InputEventKey>(ED_GET_SHORTCUT("spatial_editor/freelook_right")->get_shortcut().ptr())->get_scancode();
int key_forward = Object::cast_to<InputEventKey>(ED_GET_SHORTCUT("spatial_editor/freelook_forward")->get_shortcut().ptr())->get_scancode();
int key_backwards = Object::cast_to<InputEventKey>(ED_GET_SHORTCUT("spatial_editor/freelook_backwards")->get_shortcut().ptr())->get_scancode();
int key_up = Object::cast_to<InputEventKey>(ED_GET_SHORTCUT("spatial_editor/freelook_up")->get_shortcut().ptr())->get_scancode();
int key_down = Object::cast_to<InputEventKey>(ED_GET_SHORTCUT("spatial_editor/freelook_down")->get_shortcut().ptr())->get_scancode();
int key_speed_modifier = Object::cast_to<InputEventKey>(ED_GET_SHORTCUT("spatial_editor/freelook_speed_modifier")->get_shortcut().ptr())->get_scancode();
Vector3 direction; Vector3 direction;
bool speed_modifier = false; bool speed_modifier = false;
const Input &input = *Input::get_singleton(); if (is_shortcut_pressed("spatial_editor/freelook_left")) {
if (input.is_key_pressed(key_left)) {
direction -= right; direction -= right;
} }
if (input.is_key_pressed(key_right)) { if (is_shortcut_pressed("spatial_editor/freelook_right")) {
direction += right; direction += right;
} }
if (input.is_key_pressed(key_forward)) { if (is_shortcut_pressed("spatial_editor/freelook_forward")) {
direction += forward; direction += forward;
} }
if (input.is_key_pressed(key_backwards)) { if (is_shortcut_pressed("spatial_editor/freelook_backwards")) {
direction -= forward; direction -= forward;
} }
if (input.is_key_pressed(key_up)) { if (is_shortcut_pressed("spatial_editor/freelook_up")) {
direction += up; direction += up;
} }
if (input.is_key_pressed(key_down)) { if (is_shortcut_pressed("spatial_editor/freelook_down")) {
direction -= up; direction -= up;
} }
if (input.is_key_pressed(key_speed_modifier)) { if (is_shortcut_pressed("spatial_editor/freelook_speed_modifier")) {
speed_modifier = true; speed_modifier = true;
} }