Skip to content

Commit

Permalink
Use gdx ClassReflection methods so inGameConsole can be used with gwt…
Browse files Browse the repository at this point in the history
… backend (#59)

* Use gdx ClassReflection methods so console can be used with gwt backend

(cherry picked from commit 2096276)

* Revert changes to overridden scrolled method to work with libgdx 1.9.9
  • Loading branch information
6money authored Sep 17, 2021
1 parent f8e3353 commit 6ef713e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
9 changes: 6 additions & 3 deletions src/com/strongjoshua/console/ConsoleContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.reflect.ClassReflection;

public class ConsoleContext {
private Table root;
Expand All @@ -18,15 +19,17 @@ public class ConsoleContext {

ConsoleContext (Class<? extends Table> tableClass, Class<? extends Label> labelClass, Skin skin, String background) {
try {
root = tableClass.newInstance();
root = ClassReflection.newInstance(tableClass);
} catch (Exception e) {
throw new RuntimeException("Table class does not support empty constructor.");
}
try {
copy = labelClass.getConstructor(CharSequence.class, Skin.class).newInstance("Copy", skin);
copy = (Label) ClassReflection.getConstructor(labelClass, CharSequence.class, Skin.class)
.newInstance("Copy", skin);
} catch (Exception e) {
try {
copy = labelClass.getConstructor(CharSequence.class).newInstance("Copy");
copy = (Label) ClassReflection.getConstructor(labelClass, CharSequence.class)
.newInstance("Copy");
} catch (Exception e2) {
throw new RuntimeException(
"Label class does not support either (<CharSequence>, <Skin>) or (<CharSequence>) constructors.");
Expand Down
10 changes: 4 additions & 6 deletions src/com/strongjoshua/console/ConsoleUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,10 @@ public static String exceptionToString (final Throwable throwable) {
Throwable cause = throwable;

while (cause != null) {
if (result.length() == 0) {
result.append("\nException in thread \"").append(Thread.currentThread().getName()).append("\" ");
} else {
result.append("\nCaused by: ");
}
result.append(cause.getClass().getCanonicalName()).append(": ").append(cause.getMessage());
result.append("\nCaused by: ")
.append(cause.getClass().getCanonicalName())
.append(": ")
.append(cause.getMessage());

for (final StackTraceElement traceElement : cause.getStackTrace()) {
result.append("\n\tat ").append(traceElement.toString());
Expand Down
34 changes: 21 additions & 13 deletions src/com/strongjoshua/console/GUIConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.SnapshotArray;
import com.badlogic.gdx.utils.reflect.ClassReflection;

/**
* A simple console that allows live logging, and live execution of methods, from within an application. Please see the <a
Expand Down Expand Up @@ -159,10 +160,12 @@ public GUIConsole (Skin skin, boolean useMultiplexer, int keyID, Class<? extends
display.showSubmit(false);

try {
consoleWindow = windowClass.getConstructor(String.class, Skin.class).newInstance("Console", skin);
consoleWindow = (Window) ClassReflection.getConstructor(Window.class, String.class, Skin.class)
.newInstance("Console", skin);
} catch (Exception e) {
try {
consoleWindow = windowClass.getConstructor(String.class).newInstance("Console");
consoleWindow = (Window) ClassReflection.getConstructor(Window.class, String.class)
.newInstance("Console");
} catch (Exception e2) {
throw new RuntimeException("Window class does not support either (<String>, <Skin>) or (<String>) constructors.");
}
Expand Down Expand Up @@ -410,7 +413,7 @@ private class ConsoleDisplay {

ConsoleDisplay (Skin skin) {
try {
root = tableClass.newInstance();
root = ClassReflection.newInstance(tableClass);
} catch (Exception e) {
throw new RuntimeException("Table class does not support empty constructor.");
}
Expand All @@ -428,23 +431,26 @@ private class ConsoleDisplay {
labels = new Array<Label>();

try {
logEntries = tableClass.newInstance();
logEntries = ClassReflection.newInstance(tableClass);
} catch (Exception e) {
throw new RuntimeException("Table class does not support empty constructor.");
}

try {
input = textFieldClass.getConstructor(String.class, TextFieldStyle.class).newInstance("", tfs);
input = (TextField) ClassReflection.getConstructor(textFieldClass, String.class, TextFieldStyle.class)
.newInstance("", tfs);
} catch (Exception e) {
throw new RuntimeException("TextField class does not support (<String>, <Skin>) constructor.");
}
input.setTextFieldListener(new FieldListener());

try {
submit = textButtonClass.getConstructor(String.class, Skin.class).newInstance("Submit", skin);
submit = (TextButton) ClassReflection.getConstructor(textButtonClass, String.class, Skin.class)
.newInstance("Submit", skin);
} catch (Exception e) {
try {
submit = textButtonClass.getConstructor(String.class).newInstance("Submit");
submit = (TextButton) ClassReflection.getConstructor(textButtonClass, String.class)
.newInstance("Submit");
} catch (Exception e2) {
throw new RuntimeException(
"TextButton class does not support either (<String>, <Skin>) or (<String>) constructors.");
Expand All @@ -457,10 +463,12 @@ private class ConsoleDisplay {
});

try {
scroll = scrollPaneClass.getConstructor(Actor.class, Skin.class).newInstance(logEntries, skin);
scroll = (ScrollPane) ClassReflection.getConstructor(scrollPaneClass, Actor.class, Skin.class)
.newInstance(logEntries, skin);
} catch (Exception e) {
try {
scroll = scrollPaneClass.getConstructor(Actor.class).newInstance(logEntries);
scroll = (ScrollPane) ClassReflection.getConstructor(scrollPaneClass, Actor.class)
.newInstance(logEntries);
} catch (Exception e2) {
throw new RuntimeException(
"ScrollPane class does not support either (<Actor>, <Skin>) or (<Actor>) constructors.");
Expand Down Expand Up @@ -497,12 +505,12 @@ void refresh () {
l = labels.get(i);
} else {
try {
l = labelClass.getConstructor(CharSequence.class, Skin.class, String.class, Color.class)
.newInstance("", skin, fontName, LogLevel.DEFAULT.getColor());
l = (Label) ClassReflection.getConstructor(labelClass, CharSequence.class, Skin.class, String.class, Color.class)
.newInstance("", skin, fontName, LogLevel.DEFAULT.getColor());
} catch (Exception e) {
try {
l = labelClass.getConstructor(CharSequence.class, String.class, Color.class)
.newInstance("", fontName, LogLevel.DEFAULT.getColor());
l = (Label) ClassReflection.getConstructor(labelClass, CharSequence.class, String.class, Color.class)
.newInstance("", fontName, LogLevel.DEFAULT.getColor());
} catch (Exception e2) {
throw new RuntimeException(
"Label class does not support either (<String>, <Skin>, <String>, <Color>) or (<String>, <String>, <Color>) constructors.");
Expand Down

0 comments on commit 6ef713e

Please sign in to comment.