Created
May 23, 2021 06:19
-
-
Save 5Mixer/c75f20dd97491a41f846f7d62af7050e to your computer and use it in GitHub Desktop.
9-slice-kha
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kha.graphics2.Graphics; | |
import kha.Image; | |
class NineSlice { | |
var radiusSize:Int; | |
var sourceImageEdgeLength:Int; | |
var image:Image; | |
public function new(radiusSize:Int, image:Image) { | |
if (image.width != image.height) { | |
throw 'nine slice requires square image'; | |
} | |
sourceImageEdgeLength = image.width - (radiusSize * 2); | |
if (sourceImageEdgeLength < 0) { | |
throw 'radius size too large for provided image'; | |
} | |
this.radiusSize = radiusSize; | |
this.image = image; | |
} | |
public function render(g:Graphics, x:Int, y:Int, width:Int, height:Int) { | |
// Upper-left quadrant | |
g.drawSubImage(image, x, y, 0, 0, radiusSize, radiusSize); | |
// Upper-middle quadrant | |
g.drawScaledSubImage(image, radiusSize, 0, sourceImageEdgeLength, radiusSize, x+radiusSize, y, width-2*radiusSize, radiusSize); | |
// Upper-right quadrant | |
g.drawSubImage(image, x + width - radiusSize, y, image.width - radiusSize, 0, radiusSize, radiusSize); | |
// Middle-left quadrant | |
g.drawScaledSubImage(image, 0, radiusSize, radiusSize, sourceImageEdgeLength, x, y+radiusSize, radiusSize, height-2*radiusSize); | |
// Middle-middle quadrant | |
g.drawScaledSubImage(image, radiusSize, radiusSize, sourceImageEdgeLength, sourceImageEdgeLength, x+radiusSize, y+radiusSize, width-2*radiusSize, height-2*radiusSize); | |
// Middle-right quadrant | |
g.drawScaledSubImage(image, radiusSize+sourceImageEdgeLength, radiusSize, radiusSize, sourceImageEdgeLength, x+width-radiusSize, y+radiusSize, radiusSize, height-2*radiusSize); | |
// Bottom-left quadrant | |
g.drawSubImage(image, x, y + height - radiusSize, 0, image.height-radiusSize, radiusSize, radiusSize); | |
// Bottom-middle quadrant | |
g.drawScaledSubImage(image, radiusSize, radiusSize+sourceImageEdgeLength, sourceImageEdgeLength, radiusSize, x+radiusSize, y+height-radiusSize, width-2*radiusSize, radiusSize); | |
// Bottom-right quadrant | |
g.drawSubImage(image, x + width - radiusSize, y + height - radiusSize, image.width - radiusSize, image.height-radiusSize, radiusSize, radiusSize); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment