Skip to content

Commit

Permalink
Improved support for round rect border in CSS. Previously CSS would u…
Browse files Browse the repository at this point in the history
…se 9-piece borders for if the it couldn't reproduce it with a round rect border in normal, top-only, or bottom-only mode. E.g. if only the top left and bottom right corners have a radius defined, then it would fall back to a 9-piece border. This change allows roundrectborder to be used for any combination of corners being rounded. It still doesn't allow you to have different non-zero radiuses in different corners. All of the corners need to either have the same radius, or zero. If one corner has, for examle, a radius of 3mm and another has 2mm, then it will fall back to a 9-piece border still. #2350
  • Loading branch information
shannah committed Feb 11, 2019
1 parent ab7e226 commit 52172ba
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 41 deletions.
19 changes: 19 additions & 0 deletions CodenameOne/src/com/codename1/ui/util/Resources.java
Expand Up @@ -1622,6 +1622,25 @@ private Object createBorder(DataInputStream input, int type) throws IOException
bezierCorners(input.readBoolean()). bezierCorners(input.readBoolean()).
topOnlyMode(input.readBoolean()). topOnlyMode(input.readBoolean()).
bottomOnlyMode(input.readBoolean()); bottomOnlyMode(input.readBoolean());
// round rect border with top-left, top-right, bottom-right, bottom-left corners
// specified independently
case 0xff15:
return RoundRectBorder.create().
stroke(input.readFloat(), input.readBoolean()).
strokeColor(input.readInt()).
strokeOpacity(input.readInt()).
shadowBlur(input.readFloat()).
shadowOpacity(input.readInt()).
shadowSpread(input.readFloat()).
shadowX(input.readFloat()).
shadowY(input.readFloat()).
cornerRadius(input.readFloat()).
bezierCorners(input.readBoolean()).
topLeftMode(input.readBoolean()).
topRightMode(input.readBoolean()).
bottomRightMode(input.readBoolean()).
bottomLeftMode(input.readBoolean());

} }
return null; return null;
} }
Expand Down
49 changes: 12 additions & 37 deletions CodenameOneDesigner/src/com/codename1/designer/css/CSSTheme.java
Expand Up @@ -1400,6 +1400,9 @@ public Map<String, CacheStatus> calculateSelectorCacheStatus(File cachedFile) th
} }


public void updateResources() { public void updateResources() {
// TODO: We need to remove stale theme entries
// https://github.com/codenameone/CodenameOne/issues/2698

for (String id : elements.keySet()) { for (String id : elements.keySet()) {
if (!isModified(id)) { if (!isModified(id)) {
continue; continue;
Expand All @@ -1415,7 +1418,6 @@ public void updateResources() {
String unselId = id; String unselId = id;
String pressedId = id+".press"; String pressedId = id+".press";
String disabledId = id+".dis"; String disabledId = id+".dis";

res.setThemeProperty(themeName, unselId+".padding", el.getThemePadding(unselectedStyles)); res.setThemeProperty(themeName, unselId+".padding", el.getThemePadding(unselectedStyles));
res.setThemeProperty(themeName, unselId+".padUnit", el.getThemePaddingUnit(unselectedStyles)); res.setThemeProperty(themeName, unselId+".padUnit", el.getThemePaddingUnit(unselectedStyles));
res.setThemeProperty(themeName, selId+"#padding", el.getThemePadding(selectedStyles)); res.setThemeProperty(themeName, selId+"#padding", el.getThemePadding(selectedStyles));
Expand Down Expand Up @@ -2386,18 +2388,10 @@ public boolean canBeAchievedWithUnderlineBorder(Map<String,LexicalUnit> styles)
} }


public boolean canBeAchievedWithRoundRectBorder(Map<String,LexicalUnit> styles) { public boolean canBeAchievedWithRoundRectBorder(Map<String,LexicalUnit> styles) {
//System.out.println("Checking if we can achieve with background image generation "+styles);
if (hasUnequalBorders() || this.hasGradient() || !isBorderLineOrNone() || !isNone(backgroundImageUrl) || hasBoxShadow()) { if (hasUnequalBorders() || this.hasGradient() || !isBorderLineOrNone() || !isNone(backgroundImageUrl) || hasBoxShadow()) {
//System.out.println("Failed test 1");
//System.out.println("unequalBorders? "+hasUnequalBorders());
//System.out.println("Has gradient? "+hasGradient());
//System.out.println("BorderLineOrNone? "+isBorderLineOrNone());
//System.out.println("Background Image URL: "+backgroundImageUrl);
return false; return false;
} }




String prefix = "cn1-border"; String prefix = "cn1-border";
String[] corners = new String[]{"top-left", "top-right", "bottom-left", "bottom-right"}; String[] corners = new String[]{"top-left", "top-right", "bottom-left", "bottom-right"};
String[] xy = new String[]{"x", "y"}; String[] xy = new String[]{"x", "y"};
Expand All @@ -2411,25 +2405,9 @@ public boolean canBeAchievedWithRoundRectBorder(Map<String,LexicalUnit> styles)
} }


ScaledUnit val = null; ScaledUnit val = null;
boolean topLeft=false;
boolean topRight = false;
boolean bottomLeft = false;
boolean bottomRight = false;
for (String cornerStyle : radiusAtts) { for (String cornerStyle : radiusAtts) {
ScaledUnit u = (ScaledUnit)styles.get(cornerStyle); ScaledUnit u = (ScaledUnit)styles.get(cornerStyle);
if (u != null && u.getPixelValue() != 0) { if (u != null && u.getPixelValue() != 0) {
if (cornerStyle.indexOf("top-left") != -1) {
topLeft = true;
}
if (cornerStyle.indexOf("top-right") != -1) {
topRight = true;
}
if (cornerStyle.indexOf("bottom-left") != -1) {
bottomLeft = true;
}
if (cornerStyle.indexOf("bottom-right") != -1) {
bottomRight = true;
}
if (val != null && val.getPixelValue() != u.getPixelValue()) { if (val != null && val.getPixelValue() != u.getPixelValue()) {
// We have more than one non-zero corner radius // We have more than one non-zero corner radius
//System.out.println("Failed corner test"); //System.out.println("Failed corner test");
Expand All @@ -2440,13 +2418,6 @@ public boolean canBeAchievedWithRoundRectBorder(Map<String,LexicalUnit> styles)
} }
} }



if (topLeft != topRight || bottomLeft != bottomRight) {
// Resource files don't currently support topLeftMode, topRightMode, bottomLeftMode, and bottomRightMode
// so we need to fall back to image borders if the left and right have different radii
return false;
}

// All corners are the same, so we can proceed to the next step. // All corners are the same, so we can proceed to the next step.


prefix = "border"; prefix = "border";
Expand Down Expand Up @@ -4484,11 +4455,15 @@ private com.codename1.ui.plaf.Border createRoundRectBorder(Map<String,LexicalUni
ScaledUnit bottomRightRadius = getBorderRadius(styles, "bottom-right"); ScaledUnit bottomRightRadius = getBorderRadius(styles, "bottom-right");
//System.out.println("TopLeftRadius is : "+topLeftRadius+" isZero? "+isZero(topLeftRadius)); //System.out.println("TopLeftRadius is : "+topLeftRadius+" isZero? "+isZero(topLeftRadius));
//System.out.println("BottomRight Radius is : "+bottomRightRadius+" isZero? "+isZero(bottomRightRadius)); //System.out.println("BottomRight Radius is : "+bottomRightRadius+" isZero? "+isZero(bottomRightRadius));
if (!isZero(bottomLeftRadius) && !isZero(bottomRightRadius) && isZero(topLeftRadius) && isZero(topRightRadius)) { //if (!isZero(bottomLeftRadius) && !isZero(bottomRightRadius) && isZero(topLeftRadius) && isZero(topRightRadius)) {
out.bottomOnlyMode(true); // out.bottomOnlyMode(true);
} else if (isZero(bottomLeftRadius) && isZero(bottomRightRadius) && !isZero(topLeftRadius) && !isZero(topRightRadius)) { //} else if (isZero(bottomLeftRadius) && isZero(bottomRightRadius) && !isZero(topLeftRadius) && !isZero(topRightRadius)) {
out.topOnlyMode(true); // out.topOnlyMode(true);
} //}
out.topLeftMode(!isZero(topLeftRadius));
out.topRightMode(!isZero(topRightRadius));
out.bottomRightMode(!isZero(bottomRightRadius));
out.bottomLeftMode(!isZero(bottomLeftRadius));






Expand Down
Expand Up @@ -110,7 +110,7 @@
* @author Shai Almog * @author Shai Almog
*/ */
public class EditableResources extends Resources implements TreeModel { public class EditableResources extends Resources implements TreeModel {
private static final short MINOR_VERSION = 9; private static final short MINOR_VERSION = 10;
private static final short MAJOR_VERSION = 1; private static final short MAJOR_VERSION = 1;


private boolean modified; private boolean modified;
Expand Down Expand Up @@ -2082,8 +2082,10 @@ private void writeBorder(DataOutputStream output, Border border, boolean newVers
return; return;
} }
if(border instanceof RoundRectBorder) { if(border instanceof RoundRectBorder) {
output.writeShort(0xff13);

RoundRectBorder rb = (RoundRectBorder)border; RoundRectBorder rb = (RoundRectBorder)border;
output.writeShort(0xff15);
output.writeFloat(rb.getStrokeThickness()); output.writeFloat(rb.getStrokeThickness());
output.writeBoolean(rb.isStrokeMM()); output.writeBoolean(rb.isStrokeMM());
output.writeInt(rb.getStrokeColor()); output.writeInt(rb.getStrokeColor());
Expand All @@ -2095,8 +2097,10 @@ private void writeBorder(DataOutputStream output, Border border, boolean newVers
output.writeFloat(rb.getShadowY()); output.writeFloat(rb.getShadowY());
output.writeFloat(rb.getCornerRadius()); output.writeFloat(rb.getCornerRadius());
output.writeBoolean(rb.isBezierCorners()); output.writeBoolean(rb.isBezierCorners());
output.writeBoolean(rb.isTopOnlyMode()); output.writeBoolean(rb.isTopLeft());
output.writeBoolean(rb.isBottomOnlyMode()); output.writeBoolean(rb.isTopRight());
output.writeBoolean(rb.isBottomRight());
output.writeBoolean(rb.isBottomLeft());
return; return;
} }
int type = Accessor.getType(border); int type = Accessor.getType(border);
Expand Down

0 comments on commit 52172ba

Please sign in to comment.