use of com.codename1.charts.compat.Paint in project CodenameOne by codenameone.
the class CommonTransitions method initTransition.
/**
* {@inheritDoc}
*/
public void initTransition() {
firstFinished = false;
if (transitionType == TYPE_EMPTY) {
return;
}
startTime = System.currentTimeMillis();
Component source = getSource();
Component destination = getDestination();
position = 0;
int w = source.getWidth();
int h = source.getHeight();
// improper replace() calls, this may still be valid and shouldn't fail
if (w <= 0 || h <= 0) {
return;
}
// nothing to prepare in advance for a shift fade transition
if (transitionType == TYPE_SLIDE_AND_FADE) {
if (getSource() instanceof Form && getDestination() instanceof Form) {
motion = createMotion(100, 200, speed);
motion2 = createMotion(0, getDestination().getWidth(), speed);
motion.start();
motion2.start();
return;
}
transitionType = TYPE_SLIDE;
}
if (transitionType == TYPE_PULSATE_DIALOG) {
if (getDestination() instanceof Dialog) {
motion = createMotion(600, 1100, 150);
motion.start();
motion2 = createMotion(100, 255, 225);
motion2.start();
pulseState = 0;
Component c = getDialogParent(getDestination());
originalX = c.getX();
originalY = c.getY();
originalWidth = c.getWidth();
originalHeight = c.getHeight();
Display d = Display.getInstance();
Dialog dlg = (Dialog) destination;
// transparent image!
buffer = Image.createImage(Math.min(d.getDisplayWidth(), getDialogParent(dlg).getWidth()), Math.min(d.getDisplayHeight(), dlg.getContentPane().getParent().getHeight() + getDialogTitleHeight(dlg)), 0);
Graphics g = buffer.getGraphics();
Style stl = dlg.getDialogComponent().getStyle();
byte bgt = stl.getBgTransparency();
stl.setBgTransparency(0xff);
drawDialogCmp(buffer.getGraphics(), dlg);
stl.setBgTransparency(bgt & 0xff, true);
return;
}
transitionType = TYPE_EMPTY;
motion = createMotion(0, 0, 0);
pulseState = (byte) 3;
return;
}
if (Display.getInstance().areMutableImagesFast() || transitionType == TYPE_TIMELINE) {
if (buffer == null) {
buffer = createMutableImage(w, h);
} else {
// this might happen when screen orientation changes
if (buffer.getWidth() != w || buffer.getHeight() != h) {
buffer = createMutableImage(w, h);
rgbBuffer = null;
// slide motion might need resetting since screen size is different
motion = null;
}
}
}
if (transitionType == TYPE_FADE) {
motion = createMotion(0, 256, speed);
motion.start();
if (Display.getInstance().areMutableImagesFast()) {
Graphics g = buffer.getGraphics();
g.translate(-source.getAbsoluteX(), -source.getAbsoluteY());
if (getSource().getParent() != null) {
getSource().getComponentForm().paintComponent(g);
}
getSource().paintBackgrounds(g);
g.setClip(0, 0, buffer.getWidth() + source.getAbsoluteX(), buffer.getHeight() + source.getAbsoluteY());
paint(g, getDestination(), 0, 0);
rgbBuffer = new RGBImage(buffer.getRGBCached(), buffer.getWidth(), buffer.getHeight());
paint(g, getSource(), 0, 0, true);
g.translate(source.getAbsoluteX(), source.getAbsoluteY());
}
return;
}
if (transitionType == TYPE_TIMELINE) {
Graphics g = buffer.getGraphics();
g.translate(-source.getAbsoluteX(), -source.getAbsoluteY());
g.setClip(0, 0, buffer.getWidth() + source.getAbsoluteX(), buffer.getHeight() + source.getAbsoluteY());
if (timeline.getWidth() != buffer.getWidth() || timeline.getHeight() != buffer.getHeight()) {
timeline = timeline.scaled(buffer.getWidth(), buffer.getHeight());
}
if (timeline instanceof Timeline) {
((Timeline) timeline).setTime(0);
((Timeline) timeline).setLoop(false);
((Timeline) timeline).setAnimationDelay(0);
}
paint(g, getDestination(), 0, 0);
g.translate(source.getAbsoluteX(), source.getAbsoluteY());
return;
}
if (transitionType == TYPE_SLIDE || transitionType == TYPE_FAST_SLIDE || transitionType == TYPE_COVER || transitionType == TYPE_UNCOVER) {
int dest;
int startOffset = 0;
boolean direction = forward;
// flip the direction only for horizontal slides
if ((source.getUIManager().getLookAndFeel().isRTL()) && slideType == SLIDE_HORIZONTAL) {
direction = !direction;
}
if (slideType == SLIDE_HORIZONTAL) {
dest = w;
if (destination instanceof Dialog) {
startOffset = w - getDialogParent(destination).getWidth();
if (direction) {
startOffset -= getDialogParent(destination).getStyle().getMarginLeft(destination.isRTL());
} else {
startOffset -= getDialogParent(destination).getStyle().getMarginRight(destination.isRTL());
}
} else {
if (source instanceof Dialog) {
dest = getDialogParent(source).getWidth();
if (direction) {
dest += getDialogParent(source).getStyle().getMarginLeft(source.isRTL());
} else {
dest += getDialogParent(source).getStyle().getMarginRight(source.isRTL());
}
}
}
} else {
dest = h;
if (destination instanceof Dialog) {
startOffset = h - getDialogParent(destination).getHeight() - getDialogTitleHeight((Dialog) destination);
if (direction) {
startOffset -= getDialogParent(destination).getStyle().getMarginBottom();
} else {
startOffset -= getDialogParent(destination).getStyle().getMarginTop();
startOffset -= ((Dialog) destination).getTitleStyle().getMarginTop();
if (!drawDialogMenu && ((Dialog) destination).getCommandCount() > 0) {
Container p = ((Dialog) destination).getSoftButton(0).getParent();
if (p != null) {
startOffset -= p.getHeight();
}
}
}
} else {
if (source instanceof Dialog) {
dest = getDialogParent(source).getHeight() + getDialogTitleHeight((Dialog) source);
if (direction) {
dest += getDialogParent(source).getStyle().getMarginBottom();
} else {
dest += getDialogParent(source).getStyle().getMarginTop();
dest += ((Dialog) source).getTitleStyle().getMarginTop();
if (((Dialog) source).getCommandCount() > 0) {
Container p = ((Dialog) source).getSoftButton(0).getParent();
if (p != null) {
dest += p.getHeight();
}
}
}
}
}
}
motion = createMotion(startOffset, dest, speed);
if (!Display.getInstance().areMutableImagesFast()) {
motion.start();
buffer = null;
return;
}
// make sure the destination is painted fully at least once
// we must use a full buffer otherwise the clipping will take effect
Graphics g = buffer.getGraphics();
// tinting is expensive
if (getSource() instanceof Dialog) {
paint(g, getDestination(), 0, 0);
if (transitionType == TYPE_FAST_SLIDE && !(destination instanceof Dialog)) {
Dialog d = (Dialog) source;
secondaryBuffer = createMutableImage(getDialogParent(d).getWidth(), getDialogParent(d).getHeight() + getDialogTitleHeight(d));
drawDialogCmp(secondaryBuffer.getGraphics(), d);
}
} else {
if (getDestination() instanceof Dialog) {
paint(g, getSource(), 0, 0);
if (transitionType == TYPE_FAST_SLIDE && !(source instanceof Dialog)) {
Dialog d = (Dialog) destination;
secondaryBuffer = createMutableImage(getDialogParent(d).getWidth(), d.getContentPane().getParent().getHeight() + getDialogTitleHeight(d));
drawDialogCmp(secondaryBuffer.getGraphics(), d);
}
} else {
paint(g, source, -source.getAbsoluteX(), -source.getAbsoluteY(), true);
if (transitionType == TYPE_FAST_SLIDE) {
secondaryBuffer = createMutableImage(destination.getWidth(), destination.getHeight());
paint(secondaryBuffer.getGraphics(), destination, -destination.getAbsoluteX(), -destination.getAbsoluteY());
}
}
}
motion.start();
}
}
use of com.codename1.charts.compat.Paint in project CodenameOne by codenameone.
the class CommonTransitions method paintSlideAtPosition.
private void paintSlideAtPosition(Graphics g, int slideX, int slideY) {
Component source = getSource();
// if this is the first form we can't do a slide transition since we have no source form
if (source == null) {
return;
}
Component dest = getDestination();
int w = source.getWidth();
int h = source.getHeight();
if (slideType == SLIDE_HORIZONTAL) {
h = 0;
} else {
w = 0;
}
boolean dir = forward;
if (dest != null && dest.getUIManager().getLookAndFeel().isRTL() && slideType == SLIDE_HORIZONTAL) {
dir = !dir;
}
if (dir) {
w = -w;
h = -h;
} else {
slideX = -slideX;
slideY = -slideY;
}
g.setClip(source.getAbsoluteX() + source.getScrollX(), source.getAbsoluteY() + source.getScrollY(), source.getWidth(), source.getHeight());
// dialog animation is slightly different...
if (source instanceof Dialog) {
if (buffer != null) {
g.drawImage(buffer, 0, 0);
} else {
paint(g, dest, 0, 0);
}
paint(g, source, -slideX, -slideY);
return;
}
if (dest instanceof Dialog) {
if (buffer != null) {
g.drawImage(buffer, 0, 0);
} else {
paint(g, source, 0, 0);
}
paint(g, dest, -slideX - w, -slideY - h);
return;
}
if (source.getParent() != null || buffer == null) {
source.paintBackgrounds(g);
paint(g, source, slideX, slideY);
} else {
g.drawImage(buffer, slideX, slideY);
}
paint(g, dest, slideX + w, slideY + h);
}
use of com.codename1.charts.compat.Paint in project CodenameOne by codenameone.
the class SideMenuBar method createMenu.
private Form createMenu(final String placement) {
final Form m = new Form() {
private boolean pressedInRightPanel;
private boolean manualMotionLock;
protected boolean shouldSendPointerReleaseToOtherForm() {
return true;
}
void actionCommandImpl(Command cmd, ActionEvent ev) {
if (cmd instanceof SideMenuBar.CommandWrapper) {
cmd = ((SideMenuBar.CommandWrapper) cmd).cmd;
ev = new ActionEvent(cmd, ActionEvent.Type.Command);
}
final Command c = cmd;
final ActionEvent e = ev;
Display.getInstance().scheduleBackgroundTask(new Runnable() {
public void run() {
Display.getInstance().invokeAndBlock(new Runnable() {
public void run() {
while (Display.getInstance().getCurrent() != parent) {
try {
Thread.sleep(40);
} catch (Exception ex) {
}
}
}
});
Display.getInstance().callSerially(new Runnable() {
public void run() {
parent.actionCommandImpl(c, e);
}
});
}
});
}
protected void sizeChanged(int w, int h) {
Style formStyle = getStyle();
int width = w - (formStyle.getHorizontalMargins());
parent.sizeChangedInternal(w, h);
// close the menu
if (getWidth() != width) {
closeMenu();
}
super.sizeChanged(w, h);
}
public void pointerPressed(int x, int y) {
if (manualMotionLock) {
return;
}
super.pointerPressed(x, y);
if (rightPanel.contains(x, y)) {
pressedInRightPanel = true;
}
}
public void pointerDragged(int[] x, int[] y) {
if (manualMotionLock) {
return;
}
if (!transitionRunning && pressedInRightPanel) {
dragActivated = true;
pressedInRightPanel = false;
}
if (dragActivated) {
setMenuGlassPane(menu, placement);
draggedX = x[0];
repaint();
return;
}
super.pointerDragged(x, y);
}
public void pointerReleased(int x, int y) {
if (manualMotionLock) {
return;
}
super.pointerReleased(x, y);
boolean isRTLValue = isRTL();
if (placement == COMMAND_PLACEMENT_VALUE_RIGHT) {
isRTLValue = !isRTLValue;
}
int displayWidth = Display.getInstance().getDisplayWidth();
if (isRTLValue) {
if (!transitionRunning && dragActivated && x < (displayWidth - rightPanel.getWidth()) / 2) {
final Motion motion = Motion.createEaseInOutMotion(draggedX, rightPanel.getWidth(), 200);
motion.start();
registerAnimated(new Animation() {
public boolean animate() {
draggedX = motion.getValue();
if (motion.isFinished()) {
dragActivated = false;
Display.getInstance().getCurrent().setGlassPane(null);
deregisterAnimated(this);
}
return true;
}
public void paint(Graphics g) {
repaint();
}
});
return;
}
} else {
if (!transitionRunning && dragActivated && x > (displayWidth - rightPanel.getWidth()) / 2) {
final Motion motion = Motion.createEaseInOutMotion(draggedX, Display.getInstance().getDisplayWidth() - rightPanel.getWidth(), 200);
motion.start();
registerAnimated(new Animation() {
public boolean animate() {
draggedX = motion.getValue();
if (motion.isFinished()) {
dragActivated = false;
Display.getInstance().getCurrent().setGlassPane(null);
deregisterAnimated(this);
}
return true;
}
public void paint(Graphics g) {
repaint();
}
});
return;
}
}
if (dragActivated || rightPanel.contains(x, y)) {
setMenuGlassPane(menu, placement);
draggedX = x;
int start = x;
int end = 0;
if (isRTLValue) {
end = getWidth();
}
final Motion motion = Motion.createEaseInOutMotion(start, end, getUIManager().getThemeConstant("sideMenuAnimSpeedInt", 300));
motion.start();
manualMotionLock = true;
sideSwipePotential = false;
rightSideSwipePotential = false;
topSwipePotential = false;
registerAnimated(new Animation() {
public boolean animate() {
draggedX = motion.getValue();
if (motion.isFinished()) {
dragActivated = false;
}
return true;
}
public void paint(Graphics g) {
repaint();
if (draggedX == motion.getDestinationValue() && motion.isFinished()) {
parent.setTransitionInAnimator(CommonTransitions.createEmpty());
parent.show();
deregisterAnimated(this);
Display.getInstance().callSerially(new Runnable() {
public void run() {
clean();
}
});
}
}
});
}
}
public void keyReleased(int keyCode) {
if (keyCode == leftSK) {
if (transitionRunning) {
return;
}
closeMenu();
return;
}
super.keyReleased(keyCode);
}
};
m.setScrollable(false);
m.removeComponentFromForm(m.getTitleArea());
m.putClientProperty("Menu", "true");
m.setTransitionInAnimator(CommonTransitions.createEmpty());
m.setTransitionOutAnimator(CommonTransitions.createEmpty());
m.setBackCommand(new Command("") {
public void actionPerformed(ActionEvent evt) {
if (transitionRunning) {
return;
}
closeMenu();
}
});
m.setLayout(new BorderLayout());
if (Display.getInstance().areMutableImagesFast()) {
rightPanel = new Container(new BorderLayout());
} else {
rightPanel = new Container(new BorderLayout()) {
public void paintBackground(Graphics g) {
}
public void paintBackgrounds(Graphics g) {
}
public void paint(Graphics g) {
Component c = (Component) rightPanel.getClientProperty("$parent");
// not sure why its happening: https://code.google.com/p/codenameone/issues/detail?id=1072
if (c != null) {
boolean b = c.isVisible();
c.setVisible(true);
int x = getAbsoluteX();
g.translate(x, 0);
Container.sidemenuBarTranslation = x;
c.paintComponent(g, true);
Container.sidemenuBarTranslation = 0;
g.translate(-x, 0);
c.setVisible(b);
}
}
};
}
if (placement == COMMAND_PLACEMENT_VALUE_TOP) {
int v = 0;
if (Display.getInstance().isPortrait()) {
if (Display.getInstance().isTablet()) {
v = getUIManager().getThemeConstant("topMenuSizeTabPortraitInt", -1);
if (v < 0) {
v = m.getHeight() * 2 / 3;
} else {
v = m.getHeight() / 100 * v;
}
} else {
v = getUIManager().getThemeConstant("topMenuSizePortraitInt", -1);
if (v < 0) {
v = openButton.getHeight();
} else {
v = m.getHeight() / 100 * v;
}
}
} else {
if (Display.getInstance().isTablet()) {
v = getUIManager().getThemeConstant("topMenuSizeTabLandscapeInt", -1);
if (v < 0) {
v = m.getHeight() * 3 / 4;
} else {
v = m.getWidth() / 100 * v;
}
} else {
v = getUIManager().getThemeConstant("topMenuSizeLandscapeInt", -1);
if (v < 0) {
v = m.getHeight() * 4 / 10;
} else {
v = m.getHeight() / 100 * v;
}
}
}
rightPanel.setPreferredH(v);
} else {
if (Display.getInstance().isPortrait()) {
int v = 0;
if (Display.getInstance().isTablet()) {
v = getUIManager().getThemeConstant("sideMenuSizeTabPortraitInt", -1);
if (v < 0) {
v = m.getWidth() * 2 / 3;
} else {
v = m.getWidth() / 100 * v;
}
} else {
v = getUIManager().getThemeConstant("sideMenuSizePortraitInt", -1);
if (v < 0) {
if (placement == COMMAND_PLACEMENT_VALUE_RIGHT) {
if (isRTL()) {
v = openButton.getWidth();
} else {
v = rightSideButton.getWidth();
}
} else {
v = openButton.getWidth();
}
} else {
v = m.getWidth() / 100 * v;
}
}
rightPanel.setPreferredW(v);
} else {
int v = 0;
if (Display.getInstance().isTablet()) {
v = getUIManager().getThemeConstant("sideMenuSizeTabLandscapeInt", -1);
if (v < 0) {
v = m.getWidth() * 3 / 4;
} else {
v = m.getWidth() / 100 * v;
}
} else {
v = getUIManager().getThemeConstant("sideMenuSizeLandscapeInt", -1);
if (v < 0) {
v = m.getWidth() * 4 / 10;
} else {
v = m.getWidth() / 100 * v;
}
}
rightPanel.setPreferredW(v);
}
}
if (sidePanel != null) {
sidePanel.removeAll();
sidePanel = null;
}
sidePanel = createSideNavigationComponent(getCommands(), placement);
if (placement == COMMAND_PLACEMENT_VALUE_RIGHT) {
m.addComponent(BorderLayout.WEST, rightPanel);
m.addComponent(BorderLayout.CENTER, sidePanel);
} else {
if (placement == COMMAND_PLACEMENT_VALUE_TOP) {
m.addComponent(BorderLayout.NORTH, rightPanel);
m.addComponent(BorderLayout.CENTER, sidePanel);
Button button = new Button(" ");
button.setUIID("Container");
button.setPreferredH(Display.getInstance().getDisplayHeight() / 10);
m.addComponent(BorderLayout.SOUTH, button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
closeMenu();
}
});
} else {
m.addComponent(BorderLayout.EAST, rightPanel);
m.addComponent(BorderLayout.CENTER, sidePanel);
}
}
m.putClientProperty("cn1$sideMenuParent", this);
return m;
}
use of com.codename1.charts.compat.Paint in project CodenameOne by codenameone.
the class FlipTransition method paint.
@Override
public void paint(Graphics g) {
// this can happen if a transition is cut short
if (destBuffer == null) {
return;
}
int cx = g.getClipX();
int cy = g.getClipY();
int cw = g.getClipWidth();
int ch = g.getClipHeight();
int x = getSource().getAbsoluteX();
int y = getSource().getAbsoluteY();
int w = getSource().getWidth();
int h = getSource().getHeight();
g.setClip(x, y, w, h);
if (getBgColor() >= 0) {
int c = g.getColor();
g.setColor(getBgColor());
g.fillRect(x, y, w, h);
g.setColor(c);
} else {
getSource().paintBackgrounds(g);
}
if (g.isPerspectiveTransformSupported()) {
float displayH = Display.getInstance().getDisplayHeight();
float displayW = Display.getInstance().getDisplayWidth();
double midX = (float) x + (float) w / 2.0;
if (perspectiveT == null) {
perspectiveT = Transform.makeIdentity();
}
makePerspectiveTransform(perspectiveT);
float[] bottomRight = perspectiveT.transformPoint(new float[] { displayW, displayH, zNear });
if (currTransform == null) {
currTransform = Transform.makeTranslation(0, 0, 0);
} else {
currTransform.setIdentity();
}
float xfactor = -displayW / bottomRight[0];
float yfactor = -displayH / bottomRight[1];
currTransform.scale(xfactor, yfactor, 0f);
currTransform.translate((x + w / 2) / xfactor, (y + h / 2) / yfactor, 0);
currTransform.concatenate(perspectiveT);
float cameraZ = -zNear - w / 2 * zState;
float cameraX = -x - w / 2;
float cameraY = -y - h / 2;
currTransform.translate(cameraX, cameraY, cameraZ);
if (transitionState == STATE_FLIP) {
currTransform.translate((float) midX, y, 0);
}
Image img = null;
if (flipState < 0.5) {
img = sourceBuffer;
if (transitionState == STATE_FLIP) {
// We are showing the front image
// We will rotate it up to 90 degrees
// 0 -> 0 degrees
// 0.5 -> 90 degress
double sin = flipState * 2.0;
double angle = MathUtil.asin(sin);
// rotate about y axis
currTransform.rotate((float) angle, 0, 1, 0);
}
} else {
img = destBuffer;
if (transitionState == STATE_FLIP) {
// We are showing the back image
// We are showing the back of the image
// We will rotate it from 90 degrees back to 0
// 0.5 -> 90 degrees
// 1.0 -> 0 degrees
double sin = (1.0 - flipState) * 2.0;
double angle = Math.PI - MathUtil.asin(sin);
// rotate about y axis
currTransform.rotate((float) angle, 0, 1, 0);
}
}
if (transitionState == STATE_FLIP) {
currTransform.translate(-(float) midX, -y, 0);
if (flipState >= 0.5f) {
// The rotation will leave the destination image flipped
// backwards, so we need to transform it to be the
// mirror image
currTransform.scale(-1, 1, 1);
currTransform.translate(-2 * x - w, 0, 0);
}
}
if (tmpTransform == null) {
tmpTransform = Transform.makeIdentity();
}
g.getTransform(tmpTransform);
g.setTransform(currTransform);
g.drawImage(img, x, y, w, h);
g.setTransform(tmpTransform);
} else {
perspectiveSupported = false;
if (flipState < 0.5) {
int frontX = x + (int) (flipState * (float) w);
int frontWidth = (int) ((float) w * (1.0 - flipState * 2.0));
g.drawImage(sourceBuffer, frontX, y, frontWidth, h);
} else {
double backState = 1.0 - flipState;
int backX = x + (int) (backState * (float) w);
int backWidth = (int) ((float) w * (1.0 - backState * 2.0));
g.drawImage(destBuffer, backX, y, backWidth, h);
}
}
g.setClip(cx, cy, cw, ch);
}
use of com.codename1.charts.compat.Paint in project CodenameOne by codenameone.
the class Timeline method getRGB.
/**
* {@inheritDoc}
*/
public int[] getRGB() {
Image i = Image.createImage(getWidth(), getHeight());
paint(i.getGraphics(), new Rectangle(0, 0, getWidth(), getHeight()));
return i.getRGB();
}
Aggregations