using BepInEx;
using UniRx;
using UnityEngine;
using UnityEngine.UI;
namespace KKAPI.Maker.UI
{
///
/// Custom control that displays a texture in a small preview thumbnail
///
public class MakerImage : BaseGuiEntry
{
private readonly BehaviorSubject _texture;
private int _width = 100;
private int _height = 100;
///
/// Texture to display in the preview
///
public Texture Texture
{
get => _texture.Value;
set => _texture.OnNext(value);
}
///
/// Width of the texture preview
///
public int Width
{
get => _width;
set
{
_width = value;
_texture.OnNext(_texture.Value);
}
}
///
/// Height of the texture preview
///
public int Height
{
get => _height;
set
{
_height = value;
_texture.OnNext(_texture.Value);
}
}
///
/// Create a new custom control. Create and register it in .
///
/// Texture to be displayed in the image box. Can be null for empty.
/// Category the control will be created under
/// Plugin that owns the control
public MakerImage(Texture texture, MakerCategory category, BaseUnityPlugin owner) : base(category, owner)
{
_texture = new BehaviorSubject(texture);
}
///
protected internal override void Initialize() { }
///
protected override GameObject OnCreateControl(Transform subCategoryList)
{
var go = new GameObject("image", typeof(RectTransform), typeof(LayoutElement));
go.transform.SetParent(subCategoryList, false);
go.layer = 5;
var le = go.GetComponent();
le.minWidth = 456;
var ig = new GameObject("img", typeof(RectTransform), typeof(CanvasRenderer));
ig.transform.SetParent(go.transform, false);
ig.layer = 5;
var i = ig.AddComponent();
var irt = ig.GetComponent();
_texture.Subscribe(
texture =>
{
i.texture = texture;
le.minHeight = Height + 30;
irt.offsetMin = new Vector2(-1 * Width / 2f, -1 * Height / 2f);
irt.offsetMax = new Vector2(Width / 2f, Height / 2f);
le.enabled = false;
le.enabled = true;
});
return go;
}
}
}