Jump to content

How to organize my Funcs


Recommended Posts

I am rewriting a series of Funcs that I have for work and I am looking for some better ideas on how to organize and optimize my code.  I have thousands of lines of code and a lot of duplication, minus a few variables.  My funcs do a few basic operations, or combination of operations like:

  1. Get the coordinates on the screen
  2. MouseClick()
  3. Wait for something to appear (wait for the screen to load)

I have 50+ functions that do something similar, and the only difference is that they have a slight change in the variable.  Is there a better way to optimize this?

Func _GetCoords_BottomCorner_ByImageSearchArea()
    Local $sPathOf_Image = @ScriptDir & "\images\" & "BottomRightBorderCorner.bmp"
    Local $iPersonal_XCoord_Offset = 0
    Local $iPersonal_YCoord_Offset = 0
    Local $iTop = 0
    Local $iLeft = @DesktopWidth / 2
    Local $iRight = @DesktopWidth
    Local $iBottom = @DesktopHeight
    Local $iTolerance = 10
    Local $sCenterPos = True

    ;==>Where you found the image - Return Position
    Local $aCoords = _GetCoordsByImageSearchArea($sPathOf_Image, $iLeft, $iTop, $iRight, $iBottom, $iTolerance, $sCenterPos)
    If $aCoords[0] = 1 Then
        Return $aCoords
    ElseIf $aCoords[0] = 0 Then
;~      MsgBox(0, "", "_GetCoordsForScrollDownArrow_AssignmentRosterPage()" & @CRLF & "X: " & $aCoords[1] & @CRLF & "Y: " & $aCoords[2])
    EndIf

    Return $aCoords

EndFunc   ;==>_GetCoords_BottomCorner_ByImageSearchArea
Func _GetCoords_YearMonthCombo_ByImageSearchArea()
    Local $sPathOf_Image = @ScriptDir & "\images\" & "AttendanceYearMonthCombo.bmp"

    Local $iPersonal_XCoord_Offset = 0
    Local $iPersonal_YCoord_Offset = 0
    Local $iTop = 0
    Local $iLeft = 0
    Local $iRight = @DesktopWidth / 2
    Local $iBottom = @DesktopHeight
    Local $iTolerance = 10
    Local $sCenterPos = False

    ;==>Where you found the image - Return Position
    Local $aCoords = _GetCoordsByImageSearchArea($sPathOf_Image, $iLeft, $iTop, $iRight, $iBottom, $iTolerance, $sCenterPos)
    If $aCoords[0] = 1 Then
        Return $aCoords
    ElseIf $aCoords[0] = 0 Then
        MsgBox(0, "", "_GetCoordsForScrollDownArrow_AssignmentRosterPage()" & @CRLF & "X: " & $aCoords[1] & @CRLF & "Y: " & $aCoords[2])
    EndIf

    Return $aCoords

EndFunc   ;==>_GetCoords_YearMonthCombo_Text_InmateAssignmentPage_ByImageSearchArea

 

Link to comment
Share on other sites

I stop wrapping _GetCoordsByImageSearchArea in other functions... you're not saving yourself that much time.

Instead, you could wrap it once and set some default values if you wanted... something like this

Func My_GetCoordsByImageSearchArea($sPathOf_Image, $iLeft = 0, $iTop = 0, $iRight = @DesktopWidth, $iBottom = @DesktopHeight, $iTolerance = 10, $sCenterPos = True)
    Local $ret = _GetCoordsByImageSearchArea($sPathOf_Image, $iLeft, $iTop, $iRight, $iBottom, $iTolerance, $sCenterPos)
    Return SetError(@error, @extended, $ret)
EndFunc

This will give you some default values that you don't have to type every time

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

3 hours ago, seadoggie01 said:

I stop wrapping _GetCoordsByImageSearchArea in other functions... you're not saving yourself that much time.

Is there a way make one function, so that the "user" can just call the Func, and the Func defines the "properties", instead of having to try and remember the filepath, X, Y, coords and stuff?

Does it make sense to do something like

Func My_GetCoordsByImageSearchArea($sWhatToDo)
    Select
        Case $sWhatToDo = "Look For Pixel Color"
            $iLeft = 0
            $iTop = 0
            $iRight = @DesktopWidth
            $iBottom = @DesktopHeight
            $iTolerance = 10
            $sCenterPos = True
        Case $sWhatToDo = "Look For Text"
            $iLeft = 0
            $iTop = 0
            $iRight = @DesktopWidth
            $iBottom = @DesktopHeight
            $iTolerance = 10
            $sCenterPos = True
    EndSelect

    Local $ret = _GetCoordsByImageSearchArea($sPathOf_Image, $iLeft, $iTop, $iRight, $iBottom, $iTolerance, $sCenterPos)
    Return SetError(@error, @extended, $ret)
EndFunc

 

Link to comment
Share on other sites

7 hours ago, iosman123 said:

thanks my issue has been fixed.

Are you lost? :D

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

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...