/// <summary> /// SEを再生します。 /// </summary> public ISoundObjectBackend Play(string filename, double volume) { if (engine == null) { return(null); } if (string.IsNullOrEmpty(filename)) { return(null); } var source = GetSoundSource(filename); if (source == null) { throw new InvalidDataException( "音声ファイルの読み込みに失敗しました。"); } // 再生 var sound = engine.Play2D(source, false, true, false); if (sound == null) { throw new InvalidOperationException( "音声ファイルの再生に失敗しました。"); } // 音量を設定します。 sound.Volume = MathEx.Between(0.0f, 1.0f, (float)(engine.SoundVolume * volume)); sound.Paused = false; return(new SoundObjectBackend_IrrKlang(sound)); }
/// <summary> /// エフェクトを追加します。 /// </summary> private void AddEffect(EffectObject effect, Square square) { if (effect == null) { return; } // 効果音を調整します。 if (!IsUseEffectSound) { effect.StartSoundPath = null; effect.StartSoundVolume = 0.0; } else { var percent = EffectVolume; effect.StartSoundVolume *= MathEx.Between(0, 100, percent) / 100.0; } WPFUtil.UIProcess(() => { effect.DataContext = CreateContext(square); Container.AddEffect(effect); }); }
/// <summary> /// コンストラクタ /// </summary> public MainViewModel() { VariationList = new NotifyCollection <VariationInfo>(); MoveList = new NotifyCollection <CsaMove>(); if (string.IsNullOrEmpty(Name)) { Name = "meijin_" + MathEx.RandInt(0, 1000); } ServerPort = "4084"; ThreadNumMaximum = Global.GetCpuThreadNum(); if (ThreadNum == 0) { ThreadNum = Math.Max(1, ThreadNumMaximum - 2); } var rawMemSizeList = GetMemorySizeList().ToList(); MemSizeList = rawMemSizeList.Take(7).ToList(); if (HashMemSize == 0) { var index = MathEx.Between(0, 6, rawMemSizeList.Count - 2); HashMemSize = rawMemSizeList[index].Item1; } this.AddDependModel(Global.Settings); }
/// <summary> /// OpenGLのClearColorを更新します。 /// </summary> private void OnBackColorChanged(object sender, EventArgs e) { GLWrap.Wrap(() => GL.ClearColor( MathEx.Between(0.0f, 1.0f, BackColor.R / 255.0f), MathEx.Between(0.0f, 1.0f, BackColor.G / 255.0f), MathEx.Between(0.0f, 1.0f, BackColor.B / 255.0f), MathEx.Between(0.0f, 1.0f, BackColor.A / 255.0f))); }
/// <summary> /// バッファの先頭オフセットを進めます。 /// </summary> public void Increment(int inc) { inc = MathEx.Between(-Offset, Count, inc); // 先頭オフセットを移動し、バッファサイズは減らします。 Offset += inc; Count -= inc; }
/// <summary> /// 色に係数を掛け、新たな色を作成します。 /// </summary> public static Color4b Multiply(Color4b color, double coefficient) { return(Color4b.FromArgb( MathEx.Between(0, 255, (int)(color.A * coefficient)), MathEx.Between(0, 255, (int)(color.R * coefficient)), MathEx.Between(0, 255, (int)(color.G * coefficient)), MathEx.Between(0, 255, (int)(color.B * coefficient)))); }
/// <summary> /// 指し手を表示する矢印メッシュを追加します。 /// </summary> private void AddRenderMoveArrow(RenderBuffer renderBuffer, MoveArrowInfo info, int priority, string label) { var move = info.Move; if (move == null || !move.Validate()) { return; } // 手番が違う場合は、前に設定された指し手が残っている可能性がある。 if (move.BWType != Board.Turn || move.IsSpecialMove) { return; } // 駒の移動を開始した地点と終了した地点の座標を求めます。 var fromPoint = ( move.ActionType == ActionType.Drop ? HandPieceToPoint(move.DropPieceType, move.BWType) : SquareToPoint(move.SrcSquare)).ToPointd(); var toPoint = SquareToPoint(move.DstSquare).ToPointd(); var diff = toPoint - fromPoint; // 優先順位の高い矢印ほど、小さくなる値 var priorityRate = MathEx.Between(0.0, 1.0, 1.0 / 3.0 * (priority - 1)); // 矢印を決められた位置に描画します。 AddRenderArrow(renderBuffer, fromPoint, toPoint, priorityRate, info.Color); // ラベルを描画 if (!string.IsNullOrEmpty(label)) { var font = new TextTextureFont { Font = new Font(TextTextureFont.DefaultFont, FontStyle.Bold), Color = Color.White, EdgeColor = Color.Black, EdgeLength = 4, IsStretchSize = true, }; var rect = new RectangleF( (float)(fromPoint.X + diff.X * 1), (float)(fromPoint.Y + diff.Y * 1), SquareSize.Width / 3, SquareSize.Height / 3); rect.Offset(-rect.Width / 2, -rect.Height / 2); AddRenderText( renderBuffer, label, font, rect, ShogiZOrder.PreEffectZ2 - priorityRate); } }
/// <summary> /// 変更中のエフェクトの不透明度を計算します。 /// </summary> private double GetEffectOpacity(TimeSpan progress, bool isReserve) { if (progress >= EffectFadeInterval) { return(isReserve ? 0.0 : 1.0); } var progressSeconds = progress.TotalSeconds; var intervalSeconds = EffectFadeInterval.TotalSeconds; var rate = progressSeconds / intervalSeconds; return(MathEx.Between(0.0, 1.0, isReserve ? 1.0 - rate : rate)); }
/// <summary> /// OpenGLのClearColorを更新します。 /// </summary> private void UpdateClearColor() { if (IsDesignMode) { return; } GL.ClearColor( MathEx.Between(0.0f, 1.0f, this.clearColor.R / 255.0f), MathEx.Between(0.0f, 1.0f, this.clearColor.G / 255.0f), MathEx.Between(0.0f, 1.0f, this.clearColor.B / 255.0f), MathEx.Between(0.0f, 1.0f, this.clearColor.A / 255.0f)); }
/// <summary> /// 背景色変更中の情報を取得します。 /// </summary> private double GetBackgroundOpacity(TimeSpan progress, bool isReverse) { if (progress >= BackgroundFadeInterval) { return(isReverse ? 0.0 : 1.0); } // 背景の不透明度を更新します。 var progressSeconds = progress.TotalSeconds; var totalSeconds = BackgroundFadeInterval.TotalSeconds; var rate = (progressSeconds / totalSeconds); return(MathEx.Between(0.0, 1.0, isReverse ? 1.0 - rate : rate)); }
/// <summary> /// p0とp1で結ばれる直線と点cの距離を計算します。 /// </summary> /// <remarks> /// ベクトルを用いて距離を計算します。 /// ベクトルL = P1 - P0 /// /// 線分上で点Cと直交する点をP = P0 + t * Lとすると、 /// (P - C)・L = 0 /// (P0 - C + t * L)・L = 0 /// t * |L|^2 = - (P0 - C)・L /// /// また、 /// 距離d = |P - C| /// = |(P0 - C) + t * L| /// /// 参考:http://homepage2.nifty.com/mathfin/distance/vector.htm /// </remarks> public static double LineCircleDistance(Vector3D p0, Vector3D p1, Vector3D c) { var cp = p0 - c; var l = p1 - p0; var length2 = l.LengthSquared; if (length2 < double.Epsilon) { return(cp.Length); } var t = -Vector3D.DotProduct(cp, l) / length2; // 線分と点の距離なので点Pは端点の外には出れません。 t = MathEx.Between(0.0, 1.0, t); return((cp + t * l).Length); }
/// <summary> /// コンストラクタ /// </summary> public MainViewModel() { if (string.IsNullOrEmpty(Name)) { Name = "meijin_" + MathEx.RandInt(0, 1000); } ThreadNumMaximum = DeviceInventory.CPUCount; if (ThreadNum == 0) { ThreadNum = Math.Max(1, ThreadNumMaximum - 2); } var rawMemSizeList = Bonanza.MemorySizeList(0.5).ToList(); MemSizeList = rawMemSizeList.Take(7).ToList(); if (HashMemSize == 0) { var index = MathEx.Between(0, 6, rawMemSizeList.Count - 2); HashMemSize = rawMemSizeList[index].HashValue; } this.AddDependModel(Global.Settings); }
static object CoercePoint(DependencyObject d, object v) { var value = (double)v; return(MathEx.Between(-9999.0, 9999.0, value)); }
/// <summary> /// /// </summary> public int Read(float[] buffer, int offset, int count) { var numberOfStoredSamples = 0; if (count > 0 && this.sourceList.Any()) { // 読み取りバッファを確保 this.mixerBuffer = this.mixerBuffer.CheckBuffer(count); Array.Clear(this.mixerBuffer, 0, count); var numberOfReadSamples = new List <int>(); foreach (var pair in this.sourceList.ToArray()) { var backend = pair.Key; var source = pair.Value; // バッファ読み込み時は、すでに読み込んだ部分だけ加算するようにします。 var read = source.Read(this.mixerBuffer, 0, count); for (var n = 0; n < read; ++n) { var initial = (numberOfStoredSamples <= n + offset ? 0 : buffer[n + offset]); var add = this.mixerBuffer[n] * backend.Volume; buffer[n + offset] = (float)(initial + add); // MathEx.Between(-1.0f, +1.0f, (float)(initial + add)); } numberOfStoredSamples = Math.Max(numberOfStoredSamples, read); if (read > 0) { numberOfReadSamples.Add(read); } else if (source.Position >= source.Length) { // remove the input to make sure that the event gets only raised once. RemoveSource(backend); } } if (false) { var currentOffset = offset; numberOfReadSamples.Sort(); numberOfReadSamples.ForEachWithIndex((readSamples, i) => { while (currentOffset < offset + readSamples) { buffer[currentOffset] /= numberOfReadSamples.Count - i; buffer[currentOffset] = MathEx.Between(-1.0f, +1.0f, buffer[currentOffset]); ++currentOffset; } }); } } /*if (numberOfStoredSamples != count) * { * Array.Clear( * buffer, * Math.Max(offset + numberOfStoredSamples, 0), * count - numberOfStoredSamples); * * return count; * }*/ return(numberOfStoredSamples); }