mbitsnbites / soundbox

SoundBox is an HTML5 synth music tracker/editor, suitable for creating music for small JavaScript demos (4K / 8K).
https://gitlab.com/mbitsnbites/soundbox
429 stars 77 forks source link

Refactoring GUI logic #59

Closed vsariola closed 5 years ago

vsariola commented 6 years ago

This is a large refactoring to have better code reuse between the three tables, sequencer, pattern and fxtrack. All three are instances of class CTableEditor, which tracks the currently selected cell and selection range. The instances are called mSeq, mPattern and mFxTrack.

Despite no apparent loss in functionality and using 300 lines less, this PR also solves several issues: #19, #50, and #51.

Internally, CTableEditor tracks the selected cells. It also provices automatically copy/paste functionality. And some general methods to help adjusting the contents of the selection (e.g. methods used in +1/-1 buttons). Also, it has a "suppressUpdate" and "resumeUpdate" methods, which can be used to avoid flooding update calls if you know you are going to change several cells. The update will get called only after the last resumeUpdate has been called.

There still isn't observer-pattern to have changes in underlying data automatically reflected to the GUI; rather, the CTableEditor is just slightly better encapsulated version of the old logic: changing the data through CTableEditor guarantees that updates are called appropriately. I wasn't brave enough to start touching the instrument side of things, yet, but I thought it would be better to perfect the CTableEditor change. CInstrumentEditor will be quite a different beast.

vsariola commented 6 years ago

@mbitsnbites I went ballistic with the refactoring (rewriting?). My latest version is in https://github.com/vsariola/soundbox/tree/feature/song_encapsulation

I now encapsulated the song structure inside a CSong class, which implements a kind of observer pattern through signal type event handlers (see CSignal, it's the most minimalistic implementation of signals). Access to the underlying data structure is still provided, so that the player&export&import functions can use it, but maybe we can eventually get rid of them also.

As long as you change the song through CSong, the changes are guaranteed to be reflected to the GUI.

Also, whenever I saw repeating code, I refactored them to use the same functions.

esaruoho commented 5 years ago

@mbitsnbites hi, any chance you could look at this PR and see if it can be merged in?