(include "sci.sh")
(use "Main")
(use "Print")
(use "InvI")
(use "Obj")
(script 26)



(class ScrollableInventory of Inv
    (properties
        elements 0
        size 0
        height 0
        underBits 0
        oldMouseX 0
        oldMouseY 0
        curIcon 0
        highlightedIcon 0
        prevIcon 0
        curInvIcon 0
        useIconItem 0
        helpIconItem 0
        walkIconItem 0
        port 0
        window 0
        state 1024
        activateHeight 0
        y 0
        normalHeading -1
        heading 0
        empty -1
        iconBarInvItem 0
        okButton 0
        selectIcon 0
        curPos 0
        dispAmount 12
        items 0
        numCols 6
        numRows 2
        scrollAmount 6
        firstThru 1
        upIcon 0
        downIcon 0
    )

    (method (dispose param1)
        (if (IsObject(items))
            (send items:dispose())
            = items 0
        )
        (super:dispose(rest param1))
    )


    (method (hide param1)
        (if (IsObject(items))
            (send items:dispose())
            = items 0
        )
        = firstThru 1
        (super:hide(rest param1))
    )


    (method (advance param1)
        (var temp0, temp1, temp2, temp3)
        (asm
            lap     paramTotal
            bnt     code_0080
            lap     param1
            jmp     code_0082
code_0080:  ldi     1
code_0082:  sat     temp1
            pushi   #indexOf
            pushi   1
            pTos    highlightedIcon
            self    6
            sat     temp2
            lst     temp1
            add     
            push    
            pToa    size
            mod     
            sat     temp3
code_0097:  pushi   #at
            pushi   1
            lst     temp3
            self    6
            sat     temp0
            pushi   1
            push    
            callk   IsObject, 2
            bnt     code_00d3
            pushi   #signal
            pushi   0
            lat     temp0
            send    4
            push    
            ldi     4
            and     
            not     
            bnt     code_00d3
            pushi   #nsLeft
            pushi   0
            lat     temp0
            send    4
            push    
            ldi     65535
            gt?     
            bt      code_00cf
            pushi   #isKindOf
            pushi   1
            class   InvI
            push    
            lat     temp0
            send    6
            not     
            bnt     code_00d3
code_00cf:  jmp     code_00e0
            jmp     code_0097
code_00d3:  lst     temp3
            ldi     1
            add     
            push    
            pToa    size
            mod     
            sat     temp3
            jmp     code_0097
code_00e0:  pushi   #highlight
            pushi   2
            lst     temp0
            pushi   1
            self    8
            ret     
        )
    )


    (method (retreat param1)
        (var temp0, temp1, temp2, temp3)
        (asm
            lap     paramTotal
            bnt     code_00f4
            lap     param1
            jmp     code_00f6
code_00f4:  ldi     1
code_00f6:  sat     temp1
            pushi   #indexOf
            pushi   1
            pTos    highlightedIcon
            self    6
            sat     temp2
            push    
            lat     temp1
            sub     
            sat     temp3
            push    
            ldi     0
            lt?     
            bnt     code_0115
            pTos    size
            ldi     1
            sub     
            sat     temp3
code_0115:  pushi   #at
            pushi   1
            lst     temp3
            self    6
            sat     temp0
            pushi   1
            push    
            callk   IsObject, 2
            bnt     code_0151
            pushi   #signal
            pushi   0
            lat     temp0
            send    4
            push    
            ldi     4
            and     
            not     
            bnt     code_0151
            pushi   #nsLeft
            pushi   0
            lat     temp0
            send    4
            push    
            ldi     65535
            gt?     
            bt      code_014d
            pushi   #isKindOf
            pushi   1
            class   InvI
            push    
            lat     temp0
            send    6
            not     
            bnt     code_0151
code_014d:  jmp     code_0162
            jmp     code_0115
code_0151:  -at     temp3
            push    
            ldi     0
            lt?     
            bnt     code_0115
            pTos    size
            ldi     1
            sub     
            sat     temp3
            jmp     code_0115
code_0162:  pushi   #highlight
            pushi   2
            lst     temp0
            pushi   1
            self    8
            ret     
        )
    )


    (method (drawInvWindow param1 param2)
        (var temp0, temp1, temp2, temp3, temp4, temp5, temp6, temp7, ScrollableInventoryFirst, temp9, temp10, temp11, temp12, temp13, temp14, temp15, temp16, temp17, temp18, theNumCols, theCurPos, ScrollableInventoryWindow)
        = theCurPos (= temp0 (= temp1 (= temp2 (= temp3 (= temp4 (= temp5 0))))))
        (if (firstThru)
            (if (IsObject(items))
                (send items:dispose())
                = items 0
            )
            = items (Set:new())
        )
        = ScrollableInventoryFirst (self:first())
        (while (ScrollableInventoryFirst)
            (if ((send (= temp9 NodeValue(ScrollableInventoryFirst)):isKindOf(InvI)))
                (if ((send temp9:ownedBy(param1)))
                    (send temp9:signal((& (send temp9:signal) $fffb)))
                    (send items:add(temp9))
                    (send temp9:
                        nsLeft(65531)
                        nsRight(65531)
                        nsTop(65531)
                        nsBottom(65531)
                    )
                    (if (> (= temp6 CelWide((send temp9:view) (send temp9:loop) (send temp9:cel))) temp2)
                        = temp2 temp6
                    )
                    (if (> (= temp7 CelHigh((send temp9:view) (send temp9:loop) (send temp9:cel))) temp1)
                        = temp1 temp7
                    )
                )(else
                    (send temp9:signal((| (send temp9:signal) $0004)))
                )
            )(else
                ++temp3
                = temp5 (+ temp5 CelWide((send temp9:view) (send temp9:loop) (send temp9:cel)))
                (if (> (= temp7 CelHigh((send temp9:view) (send temp9:loop) (send temp9:cel))) temp4)
                    = temp4 temp7
                )
            )
            = ScrollableInventoryFirst (self:next(ScrollableInventoryFirst))
        )
        (if (not (send items:size))
            (if ((<= 0 normalHeading) and (<= 0 empty))
                (Print:
                    addText(empty 0 0 0 0 0 normalHeading)
                    init()
                )
            )(else
                proc921_0("You'll get nothing and like it!")
            )
            (if (IsObject(items))
                (send items:dispose())
            )
            return 0
        )
        = temp0 
            (if (< (send items:size) dispAmount)
                (send items:size)
            )(else
                dispAmount
            )
        = temp10 proc999_3((+ 4 temp5) (* numCols (+ 4 temp2)))
        = temp12 (/ (- 190 (= temp11 (* numRows (+ 4 temp1)))) 2)
        = temp13 (/ (- 320 temp10) 2)
        = temp14 (+ temp12 temp11)
        = temp15 (+ temp13 temp10)
        (if (= ScrollableInventoryWindow (self:window))
            (send ScrollableInventoryWindow:
                top(temp12)
                left(temp13)
                right(temp15)
                bottom(temp14)
                open(not firstThru)
            )
        )
        = theNumCols numCols
        (if (temp0)
            = temp17 (+ 2 
                (if ((send ScrollableInventoryWindow:respondsTo(367)))
                    (send ScrollableInventoryWindow:yOffset)
                )(else
                    0
                )
)
            = temp18 (= temp16 (+ 4 
                    (if ((send ScrollableInventoryWindow:respondsTo(366)))
                        (send ScrollableInventoryWindow:xOffset)
                    )(else
                        0
                    )
))
            = theCurPos curPos
            (while ((< theCurPos (+ curPos dispAmount)) and (< theCurPos (send items:size)))
                (if (not & (send ((= temp9 (send items:at(theCurPos)))):signal) $0080)
                    (send temp9:
                        nsLeft(+ temp16 (/ (- temp2 (= temp6 CelWide((send temp9:view) (send temp9:loop) (send temp9:cel)))) 2))
                        nsTop(+ temp17 (/ (- temp1 (= temp7 CelHigh((send temp9:view) (send temp9:loop) (send temp9:cel)))) 2))
                    )
                    (send temp9:
                        nsRight(+ (send temp9:nsLeft) temp6)
                        nsBottom(+ (send temp9:nsTop) temp7)
                    )
                    (if (--theNumCols)
                        = temp16 (+ temp16 temp2)
                    )(else
                        = theNumCols numCols
                        = temp17 (+ temp17 temp1)
                        = temp16 temp18
                    )
                )(else
                    = temp16 (send temp9:nsLeft)
                    = temp17 (send temp9:nsTop)
                )
                (send temp9:show())
                (if (== temp9 param2)
                    (send temp9:highlight())
                )
                ++theCurPos
            )
        )
        = temp16 (/ (- (- (send ScrollableInventoryWindow:right) (send ScrollableInventoryWindow:left)) temp5) 2)
        = temp11 (- (send ScrollableInventoryWindow:bottom) (send ScrollableInventoryWindow:top))
        = temp17 32767
        (if (firstThru)
            = ScrollableInventoryFirst (self:first())
            (while (ScrollableInventoryFirst)
                (if (not (send (= temp9 NodeValue(ScrollableInventoryFirst)):isKindOf(InvI)))
                    = temp6 CelWide((send temp9:view) (send temp9:loop) (send temp9:cel))
                    = temp7 CelHigh((send temp9:view) (send temp9:loop) (send temp9:cel))
                    (if (not & (send temp9:signal) $0080)
                        (if (== temp17 32767)
                            = temp17 (- temp11 temp7)
                        )
                        (send temp9:
                            nsLeft(temp16)
                            nsTop(temp17)
                            nsBottom(+ temp17 temp7)
                            nsRight(+ temp16 temp6)
                        )
                    )
                    = temp16 (+ (send temp9:nsLeft) temp6)
                    = temp17 (send temp9:nsTop)
                    (send temp9:signal((& (send temp9:signal) $fffb)))
                    (send temp9:show())
                )
                = ScrollableInventoryFirst (self:next(ScrollableInventoryFirst))
            )
        )
        (if (not curPos)
            (send upIcon:signal((| (send upIcon:signal) $0004)))
        )(else
            (send upIcon:signal((& (send upIcon:signal) $fffb)))
        )
        (if (>= curPos (- (send items:size) dispAmount))
            (send downIcon:signal((| (send downIcon:signal) $0004)))
        )(else
            (send downIcon:signal((& (send downIcon:signal) $fffb)))
        )
        (send upIcon:show())
        (send downIcon:show())
        return 1
    )


    (method (scroll param1)
        (if (paramTotal and (> 0 param1))
            (if (< (= curPos (- curPos scrollAmount)) 0)
                = curPos 0
            )
        )(else
            (if (> (= curPos (+ curPos scrollAmount)) (- size dispAmount))
                = curPos (- size dispAmount)
            )
        )
        = firstThru 0
        (send selectIcon:select())
        (self:show(gEgo))
    )

)
