diff --git a/core/src/ru/deadsoftware/cavedroid/misc/utils/RenderingUtils.kt b/core/src/ru/deadsoftware/cavedroid/misc/utils/RenderingUtils.kt
index d197229578bfb8a92e989eb228b05a2bf05f0c69..917f69b591933ee9a6bfc83354e5f012c0894363 100644 (file)
package ru.deadsoftware.cavedroid.misc.utils
package ru.deadsoftware.cavedroid.misc.utils
+import com.badlogic.gdx.Gdx
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.Color
+import com.badlogic.gdx.graphics.GL20
import com.badlogic.gdx.graphics.g2d.GlyphLayout
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.math.Rectangle
import com.badlogic.gdx.graphics.g2d.GlyphLayout
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.math.Rectangle
+import ru.deadsoftware.cavedroid.MainConfig
import ru.deadsoftware.cavedroid.misc.Assets
private fun Rectangle.shifted(shift: Float) = Rectangle(x + shift, y, width, height)
import ru.deadsoftware.cavedroid.misc.Assets
private fun Rectangle.shifted(shift: Float) = Rectangle(x + shift, y, width, height)
Assets.minecraftFont.color = color
return Assets.minecraftFont.draw(this, str, x, y)
}
Assets.minecraftFont.color = color
return Assets.minecraftFont.draw(this, str, x, y)
}
+
+/**
+ * Parses hex color string into [Color]
+ * Format is strictly #FFFFFF
+ */
+fun colorFromHexString(hex: String): Color {
+ if (hex[0] != '#' || hex.length != 7) {
+ return Color.WHITE
+ }
+
+ var rgba = try {
+ hex.substring(1).toInt(16)
+ } catch (e: NumberFormatException) {
+ 0xffffff
+ }
+
+ rgba = (rgba shl 8) or 0xFF
+ return Color(rgba)
+}
+
+fun SpriteBatch.withScissors(
+ mainConfig: MainConfig,
+ x: Float,
+ y: Float,
+ width: Float,
+ height: Float,
+ block: () -> Unit
+) {
+ val scaleX = Gdx.graphics.width / mainConfig.width
+ val scaleY = Gdx.graphics.height / mainConfig.height
+
+ flush()
+ Gdx.gl.glEnable(GL20.GL_SCISSOR_TEST)
+ Gdx.gl.glScissor(
+ /* x = */ (x * scaleX).toInt(),
+ /* y = */ ((mainConfig.height - y - height) * scaleY).toInt(),
+ /* width = */ (width * scaleX).toInt(),
+ /* height = */ (height * scaleY).toInt()
+ )
+ block.invoke()
+ flush()
+ Gdx.gl.glDisable(GL20.GL_SCISSOR_TEST)
+}