use of com.codename1.ui.plaf.Style in project CodenameOne by codenameone.
the class Component method createStyleAnimation.
ComponentAnimation createStyleAnimation(final Style sourceStyle, final Style destStyle, final int duration, final String destUIID) {
int d = duration;
Motion m = null;
if (sourceStyle.getFgColor() != destStyle.getFgColor()) {
m = Motion.createLinearColorMotion(sourceStyle.getFgColor(), destStyle.getFgColor(), d);
}
final Motion fgColorMotion = m;
m = null;
if (sourceStyle.getOpacity() != destStyle.getOpacity()) {
m = Motion.createLinearColorMotion(sourceStyle.getOpacity(), destStyle.getOpacity(), d);
}
final Motion opacityMotion = m;
m = null;
if (sourceStyle.getFont().getHeight() != destStyle.getFont().getHeight() && sourceStyle.getFont().isTTFNativeFont()) {
// allows for fractional font sizes
m = Motion.createLinearMotion(sourceStyle.getFont().getHeight() * 100, destStyle.getFont().getHeight() * 100, d);
}
final Motion fontMotion = m;
m = null;
if (sourceStyle.getPaddingTop() != destStyle.getPaddingTop()) {
m = Motion.createLinearMotion(sourceStyle.getPaddingTop(), destStyle.getPaddingTop(), d);
}
final Motion paddingTop = m;
m = null;
if (sourceStyle.getPaddingBottom() != destStyle.getPaddingBottom()) {
m = Motion.createLinearMotion(sourceStyle.getPaddingBottom(), destStyle.getPaddingBottom(), d);
}
final Motion paddingBottom = m;
m = null;
if (sourceStyle.getPaddingLeftNoRTL() != destStyle.getPaddingLeftNoRTL()) {
m = Motion.createLinearMotion(sourceStyle.getPaddingLeftNoRTL(), destStyle.getPaddingLeftNoRTL(), d);
}
final Motion paddingLeft = m;
m = null;
if (sourceStyle.getPaddingRightNoRTL() != destStyle.getPaddingRightNoRTL()) {
m = Motion.createLinearMotion(sourceStyle.getPaddingRightNoRTL(), destStyle.getPaddingRightNoRTL(), d);
}
final Motion paddingRight = m;
m = null;
if (sourceStyle.getMarginTop() != destStyle.getMarginTop()) {
m = Motion.createLinearMotion(sourceStyle.getMarginTop(), destStyle.getMarginTop(), d);
}
final Motion marginTop = m;
m = null;
if (sourceStyle.getMarginBottom() != destStyle.getMarginBottom()) {
m = Motion.createLinearMotion(sourceStyle.getMarginBottom(), destStyle.getMarginBottom(), d);
}
final Motion marginBottom = m;
m = null;
if (sourceStyle.getMarginLeftNoRTL() != destStyle.getMarginLeftNoRTL()) {
m = Motion.createLinearMotion(sourceStyle.getMarginLeftNoRTL(), destStyle.getMarginLeftNoRTL(), d);
}
final Motion marginLeft = m;
m = null;
if (sourceStyle.getMarginRightNoRTL() != destStyle.getMarginRightNoRTL()) {
m = Motion.createLinearMotion(sourceStyle.getMarginRightNoRTL(), destStyle.getMarginRightNoRTL(), d);
}
final Motion marginRight = m;
m = null;
if (paddingLeft != null || paddingRight != null || paddingTop != null || paddingBottom != null) {
// convert the padding to pixels for smooth animation
int left = sourceStyle.getPaddingLeftNoRTL();
int right = sourceStyle.getPaddingRightNoRTL();
int top = sourceStyle.getPaddingTop();
int bottom = sourceStyle.getPaddingBottom();
sourceStyle.setPaddingUnit(Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS);
sourceStyle.setPadding(top, bottom, left, right);
}
if (marginLeft != null || marginRight != null || marginTop != null || marginBottom != null) {
// convert the margin to pixels for smooth animation
int left = sourceStyle.getMarginLeftNoRTL();
int right = sourceStyle.getMarginRightNoRTL();
int top = sourceStyle.getMarginTop();
int bottom = sourceStyle.getMarginBottom();
sourceStyle.setMarginUnit(Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS);
sourceStyle.setMargin(top, bottom, left, right);
}
final AnimationTransitionPainter ap = new AnimationTransitionPainter();
if (sourceStyle.getBgTransparency() != 0 || destStyle.getBgTransparency() != 0 || (sourceStyle.getBorder() != null && sourceStyle.getBorder().isEmptyBorder()) || (destStyle.getBorder() != null && destStyle.getBorder().isEmptyBorder()) || sourceStyle.getBgImage() != null || destStyle.getBgImage() != null) {
ap.original = sourceStyle.getBgPainter();
ap.dest = destStyle.getBgPainter();
ap.originalStyle = sourceStyle;
ap.destStyle = destStyle;
if (ap.dest == null) {
ap.dest = new BGPainter();
}
sourceStyle.setBgPainter(ap);
}
final Motion bgMotion = Motion.createLinearMotion(0, 255, d);
return new ComponentAnimation() {
private boolean finished;
private boolean stepMode;
private boolean started;
@Override
public boolean isStepModeSupported() {
return true;
}
@Override
public int getMaxSteps() {
return duration;
}
@Override
public void setStep(int step) {
stepMode = true;
if (!finished) {
if (bgMotion != null) {
bgMotion.setCurrentMotionTime(step);
}
if (fgColorMotion != null) {
fgColorMotion.setCurrentMotionTime(step);
}
if (opacityMotion != null) {
opacityMotion.setCurrentMotionTime(step);
}
if (fontMotion != null) {
fontMotion.setCurrentMotionTime(step);
}
if (paddingTop != null) {
paddingTop.setCurrentMotionTime(step);
}
if (paddingBottom != null) {
paddingBottom.setCurrentMotionTime(step);
}
if (paddingLeft != null) {
paddingLeft.setCurrentMotionTime(step);
}
if (paddingRight != null) {
paddingRight.setCurrentMotionTime(step);
}
if (marginTop != null) {
marginTop.setCurrentMotionTime(step);
}
if (marginBottom != null) {
marginBottom.setCurrentMotionTime(step);
}
if (marginLeft != null) {
marginLeft.setCurrentMotionTime(step);
}
if (marginRight != null) {
marginRight.setCurrentMotionTime(step);
}
}
super.setStep(step);
}
@Override
public boolean isInProgress() {
if (!stepMode && !started) {
return true;
}
return stepMode || !((bgMotion == null || bgMotion.isFinished()) && (opacityMotion == null || opacityMotion.isFinished()) && (fgColorMotion == null || fgColorMotion.isFinished()) && (paddingLeft == null || paddingLeft.isFinished()) && (paddingRight == null || paddingRight.isFinished()) && (paddingTop == null || paddingTop.isFinished()) && (paddingBottom == null || paddingBottom.isFinished()) && (marginLeft == null || marginLeft.isFinished()) && (marginRight == null || marginRight.isFinished()) && (marginTop == null || marginTop.isFinished()) && (marginBottom == null || marginBottom.isFinished()) && (fontMotion == null || fontMotion.isFinished()));
}
@Override
protected void updateState() {
if (finished) {
return;
}
if (!started && !stepMode) {
started = true;
if (bgMotion != null) {
bgMotion.start();
}
if (opacityMotion != null) {
opacityMotion.start();
}
if (fgColorMotion != null) {
fgColorMotion.start();
}
if (fontMotion != null) {
fontMotion.start();
}
if (paddingTop != null) {
paddingTop.start();
}
if (paddingBottom != null) {
paddingBottom.start();
}
if (paddingLeft != null) {
paddingLeft.start();
}
if (paddingRight != null) {
paddingRight.start();
}
if (marginTop != null) {
marginTop.start();
}
if (marginBottom != null) {
marginBottom.start();
}
if (marginLeft != null) {
marginLeft.start();
}
if (marginRight != null) {
marginRight.start();
}
}
if (!isInProgress()) {
finished = true;
if (destUIID != null) {
setUIID(destUIID);
}
} else {
if (opacityMotion != null) {
sourceStyle.setOpacity(opacityMotion.getValue());
}
if (fgColorMotion != null) {
sourceStyle.setFgColor(fgColorMotion.getValue());
}
if (bgMotion != null) {
ap.alpha = bgMotion.getValue();
}
if (fontMotion != null) {
Font fnt = sourceStyle.getFont();
fnt = fnt.derive(((float) fontMotion.getValue()) / 100.0f, fnt.getStyle());
sourceStyle.setFont(fnt);
}
if (paddingTop != null) {
sourceStyle.setPadding(TOP, paddingTop.getValue());
}
if (paddingBottom != null) {
sourceStyle.setPadding(BOTTOM, paddingBottom.getValue());
}
if (paddingLeft != null) {
sourceStyle.setPadding(LEFT, paddingLeft.getValue());
}
if (paddingRight != null) {
sourceStyle.setPadding(RIGHT, paddingRight.getValue());
}
if (marginTop != null) {
sourceStyle.setMargin(TOP, marginTop.getValue());
}
if (marginBottom != null) {
sourceStyle.setMargin(BOTTOM, marginBottom.getValue());
}
if (marginLeft != null) {
sourceStyle.setMargin(LEFT, marginLeft.getValue());
}
if (marginRight != null) {
sourceStyle.setMargin(RIGHT, marginRight.getValue());
}
}
}
@Override
public void flush() {
if (bgMotion != null) {
bgMotion.finish();
}
if (opacityMotion != null) {
opacityMotion.finish();
}
if (fgColorMotion != null) {
fgColorMotion.finish();
}
if (fontMotion != null) {
fontMotion.finish();
}
if (paddingTop != null) {
paddingTop.finish();
}
if (paddingBottom != null) {
paddingBottom.finish();
}
if (paddingLeft != null) {
paddingLeft.finish();
}
if (paddingRight != null) {
paddingRight.finish();
}
if (marginTop != null) {
marginTop.finish();
}
if (marginBottom != null) {
marginBottom.finish();
}
if (marginLeft != null) {
marginLeft.finish();
}
if (marginRight != null) {
marginRight.finish();
}
updateState();
}
};
}
use of com.codename1.ui.plaf.Style in project CodenameOne by codenameone.
the class ComponentSelector method setIcon.
/**
* Sets the icon of all elements in this set to a material icon. This will use
* the foreground color of each label as the icon's foreground color.
* @param materialIcon The icon charcode.
* @param size The size of the icon (in mm)
* @return Self for chaining
* @see FontImage#createMaterial(char, com.codename1.ui.plaf.Style, float)
*/
public ComponentSelector setIcon(char materialIcon, float size) {
for (Component c : this) {
if (c instanceof Label) {
Label l = (Label) c;
Style style = new Style();
Style cStyle = c.getUnselectedStyle();
style.setBgTransparency(0);
style.setFgColor(cStyle.getFgColor());
l.setIcon(FontImage.createMaterial(materialIcon, style, size));
if (c instanceof Button) {
Button b = (Button) c;
style = new Style();
cStyle = c.getPressedStyle();
style.setBgTransparency(0);
style.setFgColor(cStyle.getFgColor());
b.setPressedIcon(FontImage.createMaterial(materialIcon, style, size));
}
}
}
return this;
}
use of com.codename1.ui.plaf.Style in project CodenameOne by codenameone.
the class ComponentSelector method setPadding.
/**
* Sets padding to all components in found set.
* @param top Top padding in pixels.
* @param right Right padding in pixels
* @param bottom Bottom padding in pixels.
* @param left Left padding in pixels.
* @return
* @see #getStyle(com.codename1.ui.Component)
*/
public ComponentSelector setPadding(int top, int right, int bottom, int left) {
Style s = currentStyle();
s.setPaddingUnit(Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS, Style.UNIT_TYPE_PIXELS);
s.setPadding(top, bottom, left, right);
return this;
}
use of com.codename1.ui.plaf.Style in project CodenameOne by codenameone.
the class Container method isObscuredByChildren.
/**
* Invoked internally to indicate if child components are hiding this container
* thus removing the need to invoke its own paint methods
* @return true if child components are obscuring this component
*/
boolean isObscuredByChildren() {
if (!blockOverdraw) {
return false;
}
if (!getLayout().obscuresPotential(this)) {
return false;
}
Style s = getStyle();
if (s.getPaddingTop() != 0 || s.getPaddingLeftNoRTL() != 0 || s.getPaddingRightNoRTL() != 0 || s.getPaddingBottom() != 0) {
return false;
}
int size = components.size();
for (int iter = 0; iter < size; iter++) {
Component cmp = components.get(iter);
s = cmp.getStyle();
if (cmp.getWidth() == 0 || cmp.getHeight() == 0) {
continue;
}
// need to think of a better way, this means we invoke the same logic recurisvely again and again by a factor of depth. Not good...
if (cmp instanceof Container) {
if (!((Container) cmp).getLayout().obscuresPotential(this)) {
return false;
}
if (s.getOpacity() != 0xff || s.getMarginTop() != 0 || s.getMarginLeftNoRTL() != 0 || s.getMarginRightNoRTL() != 0 || s.getMarginBottom() != 0) {
return false;
}
if ((s.getBgTransparency() & 0xff) != 0xff && !((Container) cmp).isObscuredByChildren()) {
return false;
}
} else {
if ((s.getBgTransparency() & 0xff) != 0xff || s.getOpacity() != 0xff || s.getMarginTop() != 0 || s.getMarginLeftNoRTL() != 0 || s.getMarginRightNoRTL() != 0 || s.getMarginBottom() != 0) {
return false;
}
}
}
return true;
}
use of com.codename1.ui.plaf.Style in project CodenameOne by codenameone.
the class Dialog method showPopupDialog.
/**
* A popup dialog is shown with the context of a component and its selection, it is disposed seamlessly if the back button is pressed
* or if the user touches outside its bounds. It can optionally provide an arrow in the theme to point at the context component. The popup
* dialog has the PopupDialog style by default.
*
* @param rect the screen rectangle to which the popup should point
* @return the command that might have been triggered by the user within the dialog if commands are placed in the dialog
*/
public Command showPopupDialog(Rectangle rect) {
if (getDialogUIID().equals("Dialog")) {
setDialogUIID("PopupDialog");
if (getTitleComponent().getUIID().equals("DialogTitle")) {
getTitleComponent().setUIID("PopupDialogTitle");
}
getContentPane().setUIID("PopupContentPane");
}
disposeOnRotation = true;
disposeWhenPointerOutOfBounds = true;
Command backCommand = null;
if (getBackCommand() == null) {
backCommand = new Command("Back");
setBackCommand(backCommand);
}
Component contentPane = super.getContentPane();
Label title = super.getTitleComponent();
int menuHeight = calcMenuHeight();
UIManager manager = getUIManager();
// preferred size logic of the dialog won't work with large title borders
if (dialogTitle != null && manager.isThemeConstant("hideEmptyTitleBool", false)) {
boolean b = getTitle().length() > 0;
getTitleArea().setVisible(b);
getTitleComponent().setVisible(b);
if (!b && manager.isThemeConstant("shrinkPopupTitleBool", true)) {
getTitleComponent().setPreferredSize(new Dimension(0, 0));
getTitleComponent().getStyle().setBorder(null);
getTitleArea().setPreferredSize(new Dimension(0, 0));
if (getContentPane().getClientProperty("$ENLARGED_POP") == null) {
getContentPane().putClientProperty("$ENLARGED_POP", Boolean.TRUE);
int cpPaddingTop = getContentPane().getStyle().getPaddingTop();
int titlePT = getTitleComponent().getStyle().getPaddingTop();
byte[] pu = getContentPane().getStyle().getPaddingUnit();
if (pu == null) {
pu = new byte[4];
}
pu[0] = Style.UNIT_TYPE_PIXELS;
getContentPane().getStyle().setPaddingUnit(pu);
int pop = Display.getInstance().convertToPixels(manager.getThemeConstant("popupNoTitleAddPaddingInt", 1), false);
getContentPane().getStyle().setPadding(TOP, pop + cpPaddingTop + titlePT);
}
}
}
// allows a text area to recalculate its preferred size if embedded within a dialog
revalidate();
Style contentPaneStyle = getDialogStyle();
boolean restoreArrow = false;
if (manager.isThemeConstant(getDialogUIID() + "ArrowBool", false)) {
Image t = manager.getThemeImageConstant(getDialogUIID() + "ArrowTopImage");
Image b = manager.getThemeImageConstant(getDialogUIID() + "ArrowBottomImage");
Image l = manager.getThemeImageConstant(getDialogUIID() + "ArrowLeftImage");
Image r = manager.getThemeImageConstant(getDialogUIID() + "ArrowRightImage");
Border border = contentPaneStyle.getBorder();
if (border != null) {
border.setImageBorderSpecialTile(t, b, l, r, rect);
restoreArrow = true;
}
}
int prefHeight = contentPane.getPreferredH();
int prefWidth = contentPane.getPreferredW();
if (contentPaneStyle.getBorder() != null) {
prefWidth = Math.max(contentPaneStyle.getBorder().getMinimumWidth(), prefWidth);
prefHeight = Math.max(contentPaneStyle.getBorder().getMinimumHeight(), prefHeight);
}
prefWidth += getUIManager().getLookAndFeel().getVerticalScrollWidth();
int availableHeight = Display.getInstance().getDisplayHeight() - menuHeight - title.getPreferredH();
int availableWidth = Display.getInstance().getDisplayWidth();
int width = Math.min(availableWidth, prefWidth);
int x = 0;
int y = 0;
Command result;
boolean showPortrait;
if (popupDirectionBiasPortrait != null) {
showPortrait = popupDirectionBiasPortrait.booleanValue();
} else {
showPortrait = Display.getInstance().isPortrait();
}
// if we don't have enough space then disregard device orientation
if (showPortrait) {
if (availableHeight < (availableWidth - rect.getWidth()) / 2) {
showPortrait = false;
}
} else {
if (availableHeight / 2 > availableWidth - rect.getWidth()) {
showPortrait = true;
}
}
if (showPortrait) {
if (width < availableWidth) {
int idealX = rect.getX() - width / 2 + rect.getSize().getWidth() / 2;
// if the ideal position is less than 0 just use 0
if (idealX > 0) {
// if the idealX is too far to the right just align to the right
if (idealX + width > availableWidth) {
x = availableWidth - width;
} else {
x = idealX;
}
}
}
if (rect.getY() < availableHeight / 2) {
// popup downwards
y = rect.getY() + rect.getSize().getHeight();
int height = Math.min(prefHeight, availableHeight - y);
result = show(y, availableHeight - height - y, x, availableWidth - width - x, true, true);
} else {
// popup upwards
int height = Math.min(prefHeight, availableHeight - (availableHeight - rect.getY()));
y = rect.getY() - height;
result = show(y, availableHeight - height - y, x, availableWidth - width - x, true, true);
}
} else {
int height = Math.min(prefHeight, availableHeight);
if (height < availableHeight) {
int idealY = rect.getY() - height / 2 + rect.getSize().getHeight() / 2;
// if the ideal position is less than 0 just use 0
if (idealY > 0) {
// if the idealY is too far up just align to the top
if (idealY + height > availableHeight) {
y = availableHeight - height;
} else {
y = idealY;
}
}
}
if (prefWidth > rect.getX()) {
// popup right
x = rect.getX() + rect.getSize().getWidth();
if (x + prefWidth > availableWidth) {
x = availableWidth - prefWidth;
}
width = Math.min(prefWidth, availableWidth - x);
result = show(y, availableHeight - height - y, Math.max(0, x), Math.max(0, availableWidth - width - x), true, true);
} else {
// popup left
width = Math.min(prefWidth, availableWidth - (availableWidth - rect.getX()));
x = rect.getX() - width;
result = show(y, availableHeight - height - y, Math.max(0, x), Math.max(0, availableWidth - width - x), true, true);
}
}
if (restoreArrow) {
contentPaneStyle.getBorder().clearImageBorderSpecialTile();
}
if (result == backCommand) {
return null;
}
return result;
}
Aggregations