// tango.zh
// Beta 2


// Functions used to draw to the screen or an offscreen bitmap.

void __Tango_DrawComplexBackdrop(int backdrop, float x, float y)
{
    int opacity;
    
    for(int baseIndex=0;
      backdrop[baseIndex]!=TANGO_DRAW_END;
      baseIndex+=__TANGO_SIZEOF_DRAW_DATA)
    {
        // Tiles and combos are combined, since they're almost the same.
        if(backdrop[baseIndex]<=TANGO_DRAW_COMBO_TRANS)
        {
            // Tile or transparent tile
            if(backdrop[baseIndex]==TANGO_DRAW_TILE ||
               backdrop[baseIndex]==TANGO_DRAW_COMBO)
                opacity=OP_OPAQUE;
            else
                opacity=OP_TRANS;
            
            if(backdrop[baseIndex]<=TANGO_DRAW_TILE_TRANS)
            {
                Screen->DrawTile(__TANGO_DRAWING_LAYER,
                                 x+backdrop[baseIndex+TANGO_TILE_X],
                                 y+backdrop[baseIndex+TANGO_TILE_Y],
                                 backdrop[baseIndex+TANGO_TILE_ID],
                                 backdrop[baseIndex+TANGO_TILE_WIDTH],
                                 backdrop[baseIndex+TANGO_TILE_HEIGHT],
                                 backdrop[baseIndex+TANGO_TILE_CSET],
                                 -1, -1, 0, 0, 0, 0, true, opacity);
            }
            else
            {
                Screen->DrawCombo(__TANGO_DRAWING_LAYER,
                                  x+backdrop[baseIndex+TANGO_TILE_X],
                                  y+backdrop[baseIndex+TANGO_TILE_Y],
                                  backdrop[baseIndex+TANGO_TILE_ID],
                                  backdrop[baseIndex+TANGO_TILE_WIDTH],
                                  backdrop[baseIndex+TANGO_TILE_HEIGHT],
                                  backdrop[baseIndex+TANGO_TILE_CSET],
                                  -1, -1, 0, 0, 0, 0, 0, true, opacity);
            }
        }
        else if(backdrop[baseIndex]<=TANGO_DRAW_RECT_TRANS)
        {
            // Rectangle or transparent rectangle
            if(backdrop[baseIndex]==TANGO_DRAW_RECT)
                opacity=OP_OPAQUE;
            else
                opacity=OP_TRANS;
            
            Screen->Rectangle(__TANGO_DRAWING_LAYER,
                              x+backdrop[baseIndex+TANGO_RECT_X]+1,
                              y+backdrop[baseIndex+TANGO_RECT_Y]+1,
                              x+backdrop[baseIndex+TANGO_RECT_X]+
                                backdrop[baseIndex+TANGO_RECT_WIDTH]-2,
                              y+backdrop[baseIndex+TANGO_RECT_Y]+
                                backdrop[baseIndex+TANGO_RECT_HEIGHT]-2,
                              backdrop[baseIndex+TANGO_RECT_CSET]*16+
                                backdrop[baseIndex+TANGO_RECT_COLOR],
                              -1, 0, 0, 0, true, opacity);
        }
        else
            // Text
            __Tango_DrawFrameText(backdrop[baseIndex+TANGO_TEXT_STRING],
              backdrop[baseIndex+TANGO_TEXT_FONT],
              backdrop[baseIndex+TANGO_TEXT_CSET],
              backdrop[baseIndex+TANGO_TEXT_COLOR],
              x+backdrop[baseIndex+TANGO_TEXT_X],
              y+backdrop[baseIndex+TANGO_TEXT_Y]);
    }
}

// Draws text for a complex backdrop
void __Tango_DrawFrameText(int str, int font, int cset, int color, int x, int y)
{
    for(int i=0; str[i]!=0; i++)
        x+=__Tango_PutChar(str[i], font, cset, color, x, y, __TANGO_DRAWING_LAYER);
}
                      
// Draws a single character and returns the width of the character drawn.
int __Tango_PutChar(int character, int font, int cset, int color, int x, int y, int layer)
{
    if(character!=' ') // Spaces don't need drawn...
    {
        if(font[__TANGO_FONT_TYPE]==TANGO_FONT_BUILTIN ||
           font[__TANGO_FONT_TYPE]==TANGO_FONT_BUILTIN_EXTENDED)
        {
            if(character<=__TANGO_FONT_BUILTIN_MAX)
            {
                // Valid character in built-in font
                Screen->DrawCharacter(layer,
                                      x, y,
                                      font[__TANGO_FONT_ID],
                                      cset*16+color,
                                      -1, -1, -1, character, OP_OPAQUE);
            }
            else if(font[__TANGO_FONT_TYPE]==TANGO_FONT_BUILTIN_EXTENDED)
            {
                // Extended character
                int tile=font[__TANGO_FONT_EXTENSION_TILE]+character-__TANGO_FONT_EXTENDED_START;
                Screen->FastTile(layer, x, y, tile, cset, OP_OPAQUE);
            }
        }
        else // Custom font
        {
            int tile=font[__TANGO_FONT_START_TILE]+character+__TANGO_FONT_ASCII_TO_CUSTOM;
            Screen->FastTile(layer, x, y, tile, cset, OP_OPAQUE);
        }
    }
    
    return __Tango_GetCharacterWidth(character, font);
}

// Draws the current string's portion of the bitmap to the screen.
void __Tango_DrawToScreen()
{
    // Draw the backdrop first
    int defStart=__Tango_Data[__TCS_DEF_START];
    int dataStart=__Tango_Data[__TCS_DATA_START];
    int styleStart=__Tango_Data[__TCS_STYLE_START];
    int backdrop=__Tango_Styles[styleStart+TANGO_STYLE_BACKDROP_TYPE];
    int screenX=__Tango_SlotData[dataStart+__TSDIDX_SCREEN_X];
    int screenY=__Tango_SlotData[dataStart+__TSDIDX_SCREEN_Y];
    
    if(backdrop==TANGO_BACKDROP_COLOR ||
       backdrop==TANGO_BACKDROP_COLOR_TRANS)
    {
        int color=__Tango_Styles[styleStart+TANGO_STYLE_BACKDROP_CSET]*16+
                  __Tango_Styles[styleStart+TANGO_STYLE_BACKDROP_COLOR];
        int width=__Tango_Styles[styleStart+TANGO_STYLE_BACKDROP_WIDTH];
        int height=__Tango_Styles[styleStart+TANGO_STYLE_BACKDROP_HEIGHT];
        int opacity;
        
        if(backdrop==TANGO_BACKDROP_COLOR)
            opacity=OP_OPAQUE;
        else
            opacity=OP_TRANS;
            
        Screen->Rectangle(__TANGO_DRAWING_LAYER, screenX+1, screenY+1,
                          screenX+width-2, screenY+height-2, color,
                          -1, 0, 0, 0, true, opacity);
    }
    // Tiles and combos are combined, since they're almost the same.
    else if(backdrop>=TANGO_BACKDROP_TILE &&
            backdrop<=TANGO_BACKDROP_COMBO_TRANS)
    {
        int tile=__Tango_Styles[styleStart+TANGO_STYLE_BACKDROP_TILE];
        int cset=__Tango_Styles[styleStart+TANGO_STYLE_BACKDROP_CSET];
        int width=__Tango_Styles[styleStart+TANGO_STYLE_BACKDROP_WIDTH];
        int height=__Tango_Styles[styleStart+TANGO_STYLE_BACKDROP_HEIGHT];
        int opacity;
        
        if(backdrop==TANGO_BACKDROP_TILE ||
           backdrop==TANGO_BACKDROP_COMBO)
            opacity=OP_OPAQUE;
        else
            opacity=OP_TRANS;
        
        if(backdrop<=TANGO_BACKDROP_TILE_TRANS)
        {
            Screen->DrawTile(__TANGO_DRAWING_LAYER, screenX, screenY,
                             tile, width, height, cset,
                             -1, -1, 0, 0, 0, 0, true, opacity);
        }
        else // Combo
        {
            Screen->DrawCombo(__TANGO_DRAWING_LAYER, screenX, screenY,
                              tile, width, height, cset,
                              -1, -1, 0, 0, 0, 0, 0, true, opacity);
        }
    }
    else if(backdrop==TANGO_BACKDROP_COMPLEX)
    {
        __Tango_DrawComplexBackdrop(
            __Tango_Styles[styleStart+TANGO_STYLE_BACKDROP_DATA],
            screenX, screenY);
    }
    
    
    // Then draw the string
    
    int stringX=screenX+__Tango_Styles[styleStart+TANGO_STYLE_TEXT_X];
    int stringY=screenY+__Tango_Styles[styleStart+TANGO_STYLE_TEXT_Y];
    
    int bitmapX=__Tango_SlotDefs[defStart+__TSDEF_X];
    int bitmapY=__Tango_SlotDefs[defStart+__TSDEF_Y]+
                    __Tango_SlotData[dataStart+__TSDIDX_OFFSET];
    
    int width=__Tango_Styles[styleStart+TANGO_STYLE_TEXT_WIDTH];
    int height=__Tango_Styles[styleStart+TANGO_STYLE_TEXT_HEIGHT];
    
    if(__Tango_SlotData[dataStart+__TSDIDX_STATE]==__TANGO_STATE_ADVANCING)
    {
        int offset=__Tango_SlotData[dataStart+__TSDIDX_COUNTER];
        bitmapY+=offset;
        height-=offset;
    }
    
    // Copy the text to the screen.
    Screen->DrawBitmap(__TANGO_DRAWING_LAYER, __TANGO_BITMAP,
                       bitmapX, bitmapY, width, height,
                       stringX, stringY, width, height,
                       0, true);
    
    // Centered text needs the current line copied separately.
    if((__Tango_Styles[styleStart+TANGO_STYLE_FLAGS]&TANGO_FLAG_CENTERED)!=0 &&
       __Tango_SlotData[dataStart+__TSDIDX_STATE]!=__TANGO_STATE_ADVANCING)
    {
        int defStart=__Tango_Data[__TCS_DEF_START];
        int srcY=__Tango_SlotDefs[defStart+__TSDEF_Y]+
                   __Tango_SlotDefs[defStart+__TSDEF_HEIGHT]-16;
        int destX=stringX+(width-(__Tango_GetCurrSlotData(__TSDIDX_CHAR_X)-bitmapX))/2;
        int destY=stringY+
                  __Tango_SlotData[dataStart+__TSDIDX_CHAR_Y]-
                  __Tango_SlotDefs[defStart+__TSDEF_Y]-
                  __Tango_SlotData[dataStart+__TSDIDX_OFFSET];
        
        Screen->DrawBitmap(__TANGO_DRAWING_LAYER, __TANGO_BITMAP,
                           bitmapX, srcY, width, 16,
                           destX, destY, width, 16,
                           0, true);
    }
    
    __Tango_DrawMoreIcon();
}

// Draw the "press A" prompt.
void __Tango_DrawMoreIcon()
{
    int dataStart=__Tango_Data[__TCS_DATA_START];
    bool doDraw=false;
    
    int styleStart=__Tango_Data[__TCS_STYLE_START];
    int flags=__Tango_Styles[styleStart+TANGO_STYLE_FLAGS];
    
    // Is the string waiting for A to be pressed?
    if(__Tango_SlotData[dataStart+__TSDIDX_STATE]==__TANGO_STATE_PRESS_A)
        doDraw=true;
    
    // If the string is finished, draw the icon unless it's persistent
    // and there's no next string.
    else if(__Tango_SlotData[dataStart+__TSDIDX_STATE]==__TANGO_STATE_FINISHED)
    {
        if((flags&TANGO_FLAG_PERSISTENT)==0 ||
           __Tango_SlotData[dataStart+__TSDIDX_NEXT_STRING]!=0)
            doDraw=true;
    }
    
    if(!doDraw)
        return;
    
    // Is there a combo set at all?
    int combo=__Tango_Styles[styleStart+TANGO_STYLE_MORE_COMBO];
    if(combo<=0)
        return;
    
    // The indicator needs drawn.
    
    int cset=__Tango_Styles[styleStart+TANGO_STYLE_MORE_CSET];
    int x=__Tango_SlotData[dataStart+__TSDIDX_SCREEN_X]+
            __Tango_Styles[styleStart+TANGO_STYLE_MORE_X];
    int y=__Tango_SlotData[dataStart+__TSDIDX_SCREEN_Y]+
            __Tango_Styles[styleStart+TANGO_STYLE_MORE_Y];
    
    Screen->FastCombo(__TANGO_DRAWING_LAYER, x, y, combo, cset, OP_OPAQUE);
}

void __Tango_DrawMenuCursor()
{
    int pos=__Tango_Data[__TDIDX_MENU_CURSOR_POS];
    int dataStart=__TDIDX_CHOICE_DATA+pos*__TANGO_SIZEOF_CHOICE;
    
    Screen->DrawCombo(__TANGO_DRAWING_LAYER,
      __Tango_Data[dataStart+__TANGO_CHOICE_X],
      __Tango_Data[dataStart+__TANGO_CHOICE_Y],
      __Tango_Data[__TDIDX_MENU_CURSOR_COMBO],
      __Tango_Data[__TDIDX_MENU_CURSOR_WIDTH],
      __Tango_Data[__TDIDX_MENU_CURSOR_HEIGHT],
      __Tango_Data[__TDIDX_MENU_CURSOR_CSET],
      -1, -1, 0, 0, 0, 0, 0, true, OP_OPAQUE);
}

// Clears a string's area of the offscreen bitmap.
void __Tango_ClearSlotBitmap(int slot)
{
    int defStart=slot*__TANGO_SIZEOF_SLOTDEF;
    int x=__Tango_SlotDefs[defStart+__TSDEF_X];
    int y=__Tango_SlotDefs[defStart+__TSDEF_Y];
    int width=__Tango_SlotDefs[defStart+__TSDEF_WIDTH];
    int height=__Tango_SlotDefs[defStart+__TSDEF_HEIGHT];
    
    Screen->SetRenderTarget(__TANGO_BITMAP);
    Screen->Rectangle(0, x+1, y+1, x+width-2, y+height-2,
                      0, -1, 0, 0, 0, true, OP_OPAQUE);
}

// Used in rendering centered text. Draws the line currently
// being processed. Used both to draw the line to the screen
// and to transfer it within the bitmap.
void __Tango_DrawCurrentLine(int x, int y)
{
    int defStart=__Tango_Data[__TCS_DEF_START];
    int srcX=__Tango_SlotDefs[defStart+__TSDEF_X];
    int srcY=__Tango_SlotDefs[defStart+__TSDEF_Y]+
               __Tango_SlotDefs[defStart+__TSDEF_HEIGHT]-16;
    int width=__Tango_GetCurrStyleData(TANGO_STYLE_TEXT_WIDTH);
    int destX=0;//srcX+(width-(__Tango_SlotData[dataStart+__TSDIDX_CHAR_X]-srcX))/2;
    
    Screen->DrawBitmap(0, __TANGO_BITMAP, srcX, srcY, width, 16,
                       destX, 0,//__Tango_SlotData[dataStart+__TSDIDX_CHAR_Y],
                       width, 16, 0, true);
    Screen->Rectangle(0, srcX, srcY, srcX+width, srcY+16, 0, -1, 0, 0, 0,
                      true, OP_OPAQUE);
}

