-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCircleDrawable.java
143 lines (139 loc) · 5.33 KB
/
CircleDrawable.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package ultra.deep.in.science.is.simple;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
/**
* @author Richi on 9/22/2020 AD.
*/
public class CircleDrawable extends TextureRegionDrawable {
//==============================================================
// constructors
//==============================================================
public CircleDrawable() {
}
public CircleDrawable(Pixmap pixmap) {
setPixmap(pixmap);
}
public void setPixmap(Pixmap pixmap) {
makeCircle(pixmap);
super.setRegion(new TextureRegion(new Texture(pixmap)));
}
@Override public void setRegion(TextureRegion region) {
if (!region.getTexture().getTextureData().isPrepared())
{
region.getTexture().getTextureData().prepare();
}
Pixmap origPixmap = region.getTexture().getTextureData().consumePixmap();
makeCircle(origPixmap);
super.setRegion(region);
}
//==============================================================
// Overrides
//==============================================================
//==============================================================
// Privates
//==============================================================
private void makeCircle(Pixmap pixmap) {
int width = pixmap.getWidth();
int height = pixmap.getHeight();
int min = Math.min(width, height);
int max = Math.max(width, height);
boolean is_width_smaller_than_height = width == min;
double radius = min / 2.0;
int y_offsetStart = 0;
int y_offsetEnd = 0;
int x_offsetStart = 0;
int x_offsetEnd = 0;
if (is_width_smaller_than_height)
{
y_offsetStart = (int) ((max - min) / 2f);
y_offsetEnd = (int) (y_offsetStart + min);
x_offsetEnd = (int) min;
}
else
{
x_offsetStart = (int) ((max - min) / 2f);
x_offsetEnd = (int) (x_offsetStart + min);
y_offsetEnd = (int) min;
}
pixmap.setBlending(Pixmap.Blending.None);
pixmap.setColor(Color.CLEAR);
for (int y = 0; y < pixmap.getHeight(); y++)
{
for (int x = 0; x < pixmap.getWidth(); x++)
{
if (y > y_offsetStart && y < y_offsetEnd)
{
if( x >= x_offsetStart && x < x_offsetEnd)
{
double dist_x = (radius - x + x_offsetStart);
double dist_y = radius - y + y_offsetStart;
double dist = Math.sqrt((dist_x * dist_x) + (dist_y * dist_y));
if (dist < radius)
{
continue;
}
}
}
pixmap.drawPixel(x, y);
}
}
pixmap.setBlending(Pixmap.Blending.SourceOver); // if you want to go back to blending
}
//==============================================================
// Nice code for circle PIXMAP
//==============================================================
public static Pixmap createCirclePixmap(Pixmap pixmap) {
int width = pixmap.getWidth();
int height = pixmap.getHeight();
int min = Math.min(width, height);
int max = Math.max(width, height);
boolean is_width_smaller_than_height = width == min;
Pixmap round = new Pixmap(min, min, Pixmap.Format.RGBA8888);
double radius = (min / 2.0f);
int y_offsetStart = 0;
int y_offsetEnd = 0;
int x_offsetStart = 0;
int x_offsetEnd = 0;
if (is_width_smaller_than_height)
{
y_offsetStart = (int) ((max - min) / 2f);
y_offsetEnd = (int) (y_offsetStart + min);
x_offsetEnd = (int) min;
}
else
{
x_offsetStart = (int) ((max - min) / 2f);
x_offsetEnd = (int) (x_offsetStart + min);
y_offsetEnd = (int) min;
}
final double ANTI_ALIAS_WIDTH = 3f;
double antiAliasBandry = radius - ANTI_ALIAS_WIDTH;
for (int y = y_offsetStart; y < y_offsetEnd; y++)
{
for (int x = x_offsetStart; x < x_offsetEnd; x++)
{
//check if pixel is outside circle. Set pixel to transparant;
double dist_x = (radius - x + x_offsetStart);
double dist_y = radius - y + y_offsetStart;
double dist = Math.sqrt((dist_x * dist_x) + (dist_y * dist_y));
if (radius >= dist)
{
Color color = new Color(pixmap.getPixel(x, y));
if(dist > antiAliasBandry )
color.a = (float) ((radius-dist)/ANTI_ALIAS_WIDTH);
round.drawPixel(x - x_offsetStart, y - y_offsetStart, Color.rgba8888(color));
}
else
{
round.drawPixel(x - x_offsetStart, y - y_offsetStart, 0);
}
}
}
Gdx.app.log("info", "pixmal rounded!");
return round;
}
}