Commit 749a2535 authored by nerrons's avatar nerrons
Browse files

pd_canvas.js: Add K12 Mode related stuff for initializing canvas

parent e562855b
Pipeline #1612 failed with stage
in 0 seconds
......@@ -10,6 +10,8 @@ pdgui.skin.apply(window);
//var name = pdgui.last_loaded();
var l = pdgui.get_local_string;
var k12_mode = pdgui.get_k12_mode();
var k12_offset = pdgui.get_k12_offset();
function nw_window_focus_callback(name) {
pdgui.set_focused_patchwin(name);
......@@ -224,7 +226,7 @@ var canvas_events = (function() {
//pdgui.post("x: " + evt.pageX + " y: " + evt.pageY +
// " modifier: " + (evt.shiftKey + (pdgui.cmd_or_ctrl_key(evt) << 1)));
pdgui.pdsend(name, "motion",
(evt.pageX + svg_view.x),
(evt.pageX + svg_view.x + k12_offset),
(evt.pageY + svg_view.y),
(evt.shiftKey + (pdgui.cmd_or_ctrl_key(evt) << 1))
);
......@@ -245,7 +247,7 @@ var canvas_events = (function() {
// get id ("x123456etcgobj" without the "x" or "gobj")
target_id = (draggable_label ? "_l" : "_s") +
evt.target.parentNode.parentNode.id.slice(0,-4).slice(1);
last_draggable_x = evt.pageX + svg_view.x;
last_draggable_x = evt.pageX + svg_view.x + k12_offset;
last_draggable_y = evt.pageY + svg_view.y;
// Nasty-- we have to forward magic values from g_canvas.h
......@@ -271,7 +273,7 @@ var canvas_events = (function() {
!!draggable_label, false);
pdgui.pdsend(target_id, "_click", resize_type,
(evt.pageX + svg_view.x),
(evt.pageX + svg_view.x + k12_offset),
(evt.pageY + svg_view.y));
canvas_events.iemgui_label_drag();
return;
......@@ -302,7 +304,7 @@ var canvas_events = (function() {
mod = (evt.shiftKey + (pdgui.cmd_or_ctrl_key(evt) << 1));
}
pdgui.pdsend(name, "mouse",
(evt.pageX + svg_view.x),
(evt.pageX + svg_view.x + k12_offset),
(evt.pageY + svg_view.y),
b, mod
);
......@@ -314,7 +316,7 @@ var canvas_events = (function() {
// evt.pageX + " y: " + evt.pageY +
// " button: " + (evt.button + 1));
pdgui.pdsend(name, "mouseup",
(evt.pageX + svg_view.x),
(evt.pageX + svg_view.x + k12_offset),
(evt.pageY + svg_view.y),
(evt.button + 1)
);
......@@ -481,7 +483,7 @@ var canvas_events = (function() {
// handle (which will eventually get erased by Pd anyway).
// Anyhow, this is all very bad, but it works so it's
// at least not the worst of all possible worlds.
var dx = (evt.pageX + svg_view.x) - last_draggable_x,
var dx = (evt.pageX + svg_view.x + k12_offset) - last_draggable_x,
dy = (evt.pageY + svg_view.y) - last_draggable_y,
handle_elem = document.querySelector(
draggable_label ?
......@@ -494,11 +496,11 @@ var canvas_events = (function() {
getElementsByClassName("gop_drag_handle").length ?
true : false;
last_draggable_x = evt.pageX + svg_view.x;
last_draggable_x = evt.pageX + svg_view.x + k12_offset;
last_draggable_y = evt.pageY + svg_view.y;
pdgui.pdsend(target_id, "_motion",
(evt.pageX + svg_view.x),
(evt.pageX + svg_view.x + k12_offset),
(evt.pageY + svg_view.y));
},
iemgui_label_mouseup: function(evt) {
......@@ -887,6 +889,122 @@ var canvas_events = (function() {
console.log("tried to save something");
}, false
);
// k12 related stuff:
// show k12-frame; push svg to the right; add buttons to the frame
if (k12_mode === 1) {
var k12frame = document.getElementById("k12-frame");
// add edit and control/sound buttons
document.getElementById("k12-edit-button").onclick = function () {
// first change the button in case pd responds later than the "editmode" check
if (document.getElementById("patchsvg").classList.contains("editmode")) {
edit_button.firstElementChild.src = "./extra/K12/icons-large/perform.png";
edit_button.title = "Start playing the patch you created!"
} else {
edit_button.firstElementChild.src = "./extra/K12/icons-large/edit.png";
edit_button.title = "Start building your own patch!"
}
pdgui.pdsend(name, "editmode 0");
}
document.getElementById("k12-control-button").onclick = function () {
if (k12frame.classList.contains("sound-panel")) {
k12frame.classList.remove("sound-panel");
k12frame.classList.add("control-panel");
document.getElementById("k12-buttons-control").style.visibility = "visible";
document.getElementById("k12-buttons-sound").style.visibility = "hidden";
document.getElementById("k12-control-button").firstElementChild.src =
"./extra/K12/icons/control_on.png";
document.getElementById("k12-sound-button").firstElementChild.src =
"./extra/K12/icons/sound.png";
}
}
document.getElementById("k12-sound-button").onclick = function () {
if (k12frame.classList.contains("control-panel")) {
k12frame.classList.remove("control-panel");
k12frame.classList.add("sound-panel");
document.getElementById("k12-buttons-sound").style.visibility = "visible";
document.getElementById("k12-buttons-control").style.visibility = "hidden";
document.getElementById("k12-sound-button").firstElementChild.src =
"./extra/K12/icons/sound_on.png";
document.getElementById("k12-control-button").firstElementChild.src =
"./extra/K12/icons/control.png";
}
}
// populate the k12 frame with abstraction buttons in specified order.
var k12_category_list = {
"control": [ "wii", "arduino", "raspberrypi", "math", "logic" ],
"sound": [ "signal", "instr", "midi", "fx", "" ]
}
var k12_buttons_list = {
"wii": [
"connect", "buttons", "hit", "accelerometer", "speed_xry", "speed",
"nunchuk_buttons", "nunchuk_hit", "nunchuk_accelerometer", "nunchuk_stick"
],
"arduino": [ "connect", "digital_in", "digital_out", "analog_in", "analog_out" ],
"raspberrypi": [ "digital", "analog_in", "analog_out" ],
"math": [
"number", "netsend", "netreceive", "tag", "routebytag", "add", "subtract",
"multiply", "divide", "random", "average", "scale"
],
"logic": [ "compare", "mapper", "metronome", "counter", "hit", "sequencer" ],
"signal": [
"microphone", "netsend", "netreceive", "sampler", "player",
"sine", "saw", "square", "triangle", "envelope",
"noise", "pink", "add", "multiply"
],
"instr": [ "short1", "short2", "sustained1", "sustained2" ],
"midi": [ "synth", "note", "instrument", "control" ],
"fx": [ "pitchshift", "filter", "phaser", "multitap", "reverb" ],
"": [ "output", "preset" ]
}
// iterate through all categories; with in each category, use the json-defined order
var k12_control_buttons_div = document.getElementById("k12-buttons-control");
var k12_sound_buttons_div = document.getElementById("k12-buttons-sound");
Object.keys(k12_category_list).forEach(function (panel) {
k12_category_list[panel].forEach(function (category) {
// calculate the number of placeholders needed
var num_of_buttons = k12_buttons_list[category].length;
var num_of_lines = Math.ceil(num_of_buttons / 5);
var num_of_placeholders = num_of_lines * 5 - num_of_buttons;
// iterate through the buttons list and create buttons
k12_buttons_list[category].forEach(function (ab) {
var button = document.createElement("button");
var fullname = category === "" ? ab : category + "_" + ab; // used in filenames
button.type = "button";
button.className = "k12-button k12-" + panel;
button.onclick = function () {
pdgui.pdsend(name, "dirty 1");
pdgui.pdsend(name, "obj_abstraction K12/" + fullname, "0 20");
}
button.title = l("k12." + category + "." + ab + "_tt");
// set the icon for the button
var icon = document.createElement("img");
icon.className = "k12-button-icon";
icon.src = "./extra/K12/icons-large/" + fullname + ".png";
button.appendChild(icon);
if (panel === "control") {
k12_control_buttons_div.appendChild(button);
} else {
k12_sound_buttons_div.appendChild(button);
}
});
// after buttons from one category are added, add the placeholders
for (var i = 0; i < num_of_placeholders; i++) {
var button = document.createElement("button");
button.className = "k12-button k12-" + panel;
button.style.visibility = "hidden";
if (panel === "control") {
k12_control_buttons_div.appendChild(button);
} else {
k12_sound_buttons_div.appendChild(button);
}
}
});
});
k12frame.style.visibility = "visible";
document.getElementById("patchsvg").style.marginLeft = -k12_offset + "px";
}
// Whoa-- huge workaround! Right now we're getting
// the popup menu the way Pd Vanilla does it:
// 1) send a mouse(down) message to Pd
......@@ -1287,8 +1405,12 @@ function nw_create_patch_window_menus(gui, w, name) {
chooser.click();
}
});
if (pdgui.k12_mode == 1) {
minit(m.file.k12, { click: pdgui.menu_k12_open_demos });
if (k12_mode === 1) {
minit(m.file.k12, {
click: function () {
gui.Shell.showItemInFolder(pdgui.get_k12_demo_dir());
}
});
}
minit(m.file.save, {
enabled: true,
......@@ -1461,6 +1583,18 @@ function nw_create_patch_window_menus(gui, w, name) {
enabled: true,
click: function() {
update_live_box();
if (k12_mode) {
// same as the edit button in canvas_events.init:
// change button appearance first to avoid delays
var edit_button = document.getElementById("k12-edit-button");
if (document.getElementById("patchsvg").classList.contains("editmode")) {
edit_button.firstElementChild.src = "./extra/K12/icons-large/perform.png";
edit_button.title = "Start playing the patch you created!"
} else {
edit_button.firstElementChild.src = "./extra/K12/icons-large/edit.png";
edit_button.title = "Start building your own patch!"
}
}
pdgui.pdsend(name, "editmode 0");
}
});
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment