Skip to content

Commit

Permalink
new tool: measure
Browse files Browse the repository at this point in the history
  • Loading branch information
carrotIndustries committed Jan 12, 2021
1 parent 43c1928 commit 478bc50
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 1 deletion.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ SRC_IMP = \
src/core/tools/tool_place_picture.cpp\
src/core/tools/tool_place_decal.cpp\
src/core/tools/tool_drag_polygon_edge.cpp\
src/core/tools/tool_measure.cpp\
src/document/documents.cpp\
src/core/clipboard/clipboard.cpp\
src/core/clipboard/clipboard_padstack.cpp\
Expand Down
4 changes: 4 additions & 0 deletions src/core/create_tool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
#include "tools/tool_place_decal.hpp"
#include "tools/tool_draw_plane.hpp"
#include "tools/tool_drag_polygon_edge.hpp"
#include "tools/tool_measure.hpp"

namespace horizon {

Expand Down Expand Up @@ -393,6 +394,9 @@ std::unique_ptr<ToolBase> Core::create_tool(ToolID tool_id)
case ToolID::DRAG_POLYGON_EDGE:
return std::make_unique<ToolDragPolygonEdge>(this, tool_id);

case ToolID::MEASURE:
return std::make_unique<ToolMeasure>(this, tool_id);

default:
return nullptr;
}
Expand Down
3 changes: 2 additions & 1 deletion src/core/tool_id.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ enum class ToolID {
PLACE_DECAL,
DRAW_PLANE,
DRAW_KEEPOUT,
DRAG_POLYGON_EDGE
DRAG_POLYGON_EDGE,
MEASURE,
};
} // namespace horizon
136 changes: 136 additions & 0 deletions src/core/tools/tool_measure.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#include "tool_measure.hpp"
#include "document/idocument.hpp"
#include "imp/imp_interface.hpp"
#include "canvas/canvas_gl.hpp"
#include <sstream>
#include "util/util.hpp"
#include "common/object_descr.hpp"

namespace horizon {


ToolResponse ToolMeasure::begin(const ToolArgs &args)
{
annotation = imp->get_canvas()->create_annotation();
annotation->set_visible(true);
annotation->set_display(LayerDisplay(true, LayerDisplay::Mode::OUTLINE));
update_tip();
return ToolResponse();
}


void ToolMeasure::update_tip()
{
std::vector<ActionLabelInfo> actions;
actions.reserve(8);

switch (state) {
case State::FROM:
case State::DONE:
actions.emplace_back(InToolActionID::LMB, "select from point");
break;
case State::TO:
actions.emplace_back(InToolActionID::LMB, "select to point");
break;
}
actions.emplace_back(InToolActionID::RMB, "cancel");
std::stringstream ss;
if (state != State::FROM) {
const auto delta = to - from;
if (delta.y == 0) {
ss << "ΔX: " << dim_to_string(delta.x, true);
}
else if (delta.x == 0) {
ss << "ΔY: " << dim_to_string(delta.y, true);
}
else {
ss << coord_to_string(delta, true) << " D:" << dim_to_string(sqrt(delta.mag_sq()), false);
ss << " α:" << angle_to_string(atan2(delta.y, delta.x) / M_PI * 32768, false);
}
}

if (state == State::DONE) {
ss << " " << s_from << "" << s_to;
}

imp->tool_bar_set_actions(actions);
imp->tool_bar_set_tip(ss.str());
}


static std::string name_from_target(const Target &trg)
{
if (!trg.is_valid())
return "None";

return object_descriptions.at(trg.type).name;
}

ToolResponse ToolMeasure::update(const ToolArgs &args)
{
if (args.type == ToolEventType::MOVE) {
switch (state) {
case State::FROM:
from = args.coords;
break;
case State::TO:
to = args.coords;
annotation->clear();
{
const auto v = Coordf(to - from);
const auto l = sqrt(v.mag_sq());
const auto vn = (v / l).rotate(M_PI / 2);
const auto m = vn * l / 20;
annotation->draw_line(from, to, ColorP::FROM_LAYER, 2);
for (const auto p : {Coordf(from), Coordf(to)}) {
annotation->draw_line(p, p + m, ColorP::FROM_LAYER, 2);
annotation->draw_line(p, p - m, ColorP::FROM_LAYER, 2);
}
}
break;
case State::DONE:;
}
update_tip();
return ToolResponse();
}
else if (args.type == ToolEventType::ACTION) {
switch (args.action) {
case InToolActionID::LMB:
switch (state) {
case State::FROM:
s_from = name_from_target(args.target);
state = State::TO;
break;
case State::TO:
s_to = name_from_target(args.target);
state = State::DONE;
break;
case State::DONE:
state = State::TO;
from = args.coords;
s_from = name_from_target(args.target);
s_to.clear();
break;
default:;
}
update_tip();
break;

case InToolActionID::RMB:
case InToolActionID::CANCEL:
return ToolResponse::end();

default:;
}
}
return ToolResponse();
}

ToolMeasure::~ToolMeasure()
{
if (annotation) {
imp->get_canvas()->remove_annotation(annotation);
annotation = nullptr;
}
}
} // namespace horizon
42 changes: 42 additions & 0 deletions src/core/tools/tool_measure.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#pragma once
#include "core/tool.hpp"

namespace horizon {
class ToolMeasure : public ToolBase {
public:
using ToolBase::ToolBase;
ToolResponse begin(const ToolArgs &args) override;
ToolResponse update(const ToolArgs &args) override;
bool can_begin() override
{
return true;
}
bool is_specific() override
{
return false;
}
std::set<InToolActionID> get_actions() const override
{
using I = InToolActionID;
return {
I::LMB,
I::CANCEL,
I::RMB,
};
}
~ToolMeasure();

private:
class CanvasAnnotation *annotation = nullptr;

enum class State { FROM, TO, DONE };
State state = State::FROM;
void update_tip();

Coordi from;
Coordi to;

std::string s_from;
std::string s_to;
};
} // namespace horizon
4 changes: 4 additions & 0 deletions src/imp/action_catalog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,9 @@ const std::map<ActionToolID, ActionCatalogItem> action_catalog = {
{{ActionID::ROTATE_VIEW, ToolID::NONE},
{"Rotate view", ActionGroup::VIEW, ActionCatalogItem::AVAILABLE_IN_PACKAGE_AND_BOARD,
ActionCatalogItem::FLAGS_IN_TOOL}},

{{ActionID::TOOL, ToolID::MEASURE},
{"Measure", ActionGroup::GRAPHICS, ActionCatalogItem::AVAILABLE_LAYERED, ActionCatalogItem::FLAGS_DEFAULT}},
};

const std::vector<std::pair<ActionGroup, std::string>> action_group_catalog = {
Expand Down Expand Up @@ -1186,5 +1189,6 @@ const LutEnumStr<ToolID> tool_lut = {
TOOL_LUT_ITEM(DRAW_PLANE),
TOOL_LUT_ITEM(DRAW_KEEPOUT),
TOOL_LUT_ITEM(DRAG_POLYGON_EDGE),
TOOL_LUT_ITEM(MEASURE),
};
} // namespace horizon

0 comments on commit 478bc50

Please sign in to comment.