Add support for Paint which includes gradients and images in GUI
This commit is contained in:
@@ -19,6 +19,10 @@ public interface GUI extends Renderable, Disposable, KeyEventHandler {
|
||||
|
||||
Color createColor();
|
||||
|
||||
Paint createPaint();
|
||||
|
||||
Image getImage(String imageUid);
|
||||
|
||||
void beginPath();
|
||||
|
||||
void closePath();
|
||||
@@ -71,13 +75,25 @@ public interface GUI extends Renderable, Disposable, KeyEventHandler {
|
||||
|
||||
void setFillColor(Color color);
|
||||
|
||||
void setFillPaint(Paint paint);
|
||||
|
||||
void fill();
|
||||
|
||||
void setStrokeColor(Color color);
|
||||
|
||||
void setStrokePaint(Paint paint);
|
||||
|
||||
void setStrokeWidth(float width);
|
||||
|
||||
void stroke();
|
||||
|
||||
void boxGradient(float x, float y, float width, float height, float radius, float feather, Color inner, Color outer, Paint target);
|
||||
|
||||
void linearGradient(float x, float y, float endX, float endY, Color start, Color end, Paint target);
|
||||
|
||||
void radialGradient(float x, float y, float innerRadius, float outerRadius, Color start, Color end, Paint target);
|
||||
|
||||
void imagePattern(float x, float y, float width, float height, float angle, float alpha, Image image, Paint target);
|
||||
|
||||
void clip(float x, float y, float width, float height);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.bartlomiejpluta.base.api.game.gui.base;
|
||||
|
||||
public interface Image {
|
||||
int getWidth();
|
||||
|
||||
int getHeight();
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.bartlomiejpluta.base.api.game.gui.base;
|
||||
|
||||
public interface Paint {
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package com.bartlomiejpluta.base.api.game.gui.component;
|
||||
|
||||
import com.bartlomiejpluta.base.api.game.gui.base.GUI;
|
||||
import com.bartlomiejpluta.base.api.game.gui.base.Image;
|
||||
import com.bartlomiejpluta.base.api.game.gui.base.Paint;
|
||||
import com.bartlomiejpluta.base.api.game.screen.Screen;
|
||||
|
||||
import static java.util.Objects.requireNonNull;
|
||||
|
||||
public class ImageView extends BaseComponent {
|
||||
private final GUI gui;
|
||||
private final Paint paint;
|
||||
|
||||
private Image image;
|
||||
private float angle = 0;
|
||||
private float opacity = 1;
|
||||
private float scaleX = 1;
|
||||
private float scaleY = 1;
|
||||
|
||||
public ImageView(GUI gui, String imageUid) {
|
||||
this.gui = requireNonNull(gui);
|
||||
this.paint = gui.createPaint();
|
||||
this.image = gui.getImage(imageUid);
|
||||
}
|
||||
|
||||
public void setImage(String imageUid) {
|
||||
this.image = gui.getImage(imageUid);
|
||||
}
|
||||
|
||||
public void setAngle(float angle) {
|
||||
this.angle = angle;
|
||||
}
|
||||
|
||||
public void setOpacity(float opacity) {
|
||||
this.opacity = opacity;
|
||||
}
|
||||
|
||||
public void setScaleX(float scaleX) {
|
||||
this.scaleX = scaleX;
|
||||
}
|
||||
|
||||
public void setScaleY(float scaleY) {
|
||||
this.scaleY = scaleY;
|
||||
}
|
||||
|
||||
public void setScale(float scaleX, float scaleY) {
|
||||
this.scaleX = scaleX;
|
||||
this.scaleY = scaleY;
|
||||
}
|
||||
|
||||
public void setScale(float scale) {
|
||||
this.scaleX = scale;
|
||||
this.scaleY = scale;
|
||||
}
|
||||
|
||||
public Image getImage() {
|
||||
return image;
|
||||
}
|
||||
|
||||
public float getAngle() {
|
||||
return angle;
|
||||
}
|
||||
|
||||
public float getOpacity() {
|
||||
return opacity;
|
||||
}
|
||||
|
||||
public float getScaleX() {
|
||||
return scaleX;
|
||||
}
|
||||
|
||||
public float getScaleY() {
|
||||
return scaleY;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected float getContentWidth() {
|
||||
return image.getWidth() * scaleX;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected float getContentHeight() {
|
||||
return image.getHeight() * scaleY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Screen screen, GUI gui) {
|
||||
var posX = x + paddingLeft;
|
||||
var posY = y + paddingTop;
|
||||
var width = getWidth() - paddingLeft - paddingRight;
|
||||
var height = getHeight() - paddingTop - paddingBottom;
|
||||
|
||||
gui.beginPath();
|
||||
gui.drawRectangle(posX, posY, width, height);
|
||||
gui.imagePattern(posX, posY, width, height, angle, opacity, image, paint);
|
||||
gui.setFillPaint(paint);
|
||||
gui.fill();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user