頭と尻尾はくれてやる!

iOSアプリなどの開発日記です


マイクラのmodでScreenにスライダーを置く

Minecraftのmod(forge 1.17.1-37.1.1)でスライダーを使いたいのでvanillaのコードを参考に試してみた。
Buttonみたいにさくっと使えるとラクだけど機能が複雑なのかvanillaでは
AbstractSliderButton
を継承したクラスで処理を記述してた。

↑動作の様子はこんな感じ。
コードは以下のような感じで。CycleButtonの時と同じでScreenのサブクラス内に記述してる。
   public float mySliderValue = 0F;
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");
}
}
スライダーの上に数字を表示させてるが、MyScreen (Screenのサブクラス)内のrender内でdrawStringしてるだけで表示を更新してくれる。

(A)のところでsuperに渡す引数のComponentにTextComponentのオブジェクトを渡すと、

TextComponentを表示させたSlider

↑こんな感じで文字を表示する。この使い方はあまり実用性はないかもしれないが。
スポンサーサイト




<< SwiftUIのTextFieldで指定の桁数で浮動小数点数を入力する  TopPage  マイクラのCycleButton.builderの最小実装 >>

コメント


管理者にだけ表示を許可する
 

トラックバック

トラックバックURL
https://ringsbell.blog.fc2.com/tb.php/1375-3f75d332




Copyright ©頭と尻尾はくれてやる!. Powered by FC2 Blog. Template by eriraha.