; ***************************************************************************
; copyright   : (C) 2026 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_GUI2_IMAGEVIEW_ASM
#define AQH_AVR_GUI2_IMAGEVIEW_ASM


; ***************************************************************************
; defines

; IMGVIEW in flash
.equ IMGVIEW_OFFS_IMGVIEW           =  WIDGET_SIZE
.equ IMGVIEW_OFFS_RESSOURCEID_LO    =  IMGVIEW_OFFS_IMGVIEW+0
.equ IMGVIEW_OFFS_RESSOURCEID_HI    =  IMGVIEW_OFFS_IMGVIEW+1
.equ IMGVIEW_SIZE                   =  IMGVIEW_OFFS_IMGVIEW+2


; SDRAM data for IMGVIEW
.equ IMGVIEW_SD_OFFS_IMGVIEW   = WIDGET_SD_SIZE
.equ IMGVIEW_SD_OFFS_BGCOL_LO  = IMGVIEW_SD_OFFS_IMGVIEW+0
.equ IMGVIEW_SD_OFFS_BGCOL_HI  = IMGVIEW_SD_OFFS_IMGVIEW+1
.equ IMGVIEW_SD_SIZE           = IMGVIEW_SD_OFFS_IMGVIEW+2


; signals
.equ IMGVIEW_SIGNAL_SETBGCOL       =  WIDGET_SIGNAL_NEXTFREE+0
.equ IMGVIEW_SIGNAL_NEXTFREE       =  WIDGET_SIGNAL_NEXTFREE+1



; ***************************************************************************
; code

.cseg



; ---------------------------------------------------------------------------
; @routine ImageView_OnCreate @global
;
; @param Z byte address of widget object (for LPM!)
; @return CFLAG set if signal handled
; @clobbers any, !Z

ImageView_OnCreate:
  bigcall Widget_OnCreate
  bigcall Widget_GetSdramPtr            ; (none)

  adiw zh:zl, WIDGET_OFFS_BACKCOL_LO
  lpm r16, Z+
  lpm r17, Z
  sbiw zh:zl, WIDGET_OFFS_BACKCOL_LO+1
  std Y+IMGVIEW_SD_OFFS_BGCOL_LO, r16
  std Y+IMGVIEW_SD_OFFS_BGCOL_HI, r17
  sec
  ret
; @end



; ---------------------------------------------------------------------------
; @routine ImageView_OnSetBgCol @global
;
; @param Z byte address of widget object (for LPM!)
; @param X new value for background color
; @return CFLAG set if signal handled
; @clobbers r17

ImageView_OnSetBgCol:
  bigcall OBJ_IsObject                   ; (none)
  brcc ImageView_OnSetBgCol_ret
  push zl
    push zh
      rcall Widget_GetSdramPtr           ; (none)
      std Y+IMGVIEW_SD_OFFS_BGCOL_LO, xl
      std Y+IMGVIEW_SD_OFFS_BGCOL_HI, xh
      ldd r17, Y+WIDGET_SD_OFFS_FLAGS
      ori r17, (1<<WIDGET_FLAGS_DIRTY_BIT)
      std Y+WIDGET_SD_OFFS_FLAGS, r17
    pop zh
  pop zl
ImageView_OnSetBgCol_ret:
  sec
  ret
; @end



; ---------------------------------------------------------------------------
; @routine Widget_OnDraw @global
;
; @param Z byte address of widget object (for LPM!)
; @return CFLAG set if signal handled
; @clobbers any, !Z

ImageView_OnDraw:
  bigcall Widget_GetSdramPtr       ; (none)
  ldd r16, Y+WIDGET_SD_OFFS_FLAGS
  andi r16, (1<<WIDGET_FLAGS_DIRTY_BIT)
  breq ImageView_OnDraw_ret

;  rcall Widget_Clear

  adiw zh:zl, WIDGET_OFFS_OPTS_LO
  lpm r14, Z
  sbiw zh:zl, WIDGET_OFFS_OPTS_LO

  push zl
    push zh
      ; get ressource id
      adiw zh:zl, IMGVIEW_OFFS_RESSOURCEID_LO
      lpm r16, Z+
      lpm r17, Z
      sbiw zh:zl, IMGVIEW_OFFS_RESSOURCEID_LO+1
      ; get ressource
      ldi zl, LOW(RESSOURCE_ADDR*2)
      ldi zh, HIGH(RESSOURCE_ADDR*2)
      bigcall RES_GetRessource
      mov r12, zl
      mov r13, zh
    pop zh
  pop zl
  brcc ImageView_OnDraw_done

  ; get background color
  ldd r16, Y+WIDGET_SD_OFFS_FLAGS
  andi r16, (1<<WIDGET_FLAGS_PRESSED_BIT)
  breq ImageView_OnDraw_useStoredBgCol
  ldi r16, LOW(STYLE_BUTTON_COL_BG_PRESSED)
  mov r0, r16
  ldi r16, HIGH(STYLE_BUTTON_COL_BG_PRESSED)
  mov r1, r16
  rjmp ImageView_OnDraw_getPos

ImageView_OnDraw_useStoredBgCol:
  ldd r0, Y+IMGVIEW_SD_OFFS_BGCOL_LO
  ldd r1, Y+IMGVIEW_SD_OFFS_BGCOL_HI

ImageView_OnDraw_getPos:
  ; draw at 0/0
  clr r4         ; X
  clr r5
  clr r6         ; Y
  clr r7
  sbrs r14, WIDGET_OPTSLO_BORDER_BIT
    rjmp ImageView_OnDraw_draw
  ldi r16, 2
  clr r17
  add r4, r16
  adc r5, r17
  add r6, r16
  adc r7, r17

ImageView_OnDraw_draw:
  bigcall Widget_DrawImage
  sbrs r14, WIDGET_OPTSLO_BORDER_BIT
    rjmp ImageView_OnDraw_done
  bigcall Widget_DrawBorder
ImageView_OnDraw_done:
  bigcall Widget_GetSdramPtr       ; (none)
  ldd r16, Y+WIDGET_SD_OFFS_FLAGS
  cbr r16, (1<<WIDGET_FLAGS_DIRTY_BIT)
  std Y+WIDGET_SD_OFFS_FLAGS, r16

ImageView_OnDraw_ret:
  sec
  ret
; @end



ImageView_DefaultSignalmap:
  .db 0, WIDGET_SIGNAL_DRAW,      LOW(ImageView_OnDraw),     HIGH(ImageView_OnDraw)
  .db 0, IMGVIEW_SIGNAL_SETBGCOL, LOW(ImageView_OnSetBgCol), HIGH(ImageView_OnSetBgCol)
  .db 0, OBJECT_SIGNAL_CREATE,    LOW(ImageView_OnCreate),   HIGH(ImageView_OnCreate)
  .db 0, 0, 0, 0  ; end of table



#endif

