; ***************************************************************************
; 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

.equ IMAGEVIEW_OFFS_BEGIN           =  WIDGET_SIZE
.equ IMAGEVIEW_OFFS_RESSOURCEID_LO  =  IMAGEVIEW_OFFS_BEGIN+0
.equ IMAGEVIEW_OFFS_RESSOURCEID_HI  =  IMAGEVIEW_OFFS_BEGIN+1
.equ IMAGEVIEW_OFFS_BGCOLOR_LO      =  IMAGEVIEW_OFFS_BEGIN+2
.equ IMAGEVIEW_OFFS_BGCOLOR_HI      =  IMAGEVIEW_OFFS_BEGIN+3
.equ IMAGEVIEW_SIZE                 =  IMAGEVIEW_OFFS_BEGIN+4


.equ IMAGEVIEW_VALUE_BGCOL          = WIDGET_VALUE_NEXTFREE+0
.equ IMAGEVIEW_VALUE_NEXTFREE       = WIDGET_VALUE_NEXTFREE+1



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

.cseg



; ---------------------------------------------------------------------------
; @routine ImageView_new @global
;
; @return CFLAG set of okay, cleared otherwise
; @return Y address of newly created object
; @param X parent widget
; @param r16 value for OBJECT_OFFS_OPTS
; @param r17 value for WIDGET_OFFS_PACK
; @param r21:r20 ressource id for image
; @clobbers any

ImageView_new:
  ldi r24, LOW(IMAGEVIEW_SIZE)
  ldi r25, HIGH(IMAGEVIEW_SIZE)
  push r20
    push r21
      bigcall Object_Alloc           ; (!r16, !r17, !X)
    pop r21
  pop r20
  brcc ImageView_new_ret
  rcall ImageView_Init               ; (r16, r17, X)
  sec
ImageView_new_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine ImageView_Init @global
;
; @param Y address of widget
; @param X parent widget (if any)
; @param r16 value for OBJECT_OFFS_OPTS
; @param r17 value for WIDGET_OFFS_PACK
; @param r21:r20 ressource id for image
; @clobbers r16, r17, X

ImageView_Init:
  push r20
    push r21
      ; call base class
      bigcall Widget_Init                       ; (r16, r17, X)
    pop r21
  pop r20
  
  ; setup label data
  std Y+IMAGEVIEW_OFFS_RESSOURCEID_LO, r20
  std Y+IMAGEVIEW_OFFS_RESSOURCEID_HI, r21
  
  ; set default signal map
  ldi r16, LOW(ImageView_DefaultSignalmap*2)
  std Y+OBJECT_OFFS_SIGNALMAP_LO, r16
  ldi r16, HIGH(ImageView_DefaultSignalmap*2)
  std Y+OBJECT_OFFS_SIGNALMAP_HI, r16

  ret
; @end



; ---------------------------------------------------------------------------
; @routine ImageView_SetRessourceId @global
;
; @param Y address of widget
; @param X id of text ressource
; @clobbers r16, r17, r18, r19

ImageView_SetRessourceId:
  std Y+IMAGEVIEW_OFFS_RESSOURCEID_LO, xl
  std Y+IMAGEVIEW_OFFS_RESSOURCEID_HI, xh
  ; force redraw
  ldd r16, Y+OBJECT_OFFS_FLAGS
  ori r16, (1<<WIDGET_FLAGS_DIRTY_BIT)
  std Y+OBJECT_OFFS_FLAGS, r16
;  ; force layout of this and all parent widgets
;  ldi r16, (1<<WIDGET_FLAGS_LAYOUT_BIT)
;  bigcall OBJ_AddFlagsUp                 ; (r17, r18, r19)
  ret
; @end



; ---------------------------------------------------------------------------
; @routine ImageView_OnDraw @global
;
; @param Y address of widget
; @clobbers any, !Y

ImageView_OnDraw:
  ldd r16, Y+OBJECT_OFFS_FLAGS
  andi r16, (1<<WIDGET_FLAGS_DIRTY_BIT)
  breq ImageView_OnDraw_ret

;  bigcall Widget_Clear
  rcall imageViewDraw
  
  ldd r16, Y+OBJECT_OFFS_OPTS
  andi r16, (1<<WIDGET_OPTS_BORDER_BIT)
  breq ImageView_OnDraw_done
  bigcall Widget_DrawBorder

ImageView_OnDraw_done:
  ldd r16, Y+OBJECT_OFFS_FLAGS
  cbr r16, (1<<WIDGET_FLAGS_DIRTY_BIT)
  std Y+OBJECT_OFFS_FLAGS, r16

ImageView_OnDraw_ret:
  sec
  ret
; @end



; ---------------------------------------------------------------------------
; @routine ImageView_OnGetDefaultWidth @global
;
; @param Y address of widget
; @param r17 value requested
; @param xl param1
; @param xh param2
; @return CFLAG set if signal handled
; @return r19:r18 value
; @clobbers any, !Y

ImageView_OnGetDefaultWidth:
  rcall imageViewGetRessource
  brcs ImageView_OnGetDefaultWidth_getImageWidth
  ldi r18, 1
  clr r19
  rjmp ImageView_OnGetDefaultWidth_addBorders
ImageView_OnGetDefaultWidth_getImageWidth:
  rcall imageViewGetImageWidth
  mov r18, r12
  mov r19, r13
ImageView_OnGetDefaultWidth_addBorders:
  bigcall Widget_AddOuterStyleBorders  ; (r20, r21)
  sec
  ret
; @end



; ---------------------------------------------------------------------------
; @routine ImageView_OnGetDefaultHeight @global
;
; @param Y address of widget
; @param xl param1
; @param xh param2
; @return CFLAG set if signal handled
; @return r19:r18 value
; @clobbers any, !Y

ImageView_OnGetDefaultHeight:
  rcall imageViewGetRessource
  brcs ImageView_OnGetDefaultHeight_getImageHeight
  ldi r18, 1
  clr r19
  rjmp ImageView_OnGetDefaultHeight_addBorders
ImageView_OnGetDefaultHeight_getImageHeight:
  rcall imageViewGetImageHeight
  mov r18, r12
  mov r19, r13
ImageView_OnGetDefaultHeight_addBorders:
  bigcall Widget_AddOuterStyleBorders  ; (r20, r21)
  sec
  ret
; @end



; ---------------------------------------------------------------------------
; @routine ImageView_OnDraw @global
;
; @param Y address of widget
; @clobbers any, !Y

imageViewDraw:
  rcall imageViewGetRessource
  brcc imageViewDraw_ret
  rcall imageViewAlignContentXY ; (r17, r18, r19, r20, r21, Z)
  ldd r0, Y+IMAGEVIEW_OFFS_BGCOLOR_LO
  ldd r1, Y+IMAGEVIEW_OFFS_BGCOLOR_HI
  ; select background color
  mov r16, r0
  or r16, r1
  breq imageViewDraw_useStyleColors
  ldd r16, Y+OBJECT_OFFS_FLAGS
  sbrc r16, WIDGET_FLAGS_ACTIVATED_BIT
    rjmp imageViewDraw_useStyleColors
  rjmp imageViewDraw_draw
imageViewDraw_useStyleColors:
  bigcall Widget_SelectColors           ; (R16)
imageViewDraw_draw:
  bigcall Widget_DrawImage
imageViewDraw_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine ImageView_OnSetBgCol @global
;
; @param Y address of widget
; @param X new background color
; @clobbers any, !Y

ImageView_OnSetBgCol:
  std Y+IMAGEVIEW_OFFS_BGCOLOR_LO, xl
  std Y+IMAGEVIEW_OFFS_BGCOLOR_HI, xh
  
  ldd r16, Y+OBJECT_OFFS_FLAGS
  ori r16, (1<<WIDGET_FLAGS_DIRTY_BIT)
  std Y+OBJECT_OFFS_FLAGS, r16
  sec
  ret
; @end




; ---------------------------------------------------------------------------
; @routine imageViewGetRessource
;
; @param  Y     address of widget
; @return CFLAG set if ressource found, cleared on error
; @return Z     pointer to ressource

imageViewGetRessource:
  ldd r16, Y+IMAGEVIEW_OFFS_RESSOURCEID_LO
  ldd r17, Y+IMAGEVIEW_OFFS_RESSOURCEID_HI
  bigjmp Widget_GetRessource               ; Z=ressource (r16, r17, r18)
; @end



; ---------------------------------------------------------------------------
; @routine imageViewAlignContentXY
;
; @param  Y       address of widget
; @param  Z       byte address pointer to image (for LPM!)
; @return R5:R4   X
; @return R7:R6   Y
; @clobbers r17, r18, r19, r20, r21, Z

imageViewAlignContentXY:
  rcall imageViewGetImageWidth  ; R13:R12=width (r16, r17, r18, Z)
  bigcall Widget_PackContentX     ; R5:R4=X (r17, r18, r19, r20, r21)

  rcall imageViewGetImageHeight ; R13:R12=height (r16, r17, r18, Z)
  bigcall Widget_PackContentY     ; R7:R6=Y (r17, r18, r19, r20, r21)
  ret
; @end



; ---------------------------------------------------------------------------
; @routine imageViewGetImageWidth
;
; @param Y address of widget
; @return Z byte address pointer to image in FLASH
; @return r13:r12 image width
; @clobbers r16, r17, r18, Z

imageViewGetImageWidth:
  adiw zh:zl, RES_IMAGE_OFFS_WIDTH_LO
  lpm r12, Z+
  lpm r13, Z
  sbiw zh:zl, (RES_IMAGE_OFFS_WIDTH_LO+1)
  sec
imageViewGetImageWidth_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine imageViewGetImageHeight
;
; @param Y address of widget
; @param Z byte address pointer to image ressource
; @return r13:r12 image height
; @clobbers r16, r17, r18

imageViewGetImageHeight:
  adiw zh:zl, RES_IMAGE_OFFS_HEIGHT_LO
  lpm r12, Z+
  lpm r13, Z
  sbiw zh:zl, (RES_IMAGE_OFFS_HEIGHT_LO+1)
  sec
imageViewGetImageHeight_ret:
  ret
; @end





; ***************************************************************************
; data in FLASH

ImageView_DefaultSignalmap:
  ; header
  .dw Widget_DefaultSignalmap*2  ; next table to use
  ; entries
  .db 0,                           WIDGET_SIGNAL_DRAW,     LOW(ImageView_OnDraw),             HIGH(ImageView_OnDraw)
  .db WIDGET_VALUE_DEFAULT_WIDTH,  WIDGET_SIGNAL_GETVALUE, LOW(ImageView_OnGetDefaultWidth),  HIGH(ImageView_OnGetDefaultWidth)
  .db WIDGET_VALUE_DEFAULT_HEIGHT, WIDGET_SIGNAL_GETVALUE, LOW(ImageView_OnGetDefaultHeight), HIGH(ImageView_OnGetDefaultHeight)
  .db IMAGEVIEW_VALUE_BGCOL,       WIDGET_SIGNAL_SETVALUE, LOW(ImageView_OnSetBgCol),         HIGH(ImageView_OnSetBgCol)

  .db 0, 0, 0, 0  ; end of table


#endif

