Mindustry/tools/build.gradle

369 lines
12 KiB
Groovy
Raw Normal View History

2018-06-16 05:06:05 +07:00
apply plugin: "java"
sourceCompatibility = 1.8
2019-04-08 20:03:18 +07:00
sourceSets.main.java.srcDirs = ["src/"]
2018-06-16 05:06:05 +07:00
2019-06-06 08:29:34 +07:00
import com.badlogic.gdx.graphics.Color
2018-06-16 05:06:05 +07:00
import com.badlogic.gdx.tools.texturepacker.TexturePacker
2019-10-17 01:36:46 +07:00
import com.badlogic.gdx.utils.IntArray
import com.badlogic.gdx.utils.IntIntMap
import com.badlogic.gdx.utils.IntMap
2018-06-16 05:06:05 +07:00
import javax.imageio.ImageIO
2019-10-17 01:36:46 +07:00
import java.awt.*
2019-04-08 20:03:18 +07:00
import java.awt.image.BufferedImage
2019-10-17 01:36:46 +07:00
import java.util.List
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
2018-11-13 07:03:23 +07:00
def genFolder = "../core/assets-raw/sprites_out/generated/"
2019-03-03 11:06:14 +07:00
def doAntialias = !project.hasProperty("disableAntialias")
def colorMap = new IntMap<List<Color>>(), colorIndexMap = new IntIntMap()
def transformColors = { List<List<String>> list ->
list.each{ colors ->
def newColors = []
colors.each{ hexc ->
newColors += Color.valueOf(hexc)
}
newColors.each{ color ->
colorMap.put(Color.argb8888(color), newColors)
colorIndexMap.put(Color.argb8888(color), newColors.indexOf(color))
}
}
}
transformColors([["6e7080", "989aa4", "b0bac0"], ["bc5452", "ea8878", "feb380"], ["dea158", "f8c266", "ffe18f"], ["feb380", "ea8878", "bc5452"]])
2019-02-07 21:36:39 +07:00
2019-04-08 20:03:18 +07:00
def antialias = { File file ->
if(!doAntialias || file.lastModified() <= 1000) return
2019-02-07 21:36:39 +07:00
2019-01-17 10:45:51 +07:00
def image = ImageIO.read(file)
def out = ImageIO.read(file)
def getRGB = { int ix, int iy ->
2019-01-28 11:26:07 +07:00
//if(ix <= 0 || iy <= 0 || ix >= image.width || iy >= image.height) return 0
2019-01-17 10:45:51 +07:00
return image.getRGB(Math.max(Math.min(ix, image.width - 1), 0), Math.max(Math.min(iy, image.height - 1), 0))
}
def color = new Color()
def sum = new Color()
def suma = new Color()
2019-10-17 01:36:46 +07:00
int[] p = new int[9]
2019-01-17 10:45:51 +07:00
2019-04-08 20:03:18 +07:00
for(int x = 0; x < image.getWidth(); x++){
2019-01-17 10:45:51 +07:00
for(int y = 0; y < image.getHeight(); y++){
2019-04-16 23:15:06 +07:00
int A = getRGB(x - 1, y + 1),
2019-01-17 10:45:51 +07:00
B = getRGB(x, y + 1),
C = getRGB(x + 1, y + 1),
D = getRGB(x - 1, y),
E = getRGB(x, y),
F = getRGB(x + 1, y),
G = getRGB(x - 1, y - 1),
H = getRGB(x, y - 1),
I = getRGB(x + 1, y - 1)
2019-10-17 01:36:46 +07:00
Arrays.fill(p, E);
if(D == B && D != H && B != F) p[0] = D
if((D == B && D != H && B != F && E != C) || (B == F && B != D && F != H && E != A)) p[1] = B
if(B == F && B != D && F != H) p[2] = F
if((H == D && H != F && D != B && E != A) || (D == B && D != H && B != F && E != G)) p[3] = D
if((B == F && B != D && F != H && E != I) || (F == H && F != B && H != D && E != C)) p[5] = F
if(H == D && H != F && D != B) p[6] = D
if((F == H && F != B && H != D && E != G) || (H == D && H != F && D != B && E != I)) p[7] = H
if(F == H && F != B && H != D) p[8] = F
2019-01-17 10:45:51 +07:00
suma.set(0)
2019-01-17 10:45:51 +07:00
2019-10-17 01:36:46 +07:00
for(int val : p){
2019-01-17 10:45:51 +07:00
Color.argb8888ToColor(color, val)
2019-04-08 20:03:18 +07:00
suma.r += color.r * color.a
suma.g += color.g * color.a
suma.b += color.b * color.a
suma.a += color.a
}
2019-04-08 20:03:18 +07:00
suma.mul(suma.a <= 0.001f ? 0f : (float) (1f / suma.a))
float total = 0
sum.set(0)
2019-01-17 10:45:51 +07:00
2019-10-17 01:36:46 +07:00
for(int val : p){
Color.argb8888ToColor(color, val)
float a = color.a
2019-04-08 20:03:18 +07:00
color.lerp(suma, (float) (1f - a))
sum.r += color.r
sum.g += color.g
sum.b += color.b
sum.a += a
total += 1f
2019-01-17 10:45:51 +07:00
}
2019-04-08 20:03:18 +07:00
sum.mul((float) (1f / total))
2019-01-17 10:45:51 +07:00
int result = Color.argb8888(sum)
out.setRGB(x, y, result)
sum.set(0)
}
}
ImageIO.write(out, "png", file)
}
2019-06-06 08:29:34 +07:00
def medianBlur = { File file ->
def image = ImageIO.read(file)
def result = new BufferedImage(image.width, image.height, BufferedImage.TYPE_INT_ARGB)
def radius = 4
IntArray array = new IntArray()
def getRGB = { int ix, int iy ->
return image.getRGB(Math.max(Math.min(ix, image.width - 1), 0), Math.max(Math.min(iy, image.height - 1), 0))
}
for(int x = 0; x < image.width; x++){
for(int y = 0; y < image.height; y++){
array.clear()
for(int dx = -radius; dx <= radius; dx ++){
for(int dy = -radius; dy <= radius; dy ++){
if(dx*dx + dy*dy <= radius*radius){
array.add(getRGB(x + dx, y + dy))
}
}
}
array.sort()
result.setRGB(x, y, array.get((int)(array.size / 2)))
}
}
ImageIO.write(result, "png", file)
}
2019-04-21 06:57:20 +07:00
def scaleImage = { File file ->
def image = ImageIO.read(file)
for(int iteration in 0..1){
2019-06-06 08:29:34 +07:00
def scaled = new BufferedImage(image.width * 2, image.height * 2, BufferedImage.TYPE_INT_ARGB)
2019-04-21 06:57:20 +07:00
def getRGB = { int ix, int iy ->
return image.getRGB(Math.max(Math.min(ix, image.width - 1), 0), Math.max(Math.min(iy, image.height - 1), 0))
}
2019-06-06 08:29:34 +07:00
for(int x = 0; x < image.width; x++){
for(int y = 0; y < image.height; y++){
2019-04-21 06:57:20 +07:00
int p = image.getRGB(x, y)
int p1 = p, p2 = p, p3 = p, p4 = p
int A = getRGB(x - 1, y + 1),
B = getRGB(x, y + 1),
C = getRGB(x + 1, y + 1),
D = getRGB(x - 1, y),
E = getRGB(x, y),
F = getRGB(x + 1, y),
G = getRGB(x - 1, y - 1),
H = getRGB(x, y - 1),
I = getRGB(x + 1, y - 1),
J = getRGB(x, y + 2),
K = getRGB(x - 2, y),
L = getRGB(x + 2, y),
M = getRGB(x, y - 2)
if(B == D && B != F && D != H && (E != A || E == C || E == G || A == J || A == K)) p1 = B
if(B == F & B != D & F != H && (E != C || E == A || E == I || C == J || C == L)) p2 = F
if(D == H & B != D & F != H && (E != G || E == A || E == I || G == K || G == M)) p3 = D
if(F == H & B != F & D != H && (E != I || E == C || E == G || I == L || I == M)) p4 = H
scaled.setRGB(x * 2, y * 2 + 1, p1)
scaled.setRGB(x * 2 + 1, y * 2 + 1, p2)
scaled.setRGB(x * 2, y * 2, p3)
scaled.setRGB(x * 2 + 1, y * 2, p4)
}
}
image = scaled
}
ImageIO.write(image, "png", file)
}
def tileImage = { File file ->
def image = ImageIO.read(file)
def result = new BufferedImage(image.width * 2, image.height * 2, image.getType())
Graphics2D graphics = result.createGraphics()
graphics.drawImage(image, image.width, 0, -image.width, image.height, null)
graphics.drawImage(image, image.width, 0, image.width, image.height, null)
graphics.drawImage(image, image.width, image.height*2, -image.width, -image.height, null)
graphics.drawImage(image, image.width, image.height*2, image.width, -image.height, null)
for(int x = 0; x < result.width; x++){
for(int y = 0; y < result.height; y++){
int p = result.getRGB(x, y)
if(x <= y){
List<Color> list = colorMap.get(p)
int index = colorIndexMap.get(p, -1)
if(index != -1){
int resultIndex = (x == y ? 1 : index == 2 ? 0 : index == 0 ? 2 : 1);
result.setRGB(x, y, Color.argb8888(list[resultIndex]))
}
}
}
}
ImageIO.write(result, "png", file)
}
task swapColors(){
doLast{
2019-04-08 20:03:18 +07:00
if(project.hasProperty("colors")){
2018-10-06 22:56:39 +07:00
def carr = new File(getProperty("colors")).text.split("\n")
def map = [:]
def swaps = 0
2019-04-08 20:03:18 +07:00
carr.each{ str -> map[Color.argb8888(Color.valueOf(str.split("=")[0]))] = Color.argb8888(Color.valueOf(str.split("=")[1])) }
def tmpc = new Color()
2019-04-08 20:03:18 +07:00
fileTree(dir: '../core/assets-raw/sprites', include: "**/*.png").visit{ file ->
2018-10-06 22:56:39 +07:00
if(file.isDirectory()) return
2019-04-08 20:03:18 +07:00
swaps++
def img = ImageIO.read(file.file)
2019-04-08 20:03:18 +07:00
for(x in (0..img.getWidth() - 1)){
for(y in (0..img.getHeight() - 1)){
def c = img.getRGB(x, y)
Color.argb8888ToColor(tmpc, c)
2018-08-21 08:30:37 +07:00
if(tmpc.a < 0.1f) continue
if(map.containsKey(c)){
2019-04-08 20:03:18 +07:00
img.setRGB(x, y, (int) map.get(c))
}
}
}
ImageIO.write(img, "png", file.file)
}
println "Swapped $swaps images."
}else{
2018-10-06 22:56:39 +07:00
throw new InvalidUserDataException("No replacement colors specified. Use -Pcolors=\"<path to color file>\"")
}
}
}
2019-08-04 20:41:25 +07:00
task antialiasImages(){
doLast{
for(def img : project.getProperty("images").split(",")){
println(project.getProperty("startdir") + "/" + img)
antialias(new File(project.getProperty("startdir") + "/" + img))
}
}
}
2019-04-21 06:57:20 +07:00
task scaleImages(){
doLast{
for(def img : project.getProperty("images").split(",")){
println(project.getProperty("startdir") + "/" + img)
scaleImage(new File(project.getProperty("startdir") + "/" + img))
}
}
}
task tileImages(){
doLast{
for(def img : project.getProperty("images").split(",")){
println(project.getProperty("startdir") + "/" + img)
tileImage(new File(project.getProperty("startdir") + "/" + img))
2019-04-21 06:57:20 +07:00
}
}
}
2019-07-04 02:59:50 +07:00
task pack(dependsOn: classes){
2018-11-13 07:03:23 +07:00
doLast{
//cleanup old sprites
delete{
delete "../core/assets-raw/sprites_out/"
}
//copy in new sprites
copy{
from "../core/assets-raw/sprites/"
into "../core/assets-raw/sprites_out/"
}
//run generation task; generate all needed sprites
file(genFolder).mkdirs()
javaexec{
2019-11-28 01:51:07 +07:00
main = "io.anuke.mindustry.tools.ImagePacker"
classpath = sourceSets.main.runtimeClasspath
standardInput = System.in
workingDir = genFolder
}
//upscale icon sprites using different method, which requires an OpenGL context
javaexec{
if(System.getProperty("os.name").toLowerCase().contains("mac")){
jvmArgs "-XstartOnFirstThread"
}
2019-06-30 21:09:20 +07:00
jvmArgs("-Djava.awt.headless=true")
2019-11-28 01:51:07 +07:00
main = "io.anuke.mindustry.tools.Upscaler"
classpath = sourceSets.main.runtimeClasspath
standardInput = System.in
workingDir = "../core/assets-raw/sprites_out/ui/icons"
}
2018-06-16 05:06:05 +07:00
copy{
from "../core/assets-raw/sprites_out/ui/icons"
into "../core/assets-raw/sprites_out/ui/"
}
delete{
delete "../core/assets-raw/sprites_out/ui/icons"
}
2019-10-17 01:36:46 +07:00
ExecutorService executor = Executors.newFixedThreadPool(16)
//antialias everything except UI elements
fileTree(dir: '../core/assets-raw/sprites_out/', include: "**/*.png").visit{ file ->
2019-10-05 06:14:45 +07:00
if(file.isDirectory() || file.toString().replace("\\", "/").contains("zones/") || (file.toString().replace("\\", "/").contains("/ui/") && file.toString().startsWith("icon-"))) return
2019-10-17 01:36:46 +07:00
executor.submit{
antialias(file.file)
}
}
executor.shutdown();
try{
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
}catch(InterruptedException e){
e.printStackTrace()
2018-11-14 10:11:29 +07:00
}
//pack normal sprites
2018-11-13 07:03:23 +07:00
TexturePacker.process("core/assets-raw/sprites_out/", "core/assets/sprites/", "sprites.atlas")
2018-11-15 05:14:22 +07:00
}
2019-06-21 03:27:57 +07:00
}
2019-04-08 20:03:18 +07:00
task genSprites(dependsOn: classes, type: JavaExec){
finalizedBy 'antialiasGen'
2019-04-08 20:03:18 +07:00
2019-11-28 01:51:07 +07:00
main = "io.anuke.mindustry.tools.ImagePacker"
2018-06-16 05:06:05 +07:00
classpath = sourceSets.main.runtimeClasspath
2019-06-30 21:09:20 +07:00
jvmArgs("-Djava.awt.headless=true")
2018-06-16 05:06:05 +07:00
standardInput = System.in
2018-11-13 05:37:25 +07:00
workingDir = genFolder
2018-06-16 05:06:05 +07:00
}
2019-04-08 20:03:18 +07:00
task updateBundles(dependsOn: classes, type: JavaExec){
2018-11-13 05:37:25 +07:00
file(genFolder).mkdirs()
2019-11-28 01:51:07 +07:00
main = "io.anuke.mindustry.tools.BundleLauncher"
classpath = sourceSets.main.runtimeClasspath
standardInput = System.in
workingDir = "../core/assets/bundles/"
}