Skip to content

Commit

Permalink
Update the piano
Browse files Browse the repository at this point in the history
  • Loading branch information
jpcima committed Oct 14, 2020
1 parent 5cd6a51 commit f967b12
Show file tree
Hide file tree
Showing 8 changed files with 315 additions and 234 deletions.
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 {} {selected
xywh {5 400 790 70} labelsize 12
class Piano
}
}
5 changes: 4 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 @@ -554,8 +555,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
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

0 comments on commit f967b12

Please sign in to comment.