summaryrefslogtreecommitdiff
path: root/app/bin/cswitchmotor.c
diff options
context:
space:
mode:
Diffstat (limited to 'app/bin/cswitchmotor.c')
-rw-r--r--app/bin/cswitchmotor.c348
1 files changed, 301 insertions, 47 deletions
diff --git a/app/bin/cswitchmotor.c b/app/bin/cswitchmotor.c
index 7b948a8..dbe006c 100644
--- a/app/bin/cswitchmotor.c
+++ b/app/bin/cswitchmotor.c
@@ -1,6 +1,8 @@
/** \file cswitchmotor.c
* Switch Motors
- * Created by Robert Heller on Sat Mar 14 10:39:56 2009
+ */
+
+/* Created by Robert Heller on Sat Mar 14 10:39:56 2009
* ------------------------------------------------------------------
* Modification History: $Log: not supported by cvs2svn $
* Modification History: Revision 1.5 2009/11/23 19:46:16 rheller
@@ -49,16 +51,15 @@
#include <ctype.h>
#include "track.h"
+#include "trackx.h"
#include "compound.h"
#include "i18n.h"
EXPORT TRKTYP_T T_SWITCHMOTOR = -1;
-#define SWITCHMOTORCMD
-
static int log_switchmotor = 0;
-#ifdef SWITCHMOTORCMD
+
static drawCmd_t switchmotorD = {
NULL,
&screenDrawFuncs,
@@ -82,19 +83,41 @@ static paramData_t switchmotorPLs[] = {
};
static paramGroup_t switchmotorPG = { "switchmotor", 0, switchmotorPLs, sizeof switchmotorPLs/sizeof switchmotorPLs[0] };
+static wWin_p switchmotorW;
+
+static char switchmotorEditName[STR_SHORT_SIZE];
+static char switchmotorEditNormal[STR_LONG_SIZE];
+static char switchmotorEditReverse[STR_LONG_SIZE];
+static char switchmotorEditPointSense[STR_LONG_SIZE];
+static long int switchmotorEditTonum;
+static track_p switchmotorEditTrack;
+
+static paramIntegerRange_t r0_999999 = { 0, 999999 };
+
+static paramData_t switchmotorEditPLs[] = {
+/*0*/ { PD_STRING, switchmotorEditName, "name", PDO_NOPREF, (void*)200, N_("Name") },
+/*1*/ { PD_STRING, switchmotorEditNormal, "normal", PDO_NOPREF, (void*)350, N_("Normal") },
+/*2*/ { PD_STRING, switchmotorEditReverse, "reverse", PDO_NOPREF, (void*)350, N_("Reverse") },
+/*3*/ { PD_STRING, switchmotorEditPointSense, "pointSense", PDO_NOPREF, (void*)350, N_("Point Sense") },
+/*4*/ { PD_LONG, &switchmotorEditTonum, "turnoutNumber", PDO_NOPREF, &r0_999999, N_("Turnout Number"), BO_READONLY },
+};
+
+static paramGroup_t switchmotorEditPG = { "switchmotorEdit", 0, switchmotorEditPLs, sizeof switchmotorEditPLs/sizeof switchmotorEditPLs[0] };
+static wWin_p switchmotorEditW;
+
/*
static dynArr_t switchmotorTrk_da;
#define switchmotorTrk(N) DYNARR_N( track_p , switchmotorTrk_da, N )
*/
-static wWin_p switchmotorW;
-#endif
typedef struct switchmotorData_t {
- char * name;
- char * normal;
- char * reverse;
- char * pointsense;
- track_p turnout;
+ char * name;
+ char * normal;
+ char * reverse;
+ char * pointsense;
+ BOOL_T IsHilite;
+ TRKINX_T turnindx;
+ track_p turnout;
} switchmotorData_t, *switchmotorData_p;
static switchmotorData_p GetswitchmotorData ( track_p trk )
@@ -102,27 +125,75 @@ static switchmotorData_p GetswitchmotorData ( track_p trk )
return (switchmotorData_p) GetTrkExtraData(trk);
}
+#if 0
#include "bitmaps/switchmotormark.xbm"
static wDrawBitMap_p switchmotormark_bm = NULL;
+#endif
+
+static coOrd switchmotorPoly_Pix[] = {
+ {6,0}, {6,13}, {4,13}, {4,19}, {6,19}, {6,23}, {9,23}, {9,19}, {13,19},
+ {13,23}, {27,23}, {27,10}, {13,10}, {13,13}, {9,13}, {9,0}, {6,0} };
+#define switchmotorPoly_CNT (sizeof(switchmotorPoly_Pix)/sizeof(switchmotorPoly_Pix[0]))
+#define switchmotorPoly_SF (3.0)
+static void ComputeSwitchMotorBoundingBox (track_p t)
+{
+ coOrd hi, lo, p;
+ switchmotorData_p data_p = GetswitchmotorData(t);
+ struct extraData *xx = GetTrkExtraData(data_p->turnout);
+ coOrd orig = xx->orig;
+ ANGLE_T angle = xx->angle;
+ SCALEINX_T s = GetTrkScale(data_p->turnout);
+ DIST_T scaleRatio = GetScaleRatio(s);
+ int iPoint;
+ ANGLE_T x_angle, y_angle;
+
+ x_angle = 90-(360-angle);
+ if (x_angle < 0) x_angle += 360;
+ y_angle = -(360-angle);
+ if (y_angle < 0) y_angle += 360;
+
+
+ for (iPoint = 0; iPoint < switchmotorPoly_CNT; iPoint++) {
+ Translate (&p, orig, x_angle, switchmotorPoly_Pix[iPoint].x * switchmotorPoly_SF / scaleRatio );
+ Translate (&p, p, y_angle, (10+switchmotorPoly_Pix[iPoint].y) * switchmotorPoly_SF / scaleRatio );
+ if (iPoint == 0) {
+ lo = p;
+ hi = p;
+ } else {
+ if (p.x < lo.x) lo.x = p.x;
+ if (p.y < lo.y) lo.y = p.y;
+ if (p.x > hi.x) hi.x = p.x;
+ if (p.y > hi.y) hi.y = p.y;
+ }
+ }
+ SetBoundingBox(t, hi, lo);
+}
+
+
static void DrawSwitchMotor (track_p t, drawCmd_p d, wDrawColor color )
{
- coOrd p;
- switchmotorData_p data_p = GetswitchmotorData(t);
- struct extraData *xx = GetTrkExtraData(data_p->turnout);
- coOrd orig = xx->orig;
- ANGLE_T angle = xx->angle;
-
- if (switchmotormark_bm == NULL) {
- switchmotormark_bm =
- wDrawBitMapCreate( mainD.d,
- switchmotormark_width,
- switchmotormark_height, 16, 16,
- switchmotormark_bits);
- }
- Translate (&p, orig, -angle , 2 );
- Translate (&p, p, 90-angle, 2);
- DrawBitMap(d, p, switchmotormark_bm, color);
+ coOrd p[switchmotorPoly_CNT];
+ switchmotorData_p data_p = GetswitchmotorData(t);
+ struct extraData *xx = GetTrkExtraData(data_p->turnout);
+ coOrd orig = xx->orig;
+ ANGLE_T angle = xx->angle;
+ SCALEINX_T s = GetTrkScale(data_p->turnout);
+ DIST_T scaleRatio = GetScaleRatio(s);
+ int iPoint;
+ ANGLE_T x_angle, y_angle;
+
+ x_angle = 90-(360-angle);
+ if (x_angle < 0) x_angle += 360;
+ y_angle = -(360-angle);
+ if (y_angle < 0) y_angle += 360;
+
+
+ for (iPoint = 0; iPoint < switchmotorPoly_CNT; iPoint++) {
+ Translate (&p[iPoint], orig, x_angle, switchmotorPoly_Pix[iPoint].x * switchmotorPoly_SF / scaleRatio );
+ Translate (&p[iPoint], p[iPoint], y_angle, (10+switchmotorPoly_Pix[iPoint].y) * switchmotorPoly_SF / scaleRatio );
+ }
+ DrawFillPoly(d, switchmotorPoly_CNT, p, wDrawColorBlack);
}
static struct {
@@ -199,6 +270,7 @@ static void UpdateSwitchMotor (track_p trk, int inx, descData_p descUpd, BOOL_T
static DIST_T DistanceSwitchMotor (track_p t, coOrd * p )
{
switchmotorData_p xx = GetswitchmotorData(t);
+ if (xx->turnout == NULL) return 0;
return GetTrkDistance(xx->turnout,*p);
}
@@ -227,7 +299,8 @@ static void DescribeSwitchMotor (track_p trk, char * str, CSIZE_T len )
switchmotorData.reverse[STR_LONG_SIZE-1] = '\0';
strncpy(switchmotorData.pointsense,xx->pointsense,STR_LONG_SIZE-1);
switchmotorData.pointsense[STR_LONG_SIZE-1] = '\0';
- switchmotorData.turnout = GetTrkIndex(xx->turnout);
+ if (xx->turnout == NULL) switchmotorData.turnout = 0;
+ else switchmotorData.turnout = GetTrkIndex(xx->turnout);
switchmotorDesc[TO].mode = DESC_RO;
switchmotorDesc[NM].mode =
switchmotorDesc[NOR].mode =
@@ -245,13 +318,20 @@ static void switchmotorDebug (track_p trk)
LOG( log_switchmotor, 1, ("*** switchmotorDebug(): normal = \"%s\"\n",xx->normal))
LOG( log_switchmotor, 1, ("*** switchmotorDebug(): reverse = \"%s\"\n",xx->reverse))
LOG( log_switchmotor, 1, ("*** switchmotorDebug(): pointsense = \"%s\"\n",xx->pointsense))
- LOG( log_switchmotor, 1, ("*** switchmotorDebug(): turnout = T%d, %s\n",
- GetTrkIndex(xx->turnout), GetTrkTypeName(xx->turnout)))
+ LOG( log_switchmotor, 1, ("*** switchmotorDebug(): turnindx = %d\n",xx->turnindx))
+ if (xx->turnout != NULL) {
+ LOG( log_switchmotor, 1, ("*** switchmotorDebug(): turnout = T%d, %s\n",
+ GetTrkIndex(xx->turnout), GetTrkTypeName(xx->turnout)))
+ }
}
static void DeleteSwitchMotor ( track_p trk )
{
- switchmotorData_p xx = GetswitchmotorData(trk);
+ LOG( log_switchmotor, 1,("*** DeleteSwitchMotor(%p)\n",trk))
+ LOG( log_switchmotor, 1,("*** DeleteSwitchMotor(): index is %d\n",GetTrkIndex(trk)))
+ switchmotorData_p xx = GetswitchmotorData(trk);
+ LOG( log_switchmotor, 1,("*** DeleteSwitchMotor(): xx = %p, xx->name = %p, xx->normal = %p, xx->reverse = %p, xx->pointsense = %p\n",
+ xx,xx->name,xx->normal,xx->reverse,xx->pointsense))
MyFree(xx->name); xx->name = NULL;
MyFree(xx->normal); xx->normal = NULL;
MyFree(xx->reverse); xx->reverse = NULL;
@@ -262,7 +342,8 @@ static BOOL_T WriteSwitchMotor ( track_p t, FILE * f )
{
BOOL_T rc = TRUE;
switchmotorData_p xx = GetswitchmotorData(t);
-
+
+ if (xx->turnout == NULL) return FALSE;
rc &= fprintf(f, "SWITCHMOTOR %d %d \"%s\" \"%s\" \"%s\" \"%s\"\n",
GetTrkIndex(t), GetTrkIndex(xx->turnout), xx->name,
xx->normal, xx->reverse, xx->pointsense)>0;
@@ -287,8 +368,28 @@ static void ReadSwitchMotor ( char * line )
xx->normal = normal;
xx->reverse = reverse;
xx->pointsense = pointsense;
- xx->turnout = FindTrack(trkindex);
- switchmotorDebug(trk);
+ xx->turnindx = trkindex;
+ LOG( log_switchmotor, 1,("*** ReadSwitchMotor(): trk = %p (%d), xx = %p\n",trk,GetTrkIndex(trk),xx))
+ LOG( log_switchmotor, 1,("*** ReadSwitchMotor(): name = %p, normal = %p, reverse = %p, pointsense = %p\n",
+ name,normal,reverse,pointsense))
+ switchmotorDebug(trk);
+}
+
+EXPORT void ResolveSwitchmotorTurnout ( track_p trk )
+{
+ LOG( log_switchmotor, 1,("*** ResolveSwitchmotorTurnout(%p)\n",trk))
+ switchmotorData_p xx;
+ track_p t_trk;
+ if (GetTrkType(trk) != T_SWITCHMOTOR) return;
+ xx = GetswitchmotorData(trk);
+ LOG( log_switchmotor, 1, ("*** ResolveSwitchmotorTurnout(%d)\n",GetTrkIndex(trk)))
+ t_trk = FindTrack(xx->turnindx);
+ if (t_trk == NULL) {
+ NoticeMessage( _("ResolveSwitchmotor: Turnout T%d: T%d doesn't exist"), _("Continue"), NULL, GetTrkIndex(trk), xx->turnindx );
+ }
+ xx->turnout = t_trk;
+ ComputeSwitchMotorBoundingBox(trk);
+ LOG( log_switchmotor, 1,("*** ResolveSwitchmotorTurnout(): t_trk = (%d) %p\n",xx->turnindx,t_trk))
}
static void MoveSwitchMotor (track_p trk, coOrd orig ) {}
@@ -329,7 +430,6 @@ static trackCmd_t switchmotorCmds = {
NULL /* drawDesc */
};
-#ifdef SWITCHMOTORCMD
static track_p FindSwitchMotor (track_p trk)
{
track_p a_trk;
@@ -352,7 +452,7 @@ static void SwitchMotorOk ( void * junk )
LOG( log_switchmotor, 1, ("*** SwitchMotorOk()\n"))
ParamUpdate (&switchmotorPG );
if ( switchmotorName[0]==0 ) {
- NoticeMessage( 0, "Switch motor must have a name!", _("Ok"));
+ NoticeMessage( _("Switch motor must have a name!"), _("Ok"), NULL);
return;
}
wDrawDelayUpdate( mainD.d, TRUE );
@@ -365,9 +465,12 @@ static void SwitchMotorOk ( void * junk )
xx->reverse = MyStrdup(switchmotorReverse);
xx->pointsense = MyStrdup(switchmotorPointSense);
xx->turnout = switchmotorTurnout;
+ LOG( log_switchmotor, 1,("*** SwitchMotorOk(): trk = %p (%d), xx = %p\n",trk,GetTrkIndex(trk),xx))
switchmotorDebug(trk);
UndoEnd();
- wHide( switchmotorW );
+ wHide( switchmotorW );
+ ComputeSwitchMotorBoundingBox(trk);
+ DrawNewTrack(trk);
}
static void NewSwitchMotorDialog(track_p trk)
@@ -413,6 +516,7 @@ static STATUS_T CmdSwitchMotorCreate( wAction_t action, coOrd pos )
}
}
+#if 0
extern BOOL_T inDescribeCmd;
static STATUS_T CmdSwitchMotorEdit( wAction_t action, coOrd pos )
@@ -501,27 +605,177 @@ static STATUS_T CmdSwitchMotor (wAction_t action, coOrd pos )
default: return C_TERMINATE;
}
}
+#endif
+
+static void SwitchMotorEditOk ( void * junk )
+{
+ switchmotorData_p xx;
+ track_p trk;
+
+ LOG( log_switchmotor, 1, ("*** SwitchMotorEditOk()\n"))
+ ParamUpdate (&switchmotorEditPG );
+ if ( switchmotorEditName[0]==0 ) {
+ NoticeMessage( _("Switch motor must have a name!") , _("Ok"), NULL);
+ return;
+ }
+ wDrawDelayUpdate( mainD.d, TRUE );
+ UndoStart( _("Modify Switch Motor"), "Modify Switch Motor" );
+ trk = switchmotorEditTrack;
+ xx = GetswitchmotorData( trk );
+ xx->name = MyStrdup(switchmotorEditName);
+ xx->normal = MyStrdup(switchmotorEditNormal);
+ xx->reverse = MyStrdup(switchmotorEditReverse);
+ xx->pointsense = MyStrdup(switchmotorEditPointSense);
+ switchmotorDebug(trk);
+ UndoEnd();
+ wHide( switchmotorEditW );
+}
+
+
+static void EditSwitchMotor (track_p trk)
+{
+ switchmotorData_p xx = GetswitchmotorData(trk);
+ strncpy(switchmotorEditName,xx->name,STR_SHORT_SIZE);
+ strncpy(switchmotorEditNormal,xx->normal,STR_LONG_SIZE);
+ strncpy(switchmotorEditReverse,xx->reverse,STR_LONG_SIZE);
+ strncpy(switchmotorEditPointSense,xx->pointsense,STR_LONG_SIZE);
+ if (xx->turnout == NULL) switchmotorEditTonum = 0;
+ else switchmotorEditTonum = GetTrkIndex(xx->turnout);
+ switchmotorEditTrack = trk;
+ if ( !switchmotorEditW ) {
+ ParamRegister( &switchmotorEditPG );
+ switchmotorEditW = ParamCreateDialog (&switchmotorEditPG,
+ MakeWindowTitle(_("Edit switch motor")),
+ _("Ok"), SwitchMotorEditOk,
+ wHide, TRUE, NULL, F_BLOCK,
+ NULL );
+ }
+ ParamLoadControls( &switchmotorEditPG );
+ sprintf( message, _("Edit switch motor %d"), GetTrkIndex(trk) );
+ wWinSetTitle( switchmotorEditW, message );
+ wShow (switchmotorEditW);
+}
+
+static coOrd swmhiliteOrig, swmhiliteSize;
+static POS_T swmhiliteBorder;
+static wDrawColor swmhiliteColor = 0;
+static void DrawSWMotorTrackHilite( void )
+{
+ wPos_t x, y, w, h;
+ if (swmhiliteColor==0)
+ swmhiliteColor = wDrawColorGray(87);
+ w = (wPos_t)((swmhiliteSize.x/mainD.scale)*mainD.dpi+0.5);
+ h = (wPos_t)((swmhiliteSize.y/mainD.scale)*mainD.dpi+0.5);
+ mainD.CoOrd2Pix(&mainD,swmhiliteOrig,&x,&y);
+ wDrawFilledRectangle( mainD.d, x, y, w, h, swmhiliteColor, wDrawOptTemp );
+}
+
+static int SwitchmotorMgmProc ( int cmd, void * data )
+{
+ track_p trk = (track_p) data;
+ switchmotorData_p xx = GetswitchmotorData(trk);
+ /*char msg[STR_SIZE];*/
+
+ switch ( cmd ) {
+ case CONTMGM_CAN_EDIT:
+ return TRUE;
+ break;
+ case CONTMGM_DO_EDIT:
+ EditSwitchMotor (trk);
+ /*inDescribeCmd = TRUE;*/
+ /*DescribeTrack (trk, msg, sizeof msg );*/
+ /*InfoMessage( msg );*/
+ return TRUE;
+ break;
+ case CONTMGM_CAN_DELETE:
+ return TRUE;
+ break;
+ case CONTMGM_DO_DELETE:
+ DeleteTrack (trk, FALSE);
+ return TRUE;
+ break;
+ case CONTMGM_DO_HILIGHT:
+ if (xx->turnout != NULL && !xx->IsHilite) {
+ swmhiliteBorder = mainD.scale*0.1;
+ if ( swmhiliteBorder < trackGauge ) swmhiliteBorder = trackGauge;
+ GetBoundingBox( xx->turnout, &swmhiliteSize, &swmhiliteOrig );
+ swmhiliteOrig.x -= swmhiliteBorder;
+ swmhiliteOrig.y -= swmhiliteBorder;
+ swmhiliteSize.x -= swmhiliteOrig.x-swmhiliteBorder;
+ swmhiliteSize.y -= swmhiliteOrig.y-swmhiliteBorder;
+ DrawSWMotorTrackHilite();
+ xx->IsHilite = TRUE;
+ }
+ break;
+ case CONTMGM_UN_HILIGHT:
+ if (xx->turnout != NULL && xx->IsHilite) {
+ swmhiliteBorder = mainD.scale*0.1;
+ if ( swmhiliteBorder < trackGauge ) swmhiliteBorder = trackGauge;
+ GetBoundingBox( xx->turnout, &swmhiliteSize, &swmhiliteOrig );
+ swmhiliteOrig.x -= swmhiliteBorder;
+ swmhiliteOrig.y -= swmhiliteBorder;
+ swmhiliteSize.x -= swmhiliteOrig.x-swmhiliteBorder;
+ swmhiliteSize.y -= swmhiliteOrig.y-swmhiliteBorder;
+ DrawSWMotorTrackHilite();
+ xx->IsHilite = FALSE;
+ }
+ break;
+ case CONTMGM_GET_TITLE:
+ if (xx->turnout == NULL) {
+ sprintf( message, "\t%s\t%d", xx->name, 0);
+ } else {
+ sprintf( message, "\t%s\t%d", xx->name, GetTrkIndex(xx->turnout));
+ }
+ break;
+ }
+ return FALSE;
+}
//#include "bitmaps/switchmotor.xpm"
-#include "bitmaps/switchmnew.xpm"
-#include "bitmaps/switchmedit.xpm"
-#include "bitmaps/switchmdel.xpm"
+//#include "bitmaps/switchmnew.xpm"
+//#include "bitmaps/switchmedit.xpm"
+//#include "bitmaps/switchmdel.xpm"
+#include "bitmaps/switchm.xpm"
+
+EXPORT void SwitchmotorMgmLoad( void )
+{
+ track_p trk;
+ static wIcon_p switchmI = NULL;
+
+ if ( switchmI == NULL)
+ switchmI = wIconCreatePixMap( switchm_xpm );
+
+ TRK_ITERATE(trk) {
+ if (GetTrkType(trk) != T_SWITCHMOTOR) continue;
+ ContMgmLoad( switchmI, SwitchmotorMgmProc, (void *)trk );
+ }
+}
EXPORT void InitCmdSwitchMotor( wMenu_p menu )
{
switchmotorName[0] = '\0';
switchmotorNormal[0] = '\0';
switchmotorReverse[0] = '\0';
- switchmotorPointSense[0] = '\0';
- ButtonGroupBegin( _("SwitchMotor"), "cmdSwitchMotorSetCmd", _("Switch Motors") );
- AddMenuButton( menu, CmdSwitchMotor, "cmdSwitchMotorCreate", _("Create Switch Motor"), wIconCreatePixMap(switchmnew_xpm), LEVEL0_50, IC_CANCEL|IC_POPUP, ACCL_SWITCHMOTOR1, (void*)SWITCHMOTOR_CREATE );
- AddMenuButton( menu, CmdSwitchMotor, "cmdSwitchMotorEdit", _("Edit Switch Motor"), wIconCreatePixMap(switchmedit_xpm), LEVEL0_50, IC_CANCEL|IC_POPUP, ACCL_SWITCHMOTOR2, (void*)SWITCHMOTOR_EDIT );
- AddMenuButton( menu, CmdSwitchMotor, "cmdSwitchMotorDelete", _("Delete Switch Motor"), wIconCreatePixMap(switchmdel_xpm), LEVEL0_50, IC_CANCEL|IC_POPUP, ACCL_SWITCHMOTOR3, (void*)SWITCHMOTOR_DELETE );
- ButtonGroupEnd();
+ switchmotorPointSense[0] = '\0';
+ AddMenuButton( menu, CmdSwitchMotorCreate, "cmdSwitchMotorCreate",
+ _("Switch Motor"), wIconCreatePixMap( switchm_xpm ),
+ LEVEL0_50, IC_STICKY|IC_POPUP2, ACCL_SWITCHMOTOR1,
+ NULL );
ParamRegister( &switchmotorPG );
}
-#endif
+EXPORT void CheckDeleteSwitchmotor(track_p t)
+{
+ track_p sm;
+ switchmotorData_p xx;
+
+ sm = FindSwitchMotor( t );
+ if (sm == NULL) return;
+ xx = GetswitchmotorData (sm);
+ NoticeMessage(_("Deleting Switch Motor %s"),_("Ok"),NULL,xx->name);
+ DeleteTrack (sm, FALSE);
+}
+
EXPORT void InitTrkSwitchMotor( void )