Skip to content

Commit

Permalink
half-working; gonna remove parent field from Staff model class
Browse files Browse the repository at this point in the history
  • Loading branch information
klesun committed Aug 9, 2015
1 parent bdc2e68 commit eb84d64
Show file tree
Hide file tree
Showing 21 changed files with 247 additions and 188 deletions.
3 changes: 2 additions & 1 deletion src/org/klesun_model/AbstractHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ final public Explain handleKey(Combo combo) {
result = getMyClassActionMap().get(combo).redo(getContext());

if (getContext() instanceof MidianaComponent) { // i don't like this
MidianaComponent.class.cast(getContext()).getPanel().checkCam();
// MidianaComponent.class.cast(getContext()).getPanel().checkCam();
Main.window.staffPanel.checkCam();
}
Main.window.updateMenuBar();
}
Expand Down
2 changes: 1 addition & 1 deletion src/org/klesun_model/AbstractPainter.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ final protected void drawString(String str, Rectangle rect, Color c) {
}

final protected void drawModel(MidianaComponent model, int x0, int y0, Boolean completeRepaint) {
model.drawOn(g, x + x0, y + y0, completeRepaint);
model.drawOn(g, x + x0, y + y0);
}
final protected void drawImage(Image image, int x0, int y0) {
g.drawImage(image, x + x0, y + y0, null);
Expand Down
12 changes: 8 additions & 4 deletions src/org/klesun_model/IComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import org.sheet_midusic.staff.staff_panel.MainPanel;
import org.sheet_midusic.staff.chord.Chord;
import org.sheet_midusic.staff.chord.nota.Nota;
import org.sheet_midusic.staff.staff_panel.SheetMusicPanel;
import org.sheet_midusic.staff.staff_panel.StaffComponent;

import java.awt.*;
import java.awt.event.FocusListener;
Expand All @@ -18,7 +20,7 @@ public interface IComponent {

default Component getFirstAwtParent() {
IComponent context = this;
while (!(context instanceof Component) && context != null) { // circular import? yes...
while (!(context instanceof Component) && context != null) {
context = context.getModelParent();
}
return (Component)context;
Expand Down Expand Up @@ -47,9 +49,11 @@ default java.util.List<IComponent> makeFakePossibleChildListForClassMethods() {
} else if (this.getClass() == Article.class) {
return Arrays.asList(new Paragraph((Article)this));
} else */if (this.getClass() == MainPanel.class) {
return Arrays.asList(new Staff((MainPanel)this));
} else if (this.getClass() == Staff.class) {
return Arrays.asList(new Chord((Staff)this));
return Arrays.asList(new SheetMusicPanel((MainPanel)this));
} else if (this.getClass() == SheetMusicPanel.class) {
return Arrays.asList(new StaffComponent(new Staff((MainPanel)this.getModelParent())));
} else if (this.getClass() == StaffComponent.class) {
return Arrays.asList(new Chord((StaffComponent)this));
} else if (this.getClass() == Chord.class) {
return Arrays.asList(new Nota((Chord)this));
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/org/klesun_model/IModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ default JSONObject getJsonRepresentation() {
return dict;
}

default IModel reconstructFromJson(JSONObject jsObject) throws JSONException {

default IModel reconstructFromJson(JSONObject jsObject) throws JSONException
{
for (Field field : getModelHelper().getFieldStorage()) {
if (jsObject.has(field.getName())) {
field.setValueFromJsObject(jsObject);
Expand Down
8 changes: 4 additions & 4 deletions src/org/klesun_model/field/Field.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,14 @@ public void setOnChange(Runnable onChange) {
public Object getJsonValue() { return get(); };

// override me please!
public void setValueFromJsObject(JSONObject jsObject) {

public void setValueFromJsObject(JSONObject jsObject)
{
this.setValueFromString(jsObject.get(getName()).toString());
}

public Field setValueFromString(String str) {
public Field setValueFromString(String str)
{
set((E)getParserMap().get(elemClass).apply(str));

return this;
}

Expand Down
5 changes: 3 additions & 2 deletions src/org/sheet_midusic/staff/MidianaComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ abstract public class MidianaComponent extends AbstractModel implements ICompone

abstract public MidianaComponent getFocusedChild();
abstract protected AbstractHandler makeHandler();
abstract public void drawOn(Graphics2D surface, int x, int y, Boolean completeRepaint); // TODO: renmae to paintComponent() for compatibility with AWT components
/** @return int - position of bottomest drawn pixel */
abstract public int drawOn(Graphics2D surface, int x, int y); // TODO: renmae to paintComponent() for compatibility with AWT components

// TODO: separate Model from Event handler, i wanna be able to instantiate Nota without Staff!
public MidianaComponent(IComponent parent) {
Expand Down Expand Up @@ -47,7 +48,7 @@ public MainPanel getPanel() {
}

final public Settings getSettings() {
return getPanel().getSettings();
return Settings.inst();
}
final public ImageStorage getImageStorage() { return ImageStorage.inst(); }
final public int dx() { return getSettings().getStepWidth(); }
Expand Down
65 changes: 26 additions & 39 deletions src/org/sheet_midusic/staff/Staff.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.sheet_midusic.staff;

import org.klesun_model.AbstractModel;
import org.sheet_midusic.staff.chord.Chord;
import org.sheet_midusic.staff.chord.Tact;
import org.sheet_midusic.staff.chord.nota.Nota;
import org.klesun_model.Explain;
import org.klesun_model.IModel;
import org.klesun_model.SimpleAction;
import org.sheet_midusic.staff.chord.ChordHandler;
import org.sheet_midusic.staff.staff_config.StaffConfig;

Expand All @@ -15,11 +15,12 @@
import java.util.List;

import org.sheet_midusic.staff.staff_panel.MainPanel;
import org.sheet_midusic.staff.staff_panel.StaffComponent;
import org.sheet_midusic.stuff.Midi.DeviceEbun;
import org.sheet_midusic.stuff.Midi.Playback;
import org.sheet_midusic.stuff.graphics.Settings;
import org.sheet_midusic.stuff.musica.PlayMusThread;

import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
import java.util.stream.Stream;

Expand All @@ -32,7 +33,7 @@
import org.json.JSONObject;

/** A Staff is part of SheetMusic with individual StaffConfig properties (keySignature/tempo/tactSize) */
public class Staff extends MidianaComponent
public class Staff extends AbstractModel
{
final public static int SISDISPLACE = 40;
public static final int DEFAULT_ZNAM = 64; // TODO: move it into some constants maybe
Expand All @@ -50,10 +51,9 @@ public enum aMode { insert, passive }
@Deprecated final private MainPanel blockPanel;
final private Playback playback;

private Boolean surfaceChanged = true;

public Staff(MainPanel blockPanel) {
super(null);
public Staff(MainPanel blockPanel)
{
// super(null);
this.blockPanel = blockPanel;
this.staffConfig = new StaffConfig(this);
this.playback = new Playback(this);
Expand Down Expand Up @@ -84,26 +84,22 @@ public Chord addNewAccord()

public Chord addNewAccord(int position)
{
return add(new Chord(this), position);
// TODO: it's a temporary hack till we completely separate Model from Component
StaffComponent hackPanel = blockPanel.staffContainer.getFocusedChild();
return add(new Chord(hackPanel), position);
}

/** TODO: public is temporary */
public synchronized Chord add(Chord chord, int index) {
getHandler().performAction(new SimpleAction()
.setRedo(() -> getChordList().add(index, chord))
.setUndo(() -> getChordList().remove(chord)));

getChordList().add(index, chord);
accordListChanged(index);

return chord;
}

public synchronized void remove(Chord chord) {
int index = getChordList().indexOf(chord);
if (index <= getFocusedIndex()) { setFocusedIndex(getFocusedIndex() - 1); }
getHandler().performAction(new SimpleAction()
.setRedo(() -> getChordList().remove(chord))
.setUndo(() -> getChordList().add(index, chord)));
getChordList().remove(chord);

accordListChanged(index);
}
Expand All @@ -117,15 +113,10 @@ private void accordListChanged(int repaintAllFromIndex) {
this.tactList = recalcTactList(); // TODO: maybe do some optimization using repaintAllFromIndex
}

@Deprecated
public void drawOn(Graphics2D g, int x, int y, Boolean completeRepaint) {
drawOn(g, x, y);
}

public synchronized int drawOn(Graphics2D g, int x, int y) {
new StaffPainter(this, g, x, y).draw(true);
return getHeightIf(getWidth());
}
// public synchronized int drawOn(Graphics2D g, int x, int y) {
// new StaffPainter(this, g, x, y).draw(true);
// return getHeightIf(getWidth());
// }

@Override
public JSONObject getJsonRepresentation() {
Expand Down Expand Up @@ -180,20 +171,15 @@ public Staff reconstructFromJson(JSONObject jsObject) throws JSONException {
return this;
}

@Override
public StaffHandler getHandler() { return (StaffHandler)super.getHandler(); }

public Staff clearStan() {
this.getChordList().clear();
this.focusedIndex = -1;

return this;
}

@Override
public Chord getFocusedChild() { return getFocusedAccord(); }
@Override
protected StaffHandler makeHandler() { return new StaffHandler(this); }
// public Chord getFocusedChild() { return getFocusedAccord(); }
// protected StaffHandler makeHandler() { return new StaffHandler(this); }

// getters

Expand Down Expand Up @@ -240,6 +226,9 @@ public int getAccordInRowCount() {
return Math.max(result, 1);
}

final private int dx() { return Settings.inst().getStepWidth(); }
final private int dy() { return Settings.inst().getStepHeight(); }

// field getters/setters

public StaffConfig getConfig() {
Expand All @@ -249,8 +238,6 @@ public MainPanel getParentSheet() { // ???
return this.blockPanel;
}
public Playback getPlayback() { return this.playback; }
@Override
public MainPanel getModelParent() { return getParentSheet(); }
public int getFocusedIndex() {
return this.focusedIndex;
}
Expand Down Expand Up @@ -319,7 +306,7 @@ public Nota putAt(Fraction desiredPos, INota nota)
if (!wasAccordLength.equals(chord.getFraction())) {
// putting filler in case when chord length became smaller to preserve timing
Fraction dl = wasAccordLength.subtract(chord.getFraction());
this.add(new Chord(this), i + 1).addNewNota(0, 0).setLength(dl);
this.addNewAccord(i + 1).addNewNota(0, 0).setLength(dl);
}
return newNota;
} else if (curPos.compareTo(desiredPos) > 0) {
Expand All @@ -335,7 +322,7 @@ public Nota putAt(Fraction desiredPos, INota nota)

chord.addNewNota(0, 0).setLength(onset);

Chord newChord = this.add(new Chord(this), i);
Chord newChord = this.addNewAccord(i);
Nota newNota = newChord.addNewNota(nota);
if (newNota.getLength().compareTo(offset) > 0) {
// TODO: maybe if last chord in org.sheet_midusic.staff then no need
Expand All @@ -344,7 +331,7 @@ public Nota putAt(Fraction desiredPos, INota nota)
} else if (newNota.getLength().compareTo(offset) < 0) {
// TODO: maybe if last chord in org.sheet_midusic.staff then no need
// put an empty nota after and set it's length(onset - newNota.getLength())
this.add(new Chord(this), i + 1).addNewNota(0, 0).setLength(offset.subtract(newNota.getLength()));
this.addNewAccord(i + 1).addNewNota(0, 0).setLength(offset.subtract(newNota.getLength()));
}

return newNota;
Expand All @@ -357,12 +344,12 @@ public Nota putAt(Fraction desiredPos, INota nota)

Fraction rest = desiredPos.subtract(curPos);
if (!rest.equals(new Fraction(0))) {
this.add(new Chord(this), getChordList().size()).addNewNota(0,0).setLength(rest);
this.addNewAccord().addNewNota(0,0).setLength(rest);
}

// TODO: we don't handle here pause prefix! (i.e when desired start is more than end) !!!
// if not returned already
return this.add(new Chord(this), getChordList().size()).addNewNota(nota);
return this.addNewAccord().addNewNota(nota);
}

public static class TactMeasurer {
Expand Down
61 changes: 28 additions & 33 deletions src/org/sheet_midusic/staff/StaffHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import org.klesun_model.ContextAction;
import org.klesun_model.Explain;
import org.sheet_midusic.staff.staff_config.StaffConfig;
import org.sheet_midusic.staff.staff_panel.StaffComponent;
import org.sheet_midusic.stuff.Midi.DeviceEbun;
import org.sheet_midusic.staff.chord.Chord;
import org.sheet_midusic.staff.chord.nota.Nota;
import org.sheet_midusic.stuff.OverridingDefaultClasses.TruMap;
import org.sheet_midusic.stuff.tools.FileProcessor;

import java.util.*;
import java.util.function.Consumer;
Expand All @@ -19,57 +19,52 @@

public class StaffHandler extends AbstractHandler {

public StaffHandler(Staff context) { super(context); }
public Staff getContext() {
return Staff.class.cast(super.getContext());
public StaffHandler(StaffComponent context) { super(context); }
public StaffComponent getContext() {
return (StaffComponent)super.getContext();
}

private static TruMap<Combo, ContextAction<Staff>> actionMap = new TruMap<>();
private static TruMap<Combo, ContextAction<StaffComponent>> actionMap = new TruMap<>();
static {
JFileChooser pngChooser = new JFileChooser();
pngChooser.setFileFilter(new FileNameExtensionFilter("PNG images", "png"));

String navigation = "navigation";

actionMap
// File
.p(new Combo(ctrl, k.VK_S), mkFailableAction(FileProcessor::saveMusicPanel).setCaption("Save midi.json"))
.p(new Combo(ctrl, k.VK_U), mkFailableAction(FileProcessor::saveMidi).setCaption("Save midi (alpha)"))
.p(new Combo(ctrl, k.VK_O), mkFailableAction(FileProcessor::openStaff).setCaption("Open"))
.p(new Combo(ctrl, k.VK_M), mkFailableAction(FileProcessor::openJMusic).setCaption("Open JMusic project"))
.p(new Combo(ctrl, k.VK_K), mkFailableAction(FileProcessor::openJMusic2).setCaption("Open JMusic project (with rounding)"))
.p(new Combo(ctrl, k.VK_E), mkFailableAction(FileProcessor::savePNG).setCaption("Export png"))

.p(new Combo(0, k.VK_ESCAPE), mkAction(s -> s.getConfig().getDialog().showMenuDialog(StaffConfig::syncSyntChannels))
// .p(new Combo(ctrl, k.VK_M), mkFailableAction(FileProcessor::openJMusic).setCaption("Open JMusic project"))
// .p(new Combo(ctrl, k.VK_K), mkFailableAction(FileProcessor::openJMusic2).setCaption("Open JMusic project (with rounding)"))

.p(new Combo(0, k.VK_ESCAPE), mkAction(p -> p.staff.getConfig().getDialog().showMenuDialog(StaffConfig::syncSyntChannels))
.setCaption("Settings").setPostfix(navigation))

// Navigation
.p(new Combo(0, k.VK_HOME), mkAction(s -> s.setFocusedIndex(-1)).setCaption("To Start").setPostfix(navigation))
.p(new Combo(0, k.VK_END), mkAction(s -> s.setFocusedIndex(s.getChordList().size() - 1)).setCaption("To End").setPostfix(navigation))
.p(new Combo(ctrl, k.VK_LEFT), mkAction(s -> s.moveFocusTact(-1)).setCaption("Left Tact").setPostfix("Navigation"))
.p(new Combo(ctrl, k.VK_RIGHT), mkAction(s -> s.moveFocusTact(1)).setCaption("Right Tact").setPostfix("Navigation"))
.p(new Combo(0, k.VK_LEFT), mkFailableAction(s -> s.moveFocusWithPlayback(-1)).setCaption("Left").setPostfix(navigation))
.p(new Combo(0, k.VK_RIGHT), mkFailableAction(s -> s.moveFocusWithPlayback(1)).setCaption("Right").setPostfix(navigation))
.p(new Combo(0, k.VK_UP), mkFailableAction(s -> s.moveFocusRow(-1)).setCaption("Up").setPostfix(navigation))
.p(new Combo(0, k.VK_DOWN), mkFailableAction(s -> s.moveFocusRow(1)).setCaption("Down").setPostfix(navigation))
.p(new Combo(0, k.VK_HOME), mkAction(p -> p.staff.setFocusedIndex(-1)).setCaption("To Start").setPostfix(navigation))
.p(new Combo(0, k.VK_END), mkAction(p -> p.staff.setFocusedIndex(p.staff.getChordList().size() - 1)).setCaption("To End").setPostfix(navigation))
.p(new Combo(ctrl, k.VK_LEFT), mkAction(p -> p.staff.moveFocusTact(-1)).setCaption("Left Tact").setPostfix("Navigation"))
.p(new Combo(ctrl, k.VK_RIGHT), mkAction(p -> p.staff.moveFocusTact(1)).setCaption("Right Tact").setPostfix("Navigation"))
.p(new Combo(0, k.VK_LEFT), mkFailableAction(s -> s.staff.moveFocusWithPlayback(-1)).setCaption("Left").setPostfix(navigation))
.p(new Combo(0, k.VK_RIGHT), mkFailableAction(s -> s.staff.moveFocusWithPlayback(1)).setCaption("Right").setPostfix(navigation))
.p(new Combo(0, k.VK_UP), mkFailableAction(s -> s.staff.moveFocusRow(-1)).setCaption("Up").setPostfix(navigation))
.p(new Combo(0, k.VK_DOWN), mkFailableAction(s -> s.staff.moveFocusRow(1)).setCaption("Down").setPostfix(navigation))

// TODO: move it to StaffConfig
.p(new Combo(ctrl, k.VK_D), mkFailableAction(s -> DeviceEbun.changeOutDevice(s.getConfig()))
.p(new Combo(ctrl, k.VK_D), mkFailableAction(s -> DeviceEbun.changeOutDevice(s.staff.getConfig()))
.setCaption("Change Playback Device"))
.p(new Combo(ctrl, k.VK_0), mkAction(s -> s.mode = Staff.aMode.passive).setCaption("Disable Input From MIDI Device"))
.p(new Combo(ctrl, k.VK_9), mkAction(s -> s.mode = Staff.aMode.insert).setCaption("Enable Input From MIDI Device"))
.p(new Combo(ctrl, k.VK_0), mkAction(s -> s.staff.mode = Staff.aMode.passive).setCaption("Disable Input From MIDI Device"))
.p(new Combo(ctrl, k.VK_9), mkAction(s -> s.staff.mode = Staff.aMode.insert).setCaption("Enable Input From MIDI Device"))

/** @legacy */
.p(new Combo(ctrl, k.VK_W), mkAction(StaffHandler::updateDeprecatedPauses).setCaption("Convert Deprecated Pauses"))
// .p(new Combo(ctrl, k.VK_W), mkAction(StaffHandler::updateDeprecatedPauses).setCaption("Convert Deprecated Pauses"))
;

// MIDI-key press
for (Map.Entry<Combo, Integer> entry: Combo.getComboTuneMap().entrySet()) {
ContextAction<Staff> action = new ContextAction<>();
ContextAction<StaffComponent> action = new ContextAction<>();
actionMap.p(entry.getKey(), action
.setRedo(s -> {
if (s.mode != Staff.aMode.passive) {
s.addNewAccordWithPlayback().addNewNota(entry.getValue(), s.getSettings().getDefaultChannel());
if (s.staff.mode != Staff.aMode.passive) {
s.staff.addNewAccordWithPlayback().addNewNota(entry.getValue(), s.getSettings().getDefaultChannel());
return new Explain(true);
} else {
return new Explain("Cant do, passive mode is on!");
Expand Down Expand Up @@ -101,13 +96,13 @@ private static void updateDeprecatedPauses(Staff staff) {
}
}

private static ContextAction<Staff> mkAction(Consumer<Staff> lambda) {
ContextAction<Staff> action = new ContextAction<>();
private static ContextAction<StaffComponent> mkAction(Consumer<StaffComponent> lambda) {
ContextAction<StaffComponent> action = new ContextAction<>();
return action.setRedo(lambda);
}

private static ContextAction<Staff> mkFailableAction(Function<Staff, Explain> lambda) {
ContextAction<Staff> action = new ContextAction<>();
private static ContextAction<StaffComponent> mkFailableAction(Function<StaffComponent, Explain> lambda) {
ContextAction<StaffComponent> action = new ContextAction<>();
return action.setRedo(lambda);
}
}
Loading

0 comments on commit eb84d64

Please sign in to comment.