/* ThingDirCanvas::draw
 * Draws the control
 *******************************************************************/
void ThingDirCanvas::draw()
{
	// Setup the viewport
	glViewport(0, 0, GetSize().x, GetSize().y);

	// Setup the screen projection
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(-1.2, 1.2, 1.2, -1.2, -1, 1);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	// Clear
	glClearColor(col_bg.fr(), col_bg.fg(), col_bg.fb(), 1.0f);
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

	// Draw angle ring
	glLineWidth(1.5f);
	glEnable(GL_LINE_SMOOTH);
	rgba_t col_faded(col_bg.r * 0.6 + col_fg.r * 0.4,
		col_bg.g * 0.6 + col_fg.g * 0.4,
		col_bg.b * 0.6 + col_fg.b * 0.4);
	Drawing::drawEllipse(fpoint2_t(0, 0), 1, 1, 48, col_faded);

	// Draw dir points
	for (unsigned a = 0; a < dir_points.size(); a++)
	{
		Drawing::drawFilledEllipse(dir_points[a], 0.12, 0.12, 8, col_bg);
		Drawing::drawEllipse(dir_points[a], 0.12, 0.12, 16, col_fg);
	}

	// Draw angle arrow
	glLineWidth(2.0f);
	fpoint2_t tip = MathStuff::rotatePoint(fpoint2_t(0, 0), fpoint2_t(0.8, 0), -angle);
	Drawing::drawArrow(tip, fpoint2_t(0, 0), col_fg, false, 1.2, 0.2);

	// Draw hover point
	glPointSize(8.0f);
	glEnable(GL_POINT_SMOOTH);
	if (point_hl >= 0 && point_hl < (int)dir_points.size())
	{
		OpenGL::setColour(col_faded);
		glBegin(GL_POINTS);
		glVertex2d(dir_points[point_hl].x, dir_points[point_hl].y);
		glEnd();
	}

	// Draw selected point
	if (point_sel >= 0 && point_sel < (int)dir_points.size())
	{
		OpenGL::setColour(col_fg);
		glBegin(GL_POINTS);
		glVertex2d(dir_points[point_sel].x, dir_points[point_sel].y);
		glEnd();
	}

	// Swap buffers (ie show what was drawn)
	SwapBuffers();
}
Example #2
0
/* Drawing::textExtents
 * Returns the width and height of [text] when drawn with [font]
 *******************************************************************/
fpoint2_t Drawing::textExtents(string text, int font)
{
	// Get desired font
	FTFont* ftgl_font = theFontManager->getFont(font);

	// If FTGL font is invalid, return empty
	if (!ftgl_font)
		return fpoint2_t(0,0);

	// Return width and height of text
	FTBBox bbox = ftgl_font->BBox(CHR(text), -1);
	return fpoint2_t(bbox.Upper().X() - bbox.Lower().X(), ftgl_font->LineHeight());
}
/* ThingDirCanvas::ThingDirCanvas
 * ThingDirCanvas class constructor
 *******************************************************************/
ThingDirCanvas::ThingDirCanvas(wxWindow* parent) : OGLCanvas(parent, -1, true, 15)
{
	// Init variables
	angle = 0;
	point_hl = -1;
	last_check = 0;
	point_sel = -1;

	// Get system panel background colour
	wxColour bgcolwx = Drawing::getPanelBGColour();
	col_bg.set(bgcolwx.Red(), bgcolwx.Green(), bgcolwx.Blue());

	// Get system text colour
	wxColour textcol = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
	col_fg.set(textcol.Red(), textcol.Green(), textcol.Blue());

	// Setup dir points
	double rot = 0;
	for (int a = 0; a < 8; a++)
	{
		dir_points.push_back(fpoint2_t(sin(rot), 0 - cos(rot)));
		rot -= (3.1415926535897932384626433832795 * 2) / 8.0;
	}
	
	// Bind Events
	Bind(wxEVT_MOTION, &ThingDirCanvas::onMouseEvent, this);
	Bind(wxEVT_LEAVE_WINDOW, &ThingDirCanvas::onMouseEvent, this);
	Bind(wxEVT_LEFT_DOWN, &ThingDirCanvas::onMouseEvent, this);

	// Fixed size
	SetInitialSize(wxSize(128, 128));
	SetMaxSize(wxSize(128, 128));
}
Example #4
0
void Polygon2D::updateTextureCoords(double scale_x, double scale_y, double offset_x, double offset_y, double rotation)
{
	// Can't do this if there is no texture
	if (!texture)
		return;

	// Check dimensions and scale
	double width = texture->getWidth();
	double height = texture->getHeight();
	if (scale_x == 0) scale_x = 1;
	if (scale_y == 0) scale_y = 1;
	if (width == 0) width = 1;
	if (height == 0) height = 1;

	// Get texture info
	double owidth = 1.0 / scale_x / width;
	double oheight = 1.0 / scale_y / height;

	// Set texture coordinates
	double x, y;
	for (unsigned p = 0; p < subpolys.size(); p++)
	{
		for (unsigned a = 0; a < subpolys[p]->n_vertices; a++)
		{
			x = subpolys[p]->vertices[a].x;
			y = subpolys[p]->vertices[a].y;

			// Apply rotation if any
			if (rotation != 0) {
				fpoint2_t np = MathStuff::rotatePoint(fpoint2_t(0, 0), fpoint2_t(x, y), rotation);
				x = np.x;
				y = np.y;
			}

			x = (scale_x*offset_x) + x;
			y = (scale_y*offset_y) - y;

			// Set texture coordinate for vertex
			subpolys[p]->vertices[a].tx = x * owidth;
			subpolys[p]->vertices[a].ty = y * oheight;
		}
	}

	// Update variables
	vbo_update = 1;
}
Example #5
0
int PolygonSplitter::findNextEdge(int edge, bool ignore_done, bool only_convex, bool ignore_inpoly)
{
	edge_t& e = edges[edge];
	vertex_t& v2 = vertices[e.v2];
	vertex_t& v1 = vertices[e.v1];

	// Go through all edges starting from the end of this one
	double min_angle = 2*PI;
	int next = -1;
	for (unsigned a = 0; a < v2.edges_out.size(); a++)
	{
		edge_t& out = edges[v2.edges_out[a]];

		// Ignore 'done' edges
		if (ignore_done && edges[v2.edges_out[a]].done)
			continue;

		// Ignore 'inpoly' edges
		if (ignore_inpoly && edges[v2.edges_out[a]].inpoly)
			continue;

		// Ignore edges on the reverse-side of this
		if (out.v1 == e.v2 && out.v2 == e.v1)
			continue;

		// Ignore invalid edges
		if (!out.ok)
			continue;

		// Determine angle between edges
		double angle = MathStuff::angle2DRad(fpoint2_t(v1.x, v1.y), fpoint2_t(v2.x, v2.y), fpoint2_t(vertices[out.v2].x, vertices[out.v2].y));
		if (angle < min_angle)
		{
			min_angle = angle;
			next = v2.edges_out[a];
		}
	}

	last_angle = min_angle;
	if (only_convex && min_angle > PI)
		return -1;
	else
		return next;
}
Example #6
0
/* MathStuff::rotatePoint
 * Rotates [point] around [origin] by [angle] and returns the newly
 * rotated point
 *******************************************************************/
fpoint2_t MathStuff::rotatePoint(fpoint2_t origin, fpoint2_t point, double angle)
{
	// Translate to origin
	double x = point.x - origin.x;
	double y = point.y - origin.y;

	// Maths yay
	double srot = sin(angle * deg2rad);
	double crot = cos(angle * deg2rad);
	double nx = crot * x - srot * y;
	double ny = srot * x + crot * y;

	// Return rotated point
	return fpoint2_t(nx + origin.x, ny + origin.y);
}
Example #7
0
fpoint2_t MathStuff::rotatePoint(fpoint2_t origin, fpoint2_t point, double angle)
{
	// Translate to origin
	double x = point.x - origin.x;
	double y = point.y - origin.y;

	// Maths yay
	double rot = (PI * 2.0) * ((360.0 - angle) / 360.0);
	double srot = sin(rot);
	double crot = cos(rot);
	x = crot * x - srot * y;
	y = srot * x + crot * y;

	// Return rotated point
	return fpoint2_t(x + origin.x, y + origin.y);
}
Example #8
0
/* MapLine::dirTabPoint
 * Calculates and returns the end point of the 'direction tab' for
 * the line (used as a front side indicator for 2d map display)
 *******************************************************************/
fpoint2_t MapLine::dirTabPoint(double tablen)
{
	// Calculate midpoint
	fpoint2_t mid(x1() + ((x2() - x1()) * 0.5), y1() + ((y2() - y1()) * 0.5));

	// Calculate tab length
	if (tablen == 0)
	{
		tablen = getLength() * 0.1;
		if (tablen > 16) tablen = 16;
		if (tablen < 2) tablen = 2;
	}

	// Calculate tab endpoint
	if (front_vec.x == 0 && front_vec.y == 0) frontVector();
	return fpoint2_t(mid.x - front_vec.x*tablen, mid.y - front_vec.y*tablen);
}
Example #9
0
/* MCAVertexSelection::MCAVertexSelection
 * MCAVertexSelection class constructor
 *******************************************************************/
MCAVertexSelection::MCAVertexSelection(long start, vector<MapVertex*>& verts, double size, bool select) : MCAnimation(start)
{
	// Init variables
	this->size = size;
	this->select = select;
	this->fade = 1.0f;

	// Setup vertices list
	for (unsigned a = 0; a < verts.size(); a++)
	{
		if (!verts[a]) continue;
		vertices.push_back(fpoint2_t(verts[a]->xPos(), verts[a]->yPos()));
	}

	if (!select)
		this->size = size * 1.8f;
}
Example #10
0
/* Drawing::textExtents
 * Returns the width and height of [text] when drawn with [font]
 *******************************************************************/
fpoint2_t Drawing::textExtents(string text, int font)
{
	// Setup SFML string
	sf::Text sf_str;
	sf_str.setString(CHR(text));

	// Set font
	sf::Font* f = theFontManager->getFont(font);
	sf_str.setFont(*f);
	if (font == FONT_SMALL)
		sf_str.setCharacterSize((gl_font_size * 0.6) + 1);
	else
		sf_str.setCharacterSize(gl_font_size);

	// Return width and height of text
	sf::FloatRect rect = sf_str.getGlobalBounds();
	return fpoint2_t(rect.width, rect.height);
}
Example #11
0
fpoint2_t MathStuff::closestPointOnLine(double x, double y, double x1, double y1, double x2, double y2)
{
	// Get line length
	double len = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));

	// Calculate intersection distance
	double u = 0;
	if (len > 0)
	{
		u = ((x-x1)*(x2-x1) + (y-y1)*(y2-y1)) / (len*len);

		// Limit intersection distance to the line
		double lbound = 1 / len;
		if(u < lbound) u = lbound;
		if(u > (1.0-lbound)) u = 1.0-lbound;
	}

	// Return intersection point
	return fpoint2_t(x1 + u*(x2 - x1), y1 + u*(y2 - y1));
}
Example #12
0
/* MathStuff::closestPointOnLine
 * Returns the point on the given line that's closest to the given
 * point
 *******************************************************************/
fpoint2_t MathStuff::closestPointOnLine(fpoint2_t point, fseg2_t line)
{
	// Get line length
	double len = line.length();

	// Calculate intersection distance
	double u = 0;
	if (len > 0)
	{
		u = ((point.x - line.x1()) * line.width() + (point.y-line.y1()) * line.height()) / (len * len);

		// Limit intersection distance to the line
		double lbound = 1 / len;
		if(u < lbound) u = lbound;
		if(u > (1.0-lbound)) u = 1.0-lbound;
	}

	// Return intersection point
	return fpoint2_t(line.x1() + u*line.width(), line.y1() + u*line.height());
}
Example #13
0
fpoint2_t MathStuff::vectorAngle(double angle_rad)
{
	return fpoint2_t(cos(-angle_rad), -sin(-angle_rad));
}
Example #14
0
/* MapThing::getPoint
 * Returns the object point [point]. Currently for things this is
 * always the thing position
 *******************************************************************/
fpoint2_t MapThing::getPoint(uint8_t point)
{
	return fpoint2_t(x, y);
}