Unit
   Uses pure2d

Const
    { Win Pos }
    WINPOS_CENTER = 0
    WINPOS_TOPLEFT = 1
    WINPOS_TOPRIGHT = 2
    WINPOS_BOTTOMLEFT = 3
    WINPOS_BOTTOMRIGHT = 4

Procedure PositionScreen(pos: Integer, xoff: Integer, yoff: Integer) ; Export
Begin
    Case pos of
        (WINPOS_CENTER):
            ScreenX(((SelWidth - 160)/2) + xoff)
            ScreenY(((SelHeight - 100)/2) + yoff)
        (WINPOS_TOPLEFT):
            ScreenX(0 + xoff)
            ScreenY(0 + yoff)
        (WINPOS_TOPRIGHT):
            ScreenX(SelWidth - 160 + xoff)
            ScreenY(0 + yoff)
        (WINPOS_BOTTOMLEFT):
            ScreenX(0 + xoff)
            ScreenY(SelHeight - 100 + yoff)
        (WINPOS_BOTTOMRIGHT):        
            ScreenX(SelWidth - 160 + xoff)
            ScreenY(SelHeight - 100 + yoff)
    EndCase
End

Procedure Error(msg: String) ; Export
Begin
    MessageBox(msg,"Elipsis Launcher Error")
End

Procedure JustifiedText(x: Integer, y: Integer, s: String, cx: Boolean = FALSE, cy: Boolean = FALSE, buf: Element = NULL, frame: Integer = 0) ; Export
Var
    px, py : Integer    
Begin
    If cx Then px = ToInt(TextWidth(s,buf,frame)/2) Else px = 0 
    If cy Then py = ToInt(TextHeight(s,buf,frame)/2) Else py = 0
    Text(x-px,y-py,s,buf,frame)
End

Function HexToRGBA(hexval: String, alphaval:Integer = 255) : Integer ; Export
Var
    twohex : Array[3] of String
    hexresult : Array[3] of Integer
    lefthex, righthex : String
    leftval, rightval : Integer 
    i : Integer
Begin
    hexval = Upper(hexval)
    
    // Convert Length 3 Hex to Length 6 Hex.
    If (Length(hexval)=3) Then hexval = (Mid(hexval,1,1)+""+Mid(hexval,1,1)) +""+ (Mid(hexval,2,1)+""+Mid(hexval,2,1)) +""+ (Mid(hexval,3,1)+""+Mid(hexval,3,1))
    If (Not (Length(hexval)=6)) Then
        Error("Error: Hex #"+hexval+" could not be converted to a Length 6 Hexidecimal value.")
        result = ToRGBA(0,0,0,0) 
    Else    
        twohex[0] = Mid(hexval,1,2)
        twohex[1] = Mid(hexval,3,2)
        twohex[2] = Mid(hexval,5,2)
        
        For i = 0 to 2
        	lefthex = Left(twohex[i],1)
            righthex = Right(twohex[i],1)
            If (Asc(lefthex) > 64) Then
            	leftval = (Asc(lefthex) - 55)
            Else
            	leftval = ToInt(lefthex)
            Endif
            If (Asc(righthex) > 64) Then
    			rightval = (Asc(righthex) - 55)
            Else
        		rightval = ToInt(righthex)
            Endif
            hexresult[i] = (leftval*16)+rightval
        Next
         
        result = ToRGBA(hexresult[0],hexresult[1],hexresult[2],alphaval)
    Endif	
End

Function MouseIn(mx: Integer, my: Integer, x1: Integer, y1: Integer, x2: Integer, y2: Integer) : Boolean ; Export
Begin
    If (mx > x1) and (mx < x2) and (my > y1) and (my < y2) then result = TRUE 
End

Begin
End