Skip to content

Commit

Permalink
click support
Browse files Browse the repository at this point in the history
  • Loading branch information
kmfish committed Oct 13, 2015
1 parent fde26b3 commit 16ac9b5
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import master.flame.danmaku.danmaku.renderer.IRenderer.RenderingState;
import master.flame.danmaku.danmaku.util.AndroidUtils;
import tv.cjump.jni.DeviceUtils;
import java.util.List;

public class DrawHandler extends Handler {

Expand Down Expand Up @@ -660,7 +659,7 @@ public void removeAllLiveDanmakus() {
}
}

public List<BaseDanmaku> getCurrentVisibleDanmakus() {
public IDanmakus getCurrentVisibleDanmakus() {
if (drawTask != null) {
return drawTask.getVisibleDanmakusOnTime(getCurrentTime());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@

import android.graphics.Canvas;

import java.util.ArrayList;
import java.util.List;

import master.flame.danmaku.danmaku.model.AbsDisplayer;
import master.flame.danmaku.danmaku.model.BaseDanmaku;
import master.flame.danmaku.danmaku.model.DanmakuTimer;
Expand Down Expand Up @@ -165,17 +162,17 @@ protected synchronized void removeUnusedLiveDanmakusIn(int msec) {
}

@Override
public List<BaseDanmaku> getVisibleDanmakusOnTime(long time) {
public IDanmakus getVisibleDanmakusOnTime(long time) {
long beginMills = time - DanmakuFactory.MAX_DANMAKU_DURATION - 100;
long endMills = time + DanmakuFactory.MAX_DANMAKU_DURATION;
IDanmakus subDanmakus = danmakuList.sub(beginMills, endMills);
List<BaseDanmaku> visibleDanmakus = new ArrayList<BaseDanmaku>();
IDanmakus visibleDanmakus = new Danmakus();
if (null != subDanmakus && !subDanmakus.isEmpty()) {
IDanmakuIterator iterator = subDanmakus.iterator();
while (iterator.hasNext()) {
BaseDanmaku danmaku = iterator.next();
if (danmaku.isShown() && !danmaku.isOutside()) {
visibleDanmakus.add(danmaku);
visibleDanmakus.addItem(danmaku);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@

import android.view.View;

import java.util.List;

import master.flame.danmaku.controller.DrawHandler.Callback;
import master.flame.danmaku.danmaku.model.BaseDanmaku;
import master.flame.danmaku.danmaku.model.DanmakuTimer;
import master.flame.danmaku.danmaku.model.IDanmakus;
import master.flame.danmaku.danmaku.parser.BaseDanmakuParser;

Expand Down Expand Up @@ -46,7 +43,7 @@ public interface IDanmakuView {

public void removeAllLiveDanmakus();

public List<BaseDanmaku> getCurrentVisibleDanmakus();
public IDanmakus getCurrentVisibleDanmakus();

public void setCallback(Callback callback);

Expand Down Expand Up @@ -112,7 +109,17 @@ public interface IDanmakuView {

// ------------- Click Listener -------------------
public interface OnDanmakuClickListener {
void onDanmakuClick(BaseDanmaku danmaku);
/**
*
* @param latest the latest one is clicked
*/
void onDanmakuClick(BaseDanmaku latest);

/**
*
* @param danmakus all to be clicked
*/
void onDanmakuClick(IDanmakus danmakus);
}

public void setOnDanmakuClickListener(OnDanmakuClickListener listener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@

package master.flame.danmaku.controller;

import java.util.List;

import master.flame.danmaku.danmaku.model.AbsDisplayer;
import master.flame.danmaku.danmaku.model.BaseDanmaku;
import master.flame.danmaku.danmaku.model.DanmakuTimer;
import master.flame.danmaku.danmaku.model.IDanmakus;
import master.flame.danmaku.danmaku.parser.BaseDanmakuParser;
import master.flame.danmaku.danmaku.renderer.IRenderer.RenderingState;
Expand All @@ -35,7 +32,7 @@ public interface IDrawTask {

public void clearDanmakusOnScreen(long currMillis);

public List<BaseDanmaku> getVisibleDanmakusOnTime(long time);
public IDanmakus getVisibleDanmakusOnTime(long time);

public RenderingState draw(AbsDisplayer<?> displayer);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,19 @@
import android.view.SurfaceView;
import android.view.View;

import java.util.LinkedList;
import java.util.Locale;

import master.flame.danmaku.controller.DrawHandler;
import master.flame.danmaku.controller.IDanmakuView;
import master.flame.danmaku.controller.IDanmakuViewController;
import master.flame.danmaku.controller.DrawHandler.Callback;
import master.flame.danmaku.controller.DrawHelper;
import master.flame.danmaku.controller.IDanmakuView;
import master.flame.danmaku.controller.IDanmakuViewController;
import master.flame.danmaku.danmaku.model.BaseDanmaku;
import master.flame.danmaku.danmaku.model.IDanmakus;
import master.flame.danmaku.danmaku.parser.BaseDanmakuParser;
import master.flame.danmaku.danmaku.renderer.IRenderer.RenderingState;

import java.util.LinkedList;
import java.util.List;
import java.util.Locale;

public class DanmakuSurfaceView extends SurfaceView implements IDanmakuView, IDanmakuViewController, SurfaceHolder.Callback {

public static final String TAG = "DanmakuSurfaceView";
Expand Down Expand Up @@ -115,7 +114,7 @@ public void removeAllLiveDanmakus() {
}

@Override
public List<BaseDanmaku> getCurrentVisibleDanmakus() {
public IDanmakus getCurrentVisibleDanmakus() {
if (handler != null) {
return handler.getCurrentVisibleDanmakus();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,19 @@
import android.view.TextureView;
import android.view.View;

import java.util.LinkedList;
import java.util.Locale;

import master.flame.danmaku.controller.DrawHandler;
import master.flame.danmaku.controller.IDanmakuView;
import master.flame.danmaku.controller.IDanmakuViewController;
import master.flame.danmaku.controller.DrawHandler.Callback;
import master.flame.danmaku.controller.DrawHelper;
import master.flame.danmaku.controller.IDanmakuView;
import master.flame.danmaku.controller.IDanmakuViewController;
import master.flame.danmaku.danmaku.model.BaseDanmaku;
import master.flame.danmaku.danmaku.model.IDanmakus;
import master.flame.danmaku.danmaku.parser.BaseDanmakuParser;
import master.flame.danmaku.danmaku.renderer.IRenderer.RenderingState;

import java.util.LinkedList;
import java.util.List;
import java.util.Locale;

/**
* DanmakuTextureView需要开启GPU加速才能显示弹幕
* 很遗憾...经过测试TextureView没有提升绘制速度,也许哪里用的不对
Expand Down Expand Up @@ -123,7 +122,7 @@ public void removeAllLiveDanmakus() {
}

@Override
public List<BaseDanmaku> getCurrentVisibleDanmakus() {
public IDanmakus getCurrentVisibleDanmakus() {
if (handler != null) {
return handler.getCurrentVisibleDanmakus();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
import android.graphics.RectF;
import android.view.MotionEvent;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import master.flame.danmaku.controller.IDanmakuView;
import master.flame.danmaku.danmaku.model.BaseDanmaku;
import master.flame.danmaku.danmaku.model.IDanmakuIterator;
import master.flame.danmaku.danmaku.model.IDanmakus;
import master.flame.danmaku.danmaku.model.android.Danmakus;
import master.flame.danmaku.danmaku.util.DanmakuUtils;

/**
Expand All @@ -18,12 +17,12 @@ public class DanmakuTouchHelper {

private IDanmakuView danmakuView;
private RectF mDanmakuBounds;
private List<BaseDanmaku> hitDanmakuList;
private IDanmakus hitDanmakus;

private DanmakuTouchHelper(IDanmakuView danmakuView) {
this.danmakuView = danmakuView;
this.mDanmakuBounds = new RectF();
this.hitDanmakuList = new ArrayList<BaseDanmaku>();
this.hitDanmakus = new Danmakus();
}

public static synchronized DanmakuTouchHelper instance(IDanmakuView danmakuView) {
Expand All @@ -33,9 +32,15 @@ public static synchronized DanmakuTouchHelper instance(IDanmakuView danmakuView)
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
BaseDanmaku clickDanmaku = touchHitDanmaku(event.getX(), event.getY());
if (null != clickDanmaku) {
performClick(clickDanmaku);
IDanmakus clickDanmakus = touchHitDanmaku(event.getX(), event.getY());
BaseDanmaku newestDanmaku = null;
if (null != clickDanmakus && !clickDanmakus.isEmpty()) {
performClick(clickDanmakus);
newestDanmaku = fetchLatestOne(clickDanmakus);
}

if (null != newestDanmaku) {
performClickWithlatest(newestDanmaku);
}
break;
default:
Expand All @@ -45,44 +50,56 @@ public boolean onTouchEvent(MotionEvent event) {
return false;
}

private void performClick(BaseDanmaku danmaku) {
private void performClickWithlatest(BaseDanmaku newest) {
if (danmakuView.getOnDanmakuClickListener() != null) {
danmakuView.getOnDanmakuClickListener().onDanmakuClick(danmaku);
danmakuView.getOnDanmakuClickListener().onDanmakuClick(newest);
}
}

private BaseDanmaku touchHitDanmaku(float x, float y) {
hitDanmakuList.clear();
mDanmakuBounds.setEmpty();
private void performClick(IDanmakus danmakus) {
if (danmakuView.getOnDanmakuClickListener() != null) {
danmakuView.getOnDanmakuClickListener().onDanmakuClick(danmakus);
}
}

List<BaseDanmaku> danmakus = danmakuView.getCurrentVisibleDanmakus();
private IDanmakus touchHitDanmaku(float x, float y) {
hitDanmakus.clear();
mDanmakuBounds.setEmpty();

IDanmakus danmakus = danmakuView.getCurrentVisibleDanmakus();
if (null != danmakus && !danmakus.isEmpty()) {
Iterator<BaseDanmaku> iterator = danmakus.iterator();
IDanmakuIterator iterator = danmakus.iterator();
while (iterator.hasNext()) {
BaseDanmaku danmaku = iterator.next();
mDanmakuBounds.set(danmaku.getLeft(), danmaku.getTop(), danmaku.getRight(), danmaku.getBottom());
if (mDanmakuBounds.contains(x, y)) {
hitDanmakuList.add(danmaku);
hitDanmakus.addItem(danmaku);
}
}

if (!hitDanmakuList.isEmpty()) {
final int size = hitDanmakuList.size();
BaseDanmaku newestDanmaku = null;
for (int i = 0; i < size; i++) {
BaseDanmaku hitDanmaku = hitDanmakuList.get(i);
if (null == newestDanmaku
|| DanmakuUtils.compare(hitDanmaku, newestDanmaku) > 0) {
newestDanmaku = hitDanmaku;
}
}

return newestDanmaku;
}

return hitDanmakus;
}

private BaseDanmaku fetchLatestOne(IDanmakus danmakus) {
if (danmakus.isEmpty()) {
return null;
}

BaseDanmaku newestDanmaku = null;
IDanmakuIterator hitIterator = danmakus.iterator();
while (hitIterator.hasNext()) {
BaseDanmaku hitDanmaku = hitIterator.next();
if (null == newestDanmaku
|| DanmakuUtils.compare(hitDanmaku, newestDanmaku) > 0) {
newestDanmaku = hitDanmaku;
}
}

return null;
return newestDanmaku;

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import android.view.View;

import java.util.LinkedList;
import java.util.List;
import java.util.Locale;

import master.flame.danmaku.controller.DrawHandler;
Expand All @@ -37,6 +36,7 @@
import master.flame.danmaku.controller.IDanmakuView;
import master.flame.danmaku.controller.IDanmakuViewController;
import master.flame.danmaku.danmaku.model.BaseDanmaku;
import master.flame.danmaku.danmaku.model.IDanmakus;
import master.flame.danmaku.danmaku.parser.BaseDanmakuParser;
import master.flame.danmaku.danmaku.renderer.IRenderer.RenderingState;

Expand Down Expand Up @@ -116,7 +116,7 @@ public void removeAllLiveDanmakus() {
}

@Override
public List<BaseDanmaku> getCurrentVisibleDanmakus() {
public IDanmakus getCurrentVisibleDanmakus() {
if (handler != null) {
return handler.getCurrentVisibleDanmakus();
}
Expand Down
10 changes: 8 additions & 2 deletions Sample/src/main/java/com/sample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import master.flame.danmaku.danmaku.loader.android.DanmakuLoaderFactory;
import master.flame.danmaku.danmaku.model.BaseDanmaku;
import master.flame.danmaku.danmaku.model.DanmakuTimer;
import master.flame.danmaku.danmaku.model.IDanmakus;
import master.flame.danmaku.danmaku.model.android.DanmakuGlobalConfig;
import master.flame.danmaku.danmaku.model.android.Danmakus;
import master.flame.danmaku.danmaku.model.android.SpannedCacheStuffer;
Expand Down Expand Up @@ -180,8 +181,13 @@ public void prepared() {
});
mDanmakuView.setOnDanmakuClickListener(new IDanmakuView.OnDanmakuClickListener() {
@Override
public void onDanmakuClick(BaseDanmaku danmaku) {
Log.d("DFM", "onDanmakuClick text:" + danmaku.text);
public void onDanmakuClick(BaseDanmaku latest) {
Log.d("DFM", "onDanmakuClick text:" + latest.text);
}

@Override
public void onDanmakuClick(IDanmakus danmakus) {
Log.d("DFM", "onDanmakuClick danmakus size:" + danmakus.size());
}
});

Expand Down

0 comments on commit 16ac9b5

Please sign in to comment.