23 #include "switchIcon.rcd" 31 void SwitchIcon::setNumCases(
unsigned n)
33 if (n<2)
throw error(
"switches need at least two cases");
36 m_ports.emplace_back(make_shared<Port>(*
this));
38 for (
unsigned i=m_ports.size(); i<n+2; ++i)
39 m_ports.emplace_back(make_shared<InputPort>(*
this));
40 const float width=8*numCases();
41 if (width>iWidth()) iWidth(width);
42 if (width>iHeight()) iHeight(width);
45 unsigned SwitchIcon::switchValue()
const 47 const double x=m_ports[1]->value();
55 Units SwitchIcon::units(
bool check)
const 57 bool inputFound=
false;
59 for (
size_t i=2; i<m_ports.size(); ++i)
60 for (
auto w: m_ports[i]->wires())
61 if (check && inputFound)
63 auto tmp=w->units(check);
65 throw_error(
"incompatible units: "+tmp.str()+
"≠"+r.
str());
75 void SwitchIcon::draw(cairo_t* cairo)
const 80 const float width=m_width*z, height=m_height*z;
81 cairo_set_line_width(cairo,1);
82 cairo_rectangle(cairo,-0.5*width,-0.5*height,width,height);
85 const float w=
flipped? -width: width;
89 m_ports[0]->moveTo(x()+0.5*w, y());
92 m_ports[1]->moveTo(x(), y()-0.5*height-8);
93 const float dy=height/numCases();
94 float y1=-0.5*height+0.5*dy;
96 for (
size_t i=2; i<m_ports.size(); ++i, y1+=dy)
99 m_ports[i]->moveTo(x()+-0.5*w-o, y()+y1);
102 cairo_move_to(cairo,0.5*w, 0);
105 y1=-0.5*width+0.5*dy+switchValue()*dy;
107 catch (
const std::exception&)
109 y1=-0.5*width+0.5*dy;
111 cairo_line_to(cairo,-0.45*w,0.9*y1);
117 displayTooltip(cairo,tooltip());
118 if (onResizeHandles) drawResizeHandles(cairo);
122 cairo_rectangle(cairo,-0.5*width-8,-0.5*height-8,width+16,height+8);
124 if (selected) drawSelected(cairo);
#define M_PI
some useful geometry types, defined from boost::geometry
bool flipped(double rotation)
returns if the angle (in degrees) is in the second or third quadrant
Creation and access to the minskyTCL_obj object, which has code to record whenever Minsky's state cha...
represents the units (in sense of dimensional analysis) of a variable.
void drawTriangle(cairo_t *cairo, double x, double y, const ecolab::cairo::Colour &col, double angle=0)
CLASSDESC_ACCESS_EXPLICIT_INSTANTIATION(minsky::SwitchIcon)