Пример #1
0
        private void CmboxTerrain_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox   cmbbox = (ComboBox)sender;
            TerrainGob gob    = (TerrainGob)cmbbox.SelectedItem;

            Bind(gob);
        }
Пример #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;
            }
        }
        public void PopulatedTerrainCmbox()
        {
            TerrainGob curTerrain  = m_cmboxTerrain.Items.Count > 0 ? (TerrainGob)m_cmboxTerrain.SelectedItem : null;
            var        terrainGobs = Util.FindAll <TerrainGob>();

            m_cmboxTerrain.Enabled = false;

            m_cmboxTerrain.BeginUpdate();
            m_cmboxTerrain.Items.Clear();
            foreach (var terrain in terrainGobs)
            {
                m_cmboxTerrain.Items.Add(terrain);
            }
            m_cmboxTerrain.EndUpdate();
            if (m_cmboxTerrain.Items.Count > 0)
            {
                m_cmboxTerrain.Enabled = true;
                if (curTerrain != null && m_cmboxTerrain.Items.Contains(curTerrain))
                {
                    m_cmboxTerrain.SelectedItem = curTerrain;
                }
                else
                {
                    m_cmboxTerrain.SelectedIndex = 0;
                }
            }
            ReBind();
        }
Пример #4
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);
            }
        }
Пример #5
0
        public override void Apply(ITerrainSurface target, int x, int y, out TerrainOp op)
        {
            op = null;
            if (!CanApplyTo(target))
            {
                return;
            }
            Bound2di   outRect;
            TerrainGob terrain = target.As <TerrainGob>();
            ImageData  hmImg   = terrain.GetSurface();

            ComputeBound(hmImg, x, y, out outRect);
            if (!outRect.isValid || hmImg.Format != ImageDataFORMAT.R32_FLOAT)
            {
                return;
            }

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

            Func <float, float, float> ops = null;

            if (brushOp == BrushOps.Add)
            {
                ops = (a, b) => a + b;
            }
            else if (brushOp == BrushOps.Sub)
            {
                ops = (a, b) => a - b;
            }

            if (ops == null)
            {
                throw new ArgumentException("brushOp");
            }


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

            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;
                    float  scrPixel  = Kernel[size * by + bx] * m_height;
                    float *destPixel = (float *)hmImg.GetPixel(cx, cy);
                    *      destPixel = ops(*destPixel, scrPixel);
                }
            }
            terrain.ApplyDirtyRegion(outRect);
        }
Пример #6
0
        private ListBox GetActiveList()
        {
            TerrainGob terrain = (TerrainGob)m_cmboxTerrain.SelectedItem;

            if (terrain != null && m_layersTabControl.SelectedTab != null)
            {
                return((ListBox)m_layersTabControl.SelectedTab.Controls[0]);
            }
            return(null);
        }
Пример #7
0
        private void MoveBtnClick(object sender, EventArgs e)
        {
            TerrainGob gob     = (TerrainGob)m_cmboxTerrain.SelectedItem;
            ListBox    listbox = GetActiveList();

            if (listbox == null || listbox.SelectedItem == null || gob == null)
            {
                return;
            }


            int lastIndex      = listbox.Items.Count - 1;
            int selectedIndex  = listbox.SelectedIndex;
            int insertionIndex = -1;

            if (sender == m_moveUpBtn && selectedIndex > 0)
            {
                insertionIndex = selectedIndex - 1;
            }
            else if (sender == m_moveDownBtn && selectedIndex < lastIndex)
            {
                insertionIndex = selectedIndex + 1;
            }

            if (insertionIndex >= 0)
            {
                // todo: move class NativeGameWorldAdapter to LevelEditorCore
                //       and create an interface for accessing class GameEngine
                RenderingInterop.NativeGameWorldAdapter
                    gw = GameContext.As <RenderingInterop.NativeGameWorldAdapter>();
                try
                {
                    gw.ManageNativeObjectLifeTime = false;
                    object item = listbox.Items[selectedIndex];
                    GameContext.As <ITransactionContext>().DoTransaction(() =>
                    {
                        // the item must be terrain map.
                        DomNode node          = item.As <DomNode>();
                        DomNode parentNode    = node.Parent;
                        IList <DomNode> items = parentNode.GetChildList(node.ChildInfo);
                        int itemIndex         = items.IndexOf(node);
                        System.Diagnostics.Debug.Assert(itemIndex == selectedIndex);
                        items.RemoveAt(itemIndex);
                        items.Insert(insertionIndex, node);
                    }, "Move Map");
                    listbox.SelectedItem = item;
                }
                finally
                {
                    gw.ManageNativeObjectLifeTime = true;
                }
            }
        }
Пример #8
0
        private void UpdateButtonStatus()
        {
            //Console.WriteLine("update buttons: " + Environment.TickCount);
            TerrainGob gob     = (TerrainGob)m_cmboxTerrain.SelectedItem;
            ListBox    listbox = GetActiveList();

            m_addBtn.Enabled      = gob != null && listbox != null;
            m_deleteBtn.Enabled   = listbox != null && listbox.Items.Count > 0;
            m_moveUpBtn.Enabled   = listbox != null && listbox.SelectedIndex > 0;
            m_moveDownBtn.Enabled = listbox != null &&
                                    listbox.SelectedIndex >= 0 &&
                                    listbox.SelectedIndex < (listbox.Items.Count - 1);
        }
Пример #9
0
        public Point WorldToSurfaceSpace(Vec3F posW)
        {
            Point      result  = new Point();
            TerrainGob terrain = this.GetParentAs <TerrainGob>();
            ImageData  hmImg   = terrain.GetSurface();
            ImageData  mpImg   = GetSurface();
            Point      posH    = terrain.WorldToSurfaceSpace(posW);

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

            result.X = (int)Math.Round(posH.X * dx);
            result.Y = (int)Math.Round(posH.Y * dy);
            return(result);
        }
Пример #10
0
        public void ReBind()
        {
            object     layermap = m_layerList.SelectedItem;
            object     decomap  = m_decoList.SelectedItem;
            TerrainGob gob      = (TerrainGob)m_cmboxTerrain.SelectedItem;

            Bind(gob);
            if (layermap != null && m_layerList.Items.Contains(layermap))
            {
                m_layerList.SelectedItem = layermap;
            }
            if (decomap != null && m_decoList.Items.Contains(decomap))
            {
                m_decoList.SelectedItem = decomap;
            }
        }
Пример #11
0
 private void Bind(TerrainGob gob)
 {
     m_terrainPropGrid.Bind(gob);
     m_layerList.Items.Clear();
     m_decoList.Items.Clear();
     if (gob != null)
     {
         foreach (var layer in gob.LayerMaps)
         {
             m_layerList.Items.Add(layer);
         }
         foreach (var deco in gob.DecorationMaps)
         {
             m_decoList.Items.Add(deco);
         }
     }
     m_propertyGrid.Bind(null);
 }
Пример #12
0
        private void CreateTerrainBtnClick(object sender, EventArgs e)
        {
            using (var dlg = new CreateTerrainDlg(Globals.ResourceRoot.LocalPath))
            {
                if (dlg.ShowDialog(this) == DialogResult.OK)
                {
                    TerrainGob terrain   = TerrainGob.Create(dlg.TerrainName, dlg.HeightMapPath, dlg.CellSize);
                    var        xformable = terrain.As <ITransformable>();
                    xformable.Translation = new Vec3F(-dlg.CellSize * (dlg.HmapCols - 1) / 2.0f, 0.0f, -dlg.CellSize * (dlg.HmapRows - 1) / 2.0f);
                    xformable.UpdateTransform();

                    ApplicationUtil.Insert(GameContext,
                                           GameContext.RootGameObjectFolder,
                                           terrain,
                                           "Add Terrain",
                                           null);
                }
            }
        }
Пример #13
0
        public override void Apply(ITerrainSurface target, int x, int y, out TerrainOp op)
        {
            op = null;
            if (!CanApplyTo(target))
            {
                return;
            }
            Bound2di   outRect;
            TerrainGob terrain = target.As <TerrainGob>();
            ImageData  hmImg   = terrain.GetSurface();

            ComputeBound(hmImg, x, y, out outRect);
            if (!outRect.isValid || hmImg.Format != ImageDataFORMAT.R32_FLOAT)
            {
                return;
            }

            op = new TerrainOp(target, outRect);

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

            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;
                    float  k         = Kernel[size * by + bx] * m_noise[bx, by] * m_scale;
                    float *destPixel = (float *)hmImg.GetPixel(cx, cy);
                    *      destPixel = *destPixel + k;
                }
            }

            terrain.ApplyDirtyRegion(outRect);
        }
Пример #14
0
        bool IManipulator.Pick(ViewControl vc, System.Drawing.Point scrPt)
        {
            TerrainGob   terrain = m_terrainEditor.TerrainEditorControl.SelectedTerrain;
            TerrainBrush brush   = m_terrainEditor.TerrainEditorControl.SelectedBrush;

            if (terrain != null && brush != null)
            {
                FlattenBrush fbrush = brush as FlattenBrush;
                if (fbrush != null)
                {
                    Ray3F rayw = vc.GetWorldRay(scrPt);
                    TerrainGob.RayPickRetVal retval;
                    if (terrain.RayPick(rayw, out retval))
                    {
                        Point     pt = terrain.WorldToSurfaceSpace(retval.hitpos);
                        ImageData hm = terrain.GetSurface();
                        fbrush.Height = hm.GetPixelFloat(pt.X, pt.Y);
                    }
                }
                return(true);
            }
            return(false);
        }
Пример #15
0
        public static TerrainGob Create(string name, string hmPath, float cellSize)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(name);
            }

            if (!File.Exists(hmPath))
            {
                throw new ArgumentException(hmPath + " does not exist");
            }

            Uri     ur          = new Uri(hmPath);
            DomNode terrainNode = new DomNode(Schema.terrainGobType.Type);

            terrainNode.SetAttribute(Schema.terrainGobType.cellSizeAttribute, cellSize);
            terrainNode.SetAttribute(Schema.terrainGobType.heightMapAttribute, ur);
            terrainNode.InitializeExtensions();
            TerrainGob terrain = terrainNode.As <TerrainGob>();

            terrain.Name = name;
            return(terrain);
        }
Пример #16
0
        public void PopulatedTerrainCmbox()
        {
            IGameDocumentRegistry gameDocumentRegistry = Globals.MEFContainer.GetExportedValue <IGameDocumentRegistry>();

            IEnumerable <TerrainGob> terrainGobs = EmptyEnumerable <TerrainGob> .Instance;

            if (gameDocumentRegistry != null)
            {
                terrainGobs = gameDocumentRegistry.FindAll <TerrainGob>();
            }

            TerrainGob curTerrain = m_cmboxTerrain.Items.Count > 0 ? (TerrainGob)m_cmboxTerrain.SelectedItem : null;

            m_cmboxTerrain.Enabled = false;

            m_cmboxTerrain.BeginUpdate();
            m_cmboxTerrain.Items.Clear();
            foreach (var terrain in terrainGobs)
            {
                m_cmboxTerrain.Items.Add(terrain);
            }
            m_cmboxTerrain.EndUpdate();
            if (m_cmboxTerrain.Items.Count > 0)
            {
                m_cmboxTerrain.Enabled = true;
                if (curTerrain != null && m_cmboxTerrain.Items.Contains(curTerrain))
                {
                    m_cmboxTerrain.SelectedItem = curTerrain;
                }
                else
                {
                    m_cmboxTerrain.SelectedIndex = 0;
                }
            }
            ReBind();
        }
Пример #17
0
        private void AddBtnClick(object sender, EventArgs e)
        {
            TerrainGob gob     = (TerrainGob)m_cmboxTerrain.SelectedItem;
            ListBox    listbox = GetActiveList();

            if (gob == null)
            {
                return;
            }
            ImageData hmImg = gob.GetSurface();

            using (var dlg = new CreateTerrainMapDlg(Globals.ResourceRoot.LocalPath, hmImg.Width, hmImg.Height))
            {
                if (dlg.ShowDialog(this) == DialogResult.OK)
                {
                    if (listbox == m_layerList)
                    {
                        LayerMap map = LayerMap.Create(new Uri(dlg.Mask));
                        map.Name = dlg.MapName;
                        GameContext.As <ITransactionContext>().DoTransaction(() =>
                        {
                            gob.LayerMaps.Add(map);
                        }, "Add LayerMap");
                    }
                    else if (listbox == m_decoList)
                    {
                        DecorationMap map = DecorationMap.Create(new Uri(dlg.Mask));
                        map.Name = dlg.MapName;
                        GameContext.As <ITransactionContext>().DoTransaction(() =>
                        {
                            gob.DecorationMaps.Add(map);
                        }, "Add DecorationMap");
                    }
                }
            }
        }
Пример #18
0
        public override void Apply(ITerrainSurface target, int x, int y, out TerrainOp op)
        {
            //todo: optimze this function.

            op = null;
            if (!CanApplyTo(target))
            {
                return;
            }
            TerrainGob terrain = target.As <TerrainGob>();
            ImageData  hmImg   = terrain.GetSurface();
            Bound2di   outRect;

            ComputeBound(hmImg, x, y, out outRect);
            if (!outRect.isValid || hmImg.Format != ImageDataFORMAT.R32_FLOAT)
            {
                return;
            }
            op = new TerrainOp(target, outRect);


            Func <int, int, float> getPixel = (px, py) =>
            {
                px = MathUtil.Clamp(px, 0, hmImg.Width - 1);
                py = MathUtil.Clamp(py, 0, hmImg.Height - 1);
                float pixel = *(float *)hmImg.GetPixel(px, py);
                return(pixel);
            };

            Func <int, int, float> getSmoothPixel = (px, py) =>
            {
                float r1 = getPixel(px - 1, py - 1) + 2.0f * getPixel(px, py - 1) + getPixel(px + 1, py - 1);
                float r2 = 2.0f * getPixel(px - 1, py) + 4.0f * getPixel(px, py) + 2.0f * getPixel(px + 1, py);
                float r3 = getPixel(px - 1, py + 1) + 2.0f * getPixel(px, py + 1) + getPixel(px + 1, py + 1);
                return((r1 + r2 + r3) / 16.0f);
            };

            m_templist.Clear();

            for (int cy = outRect.y1; cy < outRect.y2; cy++)
            {
                for (int cx = outRect.x1; cx < outRect.x2; cx++)
                {
                    m_templist.Add(getSmoothPixel(cx, cy));
                }
            }

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

            int k    = 0;
            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;

                    float  scrPixel  = m_templist[k++];
                    float  lerp      = Kernel[size * by + bx];
                    float *destPixel = (float *)hmImg.GetPixel(cx, cy);
                    *      destPixel = Sce.Atf.MathUtil.Interp(lerp, *destPixel, scrPixel);
                }
            }
            terrain.ApplyDirtyRegion(outRect);
        }
Пример #19
0
 private void Bind(TerrainGob gob)
 {            
     m_terrainPropGrid.Bind(gob);
     m_layerList.Items.Clear();
     m_decoList.Items.Clear();
     if (gob != null)
     {
         foreach (var layer in gob.LayerMaps)
             m_layerList.Items.Add(layer);
         foreach (var deco in gob.DecorationMaps)
             m_decoList.Items.Add(deco);
     }
     m_propertyGrid.Bind(null);
 }