public override void OnLeftMouseButtonDown(OverlayShape item, Point p, TEMouseArgs e) { if (!EnsureParseTreeExists()) { return; } Point ptikz = overlay.Rasterizer.RasterizePixelToTikz(p); p = overlay.Rasterizer.RasterizePixel(p); if (ContinueWithBigImage(ptikz) == false) { return; } if (pointcount == 0) { //overlay.SetCorrectRaster(overlay.CurEditing, true); center = ptikz; // set raster to polar, and such that origin is at center overlay.Rasterizer.View.IsCartesian = false; TikzMatrix M = overlay.Rasterizer.View.CoordinateTransform; M.m[0, 2] = center.X; M.m[1, 2] = center.Y; overlay.Rasterizer.View.CoordinateTransform = M; pointcount = 1; PreviewPie.center = PreviewArc.center = new Point(p.X, overlay.Height - p.Y); ////if (!overlay.canvas.Children.Contains(PreviewArc)) //// overlay.canvas.Children.Add(PreviewArc); ////if (!overlay.canvas.Children.Contains(PreviewPie)) //// overlay.canvas.Children.Add(PreviewPie); } else if (pointcount == 1) { p1 = ptikz; pointcount = 2; PreviewPie.p1 = PreviewArc.p2 = new Point(p.X, overlay.Height - p.Y); // compute radius TikzMatrix M = overlay.Rasterizer.View.CoordinateTransform; Point ploc = M.Inverse().Transform(p1); overlay.Rasterizer.View.ForceRadiusTo = (ploc - (new Point(0, 0))).Length; } else if (pointcount == 2) { // **** create arc / pie **** overlay.BeginUpdate(); // find next tikzpicture and add if (AddNewCurAddTo()) //(EnsureCurAddToExists(out lcreated)) { // Pie? if (IsPie) { // create new coordinate Tikz_Coord tc = new Parser.Tikz_Coord(); tc.type = overlay.UsePolarCoordinates ? Parser.Tikz_CoordType.Polar : Parser.Tikz_CoordType.Cartesian; curAddTo.AddChild(tc); tc.SetAbsPos(center); curAddTo.AddChild(new Parser.Tikz_Something(" -- ")); } Tikz_Coord tc1 = new Parser.Tikz_Coord(); if (IsPie) { tc1.type = Tikz_CoordType.Polar; tc1.deco = "++"; } curAddTo.AddChild(tc1); tc1.SetAbsPos(p1); curAddTo.AddChild(new Parser.Tikz_Something(" ")); // create arc Tikz_Arc ta = new Tikz_Arc(); curAddTo.AddChild(ta); ta.SetFromPoints(center, ptikz, IsLargeArc); if (IsPie) { curAddTo.AddChild(new Parser.Tikz_Something(" -- cycle")); } //tn.UpdateText(); curAddTo.UpdateText(); //tpict.UpdateText(); // draw the added object in the overlay overlay.AddToDisplayTree(curAddTo); } overlay.EndUpdate(); // reset everything pointcount = 0; overlay.Rasterizer.View.ForceRadiusTo = -1; overlay.SetCorrectRaster(overlay.CurEditing, true); } //Point p = new Point(e.GetPosition(canvas1).X, Height - e.GetPosition(canvas1).Y); UpdatePreviewDisplay(p); }
bool FillNodesOnArc() { TikzContainerParseItem pa = curDragged.item.parent; if (!(curDragged.item is Tikz_XYItem)) { return(false); } Tikz_XYItem cur = curDragged.item as Tikz_XYItem; nodesOnArc.Clear(); nodesOnArc.Add(cur); // find all arcs to the right for (int i = pa.Children.IndexOf(cur) + 1; i < pa.Children.Count; i++) { if (pa.Children[i] is Tikz_Arc) { nodesOnArc.Add(pa.Children[i] as Tikz_Arc); } else if (pa.Children[i].ChangesCurPoint()) { break; } } // find all arcs to left, including startpoint if (cur is Tikz_Arc) { for (int i = pa.Children.IndexOf(cur) - 1; i >= 0; i--) { if (pa.Children[i] is Tikz_Arc) { nodesOnArc.Insert(0, pa.Children[i] as Tikz_Arc); } else if (pa.Children[i].ChangesCurPoint()) { if (pa.Children[i] is Tikz_XYItem) { nodesOnArc.Insert(0, pa.Children[i] as Tikz_XYItem); } break; } } } // todo: no startpoint present... if (nodesOnArc.First() is Tikz_Arc) { } // compute radius and center // in absolute tikz coordinates Point center, p; double R; Tikz_Arc ta; if (cur is Tikz_Arc) { ta = cur as Tikz_Arc; } else if (nodesOnArc.Count > 1 && nodesOnArc[1] is Tikz_Arc) { ta = nodesOnArc[1] as Tikz_Arc; } else { return(false); } if (!ta.GetArcCenterAbs(out center) || !ta.GetAbsPos(out p)) { throw new Exception("ArcEditTool: Invalid coordinate discovered."); } R = (p - center).Length; // throw out all items with non-fitting parameters for (int i = nodesOnArc.Count - 1; i >= 0; i--) { if (nodesOnArc[i] is Tikz_Arc) { Point cc, pp; double RR; if (!(nodesOnArc[i] as Tikz_Arc).GetArcCenterAbs(out cc) || !nodesOnArc[i].GetAbsPos(out pp)) { throw new Exception("ArcEditTool: Invalid coordinate discovered."); } RR = (cc - pp).Length; if ((cc - center).Length > 1e-3 || Math.Abs(RR - R) > 1e-3) { nodesOnArc.RemoveAt(i); } } else // other Tikz_XYItem, namely startpoint { Point pp; if (!nodesOnArc[i].GetAbsPos(out pp)) { throw new Exception("ArcEditTool: Invalid coordinate discovered."); } // check if pooint lies on circle if (Math.Abs(R - (pp - center).Length) > 1e-3) { nodesOnArc.RemoveAt(i); } } } curDraggedInd = nodesOnArc.IndexOf(cur); PreviewArc.Center = overlay.TikzToScreen(center, true); center_tikz = center; // Fill the PreviewArcs Spokes array List <double> spokes = new List <double>(); for (int i = 0; i < nodesOnArc.Count; i++) { if (nodesOnArc[i] is Tikz_Arc) { Tikz_Arc tia = nodesOnArc[i] as Tikz_Arc; double a1 = tia.phi1.GetInCM() * Math.PI / 180, a2 = tia.phi2.GetInCM() * Math.PI / 180; //if (!(spokes.Count > 0 && spokes.Last() == a1)) // spokes.Add(a1); spokes.Add(a2); //Point pp; //if (!nodesOnArc[i].GetAbsPos(out pp)) // throw new Exception("ArcEditTool: Invalid coordinate discovered."); //Vector v = pp - center; } else { // should go here at most for the first item in list double a1 = (nodesOnArc[1] as Tikz_Arc).phi1.GetInCM() * Math.PI / 180; spokes.Add(a1); } } PreviewArc.Spokes = spokes; return(true); }
static bool FillItem(TikzContainerParseItem item, CommonTree t, CommonTokenStream tokens) { int curToken = t.TokenStartIndex; if (item is Tikz_ParseTree) { curToken = 0; // for root, start at the beginning } if (t.Children == null) { return(false); } foreach (CommonTree childt in t.Children) { addSomething(item, tokens, curToken, childt.TokenStartIndex - 1); switch (childt.Type) { case simpletikzParser.IM_PICTURE: Tikz_Picture tp = new Tikz_Picture(); FillItem(tp, childt, tokens); item.AddChild(tp); break; case simpletikzParser.IM_STARTTAG: item.starttag = getTokensString(tokens, childt.TokenStartIndex, childt.TokenStopIndex); break; case simpletikzParser.IM_ENDTAG: item.endtag = getTokensString(tokens, childt.TokenStartIndex, childt.TokenStopIndex); break; case simpletikzParser.IM_PATH: Tikz_Path tpath = new Tikz_Path(); FillItem(tpath, childt, tokens); item.AddChild(tpath); break; case simpletikzParser.IM_SCOPE: Tikz_Scope tscope = new Tikz_Scope(); FillItem(tscope, childt, tokens); item.AddChild(tscope); break; case simpletikzParser.IM_COORD: Tikz_Coord tc = Tikz_Coord.FromCommonTree(childt, tokens); tc.text = getTokensString(tokens, childt); item.AddChild(tc); break; case simpletikzParser.IM_ARC: Tikz_Arc ta = Tikz_Arc.FromCommonTree(childt, tokens); ta.text = getTokensString(tokens, childt); item.AddChild(ta); break; case simpletikzParser.IM_NODE: Tikz_Node tn = Tikz_Node.FromCommonTree(childt, tokens); tn.text = getTokensString(tokens, childt); item.AddChild(tn); break; case simpletikzParser.IM_OPTION_KV: case simpletikzParser.IM_OPTION_STYLE: Tikz_Option topt = Tikz_Option.FromCommonTree(childt, tokens); if (topt == null) { break; } //topt.text = getTokensString(tokens, childt); String s = getTokensString(tokens, childt); topt.text = s; item.AddChild(topt); break; case simpletikzParser.IM_OPTIONS: //Tikz_Options to = Tikz_Options.FromCommonTree(childt); Tikz_Options to = new Tikz_Options(); FillItem(to, childt, tokens); item.AddChild(to); //to.text = getTokensString(tokens, childt); //item.AddChild(tn); if (item.options == null) { // determine whether option belongs to the item (e.g. \draw [this belongs to draw] blabla [thisnot]) // i.e., the scope of the options is the whole item's body // this is hacky if (item.Children.Count == 1 || (item.Children.Count == 2 && (item.Children[0] is Tikz_Something) && item.Children[0].ToString().Trim() == "")) { item.options = to; } } break; case simpletikzParser.IM_TIKZSET: Tikz_Options to2 = new Tikz_Options(); FillItem(to2, childt, tokens); item.AddChild(to2); break; case simpletikzParser.IM_STYLE: Tikz_Option topt2 = Tikz_Option.FromCommonTree(childt, tokens); //FillItem(to2, childt, tokens); topt2.text = getTokensString(tokens, childt); item.AddChild(topt2); break; case simpletikzParser.IM_CONTROLS: Tikz_Controls tcontrols = new Tikz_Controls(); FillItem(tcontrols, childt, tokens); item.AddChild(tcontrols); break; case simpletikzParser.IM_SIZE: Tikz_Size tsize = Tikz_Size.FromCommonTree(childt, tokens); tsize.text = getTokensString(tokens, childt); item.AddChild(tsize); //Tikz_Size tsize = new Tikz_Size(); //item.AddChild(tsize); break; //case simpletikzParser.ID: //case simpletikzParser.IM_STRING: //case simpletikzParser.COMMAND: //case simpletikzParser.T__57: // break; case simpletikzParser.IM_TIKZEDT_CMD: Tikz_EdtCommand cmd = new Tikz_EdtCommand(getTokensString(tokens, childt)); item.AddChild(cmd); break; case simpletikzParser.IM_DONTCARE: Tikz_Something st = new Tikz_Something(getTokensString(tokens, childt)); item.AddChild(st); break; default: // getting here is an error throw new Exception(" childt.Type not handled! " + childt.Type.ToString() + " (\"" + getTokensString(tokens, childt) + "\")"); //break; } curToken = childt.TokenStopIndex + 1; } if (t.TokenStartIndex >= 0) // rule out empty code { addSomething(item, tokens, curToken, t.TokenStopIndex); } return(true); }