Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 添加弹幕透明度相关设置,优化描边显示 #135

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions BilibiliLive/Component/Settings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ enum Settings {

@UserDefault("Settings.danmuFilter", defaultValue: false)
static var enableDanmuFilter: Bool

@UserDefaultCodable("Settings.danmuAlpha", defaultValue: .alpha_10)
static var danmuAlpha: DanmuAlpha

@UserDefaultCodable("Settings.danmuStrokeWidth", defaultValue: .width_20)
static var danmuStrokeWidth: DanmuStrokeWidth

@UserDefaultCodable("Settings.danmuStrokeAlpha", defaultValue: .alpha_08)
static var danmuStrokeAlpha: DanmuStrokeAlpha
}

struct MediaQuality {
Expand Down Expand Up @@ -171,6 +180,51 @@ enum DanmuSize: String, Codable, CaseIterable {
}
}

enum DanmuAlpha: Double, Codable, CaseIterable {
case alpha_03 = 0.3
case alpha_04 = 0.4
case alpha_05 = 0.5
case alpha_06 = 0.6
case alpha_07 = 0.7
case alpha_08 = 0.8
case alpha_09 = 0.9
case alpha_10 = 1.0

var title: String {
return String(format: "%.1f", rawValue)
}
}

enum DanmuStrokeWidth: Double, Codable, CaseIterable {
case width_0 = 0.0
case width_05 = 0.5
case width_10 = 1.0
case width_15 = 1.5
case width_20 = 2.0

var title: String {
return String(format: "%.1f", rawValue)
}
}

enum DanmuStrokeAlpha: Double, Codable, CaseIterable {
case alpha_00 = 0.0
case alpha_01 = 0.1
case alpha_02 = 0.2
case alpha_03 = 0.3
case alpha_04 = 0.4
case alpha_05 = 0.5
case alpha_06 = 0.6
case alpha_07 = 0.7
case alpha_08 = 0.8
case alpha_09 = 0.9
case alpha_10 = 1.0

var title: String {
return String(format: "%.1f", rawValue)
}
}

extension DanmuArea {
var title: String {
switch self {
Expand Down
24 changes: 24 additions & 0 deletions BilibiliLive/Module/Personal/SettingsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,30 @@ class SettingsViewController: UIViewController {
}
Toggle(title: "智能防档弹幕", setting: Settings.danmuMask, onChange: Settings.danmuMask.toggle())
Toggle(title: "按需本地运算智能防档弹幕(Exp)", setting: Settings.vnMask, onChange: Settings.vnMask.toggle())

// 添加弹幕透明度设置
Actions(title: "弹幕透明度", message: "调整弹幕的透明度",
current: Settings.danmuAlpha.title,
options: DanmuAlpha.allCases,
optionString: DanmuAlpha.allCases.map({ $0.title })) { value in
Settings.danmuAlpha = value
}

// 添加弹幕描边宽度设置
Actions(title: "弹幕描边宽度", message: "调整弹幕描边的粗细",
current: Settings.danmuStrokeWidth.title,
options: DanmuStrokeWidth.allCases,
optionString: DanmuStrokeWidth.allCases.map({ $0.title })) { value in
Settings.danmuStrokeWidth = value
}

// 添加弹幕描边透明度设置
Actions(title: "弹幕描边透明度", message: "调整弹幕描边的透明度",
current: Settings.danmuStrokeAlpha.title,
options: DanmuStrokeAlpha.allCases,
optionString: DanmuStrokeAlpha.allCases.map({ $0.title })) { value in
Settings.danmuStrokeAlpha = value
}
}

SectionModel(title: "港澳台解锁") {
Expand Down
8 changes: 5 additions & 3 deletions BilibiliLive/Vendor/DanmakuKit/DanmakuTextCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ class DanmakuTextCell: DanmakuCell {
override func displaying(_ context: CGContext, _ size: CGSize, _ isCancelled: Bool) {
guard let model = model as? DanmakuTextCellModel else { return }
let text = NSString(string: model.text)
context.setLineWidth(2)
context.setAlpha(CGFloat(Settings.danmuAlpha.rawValue))
context.setLineWidth(CGFloat(Settings.danmuStrokeWidth.rawValue))
context.setLineJoin(.round)
context.saveGState()
context.setTextDrawingMode(.stroke)

let attributes: [NSAttributedString.Key: Any] = [.font: model.font, .foregroundColor: UIColor.black]
context.setStrokeColor(UIColor.black.cgColor)
let strokeColor = UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: CGFloat(Settings.danmuStrokeAlpha.rawValue))
let attributes: [NSAttributedString.Key: Any] = [.font: model.font, .foregroundColor: strokeColor]
context.setStrokeColor(strokeColor.cgColor)
text.draw(at: .zero, withAttributes: attributes)
context.restoreGState()

Expand Down