void spc_curve_to(snap_path_closure_t& spc, double x1, double y1_, double x2, double y2, double x3, double y3) { spc_new_path_perhaps(spc); snap_point(spc, x1, y1_); snap_point(spc, x2, y2); snap_point(spc, x3, y3); curve_to(spc.cr, POINT{ x1, y1_ }, POINT{ x2, y2 }, POINT{ x3, y3 }); }
bool LightOccluder2DEditor::forward_input_event(const InputEvent& p_event) { if (!node) return false; if (node->get_occluder_polygon().is_null()) { if (p_event.type==InputEvent::MOUSE_BUTTON && p_event.mouse_button.button_index==1 && p_event.mouse_button.pressed) { create_poly->set_text("No OccluderPolygon2D resource on this node.\nCreate and assign one?"); create_poly->popup_centered_minsize(); } return false; } switch(p_event.type) { case InputEvent::MOUSE_BUTTON: { const InputEventMouseButton &mb=p_event.mouse_button; Matrix32 xform = canvas_item_editor->get_canvas_transform() * node->get_global_transform(); Vector2 gpoint = Point2(mb.x,mb.y); Vector2 cpoint = canvas_item_editor->get_canvas_transform().affine_inverse().xform(gpoint); cpoint=snap_point(cpoint); cpoint = node->get_global_transform().affine_inverse().xform(cpoint); Vector<Vector2> poly = Variant(node->get_occluder_polygon()->get_polygon()); //first check if a point is to be added (segment split) real_t grab_treshold=EDITOR_DEF("poly_editor/point_grab_radius",8); switch(mode) { case MODE_CREATE: { if (mb.button_index==BUTTON_LEFT && mb.pressed) { if (!wip_active) { wip.clear(); wip.push_back( cpoint ); wip_active=true; edited_point_pos=cpoint; canvas_item_editor->get_viewport_control()->update(); edited_point=1; return true; } else { if (wip.size()>1 && xform.xform(wip[0]).distance_to(gpoint)<grab_treshold) { //wip closed _wip_close(true); return true; } else if (wip.size()>1 && xform.xform(wip[wip.size()-1]).distance_to(gpoint)<grab_treshold) { //wip closed _wip_close(false); return true; } else { wip.push_back( cpoint ); edited_point=wip.size(); canvas_item_editor->get_viewport_control()->update(); return true; //add wip point } } } else if (mb.button_index==BUTTON_RIGHT && mb.pressed && wip_active) { _wip_close(true); } } break; case MODE_EDIT: { if (mb.button_index==BUTTON_LEFT) { if (mb.pressed) { if (mb.mod.control) { if (poly.size() < 3) { undo_redo->create_action("Edit Poly"); undo_redo->add_undo_method(node->get_occluder_polygon().ptr(),"set_polygon",poly); poly.push_back(cpoint); undo_redo->add_do_method(node->get_occluder_polygon().ptr(),"set_polygon",poly); undo_redo->add_do_method(canvas_item_editor->get_viewport_control(),"update"); undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(),"update"); undo_redo->commit_action(); return true; } //search edges int closest_idx=-1; Vector2 closest_pos; real_t closest_dist=1e10; for(int i=0;i<poly.size();i++) { Vector2 points[2] ={ xform.xform(poly[i]), xform.xform(poly[(i+1)%poly.size()]) }; Vector2 cp = Geometry::get_closest_point_to_segment_2d(gpoint,points); if (cp.distance_squared_to(points[0])<CMP_EPSILON2 || cp.distance_squared_to(points[1])<CMP_EPSILON2) continue; //not valid to reuse point real_t d = cp.distance_to(gpoint); if (d<closest_dist && d<grab_treshold) { closest_dist=d; closest_pos=cp; closest_idx=i; } } if (closest_idx>=0) { pre_move_edit=poly; poly.insert(closest_idx+1,xform.affine_inverse().xform(closest_pos)); edited_point=closest_idx+1; edited_point_pos=xform.affine_inverse().xform(closest_pos); node->get_occluder_polygon()->set_polygon(Variant(poly)); canvas_item_editor->get_viewport_control()->update(); return true; } } else { //look for points to move int closest_idx=-1; Vector2 closest_pos; real_t closest_dist=1e10; for(int i=0;i<poly.size();i++) { Vector2 cp =xform.xform(poly[i]); real_t d = cp.distance_to(gpoint); if (d<closest_dist && d<grab_treshold) { closest_dist=d; closest_pos=cp; closest_idx=i; } } if (closest_idx>=0) { pre_move_edit=poly; edited_point=closest_idx; edited_point_pos=xform.affine_inverse().xform(closest_pos); canvas_item_editor->get_viewport_control()->update(); return true; } } } else { if (edited_point!=-1) { //apply ERR_FAIL_INDEX_V(edited_point,poly.size(),false); poly[edited_point]=edited_point_pos; undo_redo->create_action("Edit Poly"); undo_redo->add_do_method(node->get_occluder_polygon().ptr(),"set_polygon",poly); undo_redo->add_undo_method(node->get_occluder_polygon().ptr(),"set_polygon",pre_move_edit); undo_redo->add_do_method(canvas_item_editor->get_viewport_control(),"update"); undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(),"update"); undo_redo->commit_action(); edited_point=-1; return true; } } } if (mb.button_index==BUTTON_RIGHT && mb.pressed && edited_point==-1) { int closest_idx=-1; Vector2 closest_pos; real_t closest_dist=1e10; for(int i=0;i<poly.size();i++) { Vector2 cp =xform.xform(poly[i]); real_t d = cp.distance_to(gpoint); if (d<closest_dist && d<grab_treshold) { closest_dist=d; closest_pos=cp; closest_idx=i; } } if (closest_idx>=0) { undo_redo->create_action("Edit Poly (Remove Point)"); undo_redo->add_undo_method(node->get_occluder_polygon().ptr(),"set_polygon",poly); poly.remove(closest_idx); undo_redo->add_do_method(node->get_occluder_polygon().ptr(),"set_polygon",poly); undo_redo->add_do_method(canvas_item_editor->get_viewport_control(),"update"); undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(),"update"); undo_redo->commit_action(); return true; } } } break; } } break; case InputEvent::MOUSE_MOTION: { const InputEventMouseMotion &mm=p_event.mouse_motion; if (edited_point!=-1 && (wip_active || mm.button_mask&BUTTON_MASK_LEFT)) { Vector2 gpoint = Point2(mm.x,mm.y); Vector2 cpoint = canvas_item_editor->get_canvas_transform().affine_inverse().xform(gpoint); cpoint=snap_point(cpoint); edited_point_pos = node->get_global_transform().affine_inverse().xform(cpoint); canvas_item_editor->get_viewport_control()->update(); } } break; } return false; }
void SpriteRegionEditor::_region_input(const InputEvent& p_input) { Matrix32 mtx; mtx.elements[2]=-draw_ofs; mtx.scale_basis(Vector2(draw_zoom,draw_zoom)); Vector2 endpoints[8]={ mtx.xform(rect.pos)+Vector2(-4,-4), mtx.xform(rect.pos+Vector2(rect.size.x/2,0))+Vector2(0,-4), mtx.xform(rect.pos+Vector2(rect.size.x,0))+Vector2(4,-4), mtx.xform(rect.pos+Vector2(rect.size.x,rect.size.y/2))+Vector2(4,0), mtx.xform(rect.pos+rect.size)+Vector2(4,4), mtx.xform(rect.pos+Vector2(rect.size.x/2,rect.size.y))+Vector2(0,4), mtx.xform(rect.pos+Vector2(0,rect.size.y))+Vector2(-4,4), mtx.xform(rect.pos+Vector2(0,rect.size.y/2))+Vector2(-4,0) }; if (p_input.type==InputEvent::MOUSE_BUTTON) { const InputEventMouseButton &mb=p_input.mouse_button; if (mb.button_index==BUTTON_LEFT) { if (mb.pressed) { drag_from=mtx.affine_inverse().xform(Vector2(mb.x,mb.y)); drag_from=snap_point(drag_from); drag=true; rect_prev=node->get_region_rect(); drag_index=-1; for(int i=0;i<8;i++) { Vector2 tuv=endpoints[i]; if (tuv.distance_to(Vector2(mb.x,mb.y))<8) { drag_index=i; creating = false; } } if (drag_index==-1) { creating = true; rect = Rect2(drag_from,Size2()); } } else if (drag) { undo_redo->create_action(TTR("Set region_rect")); undo_redo->add_do_method(node,"set_region_rect",node->get_region_rect()); undo_redo->add_undo_method(node,"set_region_rect",rect_prev); undo_redo->add_do_method(edit_draw,"update"); undo_redo->add_undo_method(edit_draw,"update"); undo_redo->commit_action(); drag=false; } } else if (mb.button_index==BUTTON_RIGHT && mb.pressed) { if (drag) { drag=false; node->set_region_rect(rect_prev); rect=rect_prev; edit_draw->update(); } } else if (mb.button_index==BUTTON_WHEEL_UP && mb.pressed) { zoom->set_val( zoom->get_val()/0.9 ); } else if (mb.button_index==BUTTON_WHEEL_DOWN && mb.pressed) { zoom->set_val( zoom->get_val()*0.9); } } else if (p_input.type==InputEvent::MOUSE_MOTION) { const InputEventMouseMotion &mm=p_input.mouse_motion; if (mm.button_mask&BUTTON_MASK_MIDDLE || Input::get_singleton()->is_key_pressed(KEY_SPACE)) { Vector2 draged(mm.relative_x,mm.relative_y); hscroll->set_val( hscroll->get_val()-draged.x ); vscroll->set_val( vscroll->get_val()-draged.y ); } else if (drag) { Vector2 new_pos = mtx.affine_inverse().xform(Vector2(mm.x,mm.y)); new_pos = snap_point(new_pos); if (creating) { rect = Rect2(drag_from,Size2()); rect.expand_to(new_pos); node->set_region_rect(rect); edit_draw->update(); return; } switch(drag_index) { case 0: { Vector2 p=rect_prev.pos+rect_prev.size; rect = Rect2(p,Size2()); rect.expand_to(new_pos); node->set_region_rect(rect); } break; case 1: { Vector2 p=rect_prev.pos+Vector2(0,rect_prev.size.y); rect = Rect2(p,Size2(rect_prev.size.x,0)); rect.expand_to(new_pos); node->set_region_rect(rect); } break; case 2: { Vector2 p=rect_prev.pos+Vector2(0,rect_prev.size.y); rect = Rect2(p,Size2()); rect.expand_to(new_pos); node->set_region_rect(rect); } break; case 3: { Vector2 p=rect_prev.pos; rect = Rect2(p,Size2(0,rect_prev.size.y)); rect.expand_to(new_pos); node->set_region_rect(rect); } break; case 4: { Vector2 p=rect_prev.pos; rect = Rect2(p,Size2()); rect.expand_to(new_pos); node->set_region_rect(rect); } break; case 5: { Vector2 p=rect_prev.pos; rect = Rect2(p,Size2(rect_prev.size.x,0)); rect.expand_to(new_pos); node->set_region_rect(rect); } break; case 6: { Vector2 p=rect_prev.pos+Vector2(rect_prev.size.x,0); rect = Rect2(p,Size2()); rect.expand_to(new_pos); node->set_region_rect(rect); } break; case 7: { Vector2 p=rect_prev.pos+Vector2(rect_prev.size.x,0); rect = Rect2(p,Size2(0,rect_prev.size.y)); rect.expand_to(new_pos); node->set_region_rect(rect); } break; } edit_draw->update(); } } }
bool Path2DEditor::forward_input_event(const InputEvent& p_event) { if (!node) return false; if (!node->is_visible()) return false; if (!node->get_curve().is_valid()) return false; switch(p_event.type) { case InputEvent::MOUSE_BUTTON: { const InputEventMouseButton &mb=p_event.mouse_button; Matrix32 xform = canvas_item_editor->get_canvas_transform() * node->get_global_transform(); Vector2 gpoint = Point2(mb.x,mb.y); Vector2 cpoint = !mb.mod.alt? snap_point(xform.affine_inverse().xform(gpoint)) : node->get_global_transform().affine_inverse().xform( snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(gpoint)) ); //first check if a point is to be added (segment split) real_t grab_treshold=EDITOR_DEF("poly_editor/point_grab_radius",8); // Test move point!! if ( mb.pressed && action==ACTION_NONE ) { Ref<Curve2D> curve = node->get_curve(); for(int i=0;i<curve->get_point_count();i++) { bool pointunder=false; { Point2 p = xform.xform( curve->get_point_pos(i) ); if (gpoint.distance_to(p) < grab_treshold ) { if (mb.button_index==BUTTON_LEFT && !mb.mod.shift && mode==MODE_EDIT) { action=ACTION_MOVING_POINT; action_point=i; moving_from=curve->get_point_pos(i); moving_screen_from=gpoint; return true; } else if ((mb.button_index==BUTTON_RIGHT && mode==MODE_EDIT) || (mb.button_index==BUTTON_LEFT && mode==MODE_DELETE)) { undo_redo->create_action("Remove Point from Curve"); undo_redo->add_do_method(curve.ptr(),"remove_point",i); undo_redo->add_undo_method(curve.ptr(),"add_point",curve->get_point_pos(i),curve->get_point_in(i),curve->get_point_out(i),i); undo_redo->add_do_method(canvas_item_editor->get_viewport_control(),"update"); undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(),"update"); undo_redo->commit_action(); return true; } else pointunder=true; } } if (mb.button_index==BUTTON_LEFT && i<(curve->get_point_count()-1)) { Point2 p = xform.xform( curve->get_point_pos(i)+curve->get_point_out(i) ); if (gpoint.distance_to(p) < grab_treshold && (mode == MODE_EDIT || mode==MODE_EDIT_CURVE) ) { action=ACTION_MOVING_OUT; action_point=i; moving_from=curve->get_point_out(i); moving_screen_from=gpoint; return true; } } if (mb.button_index==BUTTON_LEFT && i>0) { Point2 p = xform.xform( curve->get_point_pos(i)+curve->get_point_in(i) ); if (gpoint.distance_to(p) < grab_treshold && (mode == MODE_EDIT || mode==MODE_EDIT_CURVE)) { action=ACTION_MOVING_IN; action_point=i; moving_from=curve->get_point_in(i); moving_screen_from=gpoint; return true; } } if (pointunder) return true; } } // Test add point in empty space! if ( mb.pressed && mb.button_index==BUTTON_LEFT && ((mb.mod.command && mode == MODE_EDIT) || mode == MODE_CREATE)) { Ref<Curve2D> curve = node->get_curve(); undo_redo->create_action("Add Point to Curve"); undo_redo->add_do_method(curve.ptr(),"add_point",cpoint); undo_redo->add_undo_method(curve.ptr(),"remove_point",curve->get_point_count()); undo_redo->add_do_method(canvas_item_editor->get_viewport_control(),"update"); undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(),"update"); undo_redo->commit_action(); action=ACTION_MOVING_POINT; action_point=curve->get_point_count()-1; moving_from=curve->get_point_pos(action_point); moving_screen_from=gpoint; canvas_item_editor->get_viewport_control()->update(); return true; } if ( !mb.pressed && mb.button_index==BUTTON_LEFT && action!=ACTION_NONE) { Ref<Curve2D> curve = node->get_curve(); Vector2 new_pos = moving_from + xform.basis_xform( gpoint - moving_screen_from ); switch(action) { case ACTION_MOVING_POINT: { undo_redo->create_action("Move Point in Curve"); undo_redo->add_do_method(curve.ptr(),"set_point_pos",action_point,cpoint); undo_redo->add_undo_method(curve.ptr(),"set_point_pos",action_point,moving_from); undo_redo->add_do_method(canvas_item_editor->get_viewport_control(),"update"); undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(),"update"); undo_redo->commit_action(); } break; case ACTION_MOVING_IN: { undo_redo->create_action("Move In-Control in Curve"); undo_redo->add_do_method(curve.ptr(),"set_point_in",action_point,new_pos); undo_redo->add_undo_method(curve.ptr(),"set_point_in",action_point,moving_from); undo_redo->add_do_method(canvas_item_editor->get_viewport_control(),"update"); undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(),"update"); undo_redo->commit_action(); } break; case ACTION_MOVING_OUT: { undo_redo->create_action("Move Out-Control in Curve"); undo_redo->add_do_method(curve.ptr(),"set_point_out",action_point,new_pos); undo_redo->add_undo_method(curve.ptr(),"set_point_out",action_point,moving_from); undo_redo->add_do_method(canvas_item_editor->get_viewport_control(),"update"); undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(),"update"); undo_redo->commit_action(); } break; } action=ACTION_NONE; return true; } #if 0 switch(mode) { case MODE_CREATE: { if (mb.button_index==BUTTON_LEFT && mb.pressed) { if (!wip_active) { wip.clear(); wip.push_back( snap_point(cpoint) ); wip_active=true; edited_point_pos=snap_point(cpoint); canvas_item_editor->update(); edited_point=1; return true; } else { if (wip.size()>1 && xform.xform(wip[0]).distance_to(gpoint)<grab_treshold) { //wip closed _wip_close(); return true; } else { wip.push_back( snap_point(cpoint) ); edited_point=wip.size(); canvas_item_editor->update(); return true; //add wip point } } } else if (mb.button_index==BUTTON_RIGHT && mb.pressed && wip_active) { _wip_close(); } } break; case MODE_EDIT: { if (mb.button_index==BUTTON_LEFT) { if (mb.pressed) { if (mb.mod.control) { if (poly.size() < 3) { undo_redo->create_action("Edit Poly"); undo_redo->add_undo_method(node,"set_polygon",poly); poly.push_back(cpoint); undo_redo->add_do_method(node,"set_polygon",poly); undo_redo->add_do_method(canvas_item_editor->get_viewport_control(),"update"); undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(),"update"); undo_redo->commit_action(); return true; } //search edges int closest_idx=-1; Vector2 closest_pos; real_t closest_dist=1e10; for(int i=0;i<poly.size();i++) { Vector2 points[2] ={ xform.xform(poly[i]), xform.xform(poly[(i+1)%poly.size()]) }; Vector2 cp = Geometry::get_closest_point_to_segment_2d(gpoint,points); if (cp.distance_squared_to(points[0])<CMP_EPSILON2 || cp.distance_squared_to(points[1])<CMP_EPSILON2) continue; //not valid to reuse point real_t d = cp.distance_to(gpoint); if (d<closest_dist && d<grab_treshold) { closest_dist=d; closest_pos=cp; closest_idx=i; } } if (closest_idx>=0) { pre_move_edit=poly; poly.insert(closest_idx+1,snap_point(xform.affine_inverse().xform(closest_pos))); edited_point=closest_idx+1; edited_point_pos=snap_point(xform.affine_inverse().xform(closest_pos)); node->set_polygon(poly); canvas_item_editor->update(); return true; } } else { //look for points to move int closest_idx=-1; Vector2 closest_pos; real_t closest_dist=1e10; for(int i=0;i<poly.size();i++) { Vector2 cp =xform.xform(poly[i]); real_t d = cp.distance_to(gpoint); if (d<closest_dist && d<grab_treshold) { closest_dist=d; closest_pos=cp; closest_idx=i; } } if (closest_idx>=0) { pre_move_edit=poly; edited_point=closest_idx; edited_point_pos=xform.affine_inverse().xform(closest_pos); canvas_item_editor->update(); return true; } } } else { if (edited_point!=-1) { //apply ERR_FAIL_INDEX_V(edited_point,poly.size(),false); poly[edited_point]=edited_point_pos; undo_redo->create_action("Edit Poly"); undo_redo->add_do_method(node,"set_polygon",poly); undo_redo->add_undo_method(node,"set_polygon",pre_move_edit); undo_redo->add_do_method(canvas_item_editor->get_viewport_control(),"update"); undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(),"update"); undo_redo->commit_action(); edited_point=-1; return true; } } } if (mb.button_index==BUTTON_RIGHT && mb.pressed && edited_point==-1) { int closest_idx=-1; Vector2 closest_pos; real_t closest_dist=1e10; for(int i=0;i<poly.size();i++) { Vector2 cp =xform.xform(poly[i]); real_t d = cp.distance_to(gpoint); if (d<closest_dist && d<grab_treshold) { closest_dist=d; closest_pos=cp; closest_idx=i; } } if (closest_idx>=0) { undo_redo->create_action("Edit Poly (Remove Point)"); undo_redo->add_undo_method(node,"set_polygon",poly); poly.remove(closest_idx); undo_redo->add_do_method(node,"set_polygon",poly); undo_redo->add_do_method(canvas_item_editor->get_viewport_control(),"update"); undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(),"update"); undo_redo->commit_action(); return true; } } } break; } #endif } break; case InputEvent::MOUSE_MOTION: { const InputEventMouseMotion &mm=p_event.mouse_motion; if ( action!=ACTION_NONE) { Matrix32 xform = canvas_item_editor->get_canvas_transform() * node->get_global_transform(); Vector2 gpoint = Point2(mm.x,mm.y); Vector2 cpoint = !mm.mod.alt? snap_point(xform.affine_inverse().xform(gpoint)) : node->get_global_transform().affine_inverse().xform( snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(gpoint)) ); Ref<Curve2D> curve = node->get_curve(); Vector2 new_pos = moving_from + xform.basis_xform( gpoint - moving_screen_from ); switch(action) { case ACTION_MOVING_POINT: { curve->set_point_pos(action_point,cpoint); } break; case ACTION_MOVING_IN: { curve->set_point_in(action_point,new_pos); } break; case ACTION_MOVING_OUT: { curve->set_point_out(action_point,new_pos); } break; } canvas_item_editor->get_viewport_control()->update(); return true; } #if 0 if (edited_point!=-1 && (wip_active || mm.button_mask&BUTTON_MASK_LEFT)) { Matrix32 xform = canvas_item_editor->get_canvas_transform() * node->get_global_transform(); Vector2 gpoint = Point2(mm.x,mm.y); edited_point_pos = snap_point(xform.affine_inverse().xform(gpoint)); canvas_item_editor->update(); } #endif } break; } return false; }
void Polygon2DEditor::_uv_input(const InputEvent& p_input) { Matrix32 mtx; mtx.elements[2]=-uv_draw_ofs; mtx.scale_basis(Vector2(uv_draw_zoom,uv_draw_zoom)); if (p_input.type==InputEvent::MOUSE_BUTTON) { const InputEventMouseButton &mb=p_input.mouse_button; if (mb.button_index==BUTTON_LEFT) { if (mb.pressed) { uv_drag_from=Vector2(mb.x,mb.y); uv_drag=true; uv_prev=node->get_uv(); uv_move_current=uv_mode; if (uv_move_current==UV_MODE_EDIT_POINT) { if (mb.mod.shift && mb.mod.command) uv_move_current=UV_MODE_SCALE; else if (mb.mod.shift) uv_move_current=UV_MODE_MOVE; else if (mb.mod.command) uv_move_current=UV_MODE_ROTATE; } if (uv_move_current==UV_MODE_EDIT_POINT) { uv_drag_index=-1; for(int i=0;i<uv_prev.size();i++) { Vector2 tuv=mtx.xform(uv_prev[i]); if (tuv.distance_to(Vector2(mb.x,mb.y))<8) { uv_drag_from=tuv; uv_drag_index=i; } } if (uv_drag_index==-1) { uv_drag=false; } } } else if (uv_drag) { undo_redo->create_action("Transform UV Map"); undo_redo->add_do_method(node,"set_uv",node->get_uv()); undo_redo->add_undo_method(node,"set_uv",uv_prev); undo_redo->add_do_method(uv_edit_draw,"update"); undo_redo->add_undo_method(uv_edit_draw,"update"); undo_redo->commit_action(); uv_drag=false; } } else if (mb.button_index==BUTTON_RIGHT && mb.pressed) { if (uv_drag) { uv_drag=false; node->set_uv(uv_prev); uv_edit_draw->update(); } } else if (mb.button_index==BUTTON_WHEEL_UP && mb.pressed) { uv_zoom->set_val( uv_zoom->get_val()/0.9 ); } else if (mb.button_index==BUTTON_WHEEL_DOWN && mb.pressed) { uv_zoom->set_val( uv_zoom->get_val()*0.9); } } else if (p_input.type==InputEvent::MOUSE_MOTION) { const InputEventMouseMotion &mm=p_input.mouse_motion; if (mm.button_mask&BUTTON_MASK_MIDDLE || Input::get_singleton()->is_key_pressed(KEY_SPACE)) { Vector2 drag(mm.relative_x,mm.relative_y); uv_hscroll->set_val( uv_hscroll->get_val()-drag.x ); uv_vscroll->set_val( uv_vscroll->get_val()-drag.y ); } else if (uv_drag) { Vector2 uv_drag_to=snap_point(Vector2(mm.x,mm.y)); Vector2 drag = mtx.affine_inverse().xform(uv_drag_to) - mtx.affine_inverse().xform(uv_drag_from); switch(uv_move_current) { case UV_MODE_EDIT_POINT: { DVector<Vector2> uv_new=uv_prev; uv_new.set( uv_drag_index, uv_new[uv_drag_index]+drag ); node->set_uv(uv_new); } break; case UV_MODE_MOVE: { DVector<Vector2> uv_new=uv_prev; for(int i=0;i<uv_new.size();i++) uv_new.set( i, uv_new[i]+drag ); node->set_uv(uv_new); } break; case UV_MODE_ROTATE: { Vector2 center; DVector<Vector2> uv_new=uv_prev; for(int i=0;i<uv_new.size();i++) center+=uv_prev[i]; center/=uv_new.size(); float angle = (uv_drag_from - mtx.xform(center)).normalized().angle_to( (uv_drag_to - mtx.xform(center)).normalized() ); for(int i=0;i<uv_new.size();i++) { Vector2 rel = uv_prev[i] - center; rel=rel.rotated(angle); uv_new.set(i,center+rel); } node->set_uv(uv_new); } break; case UV_MODE_SCALE: { Vector2 center; DVector<Vector2> uv_new=uv_prev; for(int i=0;i<uv_new.size();i++) center+=uv_prev[i]; center/=uv_new.size(); float from_dist = uv_drag_from.distance_to(mtx.xform(center)); float to_dist = uv_drag_to.distance_to(mtx.xform(center)); if (from_dist<2) break; float scale = to_dist/from_dist; for(int i=0;i<uv_new.size();i++) { Vector2 rel = uv_prev[i] - center; rel=rel*scale; uv_new.set(i,center+rel); } node->set_uv(uv_new); } break; } uv_edit_draw->update(); } } }
bool CollisionPolygonEditor::forward_spatial_input_event(Camera* p_camera,const InputEvent& p_event) { if (!node) return false; Transform gt = node->get_global_transform(); float depth = node->get_depth()*0.5; Vector3 n = gt.basis.get_axis(2).normalized(); Plane p(gt.origin+n*depth,n); switch(p_event.type) { case InputEvent::MOUSE_BUTTON: { const InputEventMouseButton &mb=p_event.mouse_button; Vector2 gpoint=Point2(mb.x,mb.y); Vector3 ray_from = p_camera->project_ray_origin(gpoint); Vector3 ray_dir = p_camera->project_ray_normal(gpoint); Vector3 spoint; if (!p.intersects_ray(ray_from,ray_dir,&spoint)) break; Vector2 cpoint(spoint.x,spoint.y); cpoint=snap_point(cpoint); Vector<Vector2> poly = node->get_polygon(); //first check if a point is to be added (segment split) real_t grab_treshold=EDITOR_DEF("poly_editor/point_grab_radius",8); switch(mode) { case MODE_CREATE: { if (mb.button_index==BUTTON_LEFT && mb.pressed) { if (!wip_active) { wip.clear(); wip.push_back( cpoint ); wip_active=true; edited_point_pos=cpoint; _polygon_draw(); edited_point=1; return true; } else { if (wip.size()>1 && p_camera->unproject_position(gt.xform(Vector3(wip[0].x,wip[0].y,depth))).distance_to(gpoint)<grab_treshold) { //wip closed _wip_close(); return true; } else { wip.push_back( cpoint ); edited_point=wip.size(); _polygon_draw(); return true; //add wip point } } } else if (mb.button_index==BUTTON_RIGHT && mb.pressed && wip_active) { _wip_close(); } } break; case MODE_EDIT: { if (mb.button_index==BUTTON_LEFT) { if (mb.pressed) { if (mb.mod.control) { if (poly.size() < 3) { undo_redo->create_action("Edit Poly"); undo_redo->add_undo_method(node,"set_polygon",poly); poly.push_back(cpoint); undo_redo->add_do_method(node,"set_polygon",poly); undo_redo->add_do_method(this,"_polygon_draw"); undo_redo->add_undo_method(this,"_polygon_draw"); undo_redo->commit_action(); return true; } //search edges int closest_idx=-1; Vector2 closest_pos; real_t closest_dist=1e10; for(int i=0;i<poly.size();i++) { Vector2 points[2] ={ p_camera->unproject_position(gt.xform(Vector3(poly[i].x,poly[i].y,depth))), p_camera->unproject_position(gt.xform(Vector3(poly[(i+1)%poly.size()].x,poly[(i+1)%poly.size()].y,depth))) }; Vector2 cp = Geometry::get_closest_point_to_segment_2d(gpoint,points); if (cp.distance_squared_to(points[0])<CMP_EPSILON2 || cp.distance_squared_to(points[1])<CMP_EPSILON2) continue; //not valid to reuse point real_t d = cp.distance_to(gpoint); if (d<closest_dist && d<grab_treshold) { closest_dist=d; closest_pos=cp; closest_idx=i; } } if (closest_idx>=0) { pre_move_edit=poly; poly.insert(closest_idx+1,cpoint); edited_point=closest_idx+1; edited_point_pos=cpoint; node->set_polygon(poly); _polygon_draw(); return true; } } else { //look for points to move int closest_idx=-1; Vector2 closest_pos; real_t closest_dist=1e10; for(int i=0;i<poly.size();i++) { Vector2 cp = p_camera->unproject_position(gt.xform(Vector3(poly[i].x,poly[i].y,depth))); real_t d = cp.distance_to(gpoint); if (d<closest_dist && d<grab_treshold) { closest_dist=d; closest_pos=cp; closest_idx=i; } } if (closest_idx>=0) { pre_move_edit=poly; edited_point=closest_idx; edited_point_pos=poly[closest_idx]; _polygon_draw(); return true; } } } else { if (edited_point!=-1) { //apply ERR_FAIL_INDEX_V(edited_point,poly.size(),false); poly[edited_point]=edited_point_pos; undo_redo->create_action("Edit Poly"); undo_redo->add_do_method(node,"set_polygon",poly); undo_redo->add_undo_method(node,"set_polygon",pre_move_edit); undo_redo->add_do_method(this,"_polygon_draw"); undo_redo->add_undo_method(this,"_polygon_draw"); undo_redo->commit_action(); edited_point=-1; return true; } } } if (mb.button_index==BUTTON_RIGHT && mb.pressed && edited_point==-1) { int closest_idx=-1; Vector2 closest_pos; real_t closest_dist=1e10; for(int i=0;i<poly.size();i++) { Vector2 cp = p_camera->unproject_position(gt.xform(Vector3(poly[i].x,poly[i].y,depth))); real_t d = cp.distance_to(gpoint); if (d<closest_dist && d<grab_treshold) { closest_dist=d; closest_pos=cp; closest_idx=i; } } if (closest_idx>=0) { undo_redo->create_action("Edit Poly (Remove Point)"); undo_redo->add_undo_method(node,"set_polygon",poly); poly.remove(closest_idx); undo_redo->add_do_method(node,"set_polygon",poly); undo_redo->add_do_method(this,"_polygon_draw"); undo_redo->add_undo_method(this,"_polygon_draw"); undo_redo->commit_action(); return true; } } } break; } } break; case InputEvent::MOUSE_MOTION: { const InputEventMouseMotion &mm=p_event.mouse_motion; if (edited_point!=-1 && (wip_active || mm.button_mask&BUTTON_MASK_LEFT)) { Vector2 gpoint = Point2(mm.x,mm.y); Vector3 ray_from = p_camera->project_ray_origin(gpoint); Vector3 ray_dir = p_camera->project_ray_normal(gpoint); Vector3 spoint; if (!p.intersects_ray(ray_from,ray_dir,&spoint)) break; Vector2 cpoint(spoint.x,spoint.y); cpoint=snap_point(cpoint); edited_point_pos = cpoint; _polygon_draw(); } } break; } return false; }
void spc_move_to(snap_path_closure_t& spc, double x, double y) { spc_new_path_perhaps(spc); snap_point(spc, x, y); move_to(spc.cr, POINT{ x, y }); }
void TextureRegionEditor::_region_input(const Ref<InputEvent> &p_input) { Transform2D mtx; mtx.elements[2] = -draw_ofs; mtx.scale_basis(Vector2(draw_zoom, draw_zoom)); Vector2 endpoints[8] = { mtx.xform(rect.position) + Vector2(-4, -4), mtx.xform(rect.position + Vector2(rect.size.x / 2, 0)) + Vector2(0, -4), mtx.xform(rect.position + Vector2(rect.size.x, 0)) + Vector2(4, -4), mtx.xform(rect.position + Vector2(rect.size.x, rect.size.y / 2)) + Vector2(4, 0), mtx.xform(rect.position + rect.size) + Vector2(4, 4), mtx.xform(rect.position + Vector2(rect.size.x / 2, rect.size.y)) + Vector2(0, 4), mtx.xform(rect.position + Vector2(0, rect.size.y)) + Vector2(-4, 4), mtx.xform(rect.position + Vector2(0, rect.size.y / 2)) + Vector2(-4, 0) }; Ref<InputEventMouseButton> mb = p_input; if (mb.is_valid()) { if (mb->get_button_index() == BUTTON_LEFT) { if (mb->is_pressed()) { if (node_patch9 || obj_styleBox.is_valid()) { edited_margin = -1; float margins[4]; if (node_patch9) { margins[0] = node_patch9->get_patch_margin(MARGIN_TOP); margins[1] = node_patch9->get_patch_margin(MARGIN_BOTTOM); margins[2] = node_patch9->get_patch_margin(MARGIN_LEFT); margins[3] = node_patch9->get_patch_margin(MARGIN_RIGHT); } else if (obj_styleBox.is_valid()) { margins[0] = obj_styleBox->get_margin_size(MARGIN_TOP); margins[1] = obj_styleBox->get_margin_size(MARGIN_BOTTOM); margins[2] = obj_styleBox->get_margin_size(MARGIN_LEFT); margins[3] = obj_styleBox->get_margin_size(MARGIN_RIGHT); } Vector2 pos[4] = { mtx.basis_xform(rect.position + Vector2(0, margins[0])) - draw_ofs, mtx.basis_xform(rect.position + rect.size - Vector2(0, margins[1])) - draw_ofs, mtx.basis_xform(rect.position + Vector2(margins[2], 0)) - draw_ofs, mtx.basis_xform(rect.position + rect.size - Vector2(margins[3], 0)) - draw_ofs }; if (Math::abs(mb->get_position().y - pos[0].y) < 8) { edited_margin = 0; prev_margin = margins[0]; } else if (Math::abs(mb->get_position().y - pos[1].y) < 8) { edited_margin = 1; prev_margin = margins[1]; } else if (Math::abs(mb->get_position().x - pos[2].x) < 8) { edited_margin = 2; prev_margin = margins[2]; } else if (Math::abs(mb->get_position().x - pos[3].x) < 8) { edited_margin = 3; prev_margin = margins[3]; } if (edited_margin >= 0) { drag_from = Vector2(mb->get_position().x, mb->get_position().y); drag = true; } } if (edited_margin < 0 && snap_mode == SNAP_AUTOSLICE) { Vector2 point = mtx.affine_inverse().xform(Vector2(mb->get_position().x, mb->get_position().y)); for (List<Rect2>::Element *E = autoslice_cache.front(); E; E = E->next()) { if (E->get().has_point(point)) { rect = E->get(); if (Input::get_singleton()->is_key_pressed(KEY_CONTROL) && !(Input::get_singleton()->is_key_pressed(KEY_SHIFT | KEY_ALT))) { Rect2 r; if (node_sprite) r = node_sprite->get_region_rect(); else if (node_patch9) r = node_patch9->get_region_rect(); else if (obj_styleBox.is_valid()) r = obj_styleBox->get_region_rect(); else if (atlas_tex.is_valid()) r = atlas_tex->get_region(); rect.expand_to(r.position); rect.expand_to(r.position + r.size); } undo_redo->create_action(TTR("Set Region Rect")); if (node_sprite) { undo_redo->add_do_method(node_sprite, "set_region_rect", rect); undo_redo->add_undo_method(node_sprite, "set_region_rect", node_sprite->get_region_rect()); } else if (node_patch9) { undo_redo->add_do_method(node_patch9, "set_region_rect", rect); undo_redo->add_undo_method(node_patch9, "set_region_rect", node_patch9->get_region_rect()); } else if (obj_styleBox.is_valid()) { undo_redo->add_do_method(obj_styleBox.ptr(), "set_region_rect", rect); undo_redo->add_undo_method(obj_styleBox.ptr(), "set_region_rect", obj_styleBox->get_region_rect()); } else if (atlas_tex.is_valid()) { undo_redo->add_do_method(atlas_tex.ptr(), "set_region", rect); undo_redo->add_undo_method(atlas_tex.ptr(), "set_region", atlas_tex->get_region()); } undo_redo->add_do_method(edit_draw, "update"); undo_redo->add_undo_method(edit_draw, "update"); undo_redo->commit_action(); break; } } } else if (edited_margin < 0) { drag_from = mtx.affine_inverse().xform(Vector2(mb->get_position().x, mb->get_position().y)); if (snap_mode == SNAP_PIXEL) drag_from = drag_from.snapped(Vector2(1, 1)); else if (snap_mode == SNAP_GRID) drag_from = snap_point(drag_from); drag = true; if (node_sprite) rect_prev = node_sprite->get_region_rect(); else if (node_patch9) rect_prev = node_patch9->get_region_rect(); else if (obj_styleBox.is_valid()) rect_prev = obj_styleBox->get_region_rect(); else if (atlas_tex.is_valid()) rect_prev = atlas_tex->get_region(); for (int i = 0; i < 8; i++) { Vector2 tuv = endpoints[i]; if (tuv.distance_to(Vector2(mb->get_position().x, mb->get_position().y)) < 8) { drag_index = i; } } if (drag_index == -1) { creating = true; rect = Rect2(drag_from, Size2()); } } } else if (drag) { if (edited_margin >= 0) { undo_redo->create_action("Set Margin"); static Margin m[4] = { MARGIN_TOP, MARGIN_BOTTOM, MARGIN_LEFT, MARGIN_RIGHT }; if (node_patch9) { undo_redo->add_do_method(node_patch9, "set_patch_margin", m[edited_margin], node_patch9->get_patch_margin(m[edited_margin])); undo_redo->add_undo_method(node_patch9, "set_patch_margin", m[edited_margin], prev_margin); } else if (obj_styleBox.is_valid()) { undo_redo->add_do_method(obj_styleBox.ptr(), "set_margin_size", m[edited_margin], obj_styleBox->get_margin_size(m[edited_margin])); undo_redo->add_undo_method(obj_styleBox.ptr(), "set_margin_size", m[edited_margin], prev_margin); obj_styleBox->emit_signal(CoreStringNames::get_singleton()->changed); } edited_margin = -1; } else { undo_redo->create_action("Set Region Rect"); if (node_sprite) { undo_redo->add_do_method(node_sprite, "set_region_rect", node_sprite->get_region_rect()); undo_redo->add_undo_method(node_sprite, "set_region_rect", rect_prev); } else if (atlas_tex.is_valid()) { undo_redo->add_do_method(atlas_tex.ptr(), "set_region", atlas_tex->get_region()); undo_redo->add_undo_method(atlas_tex.ptr(), "set_region", rect_prev); } else if (node_patch9) { // FIXME: Is this intentional? } else if (node_patch9) { undo_redo->add_do_method(node_patch9, "set_region_rect", node_patch9->get_region_rect()); undo_redo->add_undo_method(node_patch9, "set_region_rect", rect_prev); } else if (obj_styleBox.is_valid()) { undo_redo->add_do_method(obj_styleBox.ptr(), "set_region_rect", obj_styleBox->get_region_rect()); undo_redo->add_undo_method(obj_styleBox.ptr(), "set_region_rect", rect_prev); } drag_index = -1; } undo_redo->add_do_method(edit_draw, "update"); undo_redo->add_undo_method(edit_draw, "update"); undo_redo->commit_action(); drag = false; creating = false; } } else if (mb->get_button_index() == BUTTON_RIGHT && mb->is_pressed()) { if (drag) { drag = false; if (edited_margin >= 0) { static Margin m[4] = { MARGIN_TOP, MARGIN_BOTTOM, MARGIN_LEFT, MARGIN_RIGHT }; if (node_patch9) node_patch9->set_patch_margin(m[edited_margin], prev_margin); if (obj_styleBox.is_valid()) obj_styleBox->set_margin_size(m[edited_margin], prev_margin); edited_margin = -1; } else { apply_rect(rect_prev); rect = rect_prev; edit_draw->update(); drag_index = -1; } } } else if (mb->get_button_index() == BUTTON_WHEEL_UP && mb->is_pressed()) { _zoom_in(); } else if (mb->get_button_index() == BUTTON_WHEEL_DOWN && mb->is_pressed()) { _zoom_out(); } } Ref<InputEventMouseMotion> mm = p_input; if (mm.is_valid()) { if (mm->get_button_mask() & BUTTON_MASK_MIDDLE || Input::get_singleton()->is_key_pressed(KEY_SPACE)) { Vector2 draged(mm->get_relative().x, mm->get_relative().y); hscroll->set_value(hscroll->get_value() - draged.x); vscroll->set_value(vscroll->get_value() - draged.y); } else if (drag) { if (edited_margin >= 0) { float new_margin; if (edited_margin == 0) new_margin = prev_margin + (mm->get_position().y - drag_from.y) / draw_zoom; else if (edited_margin == 1) new_margin = prev_margin - (mm->get_position().y - drag_from.y) / draw_zoom; else if (edited_margin == 2) new_margin = prev_margin + (mm->get_position().x - drag_from.x) / draw_zoom; else if (edited_margin == 3) new_margin = prev_margin - (mm->get_position().x - drag_from.x) / draw_zoom; if (new_margin < 0) new_margin = 0; static Margin m[4] = { MARGIN_TOP, MARGIN_BOTTOM, MARGIN_LEFT, MARGIN_RIGHT }; if (node_patch9) node_patch9->set_patch_margin(m[edited_margin], new_margin); if (obj_styleBox.is_valid()) obj_styleBox->set_margin_size(m[edited_margin], new_margin); } else { Vector2 new_pos = mtx.affine_inverse().xform(mm->get_position()); if (snap_mode == SNAP_PIXEL) new_pos = new_pos.snapped(Vector2(1, 1)); else if (snap_mode == SNAP_GRID) new_pos = snap_point(new_pos); if (creating) { rect = Rect2(drag_from, Size2()); rect.expand_to(new_pos); apply_rect(rect); edit_draw->update(); return; } switch (drag_index) { case 0: { Vector2 p = rect_prev.position + rect_prev.size; rect = Rect2(p, Size2()); rect.expand_to(new_pos); apply_rect(rect); } break; case 1: { Vector2 p = rect_prev.position + Vector2(0, rect_prev.size.y); rect = Rect2(p, Size2(rect_prev.size.x, 0)); rect.expand_to(new_pos); apply_rect(rect); } break; case 2: { Vector2 p = rect_prev.position + Vector2(0, rect_prev.size.y); rect = Rect2(p, Size2()); rect.expand_to(new_pos); apply_rect(rect); } break; case 3: { Vector2 p = rect_prev.position; rect = Rect2(p, Size2(0, rect_prev.size.y)); rect.expand_to(new_pos); apply_rect(rect); } break; case 4: { Vector2 p = rect_prev.position; rect = Rect2(p, Size2()); rect.expand_to(new_pos); apply_rect(rect); } break; case 5: { Vector2 p = rect_prev.position; rect = Rect2(p, Size2(rect_prev.size.x, 0)); rect.expand_to(new_pos); apply_rect(rect); } break; case 6: { Vector2 p = rect_prev.position + Vector2(rect_prev.size.x, 0); rect = Rect2(p, Size2()); rect.expand_to(new_pos); apply_rect(rect); } break; case 7: { Vector2 p = rect_prev.position + Vector2(rect_prev.size.x, 0); rect = Rect2(p, Size2(0, rect_prev.size.y)); rect.expand_to(new_pos); apply_rect(rect); } break; } } edit_draw->update(); } } }