#include <cmath>
#include <msp/gl/meshbuilder.h>
#include <msp/gl/renderer.h>
+#include <msp/strings/format.h>
#include "designer.h"
#include "3d/layout.h"
#include "measure.h"
using namespace R2C2;
using namespace Msp;
-Measure::Measure(Designer &d):
- designer(d),
- state(NONE),
+Measure::Measure(Designer &d, Input::Mouse &m, const set<Object *> &):
+ Tool(d, m),
+ start_pinned(false),
mesh((GL::COLOR4_UBYTE, GL::VERTEX3))
-{ }
-
-void Measure::start()
{
- state = STARTING;
update_mesh();
+
+ designer.get_layout_3d().get_scene().add(*this);
}
-void Measure::button_press(float gx, float gy, unsigned btn)
+Measure::~Measure()
{
- if(!state)
- return;
+ designer.get_layout_3d().get_scene().remove(*this);
+}
+void Measure::button_press(unsigned btn)
+{
if(btn==1)
{
- ssnap.position = Vector(gx, gy, 0);
- ssnap.rotation = Angle::zero();
- snap_to_tracks(ssnap);
+ start.position = ground_pointer;
+ start.rotation = Angle::zero();
+ snap_to_tracks(start);
- state = ACTIVE;
+ start_pinned = true;
}
else if(btn==3)
{
- if(state==ACTIVE)
+ if(start_pinned)
{
- state = STARTING;
+ start_pinned = false;
update_mesh();
}
else
{
- state = NONE;
+ done = true;
signal_done.emit();
}
}
}
-void Measure::pointer_motion(float gx, float gy)
+void Measure::axis_motion(unsigned axis, float x, float y)
{
- if(!state)
- return;
+ Tool::axis_motion(axis, x, y);
- Snap sn = ssnap;
- sn.position = Vector(gx, gy, 0);
+ Snap sn = start;
+ sn.position = ground_pointer;
snap_to_tracks(sn);
- pointer = sn.position;
+ ground_pointer = sn.position;
- if(state!=STARTING)
+ if(start_pinned)
{
- Vector delta = rotated_vector(pointer-ssnap.position, -ssnap.rotation);
+ Vector delta = rotated_vector(ground_pointer-start.position, -start.rotation);
par_dist = delta.x;
perp_dist = delta.y;
- adiff = wrap_balanced(sn.rotation-ssnap.rotation+Angle::half_turn());
+ adiff = wrap_balanced(sn.rotation-start.rotation+Angle::half_turn());
update_mesh();
- signal_changed.emit();
+ string info = format("Par %.1fmm - Perp %.1fmm - Total %.1fmm - Angle %.1f°", par_dist*1000, perp_dist*1000, delta.norm()*1000, adiff.degrees());
+ signal_status.emit(info);
}
}
void Measure::render(GL::Renderer &renderer, const GL::Tag &) const
{
- if(state==NONE)
- return;
-
GL::Renderer::Push push(renderer);
- const Vector &pos = (state==ACTIVE ? ssnap.position : pointer);
+ const Vector &pos = (start_pinned ? start.position : ground_pointer);
renderer.matrix_stack() *= GL::Matrix::translation(pos);
mesh.draw(renderer);
}
bld.end();
- if(state==ACTIVE)
+ if(start_pinned)
{
- float c = cos(ssnap.rotation);
- float s = sin(ssnap.rotation);
+ float c = cos(start.rotation);
+ float s = sin(start.rotation);
bld.begin(GL::QUAD_STRIP);
bld.vertex(0, 0, 0);
bld.vertex(0, 0, 0.01);
bld.vertex(c*par_dist, s*par_dist, 0);
bld.vertex(c*par_dist, s*par_dist, 0.01);
- bld.vertex(pointer.x-ssnap.position.x, pointer.y-ssnap.position.y, 0);
- bld.vertex(pointer.x-ssnap.position.x, pointer.y-ssnap.position.y, 0.01);
+ bld.vertex(ground_pointer.x-start.position.x, ground_pointer.y-start.position.y, 0);
+ bld.vertex(ground_pointer.x-start.position.x, ground_pointer.y-start.position.y, 0.01);
bld.vertex(0, 0, 0);
bld.vertex(0, 0, 0.01);
bld.end();