DieRoller.pc (plain text)
// DieRoller

include "Ccontrols.c"

Chandle radDice, h3, h4, radSides, cmdRoll, barMain, mnuHelp;
Chandle lblResults, lblD1, lblD2, lblD3, lblD4, lblD5, lblD6, chkD1;
Chandle chkD2, chkD3, chkD4, chkD5, chkD6, h37, lblSum;

int dieValues[6];
int dieSides[6] = {4,6,8,10,12,20};

//----------------------------------------------------------------------
// Event handlers
//----------------------------------------------------------------------

null_handler()
{
    // Does nothing, but allows the control to respond to events
}

// checkbox_handler - handles taps on the check boxes and updates
//                    the "sum of selected dice" total.

checkbox_handler()
{
    int total;
    
    if (Cgetstate(chkD1) == 1)
        total = total + (int)Cgetcontent(lblD1);
    if (Cgetstate(chkD2) == 1)
        total = total + (int)Cgetcontent(lblD2);
    if (Cgetstate(chkD3) == 1)
        total = total + (int)Cgetcontent(lblD3);
    if (Cgetstate(chkD4) == 1)
        total = total + (int)Cgetcontent(lblD4);
    if (Cgetstate(chkD5) == 1)
        total = total + (int)Cgetcontent(lblD5);
    if (Cgetstate(chkD6) == 1)
        total = total + (int)Cgetcontent(lblD6);
    Csetcontent(lblSum, total);
    Cdraw(lblSum);
}

// SetDie: sets the value label of a specific die, and redraws it.

SetDie(Chandle whichDie, int newValue)
{
    string x;
    
    if (newValue == 0)
        x = "--";
    else
        x = (string)newValue;
        
    Csetcontent(whichDie, x);
    Cdraw(whichDie);
}

// update_labels: Updates all the dice with the results of the 
//                new roll.

update_labels()
{
    SetDie(lblD1, dieValues[0]);
    SetDie(lblD2, dieValues[1]);
    SetDie(lblD3, dieValues[2]);
    SetDie(lblD4, dieValues[3]);
    SetDie(lblD5, dieValues[4]);
    SetDie(lblD6, dieValues[5]);
}

// reset_checkboxes: Sets/clears and hides/shows checkboxes based
//                   on the values of the dice.

reset_checkboxes()
{
    if (Cgetcontent(lblD1) == "--")
    {
        Csetstate(chkD1, 0);
        Chide(chkD1);
    }
    else
    {
        Cshow(chkD1);
        Csetstate(chkD1, 1);
    }

    if (Cgetcontent(lblD2) == "--")
    {
        Csetstate(chkD2, 0);
        Chide(chkD2);
    }
    else
    {
        Cshow(chkD2);
        Csetstate(chkD2, 1);
    }
    
    if (Cgetcontent(lblD3) == "--")
    {
        Csetstate(chkD3, 0);
        Chide(chkD3);
    }
    else
    {
        Cshow(chkD3);
        Csetstate(chkD3, 1);
    }
    
    if (Cgetcontent(lblD4) == "--")
    {
        Csetstate(chkD4, 0);
        Chide(chkD4);
    }
    else
    {
        Cshow(chkD4);
        Csetstate(chkD4, 1);
    }
    
    if (Cgetcontent(lblD5) == "--")
    {
        Csetstate(chkD5, 0);
        Chide(chkD5);
    }
    else
    {
        Cshow(chkD5);
        Csetstate(chkD5, 1);
    }
    
    if (Cgetcontent(lblD6) == "--")
    {
        Csetstate(chkD6, 0);
        Chide(chkD6);
    }
    else
    {
        Cshow(chkD6);
        Csetstate(chkD6, 1);
    }
        
    Cdraw(chkD1);
    Cdraw(chkD2);
    Cdraw(chkD3);
    Cdraw(chkD4);
    Cdraw(chkD5);
    Cdraw(chkD6);
}

// on_cmdRoll: Event handler for the Roll! button.
    
on_cmdRoll()
{
    int i;

    // Generate the new die values    
    for (i=0; i<6; i++) 
        dieValues[i] = random(dieSides[Cgetcursel(radSides)]) + 1;
        
    // Zero out the extra values not included in the roll
    for (i=Cgetcursel(radDice) + 1; i<6; i++)
        dieValues[i] = 0;
    
    // Update the display to reflect the new roll
    update_labels();
    reset_checkboxes();
    checkbox_handler();
}

// on_barMain: handler for the main menu bar.

on_barMain()
{
    if (Cgetcontent(barMain) == "About...")
        alert("DieRoller 1.00\nCopyright (c) 2000\nTammy Cravit\n" + 
               "tammy@warmfuzzy.com");
}

// messageloop: Ccontrols event-dispatching loop.

messageloop()
{
    int e;
    while(1)
    {
        e=event(1);
        if     (Cevent(radDice,e))  null_handler();
        else if(Cevent(radSides,e)) null_handler();
        else if(Cevent(cmdRoll,e))  on_cmdRoll();
        else if(Cevent(barMain,e))  on_barMain();
        else if(Cevent(chkD1,e))    checkbox_handler();
        else if(Cevent(chkD2,e))    checkbox_handler();
        else if(Cevent(chkD3,e))    checkbox_handler();
        else if(Cevent(chkD4,e))    checkbox_handler();
        else if(Cevent(chkD5,e))    checkbox_handler();
        else if(Cevent(chkD6,e))    checkbox_handler();
    }
}

//----------------------------------------------------------------------
// GUI Initialization functions
//----------------------------------------------------------------------

initcontrols()
{
    radDice=Cradio(38,19,115,12);
    h3=Clabel(14,18,20,0,2,0);
    h4=Clabel(19,34,14,0,2,0);
    radSides=Cradio(38,35,115,12);
    cmdRoll=Cbutton(47,57,64,18,1,4);
    barMain=Cmenubar();
    mnuHelp=Cmenu(5,50,30);
    Caddmenu(barMain,mnuHelp);
    lblResults=Clabel(6,89,30,0,2,0);
    lblD1=Clabel(7,104,20,1,2,1);
    lblD2=Clabel(32,104,20,1,2,1);
    lblD3=Clabel(57,104,20,1,2,1);
    lblD4=Clabel(82,104,20,1,2,1);
    lblD5=Clabel(107,104,20,1,2,1);
    lblD6=Clabel(132,104,20,1,2,1);
    chkD1=Ccheckbox(12,117,10);
    chkD2=Ccheckbox(36,117,10);
    chkD3=Ccheckbox(61,117,10);
    chkD4=Ccheckbox(87,117,10);
    chkD5=Ccheckbox(111,117,10);
    chkD6=Ccheckbox(136,117,10);
    h37=Clabel(6,140,88,0,2,0);
    lblSum=Clabel(98,140,25,1,2,0);
}

initcontents()
{
    Csetcontent(h3,"Roll:");
    Csetcontent(h4,"of:");
    Csetcontent(cmdRoll,"Roll!");
    Csettopic(mnuHelp,"Help");
    Csetcontent(lblResults, "Results:");

    Csetcontent(lblD1,"--");
    Csetcontent(lblD2,"--");
    Csetcontent(lblD3,"--");
    Csetcontent(lblD4,"--");
    Csetcontent(lblD5,"--");
    Csetcontent(lblD6,"--");

    Csetcontent(chkD1," ");
    Csetcontent(chkD2," ");
    Csetcontent(chkD3," ");
    Csetcontent(chkD4," ");
    Csetcontent(chkD5," ");
    Csetcontent(chkD6," ");

    Csetcontent(h37, "Sum of selected dice:");
    Csetcontent(lblSum,"---");

    Csetcursel(radDice, 2);
    Csetcursel(radSides, 1);
}

inititems()
{
    Cadditem(radDice,"1");
    Cadditem(radDice,"2");
    Cadditem(radDice,"3");
    Cadditem(radDice,"4");
    Cadditem(radDice,"5");
    Cadditem(radDice,"6");

    Cadditem(radSides,"d4");
    Cadditem(radSides,"d6");
    Cadditem(radSides,"d8");
    Cadditem(radSides,"d10");
    Cadditem(radSides,"d12");
    Cadditem(radSides,"d20");
    Cadditem(mnuHelp,"About...");
}

drawcontrols()
{
    Cdraw(radDice);
    Cdraw(h3);
    Cdraw(h4);
    Cdraw(radSides);
    Cdraw(cmdRoll);
    Cdraw(lblResults);
    Cdraw(lblD1);
    Cdraw(lblD2);
    Cdraw(lblD3);
    Cdraw(lblD4);
    Cdraw(lblD5);
    Cdraw(lblD6);
    Cdraw(chkD1);
    Cdraw(chkD2);
    Cdraw(chkD3);
    Cdraw(chkD4);
    Cdraw(chkD5);
    Cdraw(chkD6);
    Cdraw(h37);
    Cdraw(lblSum);
}

//----------------------------------------------------------------------
// Main module
//----------------------------------------------------------------------

main()
{
    graph_on();
    title("DieRoller 1.00");
    initcontrols();
    initcontents();
    inititems();
    drawcontrols();
    messageloop();
}