/// <summary> /// Constructs the view model for a given <paramref name="textureAtlas"/> with a given <paramref name="colorType"/> /// </summary> /// <param name="textureAtlas">The texture atlas for which the view model should be created</param> /// <param name="colorType">The color type to be used</param> public TextureAtlasViewModel(TextureAtlas textureAtlas, SKColorType colorType = SKColorType.Rgba8888) { eventAggregator = ServiceLocator.Current.GetInstance <IEventAggregator>(); this.textureAtlas = textureAtlas; this.colorType = colorType; TextureAtlasBitmap = new ImageViewModel(textureAtlas.GetImageWithColorType(this.colorType)); SelectedRectangles = new HashSet <PPRect>(); MouseClickPosition = new PPPoint(-1, -1); //make sure that upper left retangle is not selected in the beginning PropertyChanged += TextureAtlasViewModel_PropertyChanged; //Paint = new RelayCommand((args => //{ // using (SKPaint paint = new SKPaint()) // { // paint.IsAntialias = true; // var e = (SkiaSharp.Views.Desktop.SKPaintSurfaceEventArgs)args; // foreach (var selectedRectangle in SelectedRectangles) // { // e.Surface.Canvas.DrawRect(new SKRectI(selectedRectangle.Left, selectedRectangle.Top, selectedRectangle.Right, selectedRectangle.Bottom), paint); // } // e.Surface.Canvas.Clear(SKColors.Transparent); // } //}), (_) => true); }
private void OnMouseLocked(object sender, PPError result) { mouse_locked_ = result == PPError.Ok; if (result != PPError.Ok) { Log($"Mouselock failed with error number {result}.\n"); } mouse_movement_ = PPPoint.Zero; Paint(); }
void DrawCenterSpot(ImageData image, uint spot_color) { if (image.IsEmpty) { Log("DrawCenterSpot with NULL image"); return; } int[] data = null; IntPtr dataPtr = image.Data; if (dataPtr == IntPtr.Zero) { return; } data = new int[(image.Size.Width * image.Size.Height)]; Marshal.Copy(dataPtr, data, 0, data.Length); // Draw the center spot. The ROI is bounded by the size of the spot, plus // one pixel. int center_x = image.Size.Width / 2; int center_y = image.Size.Height / 2; int region_of_interest_radius = kCentralSpotRadius + 1; var left_top = new PPPoint(Math.Max(0, center_x - region_of_interest_radius), Math.Max(0, center_x - region_of_interest_radius)); var right_bottom = new PPPoint(Math.Min(image.Size.Width, center_x + region_of_interest_radius), Math.Min(image.Size.Height, center_y + region_of_interest_radius)); for (int y = left_top.Y; y < right_bottom.Y; ++y) { for (int x = left_top.X; x < right_bottom.X; ++x) { if (MouseLockInstance.GetDistance(x, y, center_x, center_y) < kCentralSpotRadius) { unchecked { data[y * image.Stride / 4 + x] = (int)spot_color; } } } } Marshal.Copy(data, 0, dataPtr, data.Length); }
private void OnHandleMessage(object sender, Var varMessage) { if (!varMessage.IsInt) { Console.WriteLine("Unexpected message."); } PPMouseCursorType cursor = (PPMouseCursorType)varMessage.AsInt(); if (cursor == PPMouseCursorType.Custom) { var hotSpot = new PPPoint(16, 16); SetCursor(cursor, customCursor, hotSpot); } else { SetCursor(cursor); } }
private void OnMouseDownOrMove(object sender, MouseEventArgs e) { if (buffer == null) { e.Handled = true; return; } if (e.Buttons == PPInputEventMouseButton.None) { e.Handled = true; return; } PPPoint pos = e.Position; mouse.X = (int)(pos.X * deviceScale); mouse.Y = (int)(pos.Y * deviceScale); mouseDown = true; e.Handled = true; }
private void DrawHDM() { try { if (this.bmp != null) { this.bmp.Dispose(); this.bmp = null; } if (this.g != null) { this.g.Dispose(); this.bmp = null; } this.bmp = new Bitmap(this.bmpWidth, this.bmpheight); this.g = Graphics.FromImage(bmp); this.g.Clear(Color.White); // 外廓线 Pen pen = new Pen(Color.Black, 4); int outlineXOrigin = left; int outlineYOrigin = top + titleHeight; int outlineWidth = this.bmpWidth - outlineXOrigin - right; int outlineHeight = this.bmpheight - outlineYOrigin - bottom; this.g.DrawRectangle(pen, outlineXOrigin, outlineYOrigin, outlineWidth, outlineHeight); // 标题 Font font = new Font("黑体", 16); SolidBrush brush = new SolidBrush(Color.Black); SizeF vSizeF = g.MeasureString(this.mapname, font); int pxMapName = (int)vSizeF.Width; g.DrawString(this.mapname, font, brush, outlineXOrigin + (outlineWidth - pxMapName) / 2, 3); // 所在道路 font = new Font("Times New Roman", 9); g.DrawString("所在道路:" + roadname, font, brush, outlineXOrigin, outlineYOrigin - 20); // 断面号 string tempMapNum = "断面号:" + mapnum; vSizeF = g.MeasureString(tempMapNum, font); int pxMapNum = (int)vSizeF.Width; g.DrawString(tempMapNum, font, brush, outlineXOrigin + outlineWidth - pxMapNum, outlineYOrigin - 20); // 表格线 pen = new Pen(Color.Black, 3); int tableXOrigin = outlineXOrigin + outlineInterval; int tableYEnd = this.bmpheight - bottom - outlineInterval; int tableYOrigin = tableYEnd - tableHeight; int tableXEnd = this.bmpWidth - tableXOrigin - right + outlineInterval; int rowHeight = tableHeight / rowCount; this.g.DrawLine(pen, tableXOrigin, tableYOrigin, tableXEnd, tableYOrigin); this.g.DrawLine(pen, tableXOrigin, tableYEnd, tableXEnd, tableYEnd); this.g.DrawLine(pen, tableXOrigin, tableYOrigin, tableXOrigin, tableYEnd); this.g.DrawLine(pen, tableXEnd, tableYOrigin, tableXEnd, tableYEnd); pen = new Pen(Color.Black, 2); for (int i = 1; i <= rowCount - 1; i++) { this.g.DrawLine(pen, tableXOrigin, tableYOrigin + rowHeight * i, tableXEnd, tableYOrigin + rowHeight * i); } this.g.DrawLine(pen, tableXOrigin + leftYAxis, tableYOrigin, tableXOrigin + leftYAxis, tableYEnd); // 表格文字 font = new Font("Times New Roman", 11); g.DrawString("地面高程(m)", font, brush, (float)tableXOrigin + 5, (float)tableYOrigin + rowHeight * 0 + 15); g.DrawString("管线高程(m)", font, brush, (float)tableXOrigin + 5, (float)tableYOrigin + rowHeight * 1 + 15); g.DrawString("规 格(mm)", font, brush, (float)tableXOrigin + 5, (float)tableYOrigin + rowHeight * 2 + 15); g.DrawString("间 距(m)", font, brush, (float)tableXOrigin + 5, (float)tableYOrigin + rowHeight * 3 + 15); g.DrawString("总 长(m)", font, brush, (float)tableXOrigin + 5, (float)tableYOrigin + rowHeight * 4 + 15); //绘图 int drawXOrigin1 = tableXOrigin + leftYAxis; int drawYOrigin1 = outlineYOrigin + outlineInterval; int drawXEnd1 = tableXEnd; int drawYEnd1 = tableYOrigin; this.g.DrawLine(pen, drawXOrigin1, drawYOrigin1, drawXOrigin1, drawYEnd1); int drawXOrigin = drawXOrigin1 + interval; int drawYOrigin = drawYOrigin1 + 2 * interval; int drawXEnd = drawXEnd1 - 2 * interval; int drawYEnd = drawYEnd1 - interval; int drawWidth = drawXEnd - drawXOrigin; int drawHeight = drawYEnd - drawYOrigin; double scaleX = distsum / drawWidth; double scaleY = (hmax - hmin) / drawHeight; double scaleXReal = scaleX * g.DpiX / 0.0254; double scaleYReal = scaleY * g.DpiY / 0.0254; g.DrawString("比例尺", font, brush, drawXOrigin1 + (drawXEnd1 - drawXOrigin1) / 2 - 120, drawYOrigin1 + 10); g.DrawString("水平 1:" + scaleXReal.ToString("0"), font, brush, drawXOrigin1 + (drawXEnd1 - drawXOrigin1) / 2 - 60, drawYOrigin1); g.DrawString("垂直 1:" + scaleYReal.ToString("0"), font, brush, drawXOrigin1 + (drawXEnd1 - drawXOrigin1) / 2 - 60, drawYOrigin1 + 20); // 刻度 font = new Font("Times New Roman", 9); int ihmax = int.Parse(Math.Ceiling(hmax).ToString()); int ihmin = int.Parse(Math.Ceiling(hmin).ToString()); int deth = ihmax - ihmin; if (deth <= 10) { for (int i = ihmin; i <= ihmax; i = i + 1) { float dialY = (float)(drawYOrigin + (hmax - i) / scaleY); g.DrawLine(pen, drawXOrigin1 - 4, dialY, drawXOrigin1, dialY); g.DrawString(i.ToString("0.0"), font, brush, drawXOrigin1 - 30, dialY - 5); } } else if (deth < 20 && deth > 10) { for (int i = ihmin; i <= ihmax; i = i + 2) { float dialY = (float)(drawYOrigin + (hmax - i) / scaleY); g.DrawLine(pen, drawXOrigin1 - 4, dialY, drawXOrigin1, dialY); g.DrawString(i.ToString("0.0"), font, brush, drawXOrigin1 - 30, dialY - 5); } } else { double intervalDial = deth / 10 * 10.0 / dialCount; for (int i = 0; i <= dialCount; i++) { float dialY = (float)(drawYOrigin + i * intervalDial); double dialValue = hmax - i * intervalDial * scaleY; g.DrawLine(pen, drawXOrigin1 - 4, dialY, drawXOrigin1, dialY); g.DrawString(dialValue.ToString("0.0"), font, brush, drawXOrigin1 - 30, dialY - 5); } } // 管线 RotateText rotate = new RotateText(); rotate.Graphics = g; StringFormat format = new StringFormat(); format.Alignment = StringAlignment.Center; format.LineAlignment = StringAlignment.Center; PPLine2D pt0 = this.pplines[0]; foreach (PPLine2D pt in this.pplines) { PPPoint ptInter = pt.interPoint; double dis = Math.Sqrt((ptInter.X - pt0.interPoint.X) * (ptInter.X - pt0.interPoint.X) + (ptInter.Y - pt0.interPoint.Y) * (ptInter.Y - pt0.interPoint.Y)); int pxx = int.Parse(Math.Ceiling(dis / scaleX).ToString()); int pxy = int.Parse(Math.Ceiling((pt.clh - hmin) / scaleY).ToString()); int ippw = 10; int ipph = 10; int ppw = int.Parse(Math.Ceiling(pt.gj[0] * 0.001 / scaleX).ToString()); int pph = int.Parse(Math.Ceiling(pt.gj[1] * 0.001 / scaleY).ToString()); //if (ppw > ippw) ippw = ppw; //if (pph > ipph) ipph = pph; int detStandard = 0;// 内底or外顶 if (pt.hlb == 1) { detStandard += ipph / 2; } else if (pt.hlb == -1) { detStandard -= ipph / 2; } int ppcx = drawXOrigin + pxx - ippw / 2; int ppcy = drawYEnd - pxy - ipph / 2 + detStandard; pen = new Pen(Color.DarkBlue, 2); if (pt.isrect) { g.DrawRectangle(pen, ppcx, ppcy, ippw, ipph); } else { g.DrawEllipse(pen, ppcx, ppcy, ippw, ipph); } pen = new Pen(Color.Black, 1); pen.DashStyle = DashStyle.Dash; g.DrawLine(pen, drawXOrigin + pxx, drawYEnd - pxy, drawXOrigin + pxx, tableYEnd - rowHeight); font = new Font("Times New Roman", 8); brush = new SolidBrush(Color.DarkBlue); if (pt.hlb == 1) { g.DrawString(pt.facType, font, brush, ppcx + ippw / 2 + 3, ppcy + 3); } else if (pt.hlb == -1) { g.DrawString(pt.facType, font, brush, ppcx + ippw / 2 + 3, ppcy + ipph + 3); } else if (pt.hlb == 0) { g.DrawString(pt.facType, font, brush, ppcx + ippw / 2 + 3, ppcy + ipph / 2 + 3); } rotate.DrawString(pt.clh.ToString("0.000"), font, brush, new PointF(ppcx - 3, tableYOrigin + rowHeight * 1 + 20), format, -90f); if (pt.isrect) { rotate.DrawString(pt.dia, font, brush, new PointF(ppcx - 3, tableYOrigin + rowHeight * 2 + 20), format, -90f); } else { rotate.DrawString("DN" + pt.dia, font, brush, new PointF(ppcx - 3, tableYOrigin + rowHeight * 2 + 20), format, -90f); } brush = new SolidBrush(Color.Sienna); rotate.DrawString(pt.cgh.ToString("0.000"), font, brush, new PointF(ppcx - 3, tableYOrigin + rowHeight * 0 + 20), format, -90f); } int gxx0 = 0; int gxy0 = 0; int pxy0 = 0; for (int i = 0; i < this.pplines.Count; i++) { PPLine2D pt = this.pplines[i]; PPPoint ptInter = pt.interPoint; double dis = Math.Sqrt((ptInter.X - pt0.interPoint.X) * (ptInter.X - pt0.interPoint.X) + (ptInter.Y - pt0.interPoint.Y) * (ptInter.Y - pt0.interPoint.Y)); int gxx = int.Parse(Math.Ceiling(dis / scaleX).ToString()); int gxy = int.Parse(Math.Ceiling((pt.cgh - hmin) / scaleY).ToString()); int pxy = int.Parse(Math.Ceiling((pt.clh - hmin) / scaleY).ToString()); if (i == 0) { // 标识 font = new Font("Times New Roman", 9); brush = new SolidBrush(Color.Sienna); g.DrawString("地面", font, brush, drawXOrigin1 - 60, drawYEnd - gxy - 5); font = new Font("Times New Roman", 9); brush = new SolidBrush(Color.DarkBlue); g.DrawString("管线", font, brush, drawXOrigin1 - 60, drawYEnd - pxy - 5); gxx0 = gxx; gxy0 = gxy; pxy0 = pxy; } else { if (this._type == 1) { pen = new Pen(Color.DarkBlue, 3); g.DrawLine(pen, drawXOrigin + gxx0, drawYEnd - pxy0, drawXOrigin + gxx, drawYEnd - pxy); } //间距 double space = pt.space; string strspace = space.ToString("0.00"); vSizeF = g.MeasureString(strspace, font); int pxspace = (int)vSizeF.Width; g.DrawString(strspace, font, brush, drawXOrigin + (gxx + gxx0) / 2 - pxspace / 2, (float)tableYOrigin + rowHeight * 3 + 12); //地面线 pen = new Pen(Color.Sienna, 3); g.DrawLine(pen, drawXOrigin + gxx0, drawYEnd - gxy0, drawXOrigin + gxx, drawYEnd - gxy); gxx0 = gxx; gxy0 = gxy; } } //总长 string strdistnum = this.distsum.ToString("0.00"); vSizeF = g.MeasureString(strdistnum, font); int pxdistnum = (int)vSizeF.Width; g.DrawString(strdistnum, font, brush, (drawXOrigin + drawXEnd) / 2 - pxdistnum / 2, (float)tableYOrigin + rowHeight * 4 + 12); } catch (Exception ex) { } }
private void OnMouseMove(object sender, MouseEventArgs mouseEvent) { mouse_movement_ = mouseEvent.Movement; Paint(); mouseEvent.Handled = true; }
void DrawNeedle(ImageData image, uint needle_color) { if (image.IsEmpty) { Log("DrawNeedle with NULL image"); return; } int[] data = null; IntPtr dataPtr = image.Data; if (dataPtr == IntPtr.Zero) { return; } data = new int[(image.Size.Width * image.Size.Height)]; Marshal.Copy(dataPtr, data, 0, data.Length); if (GetDistance(mouse_movement_.X, mouse_movement_.Y, 0, 0) <= kCentralSpotRadius) { return; } int abs_mouse_x = Math.Abs(mouse_movement_.X); int abs_mouse_y = Math.Abs(mouse_movement_.Y); int center_x = image.Size.Width / 2; int center_y = image.Size.Height / 2; var vertex = new PPPoint(mouse_movement_.X + center_x, mouse_movement_.Y + center_y); var anchor_1 = new PPPoint(); var anchor_2 = new PPPoint(); MouseDirection direction = MouseDirection.Left; if (abs_mouse_x >= abs_mouse_y) { anchor_1.X = (center_x); anchor_1.Y = (center_y - kCentralSpotRadius); anchor_2.X = (center_x); anchor_2.Y = (center_y + kCentralSpotRadius); direction = (mouse_movement_.X < 0) ? MouseDirection.Left : MouseDirection.Right; if (direction == MouseDirection.Left) { anchor_1.Swap(ref anchor_2); } } else { anchor_1.X = (center_x + kCentralSpotRadius); anchor_1.Y = (center_y); anchor_2.X = (center_x - kCentralSpotRadius); anchor_2.Y = (center_y); direction = (mouse_movement_.Y < 0) ? MouseDirection.Up : MouseDirection.Down; if (direction == MouseDirection.Up) { anchor_1.Swap(ref anchor_2); } } var left_top = new PPPoint(Math.Max(0, center_x - abs_mouse_x), Math.Max(0, center_y - abs_mouse_y)); var right_bottom = new PPPoint(Math.Min(image.Size.Width, center_x + abs_mouse_x), Math.Min(image.Size.Height, center_y + abs_mouse_y)); for (int y = left_top.Y; y < right_bottom.Y; ++y) { for (int x = left_top.X; x < right_bottom.X; ++x) { bool within_bound_1 = ((y - anchor_1.Y) * (vertex.X - anchor_1.X)) > ((vertex.Y - anchor_1.Y) * (x - anchor_1.X)); bool within_bound_2 = ((y - anchor_2.Y) * (vertex.X - anchor_2.X)) < ((vertex.Y - anchor_2.Y) * (x - anchor_2.X)); bool within_bound_3 = (direction == MouseDirection.Up && y < center_y) || (direction == MouseDirection.Down && y > center_y) || (direction == MouseDirection.Left && x < center_x) || (direction == MouseDirection.Right && x > center_x); if (within_bound_1 && within_bound_2 && within_bound_3) { unchecked { data[y * image.Stride / 4 + x] = (int)needle_color; } } } } Marshal.Copy(data, 0, dataPtr, data.Length); }