(version 2)
(include "sci.sh")
(use "Main")
(use "Print")
(use "Obj")
(script 937)



(class IconI of Obj
    (properties
        view -1
        loop -1
        cel -1
        nsLeft 0
        nsTop -1
        nsRight 0
        nsBottom 0
        state $0000
        cursor -1
        type $4000
        message -1
        modifiers $0000
        signal $0001
        maskView 0
        maskLoop 0
        maskCel 0
        highlightColor 0
        lowlightColor 0
        noun 0
        modNum 0
        helpVerb 0
    )

    (method (show theNsLeft theNsTop)
        (var temp0[7])
        = signal (| signal $0020)
        (if (paramTotal)
            = nsRight (+ (= nsLeft theNsLeft) CelWide(view loop cel))
            = nsBottom (+ (= nsTop theNsTop) CelHigh(view loop cel))
        )(else
            = nsRight (+ nsLeft CelWide(view loop cel))
            = nsBottom (+ nsTop CelHigh(view loop cel))
        )
        DrawCel(view loop cel nsLeft nsTop -1)
        (if (& signal $0004)
            (self:mask())
        )
        (if (gPseudoMouse and (send gPseudoMouse:respondsTo(#stop)))
            (send gPseudoMouse:stop())
        )
    )


    (method (select param1)
        (var newEvent, temp1)
        return 
            (if (& signal $0004)
                0
            )(else
                (if ((paramTotal and param1) and (& signal $0001))
                    = temp1 1
                    DrawCel(view loop temp1 nsLeft nsTop -1)
                    Graph(grUPDATE_BOX nsTop nsLeft nsBottom nsRight 1)
                    (while (<> (send ((= newEvent (Event:new()))):type) 2)
                        (send newEvent:localize())
                        (if ((self:onMe(newEvent)))
                            (if (not temp1)
                                = temp1 1
                                DrawCel(view loop temp1 nsLeft nsTop -1)
                                Graph(grUPDATE_BOX nsTop nsLeft nsBottom nsRight 1)
                            )
                        )(else
                            (if (temp1)
                                = temp1 0
                                DrawCel(view loop temp1 nsLeft nsTop -1)
                                Graph(grUPDATE_BOX nsTop nsLeft nsBottom nsRight 1)
                            )
                        )
                        (send newEvent:dispose())
                    )
                    (send newEvent:dispose())
                    (if (== temp1 1)
                        DrawCel(view loop 0 nsLeft nsTop -1)
                        Graph(grUPDATE_BOX nsTop nsLeft nsBottom nsRight 1)
                    )
                    temp1
                )(else
                    1
                )
            )
    )


    (method (highlight param1)
        (var temp0, temp1, temp2, temp3, temp4)
        (if (not (& signal $0020) or (== highlightColor -1))
            return 
        )
        = temp4 
            (if (paramTotal and param1)
                highlightColor
            )(else
                lowlightColor
            )
        = temp0 (+ nsTop 2)
        = temp1 (+ nsLeft 2)
        = temp2 (- nsBottom 3)
        = temp3 (- nsRight 4)
        Graph(grDRAW_LINE temp0 temp1 temp0 temp3 temp4 -1 -1)
        Graph(grDRAW_LINE temp0 temp3 temp2 temp3 temp4 -1 -1)
        Graph(grDRAW_LINE temp2 temp3 temp2 temp1 temp4 -1 -1)
        Graph(grDRAW_LINE temp2 temp1 temp0 temp1 temp4 -1 -1)
        Graph(grUPDATE_BOX - nsTop 2 - nsLeft 2 nsBottom + nsRight 3 1)
    )


    (method (onMe param1)
        return 
            (if (((>= (send param1:x) nsLeft) and (>= (send param1:y) nsTop)) and (<= (send param1:x) nsRight))
                <= (send param1:y) nsBottom
            )(else
                0
            )
    )


    (method (mask)
        DrawCel(maskView maskLoop maskCel + nsLeft (/ (- CelWide(view loop cel) CelWide(maskView maskLoop maskCel)) 2) + nsTop (/ (- CelHigh(view loop cel) CelHigh(maskView maskLoop maskCel)) 2) -1)
    )

)
(class IconBar of Set
    (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 $0400
        activateHeight 0
        y 0
    )

    (method (doit)
        (var temp0, temp1, temp2, temp3)
        (asm
code_0a6c:  pTos    state
            ldi     32
            and     
            bnt     code_0b47
            pushi   #new
            pushi   0
            pushi   #curEvent
            pushi   0
            lag     gUser
            send    4
            send    4
            sat     temp0
            bnt     code_0b47
            pushi   #type
            pushi   0
            lat     temp0
            send    4
            sat     temp1
            pushi   #message
            pushi   0
            lat     temp0
            send    4
            sat     temp2
            pushi   #modifiers
            pushi   0
            lat     temp0
            send    4
            sat     temp3
            pushi   1
            pushi   1
            callk   Wait, 2
            lsg     global86
            pushi   0
            callk   GetTime, 0
            add     
            sag     gLastTicks
            lag     gNewSet
            bnt     code_0aba
            pushi   #eachElementDo
            pushi   1
            pushi   57
            send    6
code_0aba:  lst     temp1
            ldi     256
            eq?     
            bnt     code_0aeb
            ldi     4
            sat     temp1
            lst     temp3
            ldi     3
            and     
            bnt     code_0ad1
            ldi     27
            jmp     code_0ad3
code_0ad1:  ldi     13
code_0ad3:  sat     temp2
            ldi     0
            sat     temp3
            pushi   #type
            pushi   1
            lst     temp1
            pushi   37
            pushi   1
            lst     temp2
            pushi   61
            pushi   1
            push    
            lat     temp0
            send    18
code_0aeb:  pushi   #localize
            pushi   0
            lat     temp0
            send    4
            lst     temp1
            ldi     1
            eq?     
            bt      code_0b0a
            lst     temp1
            ldi     4
            eq?     
            bnt     code_0b34
            lst     temp2
            ldi     13
            eq?     
            bnt     code_0b34
code_0b0a:  pushi   1
            pTos    helpIconItem
            callk   IsObject, 2
            bnt     code_0b34
            pushi   #signal
            pushi   0
            pToa    helpIconItem
            send    4
            push    
            ldi     16
            and     
            bnt     code_0b34
            pushi   #type
            pushi   1
            pushi   24576
            pushi   37
            pushi   1
            pushi   #message
            pushi   0
            pToa    helpIconItem
            send    4
            push    
            lat     temp0
            send    12
code_0b34:  pushi   1
            lst     temp0
            callk   MapKeyToDir, 2
            pushi   #dispatchEvent
            pushi   1
            lst     temp0
            self    6
            bnt     code_0b44
code_0b44:  jmp     code_0a6c
code_0b47:  ret     
        )
    )


    (method (handleEvent pEvent)
        (var temp0, pEventType, temp2, temp3, theGCursorNumber, theCurIcon, theCurInvIcon)
        (send pEvent:localize())
        = pEventType (send pEvent:type)
        (if (& state $0004)
        )(else
            (if (((((((not pEventType and (& state $0400)) and (<= -10 (send pEvent:y))) and (<= (send pEvent:y) height)) and (<= 0 (send pEvent:x))) and (<= (send pEvent:x) 320)) and not (= temp0 0)) or (((== pEventType 4) and ((== (send pEvent:message) 27) or (== (send pEvent:message) 21248))) and (= temp0 1)))
                (send pEvent:globalize())
                = oldMouseX (send pEvent:x)
                = oldMouseY (send pEvent:y)
                = theGCursorNumber gCursorNumber
                = theCurIcon curIcon
                = theCurInvIcon curInvIcon
                (self:show())
                (send gGame:setCursor(999))
                (if (temp0)
                    (send gGame:setCursor(gCursorNumber 1 (+ (send curIcon:nsLeft) (/ (- (send curIcon:nsRight) (send curIcon:nsLeft)) 2)) (- (send curIcon:nsBottom) 3)))
                )
                (self:doit())
                = temp3 
                    (if ((send gUser:canControl()) or (send gUser:canInput()))
                        (send curIcon:cursor)
                    )(else
                        global21
                    )
                (if (temp0)
                    (send gGame:setCursor(temp3 1 oldMouseX oldMouseY))
                )(else
                    (send gGame:setCursor(temp3 1 (send ((send pEvent:new())):x) proc999_3((send pEvent:y) (+ 1 height))))
                )
                (self:hide())
            )(else
                (if (& pEventType $0004)
                    (switch ((send pEvent:message))
                        (case 13
                            (if (IsObject(curIcon))
                                (send pEvent:
                                    type((send curIcon:type))
                                    message(
                                        (if (== curIcon useIconItem)
                                            (send curInvIcon:message)
                                        )(else
                                            (send curIcon:message)
                                        )
)
                                )
                            )
                        )
                        (case 20992
                            (if ((send gUser:canControl()))
                                (self:swapCurIcon())
                            )
                            (send pEvent:claimed(1))
                        )
                        (case 0
                            (if (& (send pEvent:type) $0040)
                                (self:advanceCurIcon())
                                (send pEvent:claimed(1))
                            )
                        )
                    )
                )(else
                    (if (& pEventType $0001)
                        (if (& (send pEvent:modifiers) $0003)
                            (self:advanceCurIcon())
                            (send pEvent:claimed(1))
                        )(else
                            (if (& (send pEvent:modifiers) $0004)
                                (if ((send gUser:canControl()))
                                    (self:swapCurIcon())
                                )
                                (send pEvent:claimed(1))
                            )(else
                                (if (IsObject(curIcon))
                                    (send pEvent:
                                        type((send curIcon:type))
                                        message(
                                            (if (== curIcon useIconItem)
                                                (send curInvIcon:message)
                                            )(else
                                                (send curIcon:message)
                                            )
)
                                    )
                                )
                            )
                        )
                    )
                )
            )
        )
    )


    (method (show)
        (var temp0, temp1, temp2, temp3, theY, temp5, temp6, temp7)
        (send gSounds:pause())
        = state (| state $0020)
        (send gGame:setCursor(999 1))
        = temp0 (self:at(0))
        = height CelHigh((send temp0:view) (send temp0:loop) (send temp0:cel))
        = port GetPort()
        SetPort(-1)
        = underBits Graph(grSAVE_BOX y 0 (+ y height) 320 1)
        = temp1 PicNotValid()
        PicNotValid(1)
        = temp3 0
        = theY y
        = temp5 FirstNode(elements)
        (while (temp5)
            = temp6 NextNode(temp5)
            = temp7 NodeValue(temp5)
            (if (not IsObject(temp7))
                return 
            )
            (if (<= (send temp7:nsRight) 0)
                (send temp7:show(temp3 theY))
                = temp3 (send temp7:nsRight)
            )(else
                (send temp7:show())
            )
            = temp5 temp6
        )
        (if (curInvIcon)
            (if ((send gOwner:has((send gInv:indexOf(curInvIcon)))))
                = temp3 (+ (/ (- (- (send useIconItem:nsRight) (send useIconItem:nsLeft)) CelWide((send curInvIcon:view) (+ (send curInvIcon:loop) 1) (send curInvIcon:cel))) 2) (send useIconItem:nsLeft))
                = theY (+ (+ y (/ (- (- (send useIconItem:nsBottom) (send useIconItem:nsTop)) CelHigh((send curInvIcon:view) (+ (send curInvIcon:loop) 1) (send curInvIcon:cel))) 2)) (send useIconItem:nsTop))
                DrawCel((send curInvIcon:view) + (send curInvIcon:loop) 1 (send curInvIcon:cel) temp3 theY -1)
                (if (& (send useIconItem:signal) $0004)
                    (send useIconItem:mask())
                )
            )(else
                = curInvIcon 0
            )
        )
        PicNotValid(temp1)
        Graph(grUPDATE_BOX y 0 + y height 320 1)
        (self:highlight(curIcon))
    )


    (method (hide)
        (var temp0, temp1, temp2)
        (if (& state $0020)
            (send gSounds:pause(0))
            = state (& state $ffdf)
            = temp0 FirstNode(elements)
            (while (temp0)
                = temp1 NextNode(temp0)
                = temp2 NodeValue(temp0)
                (if (not IsObject(temp2))
                    return 
                )
                = temp2 NodeValue(temp0)
                (send temp2:signal((& (send temp2:signal) $ffdf)))
                = temp0 temp1
            )
            (if (not (& state $0800) and (& (send helpIconItem:signal) $0010))
                (send helpIconItem:signal((& (send helpIconItem:signal) $ffef)))
            )
            Graph(grRESTORE_BOX underBits)
            Graph(grUPDATE_BOX y 0 + y height 320 1)
            Graph(grREDRAW_BOX y 0 + y height 320)
            SetPort(port)
            = height activateHeight
        )
    )


    (method (advance)
        (var temp0, temp1)
        = temp1 1
        (while (<= temp1 size)
            = temp0 (self:at((% (+ temp1 (self:indexOf(highlightedIcon))) size)))
            (if (not IsObject(temp0))
                = temp0 NodeValue((self:first()))
            )
            (if (not & (send temp0:signal) $0004)
                break
            )
            = temp1 (% (+ temp1 1) size)
        )
        (self:highlight(temp0 (& state $0020)))
    )


    (method (retreat)
        (var temp0, temp1)
        = temp1 1
        (while (<= temp1 size)
            = temp0 (self:at((% (- (self:indexOf(highlightedIcon)) temp1) size)))
            (if (not IsObject(temp0))
                = temp0 NodeValue((self:last()))
            )
            (if (not & (send temp0:signal) $0004)
                break
            )
            = temp1 (% (+ temp1 1) size)
        )
        (self:highlight(temp0 (& state $0020)))
    )


    (method (select theCurIcon param2)
        return 
            (if ((send theCurIcon:select(
                (if (>= paramTotal 2)
                    param2
                )
)))
                (if (not & (send theCurIcon:signal) $0002)
                    = curIcon theCurIcon
                )
                1
            )(else
                0
            )
    )


    (method (highlight theHighlightedIcon param2)
        (var temp0)
        (if (not & (send theHighlightedIcon:signal) $0004)
            (if (IsObject(highlightedIcon))
                (send highlightedIcon:highlight(0))
            )
            (send (= highlightedIcon theHighlightedIcon):highlight(1))
        )
        (if ((>= paramTotal 2) and param2)
            (send gGame:setCursor(gCursorNumber 1 (+ (send theHighlightedIcon:nsLeft) (/ (- (send theHighlightedIcon:nsRight) (send theHighlightedIcon:nsLeft)) 2)) (- (send theHighlightedIcon:nsBottom) 3)))
        )
    )


    (method (swapCurIcon)
        (var temp0)
        (if (& state $0004)
            return 
        )(else
            = temp0 NodeValue((self:first()))
            (if ((<> curIcon temp0) and not (& (send temp0:signal) $0004))
                = prevIcon curIcon
                = curIcon NodeValue((self:first()))
            )(else
                (if (prevIcon and not (& (send prevIcon:signal) $0004))
                    = curIcon prevIcon
                )
            )
        )
        (send gGame:setCursor((send curIcon:cursor) 1))
    )


    (method (advanceCurIcon)
        (var theCurIcon, temp1, temp2)
        (if (& state $0004)
            return 
        )
        = theCurIcon curIcon
        = temp1 0
        (while (& (send ((= theCurIcon (self:at((% (+ (self:indexOf(theCurIcon)) 1) size))))):signal) $0006)
            (if (> temp1 (+ 1 size))
                return 
            )(else
                ++temp1
            )
        )
        = curIcon theCurIcon
        (send gGame:setCursor((send curIcon:cursor) 1))
    )


    (method (dispatchEvent param1)
        (var temp0, temp1, temp2, temp3, theHighlightedIcon, temp5, temp6, temp7[50], temp57, theHighlightedIconSignal, temp59)
        = temp1 (send param1:x)
        = temp0 (send param1:y)
        = temp2 (send param1:type)
        = temp3 (send param1:message)
        = temp5 (send param1:claimed)
        = theHighlightedIcon (self:firstTrue(#onMe param1))
        (if (theHighlightedIcon)
            = temp57 (send ((= theHighlightedIcon (self:firstTrue(#onMe param1)))):cursor)
            = theHighlightedIconSignal (send theHighlightedIcon:signal)
            = temp59 (== theHighlightedIcon helpIconItem)
        )
        (if (& temp2 $0040)
            (switch (temp3)
                (case 3
                    (self:advance())
                )
                (case 7
                    (self:retreat())
                )
            )
        )(else
            (switch (temp2)
                (case 0
                    (if (not 
                    (if (((<= 0 temp0) and (<= temp0 (+ y height))) and (<= 0 temp1))
                        <= temp1 320
                    )
)
                        (if ((& state $0400) and (not IsObject(helpIconItem) or not (& (send helpIconItem:signal) $0010)))
                            = oldMouseY 0
                            = temp5 1
                        )
                    )(else
                        (if (theHighlightedIcon and (<> theHighlightedIcon highlightedIcon))
                            = oldMouseY 0
                            (self:highlight(theHighlightedIcon))
                        )
                    )
                )
                (case 1
                    (if (theHighlightedIcon and (self:select(theHighlightedIcon 1)))
                        (if (temp59)
                            (if (temp57)
                                (send gGame:setCursor(temp57))
                            )
                            (if (& state $0800)
                                (self:noClickHelp())
                            )(else
                                (send helpIconItem:signal((| (send helpIconItem:signal) $0010)))
                            )
                        )(else
                            = temp5 (& theHighlightedIconSignal $0040)
                        )
                        (send theHighlightedIcon:doit())
                    )
                )
                (case 4
                    (switch (temp3)
                        (case 27
                            = temp5 1
                        )
                        (case 21248
                            = temp5 1
                        )
                        (case 13
                            (if (not theHighlightedIcon)
                                = theHighlightedIcon highlightedIcon
                            )
                            (if (theHighlightedIcon and (== theHighlightedIcon helpIconItem))
                                (if (<> temp57 -1)
                                    (send gGame:setCursor(temp57))
                                )
                                (if (helpIconItem)
                                    (send helpIconItem:signal((| (send helpIconItem:signal) $0010)))
                                )
                            )(else
                                (if (IsObject(theHighlightedIcon) and (self:select(theHighlightedIcon)))
                                    (send theHighlightedIcon:doit())
                                    = temp5 (& theHighlightedIconSignal $0040)
                                )
                            )
                        )
                        (case 3840
                            (self:retreat())
                        )
                        (case 9
                            (self:advance())
                        )
                    )
                )
                (case 24576
                    (if (theHighlightedIcon and (send theHighlightedIcon:helpVerb))
                        = temp6 GetPort()
                        (Print:
                            font(gFont)
                            width(250)
                            addText((send theHighlightedIcon:noun) (send theHighlightedIcon:helpVerb) 0 1 0 0 (send theHighlightedIcon:modNum))
                            init()
                        )
                        SetPort(temp6)
                    )
                    (if (helpIconItem)
                        (send helpIconItem:signal((& (send helpIconItem:signal) $ffef)))
                    )
                    (send gGame:setCursor(999))
                )
            )
        )
        return temp5
    )


    (method (disable param1)
        (var temp0, temp1)
        (if (paramTotal)
            = temp0 0
            (while (< temp0 paramTotal)
                = temp1 
                    (if (IsObject(param1[temp0]))
                        param1[temp0]
                    )(else
                        (self:at(param1[temp0]))
                    )
                (send temp1:signal((| (send temp1:signal) $0004)))
                (if (== temp1 curIcon)
                    (self:advanceCurIcon())
                )(else
                    (if (== temp1 highlightedIcon)
                        (self:advance())
                    )
                )
                ++temp0
            )
        )(else
            = state (| state $0004)
        )
    )


    (method (enable param1)
        (var temp0, temp1)
        (if (paramTotal)
            = temp0 0
            (while (< temp0 paramTotal)
                = temp1 
                    (if (IsObject(param1[temp0]))
                        param1[temp0]
                    )(else
                        (self:at(param1[temp0]))
                    )
                (send temp1:signal((& (send temp1:signal) $fffb)))
                ++temp0
            )
        )(else
            = state (& state $fffb)
        )
    )


    (method (noClickHelp)
        (var temp0, temp1, temp2, temp3, gGloryWindowEraseOnly)
        = temp2 0
        = temp1 temp2
        = temp3 GetPort()
        = gGloryWindowEraseOnly (send gGloryWindow:eraseOnly)
        (send gGloryWindow:eraseOnly(1))
        (while (not (send (= temp0 (send ((send gUser:curEvent)):new())):type))
            (if (not (self:isMemberOf(IconBar)))
                (send temp0:localize())
            )
            = temp2 (self:firstTrue(#onMe temp0))
            (if (temp2)
                (if ((<> (= temp2 (self:firstTrue(#onMe temp0))) temp1) and (send temp2:helpVerb))
                    = temp1 temp2
                    (if (gDialog)
                        (send gDialog:dispose())
                    )
                    (Print:
                        font(gFont)
                        width(250)
                        addText((send temp2:noun) (send temp2:helpVerb) 0 1 0 0 (send temp2:modNum))
                        modeless(1)
                        init()
                    )
                    SetPort(temp3)
                )
            )(else
                (if (gDialog)
                    (send gDialog:dispose())
                )(else
                    = temp1 0
                )
            )
            (send temp0:dispose())
        )
        (send gGloryWindow:eraseOnly(gGloryWindowEraseOnly))
        (send gGame:setCursor(999 1))
        (if (gDialog)
            (send gDialog:dispose())
        )
        SetPort(temp3)
        (if (not (send helpIconItem:onMe(temp0)))
            (self:dispatchEvent(temp0))
        )
    )


    (method (findIcon param1)
        (var temp0, temp1)
        = temp0 0
        (while (< temp0 size)
            = temp1 (self:at(temp0))
            (if (== (send temp1:message) param1)
                return temp1
            )
            ++temp0
        )
        return 0
    )

)
