Skip to content

Commit 073d916

Browse files
committed
Added borderRadius.
1 parent 2c61382 commit 073d916

File tree

3 files changed

+57
-7
lines changed

3 files changed

+57
-7
lines changed

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ TODO list
1111
* [ ] Targets
1212
* [x] Javascript
1313
* [ ] HashLink
14-
* [ ] Style
14+
* [x] Style
1515
* [x] Border
16-
* [ ] Border radius
16+
* [x] Border radius
1717
* [x] Background color
1818
* [x] Background image
1919
* [x] Filters
@@ -23,5 +23,4 @@ TODO list
2323
* [ ] Texts
2424
* [x] Font file supported
2525
* [ ] Bitmap font supported
26-
* [x] Images
27-
* [ ] Native components?
26+
* [x] Images

haxe/ui/backend/heaps/StyleHelper.hx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,12 @@ class StyleHelper
4848
var borderSize:Int = Std.int(style.borderLeftSize);
4949
var halfBorderSize:Float = borderSize / 2;
5050
s.lineStyle(borderSize, style.borderLeftColor, borderOpacity);
51-
s.drawRect(halfBorderSize, halfBorderSize, w - borderSize, h - borderSize);
51+
if (borderRadius > 0) {
52+
s.drawRoundRect(halfBorderSize, halfBorderSize, w - borderSize, h - borderSize, borderRadius);
53+
} else {
54+
s.drawRect(halfBorderSize, halfBorderSize, w - borderSize, h - borderSize);
55+
}
56+
5257
RECTANGLE_HELPER.left += borderSize;
5358
RECTANGLE_HELPER.top += borderSize;
5459
RECTANGLE_HELPER.width -= borderSize * 2;
@@ -221,7 +226,11 @@ class StyleHelper
221226

222227
s.lineStyle();
223228
s.beginFill(style.backgroundColor, backgroundOpacity);
224-
s.drawRect(RECTANGLE_HELPER.left, RECTANGLE_HELPER.top, RECTANGLE_HELPER.width, RECTANGLE_HELPER.height);
229+
if (borderRadius > 0) {
230+
s.drawRoundRect(RECTANGLE_HELPER.left, RECTANGLE_HELPER.top, RECTANGLE_HELPER.width, RECTANGLE_HELPER.height, borderRadius-1);
231+
} else {
232+
s.drawRect(RECTANGLE_HELPER.left, RECTANGLE_HELPER.top, RECTANGLE_HELPER.width, RECTANGLE_HELPER.height);
233+
}
225234
s.endFill();
226235
}
227236
} else {

haxe/ui/backend/heaps/UISprite.hx

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,50 @@ class UISprite extends Graphics
166166
}
167167
}
168168

169-
public function drawRoundRect(x:Float, y:Float, width:Float, height:Float) {
169+
public function drawRoundRect(x:Float, y:Float, width:Float, height:Float, radius:Float) {
170+
flush();
170171

172+
var angleStart:Float = 0;
173+
var angleLength:Float = Math.PI / 2;
174+
var nsegments:Int = Math.ceil(Math.abs(radius * angleLength / 4));
175+
if( nsegments < 3 ) nsegments = 3;
176+
var angle = angleLength / (nsegments - 1);
177+
178+
if (radius > width/2) {
179+
radius = width/2;
180+
}
181+
if (radius > height/2) {
182+
radius = height/2;
183+
}
184+
185+
radius = Math.floor(radius / 2);
186+
187+
var insetX:Float = x + radius;
188+
var insetY:Float = y + radius;
189+
var insetW:Float = width - 2 * radius;
190+
var insetH:Float = height - 2 * radius;
191+
192+
inline function drawArc(x:Float, y:Float, aStart:Float) {
193+
for( i in 1...nsegments ) {
194+
var a = i * angle + aStart;
195+
lineTo(x + Math.cos(a) * radius, y + Math.sin(a) * radius);
196+
}
197+
}
198+
199+
moveTo(insetX, y);
200+
lineTo(insetX + insetW, y);
201+
drawArc(insetX + insetW, insetY, -Math.PI/2);
202+
203+
lineTo(x + width, insetY + insetH);
204+
drawArc(insetX + insetW, insetY + insetH, 0);
205+
206+
lineTo(insetX, y + height);
207+
drawArc(insetX, insetY + insetH, Math.PI/2);
208+
209+
lineTo(x, insetY);
210+
drawArc(insetX, insetY, -Math.PI);
211+
212+
flush();
171213
}
172214

173215
private function checkInteraction() {

0 commit comments

Comments
 (0)