/**************************************************************************** EDITSS: A shipshape editor for XPilot shipshapes V2.1 Copyright (C) 1994 Ronald In 't Velt This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. Right now, it is impossible to reach me via E-Mail. When I will have an account again somewhere, I will post a notice in alt.games.xpilot (or rec.games.xpilot in case this newgroup is accepted into rec.). Ship shape to Postscript converter - (C) Steven Singer 1994 - freely distributable. The routine a_psout is an adaptation of this program. Changes and incorporation into editss with permission from the author. ACTIONS.C: Actions / Callbacks ****************************************************************************/ #include /* usual include stuff */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "const.h" /* editss stuff */ #include "types.h" #include "vars.h" #include "editss.h" #include "xstuff.h" #include "actions.h" #include "pship.h" int draglg[MAXGUNS], dragrg[MAXGUNS]; int dragll[MAXLIGHTS], dragrl[MAXLIGHTS]; int dragmr[MAXRACK], dragany=NONE; int ytoload = -1; /****************************************************************************/ /* Handle keypresses */ void a_key(Widget w, XtPointer cld, XEvent *xev) { char buffer[50], *s, *ts; KeySym k; XComposeStatus c; if (w==b_shipname) ts=shipname; else if (w==b_shipaut) ts=shipaut; else ts=shiplib; XtVaGetValues(w, XtNstring, &s, NULL); XLookupString((XKeyEvent *)xev,buffer,50,&k,&c); strncpy(ts,s,49); killcr(ts); if (k==XK_Return) { ts[49]='\0'; XtVaSetValues(w, XtNstring, ts, NULL); if (w==b_shiplib) getdir(); } } /* exit now */ void a_quit(Widget w, XtPointer cld, XtPointer cad) { exit(0); } void a_psout(Widget w, XtPointer cld, XtPointer cad) { char outname[MAXLINELEN], *p; FILE *fout; int x, y, n, page; char shipdef[MAXSHIPLEN]; shiplist *slp; getshiplib(); p = strrchr(shiplib,'/'); if (p!=NULL) p++; if ((p==NULL)||(*p=='\0')) p = shiplib; strcpy(outname, p); strcat(outname,".ps"); fout = fopen(outname, "w"); if (fout==NULL) { XtVaSetValues(l_sizeok, XtNlabel, "Cannout open PS outfile!", NULL); return; } fprintf(fout,"%%!PS-Adobe-2.0\n\ %%%%Title: shipshapes\n\ %%%%Creator: shipeshape2ps\n\ %%%%Pages: atend\n\ %%%%DocumentFonts: Helvetica\n\ %%%%EndComments\n\ userdict begin\n\ /Helvetica findfont 8 scalefont setfont\n\ /ship {moveto 1 exch 1 exch {pop lineto} for closepath stroke\n\ moveto dup stringwidth pop -2 div 0 rmoveto show \ moveto dup stringwidth pop -2 div 0 rmoveto show} def end\n\ %%%%EndProlog\n\ %%%%Page: 1\n"); y = YSIZE-BORDER-SPACING/2; x = BORDER - SPACING/2; page = 1; slp = sl; while (slp!=NULL) { if ((slp->s)&&(slp->s->n>0)&&(slp->s->n<=MAXPOINTS)) { if ((x += SPACING) > XSIZE-SPACING/2) { x = BORDER+SPACING/2; if ((y -= SPACING) < BORDER+SPACING/2) { y = YSIZE-BORDER-SPACING/2; page++; fprintf(fout,"showpage\n%%%%Page: %d\n", page); } } fprintf(fout,"(%s) %d %d\n",slp->s->name, x, y-TITLEPOS); fprintf(fout,"(%s) %d %d\n",slp->s->author, x, y-TITLEPOS-10); for (n=0; ns->n; n++) { if (n == slp->s->n-1) fprintf(fout,"%d ", slp->s->n-1); fprintf(fout,"%d ", slp->s->p[n].x*SCALE+x); fprintf(fout,"%d ", slp->s->p[n].y*SCALE+y); } fprintf(fout,"ship\n"); } slp = slp->n; } fprintf(fout,"showpage\n%%%%Trailer\n%%%%Pages: %d\n", page); XtSetSensitive(b_psout, FALSE); fclose(fout); } void update(void) { sh_ed.version=0x3200; } /* save or print the ship */ void a_dump(Widget w, XtPointer cld, XtPointer cad) { int i, j; char a[MAXSHIPLEN], b[MAXLINELEN], addnp[30],*s; char ship[MAXSHIPLEN]; FILE *fout; shiplist *slp; getshiplib(); ship[0]='\0'; getshipname(); getshipaut(); Convert_ship_2_string(&sh_ed, ship, sh_ed.version); if (w!=b_print) { getshipname(); s=shipname; killcr(s); if (strlen(s)==0) { XtVaSetValues(l_sizeok, XtNlabel, "Name the ship first!", NULL); return; } b[0]='\0'; strcpy(b,s); slp = sl; while ((slp!=NULL)&&(strcmp(slp->s->name,b))) slp=slp->n; if (slp!=NULL) { XtVaSetValues(l_sizeok, XtNlabel, "Ship already exists!", NULL); return; } a[0]='\0'; b[0]='\0'; strcpy(b,userpath()); strcat(b,"/"); if (w==b_write) strcat(b,shiplib); else strcat(b,".xpilotrc"); fout=fopen(b,"a"); } else { if (callfrom) { getshipname(); slp = sl; while ((slp!=NULL)&&(strcmp(slp->s->name,shipname))) slp=slp->n; if ((strlen(shipname)>0)&&(slp!=NULL)) { printf("*%s:%s\n",shiplib,slp->s->name); exit(0); } else XtVaSetValues(l_sizeok, XtNlabel, "Ship doesn't exist!", NULL); return; } fout=NULL; } getshipname(); s=shipname; killcr(s); addnp[0]='\0'; if (fout) { if (rcformat) fprintf(fout,"xpilot.shipShape: %s\n",ship); else fprintf(fout,"%s\n",ship); } else if (w!=b_write) { printf("\n%s\n",ship); } if (fout) { fclose(fout); getdir(); } else { printf("\n"); } if (fout) fclose(fout); } /* Handle main window resize */ void a_resize(Widget w, XtPointer cld, XEvent *xev) { Dimension x,y,x1, nx, ny; XtVaGetValues(mainw, XtNwidth, &x, XtNheight, &y, NULL); XtVaGetValues(b_box, XtNwidth, &x1, NULL); nx=x-20-x1-SPM_SIZE-14; ny=y-10; if (nx>PM_MAX) nx=PM_MAX; if (nxny) nx=ny; else ny=nx; pm_area_w=nx; pm_area_h=ny; y-=10; XtResizeWidget(shipview, SPM_SIZE+14, y, 1); if (SPM_SIZE*nrships>y) y=SPM_SIZE*nrships; XtResizeWidget(shipbm, SPM_SIZE, y, 0); XtResizeWidget(area,nx,ny,1); redrawship(); } void drawsel(point p, int s) { if (s==NONE) return; if ((s&MG)||(s&LG)||(s&RG)) arrow(p, &gc_xor); if (s&EN) square(p, &gc_xor); if (s&MR) circle(p, &gc_xor); if ((s&LL)||(s&RL)) cross(p, &gc_xor); } /* Track mouse in the drawing area */ void a_motion(Widget w, XtPointer cld, XEvent *xev) { int x, y, pf, pt, sise=FALSE; point p, po; x=rx(xev->xmotion.x); y=ry(xev->xmotion.y); p.x=x; p.y=y; po.x=xo; po.y=yo; if (drawmode==DM_TWORUBBER) { if (dragpoint-1<0) pf=sh_ed.n-1; else pf=dragpoint-1; if (dragpoint+1>=sh_ed.n) pt=0; else pt=dragpoint+1; if (dragmode==DM_NEW) pt=pf; if ((xo!=x)||(yo!=y)) { if ((sh_ed.p[pf].x==sh_ed.p[pt].x)&&(sh_ed.p[pf].y==sh_ed.p[pt].y)) sise=TRUE; if ((xo!=-99)&&(yo!=-99)) { XDrawLine(XtDisplay(toplevel),pm_area,gc_xor,cx(sh_ed.p[pf].x),cy(sh_ed.p[pf].y),cx(xo),cy(yo)); if (!sise) XDrawLine(XtDisplay(toplevel),pm_area,gc_xor,cx(sh_ed.p[pt].x),cy(sh_ed.p[pt].y),cx(xo),cy(yo)); drawsel(po, dragany); } XDrawLine(XtDisplay(toplevel),pm_area,gc_xor,cx(sh_ed.p[pf].x),cy(sh_ed.p[pf].y),cx(x),cy(y)); if (!sise) XDrawLine(XtDisplay(toplevel),pm_area,gc_xor,cx(sh_ed.p[pt].x),cy(sh_ed.p[pt].y),cx(x),cy(y)); drawsel(p, dragany); a_redrawarea(NULL, NULL, NULL, NULL); } xo=x; yo=y; } else if ((dragany!=NONE)&&(drawmode==DM_DRAG)) { if ((xo!=x)||(yo!=y)) { if ((xo!=-99)&&(yo!=-99)) { drawsel(po, dragany); } drawsel(p, dragany); a_redrawarea(NULL, NULL, NULL, NULL); } xo=x; yo=y; } } /* Handle expose event */ void a_redrawarea(Widget w, XEvent *xev, String *p, Cardinal *np) { XCopyArea(XtDisplay(area), pm_area, XtWindow(area), DefaultGCOfScreen(XtScreen(toplevel)), 0, 0, pm_area_w, pm_area_h, 0,0); } /* Handle expose event */ void a_redrawdir(Widget w, XEvent *xev, String *p, Cardinal *np) { XCopyArea(XtDisplay(shipbm), pm_ships, XtWindow(shipbm), DefaultGCOfScreen(XtScreen(toplevel)), 0, 0, SPM_SIZE, SPM_SIZE*nrships, 0,0); } /* Handle expose event */ void a_redrawsmall(Widget w, XEvent *xev, String *p, Cardinal *np) { XCopyArea(XtDisplay(small), pm_small, XtWindow(small), DefaultGCOfScreen(XtScreen(toplevel)), 0, 0, SPM_SIZE, SPM_SIZE, 0,0); } /* Change draw mode, set pointer shape */ void a_drawmode(Widget w, XtPointer cld, XtPointer cad) { char c, *cp; cp=(char *)XawToggleGetCurrent(b_draw); if (cp==NULL) c='*'; else c=*cp; switch (c) { case 'K': { XDefineCursor(XtDisplay(area),XtWindow(area),cur_kill); break; } case 'D': { XUndefineCursor(XtDisplay(area),XtWindow(area)); break; } case 'S': { XDefineCursor(XtDisplay(area),XtWindow(area),cur_split); break; } case '7': { XDefineCursor(XtDisplay(area),XtWindow(area),cur_nose); break; } case '8': { XDefineCursor(XtDisplay(area),XtWindow(area),cur_nose); break; } case '9': { XDefineCursor(XtDisplay(area),XtWindow(area),cur_nose); break; } case '1': { XDefineCursor(XtDisplay(area),XtWindow(area),cur_rear); break; } case '3': { XDefineCursor(XtDisplay(area),XtWindow(area),cur_rear); break; } case '5': { XDefineCursor(XtDisplay(area),XtWindow(area),cur_mis); break; } case '2': { XDefineCursor(XtDisplay(area),XtWindow(area),cur_eng); break; } } } /* Handle buttonpress in dirwindow */ void a_buttondir(Widget w, XEvent *xev, String *p, Cardinal *np) { Position y; char a[MAXSHIPLEN], b[MAXLINELEN]; int i; shiplist *slp; y=(xev->xmotion.y/SPM_SIZE); if (y>=nrships) return; if (abs(xev->xmotion.time-tl)<300) { if (xev->xbutton.button==Button1) { slp = sl; for (i=0; in; } if (slp!=NULL) { strcpy(shipname,slp->s->name); XtVaSetValues(b_shipname, XtNstring, shipname, NULL); ytoload = y; a_load(NULL, NULL, NULL); } } } tl=xev->xmotion.time; } int findspec(int x, int y, int s) { int r=0, i; for (i=0; i0)) return r; } for (i=0; i0)) return r; } for (i=0; i0)) return r; } for (i=0; i0)) return r; } if ((sh_ed.mg.x==x)&&(sh_ed.mg.y==y)) r+=MG; if ((s)&&(r>0)) return r; if ((sh_ed.en.x==x)&&(sh_ed.en.y==y)) r+=EN; if ((s)&&(r>0)) return r; for (i=0; i0)) return r; } if (r==0) r=NONE; return r; } void placespec(int x, int y, int d) { int i; if (d==NONE) return; if (d&MG) { sh_ed.mg.x=x; sh_ed.mg.y=y; } if (d&EN) { sh_ed.en.x=x; sh_ed.en.y=y; } if (d&MR) for (i=0; ixmotion.x); y=ry(xev->xmotion.y); cp=(char *)XawToggleGetCurrent(b_draw); if (cp==NULL) c='*'; else c=*cp; if (w!=area) return; switch (xev->xbutton.button) { case Button1: { /* break / insert */ switch (drawmode) { case DM_NONE: { /* Start break */ dragany=NONE; if (c=='D') { if (sh_ed.n==0) { /* new point */ sh_ed.n=1; dragpoint=1; sh_ed.p[0].x=x; sh_ed.p[0].y=y; sh_ed.p[1].x=x; sh_ed.p[1].y=y; xo=-99; yo=-99; drawmode=DM_TWORUBBER; dragmode=DM_NEW; } else { /* grab existing point */ dragpoint=-1; for (i=0; ixmotion.x,xev->xmotion.y); if (dd!=-1) { for (i=sh_ed.n; i>dd; i--) { sh_ed.p[i].x=sh_ed.p[i-1].x; sh_ed.p[i].y=sh_ed.p[i-1].y; } sh_ed.n+=1; showvalues(); dragpoint=dd+1; drawmode=DM_TWORUBBER; dragmode=DM_DRAG; xo=-99; yo=-99; rmvline(dragpoint, &gc_clear); rmvline(dragpoint-1, &gc_clear); a_redrawarea(NULL, NULL, NULL, NULL); } } } if (c=='8') { XawToggleSetCurrent(b_draw,&dr_val[0]); sh_ed.mg.x=x; sh_ed.mg.y=y; } if (c=='2') { XawToggleSetCurrent(b_draw,&dr_val[0]); sh_ed.en.x=x; sh_ed.en.y=y; } if (c=='7') { if (sh_ed.nlg=MAXGUNS) XawToggleSetCurrent(b_draw,&dr_val[0]); } if (c=='9') { if (sh_ed.nrg=MAXGUNS) XawToggleSetCurrent(b_draw,&dr_val[0]); } if (c=='1') { if (sh_ed.nll=MAXLIGHTS) XawToggleSetCurrent(b_draw,&dr_val[0]); } if (c=='3') { if (sh_ed.nrl=MAXLIGHTS) XawToggleSetCurrent(b_draw,&dr_val[0]); } if (c=='5') { if (sh_ed.nmr=MAXRACK) XawToggleSetCurrent(b_draw,&dr_val[0]); } if ((sh_ed.nmr>1)||(sh_ed.nlg>0)||(sh_ed.nrg>0) ||(sh_ed.nll>1)||(sh_ed.nrl>1)||(sh_ed.n>16)) update(); break; } case DM_TWORUBBER: { /* place point */ if ((sh_ed.p[dragpoint].x!=x)||(sh_ed.p[dragpoint].y!=y)) { sh_ed.p[dragpoint].x=x; sh_ed.p[dragpoint].y=y; if ((dragmode==DM_NEW)&&(sh_ed.n1) rmvline(dragpoint,&gc_xor); sh_ed.n+=1; dragpoint+=1; xo=-99; yo=-99; sh_ed.p[dragpoint].x=x; sh_ed.p[dragpoint].y=y; if (sh_ed.n==MAXPOINTS) { drawmode=DM_NONE; showvalues(); } else redrawship(); } else { drawmode=DM_NONE; placespec(x,y,dragany); dragany=NONE; showvalues(); } } else if (dragmode==DM_DRAG) { drawmode=DM_NONE; dragany=NONE; showvalues(); } break; } case DM_DRAG: { placespec(x,y,dragany); drawmode=DM_NONE; dragany=NONE; break; } } break; } default: { switch (drawmode) { case DM_TWORUBBER:{ if (sh_ed.n==1) sh_ed.n=0; drawmode=DM_NONE; dragany=NONE; showvalues(); break; } case DM_NONE: { if (c=='N') XawToggleSetCurrent(b_draw,&dr_val[0]); if (c=='K') { killspec(x,y); } if (c=='D') { dragany=findspec(x, y, TRUE); if (dragany!=NONE) { drawmode=DM_DRAG; xo=-99; yo=-99; } } break; } case DM_DRAG: { dragany=NONE; drawmode=DM_NONE; break; } } break; } } if (drawmode==DM_NONE) { redrawship(); if ((c=='D')||(c=='*')) XUndefineCursor(XtDisplay(area),XtWindow(area)); } else XDefineCursor(XtDisplay(area),XtWindow(area),cur_draw); } /* load a ship */ void a_load(Widget w, XtPointer cld, XtPointer cad) { char a[MAXLINELEN], b[MAXSHIPLEN], *h, *name; ship *newship; shiplist *slp; int i; if (ytoload>=0) { slp = sl; a[0]='\0'; for (i=0; in; } } else { getshipname(); name=shipname; if (strlen(name)==0) name=NULL; a[0]='\0'; b[0]='\0'; if (name) strcpy(a,name); else return; slp = sl; while ((slp!=NULL)&&(strcmp(slp->s->name,a))) slp=slp->n; } ytoload = -1; if (slp) { cpyship(&sh_ed, slp->s); strcpy(shipname,sh_ed.name); strcpy(shipaut, sh_ed.author); XtVaSetValues(b_shipname, XtNstring, shipname, NULL); XtVaSetValues(b_shipaut, XtNstring, shipaut, NULL); redrawship(); } else XtVaSetValues(l_sizeok, XtNlabel, "Ship doesn't exist!", NULL); } /* clear current ship */ void a_clear(Widget w, XtPointer cld, XtPointer cad) { cleanship(&sh_ed); showvalues(); shipname[0]='\0'; XtVaSetValues(b_shipname, XtNstring, shipname, NULL); redrawship(); }