Skip to content

Commit

Permalink
DanmakuGlobalConfig: add setMaximumLines(mapPair) && setOverlapping(m…
Browse files Browse the repository at this point in the history
…apPair)
  • Loading branch information
ctiao committed Sep 10, 2015
1 parent a63a573 commit 5ab69f6
Show file tree
Hide file tree
Showing 4 changed files with 261 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@

public class DanmakuFilters {

public static final int FILTER_TYPE_TYPE = 0x00000001;
public static final int FILYER_TYPE_QUANTITY = 0x0000002;
public static final int FILTER_TYPE_ELAPSED_TIME = 0x00000004;
public static final int FILTER_TYPE_TEXTCOLOR = 0x00000008;
public static final int FILTER_TYPE_USER_ID = 0x00000020;
public static final int FILTER_TYPE_USER_HASH = 0x00000040;
public static final int FILTER_TYPE_USER_GUEST = 0x00000080;
public static final int FILTER_TYPE_DUPLICATE_MERGE = 0x00000100;
public static final int FILTER_TYPE_TYPE = 1;
public static final int FILYER_TYPE_QUANTITY = 2;
public static final int FILTER_TYPE_ELAPSED_TIME = 4;
public static final int FILTER_TYPE_TEXTCOLOR = 8;
public static final int FILTER_TYPE_USER_ID = 16;
public static final int FILTER_TYPE_USER_HASH = 32;
public static final int FILTER_TYPE_USER_GUEST = 64;
public static final int FILTER_TYPE_DUPLICATE_MERGE = 128;
public static final int FILTER_TYPE_MAXIMUM_LINES = 256;
public static final int FILTER_TYPE_OVERLAPPING = 512;


public static interface IDanmakuFilter<T> {
Expand Down Expand Up @@ -459,6 +461,62 @@ public void clear() {

}

public static class MaximumLinesFilter extends BaseDanmakuFilter<Map<Integer, Integer>> {

private Map<Integer, Integer> mMaximumLinesPairs;

@Override
public boolean filter(BaseDanmaku danmaku, int lines, int totalsizeInScreen, DanmakuTimer timer, boolean willHit) {
boolean filtered = false;
if (mMaximumLinesPairs != null) {
Integer maxLines = mMaximumLinesPairs.get(danmaku.getType());
filtered = (maxLines != null && lines >= maxLines);
if (filtered) {
danmaku.mFilterParam |= FILTER_TYPE_MAXIMUM_LINES;
}
}
return filtered;
}

@Override
public void setData(Map<Integer, Integer> data) {
mMaximumLinesPairs = data;
}

@Override
public void reset() {
mMaximumLinesPairs = null;
}
}

public static class OverlappingFilter extends BaseDanmakuFilter<Map<Integer, Boolean>> {

private Map<Integer, Boolean> mEnabledPairs;

@Override
public boolean filter(BaseDanmaku danmaku, int index, int totalsizeInScreen, DanmakuTimer timer, boolean willHit) {
boolean filtered = false;
if (mEnabledPairs != null) {
Boolean enabledValue = mEnabledPairs.get(danmaku.getType());
filtered = enabledValue != null && enabledValue && willHit;
if (filtered) {
danmaku.mFilterParam |= FILTER_TYPE_OVERLAPPING;
}
}
return filtered;
}

@Override
public void setData(Map<Integer, Boolean> data) {
mEnabledPairs = data;
}

@Override
public void reset() {
mEnabledPairs = null;
}
}

public final static String TAG_TYPE_DANMAKU_FILTER = "1010_Filter";

public final static String TAG_QUANTITY_DANMAKU_FILTER = "1011_Filter";
Expand All @@ -475,12 +533,16 @@ public void clear() {

public static final String TAG_DUPLICATE_FILTER = "1017_Filter";

public static final String TAG_MAXIMUN_LINES_FILTER = "1018_Filter";

public static final String TAG_OVERLAPPING_FILTER = "1019_Filter";

private static DanmakuFilters instance = null;

public final Exception filterException = new Exception("not suuport this filter tag");

public void filter(BaseDanmaku danmaku, int index, int totalsizeInScreen,
DanmakuTimer timer, boolean fromCachingTask) {
DanmakuTimer timer, boolean fromCachingTask) {
for (IDanmakuFilter<?> f : mFilterArray) {
if (f != null) {
boolean filtered = f.filter(danmaku, index, totalsizeInScreen, timer, fromCachingTask);
Expand All @@ -492,20 +554,44 @@ public void filter(BaseDanmaku danmaku, int index, int totalsizeInScreen,
}
}

public boolean filterSecondary(BaseDanmaku danmaku, int lines, int totalsizeInScreen,
DanmakuTimer timer, boolean willHit) {
for (IDanmakuFilter<?> f : mFilterArraySecondary) {
if (f != null) {
boolean filtered = f.filter(danmaku, lines, totalsizeInScreen, timer, willHit);
danmaku.filterResetFlag = GlobalFlagValues.FILTER_RESET_FLAG;
if (filtered) {
return true;
}
}
}
return false;
}

private final static Map<String, IDanmakuFilter<?>> filters = Collections
.synchronizedSortedMap(new TreeMap<String, IDanmakuFilter<?>>());
private final static Map<String, IDanmakuFilter<?>> filtersSecondary = Collections
.synchronizedSortedMap(new TreeMap<String, IDanmakuFilter<?>>());
IDanmakuFilter<?>[] mFilterArray = new IDanmakuFilter[0];
IDanmakuFilter<?>[] mFilterArraySecondary = new IDanmakuFilter[0];

public IDanmakuFilter<?> get(String tag) {
IDanmakuFilter<?> f = filters.get(tag);
return get(tag, true);
}

public IDanmakuFilter<?> get(String tag, boolean primary) {
IDanmakuFilter<?> f = primary ? filters.get(tag) : filtersSecondary.get(tag);
if (f == null) {
f = registerFilter(tag);
f = registerFilter(tag, primary);
}
return f;
}

IDanmakuFilter<?>[] mFilterArray = new IDanmakuFilter[0];

public IDanmakuFilter<?> registerFilter(String tag) {
return registerFilter(tag, true);
}

public IDanmakuFilter<?> registerFilter(String tag, boolean primary) {
if (tag == null) {
throwFilterException();
return null;
Expand All @@ -528,25 +614,40 @@ public IDanmakuFilter<?> registerFilter(String tag) {
filter = new GuestFilter();
} else if (TAG_DUPLICATE_FILTER.equals(tag)) {
filter = new DuplicateMergingFilter();
} else if (TAG_MAXIMUN_LINES_FILTER.equals(tag)) {
filter = new MaximumLinesFilter();
} else if (TAG_OVERLAPPING_FILTER.equals(tag)) {
filter = new OverlappingFilter();
}
// add more filter
}
if (filter == null) {
throwFilterException();
return null;
}
filter.setData(null);
filters.put(tag, filter);
mFilterArray = filters.values().toArray(mFilterArray);
if (primary) {
filters.put(tag, filter);
mFilterArray = filters.values().toArray(mFilterArray);
} else {
filtersSecondary.put(tag, filter);
mFilterArraySecondary = filtersSecondary.values().toArray(mFilterArraySecondary);
}
return filter;
}

public void unregisterFilter(String tag) {
IDanmakuFilter<?> f = filters.remove(tag);
unregisterFilter(tag, true);
}

public void unregisterFilter(String tag, boolean primary) {
IDanmakuFilter<?> f = primary ? filters.remove(tag) : filtersSecondary.remove(tag);
if (f != null) {
f.clear();
f = null;
mFilterArray = filters.values().toArray(mFilterArray);
if (primary) {
mFilterArray = filters.values().toArray(mFilterArray);
} else {
mFilterArraySecondary = filtersSecondary.values().toArray(mFilterArraySecondary);
}
}
}

Expand All @@ -555,19 +656,29 @@ public void clear() {
if (f != null)
f.clear();
}
for (IDanmakuFilter<?> f : mFilterArraySecondary) {
if (f != null)
f.clear();
}
}

public void reset() {
for (IDanmakuFilter<?> f : mFilterArray) {
if (f != null)
f.reset();
}
for (IDanmakuFilter<?> f : mFilterArraySecondary) {
if (f != null)
f.reset();
}
}

public void release() {
clear();
filters.clear();
mFilterArray = new IDanmakuFilter[0];
filtersSecondary.clear();
mFilterArraySecondary = new IDanmakuFilter[0];
}

private void throwFilterException() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import master.flame.danmaku.controller.DanmakuFilters;
import master.flame.danmaku.controller.DanmakuFilters.IDanmakuFilter;
Expand All @@ -18,7 +19,7 @@
public class DanmakuGlobalConfig {

public enum DanmakuConfigTag {
FT_DANMAKU_VISIBILITY, FB_DANMAKU_VISIBILITY, L2R_DANMAKU_VISIBILITY, R2L_DANMAKU_VISIBILIY, SPECIAL_DANMAKU_VISIBILITY, TYPEFACE, TRANSPARENCY, SCALE_TEXTSIZE, MAXIMUM_NUMS_IN_SCREEN, DANMAKU_STYLE, DANMAKU_BOLD, COLOR_VALUE_WHITE_LIST, USER_ID_BLACK_LIST, USER_HASH_BLACK_LIST, SCROLL_SPEED_FACTOR, BLOCK_GUEST_DANMAKU, DUPLICATE_MERGING_ENABLED;
FT_DANMAKU_VISIBILITY, FB_DANMAKU_VISIBILITY, L2R_DANMAKU_VISIBILITY, R2L_DANMAKU_VISIBILIY, SPECIAL_DANMAKU_VISIBILITY, TYPEFACE, TRANSPARENCY, SCALE_TEXTSIZE, MAXIMUM_NUMS_IN_SCREEN, DANMAKU_STYLE, DANMAKU_BOLD, COLOR_VALUE_WHITE_LIST, USER_ID_BLACK_LIST, USER_HASH_BLACK_LIST, SCROLL_SPEED_FACTOR, BLOCK_GUEST_DANMAKU, DUPLICATE_MERGING_ENABLED, MAXIMUN_LINES, OVERLAPPING_ENABLE;

public boolean isVisibilityRelatedTag() {
return this.equals(FT_DANMAKU_VISIBILITY) || this.equals(FB_DANMAKU_VISIBILITY)
Expand Down Expand Up @@ -172,8 +173,12 @@ public DanmakuGlobalConfig setFTDanmakuVisibility(boolean visible) {
}

private <T> void setFilterData(String tag, T data) {
setFilterData(tag, data, true);
}

private <T> void setFilterData(String tag, T data, boolean primary) {
@SuppressWarnings("unchecked")
IDanmakuFilter<T> filter = (IDanmakuFilter<T>) DanmakuFilters.getDefault().get(tag);
IDanmakuFilter<T> filter = (IDanmakuFilter<T>) DanmakuFilters.getDefault().get(tag, primary);
filter.setData(data);
}

Expand Down Expand Up @@ -542,6 +547,48 @@ public DanmakuGlobalConfig setDuplicateMergingEnabled(boolean enable) {
return this;
}

public boolean isDuplicateMergingEnabled() {
return mDuplicateMergingEnable;
}

/**
* 设置最大显示行数
* @param pairs map<K,V> 设置null取消行数限制
* K = (BaseDanmaku.TYPE_SCROLL_RL|BaseDanmaku.TYPE_SCROLL_LR|BaseDanmaku.TYPE_FIX_TOP|BaseDanmaku.TYPE_FIX_BOTTOM)
* V = 最大行数
* @return
*/
public DanmakuGlobalConfig setMaximumLines(Map<Integer, Integer> pairs) {
if (pairs == null) {
DanmakuFilters.getDefault()
.unregisterFilter(DanmakuFilters.TAG_MAXIMUN_LINES_FILTER, false);
} else {
setFilterData(DanmakuFilters.TAG_MAXIMUN_LINES_FILTER, pairs, false);
}
GlobalFlagValues.updateFilterFlag();
notifyConfigureChanged(DanmakuConfigTag.MAXIMUN_LINES, pairs);
return this;
}

/**
* 设置弹幕是否重叠
* @param pairs map<K,V> 设置null恢复默认设置,默认为重叠
* K = (BaseDanmaku.TYPE_SCROLL_RL|BaseDanmaku.TYPE_SCROLL_LR|BaseDanmaku.TYPE_FIX_TOP|BaseDanmaku.TYPE_FIX_BOTTOM)
* V = true|false 是否重叠
* @return
*/
public DanmakuGlobalConfig setOverlapping(Map<Integer, Boolean> pairs) {
if (pairs == null) {
DanmakuFilters.getDefault()
.unregisterFilter(DanmakuFilters.TAG_OVERLAPPING_FILTER, false);
} else {
setFilterData(DanmakuFilters.TAG_OVERLAPPING_FILTER, pairs, false);
}
GlobalFlagValues.updateFilterFlag();
notifyConfigureChanged(DanmakuConfigTag.OVERLAPPING_ENABLE, pairs);
return this;
}

/**
* 设置缓存绘制填充器,默认使用{@link SimpleTextCacheStuffer}只支持纯文字显示, 如果需要图文混排请设置{@link SpannedCacheStuffer}
* 如果需要定制其他样式请扩展{@link SimpleTextCacheStuffer}|{@link SpannedCacheStuffer}
Expand All @@ -555,11 +602,6 @@ public DanmakuGlobalConfig setCacheStuffer(BaseCacheStuffer cacheStuffer) {
return this;
}

public boolean isDuplicateMergingEnabled() {
return mDuplicateMergingEnable;
}


public interface ConfigChangedCallback {
public boolean onDanmakuConfigChanged(DanmakuGlobalConfig config, DanmakuConfigTag tag,
Object... value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import master.flame.danmaku.controller.DanmakuFilters;
import master.flame.danmaku.danmaku.model.BaseDanmaku;
import master.flame.danmaku.danmaku.model.DanmakuTimer;
import master.flame.danmaku.danmaku.model.GlobalFlagValues;
import master.flame.danmaku.danmaku.model.IDanmakuIterator;
import master.flame.danmaku.danmaku.model.IDanmakus;
import master.flame.danmaku.danmaku.model.IDisplayer;
Expand All @@ -30,7 +31,17 @@ public class DanmakuRenderer extends Renderer {

private final DanmakuTimer mStartTimer = new DanmakuTimer();
private final RenderingState mRenderingState = new RenderingState();

private DanmakusRetainer.Verifier mVerifier = new DanmakusRetainer.Verifier() {
@Override
public boolean skipLayout(BaseDanmaku danmaku, float fixedTop, int lines, boolean willHit) {
if (DanmakuFilters.getDefault().filterSecondary(danmaku, lines, 0, mStartTimer, willHit)) {
danmaku.setVisibility(false);
return true;
}
return false;
}
};

@Override
public void clear() {
DanmakusRetainer.clear();
Expand Down Expand Up @@ -80,7 +91,7 @@ public RenderingState draw(IDisplayer disp, IDanmakus danmakus, long startRender
}

// layout
DanmakusRetainer.fix(drawItem, disp);
DanmakusRetainer.fix(drawItem, disp, mVerifier);

// draw
if (!drawItem.isOutside() && drawItem.isShown()) {
Expand Down
Loading

0 comments on commit 5ab69f6

Please sign in to comment.