Exemplo n.º 1
0
        void IManipulator.Render(ViewControl vc)
        {
            TerrainGob   terrain    = m_terrainEditor.TerrainEditorControl.SelectedTerrain;
            TerrainBrush brush      = m_terrainEditor.TerrainEditorControl.SelectedBrush;
            TerrainMap   terrainMap = m_terrainEditor.TerrainEditorControl.SelectedTerrainMap;

            if (brush == null || (!brush.CanApplyTo(terrain) && !brush.CanApplyTo(terrainMap)))
            {
                return;
            }

            Vec2F drawscale = new Vec2F(1.0f, 1.0f);

            if (brush.CanApplyTo(terrainMap))
            {
                ImageData mapImg = terrainMap.GetSurface();
                ImageData hmImg  = terrain.GetSurface();
                drawscale.X = (float)hmImg.Width / (float)mapImg.Width;
                drawscale.Y = (float)hmImg.Height / (float)mapImg.Height;
            }

            Point scrPt = vc.PointToClient(Control.MousePosition);

            if (!vc.ClientRectangle.Contains(scrPt))
            {
                return;
            }
            Ray3F rayw = vc.GetWorldRay(scrPt);

            TerrainGob.RayPickRetVal retval;
            if (terrain.RayPick(rayw, out retval))
            {
                terrain.DrawBrush(brush, drawscale, retval.hitpos);
            }
        }
Exemplo n.º 2
0
        private void ApplyBrush(ViewControl vc, System.Drawing.Point scrPt)
        {
            TerrainGob   terrain    = m_terrainEditor.TerrainEditorControl.SelectedTerrain;
            TerrainBrush brush      = m_terrainEditor.TerrainEditorControl.SelectedBrush;
            TerrainMap   terrainMap = m_terrainEditor.TerrainEditorControl.SelectedTerrainMap;

            if (brush == null || (!brush.CanApplyTo(terrain) && !brush.CanApplyTo(terrainMap)))
            {
                return;
            }

            Ray3F rayw = vc.GetWorldRay(scrPt);

            TerrainGob.RayPickRetVal retval;
            if (terrain.RayPick(rayw, out retval))
            {
                TerrainOp op = null;
                if (brush.CanApplyTo(terrain))
                {
                    Point pt = terrain.WorldToSurfaceSpace(retval.hitpos);
                    brush.Apply(terrain, pt.X, pt.Y, out op);
                }
                else if (brush.CanApplyTo(terrainMap))
                {
                    Point pt = terrainMap.WorldToSurfaceSpace(retval.hitpos);
                    brush.Apply(terrainMap, pt.X, pt.Y, out op);
                }
                m_tmpOps.Add(op);
                m_terrainOpList.Add(new WeakReference(op));
                m_memUsage += op.SizeInBytes;
            }
        }
Exemplo n.º 3
0
        private void DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index < 0)
            {
                return;
            }
            ListBox    list = (ListBox)sender;
            TerrainMap map  = (TerrainMap)list.Items[e.Index];

            // bounds
            Rectangle bounds = e.Bounds;

            bounds.Width  -= 1;
            bounds.Height -= 1;

            Brush bkgBrush = (e.State & DrawItemState.Selected) == DrawItemState.Selected
                ? SystemBrushes.Highlight : Brushes.DarkGray;

            // draw bkg
            e.Graphics.FillRectangle(bkgBrush, bounds);

            var thumbnailRect = new Rectangle(bounds.X + 5, bounds.Y + m_header, m_thumbSize, m_thumbSize);

            foreach (var texInfo in map.TextureInfos)
            {
                Image img     = GetThumNail(texInfo.Uri);
                var   srcRect = new Rectangle(0, 0, img.Width, img.Height);
                e.Graphics.DrawImage(img, thumbnailRect, srcRect, GraphicsUnit.Pixel);
                e.Graphics.DrawRectangle(Pens.Yellow, thumbnailRect);
                SizeF texsize = e.Graphics.MeasureString(texInfo.Name, e.Font);
                var   texRect = new RectangleF(thumbnailRect.X + 1, thumbnailRect.Y + 1, texsize.Width, texsize.Height);
                e.Graphics.FillRectangle(Brushes.Gray, texRect);
                e.Graphics.DrawString(texInfo.Name, e.Font, Brushes.White, texRect.Location);
                thumbnailRect.X += (thumbnailRect.Width + 6);
            }
            e.Graphics.DrawString(map.Name, e.Font, SystemBrushes.WindowText, bounds.Location);
        }
Exemplo n.º 4
0
        public override void Apply(ITerrainSurface target, int x, int y, out TerrainOp op)
        {
            op = null;
            if (!CanApplyTo(target))
            {
                return;
            }

            Bound2di   outRect;
            TerrainMap tmap = target.As <TerrainMap>();
            ImageData  mask = tmap.GetSurface();

            ComputeBound(mask, x, y, out outRect);
            bool validArgs = outRect.isValid && mask.Format == ImageDataFORMAT.R8_UNORM;

            System.Diagnostics.Debug.Assert(validArgs);
            if (validArgs == false)
            {
                return;
            }

            op = new TerrainOp(target, outRect);
            var brushOp = GetBrushOp();

            float     minheight = tmap.MinHeight;
            float     maxheight = tmap.MaxHeight;
            float     minslope  = tmap.MinSlope;
            float     maxslope  = tmap.MaxSlope;
            ImageData hmImg     = tmap.Parent.GetSurface();

            float dx = (float)hmImg.Width / (float)mask.Width;
            float dy = (float)hmImg.Height / (float)mask.Height;

            int pixelstrength         = (int)Math.Round(255.0f * Strength);
            Func <int, int, byte> ops = null;

            if (brushOp == BrushOps.Paint)
            {
                ops = (px, py) =>
                {
                    if (px >= pixelstrength)
                    {
                        return((byte)px);
                    }
                    else
                    {
                        return((byte)Math.Min(pixelstrength, px + py));
                    }
                };
            }
            else
            {
                ops = (px, py) => (byte)MathUtil.Clamp <int>(px - py, 0, 255);
            }


            // start point in kernel space.
            int bx0 = x - Radius;
            int by0 = y - Radius;

            float run  = 2.0f * tmap.Parent.CellSize;
            int   size = 2 * Radius + 1;

            for (int cy = outRect.y1; cy < outRect.y2; cy++)
            {
                int by = cy - by0;

                for (int cx = outRect.x1; cx < outRect.x2; cx++)
                {
                    int bx = cx - bx0;

                    // test for height and slope.
                    // xform px and py to heightmap space hx, hy.
                    if (brushOp == BrushOps.Paint)
                    {
                        int   hx     = (int)Math.Round(cx * dx);
                        int   hy     = (int)Math.Round(cy * dy);
                        float height = hmImg.GetPixelFloat(hx, hy);
                        if (height < minheight || height > maxheight)
                        {
                            continue;
                        }
                        // check slope
                        float slopex = hmImg.GetPixelFloat(hx + 1, hy) - hmImg.GetPixelFloat(hx - 1, hy);
                        float slopeY = hmImg.GetPixelFloat(hx, hy + 1) - hmImg.GetPixelFloat(hx, hy - 1);

                        float slope = Math.Max(Math.Abs(slopex), Math.Abs(slopeY)) / run;
                        float deg   = MathHelper.ToDegree((float)Math.Atan(slope));
                        if (deg < minslope || deg > maxslope)
                        {
                            continue;
                        }
                    }

                    float scrPixel  = Kernel[size * by + bx];
                    byte *destPixel = mask.GetPixel(cx, cy);
                    *     destPixel = ops(*destPixel, (int)Math.Round(scrPixel * 255.0f));
                }
            }

            tmap.ApplyDirtyRegion(outRect);
        }
Exemplo n.º 5
0
        public override bool CanApplyTo(ITerrainSurface target)
        {
            TerrainMap map = target.As <TerrainMap>();

            return(map != null && map.GetSurfaceInstanceId() != 0);
        }