Skip to content

Commit

Permalink
Abstract prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
Wide-Cat committed Jun 13, 2024
1 parent 6704302 commit e2c0858
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 188 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,120 +5,41 @@

package meteordevelopment.meteorclient.utils.render.prompts;

import com.mojang.blaze3d.systems.RenderSystem;
import meteordevelopment.meteorclient.gui.GuiTheme;
import meteordevelopment.meteorclient.gui.GuiThemes;
import meteordevelopment.meteorclient.gui.WindowScreen;
import meteordevelopment.meteorclient.gui.widgets.containers.WHorizontalList;
import meteordevelopment.meteorclient.gui.widgets.pressable.WButton;
import meteordevelopment.meteorclient.gui.widgets.pressable.WCheckbox;
import meteordevelopment.meteorclient.systems.config.Config;
import net.minecraft.client.gui.screen.Screen;

import java.util.ArrayList;
import java.util.List;

import static meteordevelopment.meteorclient.MeteorClient.mc;

public class OkPrompt {
private final GuiTheme theme;
private final Screen parent;

private String title = "";
private final List<String> messages = new ArrayList<>();
private boolean dontShowAgainCheckboxVisible = true;
private String id = null;

public class OkPrompt extends Prompt<OkPrompt> {
private Runnable onOk = () -> {};

private OkPrompt() {
this(GuiThemes.get(), mc.currentScreen);
}

private OkPrompt(GuiTheme theme, Screen parent) {
this.theme = theme;
this.parent = parent;
super(theme, parent);
}

public static OkPrompt create() {
return new OkPrompt();
return new OkPrompt(GuiThemes.get(), mc.currentScreen);
}

public static OkPrompt create(GuiTheme theme, Screen parent) {
return new OkPrompt(theme, parent);
}

public OkPrompt title(String title) {
this.title = title;
return this;
}

public OkPrompt message(String message) {
this.messages.add(message);
return this;
}

public OkPrompt message(String message, Object... args) {
this.messages.add(String.format(message, args));
return this;
}

public OkPrompt dontShowAgainCheckboxVisible(boolean visible) {
this.dontShowAgainCheckboxVisible = visible;
return this;
}

public OkPrompt id(String from) {
this.id = from;
return this;
}

public OkPrompt onOk(Runnable action) {
this.onOk = action;
return this;
}

public boolean show() {
if (id == null) this.id(this.title);
if (Config.get().dontShowAgainPrompts.contains(id)) return false;

if (!RenderSystem.isOnRenderThread()) {
RenderSystem.recordRenderCall(() -> mc.setScreen(new PromptScreen(theme)));
}
else {
mc.setScreen(new PromptScreen(theme));
}

return true;
}

private class PromptScreen extends WindowScreen {
public PromptScreen(GuiTheme theme) {
super(theme, OkPrompt.this.title);

this.parent = OkPrompt.this.parent;
}

@Override
public void initWidgets() {
for (String line : messages) add(theme.label(line)).expandX();
add(theme.horizontalSeparator()).expandX();

WCheckbox dontShowAgainCheckbox;

if (dontShowAgainCheckboxVisible) {
WHorizontalList checkboxContainer = add(theme.horizontalList()).expandX().widget();
dontShowAgainCheckbox = checkboxContainer.add(theme.checkbox(false)).widget();
checkboxContainer.add(theme.label("Don't show this again.")).expandX();
} else dontShowAgainCheckbox = null;

WHorizontalList list = add(theme.horizontalList()).expandX().widget();
WButton okButton = list.add(theme.button("Ok")).expandX().widget();
okButton.action = () -> {
if (dontShowAgainCheckbox != null && dontShowAgainCheckbox.checked) Config.get().dontShowAgainPrompts.add(id);
onOk.run();
close();
};
}
@Override
protected void initialiseWidgets(PromptScreen screen) {
WButton okButton = screen.list.add(theme.button("Ok")).expandX().widget();
okButton.action = () -> {
if (screen.dontShowAgainCheckbox != null && screen.dontShowAgainCheckbox.checked) Config.get().dontShowAgainPrompts.add(id);
onOk.run();
screen.close();
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package meteordevelopment.meteorclient.utils.render.prompts;

import com.mojang.blaze3d.systems.RenderSystem;
import meteordevelopment.meteorclient.gui.GuiTheme;
import meteordevelopment.meteorclient.gui.WindowScreen;
import meteordevelopment.meteorclient.gui.widgets.containers.WHorizontalList;
import meteordevelopment.meteorclient.gui.widgets.pressable.WCheckbox;
import meteordevelopment.meteorclient.systems.config.Config;
import net.minecraft.client.gui.screen.Screen;

import java.util.ArrayList;
import java.util.List;

import static meteordevelopment.meteorclient.MeteorClient.mc;

@SuppressWarnings("unchecked") // cant instantiate a Prompt directly so this is fine
public abstract class Prompt<T> {
final GuiTheme theme;
final Screen parent;

String title = "";
final List<String> messages = new ArrayList<>();
boolean dontShowAgainCheckboxVisible = true;
String id = null;

protected Prompt(GuiTheme theme, Screen parent) {
this.theme = theme;
this.parent = parent;
}

public T title(String title) {
this.title = title;
return (T) this;
}

public T message(String message) {
this.messages.add(message);
return (T) this;
}

public T message(String message, Object... args) {
this.messages.add(String.format(message, args));
return (T) this;
}

public T dontShowAgainCheckboxVisible(boolean visible) {
this.dontShowAgainCheckboxVisible = visible;
return (T) this;
}

public T id(String from) {
this.id = from;
return (T) this;
}

public boolean show() {
if (id == null) this.id(this.title);
if (Config.get().dontShowAgainPrompts.contains(id)) return false;

if (!RenderSystem.isOnRenderThread()) {
RenderSystem.recordRenderCall(() -> mc.setScreen(new PromptScreen(theme)));
}
else {
mc.setScreen(new PromptScreen(theme));
}

return true;
}

abstract void initialiseWidgets(PromptScreen screen);

protected class PromptScreen extends WindowScreen {
WCheckbox dontShowAgainCheckbox;
WHorizontalList list;

public PromptScreen(GuiTheme theme) {
super(theme, Prompt.this.title);

this.parent = Prompt.this.parent;
}

@Override
public void initWidgets() {
for (String line : messages) add(theme.label(line)).expandX();
add(theme.horizontalSeparator()).expandX();

if (dontShowAgainCheckboxVisible) {
WHorizontalList checkboxContainer = add(theme.horizontalList()).expandX().widget();
dontShowAgainCheckbox = checkboxContainer.add(theme.checkbox(false)).widget();
checkboxContainer.add(theme.label("Don't show this again.")).expandX();
} else dontShowAgainCheckbox = null;

list = add(theme.horizontalList()).expandX().widget();

initialiseWidgets(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,75 +5,30 @@

package meteordevelopment.meteorclient.utils.render.prompts;

import com.mojang.blaze3d.systems.RenderSystem;
import meteordevelopment.meteorclient.gui.GuiTheme;
import meteordevelopment.meteorclient.gui.GuiThemes;
import meteordevelopment.meteorclient.gui.WindowScreen;
import meteordevelopment.meteorclient.gui.widgets.containers.WHorizontalList;
import meteordevelopment.meteorclient.gui.widgets.pressable.WButton;
import meteordevelopment.meteorclient.gui.widgets.pressable.WCheckbox;
import meteordevelopment.meteorclient.systems.config.Config;
import net.minecraft.client.gui.screen.Screen;

import java.util.ArrayList;
import java.util.List;

import static meteordevelopment.meteorclient.MeteorClient.mc;

public class YesNoPrompt {
private final GuiTheme theme;
private final Screen parent;

private String title = "";
private final List<String> messages = new ArrayList<>();
private boolean dontShowAgainCheckboxVisible = true;
private String id = null;

public class YesNoPrompt extends Prompt<YesNoPrompt> {
private Runnable onYes = () -> {};
private Runnable onNo = () -> {};

private YesNoPrompt() {
this(GuiThemes.get(), mc.currentScreen);
}

private YesNoPrompt(GuiTheme theme, Screen parent) {
this.theme = theme;
this.parent = parent;
super(theme, parent);
}

public static YesNoPrompt create() {
return new YesNoPrompt();
return new YesNoPrompt(GuiThemes.get(), mc.currentScreen);
}

public static YesNoPrompt create(GuiTheme theme, Screen parent) {
return new YesNoPrompt(theme, parent);
}

public YesNoPrompt title(String title) {
this.title = title;
return this;
}

public YesNoPrompt message(String message) {
this.messages.add(message);
return this;
}

public YesNoPrompt message(String message, Object... args) {
this.messages.add(String.format(message, args));
return this;
}

public YesNoPrompt dontShowAgainCheckboxVisible(boolean visible) {
this.dontShowAgainCheckboxVisible = visible;
return this;
}

public YesNoPrompt id(String from) {
this.id = from;
return this;
}

public YesNoPrompt onYes(Runnable action) {
this.onYes = action;
return this;
Expand All @@ -84,55 +39,20 @@ public YesNoPrompt onNo(Runnable action) {
return this;
}

public boolean show() {
if (id == null) this.id(this.title);
if (Config.get().dontShowAgainPrompts.contains(id)) return false;

if (!RenderSystem.isOnRenderThread()) {
RenderSystem.recordRenderCall(() -> mc.setScreen(new PromptScreen(theme)));
}
else {
mc.setScreen(new PromptScreen(theme));
}

return true;
}

private class PromptScreen extends WindowScreen {
public PromptScreen(GuiTheme theme) {
super(theme, YesNoPrompt.this.title);

this.parent = YesNoPrompt.this.parent;
}

@Override
public void initWidgets() {
for (String line : messages) add(theme.label(line)).expandX();
add(theme.horizontalSeparator()).expandX();

WCheckbox dontShowAgainCheckbox;

if (dontShowAgainCheckboxVisible) {
WHorizontalList checkboxContainer = add(theme.horizontalList()).expandX().widget();
dontShowAgainCheckbox = checkboxContainer.add(theme.checkbox(false)).widget();
checkboxContainer.add(theme.label("Don't show this again.")).expandX();
} else dontShowAgainCheckbox = null;

WHorizontalList list = add(theme.horizontalList()).expandX().widget();

WButton yesButton = list.add(theme.button("Yes")).expandX().widget();
yesButton.action = () -> {
if (dontShowAgainCheckbox != null && dontShowAgainCheckbox.checked) Config.get().dontShowAgainPrompts.add(id);
onYes.run();
close();
};

WButton noButton = list.add(theme.button("No")).expandX().widget();
noButton.action = () -> {
if (dontShowAgainCheckbox != null && dontShowAgainCheckbox.checked) Config.get().dontShowAgainPrompts.add(id);
onNo.run();
close();
};
}
@Override
protected void initialiseWidgets(PromptScreen screen) {
WButton yesButton = screen.list.add(theme.button("Yes")).expandX().widget();
yesButton.action = () -> {
if (screen.dontShowAgainCheckbox != null && screen.dontShowAgainCheckbox.checked) Config.get().dontShowAgainPrompts.add(id);
onYes.run();
screen.close();
};

WButton noButton = screen.list.add(theme.button("No")).expandX().widget();
noButton.action = () -> {
if (screen.dontShowAgainCheckbox != null && screen.dontShowAgainCheckbox.checked) Config.get().dontShowAgainPrompts.add(id);
onNo.run();
screen.close();
};
}
}

0 comments on commit e2c0858

Please sign in to comment.