; ***************************************************************************
; copyright   : (C) 2025 by Martin Preuss
; email       : martin@libchipcard.de
;
; ***************************************************************************
; * This file is part of the project "AqHome".                              *
; * Please see toplevel file COPYING of that project for license details.   *
; ***************************************************************************

#ifndef AQH_AVR_GUI_TITLEWINDOW_ASM
#define AQH_AVR_GUI_TITLEWINDOW_ASM



.equ TITLEWINDOW_OFFS_SELF         = 0
.equ TITLEWINDOW_OFFS_TITLEPTR_LO  = WIN_SIZE
.equ TITLEWINDOW_OFFS_TITLEPTR_HI  = WIN_SIZE+1
.equ TITLEWINDOW_OFFS_TITLEWIN     = TITLEWINDOW_OFFS_TITLEPTR_HI+1
.equ TITLEWINDOW_OFFS_CONTENTWIN   = TITLEWINDOW_OFFS_TITLEWIN+WIN_SIZE
.equ TITLEWINDOW_SIZE              = TITLEWINDOW_OFFS_CONTENTWIN+WIN_SIZE



.cseg


; ---------------------------------------------------------------------------
; @routine TitleWindow_Init @global
;
; @param Y pointer to titledwindow data (size=TITLEWINDOW_SIZE)

TitleWindow_Init:
  rcall Window_Init
  ret
; @end



; ---------------------------------------------------------------------------
; @routine TitleWindow_SetFont @global
;
; @param Y pointer to screen object in SDRAM
; @param Z pointer to font
; @clobbers any, !Y

TitleWindow_SetFont:
  std Y+WIN_OFFS_FONT_LO, zl
  std Y+WIN_OFFS_FONT_HI, zh
  
  ; setup title window
  adiw yh:yl, TITLEWINDOW_OFFS_TITLEWIN
  std Y+WIN_OFFS_FONT_LO, zl
  std Y+WIN_OFFS_FONT_HI, zh
  sbiw yh:yl, TITLEWINDOW_OFFS_TITLEWIN

  ; setup content window
  adiw yh:yl, TITLEWINDOW_OFFS_CONTENTWIN
  std Y+WIN_OFFS_FONT_LO, zl
  std Y+WIN_OFFS_FONT_HI, zh
  sbiw yh:yl, TITLEWINDOW_OFFS_CONTENTWIN

  ret
; @end



; ---------------------------------------------------------------------------
; @routine TitleWindow_SetPosAndSize @global
;
; @param Y pointer to screen object in SDRAM
; @param  R5:R4    X (abs)
; @param  R7:R6    Y (abs)
; @param  R9:R8    W
; @param  R11:R10  H
; @clobbers any, !Y

TitleWindow_SetPosAndSize:
  std Y+WIN_OFFS_X_LO, r4
  std Y+WIN_OFFS_X_HI, r5
  std Y+WIN_OFFS_Y_LO, r6
  std Y+WIN_OFFS_Y_HI, r7
  std Y+WIN_OFFS_WIDTH_LO, r8
  std Y+WIN_OFFS_WIDTH_HI, r9
  std Y+WIN_OFFS_HEIGHT_LO, r10
  std Y+WIN_OFFS_HEIGHT_HI, r11

  ; setup title window
  adiw yh:yl, TITLEWINDOW_OFFS_TITLEWIN
  std Y+WIN_OFFS_X_LO, r4
  std Y+WIN_OFFS_X_HI, r5
  std Y+WIN_OFFS_Y_LO, r6
  std Y+WIN_OFFS_Y_HI, r7
  std Y+WIN_OFFS_WIDTH_LO, r8
  std Y+WIN_OFFS_WIDTH_HI, r9
  ldi r16, LOW(STYLE_WIN_TITLE_HEIGHT)
  ldi r17, HIGH(STYLE_WIN_TITLE_HEIGHT)
  std Y+WIN_OFFS_HEIGHT_LO, r16
  std Y+WIN_OFFS_HEIGHT_HI, r17
  sbiw yh:yl, TITLEWINDOW_OFFS_TITLEWIN

  ; setup content window
  adiw yh:yl, TITLEWINDOW_OFFS_CONTENTWIN
  std Y+WIN_OFFS_X_LO, r4
  std Y+WIN_OFFS_X_HI, r5
  ldi r16, LOW(STYLE_WIN_TITLE_HEIGHT)
  ldi r17, HIGH(STYLE_WIN_TITLE_HEIGHT)
  add r16, r6
  adc r16, r7
  std Y+WIN_OFFS_Y_LO, r16
  std Y+WIN_OFFS_Y_HI, r17
  std Y+WIN_OFFS_WIDTH_LO, r8
  std Y+WIN_OFFS_WIDTH_HI, r9
  mov r16, r10
  mov r17, r11
  subi r16, LOW(STYLE_WIN_TITLE_HEIGHT)
  sbci r17, HIGH(STYLE_WIN_TITLE_HEIGHT)
  std Y+WIN_OFFS_HEIGHT_LO, r16
  std Y+WIN_OFFS_HEIGHT_HI, r17
  sbiw yh:yl, TITLEWINDOW_OFFS_CONTENTWIN

  ret
; @end



; ---------------------------------------------------------------------------
; @routine TitleWindow_SetFullSize @global
;
; @param Y pointer to screen object in SDRAM
; @clobbers any, !Y

TitleWindow_SetFullSize:
  clr r4
  clr r5
  clr r6
  clr r7
  ldi r16, LOW(DISPLAY_WIDTH)
  mov r8, r16
  ldi r16, HIGH(DISPLAY_WIDTH)
  mov r9, r16
  ldi r16, LOW(DISPLAY_HEIGHT)
  mov r10, r16
  ldi r16, HIGH(DISPLAY_HEIGHT)
  mov r11, r16
  rcall TitleWindow_SetPosAndSize
  ret
; @enb



; ---------------------------------------------------------------------------
; @routine TitleWindow_SetStyleColors @global
;
; @param Y pointer to screen object in SDRAM
; @clobbers any, !Y

TitleWindow_SetStyleColors:
  ldi r16, LOW(STYLE_WIN_BACKGROUND)
  ldi r17, HIGH(STYLE_WIN_BACKGROUND)
  std Y+WIN_OFFS_BG_COL_LO, r16
  std Y+WIN_OFFS_BG_COL_HI, r17
  ldi r16, LOW(STYLE_WIN_FOREGROUND)
  ldi r17, HIGH(STYLE_WIN_FOREGROUND)
  std Y+WIN_OFFS_FG_COL_LO, r16
  std Y+WIN_OFFS_FG_COL_HI, r17

  ; setup title window
  adiw yh:yl, TITLEWINDOW_OFFS_TITLEWIN
  ldi r16, LOW(STYLE_WIN_TITLE_BACKGROUND)
  ldi r17, HIGH(STYLE_WIN_TITLE_BACKGROUND)
  std Y+WIN_OFFS_BG_COL_LO, r16
  std Y+WIN_OFFS_BG_COL_HI, r17
  ldi r16, LOW(STYLE_WIN_TITLE_FOREGROUND)
  ldi r17, HIGH(STYLE_WIN_TITLE_FOREGROUND)
  std Y+WIN_OFFS_FG_COL_LO, r16
  std Y+WIN_OFFS_FG_COL_HI, r17
  sbiw yh:yl, TITLEWINDOW_OFFS_TITLEWIN

  ; setup content window
  adiw yh:yl, TITLEWINDOW_OFFS_CONTENTWIN
  ldi r16, LOW(STYLE_WIN_BACKGROUND)
  ldi r17, HIGH(STYLE_WIN_BACKGROUND)
  std Y+WIN_OFFS_BG_COL_LO, r16
  std Y+WIN_OFFS_BG_COL_HI, r17
  ldi r16, LOW(STYLE_WIN_FOREGROUND)
  ldi r17, HIGH(STYLE_WIN_FOREGROUND)
  std Y+WIN_OFFS_FG_COL_LO, r16
  std Y+WIN_OFFS_FG_COL_HI, r17
  sbiw yh:yl, TITLEWINDOW_OFFS_CONTENTWIN

  ret
; @end



; ---------------------------------------------------------------------------
; @routine TitleWindow_Draw @global
;
; @param Y pointer to titledwindow data (size=TITLEWINDOW_SIZE)

TitleWindow_Draw:
  ; draw title window
  adiw yh:yl, TITLEWINDOW_OFFS_TITLEWIN
  rcall Window_Clear
  sbiw yh:yl, TITLEWINDOW_OFFS_TITLEWIN

  ; write header
  ldd zl, Y+TITLEWINDOW_OFFS_TITLEPTR_LO
  ldd zh, Y+TITLEWINDOW_OFFS_TITLEPTR_HI
  adiw yh:yl, TITLEWINDOW_OFFS_TITLEWIN
  ldi r16, 5
  mov r4, r16
  clr r5
  ldi r16, 2
  mov r6, r16
  clr r7
  rcall Window_DrawTextFlash
  sbiw yh:yl, TITLEWINDOW_OFFS_TITLEWIN

  ; clear content window
  adiw yh:yl, TITLEWINDOW_OFFS_CONTENTWIN
  rcall Window_Clear
  sbiw yh:yl, TITLEWINDOW_OFFS_CONTENTWIN
  
  ret
; @end




#endif
