Jump to content

button help


Recommended Posts

ok, here I am again.

CODE
#include <GUIConstants.au3>

Global $squares[65]
For $i = 1 To 64
    $squares[$i] = ""
Next
Global $MarblePics[65]
$gui = GUICreate ("AutoIt Reversi", 490, 595)
$board = GUICtrlCreatePic ("board.bmp", 5, 5, 480, 480)
GUICtrlSetState($board, $GUI_DISABLE)
PlaceMarble ("white", 3, 3)
PlaceMarble ("black", 3, 4)
PlaceMarble ("black", 4, 3)
PlaceMarble ("white", 4, 4)
Global $square_buttons[65]
For $x=1 To 8
    For $y = 1 To 8
        $square = (8*($y-1))+$x
        $square_buttons[$square] = GUICtrlCreateButton ("", coord($x)-60, coord($y)-60, 60, 60)
        ;GUICtrlSetState (-1, $GUI_HIDE)
    Next
Next
GUISetState ()

While 1
    $msg = GUIGetMsg ()
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    For $i=1 To 64
        If $msg = $square_buttons[$i] Then
            PlaceMarble ("black", SquareX($i)-1, squareY($i)-1)
        EndIf
    Next
WEnd

Func coord($num)
    If $num < 1 Or $num > 8 Then
        SetError (1)
        Return 0
    EndIf
    Return ($num*60)+5
EndFunc

Func PlaceMarble ($color, $x, $y)
    Local $square = (8*($y-1))+$x
    If $square < 1 Or $square > 64 Then Return 0
    If $squares[$square] <> "" Then
        GUICtrlDelete ($MarblePics[$square])
        $squares[$square] = ""
    EndIf
    $MarblePics[$square] = GUICtrlCreatePic (@ScriptDir&"\"&$color&".bmp", coord($x), coord($y), 59, 59)
    GUICtrlSetState ( -1, $GUI_SHOW)
    $squares[$square] = $color
    Return 1
EndFunc

Func ReverseMarble ($x, $y)
    Local $square = (8*($y-1))+$x
    If $squares[$square] = "" Then Return 0
    Switch $squares[$square]
        Case "white"
            PlaceMarble ("black", $x, $y)
        Case "black"
            PlaceMarble ("white", $x, $y)
    EndSwitch
EndFunc

Func squareX ($num)
    $ret = 0
    Do
        $num -=1
        $ret +=1
    Until Round ($num/8) = $num/8
    Return $ret
EndFunc

Func squareY ($num)
    $num -= SquareX($num)
    Return ($num/8)+1
EndFunc

so, I am trying to detect when A user clicks on a square. Well, I have managed to do that. The problem is, I can't set those buttons as hidden or they don't work. (by uncommenting line 20)

Is there some way to hide those buttons in a way that they do work, or a better way to find out which square the user clicked on?

The cake is a lie.www.theguy0000.com is currentlyUP images.theguy0000.com is currentlyUP all other *.theguy0000.com sites are DOWN

Link to comment
Share on other sites

having looked at this the first time.. i would use a single block picture and create the board in a loop. that way each "square" would have its own control id and you could check which control was clicked then move the playing pieces accordingly

maybe _ControlHover() could help here also ( in fact i am sure it would do the trick )

for $x = 1 to $squares

$square[$x] = GUICtrlCreatePic()

; maybe - _ControlHover(2,... ) ; add the control/pic

next

8)

Edit.. you are ready have the loop idea

Global $squares[65]
For $i = 1 To 64
    $squares[$i] = ""
Next
Edited by Valuater

NEWHeader1.png

Link to comment
Share on other sites

CODE
#include <GUIConstants.au3>
#include <Array.au3>
Global $cHoverID[2], $Global_cHID = 0, $cHoverActive = 0, $cHClicked = 0

Global $squares[65]
Global $MarblePics[65]
For $y = 1 To 8
    For $x = 1 To 8
        $i = (8*($y-1))+$x
        $squares[$i] = ""
        $MarblePics[$i] = GUICtrlCreatePic ("square.bmp", coord($x), coord($y), 60, 60)
        _ControlHover (2, "", $MarblePics[$i])
        $squares[$i] = ""
    Next
Next
$gui = GUICreate ("AutoIt Reversi", 490, 595)
PlaceMarble ("white", 3, 3)
PlaceMarble ("black", 3, 4)
PlaceMarble ("black", 4, 3)
PlaceMarble ("white", 4, 4)
Global $square_buttons[65]
GUISetState ()

While 1
    $msg = GUIGetMsg ()
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    $Over = _ControlHover (0, $gui)
    If $over = 1 Then
        $TempID = @extended
        For $y = 1 To 8
            For $x = 1 To 8
                $i = (8*($y-1))+$x
                If $TempID = $MarblePics[$i] Then
                    If $squares[$i] = "" Then GUICtrlSetImage ($MarblePics[$i], "hover.bmp")
                Else
                    GUICtrlSetImage ($MarblePics[$i], "square.bmp")
                EndIf
            Next
        Next
    EndIf
WEnd

Func coord($num)
    If $num < 1 Or $num > 8 Then
        SetError (1)
        Return 0
    EndIf
    Return ($num*60)+5
EndFunc

Func PlaceMarble ($color, $x, $y)
    Local $square = (8*($y-1))+$x
    If $square < 1 Or $square > 64 Then Return 0
    $MarblePics[$square] = GUICtrlSetImage ($MarblePics[$square], $color&".bmp")
    GUICtrlSetState ( -1, $GUI_SHOW)
    $squares[$square] = $color
    Return 1
EndFunc

Func ReverseMarble ($x, $y)
    Local $square = (8*($y-1))+$x
    If $squares[$square] = "" Then Return 0
    Switch $squares[$square]
        Case "white"
            PlaceMarble ("black", $x, $y)
        Case "black"
            PlaceMarble ("white", $x, $y)
    EndSwitch
EndFunc

Func squareX ($num)
    $ret = 0
    Do
        $num -=1
        $ret +=1
    Until Round ($num/8) = $num/8
    Return $ret
EndFunc

Func squareY ($num)
    $num -= SquareX($num)
    Return ($num/8)+1
EndFunc

;ControlHover by Valuater
Func _ControlHover($cH_Mode = 0, $cH_hWin = "", $cH_hCntrl = "")
    
    Local $cH_i, $cH_Data
    If not @Compiled Then Opt("TrayIconDebug", 1) ; 1=debug line info
    
    Select
        Case $cH_Mode = 0 Or $cH_Mode = 1 ; 0 = Check if mouse is over a control, 1 = Check if mouse clicked on a control 
            
            ; Developer did not add any controls before the function mode 0 or 1 was called
            If UBound($cHoverID) = 2 Then ConsoleWrite("*** _ControlHover() Error *** " & @CRLF & "No Controls were Added" & @CRLF) 
            
            If $cH_hWin = "" Then 
                $cH_Data = GUIGetCursorInfo() ; Get cursor data from the active window
            Else
                If Not WinActive($cH_hWin) Then Return SetError( -1, 1, "Window not active") ; Dont waist CPU if the known window is not active
                $cH_Data = GUIGetCursorInfo($cH_hWin) ; Get cursor data from the known window
            EndIf
            
            If Not IsArray($cH_Data) Then Return SetError( -1, 2, "Window not found") ; A readable GUI window was not found
            
            For $cH_i = 1 to UBound($cHoverID) -1 ; Search the known controls for the currently hovered control 
                If $cH_Data[4] = $cHoverID[$cH_i] Then ; Mouse is over a current known control
                    
                    ; Mode 1 - check for a Click on a known control
                    If $cH_Mode And $cH_Data[2] = 1 and Not $cHClicked Then
                        $cHClicked = 1
                        Return SetError( 0, $cHoverID[$Global_cHID], 1) ; Mouse clicked on current control
                    ElseIf $cH_Mode And $cH_Data[2] <> 1 And $cHClicked Then
                        $cHClicked = 0
                        Return SetError( 1, $cHoverID[$Global_cHID], 0) ; Release clicked current control
                    EndIf
                    If $cH_Mode Then Return SetError ( "", "", "") ; Mouse is still over last known control and may click again
                    
                    ; Mode 0 & 1
                    If $cHoverID[$cH_i] = $cHoverID[$Global_cHID] And $cHoverActive Then Return SetError ( "", "", "") ; Mouse is still over last known control
                    If $cHoverActive Then 
                        $cHoverActive = 0
                        Return SetError( 1, $cHoverID[$Global_cHID], 0) ; Release Active - Mouse is over a new known control
                    EndIf
                    $cHoverActive = 1
                    $cHoverID[$Global_cHID] = $cHoverID[$cH_i] ; Re-set the current control to the new known control
                    Return SetError(0, $cHoverID[$Global_cHID], 1) ; Mouse is over the new-current known control
                    
                EndIf
            Next
            ; hover control not found in the known control list
            If $cHoverActive Then 
                If  $cH_Mode And $cHClicked Then  ; check - Release clicked current control
                    $cHClicked = 0
                    Return SetError( 1, $cHoverID[$Global_cHID], 0) ; Release clicked current control
                EndIf
                If $cH_Mode Then Return SetError ( "", "", "") ; Protect the release of the active control
                $cHoverActive = 0
                Return SetError( 1, $cHoverID[$Global_cHID], 0) ; Release Active - Mouse is over a new unknown control
            EndIf
            If  $cH_Mode And $cHClicked Then
                $cHClicked = 0
                Return SetError( 1, $cHoverID[$Global_cHID], 0) ; Release clicked current control
            EndIf
            Return SetError ( "", "", "")
        
        Case $cH_Mode = 2 ; Add the new control to the known control list
            
            If $cH_hCntrl = "" Then Return SetError( -1, 8, "Control not provided #1") ; The control ID was not given
            _ArrayAdd($cHoverID, $cH_hCntrl) ; Add the control to the known control array
            If @error Then Return SetError( -1, 9, "Control not added") ; Control not added to the known control list
            Return SetError( 0, 10, 1) ; ; Control was added to the known control list
            
        Case $cH_Mode = 3 ; Remove the control from the known control list
            
            If $cH_hCntrl = "" Then Return SetError( -1, 11, "Control not provided #2") ; The control ID was not given
            _ArrayDelete($cHoverID, $cH_hCntrl) ; Delete the control from the known control array list
            If @error Then Return SetError( -1, 12, "Control not removed") ; Control not deleted from the known control list
            Return SetError( 0, 13, 1) ; Control was deleted from the known control list
            
        Case $cH_Mode = 4 ; Verify if control is in the known control list
            
            If $cH_hCntrl = "" Then Return SetError( -1, 14, "Control not provided #3") ; The control ID was not given
            _ArraySearch($cHoverID, $cH_hCntrl) ; Search to verify if control is in the known control list
            If @error Then Return SetError( -1, 15, "Control not found") ; Control was not found in the known control list
            Return SetError( 0, 16, 1) ; Control was found in the known control list
            
        Case Else
            Return SetError( -1, 17, "$cH_Mode incorrect") ; $cH_Mode has an incorrect value
    EndSelect
    Return SetError( -1, 18, "Unknown Error") ; Error is Unknown
EndFunc

I'm not sure what I'm doing wrong, but I get a ton of errors in the console that say:

*** _ControlHover() Error *** 
No Controls were Added

The cake is a lie.www.theguy0000.com is currentlyUP images.theguy0000.com is currentlyUP all other *.theguy0000.com sites are DOWN

Link to comment
Share on other sites

nice, the only line that would have a problem is this:

If $squares[$i] = "" Then GUICtrlSetImage($TempID, "hover.bmp")

there is no more $i because we took out the loop, so how will it know whether or not there is already a pieve there. If $squares[$i] is NOT "" Then It should not change the hover image...only change to the hover image if it is ""

the way you have it now, it does highlight when you put the mouse over a square, but 2 problems...

1. it still changes even if the square is occupied

2. if you take the mouse off the square, any piece that was already in that space is erased.

Edited by theguy0000

The cake is a lie.www.theguy0000.com is currentlyUP images.theguy0000.com is currentlyUP all other *.theguy0000.com sites are DOWN

Link to comment
Share on other sites

ok, i got it to work for you ( _ControlHover() )

I didn't know i had to help write the whole game, i don't know the game...so

is there only gojng to be "4" marble pieces in the entire game.. these...

PlaceMarble("white", 3, 3)

PlaceMarble("black", 3, 4)

PlaceMarble("black", 4, 3)

PlaceMarble("white", 4, 4)

???

8)

NEWHeader1.png

Link to comment
Share on other sites

actually, yes, I would definitely need to loop to get the X and Y of the selected square. There are things I can't do without it, especially when I add the part for clicking.

The cake is a lie.www.theguy0000.com is currentlyUP images.theguy0000.com is currentlyUP all other *.theguy0000.com sites are DOWN

Link to comment
Share on other sites

ok, i got it to work for you ( _ControlHover() )

I didn't know i had to help write the whole game, i don't know the game...so

is there only gojng to be "4" marble pieces in the entire game.. these...

???

8)

Sorry :">

and no those are at the beginning and I still have yet to add the part where when a user clicks a square it adds a piece. For that part I will need to use PlaceMarble and that needs X and Y values so the loop will be necessary...

The cake is a lie.www.theguy0000.com is currentlyUP images.theguy0000.com is currentlyUP all other *.theguy0000.com sites are DOWN

Link to comment
Share on other sites

how many marble pieces... total... 4 ???

8)

well the pieces don't move for one thing. And those four are always created at the beginning.

But, when a user clicks a square, it creates another piece.

So, there could be 64 pieces total until every square fills up, or until there are no more possible moves.

So, it's impossible to know what square was clicked unless individually testing all 64 to see if they match...hence the loop

Edited by theguy0000

The cake is a lie.www.theguy0000.com is currentlyUP images.theguy0000.com is currentlyUP all other *.theguy0000.com sites are DOWN

Link to comment
Share on other sites

I got the hover thing to work...

#include <GUIConstants.au3>
#include <Array.au3>
Global $cHoverID[2], $Global_cHID = 0, $cHoverActive = 0, $cHClicked = 0

Global $squares[65]
Global $MarblePics[65]
$gui = GUICreate("AutoIt Reversi", 600, 595)
For $y = 1 To 8
    For $x = 1 To 8
        $i = (8* ($y - 1)) + $x
        $squares[$i] = ""
        $MarblePics[$i] = GUICtrlCreatePic("square.bmp", coord($x), coord($y), 60, 60)
        _ControlHover(2, $gui, $MarblePics[$i])
        $squares[$i] = ""
    Next
Next

PlaceMarble("white", 3, 3)
PlaceMarble("black", 3, 4)
PlaceMarble("black", 4, 3)
PlaceMarble("white", 4, 4)
Global $square_buttons[65]
GUISetState()

While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    $Over = _ControlHover(0, $gui)
    If $Over = 1 Then
        $TempID = @extended
        For $i = 1 To 64
            If $MarblePics[$i] = $TempID Then
                If $squares[$i] = "" Then
                    GUICtrlSetImage($TempID, "hover.bmp")
                    $squares[$i] = "hover"
                EndIf
            Else
                If $squares[$i] = "hover" Then
                    GUICtrlSetImage ($MarblePics[$i], "square.bmp")
                    $squares[$i] = ""
                EndIf
            EndIf
        Next
    EndIf
WEnd

Func coord($num)
    If $num < 1 Or $num > 8 Then
        SetError(1)
        Return 0
    EndIf
    Return ($num * 60) + 5
EndFunc   ;==>coord

Func PlaceMarble($color, $x, $y)
    Local $square = (8*$y) + $x+1
    If $square < 1 Or $square > 64 Then Return 0
    $MarblePics[$square] = GUICtrlSetImage($MarblePics[$square], $color & ".bmp")
    GUICtrlSetState(-1, $GUI_SHOW)
    $squares[$square] = $color
    Return 1
EndFunc   ;==>PlaceMarble

Func ReverseMarble($x, $y)
    Local $square = (8* ($y - 1)) + $x
    If $squares[$square] = "" Then Return 0
    Switch $squares[$square]
        Case "white"
            PlaceMarble("black", $x, $y)
        Case "black"
            PlaceMarble("white", $x, $y)
    EndSwitch
EndFunc   ;==>ReverseMarble

Func squareX($num)
    $ret = 0
    Do
        $num -= 1
        $ret += 1
    Until Round($num / 8) = $num / 8
    Return $ret
EndFunc   ;==>squareX

Func squareY($num)
    $num -= squareX($num)
    Return ($num / 8) + 1
EndFunc   ;==>squareY

;ControlHover by Valuater
#region _ControlHover;
;===============================================================================
;    UDF Name: _ControlHover.au3
;
;    Version v1.2.0  Sept 25, 2006, built with Autoit 3.2.1.3
;
;    Author: Valuater, Robert M
;
;    Contributions: marfdaman(Marvin), RazerM, Quaizywabbit(AnyGUI).
;
;    Email: <Valuater [at] aol [dot] com>
;
;    Use: Enable Mouse/Hover Over Control Events
;===============================================================================

;===============================================================================
;
; Function Name:    _ControlHover()
; Description:      Enable Mouse/Hover Over Control Events
;                    Find if Mouse is over a known control
;                    Find if a Mouse has Clicked on a known control
;                    Add, Remove, or Verify the control within the known control list
; Parameter(s):     $cH_Mode    [default]       0 = Find if Mouse is over a known control
;                               [optional]        1 = Find if a Mouse has Clicked on a known control
;                                               2 = Add a control to the known control list
;                                               3 = Remove a control from the known control list
;                                               4 = Verify a control is in the known control list
;                   $cH_hWin    [optional]       - Window handle as returned by GUICreate()
;                                [preferred]        - Window handle to save CPU usage and speed of _ControlHover()
;                                                - and Avoid array errors for "non-active window"
;                   $cH_hCntrl    [required]       - Control ID to Add, Remove, or Verify the control within the known control list
;                                [not used]        - to find if a mouse is over or has clicked on a known control
; Requirement(s):   #include<Array.au3>
; Return Value(s):
;                    $cH_Mode 0 or 1
;                                On Success     Return = 1        @error = 0, @extended = Current known control
;                               While Success Return = ""
;                               On Failure: Return = 0        @error = 1, @extended = Last known control
;                    $cH_Mode 2, 3, or 4
;                                On Success     Return = 1        @error = 0, @extended = Function's return line number
;                    All $cH_Mode's
;                               On Error:     Return = Description of the error
;                                                            @error = -1, @extended = Function's return line number
; Author(s):        Valuater, Robert M
;
;===============================================================================
Func _ControlHover($cH_Mode = 0, $cH_hWin = "", $cH_hCntrl = "")
   
    Local $cH_i, $cH_Data
    If Not @Compiled Then Opt("TrayIconDebug", 1) ; 1=debug line info
   
    Select
        Case $cH_Mode = 0 Or $cH_Mode = 1 ; 0 = Check if mouse is over a control, 1 = Check if mouse clicked on a control
           
            ; Developer did not add any controls before the function mode 0 or 1 was called
            If UBound($cHoverID) = 2 Then ConsoleWrite("*** _ControlHover() Error *** " & @CRLF & "No Controls were Added" & @CRLF)
           
            If $cH_hWin = "" Then
                $cH_Data = GUIGetCursorInfo() ; Get cursor data from the active window
            Else
                If Not WinActive($cH_hWin) Then Return SetError(-1, 1, "Window not active") ; Dont waist CPU if the known window is not active
                $cH_Data = GUIGetCursorInfo($cH_hWin) ; Get cursor data from the known window
            EndIf
           
            If Not IsArray($cH_Data) Then Return SetError(-1, 2, "Window not found") ; A readable GUI window was not found
           
            For $cH_i = 1 To UBound($cHoverID) - 1 ; Search the known controls for the currently hovered control
                If $cH_Data[4] = $cHoverID[$cH_i] Then ; Mouse is over a current known control
                   
                    ; Mode 1 - check for a Click on a known control
                    If $cH_Mode And $cH_Data[2] = 1 And Not $cHClicked Then
                        $cHClicked = 1
                        Return SetError(0, $cHoverID[$Global_cHID], 1) ; Mouse clicked on current control
                    ElseIf $cH_Mode And $cH_Data[2] <> 1 And $cHClicked Then
                        $cHClicked = 0
                        Return SetError(1, $cHoverID[$Global_cHID], 0) ; Release clicked current control
                    EndIf
                    If $cH_Mode Then Return SetError("", "", "") ; Mouse is still over last known control and may click again
                   
                    ; Mode 0 & 1
                    If $cHoverID[$cH_i] = $cHoverID[$Global_cHID] And $cHoverActive Then Return SetError("", "", "") ; Mouse is still over last known control
                    If $cHoverActive Then
                        $cHoverActive = 0
                        Return SetError(1, $cHoverID[$Global_cHID], 0) ; Release Active - Mouse is over a new known control
                    EndIf
                    $cHoverActive = 1
                    $cHoverID[$Global_cHID] = $cHoverID[$cH_i] ; Re-set the current control to the new known control
                    Return SetError(0, $cHoverID[$Global_cHID], 1) ; Mouse is over the new-current known control
                   
                EndIf
            Next
            ; hover control not found in the known control list
            If $cHoverActive Then
                If $cH_Mode And $cHClicked Then  ; check - Release clicked current control
                    $cHClicked = 0
                    Return SetError(1, $cHoverID[$Global_cHID], 0) ; Release clicked current control
                EndIf
                If $cH_Mode Then Return SetError("", "", "") ; Protect the release of the active control
                $cHoverActive = 0
                Return SetError(1, $cHoverID[$Global_cHID], 0) ; Release Active - Mouse is over a new unknown control
            EndIf
            If $cH_Mode And $cHClicked Then
                $cHClicked = 0
                Return SetError(1, $cHoverID[$Global_cHID], 0) ; Release clicked current control
            EndIf
            Return SetError("", "", "")
           
        Case $cH_Mode = 2 ; Add the new control to the known control list
           
            If $cH_hCntrl = "" Then Return SetError(-1, 8, "Control not provided #1") ; The control ID was not given
            _ArrayAdd($cHoverID, $cH_hCntrl) ; Add the control to the known control array
            If @error Then Return SetError(-1, 9, "Control not added") ; Control not added to the known control list
            Return SetError(0, 10, 1) ; ; Control was added to the known control list
           
        Case $cH_Mode = 3 ; Remove the control from the known control list
           
            If $cH_hCntrl = "" Then Return SetError(-1, 11, "Control not provided #2") ; The control ID was not given
            _ArrayDelete($cHoverID, $cH_hCntrl) ; Delete the control from the known control array list
            If @error Then Return SetError(-1, 12, "Control not removed") ; Control not deleted from the known control list
            Return SetError(0, 13, 1) ; Control was deleted from the known control list
           
        Case $cH_Mode = 4 ; Verify if control is in the known control list
           
            If $cH_hCntrl = "" Then Return SetError(-1, 14, "Control not provided #3") ; The control ID was not given
            _ArraySearch($cHoverID, $cH_hCntrl) ; Search to verify if control is in the known control list
            If @error Then Return SetError(-1, 15, "Control not found") ; Control was not found in the known control list
            Return SetError(0, 16, 1) ; Control was found in the known control list
           
        Case Else
            Return SetError(-1, 17, "$cH_Mode incorrect") ; $cH_Mode has an incorrect value
    EndSelect
    Return SetError(-1, 18, "Unknown Error") ; Error is Unknown
EndFunc   ;==>_ControlHover
#endregion

thanks, you helped a lot though...

I might be back If I can't get the clicking to work right though ;)

The cake is a lie.www.theguy0000.com is currentlyUP images.theguy0000.com is currentlyUP all other *.theguy0000.com sites are DOWN

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...