implemented 9slice

This commit is contained in:
DaniTheSkunk 2022-10-06 06:22:30 +00:00
parent 009f80dd3a
commit 903e5246dc
4 changed files with 74 additions and 9 deletions

View File

@ -33,10 +33,11 @@ public class Test {
while(!window.shouldClose()) {
window.tick();
var renderContext = window.renderStart();
//renderContext.drawTextureRectangle(
// new Recti(0, 0, 1280, 720),
// tex
//);
renderContext.drawTextureRectangle(
new Recti(0, 0, 1280, 720),
tex,
false
);
//renderContext.drawTextureRectangle(
// new Recti(new Vec2i(200, 100), tex2.getSize()),
// tex2,
@ -53,7 +54,7 @@ public class Test {
renderContext.drawString(new Vec2i(710, 140), "hello world mew", font2);
//renderContext.drawNineSlice(slice, new Recti(100, 100, 1080, 520));
renderContext.drawNineSlice(slice, new Recti(100, 100, 200, 50));
window.renderFinish(renderContext);
}

View File

@ -20,12 +20,62 @@ class RenderContext extends BaseRenderContext implements IRenderContext {
@Override
public void drawNineSlice(NineSlice slice, Recti rect) {
var centerWidth = Math.min(0, rect.getWidth() - slice.getLeft().getSize().getX() - slice.getRight().getSize().getX());
var centerHeight = Math.min(0, rect.getHeight() - slice.getTop().getSize().getY() - slice.getBottom().getSize().getY());
var centerWidth = Math.max(0, rect.getWidth() - slice.getLeft().getSize().getX() - slice.getRight().getSize().getX());
var centerHeight = Math.max(0, rect.getHeight() - slice.getTop().getSize().getY() - slice.getBottom().getSize().getY());
var pos = rect.getPos();
var size = rect.getSize();
drawTexture(pos, slice.getTopLeft());
drawTexture(new Vec2i(pos.getX() + slice.getLeft().getWidth() + centerWidth, pos.getY()), slice.getTopRight());
drawTexture(new Vec2i(pos.getX(), pos.getY() + slice.getTop().getHeight() + centerHeight), slice.getBottomLeft());
drawTexture(Vec2i.add(pos, slice.getTopLeft().getSize(), new Vec2i(centerWidth, centerHeight)), slice.getBottomRight());
drawTextureRectangle(new Recti(Vec2i.add(pos, slice.getTopLeft().getSize()), new Vec2i(centerWidth, centerHeight)), slice.getCenter(), true);
//top
drawTextureRectangle(
new Recti(
pos.getX() + slice.getLeft().getWidth(),
pos.getY(),
centerWidth,
slice.getTop().getHeight()
),
slice.getTop(),
true
);
//bottom
drawTextureRectangle(
new Recti(
pos.getX() + slice.getLeft().getWidth(),
pos.getY() + slice.getTop().getHeight() + centerHeight,
centerWidth,
slice.getTop().getHeight()
),
slice.getBottom(),
true
);
//left
drawTextureRectangle(
new Recti(
pos.getX(),
pos.getY() + slice.getTop().getHeight(),
slice.getLeft().getWidth(),
centerHeight
),
slice.getLeft(),
true
);
//right
drawTextureRectangle(
new Recti(
pos.getX() + slice.getLeft().getWidth() + centerWidth,
pos.getY() + slice.getTop().getHeight(),
slice.getLeft().getWidth(),
centerHeight
),
slice.getRight(),
true
);
}
@Override
@ -71,7 +121,7 @@ class RenderContext extends BaseRenderContext implements IRenderContext {
@Override
public void drawTextureRectangle(Recti rect, ITexture texture, Color color, boolean repeat) {
var tex = (Texture)texture;
var tex = (Texture) texture;
var tl = rect.getTopLeft();
var tr = rect.getTopRight();
@ -86,7 +136,8 @@ class RenderContext extends BaseRenderContext implements IRenderContext {
*/
var topleft = tex.getTexArea().getTopLeft();
var size = repeat ? tex.getSize() : rect.getSize();
//var size = repeat ? tex.getSize() : rect.getSize();
var size = repeat ? tex.getSize() : new Vec2i(100000, 100000);
/*var ttl = Vec2i.ZERO;
var ttr = Vec2i.sub(tex.getTexArea().getTopRight(), topleft);
var tbl = Vec2i.sub(tex.getTexArea().getBottomLeft(), topleft);

View File

@ -22,6 +22,17 @@ class Texture implements ITexture {
}
@Override
public Vec2i getSize() { return this.img.getSize(); }
@Override
public int getWidth() {
return img.getWidth();
}
@Override
public int getHeight() {
return img.getHeight();
}
void setTexArea(Recti texArea) {
this.texArea = texArea;
}

View File

@ -4,4 +4,6 @@ import com.danitheskunk.skunkworks.Vec2i;
public interface ITexture {
Vec2i getSize();
int getWidth();
int getHeight();
}