forked from HEL/chronos
		
	added basis for lifelines
This commit is contained in:
		
							
								
								
									
										134
									
								
								src/renderer.typ
									
									
									
									
									
								
							
							
						
						
									
										134
									
								
								src/renderer.typ
									
									
									
									
									
								
							| @@ -5,9 +5,15 @@ | ||||
| #let PAR-PAD = (5pt, 3pt) | ||||
| #let PAR-SPACE = 10 | ||||
| #let COMMENT-PAD = 8 | ||||
| #let LIFELINE-W = 10 | ||||
|  | ||||
|  | ||||
| #let get-columns-width(participants, elements) = { | ||||
|   participants = participants.map(p => { | ||||
|     p.insert("lifeline-lvl", 0) | ||||
|     p.insert("max-lifelines", 0) | ||||
|     p | ||||
|   }) | ||||
|   let pars-i = get-participants-i(participants) | ||||
|   let cells = () | ||||
|   for elmt in elements { | ||||
| @@ -23,6 +29,23 @@ | ||||
|           cell: box(com, inset: 3pt) | ||||
|         ) | ||||
|       ) | ||||
|  | ||||
|       if elmt.disable-src or elmt.destroy-src { | ||||
|         let p = participants.at(i1) | ||||
|         p.lifeline-lvl -= 1 | ||||
|         participants.at(i1) = p | ||||
|       } | ||||
|       if elmt.disable-dst { | ||||
|         let p = participants.at(i2) | ||||
|         p.lifeline-lvl -= 1 | ||||
|         participants.at(i2) = p | ||||
|       } | ||||
|       if elmt.enable-dst { | ||||
|         let p = participants.at(i2) | ||||
|         p.lifeline-lvl += 1 | ||||
|         p.max-lifelines = calc.max(p.max-lifelines, p.lifeline-lvl) | ||||
|         participants.at(i2) = p | ||||
|       } | ||||
|     } | ||||
|   } | ||||
|  | ||||
| @@ -54,6 +77,15 @@ | ||||
|       m.width / 1pt - widths.slice(0, cell.i2 - 1).sum() | ||||
|     ) | ||||
|   } | ||||
|   for (i, w) in widths.enumerate() { | ||||
|     let p1 = participants.at(i) | ||||
|     let p2 = participants.at(i + 1) | ||||
|     let w = w + p1.max-lifelines * LIFELINE-W / 2 | ||||
|     if p2.max-lifelines != 0 { | ||||
|       w += LIFELINE-W / 2 | ||||
|     } | ||||
|     widths.at(i) = w | ||||
|   } | ||||
|   return widths | ||||
| } | ||||
|  | ||||
| @@ -119,12 +151,68 @@ | ||||
|  | ||||
|   let y = -Y-SPACE | ||||
|   let groups = () | ||||
|   let lifelines = participants.map(_ => ( | ||||
|     level: 0, | ||||
|     lines: () | ||||
|   )) | ||||
|  | ||||
|   // Draw sequences | ||||
|   for elmt in elements { | ||||
|     if elmt.type == "seq" { | ||||
|       let x1 = x-pos.at(pars-i.at(elmt.p1)) | ||||
|       let x2 = x-pos.at(pars-i.at(elmt.p2)) | ||||
|       let i1 = pars-i.at(elmt.p1) | ||||
|       let i2 = pars-i.at(elmt.p2) | ||||
|  | ||||
|       if elmt.comment != none { | ||||
|         y -= measure(box(elmt.comment)).height / 1pt + 6 | ||||
|       } | ||||
|  | ||||
|       if elmt.disable-src { | ||||
|         let src-line = lifelines.at(i1) | ||||
|         src-line.level -= 1 | ||||
|         src-line.lines.push(("disable", y, auto)) | ||||
|         lifelines.at(i1) = src-line | ||||
|       } | ||||
|       if elmt.destroy-src { | ||||
|         let src-line = lifelines.at(i1) | ||||
|         src-line.level -= 1 | ||||
|         src-line.lines.push(("destroy", y, auto)) | ||||
|         lifelines.at(i1) = src-line | ||||
|       } | ||||
|  | ||||
|       let ll-lvl1 = lifelines.at(i1).level * LIFELINE-W / 2 | ||||
|  | ||||
|       if elmt.disable-dst { | ||||
|         let dst-line = lifelines.at(i2) | ||||
|         dst-line.level -= 1 | ||||
|         dst-line.lines.push(("disable", y, auto)) | ||||
|         lifelines.at(i2) = dst-line | ||||
|       } | ||||
|       if elmt.destroy-dst { | ||||
|         let dst-line = lifelines.at(i2) | ||||
|         dst-line.level -= 1 | ||||
|         dst-line.lines.push(("destroy", y, auto)) | ||||
|         lifelines.at(i2) = dst-line | ||||
|       } | ||||
|       if elmt.enable-dst { | ||||
|         let dst-line = lifelines.at(i2) | ||||
|         dst-line.level += 1 | ||||
|         lifelines.at(i2) = dst-line | ||||
|       } | ||||
|  | ||||
|       let x1 = x-pos.at(i1) | ||||
|       let x2 = x-pos.at(i2) | ||||
|  | ||||
|       let ll-lvl2 = lifelines.at(i2).level * LIFELINE-W / 2 | ||||
|  | ||||
|       let f = if elmt.flip {-1} else {1} | ||||
|       if i1 <= i2 { | ||||
|         x1 += ll-lvl1 * f | ||||
|         x2 -= ll-lvl2 * f | ||||
|       } else { | ||||
|         x1 -= ll-lvl1 * f | ||||
|         x2 += ll-lvl2 * f | ||||
|       } | ||||
|  | ||||
|       let style = ( | ||||
|         mark: (end: "straight"), | ||||
|         stroke: ( | ||||
| @@ -134,10 +222,11 @@ | ||||
|       ) | ||||
|  | ||||
|       if elmt.p1 == elmt.p2 { | ||||
|         let x3 = x1 - ll-lvl1 + ll-lvl2 | ||||
|  | ||||
|         x2 = if elmt.flip {x1 - 20} else {x1 + 20} | ||||
|  | ||||
|         if elmt.comment != none { | ||||
|           y -= measure(box(elmt.comment)).height / 1pt + 6 | ||||
|           draw.content( | ||||
|             (x1, y), | ||||
|             elmt.comment, | ||||
| @@ -150,7 +239,7 @@ | ||||
|           (x1, y), | ||||
|           (x2, y), | ||||
|           (x2, y - 10), | ||||
|           (x1, y - 10), | ||||
|           (x3, y - 10), | ||||
|           ..style | ||||
|         ) | ||||
|         y -= 10 | ||||
| @@ -161,7 +250,6 @@ | ||||
|           if x2 < x1 { | ||||
|             x += COMMENT-PAD | ||||
|           } | ||||
|           y -= measure(box(elmt.comment)).height / 1pt + 6 | ||||
|           draw.content( | ||||
|             (x, y), | ||||
|             elmt.comment, | ||||
| @@ -176,6 +264,11 @@ | ||||
|           ..style | ||||
|         ) | ||||
|       } | ||||
|       if elmt.enable-dst { | ||||
|         let dst-line = lifelines.at(i2) | ||||
|         dst-line.lines.push(("enable", y, auto)) | ||||
|         lifelines.at(i2) = dst-line | ||||
|       } | ||||
|       y -= Y-SPACE | ||||
|  | ||||
|     } else if elmt.type == "grp" { | ||||
| @@ -244,6 +337,37 @@ | ||||
|         (x, y), | ||||
|         stroke: (dash: "dashed", paint: gray.darken(40%)) | ||||
|       ) | ||||
|  | ||||
|       let rects = () | ||||
|       let destructions = () | ||||
|       let lines = () | ||||
|       for line in lifelines.at(i).lines { | ||||
|         let event = line.first() | ||||
|         if event == "enable" { | ||||
|           lines.push(line) | ||||
|         } else if event == "disable" or event == "destroy" { | ||||
|           let l = lines.pop() | ||||
|           let lvl = lines.len() | ||||
|           rects.push((x + lvl * LIFELINE-W / 2, l.at(1), line.at(1))) | ||||
|           if event == "destroy" { | ||||
|             destructions.push((x + lvl * LIFELINE-W / 2, line.at(1))) | ||||
|           } | ||||
|         } | ||||
|       } | ||||
|  | ||||
|       for rect in rects.rev() { | ||||
|         let (cx, y0, y1) = rect | ||||
|         draw.rect( | ||||
|           (cx - LIFELINE-W / 2, y0), | ||||
|           (cx + LIFELINE-W / 2, y1), | ||||
|         ) | ||||
|       } | ||||
|       for dest in destructions { | ||||
|         let (cx, cy) = dest | ||||
|         draw.line((cx - 8, cy - 8), (cx + 8, cy + 8), stroke: red + 2pt) | ||||
|         draw.line((cx - 8, cy + 8), (cx + 8, cy - 8), stroke: red + 2pt) | ||||
|       } | ||||
|  | ||||
|       draw.content( | ||||
|         (x, y), | ||||
|         p.display-name, | ||||
|   | ||||
		Reference in New Issue
	
	Block a user