Skip to content

Commit

Permalink
use widget for stats
Browse files Browse the repository at this point in the history
  • Loading branch information
sksum committed Aug 25, 2020
1 parent 978639f commit b01d78a
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 82 deletions.
83 changes: 5 additions & 78 deletions js/activity.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ function Activity() {
"widgets/musickeyboard",
"widgets/timbre",
"widgets/oscilloscope",
"widgets/statistics",
"activity/lilypond",
"activity/abc",
"activity/mxml",
Expand Down Expand Up @@ -1488,33 +1489,6 @@ function Activity() {
}
};

/*
* @param chartBitmap bitmap of analysis charts
* @param ctx canvas
* Renders close icon and functionality to
* stop analytics of the MB project
*/
this.closeAnalytics = function(chartBitmap, ctx) {
blocks.activeBlock = null;
button.x = canvas.width / (2 * turtleBlocksScale) + 300 / Math.sqrt(2);
button.y = 200.0;
this.closeButton = _makeButton(
CANCELBUTTON,
_("Close"),
button.x,
button.y,
55,
0
);
this.closeButton.onclick = (event) => {
this.closeButton.style.visibility = "hidden";
stage.removeChild(chartBitmap);
blocks.showBlocks();
update = true;
ctx.clearRect(0, 0, 600, 600);
};
};

/*
* @param canvas {compares existing canvas with a new blank canvas}
* @return {boolean} {if canvas is blank }
Expand All @@ -1527,56 +1501,6 @@ function Activity() {
return canvas.toDataURL() === blank.toDataURL();
}

/*
* Renders and carries out analysis
* of the MB project
*/
closeAnalytics = this.closeAnalytics;
let th = this;
doAnalytics = function() {
toolbar.closeAuxToolbar(_showHideAuxMenu);
blocks.activeBlock = null;
myChart = docById("myChart");

if (_isCanvasBlank(myChart) === false) {
return;
}

let ctx = myChart.getContext("2d");
loading = true;
document.body.style.cursor = "wait";

let myRadarChart = null;
let scores = analyzeProject(blocks);
let projectStats = runAnalytics(logo)
let data = scoreToChartData(scores);
let Analytics = this;
Analytics.close = th.closeAnalytics;

__callback = function() {
imageData = myRadarChart.toBase64Image();
img = new Image();
img.onload = function() {
chartBitmap = new createjs.Bitmap(img);
stage.addChild(chartBitmap);
chartBitmap.x = canvas.width / (2 * turtleBlocksScale) - 300;
chartBitmap.y = 200;
chartBitmap.scaleX = chartBitmap.scaleY = chartBitmap.scale =
600 / chartBitmap.image.width;
blocks.hideBlocks();
logo.showBlocksAfterRun = false;
update = true;
document.body.style.cursor = "default";
loading = false;
Analytics.close(chartBitmap, ctx);
};
img.src = imageData;
};

options = getChartOptions(__callback);
myRadarChart = new Chart(ctx).Radar(data, options);
};

/*
* Increases block size
*/
Expand Down Expand Up @@ -5055,7 +4979,10 @@ function Activity() {
toolbar.renderRunSlowlyIcon(that._doSlowButton);
toolbar.renderRunStepIcon(_doStepButton);
toolbar.renderAdvancedIcons(
doAnalytics,
() => {
if (!logo.statsWindow) logo.statsWindow = new StatsWindow();
logo.statsWindow.init()
},
doOpenPlugin,
deletePlugin,
setScroller,
Expand Down
3 changes: 2 additions & 1 deletion js/logo.js
Original file line number Diff line number Diff line change
Expand Up @@ -1801,7 +1801,8 @@ class Logo {
if (logo.runningLilypond) {
if (logo.collectingStats){
console.debug("stats collection completed")
getStatsFromNotation(logo);
logo.projectStats = getStatsFromNotation(logo);
logo.statsWindow.jsonObject.innerHTML = JSON.stringify(projectStats);
} else {
console.debug("saving lilypond output:");
save.afterSaveLilypond();
Expand Down
5 changes: 2 additions & 3 deletions js/rubrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -733,8 +733,7 @@ let getStatsFromNotation = (logo) => {
projectStats = {};
// since we use the lilypond output to generate stats , please make sure to change these rules if
// we ever change the lilypond notation structure.
notation = logo.notation;

let notation = logo.notation;
projectStats["duples"] = 0;
projectStats["triplets"] = 0;
projectStats["quintuplets"] = 0;
Expand All @@ -747,7 +746,7 @@ let getStatsFromNotation = (logo) => {

for (tur in notation.notationStaging){
for (it in notation.notationStaging[tur]) {
let item = notation.notationStaging[tur];
let item = notation.notationStaging[tur][it];

if (typeof item == "object" && item[0].length){
for (note of item[0]) {
Expand Down
86 changes: 86 additions & 0 deletions js/widgets/statistics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright (c) 2016-20 Walter Bender
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the The GNU Affero General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// You should have received a copy of the GNU Affero General Public
// License along with this library; if not, write to the Free Software
// Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA

// This widget makes displays the status of selected parameters and
// notes as they are being played.

function StatsWindow() {
const BUTTONSIZE = 53;
const ICONSIZE = 32;

this.init = function(logo) {
this._logo = logo;
this.isOpen = true;

var w = window.innerWidth;
this._cellScale = w / 1200;
var iconSize = ICONSIZE * this._cellScale;

var widgetWindow = window.widgetWindows.windowFor(
this,
"stats",
"stats"
);
this.widgetWindow = widgetWindow;
widgetWindow.clear();
widgetWindow.show();

var that = this;

widgetWindow.onclose = function() {
that.isOpen = false;
blocks.showBlocks();
this.destroy();
};
this.doAnalytics();

widgetWindow.sendToCenter();
};

/*
* Renders and carries out analysis
* of the MB project
*/
this.doAnalytics = function() {
toolbar.closeAuxToolbar(_showHideAuxMenu);
blocks.activeBlock = null;
myChart = docById("myChart");

// if (_isCanvasBlank(myChart) === false) {
// return;
// }

let ctx = myChart.getContext("2d");
loading = true;
document.body.style.cursor = "wait";

let myRadarChart = null;
let scores = analyzeProject(blocks);
runAnalytics(logo)
let data = scoreToChartData(scores);
__callback = () => {
imageData = myRadarChart.toBase64Image();
img = new Image();
img.src = imageData;
this.widgetWindow.getWidgetBody().appendChild(img)
blocks.hideBlocks();
logo.showBlocksAfterRun = false;
document.body.style.cursor = "default";
};
options = getChartOptions(__callback);
myRadarChart = new Chart(ctx).Radar(data, options);

this.jsonObject = document.createElement('p');
this.widgetWindow.getWidgetBody().appendChild(this.jsonObject)

};

}

0 comments on commit b01d78a

Please sign in to comment.