/* $Id$
-This file is part of the MSP Märklin suite
-Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa
+This file is part of R²C²
+Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
Distributed under the GPL
*/
#include <signal.h>
+#include <algorithm>
#include <cmath>
-#include <iostream>
-#include <GL/gl.h>
+#include <msp/fs/utils.h>
+#include <msp/gl/blend.h>
+#include <msp/gl/framebuffer.h>
+#include <msp/gl/matrix.h>
+#include <msp/gl/misc.h>
+#include <msp/gl/projection.h>
#include <msp/gl/rendermode.h>
#include <msp/gl/select.h>
+#include <msp/gl/tests.h>
#include <msp/gl/texture2d.h>
#include <msp/input/keys.h>
+#include <msp/io/print.h>
#include <msp/strings/codec.h>
#include <msp/strings/lexicalcast.h>
#include <msp/strings/utf8.h>
#include <msp/strings/utils.h>
#include <msp/time/units.h>
#include <msp/time/utils.h>
-#include "libmarklin/tracktype.h"
+#include "libr2c2/route.h"
+#include "libr2c2/tracktype.h"
+#include "3d/path.h"
#include "designer.h"
#include "input.h"
#include "manipulator.h"
#include "measure.h"
#include "selection.h"
+#include "svgexporter.h"
+#include "toolbar.h"
using namespace std;
-using namespace Marklin;
+using namespace R2C2;
using namespace Msp;
+Application::RegApp<Designer> Designer::reg;
+
Designer::Designer(int argc, char **argv):
- screen_w(1280),
- screen_h(960),
- base_mesh(0),
- input(0),
+ window(1280, 960),
+ ui_res("r2c2.res"),
+ root(ui_res, window),
+ base_object(0),
+ cur_route(0),
mode(SELECT),
- cam_yaw(M_PI/2),
- cam_pitch(-M_PI/4),
- cam_pos(0, -0.5, 0.5),
- shift(false),
- move_x(0),
- move_y(0),
- zoom(0),
- rotate(0),
- pitch(0)
+ manipulator(*this, root, selection),
+ measure(*this),
+ camera_ctl(*this, root, camera),
+ track_wrap(*this, selection)
{
- DataFile::load(catalogue, "tracks.dat");
+ window.set_title("Railway Designer");
+ window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Designer::exit), 0));
- cat_layout=new Layout(catalogue);
- cat_layout_3d=new Layout3D(*cat_layout);
+ selection.signal_changed.connect(sigc::mem_fun(this, &Designer::selection_changed));
+ manipulator.signal_status.connect(sigc::mem_fun(this, &Designer::manipulation_status));
+ manipulator.signal_done.connect(sigc::mem_fun(this, &Designer::manipulation_done));
+ measure.signal_changed.connect(sigc::mem_fun(this, &Designer::measure_changed));
+ measure.signal_done.connect(sigc::mem_fun(this, &Designer::measure_done));
- const map<unsigned, TrackType *> &ctracks=catalogue.get_tracks();
- unsigned n=0;
- for(map<unsigned, TrackType *>::const_iterator i=ctracks.begin(); i!=ctracks.end(); ++i, ++n)
- {
- Track *track=new Track(*i->second);
- track->set_position(Point((n%11)*0.1-0.5, 0.2-n/11*0.3, 0));
- track->set_rotation(M_PI/2);
- cat_layout->add_track(*track);
- }
+ // Setup catalogue and layout
+ DataFile::load(catalogue, "tracks.dat");
- manipulator=new Manipulator(*this);
- manipulator->signal_status.connect(sigc::mem_fun(this, &Designer::manipulation_status));
- manipulator->signal_done.connect(sigc::mem_fun(this, &Designer::manipulation_done));
+ cat_layout_3d = new Layout3D(catalogue.get_layout());
- layout=new Layout(catalogue);
- layout_3d=new Layout3D(*layout);
+ layout = new Layout(catalogue);
+ layout_3d = new Layout3D(*layout);
+
+ layout->signal_track_added.connect(sigc::mem_fun(this, &Designer::track_added));
+ layout->signal_track_removed.connect(sigc::mem_fun(this, &Designer::track_removed));
if(argc>1)
{
+ filename = argv[1];
DataFile::load(*layout, argv[1]);
- const list<Track3D *> <racks=layout_3d->get_tracks();
- for(list<Track3D *>::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
- {
- if((*i)->get_track().get_sensor_id())
- (*i)->set_color(GL::Color(1, 1, 0.5));
- else if((*i)->get_track().get_turnout_id())
- (*i)->set_color(GL::Color(0.5, 1, 1));
- else if((*i)->get_track().get_flex())
- (*i)->set_color(GL::Color(1, 0.5, 1));
- }
if(!layout->get_base().empty())
{
- base_mesh=new GL::Mesh;
- DataFile::load(*base_mesh, layout->get_base());
+ base_object = new GL::Object;
+ DataFile::load(*base_object, layout->get_base());
}
}
- selection=new Selection;
- manipulator->set_selection(selection);
+ // Setup OpenGL
+ GL::enable(GL_CULL_FACE);
- measure=new Measure(*this);
- measure->signal_changed.connect(sigc::mem_fun(this, &Designer::measure_changed));
- measure->signal_done.connect(sigc::mem_fun(this, &Designer::measure_done));
-}
+ pipeline = new GL::Pipeline(window.get_width(), window.get_height(), false);
+ pipeline->set_camera(&camera);
+ pipeline->add_renderable_for_pass(layout_3d->get_scene(), 0);
+ if(base_object)
+ pipeline->add_renderable(*base_object);
+ pipeline->add_renderable_for_pass(track_wrap, "unlit");
+ pipeline->add_renderable_for_pass(layout_3d->get_path_scene(), "unlit");
+ pipeline->add_renderable_for_pass(layout_3d->get_endpoint_scene(), "unlit");
-Designer::~Designer()
-{
- delete manipulator;
- delete selection;
- delete layout;
- delete layout_3d;
- delete cat_layout;
- delete cat_layout_3d;
- delete measure;
-}
+ light.set_position(0, -0.259, 0.966, 0);
+ lighting.attach(0, light);
-int Designer::main()
-{
- dpy=new Graphics::Display;
- wnd=new Graphics::Window(*dpy, screen_w, screen_h);
- glc=new Graphics::GLContext(*wnd);
+ GL::PipelinePass *pass = &pipeline->add_pass(0);
+ pass->lighting = &lighting;
+ pass->depth_test = &GL::DepthTest::lequal();
+
+ pass = &pipeline->add_pass("unlit");
+ pass->depth_test = &GL::DepthTest::lequal();
+ pass->blend = &GL::Blend::alpha();
- wnd->signal_close.connect(sigc::bind(sigc::mem_fun(this, &Designer::exit), 0));
- wnd->signal_key_press.connect(sigc::mem_fun(this, &Designer::key_press));
- wnd->signal_key_release.connect(sigc::mem_fun(this, &Designer::key_release));
- wnd->signal_button_press.connect(sigc::mem_fun(this, &Designer::button_press));
- wnd->signal_pointer_motion.connect(sigc::mem_fun(this, &Designer::pointer_motion));
+ pass = &pipeline->add_pass("blended");
+ pass->lighting = &lighting;
+ pass->depth_test = &GL::DepthTest::lequal();
+ pass->blend = &GL::Blend::alpha();
- wnd->show();
+ camera.set_up_direction(GL::Vector3(0, 0, 1));
+ camera.set_look_direction(GL::Vector3(0, 0.5, -0.866));
- glEnableClientState(GL_VERTEX_ARRAY);
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- glEnable(GL_CULL_FACE);
+ // Setup UI
+ root.signal_key_press.connect(sigc::mem_fun(this, &Designer::key_press));
+ root.signal_button_press.connect(sigc::mem_fun(this, &Designer::button_press));
+ root.signal_pointer_motion.connect(sigc::mem_fun(this, &Designer::pointer_motion));
+ root.signal_tooltip.connect(sigc::mem_fun(this, &Designer::tooltip));
- GL::Texture2D *font_tex=new GL::Texture2D;
- font_tex->set_min_filter(GL::LINEAR);
- font_tex->load_image("dejavu-20.png");
- font=new GL::Font();
- font->set_texture(*font_tex);
- DataFile::load(*font, "dejavu-20.font");
+ toolbar = new Toolbar(*this);
+ root.add(*toolbar);
+ toolbar->set_position(0, window.get_height()-toolbar->get_geometry().h);
+ toolbar->set_focusable(false);
- mode=SELECT;
+ GLtk::Panel *statusbar = new GLtk::Panel;
+ root.add(*statusbar);
+ statusbar->set_size(window.get_width(), 20);
+ statusbar->set_focusable(false);
- Application::main();
+ lbl_status = new GLtk::Label;
+ statusbar->add(*lbl_status);
+ lbl_status->set_geometry(GLtk::Geometry(20, 2, 300, 16));
- delete font;
- delete font_tex;
- delete input;
+ overlay = new Overlay3D(window, camera, ui_res.get_default_font());
- delete glc;
- delete wnd;
- delete dpy;
+ camera_ctl.view_all();
- return exit_code;
+ const Layout3D::TrackMap &tracks = layout_3d->get_tracks();
+ for(Layout3D::TrackMap::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
+ update_track_icon(*i->second);
}
-void Designer::map_pointer_coords(int x, int y, float &gx, float &gy)
+Designer::~Designer()
{
- float cos_pitch=cos(cam_pitch);
- float sin_pitch=sin(cam_pitch);
- float cos_yaw=cos(cam_yaw);
- float sin_yaw=sin(cam_yaw);
-
- float rx=sin_yaw*0.55228;
- float ry=-cos_yaw*0.55228;
+ delete overlay;
+ delete pipeline;
+ delete base_object;
+ delete layout_3d;
+ delete layout;
+ delete cat_layout_3d;
+}
- float ux=cos_yaw*-sin_pitch*0.41421;
- float uy=sin_yaw*-sin_pitch*0.41421;
- float uz=cos_pitch*0.41421;
+int Designer::main()
+{
+ window.show();
- float xf=static_cast<float>(x)*2/screen_w-1;
- float yf=static_cast<float>(y)*2/screen_h-1;
+ mode = SELECT;
- float vx=cos_yaw*cos_pitch + xf*rx + yf*ux;
- float vy=sin_yaw*cos_pitch + xf*ry + yf*uy;
- float vz=sin_pitch + yf*uz;
+ return Application::main();
+}
- gx=cam_pos.x-vx*cam_pos.z/vz;
- gy=cam_pos.y-vy*cam_pos.z/vz;
+void Designer::save()
+{
+ InputDialog *input = new InputDialog(*this, "Save layout", filename);
+ input->signal_accept.connect(sigc::mem_fun(layout, &Layout::save));
}
-void Designer::tick()
+void Designer::quit()
{
- const Msp::Time::TimeStamp t=Msp::Time::now();
- float dt=(t-last_tick)/Msp::Time::sec;
- last_tick=t;
+ exit(0);
+}
- dpy->tick();
+void Designer::new_track()
+{
+ mode = CATALOGUE;
+}
- if(move_y)
+void Designer::set_turnout_id()
+{
+ Track *track = selection.get_track();
+ if(selection.size()==1 && track->get_type().is_turnout())
{
- cam_pos.x+=cos(cam_yaw)*dt*move_y;
- cam_pos.y+=sin(cam_yaw)*dt*move_y;
+ InputDialog *input = new InputDialog(*this, "Turnout ID", lexical_cast(track->get_turnout_id()));
+ input->signal_accept.connect(sigc::mem_fun(this, &Designer::turnout_id_accept));
}
- if(move_x)
+}
+
+void Designer::set_sensor_id()
+{
+ const set<Track *> &tracks = selection.get_tracks();
+ bool ok = false;
+ int id = -1;
+ for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
{
- cam_pos.x+=sin(cam_yaw)*dt*move_x;
- cam_pos.y+=-cos(cam_yaw)*dt*move_x;
+ if(!(*i)->get_type().is_turnout())
+ ok = true;
+ if(static_cast<int>((*i)->get_sensor_id())!=id)
+ {
+ if(id==-1)
+ id = (*i)->get_sensor_id();
+ else
+ id = -2;
+ }
}
- if(zoom)
+
+ if(ok)
{
- cam_pos.x+=cos(cam_yaw)*cos(cam_pitch)*dt*zoom;
- cam_pos.y+=sin(cam_yaw)*cos(cam_pitch)*dt*zoom;
- cam_pos.z+=sin(cam_pitch)*dt*zoom;
+ InputDialog *input = new InputDialog(*this, "Sensor ID", (id>=0 ? lexical_cast(id) : string()));
+ input->signal_accept.connect(sigc::mem_fun(this, &Designer::sensor_id_accept));
}
- if(rotate)
+}
+
+void Designer::rename_route()
+{
+ if(!cur_route)
+ return;
+
+ InputDialog *input = new InputDialog(*this, "Route name", cur_route->get_name());
+ input->signal_accept.connect(sigc::mem_fun(this, &Designer::route_name_accept));
+}
+
+void Designer::svg_export()
+{
+ InputDialog *input = new InputDialog(*this, "SVG export", FS::basepart(filename)+".svg");
+ input->signal_accept.connect(sigc::mem_fun(this, &Designer::svg_export_accept));
+}
+
+void Designer::edit_route(Route *r)
+{
+ cur_route = r;
+ show_route(r);
+}
+
+void Designer::add_selection_to_route()
+{
+ if(!cur_route)
+ return;
+
+ try
{
- float vx=cos(cam_yaw)*cos(cam_pitch);
- float vy=sin(cam_yaw)*cos(cam_pitch);
- float vz=sin(cam_pitch);
-
- float gx=cam_pos.x-vx*cam_pos.z/vz;
- float gy=cam_pos.y-vy*cam_pos.z/vz;
- float d=sqrt(vx*vx+vy*vy)*cam_pos.z/vz;
-
- cam_yaw+=M_PI*dt*rotate;
- if(cam_yaw>M_PI*2)
- cam_yaw-=M_PI*2;
- else if(cam_yaw<0)
- cam_yaw+=M_PI*2;
-
- cam_pos.x=gx+cos(cam_yaw)*d;
- cam_pos.y=gy+sin(cam_yaw)*d;
+ cur_route->add_tracks(selection.get_tracks());
}
- if(pitch)
+ catch(const Exception &e)
{
- cam_pitch+=M_PI/2*dt*pitch;
- if(cam_pitch>M_PI/12)
- cam_pitch=M_PI/12;
- else if(cam_pitch<-M_PI/2)
- cam_pitch=-M_PI/2;
+ lbl_status->set_text(e.what());
}
- if(tooltip_timeout && t>tooltip_timeout)
- {
- Track3D *t3d=0;
+ show_route(cur_route);
+}
- if(mode==CATALOGUE)
- t3d=pick_track(pointer_x, pointer_y);
- else
- t3d=pick_track(pointer_x, pointer_y);
+Point Designer::map_pointer_to_ground(int x, int y)
+{
+ float xf = x*2.0/window.get_width()-1.0;
+ float yf = y*2.0/window.get_height()-1.0;
+ GL::Vector4 vec = camera.unproject(GL::Vector4(xf, yf, 0, 0));
+ const GL::Vector3 &pos = camera.get_position();
- if(t3d)
- {
- const Track &track=t3d->get_track();
- const TrackType &ttype=track.get_type();
- ostringstream ss;
- ss.precision(2);
- ss<<ttype.get_article_number()<<' '<<ttype.get_description();
- if(mode!=CATALOGUE)
- ss<<" (slope "<<track.get_slope()/ttype.get_total_length()*100<<"%)";
- if(track.get_turnout_id())
- ss<<" (turnout "<<track.get_turnout_id()<<')';
- else if(track.get_sensor_id())
- ss<<" (sensor "<<track.get_sensor_id()<<')';
- tooltip=ss.str();
-
- move_tooltip(pointer_x, pointer_y);
- }
- else
- tooltip="";
+ return Point(pos.x-vec.x*pos.z/vec.z, pos.y-vec.y*pos.z/vec.z);
+}
- tooltip_timeout=Msp::Time::TimeStamp();
- }
+void Designer::tick()
+{
+ const Msp::Time::TimeStamp t = Msp::Time::now();
+ float dt = (t-last_tick)/Msp::Time::sec;
+ last_tick = t;
+
+ if(mode==MANIPULATE_DONE)
+ mode = SELECT;
+
+ window.tick();
+ root.tick();
+ camera_ctl.tick(dt);
+
+ for(list<Track *>::iterator i=new_tracks.begin(); i!=new_tracks.end(); ++i)
+ layout_3d->get_track(**i).get_path().set_mask(0);
+ new_tracks.clear();
render();
- glc->swap_buffers();
+ window.swap_buffers();
}
-/*** private ***/
-
-void Designer::key_press(unsigned code, unsigned mod, wchar_t ch)
+void Designer::key_press(unsigned key, unsigned mod, wchar_t)
{
- unsigned key=Msp::Input::key_from_sys(code);
+ key = Input::key_from_sys(key);
+ mod = Input::mod_from_sys(mod);
- if(mode==INPUT)
+ if(key==Msp::Input::KEY_N && (mod&Input::MOD_SHIFT))
{
- input->key_press(key, mod, ch);
- return;
+ manipulator.start_extend();
+ mode = MANIPULATE;
}
-
- if(key==Msp::Input::KEY_SHIFT_L || key==Msp::Input::KEY_SHIFT_R)
- shift=true;
-
- if(key==Msp::Input::KEY_N)
- mode=CATALOGUE;
+ else if(key==Msp::Input::KEY_N)
+ new_track();
else if(key==Msp::Input::KEY_G)
{
- manipulator->start_move();
- mode=MANIPULATE;
+ manipulator.start_move();
+ mode = MANIPULATE;
}
else if(key==Msp::Input::KEY_R)
{
- manipulator->start_rotate();
- mode=MANIPULATE;
+ manipulator.start_rotate();
+ mode = MANIPULATE;
}
else if(key==Msp::Input::KEY_D)
{
- manipulator->duplicate();
- manipulator->start_move();
- mode=MANIPULATE;
+ manipulator.duplicate();
+ manipulator.start_move();
+ mode = MANIPULATE;
}
else if(key==Msp::Input::KEY_W)
- {
- input=new ::Input(*this, "Filename");
- input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
- input->signal_accept.connect(sigc::mem_fun(this, &Designer::save_accept));
- mode=INPUT;
- }
+ save();
else if(key==Msp::Input::KEY_PLUS)
- selection->select_more();
- else if(key==Msp::Input::KEY_L && (mod&1))
- {
- const set<Track *> &tracks=layout->get_tracks();
- float len=0;
- for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
- len+=(*i)->get_type().get_total_length();
- cout<<"Total length: "<<len<<"m\n";
- }
+ selection.select_more();
+ else if(key==Msp::Input::KEY_L && (mod&Input::MOD_SHIFT))
+ selection.select_blocks();
else if(key==Msp::Input::KEY_L)
- selection->select_linked();
+ selection.select_linked();
else if(key==Msp::Input::KEY_M)
{
- measure->start();
- mode=MEASURE;
+ measure.start();
+ mode = MEASURE;
}
else if(key==Msp::Input::KEY_Z)
{
- manipulator->start_elevate();
- mode=MANIPULATE;
+ manipulator.start_elevate();
+ mode = MANIPULATE;
}
else if(key==Msp::Input::KEY_ESC)
{
if(mode==MANIPULATE)
- manipulator->cancel();
+ manipulator.cancel();
else if(mode==CATALOGUE)
- mode=SELECT;
+ mode = SELECT;
else
- selection->clear();
+ selection.clear();
}
else if(key==Msp::Input::KEY_X)
{
- set<Track *> tracks=selection->get_tracks();
- selection->clear();
+ set<Track *> tracks = selection.get_tracks();
+ selection.clear();
for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
{
+ overlay->clear(layout_3d->get_track(**i));
layout->remove_track(**i);
delete *i;
}
}
- else if(key==Msp::Input::KEY_F && (mod&1))
+ else if(key==Msp::Input::KEY_F && (mod&Input::MOD_SHIFT))
{
- const set<Track *> &tracks=selection->get_tracks();
- const set<Track *> <racks=layout->get_tracks();
+ const set<Track *> &tracks = selection.get_tracks();
+ const set<Track *> <racks = layout->get_tracks();
for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
{
(*i)->set_flex(!(*i)->get_flex());
if(*j!=*i)
(*i)->snap_to(**j, true);
- Track3D &t3d=layout_3d->get_track(**i);
- if((*i)->get_flex())
- t3d.set_color(GL::Color(1, 0.5, 1));
- else
- t3d.set_color(GL::Color(1, 1, 1));
+ update_track_icon(layout_3d->get_track(**i));
}
}
else if(key==Msp::Input::KEY_F)
- manipulator->flatten();
- else if(key==Msp::Input::KEY_E && (mod&1))
- manipulator->even_slope(true);
+ manipulator.flatten();
+ else if(key==Msp::Input::KEY_E && (mod&Input::MOD_SHIFT))
+ manipulator.even_slope(true);
else if(key==Msp::Input::KEY_E)
- manipulator->even_slope();
+ manipulator.even_slope();
else if(key==Msp::Input::KEY_T)
- {
- Track *track=selection->get_track();
- if(selection->size()==1 && track->get_type().get_n_routes()>1)
- {
- ostringstream ss;
- ss<<track->get_turnout_id();
- input=new ::Input(*this, "Turnout ID", ss.str());
- input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
- input->signal_accept.connect(sigc::mem_fun(this, &Designer::turnout_id_accept));
- mode=INPUT;
- }
- }
+ set_turnout_id();
else if(key==Msp::Input::KEY_S)
- {
- const set<Track *> &tracks=selection->get_tracks();
- bool ok=false;
- int id=-1;
- for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
- {
- if((*i)->get_type().get_n_routes()==1)
- ok=true;
- if(static_cast<int>((*i)->get_sensor_id())!=id)
- {
- if(id==-1)
- id=(*i)->get_sensor_id();
- else
- id=-2;
- }
- }
- if(ok)
- {
- ostringstream ss;
- if(id>=0)
- ss<<id;
- input=new ::Input(*this, "Sensor ID", ss.str());
- input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
- input->signal_accept.connect(sigc::mem_fun(this, &Designer::sensor_id_accept));
- mode=INPUT;
- }
- }
- else if(key==Msp::Input::KEY_RIGHT)
- rotate=-1;
- else if(key==Msp::Input::KEY_LEFT)
- rotate=1;
- else if(key==Msp::Input::KEY_UP)
- move_y=1;
- else if(key==Msp::Input::KEY_DOWN)
- move_y=-1;
- else if(key==Msp::Input::KEY_INSERT)
- zoom=-1;
- else if(key==Msp::Input::KEY_PGUP)
- zoom=1;
- else if(key==Msp::Input::KEY_HOME)
- pitch=1;
- else if(key==Msp::Input::KEY_END)
- pitch=-1;
- else if(key==Msp::Input::KEY_DELETE)
- move_x=-1;
- else if(key==Msp::Input::KEY_PGDN)
- move_x=1;
-}
-
-void Designer::key_release(unsigned code, unsigned)
-{
- unsigned key=Msp::Input::key_from_sys(code);
-
- if(mode==INPUT)
- return;
-
- if(key==Msp::Input::KEY_SHIFT_L || key==Msp::Input::KEY_SHIFT_R)
- shift=false;
- else if(key==Msp::Input::KEY_RIGHT || key==Msp::Input::KEY_LEFT)
- rotate=0;
- else if(key==Msp::Input::KEY_UP || key==Msp::Input::KEY_DOWN)
- move_y=0;
- else if(key==Msp::Input::KEY_INSERT || key==Msp::Input::KEY_PGUP)
- zoom=0;
- else if(key==Msp::Input::KEY_HOME || key==Msp::Input::KEY_END)
- pitch=0;
- else if(key==Msp::Input::KEY_DELETE || key==Msp::Input::KEY_PGDN)
- move_x=0;
+ set_sensor_id();
+ else if(key==Msp::Input::KEY_A)
+ add_selection_to_route();
+ else if(key==Msp::Input::KEY_C)
+ manipulator.connect();
+ else if(key==Msp::Input::KEY_V)
+ svg_export();
}
-void Designer::button_press(int x, int y, unsigned btn, unsigned)
+void Designer::button_press(int x, int y, unsigned btn, unsigned mod)
{
- y=screen_h-y-1;
+ y = window.get_height()-y-1;
+ mod = Input::mod_from_sys(mod);
- float gx, gy;
- map_pointer_coords(x, y, gx, gy);
+ Point ground = map_pointer_to_ground(x, y);
if(mode==CATALOGUE)
{
if(btn==1)
{
- Track3D *ctrack=pick_track(x, y);
+ Track3D *ctrack = pick_track(x, y);
if(ctrack)
{
- Track *track=ctrack->get_track().copy();
- track->set_position(Point(gx, gy, 0));
- layout->add_track(*track);
+ Track *track = new Track(*layout, ctrack->get_track().get_type());
+ track->set_position(ground);
- selection->clear();
- selection->add_track(track);
+ selection.clear();
+ selection.add_track(track);
- mode=SELECT;
+ mode = SELECT;
}
}
else
- mode=SELECT;
+ mode = SELECT;
}
else if(mode==SELECT)
{
if(btn==1)
{
- Track3D *track=pick_track(x, y);
+ Track3D *track = pick_track(x, y);
if(track)
{
- if(!shift)
- selection->clear();
- selection->toggle_track(&track->get_track());
+ if(!(mod&Input::MOD_SHIFT))
+ selection.clear();
+ selection.toggle_track(&track->get_track());
}
}
}
- else if(mode==MANIPULATE)
- manipulator->button_press(x, y, gx, gy, btn);
else if(mode==MEASURE)
- measure->button_press(x, y, gx, gy, btn);
+ measure.button_press(x, y, ground.x, ground.y, btn);
}
void Designer::pointer_motion(int x, int y)
{
- y=screen_h-y-1;
+ y = window.get_height()-y-1;
- float gx, gy;
- map_pointer_coords(x, y, gx, gy);
-
- if(mode==SELECT || mode==CATALOGUE)
+ if(!root.get_child_at(x, y))
{
- pointer_x=x;
- pointer_y=y;
- tooltip_timeout=Msp::Time::now()+100*Msp::Time::msec;
+ Point ground = map_pointer_to_ground(x, y);
+ measure.pointer_motion(x, y, ground.x, ground.y);
}
-
- if(mode!=INPUT)
- {
- manipulator->pointer_motion(x, y, gx, gy);
- measure->pointer_motion(x, y, gx, gy);
- }
-
- if(mode==MEASURE || mode==MANIPULATE)
- move_tooltip(x, y);
-}
-
-void Designer::project_3d()
-{
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glFrustum(-0.055228, 0.055228, -0.041421, 0.041421, 0.1, 10);
- glMatrixMode(GL_MODELVIEW);
}
void Designer::apply_camera()
{
- glLoadIdentity();
if(mode==CATALOGUE)
- glTranslatef(0, 0, -1);
- else
{
- glRotatef(-cam_pitch*180/M_PI-90, 1, 0, 0);
- glRotatef(90-cam_yaw*180/M_PI, 0, 0, 1);
- glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
+ GL::matrix_mode(GL::PROJECTION);
+ GL::load_identity();
+ GL::frustum_centered(0.11046, 0.082843, 0.1, 10);
+ GL::matrix_mode(GL::MODELVIEW);
+ GL::load_identity();
+ GL::translate(0, 0, -1);
}
+ else
+ camera.apply();
}
void Designer::render()
{
- glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
- glEnable(GL_DEPTH_TEST);
+ GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
- project_3d();
- apply_camera();
if(mode==CATALOGUE)
- cat_layout_3d->render();
+ {
+ apply_camera();
+ cat_layout_3d->get_scene().render();
+ }
else
{
- if(base_mesh)
+ pipeline->render_all();
+ GL::enable(GL_CULL_FACE);
{
- GL::Texture::unbind();
- base_mesh->draw();
+ GL::Bind bind_blend(GL::Blend::alpha());
+ overlay->render(0);
}
- layout_3d->render(true);
- manipulator->render();
+
+ GL::Bind bind_depth(GL::DepthTest::lequal());
if(mode==MEASURE)
- measure->render();
+ measure.render();
}
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0, screen_w, 0, screen_h, 0, 1);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
+ GL::matrix_mode(GL::PROJECTION);
+ GL::load_identity();
+ GL::ortho_bottomleft(window.get_width(), window.get_height());
+ GL::matrix_mode(GL::MODELVIEW);
+ GL::load_identity();
- glDisable(GL_DEPTH_TEST);
+ GL::disable(GL::DEPTH_TEST);
- if(!tooltip.empty())
- {
- glTranslatef(tooltip_x, tooltip_y, 0);
- glScalef(20, 20, 20);
- float width=font->get_string_width(tooltip);
- glColor4f(0, 0, 0, 0.5);
- glBegin(GL_QUADS);
- glVertex2f(0, 0);
- glVertex2f(width, 0);
- glVertex2f(width, 1);
- glVertex2f(0, 1);
- glEnd();
- glColor4f(1, 1, 1, 1);
- font->draw_string(tooltip);
- }
+ GL::Bind bind_blend(GL::Blend::alpha());
+ root.render();
+ GL::Texture::unbind();
+}
+
+void Designer::track_added(Track &trk)
+{
+ new_tracks.push_back(&trk);
+}
- if(mode==INPUT)
- input->render();
+void Designer::track_removed(Track &trk)
+{
+ list<Track *>::iterator i = find(new_tracks.begin(), new_tracks.end(), &trk);
+ if(i!=new_tracks.end())
+ new_tracks.erase(i);
}
Track3D *Designer::pick_track(int x, int y)
{
- Layout3D *l=layout_3d;
+ Layout3D *l = layout_3d;
if(mode==CATALOGUE)
- l=cat_layout_3d;
+ l = cat_layout_3d;
- float xx=(static_cast<float>(x-static_cast<int>(screen_w)/2)/screen_h)*0.82843;
- float yy=(static_cast<float>(y)/screen_h-0.5)*0.82843;
- float size=4.0/screen_h*0.82843;
+ float xx = ((float(x)-window.get_width()/2)/window.get_height())*0.82843;
+ float yy = (float(y)/window.get_height()-0.5)*0.82843;
+ float size = 4.0/window.get_height()*0.82843;
- project_3d();
apply_camera();
return l->pick_track(xx, yy, size);
}
-void Designer::manipulation_status(const string &status)
+void Designer::update_track_icon(Track3D &track)
{
- tooltip=status;
+ overlay->clear(track);
+
+ if(track.get_track().get_flex())
+ overlay->add_graphic(track, "flex");
+
+ if(unsigned sid = track.get_track().get_sensor_id())
+ {
+ overlay->add_graphic(track, "sensor");
+ overlay->set_label(track, lexical_cast(sid));
+ }
+ else if(unsigned tid = track.get_track().get_turnout_id())
+ {
+ if(tid<0x800)
+ {
+ overlay->add_graphic(track, "turnout");
+ overlay->set_label(track, lexical_cast(tid));
+ }
+ }
}
-void Designer::manipulation_done(bool)
+void Designer::selection_changed()
{
- mode=SELECT;
+ const set<Track *> &tracks = selection.get_tracks();
+ if(tracks.empty())
+ lbl_status->set_text(string());
+ else
+ {
+ float len = 0;
+ for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
+ len += (*i)->get_type().get_total_length();
+ lbl_status->set_text(format("%.2fm of track selected\n", len));
+ }
}
-void Designer::measure_changed()
+void Designer::manipulation_status(const string &status)
{
- float pard=measure->get_parallel_distance()*1000;
- float perpd=measure->get_perpendicular_distance()*1000;
- float d=sqrt(pard*pard+perpd*perpd);
- float adiff=measure->get_angle_difference()*180/M_PI;
- ostringstream ss;
- ss.precision(3);
- ss<<"Par "<<pard<<"mm - Perp "<<perpd<<"mm - Total "<<d<<"mm - Angle "<<adiff<<"°";
- tooltip=ss.str();
+ lbl_status->set_text(status);
}
-void Designer::measure_done()
+void Designer::manipulation_done(bool)
{
- mode=SELECT;
+ mode = MANIPULATE_DONE;
+ selection_changed();
}
-void Designer::move_tooltip(int x, int y)
+void Designer::measure_changed()
{
- int w=static_cast<int>(font->get_string_width(tooltip)*20);
- tooltip_x=max(min(static_cast<int>(screen_w)-w, x), 0);
- tooltip_y=max(min(static_cast<int>(screen_h)-20, y), 0);
+ float pard = measure.get_parallel_distance()*1000;
+ float perpd = measure.get_perpendicular_distance()*1000;
+ float d = sqrt(pard*pard+perpd*perpd);
+ float adiff = measure.get_angle_difference()*180/M_PI;
+ string info = format("Par %.1fmm - Perp %.1fmm - Total %.1fmm - Angle %.1f°", pard, perpd, d, adiff);
+ lbl_status->set_text(info);
}
-void Designer::save_accept()
+void Designer::measure_done()
{
- layout->save(input->get_text());
-
- input_dismiss();
+ mode = SELECT;
+ selection_changed();
}
-void Designer::turnout_id_accept()
+void Designer::turnout_id_accept(const string &text)
{
- Track *track=selection->get_track();
- unsigned id=lexical_cast<unsigned>(input->get_text());
+ Track *track = selection.get_track();
+ unsigned id = (text.empty() ? 0 : lexical_cast<unsigned>(text));
track->set_turnout_id(id);
- Track3D &t3d=layout_3d->get_track(*track);
- if(id)
- t3d.set_color(GL::Color(0.5, 1, 1));
- else
- t3d.set_color(GL::Color(1, 1, 1));
-
- input_dismiss();
+ update_track_icon(layout_3d->get_track(*track));
}
-void Designer::sensor_id_accept()
+void Designer::sensor_id_accept(const string &text)
{
- const set<Track *> &tracks=selection->get_tracks();
- unsigned id=lexical_cast<unsigned>(input->get_text());
+ const set<Track *> &tracks = selection.get_tracks();
+ unsigned id = (text.empty() ? 0 : lexical_cast<unsigned>(text));
for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
{
(*i)->set_sensor_id(id);
- Track3D &t3d=layout_3d->get_track(**i);
- if(id)
- t3d.set_color(GL::Color(1, 1, 0.5));
- else
- t3d.set_color(GL::Color(1, 1, 1));
+ update_track_icon(layout_3d->get_track(**i));
}
+}
- input_dismiss();
+void Designer::route_name_accept(const string &text)
+{
+ if(cur_route)
+ cur_route->set_name(text);
}
-void Designer::input_dismiss()
+void Designer::svg_export_accept(const string &text)
{
- delete input;
- input=0;
- mode=SELECT;
+ SvgExporter svg_exp(*layout);
+ svg_exp.save(text);
}
-Application::RegApp<Designer> Designer::reg;
+string Designer::tooltip(int x, int y)
+{
+ if(Track3D *t3d = pick_track(x, y))
+ {
+ const Track &track = t3d->get_track();
+ const TrackType &ttype = track.get_type();
+ string info = format("%d %s", ttype.get_article_number(), ttype.get_description());
+ if(mode!=CATALOGUE && abs(track.get_slope())>1e-4)
+ info += format(" (slope %.1f%%)", abs(track.get_slope()/ttype.get_total_length()*100));
+ if(track.get_turnout_id())
+ info += format(" (turnout %d)", track.get_turnout_id());
+ else if(track.get_sensor_id())
+ info += format(" (sensor %d)", track.get_sensor_id());
+
+ return info;
+ }
+
+ return string();
+}
+
+void Designer::show_route(const Route *route)
+{
+ const set<Track *> <racks = layout->get_tracks();
+ for(set<Track *>::iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
+ {
+ Track3D &t3d = layout_3d->get_track(**i);
+ if(route && route->has_track(**i))
+ {
+ t3d.get_path().set_color(GL::Color(0.5, 0.8, 1.0));
+ if((*i)->get_type().is_turnout())
+ {
+ unsigned tid = (*i)->get_turnout_id();
+ int path = (tid ? route->get_turnout(tid) : -1);
+ if(path>=0)
+ t3d.get_path().set_path(path);
+ else
+ t3d.get_path().set_mask((*i)->get_type().get_paths());
+ }
+ else
+ t3d.get_path().set_path(0);
+ }
+ else
+ t3d.get_path().set_mask(0);
+ }
+}