use of com.codename1.ui.geom.Rectangle in project CodenameOne by codenameone.
the class List method pointerSelect.
private int pointerSelect(int x, int y) {
int selectedIndex = -1;
int numOfcomponents = getModel().getSize();
Style style = getStyle();
Dimension rendererSize = getElementSize(false, true);
Dimension selectedSize = getElementSize(true, true);
Rectangle pos = new Rectangle();
int width = getWidth() - style.getHorizontalPadding() - getSideGap();
if (isScrollableX()) {
width = Math.max(width, getScrollDimension().getWidth() - style.getHorizontalPadding() - getSideGap());
}
y = y - getAbsoluteY();
x = x - getAbsoluteX();
if (fixedSelection < FIXED_NONE_BOUNDRY) {
calculateComponentPosition(getSelectedIndex(), width, pos, rendererSize, getElementSize(true, true), true);
if (orientation != HORIZONTAL) {
if (y < pos.getY()) {
selectedIndex = (y - style.getPaddingTop()) / (rendererSize.getHeight() + itemGap);
} else {
int current = getSelectedIndex();
if (y < pos.getY() + selectedSize.getHeight()) {
selectedIndex = current;
} else {
selectedIndex = (current + 1) + (y - (pos.getY() + selectedSize.getHeight())) / (rendererSize.getHeight() + itemGap);
}
}
} else {
if (isRTL()) {
if (x > pos.getX() + selectedSize.getWidth()) {
int delta = x - (pos.getX() + selectedSize.getWidth());
delta /= (rendererSize.getWidth() + itemGap);
// should have been -1-delta, but works better like this.
selectedIndex = getSelectedIndex() - 1 - delta;
} else {
if (x >= pos.getX()) {
selectedIndex = getSelectedIndex();
} else {
int delta = pos.getX() - x;
delta /= (rendererSize.getWidth() + itemGap);
selectedIndex = getSelectedIndex() + 1 + delta;
}
}
} else {
if (x < pos.getX()) {
selectedIndex = (x - style.getPaddingLeftNoRTL()) / (rendererSize.getWidth() + itemGap);
} else {
int current = getSelectedIndex();
if (x < pos.getX() + selectedSize.getWidth()) {
selectedIndex = current;
} else {
selectedIndex = (current + 1) + (x - (pos.getX() + selectedSize.getWidth())) / (rendererSize.getWidth() + itemGap);
}
}
}
}
} else {
for (int i = 0; i < numOfcomponents; i++) {
calculateComponentPosition(i, width, pos, rendererSize, selectedSize, true);
if (pos.contains(x, y)) {
selectedIndex = i;
break;
}
}
}
if (selectedIndex < 0 || selectedIndex >= size()) {
return -1;
}
return selectedIndex;
}
use of com.codename1.ui.geom.Rectangle in project CodenameOne by codenameone.
the class List method getVisibleBounds.
/**
* {@inheritDoc}
*/
protected Rectangle getVisibleBounds() {
Rectangle pos = new Rectangle();
Dimension rendererSize = getElementSize(false, true);
Style style = getStyle();
int width = getWidth() - style.getHorizontalPadding() - getSideGap();
calculateComponentPosition(getCurrentSelected(), width, pos, rendererSize, getElementSize(true, true), true);
pos.setX(pos.getX() + getX());
pos.setY(pos.getY() + getY());
return pos;
}
use of com.codename1.ui.geom.Rectangle in project CodenameOne by codenameone.
the class List method scrollRectToVisible.
/**
* Makes sure the selected index is visible if it is not in the current view
* rect the list will scroll so it fits within
*
* @param rect the rectangle area to scroll to
*/
public void scrollRectToVisible(Rectangle rect) {
if (fixedSelection < FIXED_NONE_BOUNDRY) {
// Dimension elemSize = getElementSize();
Rectangle toScroll;
if (orientation != HORIZONTAL) {
toScroll = new Rectangle(getScrollX(), rect.getY(), rect.getSize().getWidth(), rect.getSize().getHeight() + itemGap);
} else {
toScroll = new Rectangle(rect.getX(), getScrollY(), rect.getSize().getWidth() + itemGap, rect.getSize().getHeight());
}
super.scrollRectToVisible(toScroll, this);
}
}
use of com.codename1.ui.geom.Rectangle in project CodenameOne by codenameone.
the class List method selectElement.
void selectElement(int selectedIndex) {
Dimension size = getElementSize(false, true);
Rectangle rect;
if (getOrientation() != HORIZONTAL) {
rect = new Rectangle(getX(), (size.getHeight() + itemGap) * selectedIndex, getElementSize(true, true));
} else {
int x = (size.getWidth() + itemGap) * selectedIndex;
if (isRTL() && isScrollableX()) {
x = getScrollDimension().getWidth() - x - (size.getWidth() + itemGap);
}
rect = new Rectangle(x, getY(), getElementSize(true, true));
}
if (hasScrollableParent(getParent())) {
if (hasFocus()) {
scrollRectToVisible(rect);
}
} else {
scrollRectToVisible(rect);
}
}
use of com.codename1.ui.geom.Rectangle in project CodenameOne by codenameone.
the class List method pointerReleasedImpl.
private void pointerReleasedImpl(int x, int y, boolean isHover, boolean longPress) {
if (!isEnabled()) {
return;
}
if (isDragActivated()) {
if (fixedSelection < FIXED_NONE_BOUNDRY) {
super.pointerReleased(x, y);
} else {
boolean vertical = getOrientation() == VERTICAL;
float speed = getDragSpeed(vertical);
if (vertical) {
fixedDraggedMotion = Motion.createFrictionMotion(-fixedDraggedAnimationPosition, Integer.MAX_VALUE, speed, 0.0007f);
} else {
fixedDraggedMotion = Motion.createFrictionMotion(-fixedDraggedAnimationPosition, Integer.MAX_VALUE, speed, 0.0007f);
}
fixedDraggedPosition = fixedDraggedAnimationPosition;
Form p = getComponentForm();
if (p != null) {
p.registerAnimatedInternal(this);
}
fixedDraggedMotion.start();
}
return;
}
if (!isHover && pointerSelect(x, y) > -1) {
if (fixedSelection > FIXED_NONE_BOUNDRY) {
int index = pointerSelect(x, y);
updateAnimationPosition(index - getSelectedIndex());
setSelectedIndex(index);
fireActionEvent(new ActionEvent(eventSource, ActionEvent.Type.Other));
return;
}
if ((fireOnClick && fixedSelection < FIXED_NONE_BOUNDRY) || fireOnRelease) {
// fire the action event into the selected component
Component selectionCmp = renderer.getListCellRendererComponent(this, getSelectedItem(), getSelectedIndex(), true);
Style style = getStyle();
int width = getWidth() - style.getHorizontalPadding() - getSideGap();
Rectangle pos = new Rectangle();
Dimension rendererSize = getElementSize(false, true);
calculateComponentPosition(getSelectedIndex(), width, pos, rendererSize, getElementSize(true, true), true);
int absX = getAbsoluteX();
int posX = pos.getX();
int absY = getAbsoluteY();
int posY = pos.getY();
int newX = x - absX - posX;
int newY = y - absY - posY;
selectionCmp.setX(0);
selectionCmp.setY(0);
if (selectionCmp instanceof Container) {
Component tmp = ((Container) selectionCmp).getComponentAt(newX, newY);
if (tmp != null) {
selectionCmp = tmp;
}
}
if (longPress) {
selectionCmp.longPointerPress(newX, newY);
fireActionEvent(new ActionEvent(eventSource, newX, newY, true));
} else {
selectionCmp.pointerPressed(newX, newY);
selectionCmp.pointerReleased(newX, newY);
fireActionEvent(new ActionEvent(eventSource, newX, newY, false));
}
}
}
}
Aggregations