Пример #1
0
        public void Init(MaterialProperties props, float mass, float temperature)
        {
            var oldMtl = Material;

            Material      = props;
            Mass          = mass;
            _heatCapacity = props.SpecificHeat * mass;
            Temperature   = temperature;
            _lastUpdate   = null;
            MaterialChanged?.Invoke(oldMtl, Material);
            OverheatDamageMultiplier = 1;
        }
Пример #2
0
        /// <summary>
        /// Adds the specified substance to this substance. Both substances must be of the same material type, or an exception will fire.
        /// </summary>
        /// <param name="substance">The substance.</param>
        /// <exception cref="WriteProtectionViolationException">Fired if there is a write lock on the substance.</exception>
        /// <exception cref="ApplicationException">Fired if the substances are not of the same material type.</exception>
        public void Add(Substance substance)
        {
            //if ( substance.Name.Equals("Water") ) System.Diagnostics.Debugger.Break();
            if (!m_writeLock.IsWritable)
            {
                throw new WriteProtectionViolationException(this, m_writeLock);
            }
            if (!m_type.Equals(substance.m_type))
            {
                throw new ApplicationException("Substance " + Name + " being added to substance " + substance.Name + "!");
            }
            double oldEnergy = Energy;

            m_mass += substance.Mass; // Have to set Mass before setting energy, since energy->temp requires mass.
            Energy  = oldEnergy + substance.Energy;

            if (substance.m_materialSpecs != null)
            {
                // If the incoming has materialSpecs and we don't, then we must create them.
                if (m_materialSpecs == null)
                {
                    m_materialSpecs = new Hashtable();
                }
                foreach (DictionaryEntry de in substance.m_materialSpecs)
                {
                    if (!m_materialSpecs.ContainsKey(de.Key))
                    {
                        m_materialSpecs.Add(de.Key, 0.0);
                    }
                }

                foreach (DictionaryEntry de in substance.m_materialSpecs)
                {
                    if (m_materialSpecs.Contains(de.Key))
                    {
                        double old = (double)m_materialSpecs[de.Key];
                        m_materialSpecs.Remove(de.Key);
                        m_materialSpecs.Add(de.Key, (old + (double)de.Value));
                    }
                    else
                    {
                        if (!Guid.Empty.Equals(de.Key))
                        {
                            m_materialSpecs.Add(de.Key, de.Value);
                        }
                    }
                }
            }

            m_ssh.ReportChange();
            MaterialChanged?.Invoke(this, MaterialChangeType.Contents);
        }
Пример #3
0
        public ItemMaterialButton(PrinterConfig printer, ThemeConfig theme, int initialMaterialIndex)
        {
            this.currentIndex = initialMaterialIndex;
            this.ToolTipText  = "Material".Localize();
            var scaledButtonSize = 24 * GuiWidget.DeviceScale;

            Width  = 30 * GuiWidget.DeviceScale;
            Height = 30 * GuiWidget.DeviceScale;

            var menuTheme = AppContext.MenuTheme;

            this.Name = "ItemMaterialButton";

            this.DynamicPopupContent = () =>
            {
                var materialControl = new MaterialControls(printer, menuTheme, currentIndex)
                {
                    Padding         = theme.DefaultContainerPadding,
                    BackgroundColor = menuTheme.BackgroundColor,
                    HAnchor         = HAnchor.Fit,
                    VAnchor         = VAnchor.Fit,
                };

                materialControl.IndexChanged += (s, e) =>
                {
                    currentIndex = e;
                    MaterialChanged?.Invoke(this, e);
                    materialColorButton.BackgroundColor = MaterialRendering.Color(printer, currentIndex, theme.BorderColor);
                };

                return(materialControl);
            };

            materialColorButton = new ColorButton(MaterialRendering.Color(printer, currentIndex, theme.BorderColor))
            {
                Width                  = scaledButtonSize,
                Height                 = scaledButtonSize,
                BackgroundRadius       = scaledButtonSize / 2,
                BackgroundOutlineWidth = 1,
                HAnchor                = HAnchor.Center,
                VAnchor                = VAnchor.Center,
                DisabledColor          = theme.MinimalShade,
                BorderColor            = theme.TextColor,
                Selectable             = false,
            };

            this.AddChild(materialColorButton);
        }
Пример #4
0
        /// <summary>
        /// Removes the specified substance from this substance. Both substances must be of the same material type, or an exception will fire..
        /// </summary>
        /// <param name="substance">The substance.</param>
        /// <returns>Substance.</returns>
        /// <exception cref="ApplicationException">Fired if the substances are not of the same material type.</exception>
        /// <exception cref="WriteProtectionViolationException">Fired if there is a write lock on the substance.</exception>
        public virtual Substance Remove(Substance substance)
        {
            if (!m_type.Equals(substance.m_type))
            {
                throw new ApplicationException("Substance " + Name + " being removed from substance " + substance.Name + "!");
            }
            if (!m_writeLock.IsWritable)
            {
                throw new WriteProtectionViolationException(this, m_writeLock);
            }
            // Trace.Write("Removing " + substance.Mass + " kg of " + substance.Name + " from an existing " + m_mass + " kg, ");
            // TODO: Assert we are not removing more than is there.
            double    mass = Math.Min(substance.Mass, m_mass);
            Substance s    = Remove(mass);

            // _Debug.WriteLine("leaving " + m_mass + " kg.");
            m_ssh.ReportChange();
            MaterialChanged?.Invoke(this, MaterialChangeType.Contents);
            return(s);
        }
Пример #5
0
        internal void SetTempInKelvin(double kelvin)
        {
            double temp = Temp;

            if (!m_writeLock.IsWritable)
            {
                throw new WriteProtectionViolationException(this, m_writeLock);
            }
            Temp = kelvin;
            if (temp != Temp)
            {
#if DEBUG
                if (s_breakOnIsNaNTemp && double.IsNaN(Temp))
                {
                    System.Diagnostics.Debugger.Break();
                }
#endif

                m_ssh.ReportChange();
                MaterialChanged?.Invoke(this, MaterialChangeType.Temperature);
            }
        }
Пример #6
0
        public ItemMaterialButton(ThemeConfig theme, int initialMaterialIndex)
        {
            this.ToolTipText = "Material".Localize();
            var scaledButtonSize = 14 * GuiWidget.DeviceScale;

            Width  = 30 * GuiWidget.DeviceScale;
            Height = 30 * GuiWidget.DeviceScale;

            this.DynamicPopupContent = () =>
            {
                var materialControl = new MaterialControls(theme, initialMaterialIndex)
                {
                    Padding         = theme.DefaultContainerPadding,
                    BackgroundColor = this.HoverColor,
                    HAnchor         = HAnchor.Fit,
                    VAnchor         = VAnchor.Fit,
                };

                materialControl.IndexChanged += (s, e) =>
                {
                    MaterialChanged?.Invoke(this, e);
                };

                return(materialControl);
            };

            materialColorButton = new ColorButton(MaterialRendering.Color(initialMaterialIndex, theme.BorderColor))
            {
                Width         = scaledButtonSize,
                Height        = scaledButtonSize,
                HAnchor       = HAnchor.Center,
                VAnchor       = VAnchor.Center,
                DrawGrid      = true,
                DisabledColor = theme.MinimalShade,
                Selectable    = false,
            };

            this.AddChild(materialColorButton);
        }
Пример #7
0
        /// <summary>
        /// Removes the specified mass from this substance.
        /// </summary>
        /// <param name="mass">The mass.</param>
        /// <returns>Substance.</returns>
        /// <exception cref="WriteProtectionViolationException">Fired if there is a write lock on the substance.</exception>
        public virtual Substance Remove(double mass)
        {
            if (!m_writeLock.IsWritable)
            {
                throw new WriteProtectionViolationException(this, m_writeLock);
            }

            Substance s = (Substance)MaterialType.CreateMass(mass, Temperature);

            if (m_mass == float.MaxValue)
            {
                if (m_materialSpecs != null)
                {
                    // First figure out the total mass...
                    double totalMass = m_materialSpecs.Cast <DictionaryEntry>().Sum(de => (double)de.Value);

                    if (totalMass > 0.0)
                    {
                        foreach (DictionaryEntry de in m_materialSpecs)
                        {
                            if (s.m_materialSpecs == null)
                            {
                                s.m_materialSpecs = new Hashtable();
                            }
                            s.m_materialSpecs.Add(de.Key, mass * ((double)de.Value) / totalMass);
                        }
                    }
                }
                return(s);
            }
            else
            {
                double    pctRemoved = mass / m_mass;
                Hashtable ht         = new Hashtable();
                if (m_materialSpecs != null)
                {
                    foreach (DictionaryEntry de in m_materialSpecs)
                    {
                        if (s.m_materialSpecs == null)
                        {
                            s.m_materialSpecs = new Hashtable();
                        }
                        if (s.m_materialSpecs.ContainsKey(de.Key))
                        {
                            s.m_materialSpecs[de.Key] = pctRemoved * ((double)de.Value);
                        }
                        else
                        {
                            s.m_materialSpecs.Add(de.Key, pctRemoved * ((double)de.Value));
                        }
                        if (pctRemoved < 1.0)
                        {
                            ht.Add(de.Key, (1.0 - pctRemoved) * ((double)de.Value));
                        }
                    }
                }
                m_materialSpecs = ht;
            }

            double have  = Energy;
            double minus = s.Energy;

            m_mass -= s.Mass; // Have to set Mass before setting energy, since energy->temp requires mass.
            Energy  = (have - minus);

            m_ssh.ReportChange();
            MaterialChanged?.Invoke(this, MaterialChangeType.Contents);
            return(s);
        }
Пример #8
0
 public virtual void OnMaterialChanged(object sender, MaterialChangedEventArgs e)
 {
     MaterialChanged?.Invoke(sender, e);
 }