50 lines
2.2 KiB
C#
50 lines
2.2 KiB
C#
using Dalamud.Bindings.ImGui;
|
||
|
||
namespace ICRebuild.Runtime;
|
||
|
||
// All slider call sites use this helper so precise input has the same limits as dragging.
|
||
public sealed class PrecisionSliders
|
||
{
|
||
private float editing,firstClickValue;
|
||
private uint firstClickId;
|
||
private bool focus;
|
||
public bool Float(string label,ref float value,float min,float max)
|
||
{
|
||
ImGui.PushID(label);
|
||
var before=value;
|
||
var id=ImGui.GetID("slider");
|
||
var changed=ImGui.SliderFloat(label,ref value,min,max,"%.3f",ImGuiSliderFlags.AlwaysClamp|ImGuiSliderFlags.NoRoundToFormat);
|
||
if(ImGui.IsItemHovered() && ImGui.IsMouseDoubleClicked(ImGuiMouseButton.Left))
|
||
{
|
||
value=firstClickId==id ? firstClickValue : before;
|
||
changed=value!=before;
|
||
editing=value; focus=true;
|
||
ImGui.OpenPopup("输入精确数值");
|
||
}
|
||
else if(ImGui.IsItemClicked(ImGuiMouseButton.Left)) { firstClickId=id; firstClickValue=before; }
|
||
if(ImGui.IsItemHovered()) ImGui.SetTooltip($"双击输入精确数值,或 Ctrl+单击直接输入。当前值:{value:G9}");
|
||
if(ImGui.BeginPopup("输入精确数值"))
|
||
{
|
||
ImGui.TextUnformatted(label);
|
||
ImGui.Text($"范围:{min:G9} ~ {max:G9}");
|
||
if(focus) { ImGui.SetKeyboardFocusHere(); focus=false; }
|
||
ImGui.SetNextItemWidth(210);
|
||
var enter=ImGui.InputFloat("数值",ref editing,0,0,"%.6f",ImGuiInputTextFlags.EnterReturnsTrue|ImGuiInputTextFlags.AutoSelectAll);
|
||
if(!float.IsFinite(editing)) ImGui.TextUnformatted("请输入有限数值。");
|
||
ImGui.BeginDisabled(!float.IsFinite(editing));
|
||
var apply=ImGui.Button("确定");
|
||
ImGui.EndDisabled();
|
||
if((enter || apply) && float.IsFinite(editing))
|
||
{
|
||
value=Math.Clamp(editing,min,max); changed=true; ImGui.CloseCurrentPopup();
|
||
}
|
||
ImGui.SameLine();
|
||
if(ImGui.Button("取消")) ImGui.CloseCurrentPopup();
|
||
ImGui.TextUnformatted("超出范围的数值按上下限保存。");
|
||
ImGui.EndPopup();
|
||
}
|
||
ImGui.PopID();
|
||
return changed;
|
||
}
|
||
}
|