using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using Materials = Rmake.Model.Materials;
using Rmake.Framework.Logging;
using Rmake.Model.Enums;
using Rmake.Rendering.APIWrapper;
using Rmake.Rendering.ComponentLayer;
using Rmake.Model.Events;
namespace Rmake.GameRendering
{
///
/// キャラクターなど複数のアニメーションを持つ
/// スプライトの描画
///
public class CharacterView : GameRenderingView
{
private ArrayList animations = null;
private int activeIndex = 0;
private Rectangle oldTableMapRect =
new Rectangle(-2147483648, -2147483648, 0, 0);
private Point position = new Point(0, 0);
private Object model = null;
public Object Model
{
get { return model; }
set { model = value; }
}
///
/// 描画には使わない Model 上の位置
///
public Point Position
{
get { return position; }
set { position = value; }
}
internal override int LayerIndex
{
get
{
AnimationView animationView = (AnimationView)animations[0];
return animationView.LayerIndex;
}
set
{
int i, sz;
AnimationView animationView;
sz = animations.Count;
for(i = 0; i < sz ; i++)
{
animationView = (AnimationView)animations[i];
animationView.LayerIndex = value;
}
}
}
///
/// コピー先の座標
///
public Point DestinationPoint
{
get
{
AnimationView animationView = (AnimationView)animations[0];
return animationView.DestinationPoint;
}
set
{
int i, sz;
AnimationView animationView;
sz = animations.Count;
for(i = 0; i < sz ; i++)
{
animationView = (AnimationView)animations[i];
animationView.DestinationPoint = value;
}
bool flg = false;
if(Layer != null)
{
Rectangle r = GetTableMapRect();
if(oldTableMapRect.X != r.X ||
oldTableMapRect.Y != r.Y ||
oldTableMapRect.Width != r.Width ||
oldTableMapRect.Height != r.Height
)
{
oldTableMapRect = r;
RemoveRenderView();
flg = true;
}
}
if(Layer != null)
{
if(flg)
AddRenderView();
}
}
}
///
/// FrameListの何番目のコピー元のRectangleで
/// 描画するかどうかの指定。
///
public int AnimationIndex
{
get
{
AnimationView animationView = (AnimationView)animations[0];
return animationView.AnimationIndex;
}
set
{
int i, sz;
AnimationView animationView;
sz = animations.Count;
for(i = 0; i < sz ; i++)
{
animationView = (AnimationView)animations[i];
animationView.AnimationIndex = value;
}
}
}
///
/// アニメーションのスピード。
/// 何フレームでAnimationIndexを加算するか指定する。
///
public int AnimationSpeed
{
get
{
AnimationView animationView = (AnimationView)animations[0];
return animationView.AnimationSpeed;
}
set
{
int i, sz;
AnimationView animationView;
sz = animations.Count;
for(i = 0; i < sz ; i++)
{
animationView = (AnimationView)animations[i];
animationView.AnimationSpeed = value;
}
}
}
public int RectColor
{
get
{
AnimationView animationView = (AnimationView)animations[0];
return animationView.RectColor;
}
set
{
int i, sz;
AnimationView animationView;
sz = animations.Count;
for(i = 0; i < sz ; i++)
{
animationView = (AnimationView)animations[i];
animationView.RectColor = value;
}
}
}
public Matrix Transform
{
get
{
AnimationView animationView = (AnimationView)animations[0];
return animationView.Transform;
}
set
{
int i, sz;
AnimationView animationView;
sz = animations.Count;
for(i = 0; i < sz ; i++)
{
animationView = (AnimationView)animations[i];
animationView.Transform = value;
}
}
}
///
/// 何番目のアニメーションを使って描画するかどうかの設定
///
public int ActiveIndex
{
get
{
return activeIndex;
}
set
{
activeIndex = value;
Event model = Model as Event;
if (model != null)
{
model.Direction = activeIndex;
}
}
}
public int GetInverseDirection()
{
if (activeIndex == 0)
{
return 3;
}
else if (activeIndex == 1)
{
return 2;
}
else if (activeIndex == 2)
{
return 1;
}
else if (activeIndex == 3)
{
return 0;
}
return 0;
}
///
/// テクスチャーの設定
///
/// 読み込む画像のファイル名
/// 一番左上のピクセルの色を透過色とするかどうか
public void SetTexture(string fileName, bool leftTopTransparent)
{
foreach(AnimationView animationView in animations)
{
animationView.SetTexture(fileName, leftTopTransparent);
}
}
///
/// テクスチャーの設定
///
/// CharacterViewのマテリアル
public void SetTexture(Materials.Material material)
{
string filename = Folders.GetMaterialFolder(material.MaterialType) + material.Filename;
foreach(AnimationView animationView in animations)
{
animationView.SetTexture(filename, material.IsTransparencyByTopLeftPixel);
}
}
internal Rectangle GetTableMapRect()
{
Rectangle r;
AnimationView animationView = (AnimationView)animations[0];
Rectangle sr = (Rectangle)animationView.FrameList[0];
Size sz;
Point dp = DestinationPoint;
Matrix m;
Point mn = new Point(2147483647, 2147483647);
Point mx = new Point(-2147483648, -2147483648);
float px, py;
sz = sr.Size;
m = Transform;
if(true)
{
px = dp.X * m.M11 + dp.Y * m.M21 + m.M41;
py = dp.X * m.M12 + dp.Y * m.M22 + m.M42;
if(mn.X > (px - 0.5f)) mn.X = (int)(px - 0.5f);
if(mn.Y > (py - 0.5f)) mn.Y = (int)(py - 0.5f);
if(mx.X < (px + 0.5f)) mx.X = (int)(px + 0.5f);
if(mx.Y < (py + 0.5f)) mx.Y = (int)(py + 0.5f);
px = (dp.X + sz.Width) * m.M11 + dp.Y * m.M21 + m.M41;
py = (dp.X + sz.Width) * m.M12 + dp.Y * m.M22 + m.M42;
if(mn.X > (px - 0.5f)) mn.X = (int)(px - 0.5f);
if(mn.Y > (py - 0.5f)) mn.Y = (int)(py - 0.5f);
if(mx.X < (px + 0.5f)) mx.X = (int)(px + 0.5f);
if(mx.Y < (py + 0.5f)) mx.Y = (int)(py + 0.5f);
px = (dp.X + sz.Width) * m.M11 + (dp.Y + sz.Height) * m.M21 + m.M41;
py = (dp.X + sz.Width) * m.M12 + (dp.Y + sz.Height) * m.M22 + m.M42;
if(mn.X > (px - 0.5f)) mn.X = (int)(px - 0.5f);
if(mn.Y > (py - 0.5f)) mn.Y = (int)(py - 0.5f);
if(mx.X < (px + 0.5f)) mx.X = (int)(px + 0.5f);
if(mx.Y < (py + 0.5f)) mx.Y = (int)(py + 0.5f);
px = dp.X * m.M11 + (dp.Y + sz.Height) * m.M21 + m.M41;
py = dp.X * m.M12 + (dp.Y + sz.Height) * m.M22 + m.M42;
if(mn.X > (px - 0.5f)) mn.X = (int)(px - 0.5f);
if(mn.Y > (py - 0.5f)) mn.Y = (int)(py - 0.5f);
if(mx.X < (px + 0.5f)) mx.X = (int)(px + 0.5f);
if(mx.Y < (py + 0.5f)) mx.Y = (int)(py + 0.5f);
}
mn.X /= Layer.TablePieceWidth;
mn.Y /= Layer.TablePieceHeight;
mx.X /= Layer.TablePieceWidth;
mx.Y /= Layer.TablePieceHeight;
r = new Rectangle(mn.X ,mn.Y , (mx.X - mn.X), (mx.Y - mn.Y));
return r;
}
internal override void AddRenderView()
{
if (this.animations.Count <= 0) return;
Rectangle r;
int i, ied, j, jed;
int ia, ja;
r = GetTableMapRect();
ied = r.Bottom;
jed = r.Right;
LayerVisible = false;
ArrayList tableMap = Layer.TableMap;
for(i = r.Top ; i <= ied ; i++)
{
ia = i % Layer.TableHeight;
if(ia < 0) ia += Layer.TableHeight;
ArrayList tableList = (ArrayList)tableMap[ia];
for(j = r.Left ; j <= jed ; j++)
{
ja = j % Layer.TableWidth;
if(ja < 0) ja += Layer.TableWidth;
Hashtable hash = (Hashtable)tableList[ja];
hash.Add(this, this);
}
}
//if(Layer != null)
// Layer.RenderingViewHash.Add(this, this);
}
internal override void RemoveRenderView()
{
if (this.animations.Count <= 0) return;
Rectangle r;
int i, ied, j, jed;
int ia, ja;
r = GetTableMapRect();
ied = r.Bottom;
jed = r.Right;
ArrayList tableMap = Layer.TableMap;
for(i = r.Top ; i <= ied ; i++)
{
ia = i % Layer.TableHeight;
if(ia < 0) ia += Layer.TableHeight;
ArrayList tableList = (ArrayList)tableMap[ia];
for(j = r.Left ; j <= jed ; j++)
{
ja = j % Layer.TableWidth;
if(ja < 0) ja += Layer.TableWidth;
Hashtable hash = (Hashtable)tableList[ja];
hash.Remove(this);
}
}
//if(Layer != null)
// Layer.RenderingViewHash.Remove(this);
}
public CharacterView()
{
//
// TODO: コンストラクタ ロジックをここに追加してください。
//
animations = new ArrayList();
}
internal void Initialize(ArrayList animations)
{
int i, sz;
this.animations.Clear();
sz = animations.Count;
for(i = 0 ; i < sz ; i++)
{
this.animations.Add(animations[i]);
}
}
internal override void Finalize(GameRenderingManager manager)
{
int i, sz;
AnimationView animationView;
sz = animations.Count;
for(i = 0 ; i < sz ; i++)
{
animationView = (AnimationView)animations[i];
manager.Manager.DeleteRenderView(animationView);
}
this.animations.Clear();
}
///
/// アニメーションのコピー元パターンを追加する。
///
/// 何番目のアニメーションに追加するかの設定
/// コピー元の矩形
public void AddFrameRectangle(int index, Rectangle source)
{
if(index < 0) return;
if(index >= animations.Count) return;
AnimationView animationView;
animationView = (AnimationView)animations[index];
animationView.AddFrameRectangle(source);
}
///
/// index番目のアニメーションを消去する
///
/// 何番目のアニメーションを消去するかの設定
public void ClearFrameRectangle(int index)
{
if(index < 0) return;
if(index >= animations.Count) return;
AnimationView animationView;
animationView = (AnimationView)animations[index];
animationView.ClearFrameRectangle();
}
internal override void Render(GameRenderingManager manager)
{
int i, sz;
AnimationView animationView;
sz = animations.Count;
for(i = 0 ; i < sz ; i++)
{
animationView = (AnimationView)animations[i];
if(i != activeIndex)
{
animationView.Visible = false;
}
else
{
if(Visible && LayerVisible)
animationView.Visible = true;
else
animationView.Visible = false;
}
}
}
internal override void SetVisible(bool visible)
{
int i, sz;
AnimationView animationView;
sz = animations.Count;
for(i = 0; i < sz ; i++)
{
animationView = (AnimationView)animations[i];
animationView.ViewVisible = visible;
}
}
}
}