Skip to content

Commit 6e437ba

Browse files
committed
Circle Support for Button
Buttons can now be circular in shape. Improved the shape framework to better encompass circle support.
1 parent 493ddce commit 6e437ba

6 files changed

Lines changed: 46 additions & 9 deletions

File tree

src/studios/vanish/engine/Button.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,26 @@
22

33
import studios.vanish.utility.EventHandler;
44

5+
56
import java.awt.Font;
67
import java.awt.event.MouseEvent;
78
public class Button
89
{
10+
public enum ButtonType
11+
{
12+
Rectangle, Circle
13+
}
914
public Point Location = new Point(0, 0);
1015
public Size Size = new Size(130, 25);
1116
public String Text = "button";
1217
public Font TextFont = new Font("Segoe UI", Font.PLAIN, 11);
1318
public Color[] BackColor = {Color.Black, Color.Red, Color.Green};
1419
public Color[] ForeColor = {Color.White, Color.Green, Color.Red};
1520
public EventHandler OnClick = new EventHandler();
21+
public ButtonType Type = ButtonType.Rectangle;
1622
public boolean Enabled = true;
1723
private int state = 0;
18-
private Rectangle rect;
24+
private Shape bounds;
1925
private boolean down = false;
2026
Window wnd;
2127
public Button(Window wnd, String text)
@@ -48,14 +54,26 @@ public void SetForeColor(Color normal, Color hover, Color press)
4854
}
4955
public void Initialize()
5056
{
51-
rect = new Rectangle(BackColor[state], Location, Size);
57+
int rad = Size.Width;
58+
if (Size.Height > Size.Width)
59+
{
60+
rad = Size.Height;
61+
}
62+
if (Type == ButtonType.Rectangle)
63+
{
64+
bounds = new Rectangle(BackColor[state], Location, Size);
65+
}
66+
else if (Type == ButtonType.Circle)
67+
{
68+
bounds = new Circle(BackColor[state], Location, rad);
69+
}
5270
}
5371
public void Check(Point MouseLocation, int Button)
5472
{
5573
if (Enabled == true)
5674
{
57-
rect = new Rectangle(BackColor[state], Location, Size);
58-
if (rect.Intersects(MouseLocation))
75+
Initialize();
76+
if (bounds.Intersects(MouseLocation))
5977
{
6078
if (wnd.Mouse[MouseEvent.BUTTON1])
6179
{
@@ -82,8 +100,9 @@ public void Check(Point MouseLocation, int Button)
82100
public void Render(GraphicsUnit Graphics)
83101
{
84102
Size center = Graphics.GetTextSize(Text, TextFont);
85-
Point text = new Point((Location.X + (Size.Width) / 2) - (center.Width / 2), (Location.Y + (Size.Height) / 2) - (center.Height / 2));
86-
Graphics.FillRectangle(BackColor[state], Location, Size);
103+
Initialize();
104+
Point text = new Point(bounds.GetShapeCenter().X - (center.Width / 2), bounds.GetShapeCenter().Y - (center.Height / 2));
105+
bounds.Render(Graphics);
87106
Graphics.DrawString(Text, ForeColor[state], text, TextFont);
88107
}
89108
}

src/studios/vanish/engine/Circle.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ else if ((Center.Y > shape.Location.Y) && (Center.Y < shape.Location.Y + shape.S
7373
}
7474
return false;
7575
}
76+
public boolean Intersects(Point point)
77+
{
78+
if (Center.distance(point) < Radius)
79+
{
80+
return true;
81+
}
82+
return false;
83+
}
84+
public Point GetShapeCenter()
85+
{
86+
return Center;
87+
}
7688
public void Render(GraphicsUnit Graphics)
7789
{
7890
Graphics.FillEllipse(Color, new Point(Center.X - Radius, Center.Y - Radius), new Size(Radius * 2, Radius * 2));

src/studios/vanish/engine/Rectangle.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ public boolean Intersects(Point point)
2121
}
2222
return false;
2323
}
24+
public Point GetShapeCenter()
25+
{
26+
return Location.add(Size.divide(new Size(2, 2)).toPoint());
27+
}
2428
public void Render(GraphicsUnit Graphics)
2529
{
2630
Graphics.FillRectangle(Color, Location, Size);

src/studios/vanish/engine/Shape.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,7 @@ public void Initialize(Color Color, Point Center, int Radius)
2424
this.Location = new Point(this.Center.X - (side / 2), this.Center.Y - (side / 2));
2525
}
2626
public abstract boolean Intersects(Shape shape);
27+
public abstract boolean Intersects(Point point);
2728
public abstract void Render(GraphicsUnit Graphics);
29+
public abstract Point GetShapeCenter();
2830
}

src/studios/vanish/utility/Method.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ public Method(Object _object, String _name)
1010
}
1111
public Object Invoke(Object... obj)
1212
{
13-
return Spark.InvokeMethod(Handle, Name, obj);
13+
return Utility.InvokeMethod(Handle, Name, obj);
1414
}
1515
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package studios.vanish.utility;
2-
public class Spark
2+
public class Utility
33
{
44
public static Object InvokeMethod(Object _object, String _method, Object... _parameters)
55
{
66
Object _return = false;
77
try
88
{
9-
_return = _object.getClass().getMethod(_method, Spark.GetClassType(_parameters)).invoke(_object, _parameters);
9+
_return = _object.getClass().getMethod(_method, Utility.GetClassType(_parameters)).invoke(_object, _parameters);
1010
if (_return == null)
1111
{
1212
_return = "success: return type null";

0 commit comments

Comments
 (0)