/* This file is part of Machina. * Copyright 2007-2011 David Robillard * * Machina is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Machina is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Machina. If not, see . */ #include "machina/Controller.hpp" #include "machina/URIs.hpp" #include "machina/types.hpp" #include "client/ClientModel.hpp" #include "MachinaCanvas.hpp" #include "MachinaGUI.hpp" #include "NodePropertiesWindow.hpp" #include "NodeView.hpp" using namespace std; using Machina::URIs; NodeView::NodeView(Gtk::Window* window, Ganv::Canvas& canvas, SharedPtr node, double x, double y) : Ganv::Circle(canvas, "", x, y, 20, false) , _window(window) , _node(node) , _default_border_color(get_border_color()) , _default_fill_color(get_fill_color()) { signal_event().connect( sigc::mem_fun(this, &NodeView::on_event)); node->signal_property.connect( sigc::mem_fun(this, &NodeView::on_property)); } bool NodeView::on_double_click(GdkEventButton*) { NodePropertiesWindow::present(_window, _node); return true; } bool NodeView::node_is(Raul::Forge& forge, Machina::URIInt key) { const Raul::Atom& value = _node->get(key); return value.type() == forge.Bool && value.get_bool(); } bool NodeView::on_event(GdkEvent* event) { MachinaCanvas* canvas = dynamic_cast(this->canvas()); Raul::Forge& forge = canvas->app()->forge(); if (event->type == GDK_BUTTON_PRESS) { if (event->button.state & GDK_CONTROL_MASK) { if (event->button.button == 1) { canvas->app()->controller()->set_property( _node->id(), URIs::instance().machina_initial, forge.make(!node_is(forge, URIs::instance().machina_initial))); return true; } else if (event->button.button == 3) { canvas->app()->controller()->set_property( _node->id(), URIs::instance().machina_selector, forge.make(!node_is(forge, URIs::instance().machina_selector))); return true; } } else { _signal_clicked.emit(&event->button); } } else if (event->type == GDK_2BUTTON_PRESS) { return on_double_click(&event->button); } return false; } static std::string midi_note_name(uint8_t num) { static const char* notes[] = { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" }; const uint8_t octave = num / 12; num -= octave * 12; static char str[8]; snprintf(str, sizeof(str), "%s%d", notes[num], octave); return str; } void NodeView::show_label(bool show) { if (show && _enter_action) { Raul::Atom note_number = _enter_action->get( URIs::instance().machina_note_number); if (note_number.is_valid()) { set_label(midi_note_name(note_number.get_int32()).c_str()); return; } } set_label(""); } void NodeView::on_property(Machina::URIInt key, const Raul::Atom& value) { static const uint32_t active_color = 0x408040FF; static const uint32_t active_border_color = 0x00FF00FF; if (key == URIs::instance().machina_selector) { //_node.property_dash() = value.get_bool() ? selector_dash() : 0; std::cerr << "FIXME: dash" << std::endl; } else if (key == URIs::instance().machina_initial) { set_border_width(value.get_bool() ? 4.0 : 1.0); } else if (key == URIs::instance().machina_active) { if (value.get_bool()) { if (get_fill_color() != active_color) { set_fill_color(active_color); set_border_color(active_border_color); } } else if (get_fill_color() == active_color) { set_default_colors(); } } else if (key == URIs::instance().machina_enter_action) { const uint64_t action_id = value.get_int32(); MachinaCanvas* canvas = dynamic_cast(this->canvas()); _enter_action_connection.disconnect(); _enter_action = canvas->app()->client_model()->find(action_id); if (_enter_action) { _enter_action_connection = _enter_action->signal_property.connect( sigc::mem_fun(this, &NodeView::on_action_property)); for (auto i : _enter_action->properties()) { on_action_property(i.first, i.second); } } } else { cout << "Unknown property " << key << endl; } } void NodeView::on_action_property(Machina::URIInt key, const Raul::Atom& value) { if (key == URIs::instance().machina_note_number) { show_label(true); } } void NodeView::set_default_colors() { set_fill_color(_default_fill_color); set_border_color(_default_border_color); }