マイクラのmodでScreenにスライダーを置く
Minecraftのmod(forge 1.17.1-37.1.1)でスライダーを使いたいのでvanillaのコードを参考に試してみた。
Buttonみたいにさくっと使えるとラクだけど機能が複雑なのかvanillaでは
AbstractSliderButton
を継承したクラスで処理を記述してた。
コードは以下のような感じで。CycleButtonの時と同じでScreenのサブクラス内に記述してる。
(A)のところでsuperに渡す引数のComponentにTextComponentのオブジェクトを渡すと、

↑こんな感じで文字を表示する。この使い方はあまり実用性はないかもしれないが。
Buttonみたいにさくっと使えるとラクだけど機能が複雑なのかvanillaでは
AbstractSliderButton
を継承したクラスで処理を記述してた。
↑動作の様子はこんな感じ。マイクラのmod(forge1.17)でsliderが動いた。数字もちゃんと取れそう。 #Minecraft pic.twitter.com/rgtZ2W77Q9
— Tatsuya (@yt) February 9, 2022
コードは以下のような感じで。CycleButtonの時と同じでScreenのサブクラス内に記述してる。
public float mySliderValue = 0F;スライダーの上に数字を表示させてるが、MyScreen (Screenのサブクラス)内のrender内でdrawStringしてるだけで表示を更新してくれる。
private void createView() {
this.addRenderableWidget(new MySlider(x, y, w, h, mySliderValue, minValue, maxValue));
}
@OnlyIn(Dist.CLIENT)
class MySlider extends AbstractSliderButton {
private final double minValue;
private final double maxValue;
public MySlider(int x, int y, int w, int h, float initValue, float minValue, float maxValue) {
super(x, y, w, h, TextComponent.EMPTY, 0.0D);
// super(x, y, w, h, new TextComponent("text component"), 0.0D);//(A)
this.minValue = (double)minValue;
this.maxValue = (double)maxValue;
this.value = (double)((Mth.clamp(initValue, minValue, maxValue) - minValue) / (maxValue - minValue));
this.updateMessage();
}
public void applyValue() {
MyScreen.this.mySliderValue = (float)Mth.lerp(Mth.clamp(this.value, 0.0D, 1.0D), this.minValue, this.maxValue);
}
protected void updateMessage() {
System.out.println("updateMessage");
}
public void onClick(double p_89954_, double p_89955_) {
System.out.println("onClick");
}
public void onRelease(double p_89957_, double p_89958_) {
System.out.println("onRelease");
}
}
(A)のところでsuperに渡す引数のComponentにTextComponentのオブジェクトを渡すと、

↑こんな感じで文字を表示する。この使い方はあまり実用性はないかもしれないが。
スポンサーサイト
<< SwiftUIのTextFieldで指定の桁数で浮動小数点数を入力する TopPage マイクラのCycleButton.builderの最小実装 >>
トラックバック
トラックバックURL
https://ringsbell.blog.fc2.com/tb.php/1375-3f75d332
https://ringsbell.blog.fc2.com/tb.php/1375-3f75d332