Add skin property image.frames and image.index.

This commit is contained in:
2023-02-17 14:40:34 +08:00
parent e7ce0985fb
commit 0d4cc5e208
4 changed files with 56 additions and 26 deletions

View File

@@ -29,19 +29,12 @@ namespace Cryville.Crtr.Components {
return Rect.width / Rect.height;
}
}
bool _loaded;
Material _mat;
public void Bind(Material mat) {
_loaded = true;
_mat = mat;
Reload();
}
public void Load() {
_loaded = true;
Reload();
}
public void Reload() {
if (!_loaded) return;
if (!string.IsNullOrEmpty(FrameName)) {
if (ChartPlayer.frames.ContainsKey(FrameName)) {
Frame = ChartPlayer.frames[FrameName];
@@ -60,7 +53,9 @@ namespace Cryville.Crtr.Components {
public class SpritePlane : SpriteBase {
public SpritePlane() {
SubmitProperty("frame", new PropOp.String(v => Frame = v));
SubmitProperty("frame", new PropOp.String(v => { Frames = new string[] { v }; Index = 0; }));
SubmitProperty("frames", new PropOp.StringArray(v => Frames = v));
SubmitProperty("index", new PropOp.Integer(v => Index = v));
SubmitProperty("fit", new PropOp.Enum<FitMode>(v => Fit = v, v => (FitMode)v));
SubmitProperty("opacity", new PropOp.Float(v => Opacity = v));
}
@@ -83,35 +78,53 @@ namespace Cryville.Crtr.Components {
}
}
protected SpriteInfo frameInfo = new SpriteInfo();
public string Frame {
get { return frameInfo.FrameName; }
int m_index;
public int Index {
get { return m_index; }
set {
frameInfo.FrameName = value;
m_index = value;
OnFrameUpdate();
}
}
SpriteInfo[] m_frames = new SpriteInfo[0];
public string[] Frames {
set {
m_frames = new SpriteInfo[value.Length];
for (int i = 0; i < value.Length; i++) {
m_frames[i] = new SpriteInfo() { FrameName = value[i] };
}
OnFrameUpdate();
}
}
protected SpriteInfo CurrentFrame {
get {
if (m_index < 0) m_index = 0;
else if (m_index >= m_frames.Length) m_index = m_frames.Length - 1;
return m_frames[m_index];
}
}
protected void OnFrameUpdate() {
if (!mesh.Initialized) return;
if (frameInfo.Frame == null) {
if (CurrentFrame.Frame == null) {
mesh.Renderer.enabled = false;
return;
}
mesh.Renderer.enabled = true;
mesh.Renderer.material.mainTexture = CurrentFrame.Frame.Texture;
UpdateUV();
UpdateScale();
UpdateZIndex();
}
protected virtual void UpdateUV() {
if (frameInfo.Frame == null) {
Logger.Log("main", 4, "Skin", "Unable to load texture {0}", frameInfo.FrameName);
var frame = CurrentFrame;
if (frame.Frame == null) {
Logger.Log("main", 4, "Skin", "Unable to load texture {0}", frame.FrameName);
return;
}
Vector2[] muv = OriginalUV;
Vector2[] uv = new Vector2[muv.Length];
for (int i = 0; i < uv.Length; i++) {
uv[i] = frameInfo.Frame.GetUV(muv[i]);
uv[i] = frame.Frame.GetUV(muv[i]);
}
mesh.Mesh.uv = uv;
}
@@ -124,7 +137,7 @@ namespace Cryville.Crtr.Components {
UpdateOpacity();
}
}
void UpdateOpacity() {
protected void UpdateOpacity() {
if (!mesh.Initialized) return;
var c = mesh.Renderer.material.color;
c.a = _opacity;
@@ -144,7 +157,7 @@ namespace Cryville.Crtr.Components {
}
protected override void UpdateScale() {
if (frameInfo.Frame == null) return;
if (CurrentFrame.Frame == null) return;
base.UpdateScale();
if (m_fit != FitMode.none && Scale.x != Scale.y) m_fit = FitMode.none;
}
@@ -153,8 +166,8 @@ namespace Cryville.Crtr.Components {
get {
switch (m_fit) {
case FitMode.none: return Vector3.one;
case FitMode.width: return new Vector3(1, 1, 1 / frameInfo.Ratio);
case FitMode.height: return new Vector3(frameInfo.Ratio, 1, 1);
case FitMode.width: return new Vector3(1, 1, 1 / CurrentFrame.Ratio);
case FitMode.height: return new Vector3(CurrentFrame.Ratio, 1, 1);
default: throw new NotSupportedException("Unsupported fit mode");
}
}
@@ -162,7 +175,6 @@ namespace Cryville.Crtr.Components {
public override void Init() {
InternalInit();
frameInfo.Bind(mesh.Renderer.material);
OnFrameUpdate();
UpdateOpacity();
}

View File

@@ -48,7 +48,8 @@ namespace Cryville.Crtr.Components {
Vector2[] muv = OriginalUV;
Vector2[] uv = new Vector2[muv.Length];
var or = frameInfo.Ratio;
var frame = CurrentFrame;
var or = frame.Ratio;
var sr = Scale.x / Scale.y;
var b = new Vector2(
(or / sr) * _border.x,
@@ -71,7 +72,7 @@ namespace Cryville.Crtr.Components {
case 3: y = 1; break;
default: throw new NotSupportedException("Built-in resource corrupted");
}
uv[i] = frameInfo.Frame.GetUV(x, y);
uv[i] = frame.Frame.GetUV(x, y);
bx -= 0.5f; y -= 0.5f;
vert[i] = new Vector3(bx, 0, y);
}
@@ -80,9 +81,9 @@ namespace Cryville.Crtr.Components {
}
public override void Init() {
frameInfo.Load();
InternalInit("quad_scale3h");
OnFrameUpdate();
UpdateOpacity();
}
}
}

View File

@@ -77,7 +77,6 @@ namespace Cryville.Crtr.Components {
verts.Clear();
uvs.Clear();
foreach (var f in m_frames) {
f.Value.Load();
if (frameHeight == 0) frameHeight = f.Value.Rect.height;
else if (frameHeight != f.Value.Rect.height) throw new Exception("Inconsistent frame height for text component");
var tex = f.Value.Frame.Texture;

View File

@@ -60,6 +60,24 @@ namespace Cryville.Crtr {
_cb(GetOperand(0).AsString());
}
}
public class StringArray : PropOp {
readonly Action<string[]> _cb;
public StringArray(Action<string[]> cb) { _cb = cb; }
protected unsafe override void Execute() {
var op = GetOperand(0);
int arrtype; int len;
op.GetArraySuffix(out arrtype, out len);
if (arrtype != PdtInternalType.String) throw new InvalidCastException("Not an array of strings");
var result = new string[len];
int o = 0;
for (int i = 0; i < len; i++) {
string v = op.AsString(o);
o += v.Length * sizeof(char) + sizeof(int);
result[i] = v;
}
_cb(result);
}
}
public class TargetString : PropOp {
readonly Func<RTargetString> _cb;
public TargetString(Func<RTargetString> cb) { _cb = cb; }