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

Update the piano #497

Merged
merged 3 commits into from
Oct 14, 2020
Merged
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
2 changes: 2 additions & 0 deletions editor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ add_library(sfizz_editor STATIC EXCLUDE_FROM_ALL
src/editor/EditorController.h
src/editor/GUIComponents.h
src/editor/GUIComponents.cpp
src/editor/GUIPiano.h
src/editor/GUIPiano.cpp
src/editor/NativeHelpers.h
src/editor/NativeHelpers.cpp
src/editor/layout/main.hpp
Expand Down
12 changes: 6 additions & 6 deletions editor/layout/main.fl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ widget_class mainView {open
}
Fl_Box sfzFileLabel_ {
label {DefaultInstrument.sfz}
comment {tag=kTagLoadSfzFile} selected
comment {tag=kTagLoadSfzFile}
xywh {195 11 250 31} labelsize 20 align 20
class ClickableLabel
}
Expand Down Expand Up @@ -138,7 +138,7 @@ widget_class mainView {open
}
}
Fl_Group {subPanels_[kPanelGeneral]} {
xywh {5 110 791 285} hide
xywh {5 110 791 285}
class LogicalGroup
} {
Fl_Group {} {open
Expand Down Expand Up @@ -212,8 +212,8 @@ widget_class mainView {open
}
}
}
Fl_Group {subPanels_[kPanelSettings]} {
xywh {5 109 790 286}
Fl_Group {subPanels_[kPanelSettings]} {open
xywh {5 109 790 286} hide
class LogicalGroup
} {
Fl_Group {} {
Expand Down Expand Up @@ -305,8 +305,8 @@ widget_class mainView {open
}
}
}
Fl_Box {} {
xywh {5 400 790 70}
Fl_Box piano_ {selected
xywh {5 400 790 70} labelsize 12
class Piano
}
}
24 changes: 23 additions & 1 deletion editor/src/editor/Editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "EditorController.h"
#include "EditIds.h"
#include "GUIComponents.h"
#include "GUIPiano.h"
#include "NativeHelpers.h"
#include <absl/strings/string_view.h>
#include <absl/strings/match.h>
Expand Down Expand Up @@ -96,6 +97,8 @@ struct Editor::Impl : EditorController::Receiver, IControlListener {

SActionMenu* fileOperationsMenu_ = nullptr;

SPiano* piano_ = nullptr;

void uiReceiveValue(EditId id, const EditValue& v) override;

void createFrameContents();
Expand Down Expand Up @@ -554,8 +557,10 @@ void Editor::Impl::createFrameContents()
auto createNextFileButton = [&createGlyphButton](const CRect& bounds, int tag, const char*, CHoriTxtAlign, int fontsize) {
return createGlyphButton(u8"\ue0da", bounds, tag, fontsize);
};
auto createPiano = [](const CRect& bounds, int, const char*, CHoriTxtAlign, int) {
auto createPiano = [](const CRect& bounds, int, const char*, CHoriTxtAlign, int fontsize) {
SPiano* piano = new SPiano(bounds);
auto font = owned(new CFontDesc("Roboto", fontsize));
piano->setFont(font);
return piano;
};
auto createChevronDropDown = [this, &theme](const CRect& bounds, int, const char*, CHoriTxtAlign, int fontsize) {
Expand Down Expand Up @@ -693,6 +698,23 @@ void Editor::Impl::createFrameContents()
menu->addEntry("Edit file", kTagEditSfzFile);
}

if (SPiano* piano = piano_) {
piano->onKeyPressed = [this](unsigned key, float vel) {
uint8_t msg[3];
msg[0] = 0x90;
msg[1] = static_cast<uint8_t>(key);
msg[2] = static_cast<uint8_t>(std::max(1, static_cast<int>(vel * 127)));
ctrl_->uiSendMIDI(msg, sizeof(msg));
};
piano->onKeyReleased = [this](unsigned key, float vel) {
uint8_t msg[3];
msg[0] = 0x80;
msg[1] = static_cast<uint8_t>(key);
msg[2] = static_cast<uint8_t>(vel * 127);
ctrl_->uiSendMIDI(msg, sizeof(msg));
};
}

///
CViewContainer* panel;
activePanel_ = 0;
Expand Down
195 changes: 0 additions & 195 deletions editor/src/editor/GUIComponents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,201 +154,6 @@ bool SFileDropTarget::isFileDrop(IDataPackage* package)
package->getDataType(0) == IDataPackage::kFilePath;
}

///
SPiano::SPiano(const CRect& bounds)
: CView(bounds), font_(kNormalFont)
{
}

void SPiano::setFont(CFontRef font)
{
font_ = font;
invalid();
}

void SPiano::clearKeyRanges()
{
keyInRange_.reset();
}

void SPiano::addKeyRange(int start, int end)
{
start = std::min(127, std::max(0, start));
end = std::min(127, std::max(0, end));

for (int x = start; x <= end; ++x)
keyInRange_.set(x);
}

CCoord SPiano::getKeyWidth()
{
return 6.0;
}

CCoord SPiano::getKeySwitchesHeight()
{
return 20.0;
}

CCoord SPiano::getKeyRangesHeight()
{
return 11.0;
}

CCoord SPiano::getKeysHeight() const
{
return getHeight() -
(getKeySwitchesHeight() + getKeyRangesHeight() + getOctavesHeight());
}

CCoord SPiano::getOctavesHeight() const
{
return font_->getSize();
}

void SPiano::getZoneDimensions(
CRect* pKeySwitches,
CRect* pKeyboard,
CRect* pKeyRanges,
CRect* pOctaves)
{
CRect bounds = getViewSize();

CRect keySwitches(bounds);
keySwitches.setHeight(getKeySwitchesHeight());

CRect keyboard(bounds);
keyboard.top = keySwitches.bottom;
keyboard.setHeight(getKeysHeight());

CRect keyRanges(bounds);
keyRanges.top = keyboard.bottom;
keyRanges.setHeight(getKeyRangesHeight());

CRect octaves(bounds);
octaves.top = keyRanges.bottom;
octaves.setHeight(getOctavesHeight());

// apply some paddings
keySwitches.extend(-2.0, -2.0);
keyboard.extend(-2.0, -2.0);
keyRanges.extend(-2.0, -4.0);
octaves.extend(-2.0, -2.0);

// offsets for centered keyboard
CCoord keyWidth = getKeyWidth();
CCoord offset = std::round((keyboard.getWidth() - (128.0 * keyWidth)) * 0.5);
if (offset > 0) {
keySwitches.extend(-offset, 0.0);
keyboard.extend(-offset, 0.0);
keyRanges.extend(-offset, 0.0);
octaves.extend(-offset, 0.0);
}

//
if (pKeySwitches)
*pKeySwitches = keySwitches;
if (pKeyboard)
*pKeyboard = keyboard;
if (pKeyRanges)
*pKeyRanges = keyRanges;
if (pOctaves)
*pOctaves = octaves;
}

void SPiano::draw(CDrawContext* dc)
{
CRect bounds = getViewSize();

dc->setDrawMode(kAntiAliasing);

SharedPointer<CGraphicsPath> path;

path = owned(dc->createGraphicsPath());
path->addRoundRect(bounds, 5.0);
dc->setFillColor(CColor(0xca, 0xca, 0xca));
dc->drawGraphicsPath(path, CDrawContext::kPathFilled);

//
CRect rectKeySwitches;
CRect rectKeyboard;
CRect rectKeyRanges;
CRect rectOctaves;
getZoneDimensions(&rectKeySwitches, &rectKeyboard, &rectKeyRanges, &rectOctaves);

//
path = owned(dc->createGraphicsPath());
path->addRoundRect(rectKeyboard, 1.0);
dc->setFillColor(CColor(0xff, 0xff, 0xff));
dc->drawGraphicsPath(path, CDrawContext::kPathFilled);

CCoord keyWidth = getKeyWidth();
for (int key = 0; key < 128; ++key) {
CCoord keyX = rectKeyboard.left + key * keyWidth;
int key12 = key % 12;
if (key12 == 1 || key12 == 3 ||
key12 == 6 || key12 == 8 || key12 == 10)
{
CRect blackRect(keyX, rectKeyboard.top + 2, keyX + keyWidth, rectKeyboard.bottom - 2);
path = owned(dc->createGraphicsPath());
path->addRoundRect(blackRect, 1.0);
dc->setFillColor(CColor(0x02, 0x02, 0x02));
dc->drawGraphicsPath(path, CDrawContext::kPathFilled);
}
if (key != 0 && key12 == 0) {
dc->setLineWidth(1.5);
dc->setFrameColor(CColor(0x63, 0x63, 0x63));
dc->drawLine(CPoint(keyX, rectKeyboard.top), CPoint(keyX, rectKeyboard.bottom));
}
if (key12 == 5) {
CCoord pad = rectKeyboard.getHeight() * 0.4;
dc->setLineWidth(1.0);
dc->setFrameColor(CColor(0x63, 0x63, 0x63));
dc->drawLine(CPoint(keyX, rectKeyboard.top + pad), CPoint(keyX, rectKeyboard.bottom - pad));
}
}

//

for (int rangeStart = 0; rangeStart < 128;)
{
if (!keyInRange_[rangeStart]) {
++rangeStart;
}
else {
int rangeEnd = rangeStart;
while (rangeEnd + 1 < 128 && keyInRange_[rangeEnd + 1])
++rangeEnd;

CCoord rangeStartX = rectKeyRanges.left + rangeStart * keyWidth;
CCoord rangeEndX = rectKeyRanges.left + (rangeEnd + 1.0) * keyWidth;
CRect rectRange(rangeStartX, rectKeyRanges.top, rangeEndX, rectKeyRanges.bottom);

path = owned(dc->createGraphicsPath());
path->addRoundRect(rectRange, 2.0);
dc->setFillColor(CColor(0x0f, 0x0f, 0x0f));
dc->drawGraphicsPath(path, CDrawContext::kPathFilled);

rangeStart = rangeEnd + 1;
}
}

//

for (int key = 0; key < 128; ++key) {
CCoord keyX = rectOctaves.left + key * keyWidth;
int key12 = key % 12;
if (key12 == 0) {
CRect textRect(keyX, rectOctaves.top, keyX + 12 * keyWidth, rectOctaves.bottom);
dc->setFont(font_);
dc->setFontColor(CColor(0x63, 0x63, 0x63));
dc->drawString(std::to_string(key / 12 - 1).c_str(), textRect, kLeftText);
}
}

//
}

///
SValueMenu::SValueMenu(const CRect& bounds, IControlListener* listener, int32_t tag)
: CParamDisplay(bounds), menuListener_(owned(new MenuListener(*this)))
Expand Down
30 changes: 0 additions & 30 deletions editor/src/editor/GUIComponents.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,36 +82,6 @@ class SFileDropTarget : public IDropTarget,
FileDropFunction dropFunction_;
};

///
class SPiano : public CView {
public:
explicit SPiano(const CRect& bounds);
CFontRef getFont() const { return font_; }
void setFont(CFontRef font);

void clearKeyRanges();
void addKeyRange(int start, int end);

protected:
static CCoord getKeyWidth();
static CCoord getKeySwitchesHeight();
static CCoord getKeyRangesHeight();
CCoord getKeysHeight() const;
CCoord getOctavesHeight() const;

void getZoneDimensions(
CRect* pKeySwitches,
CRect* pKeyboard,
CRect* pKeyRanges,
CRect* pOctaves);

void draw(CDrawContext* dc) override;

private:
SharedPointer<CFontDesc> font_;
std::bitset<128> keyInRange_;
};

///
class SValueMenu : public CParamDisplay {
public:
Expand Down
Loading