+++ /dev/null
-using System;
-using System.Collections.Generic;
-using System.Diagnostics;
-using System.Linq;
-
-namespace FDK.メディア
-{
- /// <summary>
- /// DirectWrite を使って文字列を書き込めるテクスチャ。
- /// </summary>
- public class 文字列テクスチャ : テクスチャ
- {
- public string 表示文字列 { get; set; } = null;
- public float フォントサイズpt { get; set; } = 20.0f;
- public string フォント名 { get; set; } = "メイリオ";
-
- public 文字列テクスチャ( string 初期文字列, float フォントサイズpt = 20.0f, string フォント名 = "メイリオ" ) : base( SharpDX.Size2.Zero )
- {
- this.表示文字列 = 初期文字列 ?? "";
- this.フォントサイズpt = フォントサイズpt;
- this.フォント名 = フォント名;
- }
- protected override void Onデバイス依存リソースの作成( デバイスリソース dr )
- {
- this.前回の表示文字列 = this.表示文字列;
- this.テクスチャとビットマップを作成する( dr );
- }
- protected override void Onデバイス依存リソースの解放( デバイスリソース dr )
- {
- // ビットマップその他を解放する。
- FDK.Utilities.解放する( ref this.黒ブラシ );
- FDK.Utilities.解放する( ref this.白ブラシ );
- FDK.Utilities.解放する( ref this.ビットマップターゲット );
- FDK.Utilities.解放する( ref this.テキストレイアウト );
- FDK.Utilities.解放する( ref this.テキストフォーマット );
-
- // テクスチャを解放する。
- base.Onデバイス依存リソースの解放( dr );
- }
- public new void 進行描画する( デバイスリソース dr, SharpDX.Matrix ワールド行列変換, SharpDX.RectangleF? 転送元矩形 = null )
- {
- if( this.表示文字列.Nullまたは空である() )
- return;
-
- // 表示文字列が変更されているなら、ここで表示ビットマップの再構築を行う。
- if( false == string.Equals( this.表示文字列, this.前回の表示文字列 ) )
- this.テクスチャとビットマップを作成する( dr );
-
- // テクスチャを描画する。
- base.進行描画する( dr, ワールド行列変換, 転送元矩形 );
- }
-
- protected string 前回の表示文字列 = null;
- protected SharpDX.DirectWrite.TextFormat テキストフォーマット = null;
- protected SharpDX.DirectWrite.TextLayout テキストレイアウト = null;
- protected SharpDX.Direct2D1.Bitmap1 ビットマップターゲット = null;
- protected SharpDX.Direct2D1.SolidColorBrush 白ブラシ = null;
- protected SharpDX.Direct2D1.SolidColorBrush 黒ブラシ = null;
-
- protected void テクスチャとビットマップを作成する( デバイスリソース dr )
- {
- this.前回の表示文字列 = this.表示文字列;
-
- // テクスチャとビットマップを解放する。
- this.Onデバイス依存リソースの解放( dr ); // base. じゃなくて this.
-
- if( this.表示文字列.Nullまたは空である() )
- return;
-
- #region " テキストフォーマット/レイアウトを作成し、表示ビットマップのサイズを計算し、ユーザ指定サイズdpx に格納する。"
- //----------------
- this.テキストフォーマット = new SharpDX.DirectWrite.TextFormat( dr.DWriteFactory, this.フォント名, this.フォントサイズpt ) {
- TextAlignment = SharpDX.DirectWrite.TextAlignment.Leading,
- };
-
- var 最大サイズdpx = dr.設計画面サイズdpx;
-
- this.テキストレイアウト = new SharpDX.DirectWrite.TextLayout(
- dr.DWriteFactory,
- this.表示文字列.Nullまたは空である() ? " " : this.表示文字列,
- this.テキストフォーマット,
- 最大サイズdpx.Width,
- 最大サイズdpx.Height );
-
- this.ユーザ指定サイズdpx = new SharpDX.Size2(
- (int) ( this.テキストレイアウト.Metrics.WidthIncludingTrailingWhitespace + 0.5f ),
- (int) ( this.テキストレイアウト.Metrics.Height + 0.5f ) );
- //----------------
- #endregion
-
- // ユーザ指定サイズdpx に従って、テクスチャを作成する。
- base.Onデバイス依存リソースの作成( dr ); // this. じゃなくて base.
-
- #region " 作成したテクスチャとデータを共有するビットマップを作成する。"
- //----------------
- using( var dxgiSurface = this.Texture.QueryInterfaceOrNull<SharpDX.DXGI.Surface1>() )
- {
- var bmpProp = new SharpDX.Direct2D1.BitmapProperties1() {
- PixelFormat = new SharpDX.Direct2D1.PixelFormat( dxgiSurface.Description.Format, SharpDX.Direct2D1.AlphaMode.Premultiplied ),
- BitmapOptions = SharpDX.Direct2D1.BitmapOptions.Target | SharpDX.Direct2D1.BitmapOptions.CannotDraw,
- };
- this.ビットマップターゲット = new SharpDX.Direct2D1.Bitmap1( dr.D2DContext1, dxgiSurface, bmpProp );
- }
- //----------------
- #endregion
- #region " ビットマップに文字列を描画する。"
- //----------------
- using( var 旧ターゲット = dr.D2DContext1.Target )
- {
- try
- {
- this.白ブラシ = new SharpDX.Direct2D1.SolidColorBrush( dr.D2DContext1, SharpDX.Color.LightGray );
- this.黒ブラシ = new SharpDX.Direct2D1.SolidColorBrush( dr.D2DContext1, SharpDX.Color.Black );
-
- dr.D2DContextの設定をリセットする( dr.D2DContext1 );
- dr.D2DContext1.Target = this.ビットマップターゲット;
-
- FDK.Utilities.D2DBatchDraw( dr.D2DContext1, () => {
-
- dr.D2DContext1.Clear( SharpDX.Color.Transparent );
-
- dr.D2DContext1.DrawTextLayout( // ドロップシャドウ部
- new SharpDX.Vector2( 1.0f, 1.0f ),
- this.テキストレイアウト,
- 黒ブラシ,
- SharpDX.Direct2D1.DrawTextOptions.Clip );
-
- dr.D2DContext1.DrawTextLayout( // 本体部
- new SharpDX.Vector2( 0.0f, 0.0f ),
- this.テキストレイアウト,
- 白ブラシ,
- SharpDX.Direct2D1.DrawTextOptions.Clip );
-
- } );
- }
- finally
- {
- dr.D2DContext1.Target = 旧ターゲット;
- }
- }
- //----------------
- #endregion
- }
- }
-}
--- /dev/null
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Linq;
+using FDK.メディア;
+using FDK; // string拡張メソッド用。
+
+namespace SST.曲
+{
+ public class タイトルテクスチャ : FDK.メディア.ビットマップ付きテクスチャ
+ {
+ public string 表示文字列 { get; set; } = null;
+
+ public タイトルテクスチャ( string 初期文字列 ) : base( new SharpDX.Size2( 1, 1 ) )
+ {
+ this.表示文字列 = 初期文字列 ?? "(no title)";
+ }
+ protected override void Onデバイス依存リソースの作成( デバイスリソース dr )
+ {
+ this.テクスチャを更新する( dr );
+ }
+ protected override void Onデバイス依存リソースの解放( デバイスリソース dr )
+ {
+ // テキスト描画用リソースを解放する。
+ FDK.Utilities.解放する( ref this.黒ブラシ );
+ FDK.Utilities.解放する( ref this.白ブラシ );
+ FDK.Utilities.解放する( ref this.テキストレイアウト );
+ FDK.Utilities.解放する( ref this.テキストフォーマット );
+
+ // ビットマップ付きテクスチャを解放する。
+ base.Onデバイス依存リソースの解放( dr );
+ }
+ public new void 進行描画する( デバイスリソース dr, SharpDX.Matrix ワールド行列変換, SharpDX.RectangleF? 転送元矩形 = null )
+ {
+ if( this.表示文字列.Nullまたは空である() )
+ return;
+
+ // 表示文字列が変更されているなら、ここで表示ビットマップの再構築を行う。
+ if( false == string.Equals( this.表示文字列, this.前回の表示文字列 ) )
+ this.テクスチャを更新する( dr );
+
+ // テクスチャを描画する。
+ base.進行描画する( dr, ワールド行列変換, 転送元矩形 );
+ }
+
+ protected float フォントサイズpt = 30.0f;
+ protected string フォント名 = "メイリオ";
+ protected string 前回の表示文字列 = null;
+ protected SharpDX.DirectWrite.TextFormat テキストフォーマット = null;
+ protected SharpDX.DirectWrite.TextLayout テキストレイアウト = null;
+ protected SharpDX.Direct2D1.SolidColorBrush 白ブラシ = null;
+ protected SharpDX.Direct2D1.SolidColorBrush 黒ブラシ = null;
+
+ /// <summary>
+ /// デバイス依存リソースの作成() から、または表示文字列が変更された場合に呼び出される。
+ /// </summary>
+ protected void テクスチャを更新する( デバイスリソース dr )
+ {
+ this.前回の表示文字列 = this.表示文字列;
+
+ // すべてのリソースを解放する。
+ this.Onデバイス依存リソースの解放( dr ); // base. じゃなくて this.
+
+ if( this.表示文字列.Nullまたは空である() )
+ return;
+
+ #region " テキストフォーマット/レイアウトを作成し、表示ビットマップのサイズを計算し、ユーザ指定サイズdpx に格納する。"
+ //----------------
+ this.テキストフォーマット = new SharpDX.DirectWrite.TextFormat( dr.DWriteFactory, this.フォント名, this.フォントサイズpt ) {
+ TextAlignment = SharpDX.DirectWrite.TextAlignment.Leading,
+ };
+
+ var 最大サイズdpx = dr.設計画面サイズdpx;
+ this.テキストレイアウト = new SharpDX.DirectWrite.TextLayout(
+ dr.DWriteFactory,
+ this.表示文字列.Nullまたは空である() ? " " : this.表示文字列,
+ this.テキストフォーマット,
+ 最大サイズdpx.Width,
+ 最大サイズdpx.Height );
+
+ this.ユーザ指定サイズdpx = new SharpDX.Size2(
+ (int) ( this.テキストレイアウト.Metrics.WidthIncludingTrailingWhitespace + 0.5f ),
+ (int) ( this.テキストレイアウト.Metrics.Height + 0.5f ) );
+ //----------------
+ #endregion
+
+ // ビットマップ付きテクスチャを作成する。(上で設定した ユーザ指定サイズdpx の大きさで作成される。)
+ base.Onデバイス依存リソースの作成( dr );
+
+ #region " ビットマップに文字列を描画する。"
+ //----------------
+ this.ビットマップへ描画する( dr, ( context, bitmap ) => {
+
+ this.白ブラシ = new SharpDX.Direct2D1.SolidColorBrush( dr.D2DContext1, SharpDX.Color.LightGray );
+ this.黒ブラシ = new SharpDX.Direct2D1.SolidColorBrush( dr.D2DContext1, SharpDX.Color.Black );
+
+ dr.D2DContext1.Clear( SharpDX.Color.Transparent );
+
+ dr.D2DContext1.DrawTextLayout( // ドロップシャドウ部
+ new SharpDX.Vector2( 1.0f, 1.0f ),
+ this.テキストレイアウト,
+ this.黒ブラシ,
+ SharpDX.Direct2D1.DrawTextOptions.Clip );
+
+ dr.D2DContext1.DrawTextLayout( // 本体部
+ new SharpDX.Vector2( 0.0f, 0.0f ),
+ this.テキストレイアウト,
+ this.白ブラシ,
+ SharpDX.Direct2D1.DrawTextOptions.Clip );
+ } );
+ //----------------
+ #endregion
+ }
+ }
+}