1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

メメントモリを楽しく!アプリを作ってみよう!(キャラ管理:詳細)

Last updated at Posted at 2025-08-26

キャラの詳細(PropertyGrid)の部分を考える

画面構成の、キャラの詳細(PropertyGrid)の部分を、いろいろ考えてみた。
表示イメージは前回内容をみてください。
https://qiita.com/puyon/items/698b9f874241c32aa89c

PropertyGrid を利用するので、それ用のクラスを用意する。
PropertyGrid の表示内容を日本語化する。

PropertyGrid 用クラス作成

クラス表現は、こんな感じ。

/// <summary>
/// キャラ管理UI
/// </summary>
public class MemeMoriCharRecepterUI
{
	/// <summary>
	/// 所属する属性
	/// </summary>
	public MemeMoriAttrType Attr { get; internal set; }
	/// <summary>
	/// キャラ識別(認識テキスト)
	/// </summary>
	public string AuxName { get; set; }
	/// <summary>
	/// キャラ名
	/// </summary>
	public string Name { get; internal set; }
	/// <summary>
	/// レアリティ
	/// </summary>
	public MemeMoriRarity Rarity { get; set; }
	/// <summary>
	/// ルーン装着状況
	/// </summary>
	public MemeMoriArmorSet Armor { get; set; }
}

あとは、PropertyGrid を画面に配置して、インスタンスを渡すだけ。

pgChar.SelectedObject = new MemeMoriCharRecepterUI();

表示内容の日本語化(普通な感じ)

日本語化の対象になるのは、「カテゴリ」と「プロパティ」(設定項目の名前や値)だと思う。
普通に考えると、それぞれプロパティへ Attribute を付加すると思う。

対象 Attribute
カテゴリ CategoryAttribute
プロパティ DisplayAttribute

たとえば、こんな感じ。

/// <summary>
/// キャラ名
/// </summary>
[Category("キャラ情報")
public string Name { get; internal set; }

これって、「ハードコーディング」なので、なんとかならないかと、こぱちゃんに聞いてみた。

表示内容の日本語化(こぱちゃん解:カスタム属性作成)

カテゴリの場合、CategoryAttibute から継承したらいいらしい。
(今回はカテゴリが1つだけだけど)複数ある場合を想定して、カテゴリを識別できるように考えた。
ようは、日本語の文字列を指定するのではなく、識別(enum)を指定する。

識別(enum)を用意

/// <summary>
/// キャラ管理:カテゴリ名→日本語置き換えの対象
/// </summary>
public enum MemeMoriCharRecepterCategoryNames
{
	CharInfo = 0,       //キャラ情報
}

カスタム属性を作成(CategoryAttibute からの継承)

/// <summary>
/// キャラ管理:カテゴリ名→日本語置き換え
/// </summary>
[AttributeUsage(AttributeTargets.All)]
public class MemeMoriCharRecepterCategoryAttribute : CategoryAttribute
{
	public MemeMoriCharRecepterCategoryAttribute(MemeMoriCharRecepterCategoryNames attribute)
		: base(GetName(attribute)) { }

	private static string GetName(MemeMoriCharRecepterCategoryNames attr)
	{
		switch (attr)
		{
			case MemeMoriCharRecepterCategoryNames.CharInfo:
				return "キャラ情報";
		}
		return attr.ToString();
	}
}

Attribute を指定する

/// <summary>
/// キャラ名
/// </summary>
[MemeMoriCharRecepterCategory(MemeMoriCharRecepterCategoryNames.CharInfo)]
public string Name { get; internal set; }

この実装では、結局はハードコーディングになっているけど、リソースや設定ファイルからの取得に置き換えできるようになった。
DisplayAttribute も、同じ乗りで、「カスタム属性」を作ればいい感じ。

次回は、レアリティの選択について

レアリティの選択について考えてみた。
進化対象のキャラと、そうでないキャラがあるので、それをかんがえてみる。
https://qiita.com/puyon/items/ca48f4a9b001c9fd1871

1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?