Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class InstrumentFragment : Fragment(), InstrumentView {
- private var ecosystemRepository: EcosystemRepository? = null
- private lateinit var noteTitleTable: TableLayout
- private lateinit var instrumentTable: TableLayout
- private lateinit var velocityTable: TableLayout
- private lateinit var noteScroll: HorizontalScrollView
- private lateinit var velocityScroll: HorizontalScrollView
- private lateinit var velocityTableRow: TableRow
- private val checkTable = ArrayList<ArrayList<CheckBox>>()
- private var instrumentScreen: InstrumentScreen? = null
- @RequiresApi(Build.VERSION_CODES.M)
- override fun onCreateView(
- inflater: LayoutInflater, container: ViewGroup?,
- savedInstanceState: Bundle?
- ): View? {
- return inflater.inflate(R.layout.fragment_instrument, container, false)
- }
- @RequiresApi(Build.VERSION_CODES.M)
- override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
- super.onViewCreated(view, savedInstanceState)
- if (instrumentScreen != null) {
- initView()
- showTable()
- }
- }
- companion object {
- fun newInstance() = InstrumentFragment()
- }
- fun setInstrument(instrumentScreen: InstrumentScreen) {
- this.instrumentScreen = instrumentScreen
- }
- fun getInstrument() = instrumentScreen
- @RequiresApi(Build.VERSION_CODES.M)
- private fun initView() {
- noteTitleTable = requireView().findViewById(R.id.note_table)
- instrumentTable = requireView().findViewById(R.id.instrument_table)
- velocityTable = requireView().findViewById(R.id.velocity_table)
- noteScroll = requireView().findViewById(R.id.note_scroll)
- velocityScroll = requireView().findViewById(R.id.velocity_scroll)
- noteScroll.setOnScrollChangeListener { _, i, _, _, _ ->
- velocityScroll.scrollTo(i, 0)
- }
- velocityScroll.setOnScrollChangeListener { _, i, _, _, _ ->
- noteScroll.scrollTo(i, 0)
- }
- for (item in instrumentScreen!!.notes) {
- val checkBoxes = ArrayList<CheckBox>()
- for (i in 0 until item.size) {
- val checkBox = CheckBox(requireContext())
- checkBoxes.add(checkBox)
- }
- checkTable.add(checkBoxes)
- }
- }
- @SuppressLint("UseCompatLoadingForDrawables", "ResourceType")
- @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
- override fun showTable() {
- for (i in 0 until instrumentScreen!!.notes.size) {
- val tableRow = TableRow(requireContext())
- if (instrumentScreen!!.notesTitle != null) {
- val noteRow = TableRow(requireContext())
- val noteTitle = TextView(requireContext())
- noteTitle.setTextColor(Color.WHITE)
- noteTitle.background = requireContext().getDrawable(R.drawable.piano_note_title_bg)
- noteTitle.gravity = Gravity.CENTER
- noteTitle.textSize = 13.3F
- noteTitle.text = instrumentScreen!!.notesTitle!![i]
- noteRow.addView(noteTitle)
- noteTitleTable.addView(noteRow, i)
- }
- if (instrumentScreen!!.name == DRUM) {
- tableRow.setPadding(15)
- }
- for (j in 0 until instrumentScreen!!.notes[i].size) {
- if (instrumentScreen!!.notes[i][j].isChecked) {
- checkTable[i][j].isChecked = true
- }
- checkTable[i][j].setOnCheckedChangeListener { _, b ->
- ecosystemRepository?.logEventByName(MANAGER)
- if (b) {
- instrumentScreen!!.notes[i][j].isChecked = true
- check(b)
- .subscribeOn(Schedulers.io())
- .subscribe({
- instrumentScreen!!.notes[i][j].play(instrumentScreen!!.volume)
- velocityTableRow.getChildAt(j).visibility = View.VISIBLE
- }, {
- })
- } else {
- instrumentScreen!!.notes[i][j].isChecked = false
- }
- }
- checkTable[i][j].buttonDrawable =
- requireContext().getDrawable(instrumentScreen!!.notes[i][j].btn)
- if (instrumentScreen!!.name == DRUM) {
- if (j % 8 == 0 || j % 8 == 1 || j % 8 == 2 || j % 8 == 3) {
- checkTable[i][j].setBackgroundColor(Color.parseColor("#29FF8E00"))
- } else {
- checkTable[i][j].setBackgroundColor(Color.parseColor("#000000"))
- }
- checkTable[i][j].setPadding(15)
- } else {
- instrumentTable.setBackgroundColor(Color.parseColor("#2915FF74"))
- if (j % 8 == 0 || j % 8 == 1 || j % 8 == 2 || j % 8 == 3) {
- checkTable[i][j].buttonDrawable =
- requireContext().getDrawable(R.drawable.note_btn)
- } else {
- checkTable[i][j].buttonDrawable =
- requireContext().getDrawable(R.drawable.note_btn_four)
- }
- }
- if (checkTable[i][j].parent != null) {
- ((checkTable[i][j].parent) as ViewGroup).removeView(checkTable[i][j])
- }
- tableRow.addView(checkTable[i][j], j)
- }
- if (tableRow.parent != null) {
- ((tableRow.parent) as ViewGroup).removeView(tableRow)
- }
- instrumentTable.addView(tableRow, i)
- }
- velocityTableRow = TableRow(requireContext())
- for (i in 0 until instrumentScreen!!.notes[0].size) {
- val velocityConstraint = RelativeLayout(requireContext())
- val velocityItem =
- if (instrumentScreen!!.name == PIANO) LayoutInflater.from(requireContext())
- .inflate(R.layout.velocity_item_layout, velocityConstraint, false)
- else LayoutInflater.from(requireContext())
- .inflate(R.layout.drum_velocity_item_layout, velocityConstraint, false)
- val seekBar: SeekBar = velocityItem.findViewById(R.id.velocity_sb)
- seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
- override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {
- for (noteRow in instrumentScreen!!.notes) {
- for ((index, note) in noteRow.withIndex()) {
- if (index == i) {
- note.volume = p1
- }
- }
- }
- }
- override fun onStartTrackingTouch(p0: SeekBar?) {
- }
- override fun onStopTrackingTouch(p0: SeekBar?) {
- }
- })
- velocityConstraint.addView(velocityItem)
- velocityTableRow.addView(velocityConstraint, i)
- }
- val params = FrameLayout.LayoutParams(
- FrameLayout.LayoutParams.MATCH_PARENT,
- FrameLayout.LayoutParams.WRAP_CONTENT
- )
- params.gravity = Gravity.BOTTOM
- if (instrumentScreen!!.name == PIANO) {
- val r: Resources = resources
- val px = TypedValue.applyDimension(
- TypedValue.COMPLEX_UNIT_DIP,
- 37F,
- r.displayMetrics
- )
- params.marginStart = px.toInt()
- }
- velocityScroll.layoutParams = params
- velocityTable.addView(velocityTableRow)
- }
- fun resetTable() {
- for (row in checkTable) {
- for (check in row) {
- check.isChecked = false
- }
- }
- }
- private fun check(boolean: Boolean): Observable<Boolean> {
- return Observable.just(boolean)
- }
- fun setVelocity(flag: Boolean) {
- velocityScroll.visibility = if (flag) View.VISIBLE else View.GONE
- }
- }
Add Comment
Please, Sign In to add comment