moved white box tex to AppLoader; textButton now has alignment option

This commit is contained in:
minjaesong
2018-12-09 04:36:53 +09:00
parent 59c6876849
commit d0e0f8dd8d
6 changed files with 59 additions and 34 deletions

View File

@@ -41,7 +41,19 @@ public class AppLoader implements ApplicationListener {
private static AppLoader INSTANCE = null;
private AppLoader() { }
private static Screen injectScreen = null;
public AppLoader(LwjglApplicationConfiguration appConfig, Screen injectScreen) {
AppLoader.injectScreen = injectScreen;
AppLoader.appConfig = appConfig;
}
public AppLoader(LwjglApplicationConfiguration appConfig) {
AppLoader.appConfig = appConfig;
}
public AppLoader() {
}
public static AppLoader getINSTANCE() {
if (INSTANCE == null) {
@@ -98,7 +110,7 @@ public class AppLoader implements ApplicationListener {
public static void main(String[] args) {
ShaderProgram.pedantic = false;
appConfig = new LwjglApplicationConfiguration();
LwjglApplicationConfiguration appConfig = new LwjglApplicationConfiguration();
appConfig.vSyncEnabled = false;
appConfig.resizable = false;//true;
//appConfig.width = 1072; // IMAX ratio
@@ -110,7 +122,7 @@ public class AppLoader implements ApplicationListener {
appConfig.title = GAME_NAME;
appConfig.forceExit = false;
new LwjglApplication(new AppLoader(), appConfig);
new LwjglApplication(new AppLoader(appConfig), appConfig);
}
@@ -128,6 +140,8 @@ public class AppLoader implements ApplicationListener {
public Screen screen;
public static Texture textureWhiteSquare;
private void initViewPort(int width, int height) {
// Set Y to point downwards
camera.setToOrtho(true, width, height);
@@ -153,6 +167,10 @@ public class AppLoader implements ApplicationListener {
initViewPort(appConfig.width, appConfig.height);
textureWhiteSquare = new Texture(Gdx.files.internal("assets/graphics/ortho_line_tex_2px.tga"));
textureWhiteSquare.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest);
shaderBayerSkyboxFill = new ShaderProgram(Gdx.files.internal("assets/4096.vert"), Gdx.files.internal("assets/4096_bayer_skyboxfill.frag"));
shaderHicolour = new ShaderProgram(Gdx.files.internal("assets/4096.vert"), Gdx.files.internal("assets/hicolour.frag"));
shaderColLUT = new ShaderProgram(Gdx.files.internal("assets/4096.vert"), Gdx.files.internal("assets/passthru.frag"));
@@ -182,6 +200,12 @@ public class AppLoader implements ApplicationListener {
TextureRegionPack.Companion.setGlobalFlipY(true);
fontGame = new GameFontBase("assets/graphics/fonts/terrarum-sans-bitmap", false, true, Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest, false, 128, false);
// if there is a predefined screen, open that screen after my init process
if (injectScreen != null) {
setScreen(injectScreen);
}
}
@Override
@@ -200,6 +224,7 @@ public class AppLoader implements ApplicationListener {
FrameBufferManager.begin(renderFBO);
setCameraPosition(0, 0);
// if there's no predefined screen, default to the actual game's screen, and set the screen as the one
if (screen == null) {
shaderBayerSkyboxFill.begin();
shaderBayerSkyboxFill.setUniformMatrix("u_projTrans", camera.combined);

View File

@@ -222,10 +222,6 @@ object Terrarum : Screen {
lateinit var shaderRGBOnly: ShaderProgram
lateinit var shaderAtoGrey: ShaderProgram
lateinit var textureWhiteSquare: Texture
lateinit var testTexture: Texture
@@ -357,10 +353,6 @@ object Terrarum : Screen {
fontSmallNumbers = TinyAlphNum
textureWhiteSquare = Texture(Gdx.files.internal("assets/graphics/ortho_line_tex_2px.tga"))
textureWhiteSquare.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest)
ShaderProgram.pedantic = false
shaderBlur = ShaderProgram(Gdx.files.internal("assets/blur.vert"), Gdx.files.internal("assets/blur.frag"))
@@ -788,7 +780,7 @@ fun Float.round(): Float {
// ShapeRenderer alternative for rects
fun SpriteBatch.fillRect(x: Float, y: Float, w: Float, h: Float) {
this.draw(Terrarum.textureWhiteSquare, x, y, w, h)
this.draw(AppLoader.textureWhiteSquare, x, y, w, h)
}
inline fun SpriteBatch.drawStraightLine(x: Float, y: Float, otherEnd: Float, thickness: Float, isVertical: Boolean) {
if (!isVertical)
@@ -840,11 +832,11 @@ object BlendMode {
const val NORMAL = "normal"
//const val MAX = "GL_MAX" // not supported by GLES -- use shader
fun resolve(mode: String) {
fun resolve(mode: String, batch: SpriteBatch? = null) {
when (mode) {
SCREEN -> blendScreen()
MULTIPLY -> blendMul()
NORMAL -> blendNormal()
SCREEN -> blendScreen(batch)
MULTIPLY -> blendMul(batch)
NORMAL -> blendNormal(batch)
//MAX -> blendLightenOnly() // not supported by GLES -- use shader
else -> throw Error("Unknown blend mode: $mode")
}

View File

@@ -78,9 +78,9 @@ abstract class UICanvas(
}
/** Override this for the actual update. You must update uiItems by yourself. */
/** Override this for the actual update. Note that you must update uiItems by yourself. */
abstract fun updateUI(delta: Float)
/** Override this for the actual render. You must render uiItems by yourself. */
/** Override this for the actual render. Note that you must render uiItems by yourself. */
abstract fun renderUI(batch: SpriteBatch, camera: Camera)
/**

View File

@@ -4,6 +4,7 @@ import com.badlogic.gdx.graphics.Camera
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import net.torvald.terrarum.Terrarum
import net.torvald.terrarum.floor
import net.torvald.terrarum.ui.UIItemTextButton.Companion.Alignment
class UIItemTextArea(
parentUI: UICanvas,
@@ -13,16 +14,9 @@ class UIItemTextArea(
override val height: Int,
val lineGap: Int = 0,
val lineCount: Int = ((height + lineGap) / Terrarum.fontGame.lineHeight).toInt(),
val align: UIItemTextArea.Align = Align.LEFT
val align: Alignment = Alignment.LEFT
) : UIItem(parentUI) {
enum class Align {
LEFT, CENTRE, RIGHT
}
private var entireText: List<String> = listOf("") // placeholder
var scrollPos = 0
@@ -45,9 +39,9 @@ class UIItemTextArea(
val textWidth = Terrarum.fontGame.getWidth(entireText[i])
when (align) {
Align.LEFT -> Terrarum.fontGame.draw(batch, entireText[i], posX.toFloat(), posY + yPtr * (Terrarum.fontGame.lineHeight + lineGap))
Align.CENTRE -> Terrarum.fontGame.draw(batch, entireText[i], posX + ((width - textWidth) / 2f).floor(), posY + yPtr * (Terrarum.fontGame.lineHeight + lineGap))
Align.RIGHT -> Terrarum.fontGame.draw(batch, entireText[i], posX + width - textWidth.toFloat(), posY + yPtr * (Terrarum.fontGame.lineHeight + lineGap))
Alignment.LEFT -> Terrarum.fontGame.draw(batch, entireText[i], posX.toFloat(), posY + yPtr * (Terrarum.fontGame.lineHeight + lineGap))
Alignment.CENTRE -> Terrarum.fontGame.draw(batch, entireText[i], posX + ((width - textWidth) / 2f).floor(), posY + yPtr * (Terrarum.fontGame.lineHeight + lineGap))
Alignment.RIGHT -> Terrarum.fontGame.draw(batch, entireText[i], posX + width - textWidth.toFloat(), posY + yPtr * (Terrarum.fontGame.lineHeight + lineGap))
}
}
}

View File

@@ -28,7 +28,9 @@ open class UIItemTextButton(
val highlightBackBlendMode: String = BlendMode.MULTIPLY,
val inactiveCol: Color = defaultInactiveCol,
val preGapX: Int = 0,
val postGapX: Int = 0
val postGapX: Int = 0,
val alignment: Alignment = Alignment.CENTRE
) : UIItem(parentUI) {
companion object {
@@ -36,6 +38,10 @@ open class UIItemTextButton(
val height = font.lineHeight.toInt() * 2
val defaultInactiveCol: Color = Color(0xc8c8c8_ff.toInt())
val defaultHighlightCol: Color = Color(0x00f8ff_ff)
enum class Alignment {
CENTRE, LEFT, RIGHT
}
}
/** Actually displayed text (changes with the app language) */
@@ -74,7 +80,11 @@ open class UIItemTextButton(
font.draw(batch,
label,
posX.toFloat() + width.minus(textW).div(2) + (preGapX - postGapX).div(2),
when (alignment) {
Alignment.CENTRE -> posX.toFloat() + width.minus(textW).div(2) + (preGapX - postGapX).div(2)
Alignment.LEFT -> posX.toFloat() + preGapX
Alignment.RIGHT -> width - postGapX - textW.toFloat()
},
posY.toFloat() + height / 4
)
}

View File

@@ -40,7 +40,9 @@ class UIItemTextButtonList(
val inactiveCol: Color = Color(0xc0c0c0_ff.toInt()),
val backgroundCol: Color = Color(0x242424_80),
val backgroundBlendMode: String = BlendMode.NORMAL,
val kinematic: Boolean = false
val kinematic: Boolean = false,
val alignment: UIItemTextButton.Companion.Alignment = UIItemTextButton.Companion.Alignment.CENTRE
) : UIItem(parentUI) {
val iconToTextGap = 20
@@ -71,7 +73,8 @@ class UIItemTextButtonList(
highlightBackBlendMode = highlightBackBlendMode,
inactiveCol = inactiveCol,
preGapX = pregap,
postGapX = postgap
postGapX = postgap,
alignment = alignment
)
}
else {
@@ -89,7 +92,8 @@ class UIItemTextButtonList(
highlightBackBlendMode = activeBackBlendMode, // we are using custom highlighter
inactiveCol = inactiveCol,
preGapX = pregap,
postGapX = postgap
postGapX = postgap,
alignment = alignment
)
}
}