Exemple #1
0
        private void paintTick(int i, float valPixel, float valUnit, UnitScale scale, bool vertical)
        {
            int length = i % scale.BTickSteps == 0 ? drawWidth / 4 : i % scale.MTickSteps == 0 ? drawWidth / 6 : drawWidth / 16;
            // Only draw one row of ticks in slim mode.
            bool drawUpper = true;
            bool drawLower = !settings.SlimMode;

            // In 2D mode, don't draw ticks at connection to other scale.
            if (resizeMode == FormResizeMode.TwoDimensional)
            {
                /*
                 * Check if we are within the area of a tick in the opposite direction
                 * or within the area of the opposite direction scale.
                 */
                drawUpper &= !(valPixel < length);
                drawLower &= !(valPixel < drawWidth);
            }
            using (Brush brush = new SolidBrush(settings.Theme.TickColor))
                using (Pen pen = new Pen(brush, 1))
                    using (Font font = new Font("Arial", 9))
                    {
                        float pos = valPixel;
                        if (!vertical)
                        {
                            if (drawUpper)
                            {
                                g.DrawLine(pen, pos, 0, pos, length);
                            }
                            if (drawLower)
                            {
                                g.DrawLine(pen, pos, drawWidth - length, pos, drawWidth);
                            }
                            if (valUnit > 0 && i % scale.BTickSteps == 0)
                            {
                                DrawString(Math.Round(valUnit, 2).ToString(), font, brush, pos - 8, length + 3);
                            }
                        }
                        else
                        {
                            if (drawUpper)
                            {
                                g.DrawLine(pen, 0, pos, length, pos);
                            }
                            if (drawLower)
                            {
                                g.DrawLine(pen, drawWidth - length, pos, drawWidth, pos);
                            }
                            if (valUnit > 0 && i % scale.BTickSteps == 0)
                            {
                                DrawString(Math.Round(valUnit, 2).ToString(), font, brush, length + 3, pos - 7);
                            }
                        }
                    }
        }
        protected void PaintRulerScale(bool vertical)
        {
            int max = vertical ? c.Size.Height : c.Size.Width;
            // ----- Draw the ruler scale -----
            UnitScale scale = Ticks[settings.MeasuringUnit];
            // valUnit: the current position in the chosen unit.
            // valPixel: the current position in pixels.
            float valUnit = 0, valPixel = 0, i = 0;

            while (valPixel <= max)
            {
                valPixel = converter.ConvertToPixel(valUnit, vertical);
                int length = i % scale.BTickSteps == 0 ? drawWidth / 4 : i % scale.MTickSteps == 0 ? drawWidth / 6 : drawWidth / 16;
                using (Brush brush = new SolidBrush(settings.Theme.TickColor))
                    using (Pen pen = new Pen(brush, 1))
                        using (Font font = new Font("Arial", 9))
                        {
                            float pos = valPixel;
                            if (!vertical)
                            {
                                if (resizeMode == FormResizeMode.Horizontal || pos > length)
                                {
                                    g.DrawLine(pen, pos, 0, pos, length);
                                }
                                if (!settings.SlimMode && (resizeMode == FormResizeMode.Horizontal || valPixel > drawWidth))
                                {
                                    g.DrawLine(pen, pos, drawWidth - length, pos, drawWidth);
                                }
                                if (valPixel > 0 && i % scale.BTickSteps == 0)
                                {
                                    g.DrawString(Math.Round(valUnit, 2).ToString(), font, brush, pos - 8, length + 3);
                                }
                            }
                            else
                            {
                                if (resizeMode == FormResizeMode.Vertical || pos > length)
                                {
                                    g.DrawLine(pen, 0, pos, length, pos);
                                }
                                if (!settings.SlimMode && (resizeMode == FormResizeMode.Vertical || valPixel > drawWidth))
                                {
                                    g.DrawLine(pen, drawWidth - length, pos, drawWidth, pos);
                                }
                                if (valPixel > 0 && i % scale.BTickSteps == 0)
                                {
                                    g.DrawString(Math.Round(valUnit, 2).ToString(), font, brush, length + 3, pos - 7);
                                }
                            }
                        }
                valUnit += scale.StepSize;
                i       += 1;
            }
            // ----- Optionally, draw total length bound to right border and start offset -----
            if (settings.ShowOffsetLengthLabels)
            {
                int    roundingDigits = settings.SlimMode ? 1 : 2;
                string lblLength      = String.Format("{0}{1}",
                                                      Math.Round(converter.ConvertFromPixel(max, vertical), roundingDigits), converter.UnitString);
                int    offset    = vertical ? c.Location.Y : c.Location.X;
                string lblOffset = String.Format("{0}{1}",
                                                 Math.Round(converter.ConvertFromPixel(offset, vertical), roundingDigits), converter.UnitString);
                using (Brush brush = new SolidBrush(settings.Theme.LengthLabelColor))
                    using (Font font = new Font("Arial", 9))
                    {
                        StringFormat format = new StringFormat()
                        {
                            Alignment = StringAlignment.Far
                        };
                        if (!vertical)
                        {
                            float y = drawWidth;
                            // adjust the label text based on ruler width
                            if (settings.SlimMode)
                            {
                                y -= 14;
                            }
                            else
                            {
                                y /= 2.0f;
                            }
                            g.DrawString(lblLength, font, brush, max, y, format);
                            // only draw offset label if not in two-dimensional mode, otherwise it would look messy
                            if (resizeMode != FormResizeMode.TwoDimensional)
                            {
                                g.DrawString(lblOffset, font, brush, 0, y);
                            }
                        }
                        else
                        {
                            float x = drawWidth;
                            if (!settings.SlimMode)
                            {
                                x *= (7.0f / 8.0f);
                            }
                            g.DrawString(lblLength, font, brush, x, max - 14, format);
                            // only draw offset label if not in two-dimensional mode, otherwise it would look messy
                            if (resizeMode != FormResizeMode.TwoDimensional)
                            {
                                g.DrawString(lblOffset, font, brush, x, 0, format);
                            }
                        }
                    }
            }
        }
Exemple #3
0
        protected void PaintRulerScale(bool vertical)
        {
            int max = vertical ? c.Height : c.Width;
            // ----- Draw the ruler scale -----
            UnitScale scale = Ticks[settings.MeasuringUnit];
            // valUnit: the current position in the chosen unit.
            // valPixel: the current position in pixels.
            float valUnit = 0, valPixel = 0;
            int   i = 0;

            while (valPixel <= max)
            {
                valPixel = converter.ConvertToPixel(valUnit, vertical);
                paintTick(i, valPixel, valUnit, scale, vertical);
                valUnit += scale.StepSize;
                i       += 1;
            }
            // ----- Optionally, draw total length bound to right border and start offset -----
            if (settings.ShowOffsetLengthLabels)
            {
                int    roundingDigits = settings.SlimMode ? 1 : 2;
                string lblLength      = converter.FormatFromPixel(max, vertical, roundingDigits);
                // The offset value depends on whether the ruler is flipped.
                int offset;
                if (vertical)
                {
                    if (settings.FlippedY)
                    {
                        offset = converter.ScreenSize.Height - c.Bottom;
                    }
                    else
                    {
                        offset = c.Top;
                    }
                }
                else
                {
                    if (settings.FlippedX)
                    {
                        offset = converter.ScreenSize.Width - c.Right;
                    }
                    else
                    {
                        offset = c.Left;
                    }
                }
                string lblOffset = converter.FormatFromPixel(offset, vertical, roundingDigits);
                using (Brush brush = new SolidBrush(settings.Theme.LengthLabelColor))
                    using (Font font = new Font("Arial", 9))
                    {
                        StringFormat format = new StringFormat()
                        {
                            Alignment = StringAlignment.Far
                        };
                        if (!vertical)
                        {
                            float y = drawWidth;
                            // adjust the label text based on ruler width
                            if (settings.SlimMode)
                            {
                                y -= LABEL_HEIGHT;
                            }
                            else
                            {
                                y /= 2.0f;
                            }
                            DrawString(lblLength, font, brush, max, y, format);
                            labelMargins.X     = 0;
                            labelMargins.Width = max - (int)g.MeasureString(lblLength, font).Width;
                            // only draw offset label if not in two-dimensional mode, otherwise it would look messy
                            if (resizeMode != FormResizeMode.TwoDimensional)
                            {
                                DrawString(lblOffset, font, brush, 0, y);
                                labelMargins.X      = (int)g.MeasureString(lblOffset, font).Width;
                                labelMargins.Width -= labelMargins.X;
                            }
                        }
                        else
                        {
                            float x = drawWidth;
                            if (!settings.SlimMode)
                            {
                                x *= (7.0f / 8.0f);
                            }
                            DrawString(lblLength, font, brush, x, max - LABEL_HEIGHT, format);
                            labelMargins.Y      = 0;
                            labelMargins.Height = max - LABEL_HEIGHT;
                            // only draw offset label if not in two-dimensional mode, otherwise it would look messy
                            if (resizeMode != FormResizeMode.TwoDimensional)
                            {
                                DrawString(lblOffset, font, brush, x, 0, format);
                                labelMargins.Y       = LABEL_HEIGHT;
                                labelMargins.Height -= labelMargins.Y;
                            }
                        }
                    }
            }
            else
            {
                labelMargins = new Rectangle(0, 0, int.MaxValue, int.MaxValue);
            }
        }