DiagramState

基本信息

api.diagram.DiagramState —— 图的动画/循环状态。 每个 DiagramGroup 通过 Supplier<DiagramState> 持有自己的实例。

关键常量

常量 值
TICKS_PER_CYCLE 20(1 秒 = 1 循环)

cycles 用于放慢动画,例如轮换一个槽位里依次显示的组件。

tick() 的三态逻辑

条件 行为
ctrl_fast_forward 开启 且 按住 <Ctrl>,且 按住 <Shift> ticks -= 20(快退)
ctrl_fast_forward 开启 且 按住 <Ctrl>,未按 <Shift> ticks += 20(快进)
未按 <Shift> ticks++(正常)

配置项 ctrl_fast_forward 默认 true,见 Config Options。

滚动与派生量

方法 行 说明
scroll(boolean scrollUp) 45 向上滚 ticks -= 20,向下 ticks += 20
ticks() 50 原始 tick 数 —— 可能为负
cycle() 55 ticks / TICKS_PER_CYCLE —— 可能为负
cycleIndex(int maxIndex) 59 把可能为负的 cycle() 折回 [0, maxIndex)

⚠️ 负值陷阱(源码注释强调)

Because cycle() can be negative, we must not use the modulus (%) operator here. In Java, modulus of a negative number and a positive number may return a negative value.

Java 的 % 对负数返回负余数,因此 cycleIndex 不能用 cycle() % maxIndex, 必须用「先加若干倍再取模」的方式折回正区间。

同理 ticks() / cycle() 的文档注释都写明 may be negative(向上滚动导致)。

相关条目

源码

  • src/main/java/com/github/dcysteine/neicustomdiagram/api/diagram/DiagramState.java