This repository has been archived on 2025-03-02. You can view files and clone it, but cannot push or open issues or pull requests.
neofx-zoomedit/draw.c
2007-06-24 00:35:04 +00:00

140 lines
3 KiB
C

#include "draw.h"
#include "level.h"
#include "geometry.h"
#include <math.h>
#include <gtk/gtk.h>
#include <cairo/cairo.h>
static double scale = 100.0;
static double xTranslate = 0.0, yTranslate = 0.0;
static void drawGrid(cairo_t *cr, const RECTANGLE *rect) {
double depth = log10(scale)-0.75;
double depth2 = floor(depth);
double step = pow(0.1, depth2);
double x;
int i;
cairo_set_source_rgb(cr, 0.2*(depth-depth2), 0.2*(depth-depth2), 0.2*(depth-depth2));
for(i = 0; i < 3; i++) {
for(x = rect->x - fmod(rect->x, step); x <= rect->x+rect->width; x+=step) {
cairo_move_to(cr, x, rect->y);
cairo_line_to(cr, x, rect->y+rect->height);
cairo_stroke(cr);
}
for(x = rect->y - fmod(rect->y, step); x <= rect->y+rect->height; x+=step) {
cairo_move_to(cr, rect->x, x);
cairo_line_to(cr, rect->x+rect->width, x);
cairo_stroke(cr);
}
step *= 10;
if(i == 0 && 0.2*(depth-depth2+1) < 0.3)
cairo_set_source_rgb(cr, 0.2*(depth-depth2+1), 0.2*(depth-depth2+1), 0.2*(depth-depth2+1));
else
cairo_set_source_rgb(cr, 0.3, 0.3, 0.3);
}
}
static void room2path(cairo_t *cr, const ROOM *room, const RECTANGLE *rect) {
int i;
ROOM room2 = {0, NULL};
// no vertices
if(room->nVertices == 0) return;
if(rect)
simplifyPolygon(room, rect, &room2);
else
room2 = *room;
if(room2.nVertices == 0) return;
cairo_new_sub_path(cr);
for(i = 0; i < room2.nVertices; i++) {
cairo_line_to(cr, room2.vertices[i].x, room2.vertices[i].y);
}
cairo_close_path(cr);
if(rect && room2.vertices)
free(room2.vertices);
}
gboolean drawTopView(GtkWidget *widget, GdkEventExpose *event, gpointer data) {
VERTEX vertices[4] = {{-1,-1}, {-1,1}, {1,1}, {1,-1}};
ROOM room = {sizeof(vertices)/sizeof(vertices[0]), vertices};
LEVEL lvl = {1, &room};
cairo_t *cr;
RECTANGLE rect = {-5, -5, widget->allocation.width+10, widget->allocation.height+10};
int i;
cr = gdk_cairo_create(widget->window);
gdk_cairo_region(cr, event->region);
cairo_clip(cr);
cairo_translate(cr, getImageWidth()/2-xTranslate, getImageHeight()/2-yTranslate);
cairo_scale(cr, scale, scale);
cairo_device_to_user(cr, &rect.x, &rect.y);
cairo_device_to_user_distance(cr, &rect.width, &rect.height);
cairo_set_line_width(cr, 1.0/scale);
cairo_set_line_join(cr, CAIRO_LINE_JOIN_MITER);
drawGrid(cr, &rect);
for(i = 0; i < lvl.nRooms; i++)
room2path(cr, &lvl.rooms[i], &rect);
cairo_set_source_rgba(cr, 0.0, 0.7, 1.0, 0.3);
cairo_fill_preserve(cr);
cairo_set_source_rgba(cr, 0.0, 0.7, 1.0, 0.7);
cairo_stroke(cr);
cairo_destroy (cr);
return FALSE;
}
double getScale() {
return scale;
}
void setScale(double s) {
scale = s;
}
double getImageWidth() {
return 5*scale;
}
double getImageHeight() {
return 5*scale;
}
double getXTranslate() {
return xTranslate;
}
void setXTranslate(double x) {
xTranslate = x;
}
double getYTranslate() {
return yTranslate;
}
void setYTranslate(double y) {
yTranslate = y;
}