Skip to content

Commit

Permalink
Refresh selection while dragging.
Browse files Browse the repository at this point in the history
Allows the selection to be refreshed inmediately while dragging the
selection marks and not only at the drag-end event.
  • Loading branch information
netomin committed Aug 19, 2014
1 parent 9081aa5 commit 3efe7bc
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 10 deletions.
2 changes: 1 addition & 1 deletion project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
# project structure.

# Project target.
target=android-8
target=android-19
30 changes: 21 additions & 9 deletions src/com/blahti/drag/DragController.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@ public DragController(Context context) {

}

/**
* Used to notify the on drag event
* */
public void onDrag() {
if (mListener != null) {
mListener.onDrag();
}
}

/**
* Starts a drag.
* It creates a bitmap of the view being dragged. That bitmap is what you see moving.
Expand Down Expand Up @@ -290,8 +299,8 @@ public boolean onInterceptTouchEvent(MotionEvent ev) {
recordScreenSize();
}

final int screenX = clamp((int)ev.getRawX(), 0, mDisplayMetrics.widthPixels);
final int screenY = clamp((int)ev.getRawY(), 0, mDisplayMetrics.heightPixels);
final float screenX = clamp((int)ev.getRawX(), 0, mDisplayMetrics.widthPixels);
final float screenY = clamp((int)ev.getRawY(), 0, mDisplayMetrics.heightPixels);

switch (action) {
case MotionEvent.ACTION_MOVE:
Expand Down Expand Up @@ -336,8 +345,8 @@ public boolean onTouchEvent(MotionEvent ev) {
}

final int action = ev.getAction();
final int screenX = clamp((int)ev.getRawX(), 0, mDisplayMetrics.widthPixels);
final int screenY = clamp((int)ev.getRawY(), 0, mDisplayMetrics.heightPixels);
final float screenX = clamp((int)ev.getRawX(), 0, mDisplayMetrics.widthPixels);
final float screenY = clamp((int)ev.getRawY(), 0, mDisplayMetrics.heightPixels);

switch (action) {
case MotionEvent.ACTION_DOWN:
Expand Down Expand Up @@ -402,6 +411,9 @@ public boolean onTouchEvent(MotionEvent ev) {
}
}
*/
if (mDragSource!=null && mDragging){
onDrag();
}
break;
case MotionEvent.ACTION_UP:
if (mDragging) {
Expand Down Expand Up @@ -439,7 +451,7 @@ private boolean drop(float x, float y) {
return false;
}

private DropTarget findDropTarget(int x, int y, int[] dropCoordinates) {
private DropTarget findDropTarget(float x, float y, int[] dropCoordinates) {
final Rect r = mRectTemp;

final ArrayList<DropTarget> dropTargets = mDropTargets;
Expand All @@ -449,9 +461,9 @@ private DropTarget findDropTarget(int x, int y, int[] dropCoordinates) {
target.getHitRect(r);
target.getLocationOnScreen(dropCoordinates);
r.offset(dropCoordinates[0] - target.getLeft(), dropCoordinates[1] - target.getTop());
if (r.contains(x, y)) {
dropCoordinates[0] = x - dropCoordinates[0];
dropCoordinates[1] = y - dropCoordinates[1];
if (r.contains((int)x, (int)y)) {
dropCoordinates[0] = (int) x - dropCoordinates[0];
dropCoordinates[1] = (int) y - dropCoordinates[1];
return target;
}
}
Expand All @@ -470,7 +482,7 @@ private void recordScreenSize() {
/**
* Clamp val to be &gt;= min and &lt; max.
*/
private static int clamp(int val, int min, int max) {
private static float clamp(float val, float min, float max) {
if (val < min) {
return min;
} else if (val >= max) {
Expand Down
8 changes: 8 additions & 0 deletions src/com/blahti/drag/DragLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,14 @@ public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffse
public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
DragView dragView, Object dragInfo)
{
View v = (View) dragInfo;

int w = v.getWidth ();
int h = v.getHeight ();
int left = x - xOffset;
int top = y - yOffset;
DragLayer.LayoutParams lp = new DragLayer.LayoutParams (w, h, left, top);
this.updateViewLayout(v, lp);
}

public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
Expand Down
5 changes: 5 additions & 0 deletions src/com/blahti/drag/DragListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public interface DragListener {
*/
void onDragStart(DragSource source, Object info, int dragAction);

/**
* Fired while dragging the source to the target.
* */
void onDrag();

/**
* The drag has eneded
*/
Expand Down
31 changes: 31 additions & 0 deletions src/com/brandontate/androidwebviewselection/BTWebView.java
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,37 @@ public void onDragStart(DragSource source, Object info, int dragAction) {

}

@Override
public void onDrag() {
// TODO Auto-generated method stub

MyAbsoluteLayout.LayoutParams startHandleParams = (MyAbsoluteLayout.LayoutParams) mStartSelectionHandle.getLayoutParams();
MyAbsoluteLayout.LayoutParams endHandleParams = (MyAbsoluteLayout.LayoutParams) mEndSelectionHandle.getLayoutParams();

float scale = getDensityIndependentValue(getScale(), mContext);

float startX = startHandleParams.x - getScrollX();
float startY = startHandleParams.y - getScrollY();
float endX = endHandleParams.x - getScrollX();
float endY = endHandleParams.y - getScrollY();

startX = getDensityIndependentValue(startX, mContext) / scale;
startY = getDensityIndependentValue(startY, mContext) / scale;
endX = getDensityIndependentValue(endX, mContext) / scale;
endY = getDensityIndependentValue(endY, mContext) / scale;


if(mLastTouchedSelectionHandle == SELECTION_START_HANDLE && startX > 0 && startY > 0){
String saveStartString = String.format(Locale.US, "javascript: android.selection.setStartPos(%f, %f);", startX, startY);
loadUrl(saveStartString);
}

if(mLastTouchedSelectionHandle == SELECTION_END_HANDLE && endX > 0 && endY > 0){
String saveEndString = String.format(Locale.US, "javascript: android.selection.setEndPos(%f, %f);", endX, endY);
loadUrl(saveEndString);
}
}

@Override
public void onDragEnd() {
// TODO Auto-generated method stub
Expand Down

0 comments on commit 3efe7bc

Please sign in to comment.