Improved arrow handling, fixed issue 463

git-svn-id: http://svg-edit.googlecode.com/svn/trunk@1341 eee81c28-f429-11dd-99c0-75d572ba1ddd
master
Alexis Deveria 2010-02-04 17:10:03 +00:00
parent e2b264ea64
commit 2229ecdd45
4 changed files with 69 additions and 22 deletions

View File

@ -306,7 +306,7 @@ function svg_edit_setup() {
$(this).mouseup(func); $(this).mouseup(func);
if(opts.key) { if(opts.key) {
$(document).bind('keydown', {combi: opts.key+''}, func); $(document).bind('keydown', {combi: opts.key+'', disableInInput:true}, func);
} }
}); });
@ -485,7 +485,7 @@ function svg_edit_setup() {
if(btn.type == 'mode') { if(btn.type == 'mode') {
button.bind(name, func); button.bind(name, func);
if(btn.key) { if(btn.key) {
$(document).bind('keydown', {combi: btn.key}, func); $(document).bind('keydown', {combi: btn.key, disableInInput: true}, func);
if(btn.title) button.attr("title", btn.title + ' ['+btn.key+']'); if(btn.title) button.attr("title", btn.title + ' ['+btn.key+']');
} }
} }

View File

@ -990,6 +990,7 @@ function BatchCommand(text) {
var ext = ext_func({ var ext = ext_func({
content: svgcontent, content: svgcontent,
root: svgroot, root: svgroot,
call: call,
getNextId: getNextId, getNextId: getNextId,
getElem: getElem, getElem: getElem,
addSvgElementFromJson: addSvgElementFromJson, addSvgElementFromJson: addSvgElementFromJson,

View File

@ -28,8 +28,30 @@ $(function() {
$('#arrow_panel').toggle(on); $('#arrow_panel').toggle(on);
if(on) { if(on) {
var has_arrow = selElems[0].getAttribute("marker-end"); var el = selElems[0];
$("#arrow_list").val(has_arrow?"arrow":"none"); var end = el.getAttribute("marker-end");
var start = el.getAttribute("marker-start");
var mid = el.getAttribute("marker-mid");
var val;
if(end && start) {
val = "both";
} else if(end) {
val = "end";
} else if(start) {
val = "start";
} else if(mid) {
val = "mid";
if(mid.indexOf("bk") != -1) {
val = "mid_bk";
}
}
if(!start && !mid && !end) {
val = "none";
}
$("#arrow_list").val(val);
} }
} }
@ -45,8 +67,8 @@ $(function() {
var marker = getElem(id); var marker = getElem(id);
var pathdata = { var pathdata = {
se_arrow_fw: {d:"m0,0l10,5l-10,5l5,-5l-5,-5z", refx:10}, se_arrow_fw: {d:"m0,0l10,5l-10,5l5,-5l-5,-5z", refx:8},
se_arrow_bk: {d:"m10,0l-10,5l10,5l-5,-5l5,-5z", refx:0} se_arrow_bk: {d:"m10,0l-10,5l10,5l-5,-5l5,-5z", refx:2}
} }
var data = pathdata[id]; var data = pathdata[id];
@ -78,9 +100,8 @@ $(function() {
marker.appendChild(arrow); marker.appendChild(arrow);
S.findDefs().appendChild(marker); S.findDefs().appendChild(marker);
} }
marker.setAttribute('refX', data.refx);
marker.setAttribute('refX', data.refx);
} }
function setArrow(type) { function setArrow(type) {
@ -101,7 +122,7 @@ $(function() {
id = bk_id; id = bk_id;
} else if(type == "both") { } else if(type == "both") {
addMarker(bk_id, type); addMarker(bk_id, type);
selElems[0].setAttribute("marker-start", "url(#" + bk_id + ")"); svgCanvas.changeSelectedAttribute("marker-start", "url(#" + bk_id + ")");
type = "end"; type = "end";
id = fw_id; id = fw_id;
} else if (type == "start") { } else if (type == "start") {
@ -109,11 +130,8 @@ $(function() {
} }
addMarker(id, type); addMarker(id, type);
selElems[0].setAttribute("marker-"+type, "url(#" + id + ")"); svgCanvas.changeSelectedAttribute("marker-"+type, "url(#" + id + ")");
S.call("changed", selElems);
} }
// Init code // Init code

View File

@ -124,7 +124,7 @@ $(function() {
}); });
} }
} }
function updateConnectors() { function updateConnectors() {
// Updates connector lines based on selected elements // Updates connector lines based on selected elements
// Is not used on mousemove, as it runs getStrokedBBox every time, // Is not used on mousemove, as it runs getStrokedBBox every time,
@ -139,13 +139,15 @@ $(function() {
var elem = conn.elem; var elem = conn.elem;
var pre = conn.is_start?'start':'end'; var pre = conn.is_start?'start':'end';
var sw = line.getAttribute('stroke-width');
// Update bbox for this element // Update bbox for this element
var bb = svgCanvas.getStrokedBBox([elem]); var bb = svgCanvas.getStrokedBBox([elem]);
bb.x = conn.start_x; bb.x = conn.start_x;
bb.y = conn.start_y; bb.y = conn.start_y;
$(line).data(pre+'_bb', bb); $(line).data(pre+'_bb', bb);
var add_offset = $(line).data(pre+'_off');
var alt_pre = conn.is_start?'end':'start'; var alt_pre = conn.is_start?'end':'start';
// Get center pt of connected element // Get center pt of connected element
@ -154,17 +156,26 @@ $(function() {
var src_y = bb2.y + bb2.height/2; var src_y = bb2.y + bb2.height/2;
// Set point of element being moved // Set point of element being moved
var pt = getBBintersect(src_x, src_y, bb); var pt = getBBintersect(src_x, src_y, bb, add_offset?sw:0);
setPoint(line, conn.is_start?0:'end', pt.x, pt.y, true); setPoint(line, conn.is_start?0:'end', pt.x, pt.y, true);
// Set point of connected element // Set point of connected element
var pt2 = getBBintersect(pt.x, pt.y, $(line).data(alt_pre + '_bb')); var pt2 = getBBintersect(pt.x, pt.y, $(line).data(alt_pre + '_bb'), $(line).data(alt_pre + '_off')?sw:0);
setPoint(line, conn.is_start?'end':0, pt2.x, pt2.y, true); setPoint(line, conn.is_start?'end':0, pt2.x, pt2.y, true);
} }
} }
} }
function getBBintersect(x, y, bb) { function getBBintersect(x, y, bb, offset) {
if(offset) {
offset -= 0;
bb = $.extend({}, bb);
bb.width += offset;
bb.height += offset;
bb.x -= offset/2;
bb.y -= offset/2;
}
var mid_x = bb.x + bb.width/2; var mid_x = bb.x + bb.width/2;
var mid_y = bb.y + bb.height/2; var mid_y = bb.y + bb.height/2;
var len_x = x - mid_x; var len_x = x - mid_x;
@ -179,6 +190,8 @@ $(function() {
} else { } else {
ratio = (bb.height/2) / Math.abs(len_y); ratio = (bb.height/2) / Math.abs(len_y);
} }
return { return {
x: mid_x + len_x * ratio, x: mid_x + len_x * ratio,
y: mid_y + len_y * ratio y: mid_y + len_y * ratio
@ -333,7 +346,7 @@ $(function() {
while(slen--) { while(slen--) {
var elem = selElems[slen]; var elem = selElems[slen];
// Look for selected connector elements // Look for selected connector elements
if(elem && $(elem).is(conn_sel)) { if(elem && $(elem).data('c_start')) {
// Remove the "translate" transform given to move // Remove the "translate" transform given to move
svgCanvas.removeFromSelection([elem]); svgCanvas.removeFromSelection([elem]);
svgCanvas.getTransformList(elem).clear(); svgCanvas.getTransformList(elem).clear();
@ -349,6 +362,7 @@ $(function() {
var elem = conn.elem; var elem = conn.elem;
var pre = conn.is_start?'start':'end'; var pre = conn.is_start?'start':'end';
var sw = line.getAttribute('stroke-width');
// Update bbox for this element // Update bbox for this element
var bb = $(line).data(pre+'_bb'); var bb = $(line).data(pre+'_bb');
@ -364,11 +378,11 @@ $(function() {
var src_y = bb2.y + bb2.height/2; var src_y = bb2.y + bb2.height/2;
// Set point of element being moved // Set point of element being moved
var pt = getBBintersect(src_x, src_y, bb); var pt = getBBintersect(src_x, src_y, bb, $(line).data(pre+'_off')?sw:0);
setPoint(line, conn.is_start?0:'end', pt.x, pt.y, true); setPoint(line, conn.is_start?0:'end', pt.x, pt.y, true);
// Set point of connected element // Set point of connected element
var pt2 = getBBintersect(pt.x, pt.y, $(line).data(alt_pre + '_bb')); var pt2 = getBBintersect(pt.x, pt.y, $(line).data(alt_pre + '_bb'), $(line).data(alt_pre+'_off')?sw:0);
setPoint(line, conn.is_start?'end':0, pt2.x, pt2.y, true); setPoint(line, conn.is_start?'end':0, pt2.x, pt2.y, true);
} }
@ -473,6 +487,20 @@ $(function() {
init(); init();
} }
// Has marker, so change offset
if(elem && (
elem.getAttribute("marker-start") ||
elem.getAttribute("marker-mid") ||
elem.getAttribute("marker-end")
)) {
var start = elem.getAttribute("marker-start");
var end = elem.getAttribute("marker-end");
console.log('elem',elem);
cur_line = elem;
$(elem)
.data("start_off", !!start)
.data("end_off", !!end);
}
updateConnectors(); updateConnectors();
}, },
// toolButtonStateUpdate: function(opts) { // toolButtonStateUpdate: function(opts) {