(include "sci.sh")
(use "Main")
(use "Print")
(use "IconI")
(use "Tutorial")
(use "Obj")
(script 937)



(class IconI of Obj
    (properties
        view -1
        loop -1
        cel -1
        nsLeft 0
        nsTop -1
        nsRight 0
        nsBottom 0
        state 0
        cursor -1
        type 16384
        message -1
        modifiers 0
        signal 1
        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(173)))
            (send gPseudoMouse:stop())
        )
    )


    (method (select param1)
        (var newEvent, temp1, gSQ5Script)
        return 
            (if (& signal $0004)
                0
            )(else
                (if ((paramTotal and param1) and (& signal $0001))
                    DrawCel(view loop = temp1 1 nsLeft nsTop -1)
                    Graph(12 nsTop nsLeft nsBottom nsRight 1)
                    (while (<> (send ((= newEvent (Event:new()))):type) 2)
                        (send newEvent:localize())
                        (if ((self:onMe(newEvent)))
                            (if (not temp1)
                                DrawCel(view loop = temp1 1 nsLeft nsTop -1)
                                Graph(12 nsTop nsLeft nsBottom nsRight 1)
                            )
                        )(else
                            (if (temp1)
                                DrawCel(view loop = temp1 0 nsLeft nsTop -1)
                                Graph(12 nsTop nsLeft nsBottom nsRight 1)
                            )
                        )
                        (send newEvent:dispose())
                    )
                    (send newEvent:dispose())
                    (if (== temp1 1)
                        DrawCel(view loop 0 nsLeft nsTop -1)
                        Graph(12 nsTop nsLeft nsBottom nsRight 1)
                    )
                    (if ((= gSQ5Script (send gSQ5:script)) and (send gSQ5Script:isKindOf(Tutorial)))
                        (if ((== (send gSQ5Script:nextItem) self) and (<> (send gSQ5Script:nextAction) (send ((send gSq5IconBar:helpIconItem)):message)))
                            (send gSQ5Script:cue())
                        )(else
                            (if (not temp1)
                                return 0
                            )(else
                                (send gSQ5Script:report())
                                return 0
                            )
                        )
                    )
                    temp1
                )(else
                    (if ((= gSQ5Script (send gSQ5:script)) and (send gSQ5Script:isKindOf(Tutorial)))
                        (if ((== (send gSQ5Script:nextItem) self) and (<> (send gSQ5Script:nextAction) (send ((send gSq5IconBar:helpIconItem)):message)))
                            (send gSQ5Script:cue())
                        )(else
                            (send gSQ5Script:report())
                            return 0
                        )
                    )
                    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)
        Graph(4 temp0 temp1 temp0 = temp3 (- nsRight 4) temp4 -1 -1)
        Graph(4 temp0 temp3 temp2 temp3 temp4 -1 -1)
        Graph(4 temp2 temp3 temp2 temp1 temp4 -1 -1)
        Graph(4 temp2 temp1 temp0 temp1 temp4 -1 -1)
        Graph(12 - 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 1024
        activateHeight 0
        y 0
    )

    (method (doit)
        (var temp0[5])
    )


    (method (handleEvent pEvent)
        (var temp0, pEventType, temp2, temp3, theGSaveCursor, 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)
                = theGSaveCursor gSaveCursor
                = theCurIcon curIcon
                = theCurInvIcon curInvIcon
                (self:show())
                (send gSQ5:setCursor(999))
                (if (temp0)
                    (send gSQ5:setCursor(gSaveCursor 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 gSQ5:setCursor(temp3 1 oldMouseX oldMouseY))
                )(else
                    (send gSQ5: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 (not IsObject(curIcon))
                            )(else
                                (if ((<> curIcon useIconItem) or curInvIcon)
                                    (send pEvent:
                                        type((send curIcon:type))
                                        message(
                                            (if (== curIcon useIconItem)
                                                (send curInvIcon:message)
                                            )(else
                                                (send curIcon:message)
                                            )
)
                                    )
                                )(else
                                    (send pEvent:type(0))
                                )
                            )
                        )
                        (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 gSQ5:setCursor(999 1))
        = height CelHigh((send ((= temp0 (self:at(0)))):view) (send temp0:loop) (send temp0:cel))
        = port GetPort()
        SetPort(-1)
        = underBits Graph(7 y 0 (+ y height) 320 1)
        = temp1 PicNotValid()
        PicNotValid(1)
        = temp3 0
        = theY y
        = temp5 FirstNode(elements)
        (while (temp5)
            = temp6 NextNode(temp5)
            (if (not IsObject(= temp7 NodeValue(temp5)))
                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 gEgo:has((send gSq5Inv:indexOf(curInvIcon)))))
                = temp3 (+ (/ (- (- (send useIconItem:nsRight) (send useIconItem:nsLeft)) CelWide((send curInvIcon:view) (send curInvIcon:loop) (send curInvIcon:cel))) 2) (send useIconItem:nsLeft))
                = theY (+ (+ y (/ (- (- (send useIconItem:nsBottom) (send useIconItem:nsTop)) CelHigh((send curInvIcon:view) (send curInvIcon:loop) (send curInvIcon:cel))) 2)) (send useIconItem:nsTop))
                DrawCel((send curInvIcon:view) (send curInvIcon:loop) (send curInvIcon:cel) temp3 theY -1)
                (if (& (send useIconItem:signal) $0004)
                    (send useIconItem:mask())
                )
            )(else
                = curInvIcon 0
            )
        )
        PicNotValid(temp1)
        Graph(12 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)
                (if (not IsObject(= temp2 NodeValue(temp0)))
                    return 
                )
                (send (= temp2 NodeValue(temp0)):signal((& (send temp2:signal) $ffdf)))
                = temp0 temp1
            )
            (if ((not (& state $0800) and IsObject(helpIconItem)) and (& (send helpIconItem:signal) $0010))
                (send helpIconItem:signal((& (send helpIconItem:signal) $ffef)))
            )
            Graph(8 underBits)
            Graph(12 y 0 + y height 320 1)
            Graph(13 y 0 + y height 320)
            SetPort(port)
            = height activateHeight
        )
    )


    (method (advance)
        (var temp0, temp1)
        (asm
            ldi     1
            sat     temp1
code_0ed3:  lst     temp1
            pToa    size
            le?     
            bnt     code_0f23
            pushi   64
            pushi   1
            lst     temp1
            pushi   #indexOf
            pushi   1
            pTos    highlightedIcon
            self    6
            add     
            push    
            pToa    size
            mod     
            push    
            self    6
            sat     temp0
            pushi   1
            push    
            callk   IsObject, 2
            not     
            bnt     code_0f07
            pushi   1
            pushi   #first
            pushi   0
            self    4
            push    
            callk   NodeValue, 2
            sat     temp0
code_0f07:  pushi   #signal
            pushi   0
            lat     temp0
            send    4
            push    
            ldi     4
            and     
            not     
            bt      code_0f23
            lst     temp1
            ldi     1
            add     
            push    
            pToa    size
            mod     
            sat     temp1
            jmp     code_0ed3
code_0f23:  pushi   225
            pushi   #view
            lst     temp0
            pTos    state
            ldi     32
            and     
            push    
            self    8
            ret     
        )
    )


    (method (retreat)
        (var temp0, temp1)
        (asm
            ldi     1
            sat     temp1
code_0f38:  lst     temp1
            pToa    size
            le?     
            bnt     code_0f89
            pushi   64
            pushi   1
            pushi   #indexOf
            pushi   1
            pTos    highlightedIcon
            self    6
            push    
            lat     temp1
            sub     
            push    
            pToa    size
            mod     
            push    
            self    6
            sat     temp0
            pushi   1
            push    
            callk   IsObject, 2
            not     
            bnt     code_0f6d
            pushi   1
            pushi   #last
            pushi   0
            self    4
            push    
            callk   NodeValue, 2
            sat     temp0
code_0f6d:  pushi   #signal
            pushi   0
            lat     temp0
            send    4
            push    
            ldi     4
            and     
            not     
            bt      code_0f89
            lst     temp1
            ldi     1
            add     
            push    
            pToa    size
            mod     
            sat     temp1
            jmp     code_0f38
code_0f89:  pushi   225
            pushi   #view
            lst     temp0
            pTos    state
            ldi     32
            and     
            push    
            self    8
            ret     
        )
    )


    (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 gSQ5:setCursor(gSaveCursor 1 (+ (send theHighlightedIcon:nsLeft) (/ (- (send theHighlightedIcon:nsRight) (send theHighlightedIcon:nsLeft)) 2)) (- (send theHighlightedIcon:nsBottom) 3)))
        )
    )


    (method (swapCurIcon)
        (var temp0)
        (if (& state $0004)
            return 
        )(else
            (if ((<> curIcon (= temp0 NodeValue((self:first())))) and not (& (send temp0:signal) $0004))
                = prevIcon curIcon
                = curIcon NodeValue((self:first()))
            )(else
                (if (prevIcon and not (& (send prevIcon:signal) $0004))
                    = curIcon prevIcon
                )
            )
        )
        (send gSQ5: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 gSQ5:setCursor((send curIcon:cursor) 1))
    )


    (method (dispatchEvent param1)
        (var temp0, temp1, temp2, temp3, theHighlightedIcon, temp5, temp6, temp7[50], temp57, theHighlightedIconSignal, temp59, temp60)
        = temp1 (send param1:x)
        = temp0 (send param1:y)
        = temp2 (send param1:type)
        = temp3 (send param1:message)
        = temp5 (send param1:claimed)
        (if (= theHighlightedIcon (self:firstTrue(226 param1)))
            = temp57 (send ((= theHighlightedIcon (self:firstTrue(226 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 gSQ5: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 gSQ5: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))
                        (if (not HaveMouse())
                            = temp60 (send gSQ5:setCursor(996))
                        )
                        = 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 (not HaveMouse())
                            (send gSQ5:setCursor(temp60))
                        )
                    )
                    (if (helpIconItem)
                        (send helpIconItem:signal((& (send helpIconItem:signal) $ffef)))
                    )
                    (send gSQ5:setCursor(999))
                )
            )
        )
        return temp5
    )


    (method (disable param1)
        (var temp0, temp1)
        (if (paramTotal)
            = temp0 0
            (while (< temp0 paramTotal)
                (send temp1:signal((| (send ((= temp1 
                        (if (IsObject(param1[temp0]))
                            param1[temp0]
                        )(else
                            (self:at(param1[temp0]))
                        )
)):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)
                (send temp1:signal((& (send ((= temp1 
                        (if (IsObject(param1[temp0]))
                            param1[temp0]
                        )(else
                            (self:at(param1[temp0]))
                        )
)):signal) $fffb)))
                ++temp0
            )
        )(else
            = state (& state $fffb)
        )
    )


    (method (noClickHelp)
        (var temp0, temp1, temp2, temp3, gSq5WinEraseOnly, temp5)
        = temp1 (= temp2 0)
        = temp3 GetPort()
        = gSq5WinEraseOnly (send gSq5Win:eraseOnly)
        (send gSq5Win:eraseOnly(1))
        (while (not (send (= temp0 (send ((send gUser:curEvent)):new())):type))
            (if (not (self:isMemberOf(IconBar)))
                (send temp0:localize())
            )
            (if (= temp2 (self:firstTrue(226 temp0)))
                (if ((<> (= temp2 (self:firstTrue(226 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 gSq5Win:eraseOnly(gSq5WinEraseOnly))
        (send gSQ5: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)
            (if (== (send ((= temp1 (self:at(temp0)))):message) param1)
                return temp1
            )
            ++temp0
        )
        return 0
    )

)
