/// <summary>Set the centre of the visible range to client rect relative coord 'y'</summary> private void CentreVisible(int y) { var vis = VisibleRange; vis.Mid = ZoomedRange.Beg + (long)(Math_.Frac(0, y, Height) * ZoomedRange.Size); VisibleRange = vis; }
/// <summary>Create the rectangle describing the thumb position</summary> private Rectangle MakeThumbRect(Rectangle bounds) { var height = bounds.Height; var thm = ThumbRange; var total = TotalRange; int sy = (int)(Math_.Frac(total.Beg, thm.Beg, total.End) * bounds.Height); int ey = (int)(Math_.Frac(total.Beg, thm.End, total.End) * bounds.Height); var r = new Rectangle(bounds.X, bounds.Y + sy, bounds.Width, ey - sy); // Apply the minimum thumb size constraint if (r.Height < MinThumbSize) { int diff = MinThumbSize - r.Height; r.Height += diff; r.Y -= diff / 2; if (r.Top < 0) { r.Y = 0; } if (r.Bottom > height) { r.Y = height - r.Height; } } return(r); }
/// <summary>Determine the density limits</summary> private void CalculateDensity(GameConstants consts, Random rnd) { // Factors influency density: // -Bigger molar mass = higher density // -Stronger bonds = higher density // -More Ionic = higher density // -Smaller atomic radii = higher density var norm_ionicity = Math_.Frac(consts.MinElectronegativity, Ionicity, consts.MaxElectronegativity); var mass_adj = Math_.Frac(1, MolarMass / (Count1 + Count2), consts.MaxMolarMass); // normalised average molar mass var ionicity_adj = 1.0 + 0.5 * Math.Pow(norm_ionicity, 8); // no significant ionicity until about 0.7 //var bond_adj = dominant_bond.Strength; //var radii = Elem1.ValenceOrbitalRadius + Elem2.ValenceOrbitalRadius; var scaler = mass_adj * ionicity_adj; Debug.Assert(scaler >= 0 && scaler < 10); m_solid_density = Math_.Lerp(consts.MinSolidMaterialDensity, consts.MaxSolidMaterialDensity, scaler); // The liquid_density0 value can only be lower than the solid density when // the Compound is strongly ionic such that it forms a crystals var ionicity_solid_density_scaler = 0.1 * Math_.Max(0, norm_ionicity - 0.75); m_liquid_density0 = m_solid_density * rnd.Double(1.0, 1.0 - ionicity_solid_density_scaler); m_liquid_density1 = m_liquid_density0 * rnd.DoubleC(0.8, 0.0); // at boiling point, density is roughly 20% less }
public ToolFormUI(Control owner) : base(owner, EPin.TopRight) { InitializeComponent(); AutoFade = true; m_combo_pin_to.Items.AddRange(Enum <EPin> .Values.Cast <object>().ToArray()); m_combo_pin_to.SelectedIndexChanged += (s, a) => { Pin = (EPin)m_combo_pin_to.SelectedItem; }; PopulateChildCombo(owner.TopLevelControl); m_combo_child.DisplayMember = "Name"; m_combo_child.SelectedIndexChanged += (s, a) => { PinTarget = (Control)m_combo_child.SelectedItem; }; m_track_autofade.Value = m_track_autofade.Maximum; m_track_autofade.ValueChanged += (s, a) => { FadeRange = new RangeF(Math_.Frac(m_track_autofade.Minimum, m_track_autofade.Value, m_track_autofade.Maximum), 1f); }; m_cb_form_border.Items.AddRange(Enum <FormBorderStyle> .ValuesArray); m_cb_form_border.SelectedIndexChanged += (s, a) => { FormBorderStyle = (FormBorderStyle)m_cb_form_border.SelectedItem; }; }
public Candle SubCandle(DateTimeOffset now, ETimeFrame time_frame) { // Interpolate the latest candle to determine the spot price var t = Math_.Frac(Timestamp, now.Ticks, Timestamp + Misc.TimeFrameToTicks(1.0, time_frame)); return(SubCandle(Math_.Clamp(t, 0.0, 1.0))); }
/// <summary>Set the thumb position given a control space Y value</summary> private void ScrollThumbPos(int y) { RangeI thm = ThumbRange; thm.Mid = TotalRange.Beg + (long)(Math_.Frac(0, y, Height) * TotalRange.Size); ThumbRange = thm; }
/// <summary>Searches the file from 'start' looking for a match to 'pat'</summary> /// <returns>Returns true if a match is found, false otherwise. If true /// is returned 'found' contains the file byte offset of the first match</returns> private bool Find(Pattern pat, long start, bool backward, out long found) { long at = -1; DialogResult res = DialogResult.Cancel; try { var body = backward ? (start == FileByteRange.End ? "Searching backward from the end of the file..." : "Searching backward from the current selection position...") : (start == FileByteRange.Beg ? "Searching forward from the start of the file..." : "Searching forward from the current selection position..."); // Although this search runs in a background thread, it's wrapped in a modal // dialog box, so it should be ok to use class members directly var search = new ProgressForm("Searching...", body, null, ProgressBarStyle.Marquee, (s, a, cb) => { var d = new BLIData(this, Src, fileend_: m_fileend); int last_progress = 0; d.progress = (scanned, length) => { int progress = (int)(100 * Math_.Frac(0, scanned, length != 0?length:1)); if (progress != last_progress) { cb(new ProgressForm.UserState { FractionComplete = progress * 0.01f }); last_progress = progress; } return(!s.CancelPending); }; // Searching.... DoFind(pat, start, backward, d, rng => { at = rng.Beg; return(false); }); // We can call BuildLineIndex in this thread context because we know // we're in a modal dialog. if (at != -1 && !s.CancelPending) { this.BeginInvoke(() => SelectRowByAddr(at)); } }) { StartPosition = FormStartPosition.CenterParent }; using (search) res = search.ShowDialog(this, 500); } catch (OperationCanceledException) {} catch (Exception ex) { Misc.ShowMessage(this, "Find terminated by an error.", "Find error", MessageBoxIcon.Error, ex); } found = at; return(res == DialogResult.OK); }
/// <summary>Searches the entire file and bookmarks all locations that match the find pattern</summary> private void FindBookmarkAll() { if (!PreFind()) { return; } try { Log.Write(ELogLevel.Info, "FindBookmarkAll"); var pat = FindUI.Pattern; const string body = "Bookmarking all found instances..."; // Although this search runs in a background thread, it's wrapped in a modal // dialog box, so it should be ok to use class members directly var search = new ProgressForm("Searching...", body, null, ProgressBarStyle.Marquee, (s, a, cb) => { var d = new BLIData(this, Src, fileend_: m_fileend); int last_progress = 0; d.progress = (scanned, length) => { int progress = (int)(100 * Math_.Frac(0, scanned, length != 0?length:1)); if (progress != last_progress) { cb(new ProgressForm.UserState { FractionComplete = progress * 0.01f }); last_progress = progress; } return(!s.CancelPending); }; // Searching.... DoFind(pat, 0, false, d, rng => { this.BeginInvoke(() => SetBookmark(rng, Bit.EState.Set)); return(true); }); }) { StartPosition = FormStartPosition.CenterParent }; using (search) search.ShowDialog(this, 500); } catch (OperationCanceledException) {} catch (Exception ex) { Misc.ShowMessage(this, "Find terminated by an error.", "Find error", MessageBoxIcon.Error, ex); } }
/// <summary>Paint the progress bar</summary> protected override void OnPaint(PaintEventArgs e) { var gfx = e.Graphics; var rect = ClientRectangle; var visual_styles_enabled = ProgressBarRenderer.IsSupported; // Draw the background if (visual_styles_enabled) { ProgressBarRenderer.DrawHorizontalBar(gfx, rect); } else { gfx.FillRectangle(SystemBrushes.Control, rect); gfx.DrawRectangle(SystemPens.ControlDark, rect); } // Fill with chunks if (Value != Minimum) { const int pad = 3; var frac = Math_.Frac(Minimum, Value, Maximum); var clip = new Rectangle(rect.X + pad, rect.Y + pad, (int)(frac * (rect.Width - 2 * pad)), rect.Height - 2 * pad); if (visual_styles_enabled) { ProgressBarRenderer.DrawHorizontalChunks(gfx, clip); } else { gfx.FillRectangle(SystemBrushes.Highlight, clip); } } // Draw the text using (var b = new SolidBrush(ForeColor)) { if (TextL.Text.HasValue()) { var sz = gfx.MeasureString(TextL.Text, TextL.Font); gfx.DrawString(TextL.Text, TextL.Font, b, new PointF(rect.Left + (RightToLeftLayout ? rect.Width - sz.Width : 0), rect.Top + (rect.Height - sz.Height) / 2f)); } if (TextC.Text.HasValue()) { var sz = gfx.MeasureString(TextC.Text, TextC.Font); gfx.DrawString(TextC.Text, TextC.Font, b, new PointF(rect.Left + (rect.Width - sz.Width) / 2, rect.Top + (rect.Height - sz.Height) / 2f)); } if (TextR.Text.HasValue()) { var sz = gfx.MeasureString(TextR.Text, TextR.Font); gfx.DrawString(TextR.Text, TextR.Font, b, new PointF(rect.Left + (RightToLeftLayout ? 0 : rect.Width - sz.Width), rect.Top + (rect.Height - sz.Height) / 2f)); } } }
/// <summary>Set the alpha based on point x,y relative to the VSlider</summary> private void SelectAlpha(int x, int y) { var dim = LayoutDimensions; if (dim.Empty || dim.ASlider.IsEmpty) { return; } var a = VerticalLayout ? Math_.Clamp(Math_.Frac(dim.ASlider.Left, x, dim.ASlider.Right), 0.0, 1.0) : Math_.Clamp(Math_.Frac(dim.ASlider.Bottom, y, dim.ASlider.Top), 0.0, 1.0); HSVColour = HSV.FromAHSV((float)a, HSVColour.H, HSVColour.S, HSVColour.V); }
/// <summary>Set the brightness based on point x,y relative to the VSlider</summary> private void SelectBrightness(int x, int y) { var dim = LayoutDimensions; if (dim.Empty || dim.VSlider.IsEmpty) { return; } var v = VerticalLayout ? Math_.Clamp(Math_.Frac(dim.VSlider.Left, x, dim.VSlider.Right), 0.0, 1.0) : Math_.Clamp(Math_.Frac(dim.VSlider.Bottom, y, dim.VSlider.Top), 0.0, 1.0); HSVColour = HSV.FromAHSV(HSVColour.A, HSVColour.H, HSVColour.S, (float)v); }
/// <summary>Returns the age of this candle as a normalised fraction of the time frame</summary> public double Age(Instrument instr) { var one = Misc.TimeFrameToTicks(1.0, instr.TimeFrame); var age_ticks = Model.UtcNow.Ticks - Timestamp; if (age_ticks < 0) { return(0.0); } if (age_ticks > one) { return(1.0); } return(Math_.Frac(0.0, age_ticks, one)); }
/// <summary>The density of the Compound at the given temperature/pressure (kg/m³)</summary> public double Density(double temperature, double pressure) { switch (Phase(temperature, pressure)) { default: throw new ArgumentException(); case EPhase.Gas: return(0.0); case EPhase.Solid: return(m_solid_density); case EPhase.Liquid: var f = Math_.Frac(MeltingPoint, temperature, BoilingPoint); return(Math_.Lerp(m_liquid_density0, m_liquid_density1, f)); } }
/// <summary> /// Export the file 'filepath' using current filters to the stream 'outp'. /// Note: this method throws if an exception occurs in the background thread.</summary> /// <param name="d">A copy of the data needed to do the export</param> /// <param name="ranges">Byte ranges within 'filepath' to be exported</param> /// <param name="row_delimiter">The delimiter that defines rows (robitised)</param> /// <param name="col_delimiter">The delimiter that defines columns (robitised)</param> /// <param name="outp">The stream to write the exported file to</param> private bool DoExportWithProgress(BLIData d, IEnumerable <RangeI> ranges, string row_delimiter, string col_delimiter, StreamWriter outp) { DialogResult res = DialogResult.Cancel; try { // Although this search runs in a background thread, it's wrapped in a modal // dialog box, so it should be ok to use class members directly var export = new ProgressForm("Exporting...", null, null, ProgressBarStyle.Continuous, (s, a, cb) => { // Report progress and test for cancel int last_progress = -1; d.progress = (scanned, length) => { int progress = (int)(100 * Math_.Frac(0, scanned, length != 0?length:1)); if (progress != last_progress) { cb(new ProgressForm.UserState { FractionComplete = progress * 0.01f }); last_progress = progress; } return(!s.CancelPending); }; // Do the export DoExport(d, ranges, row_delimiter, col_delimiter, outp); }) { StartPosition = FormStartPosition.CenterParent }; using (export) res = export.ShowDialog(this); } catch (OperationCanceledException) { } catch (Exception ex) { Misc.ShowMessage(this, "Exporting terminated due to an error.", "Export error", MessageBoxIcon.Error, ex); } return(res == DialogResult.OK); }
public Element(int atomic_number, GameConstants consts) { Debug.Assert(atomic_number > 0 && atomic_number <= consts.ElementCount, ""); var rnd = new Random(consts.GameSeed + atomic_number); Seed = rnd.Next(); AtomicNumber = atomic_number; Name = GameConstants.ElementNames[AtomicNumber - 1]; MolarMass = AtomicNumber != 1 ? 2 * AtomicNumber * rnd.DoubleC(1.01, 0.1) : 1; // Roughly double the atomic number Period = CalcPeriod(consts, atomic_number); TableX = Math_.Frac(consts.ValenceLevels[Period - 1] + 1, atomic_number, consts.ValenceLevels[Period]); TableY = Math_.Frac(1.0, Period, 7.0); CalcValency(consts); CalcElectronegativity(consts); CalcSolidDensity(consts); CalcPhaseChangeTemperatures(consts); KnownProperties = EElemProp.Name; // All elements start with a name KnownProperties |= EElemProp.AtomicNumber; //hack Debug.Assert(ValenceElectrons >= 0 && ValenceElectrons <= consts.ValenceLevels[Period] - consts.ValenceLevels[Period - 1], ""); Debug.Assert(ValenceHoles >= 0 && ValenceHoles <= consts.ValenceLevels[Period] - consts.ValenceLevels[Period - 1], ""); }
protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); // Find the bounding area of the control var bounds = ClientRectangle; bounds.Inflate(-1, -1); if (bounds.Width <= 0 || bounds.Height <= 0) { return; } var gfx = e.Graphics; var total = ZoomedRange; gfx.SmoothingMode = SmoothingMode.AntiAlias; Color c0, c1; Point pt0 = new Point(bounds.Left, 0); Point pt1 = new Point(bounds.Right, 0); // Background c0 = Gfx_.Blend(Color.Black, TrackColor, 0.7f); c1 = TrackColor; using (var bsh = new LinearGradientBrush(pt0, pt1, c0, c1)) // This throws OutOfMemoryException if pt0 == pt1.. ffs MS.. gfx.FillRectangle(bsh, bounds); // Draw the overlay image if (Overlay != null) { var r = new Rectangle(bounds.X, bounds.Y, bounds.Width, bounds.Height); var s = new Rectangle(0, 0, Overlay.Width, Overlay.Height); if (OverlayAttributes == null) { gfx.DrawImage(Overlay, r, s, GraphicsUnit.Pixel); } else { gfx.DrawImage(Overlay, r, s, GraphicsUnit.Pixel, OverlayAttributes); } } // Draw the visible range if (bounds.Width > 2) { int sy = (int)(Math_.Frac(total.Beg, VisibleRange.Beg, total.End) * bounds.Height); int ey = (int)(Math_.Frac(total.Beg, VisibleRange.End, total.End) * bounds.Height); var r = new Rectangle(bounds.X + 1, bounds.Y + sy, bounds.Width - 2, ey - sy); using (var bsh = new SolidBrush(VisibleRangeColor)) { gfx.FillRectangle(bsh, r); } if (bounds.Width > 4) { using (var pen = new Pen(VisibleRangeBorderColor)) { gfx.DrawLine(pen, r.Left + 1, r.Top, r.Right - 1, r.Top); gfx.DrawLine(pen, r.Left + 1, r.Bottom, r.Right - 1, r.Bottom); } } } // Borders gfx.DrawRectangleRounded(SystemPens.ControlDarkDark, bounds, m_corner_radius); }
protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); var bounds = ClientRectangle; bounds.Inflate(-1, -1); if (bounds.Width <= 0 || bounds.Height <= 0) { return; } var gfx = e.Graphics; var total = TotalRange; gfx.SmoothingMode = SmoothingMode.AntiAlias; Color c0, c1; Point pt0 = new Point(bounds.Left, 0); Point pt1 = new Point(bounds.Right, 0); // Create rectangles for the highlight ranges foreach (var r in m_indicator_ranges) { int sy = (int)(Math_.Frac(total.Beg, r.Range.Beg, total.End) * bounds.Height); int ey = (int)(Math_.Frac(total.Beg, r.Range.End, total.End) * bounds.Height); r.m_rect = new Rectangle(bounds.X, bounds.Y + sy, bounds.Width, Math.Max(ey - sy, 1)); } // Background c0 = Gfx_.Blend(Color.Black, TrackColor, 0.8f); c1 = TrackColor; using (var bsh = new LinearGradientBrush(pt0, pt1, c0, c1)) gfx.FillRectangle(bsh, bounds); // Draw the overlay image if (Overlay != null) { var r = new Rectangle(bounds.X, bounds.Y, bounds.Width, bounds.Height); var s = new Rectangle(0, 0, Overlay.Width, Overlay.Height); if (OverlayAttributes == null) { gfx.DrawImage(Overlay, r, s, GraphicsUnit.Pixel); } else { gfx.DrawImage(Overlay, r, s, GraphicsUnit.Pixel, OverlayAttributes); } } // Indicator ranges foreach (var r in m_indicator_ranges) { c0 = r.Color; c1 = Gfx_.Blend(c0, Color.FromArgb(c0.A, Color.White), 0.2f); using (var bsh = new LinearGradientBrush(pt0, pt1, c0, c1)) gfx.FillRectangle(bsh, r.m_rect); } // Thumb { var r = MakeThumbRect(bounds); c0 = Gfx_.Blend(Color.White, Color.FromArgb(0x80, ThumbColor), 0.8f); c1 = ThumbColor; using (var bsh = new LinearGradientBrush(pt0, pt1, c0, c1)) gfx.FillRectangle(bsh, r); if (r.Width > 2) { using (var pen = new Pen(ThumbColor)) { gfx.DrawLine(pen, r.Left + 1, r.Top, r.Right - 1, r.Top); gfx.DrawLine(pen, r.Left + 1, r.Bottom, r.Right - 1, r.Bottom); } } } // Borders gfx.DrawRectangleRounded(SystemPens.ControlDarkDark, bounds, m_corner_radius); }