UIutils.c 5.2 KB

#include <ultra64.h>
#include <ultralog.h>

#include <assert.h>

#include "UI.h"
#include "font_ext.h"
#include "music.h"      /* for PRINTF */

#define kBevel_w 0


static char     sIsMouseDown;
static void     __DPFrameRectangle(Gfx **ppGfx, int l, int t, int r, int b);


void
SetMouseDown (char isDown)
{
    sIsMouseDown = isDown;
}


char
IsMouseDown (void)
{
    return sIsMouseDown;
}


void
SetRect (Rect *pR, int l, int t, int r, int b)
{
    pR->left = l;
    pR->top = t;
    pR->right = r;
    pR->bottom = b;
}


void
OffsetRect (Rect *pR, int x, int y)
{
    pR->left += x;
    pR->top += y;
    pR->right += x;
    pR->bottom += y;
}


void
InsetRect (Rect *pR, int x, int y)
{
    pR->left += x;
    pR->top += y;
    pR->right -= x;
    pR->bottom -= y;
}


char
PointInRect (Rect *pR, Point *pPt)
{
    if (pPt->x < pR->left || pPt->x > pR->right || pPt->y < pR->top || pPt->y > pR->bottom)
        return FALSE;

    return TRUE;
}


/*
  Draws a beveled rectangle.
  The rectangle coordinates are defined by 'pR' and if 'isSunken'
  is true the rectangle is draw to look like a well, otherwise
  it is drawn to look like a raised surface.  The rectangle is
  filled with gray. The display list commands for rendering the
  beveled rectangle are written to 'ppGfx'.
*/
void
DrawBeveledRect (Rect *pR, char isSunken, char isHilited, Gfx **ppGfx)
{
    u16 shortcolor;
    
    /*assert(ppGfx != NULL);
    assert(pR != NULL);*/
    
    /* Set up the display list commands. */
    gDPPipeSync((*ppGfx)++);
    gDPSetCycleType ((*ppGfx)++, G_CYC_FILL);
    gDPSetRenderMode ((*ppGfx)++, G_RM_OPA_SURF, G_RM_OPA_SURF2);

    /* Fill in the rectangle with gray. */
    if (isHilited)
	shortcolor = GPACK_RGBA5551(255, 170, 99, 1);
    else
	shortcolor = GPACK_RGBA5551(176, 176, 176, 1);
    gDPSetFillColor((*ppGfx)++, shortcolor << 16 | shortcolor);
    gDPFillRectangle((*ppGfx)++, pR->left+1, pR->top+1, pR->right-1, pR->bottom-1);

    /* Draw the left and top edges. */
    if (isSunken)
        shortcolor = GPACK_RGBA5551(139, 102, 139, 1);
    else
        shortcolor = GPACK_RGBA5551(255, 236, 139, 1);

    gDPPipeSync((*ppGfx)++);    
    gDPSetFillColor((*ppGfx)++, shortcolor << 16 | shortcolor);
    gDPFillRectangle((*ppGfx)++, pR->left, pR->top, pR->right, pR->top+kBevel_w);  
    gDPFillRectangle((*ppGfx)++, pR->left, pR->top, pR->left+kBevel_w, pR->bottom);


    /* Draw the right and bottom edges. */
    if (isSunken)
        shortcolor = GPACK_RGBA5551(255, 236, 139, 1);
    else
        shortcolor = GPACK_RGBA5551(139, 139, 139, 1);

    gDPPipeSync((*ppGfx)++);    
    gDPSetFillColor((*ppGfx)++, shortcolor << 16 | shortcolor);
    gDPFillRectangle((*ppGfx)++, pR->left, pR->bottom, pR->right, pR->bottom+kBevel_w);
    gDPFillRectangle((*ppGfx)++, pR->right, pR->top, pR->right+kBevel_w, pR->bottom);    

    gDPPipeSync((*ppGfx)++);    
}


/*
  Draws an icon defined by a sprite image centered
  in the given rectangle.
*/
void
DrawSpriteIcon (Rect *pR, Sprite *pIcon, Gfx **ppGfx)
{
    Gfx *       dl;
    int         xIcon, yIcon;

    /* Calculate the x and y coordinates for centering the icon. */
    xIcon = pR->left + ((pR->right - pR->left)>>1) - (pIcon->width>>1);
    yIcon = pR->top + ((pR->bottom - pR->top)>>1) - (pIcon->height>>1);

    
    spSetAttribute (pIcon, SP_TRANSPARENT | SP_CUTOUT);

    /* Center the icon. */
    spMove (pIcon, xIcon, yIcon);

    pIcon->rsp_dl_next = pIcon->rsp_dl;

    dl = spDraw (pIcon);

    gSPDisplayList ((*ppGfx)++, dl);

    if((pIcon->rsp_dl_next -dl) >= pIcon->ndisplist)
      PRINTF("Display List overrun by %d\n", (pIcon->rsp_dl_next -dl) - pIcon->ndisplist);
}


/*
  Draws a gray rectangle.
  The rectangle coordinates are defined by 'pR' and 
  the gray color is defined by 'grayIndex'.  Display
  list commands for rendering the rectangle are
  written into 'ppGfx'.
*/
void
DrawGrayRect (Rect *pR, char grayIndex, Gfx **ppGfx)
{
    u16 shortcolor;
        
    gDPPipeSync((*ppGfx)++);
    gDPSetCycleType ((*ppGfx)++, G_CYC_FILL);
    gDPSetRenderMode ((*ppGfx)++, G_RM_OPA_SURF, G_RM_OPA_SURF2);

    shortcolor = GPACK_RGBA5551(grayIndex, grayIndex, grayIndex, 1);
    gDPSetFillColor((*ppGfx)++, shortcolor << 16 | shortcolor);
    __DPFrameRectangle(ppGfx, pR->left, pR->top, pR->right, pR->bottom);

    gDPPipeSync((*ppGfx)++);
}



/*
  Draws a sunken box with a number string.
  The 'str' must contain only numeric characters and/or decimals, commas,
  pluses, or minues.  No other characters currently exist in kDigit6Font.
*/
void
DrawValueStrBox (Rect *pR, char *valueStr, char isHilited, Sprite *pStrSprite, Gfx **ppGfx)
{
    Gfx         *gp;
    
    DrawBeveledRect (pR, TRUE, isHilited, ppGfx);

    gp = *ppGfx;
    
    font_init (&gp);

    font_set_pos (pR->left + 2, pR->top + 2);	/* Inset from the box's frame. */
    font_set_color (0, 0, 0, 255);
    font_set_scale (1.0, 1.0);
    font_set_win (8, 1);

    font_show_string (&gp, valueStr, pStrSprite);
    
    font_finish (&gp);

    *ppGfx = gp;    
}


static void
__DPFrameRectangle(Gfx **ppGfx, int l, int t, int r, int b)
{
    gDPFillRectangle((*ppGfx)++, l, t, r, t);  
    gDPFillRectangle((*ppGfx)++, r, t, r, b);
    gDPFillRectangle((*ppGfx)++, l, b, r, b);
    gDPFillRectangle((*ppGfx)++, l, t, l, b);
}