Leaderboard
Popular Content
Showing content with the highest reputation on 08/13/2025 in all areas
-
JSON UDF in pure AutoIt
ioa747 and 3 others reacted to AspirinJunkie for a topic
There is a new version. This brings a significant performance increase. In my tests, _JSON_Parse() is now around 40–55% faster, and for _JSON_Generate(), the increase is around 15–35%. I didn't thought there was still such potential in the code. However, I still had a few ideas in mind that required quite a bit of restructuring, but I've now tackled them accordingly. In my opinion, it was worth it. In the process, I removed ByRef everywhere, so that direct value transfer is now possible for all functions (which was a request). I also added notes on how to handle .json files to the Readme.md (@WildByDesign: I hope that was what you meant?).4 points -
Goal: select the squares to match every row and column results (screenshot below). you can change the puzzle type (addition or multiplication), grid size (3x3 to 9x9), and other preferences. Dependency: this app is also a showcase for the WinPose UDF (which was actually developed for it). Script: #Region wrapper directives #AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 #AutoIt3Wrapper_UseX64=N #AutoIt3Wrapper_Compile_both=N #AutoIt3Wrapper_Icon=MagicMath.ico #AutoIt3Wrapper_UseUpx=N #AutoIt3Wrapper_Res_Description=Magic Math #AutoIt3Wrapper_Res_Fileversion=1.0.0.0 #AutoIt3Wrapper_Res_ProductVersion=1.0.0.0 #AutoIt3Wrapper_Res_LegalCopyright=Or Ben Shabat (or.ben.shabat@gmail.com) #AutoIt3Wrapper_Run_Au3Stripper=y #Au3Stripper_Parameters=/SO /RM #EndRegion wrapper directives #NoTrayIcon #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <StaticConstants.au3> #include <ColorConstants.au3> #include <FontConstants.au3> #include <AutoItConstants.au3> #include <EditConstants.au3> #include <Math.au3> #include 'WinPose.au3' ; init ; - general Global Const $sAppName = 'Magic Math' ; - colors Global Const $iDefaultSqrSize = 45 Global Const $iDefaultColCount = 5 Global Const $iDefaultRowCount = 5 Global Const $iDefaultResizeSpeed = 50 Global Const $iColor_BoxText = $COLOR_BLACK Global Const $iColor_BoxTextError = $COLOR_RED Global Const $iColor_BoxUnchecked = $COLOR_LIGHTGRAY Global Const $iColor_BoxChecked = $COLOR_LIGHTGREEN Global Const $iColor_BoxEliminated = $COLOR_DARKGRAY Global Const $iColor_BoxResult = $COLOR_PEACHPUFF Global Const $iColor_MenuItem = $COLOR_DARKBLUE Global Const $iColor_MenuItemPressed = $COLOR_LIGHTGRAY Global Const $iColor_MenuItemEmphasis = $COLOR_WHITE Global Const $iColor_Vprimary = $COLOR_LIMEGREEN Global Const $iColor_Vsecondary = $COLOR_AQUA ; - symbols Global Const $symSetup = Chr(64) ; Webdings Global Const $symRenew = Chr(81) ; Wingdings 3 Global Const $symSolve = Chr(97) ; Webdings Global Const $symHelp = Chr(115) ; Webdings Global Const $symP = Chr(200) ; Wingdings 2 Global Const $symX = Chr(208) ; Wingdings 2 Global Const $symV = Chr(252) ; Wingdings ; declare variables Global $bPuzzleTypeIsAddition = True Global $iColCount = $iDefaultColCount, $iRowCount = $iDefaultRowCount, $iSqrSize = $iDefaultSqrSize, $iResizeSpeed = $iDefaultResizeSpeed Global $iGUI_Width, $iGUI_Height, $hGUI Global $aPos, $iWDiff, $iHDiff ; - main GUI Global $gMenu_New, $gMenu_Solve, $gMenu_Pref, $gMenu_Help, $gdGUIStart, $gdGUIEnd, $gdGridStart, $gdGridEnd Global Enum $iGrid_iValue, $iGrid_gID, $iGrid_bSelected, $iGrid_bChecked, $iGrid_bEliminated, $iGrid_iDimCount Global $aGrid[$iColCount][$iRowCount][$iGrid_iDimCount] Global Enum $iResult_iValue, $iResult_iDiff, $iResult_iDimCount Global $aResultOfRow[$iRowCount][$iResult_iDimCount] Global $aResultOfCol[$iColCount][$iResult_iDimCount] Global $gPuzzleType Global $bUserInputAllowed Global $bCheckForVictory ; - pref GUI Global $gPuzzleType_Addition, $gPuzzleType_Multiplication, _ $gGridWidth_Down, $gGridWidth_Data, $gGridWidth_Up, _ $gGridHeight_Down, $gGridHeight_Data, $gGridHeight_Up, _ $gSqrSize_Down, $gSqrSize_Data, $gSqrSize_Up, _ $gResizeSpeed_data, _ $gOK ; GUI $iGUI_Width = $iSqrSize * ($iColCount + 3) + 2 $iGUI_Height = $iSqrSize * ($iRowCount + 2) + 2 $hGUI = GUICreate($sAppName, $iGUI_Width, $iGUI_Height) $aPos = WinGetPos($hGUI) $iWDiff = $aPos[2] - $iGUI_Width $iHDiff = $aPos[3] - $iGUI_Height BuildGUI() GUISetState(@SW_SHOW, $hGUI) ; main loop Global $msg Global $aCursorInfo $bCheckForVictory = False While True $msg = GUIGetMsg() Switch $msg Case $gMenu_New GUICtrlSetBkColor($gMenu_New, $iColor_MenuItemPressed) Sleep(100) Renew() Sleep(100) GUICtrlSetBkColor($gMenu_New, $GUI_BKCOLOR_TRANSPARENT) Case $gMenu_Solve GUICtrlSetBkColor($gMenu_Solve, $iColor_MenuItemPressed) Sleep(100) Solve() Sleep(100) GUICtrlSetBkColor($gMenu_Solve, $GUI_BKCOLOR_TRANSPARENT) Case $gMenu_Pref GUICtrlSetBkColor($gMenu_Pref, $iColor_MenuItemPressed) Sleep(100) BuildGUI(Pref()) Sleep(100) GUICtrlSetBkColor($gMenu_Pref, $GUI_BKCOLOR_TRANSPARENT) Case $gMenu_Help GUICtrlSetBkColor($gMenu_Help, $iColor_MenuItemPressed) Sleep(100) BuildGUI(Help()) Sleep(100) GUICtrlSetBkColor($gMenu_Help, $GUI_BKCOLOR_TRANSPARENT) Case $gdGridStart To $gdGridEnd If $bUserInputAllowed Then For $iRow = 0 To $iRowCount - 1 For $iCol = 0 To $iColCount - 1 If $msg = $aGrid[$iCol][$iRow][$iGrid_gID] Then $aGrid[$iCol][$iRow][$iGrid_bEliminated] = False $aGrid[$iCol][$iRow][$iGrid_bChecked] = Not $aGrid[$iCol][$iRow][$iGrid_bChecked] GUICtrlSetBkColor($aGrid[$iCol][$iRow][$iGrid_gID], $aGrid[$iCol][$iRow][$iGrid_bChecked] ? $iColor_BoxChecked : $iColor_BoxUnchecked) EndIf Next Next $bCheckForVictory = True EndIf Case $GUI_EVENT_SECONDARYUP $aCursorInfo = GUIGetCursorInfo() If $aCursorInfo[4] > $gdGridStart And $aCursorInfo[4] < $gdGridEnd Then If $bUserInputAllowed Then For $iRow = 0 To $iRowCount - 1 For $iCol = 0 To $iColCount - 1 If $aCursorInfo[4] = $aGrid[$iCol][$iRow][$iGrid_gID] Then $aGrid[$iCol][$iRow][$iGrid_bChecked] = False $aGrid[$iCol][$iRow][$iGrid_bEliminated] = Not $aGrid[$iCol][$iRow][$iGrid_bEliminated] GUICtrlSetBkColor($aGrid[$iCol][$iRow][$iGrid_gID], $aGrid[$iCol][$iRow][$iGrid_bEliminated] ? $iColor_BoxEliminated : $iColor_BoxUnchecked) EndIf Next Next $bCheckForVictory = True EndIf EndIf Case $GUI_EVENT_CLOSE ExitLoop EndSwitch If $bCheckForVictory Then If IsVictoryAchieved() Then ShowVictory() GUICtrlSetBkColor($gMenu_New, $iColor_MenuItemEmphasis) Sleep(125) Renew() Sleep(125) GUICtrlSetBkColor($gMenu_New, $GUI_BKCOLOR_TRANSPARENT) EndIf $bCheckForVictory = False EndIf WEnd Func Renew($bNew = True) $bUserInputAllowed = True Local $bNoZeroResults GUISetState(@SW_LOCK) Do $bNoZeroResults = True For $iRow = 0 To $iRowCount - 1 For $iCol = 0 To $iColCount - 1 If $bNew Then $aGrid[$iCol][$iRow][$iGrid_iValue] = $bPuzzleTypeIsAddition ? Random(1, 9, 1) : Random(2, 9, 1) $aGrid[$iCol][$iRow][$iGrid_bSelected] = (Random() < 0.5) $aGrid[$iCol][$iRow][$iGrid_bChecked] = False $aGrid[$iCol][$iRow][$iGrid_bEliminated] = False EndIf GUICtrlSetData($aGrid[$iCol][$iRow][$iGrid_gID], $aGrid[$iCol][$iRow][$iGrid_iValue]) GUICtrlSetColor($aGrid[$iCol][$iRow][$iGrid_gID], $iColor_BoxText) Select Case $aGrid[$iCol][$iRow][$iGrid_bChecked] GUICtrlSetBkColor($aGrid[$iCol][$iRow][$iGrid_gID], $iColor_BoxChecked) Case $aGrid[$iCol][$iRow][$iGrid_bEliminated] GUICtrlSetBkColor($aGrid[$iCol][$iRow][$iGrid_gID], $iColor_BoxEliminated) Case Else GUICtrlSetBkColor($aGrid[$iCol][$iRow][$iGrid_gID], $iColor_BoxUnchecked) EndSelect GUICtrlSetCursor($aGrid[$iCol][$iRow][$iGrid_gID], $MCID_ARROW) Next Next Local $iResult For $iRow = 0 To $iRowCount - 1 $iResult = 0 For $iCol = 0 To $iColCount - 1 If $aGrid[$iCol][$iRow][$iGrid_bSelected] Then $iResult = Accumulate($iResult, $aGrid[$iCol][$iRow][$iGrid_iValue]) Next If $iResult = 0 Then $bNoZeroResults = False GUICtrlSetData($aResultOfRow[$iRow][$iResult_iValue], $iResult) GUICtrlSetFont($aResultOfRow[$iRow][$iResult_iValue], _Min($iSqrSize / 3, $iSqrSize / StringLen($iResult))) Next For $iCol = 0 To $iColCount - 1 $iResult = 0 For $iRow = 0 To $iRowCount - 1 If $aGrid[$iCol][$iRow][$iGrid_bSelected] Then $iResult = Accumulate($iResult, $aGrid[$iCol][$iRow][$iGrid_iValue]) Next If $iResult = 0 Then $bNoZeroResults = False GUICtrlSetData($aResultOfCol[$iCol][$iResult_iValue], $iResult) GUICtrlSetFont($aResultOfCol[$iCol][$iResult_iValue], _Min($iSqrSize / 3, $iSqrSize / StringLen($iResult))) Next Until $bNoZeroResults GUISetState(@SW_UNLOCK) EndFunc ;==>Renew Func Accumulate(ByRef $iBase, $iDelta) If $iBase = 0 Then Return $iDelta If $bPuzzleTypeIsAddition Then Return $iBase + $iDelta Else Return $iBase * $iDelta EndIf EndFunc ;==>Accumulate Func Solve() $bUserInputAllowed = False For $iRow = 0 To $iRowCount - 1 For $iCol = 0 To $iColCount - 1 GUICtrlSetBkColor($aGrid[$iCol][$iRow][$iGrid_gID], $aGrid[$iCol][$iRow][$iGrid_bSelected] ? $iColor_BoxChecked : $iColor_BoxEliminated) If $aGrid[$iCol][$iRow][$iGrid_bSelected] <> $aGrid[$iCol][$iRow][$iGrid_bChecked] Then GUICtrlSetColor($aGrid[$iCol][$iRow][$iGrid_gID], $iColor_BoxTextError) GUICtrlSetCursor($aGrid[$iCol][$iRow][$iGrid_gID], $MCID_NO) Next Next EndFunc ;==>Solve Func IsVictoryAchieved() Local $iResult For $iRow = 0 To $iRowCount - 1 $iResult = 0 For $iCol = 0 To $iColCount - 1 If $aGrid[$iCol][$iRow][$iGrid_bChecked] Then $iResult = Accumulate($iResult, $aGrid[$iCol][$iRow][$iGrid_iValue]) Next If GUICtrlRead($aResultOfRow[$iRow][$iResult_iValue]) <> $iResult Then Return False Next For $iCol = 0 To $iColCount - 1 $iResult = 0 For $iRow = 0 To $iRowCount - 1 If $aGrid[$iCol][$iRow][$iGrid_bChecked] Then $iResult = Accumulate($iResult, $aGrid[$iCol][$iRow][$iGrid_iValue]) Next If GUICtrlRead($aResultOfCol[$iCol][$iResult_iValue]) <> $iResult Then Return False Next Return True EndFunc ;==>IsVictoryAchieved Func ShowVictory() Sleep(150) Local $gV = GUICtrlCreateLabel($symV, 0, 0, $iGUI_Width, $iGUI_Height, BitOR($SS_CENTER, $SS_CENTERIMAGE)) GUICtrlSetFont($gV, _Min($iGUI_Height, $iGUI_Width) * 0.6, Default, Default, 'Wingdings') GUICtrlSetBkColor($gV, $GUI_BKCOLOR_TRANSPARENT) GUICtrlSetColor($gV, $iColor_Vprimary) For $i = 1 To 2 Sleep(125) GUICtrlSetColor($gV, $iColor_Vsecondary) Sleep(125) GUICtrlSetColor($gV, $iColor_Vprimary) Next Sleep(1000) GUICtrlDelete($gV) Sleep(250) EndFunc ;==>ShowVictory Func BuildGUI($bNew = True) GUISetFont($iSqrSize / 3, Default, Default, 'Tahoma') ; GUI contents $gdGUIStart = GUICtrlCreateDummy() ; - menu $gMenu_Pref = GUICtrlCreateLabel($symSetup, 1, 1 + $iSqrSize / 2, $iSqrSize * 1.5 - 2, $iSqrSize - 2, BitOR($SS_CENTER, $SS_CENTERIMAGE)) GUICtrlSetTip(-1, 'Set your preferences and construct a new puzzle', 'Setup') GUICtrlSetFont(-1, $iSqrSize / 2, Default, Default, 'Webdings') GUICtrlSetColor(-1, $iColor_MenuItem) GUICtrlSetCursor(-1, $MCID_HAND) $gMenu_New = GUICtrlCreateLabel($symRenew, 1, $iSqrSize + 1 + $iSqrSize / 2, $iSqrSize * 1.5 - 2, $iSqrSize - 2, BitOR($SS_CENTER, $SS_CENTERIMAGE)) GUICtrlSetTip(-1, 'Construct a new puzzle using current preferences', 'New Puzzle') GUICtrlSetFont(-1, $iSqrSize / 2, Default, Default, 'Wingdings 3') GUICtrlSetColor(-1, $iColor_MenuItem) GUICtrlSetCursor(-1, $MCID_HAND) $gMenu_Solve = GUICtrlCreateLabel($symV, 1, $iSqrSize * 2 + 1 + $iSqrSize / 2, $iSqrSize * 1.5 - 2, $iSqrSize - 2, BitOR($SS_CENTER, $SS_CENTERIMAGE)) GUICtrlSetTip(-1, 'I give up :(', 'Show Solution') GUICtrlSetFont(-1, $iSqrSize / 2, Default, Default, 'Wingdings') GUICtrlSetColor(-1, $iColor_MenuItem) GUICtrlSetCursor(-1, $MCID_HAND) $gMenu_Help = GUICtrlCreateLabel($symHelp, 1, $iSqrSize * 3 + 1 + $iSqrSize / 2, $iSqrSize * 1.5 - 2, $iSqrSize - 2, BitOR($SS_CENTER, $SS_CENTERIMAGE)) GUICtrlSetTip(-1, 'What do I do here?', 'Instructions') GUICtrlSetFont(-1, $iSqrSize / 2, Default, Default, 'Webdings') GUICtrlSetColor(-1, $iColor_MenuItem) GUICtrlSetCursor(-1, $MCID_HAND) ; - grid ; -- center part ReDim $aGrid[$iColCount][$iRowCount][$iGrid_iDimCount] $gdGridStart = GUICtrlCreateDummy() For $iRow = 0 To $iRowCount - 1 For $iCol = 0 To $iColCount - 1 $aGrid[$iCol][$iRow][$iGrid_gID] = GUICtrlCreateLabel('', ($iCol + 1) * $iSqrSize + 2 + $iSqrSize / 2, $iRow * $iSqrSize + 2 + $iSqrSize / 2, $iSqrSize - 2, $iSqrSize - 2, BitOR($SS_CENTER, $SS_CENTERIMAGE)) Next Next $gdGridEnd = GUICtrlCreateDummy() ; -- results of rows ReDim $aResultOfRow[$iRowCount][$iResult_iDimCount] For $iRow = 0 To $iRowCount - 1 $aResultOfRow[$iRow][$iResult_iValue] = GUICtrlCreateLabel('', ($iColCount + 1) * $iSqrSize + 2 + $iSqrSize / 2, $iRow * $iSqrSize + 2 + $iSqrSize / 2, $iSqrSize - 2, $iSqrSize - 2, BitOR($SS_CENTER, $SS_CENTERIMAGE)) GUICtrlSetBkColor(-1, $iColor_BoxResult) Next ; -- results of cols ReDim $aResultOfCol[$iColCount][$iResult_iDimCount] For $iCol = 0 To $iColCount - 1 $aResultOfCol[$iCol][$iResult_iValue] = GUICtrlCreateLabel('', ($iCol + 1) * $iSqrSize + 2 + $iSqrSize / 2, $iRowCount * $iSqrSize + 2 + $iSqrSize / 2, $iSqrSize - 2, $iSqrSize - 2, BitOR($SS_CENTER, $SS_CENTERIMAGE)) GUICtrlSetBkColor(-1, $iColor_BoxResult) Next ; -- puzzle type GUICtrlCreateLabel($bPuzzleTypeIsAddition ? $symP : $symX, ($iColCount + 1) * $iSqrSize + 2 + $iSqrSize / 2, $iRowCount * $iSqrSize + 2 + $iSqrSize / 2, $iSqrSize - 2, $iSqrSize - 2, BitOR($SS_CENTER, $SS_CENTERIMAGE)) GUICtrlSetFont(-1, Default, Default, Default, 'Wingdings 2') GUICtrlSetBkColor(-1, $iColor_MenuItemEmphasis) GUICtrlSetTip(-1, ($bPuzzleTypeIsAddition ? 'Addition' : 'Multplication'), 'Puzzle Type:') ; - populate grid Renew($bNew) ; finalize GUI $gdGUIEnd = GUICtrlCreateDummy() EndFunc ;==>BuildGUI Func Pref() Local Const $iSUI_W = 200, $iSUI_H = 280, $iSUI_ButtonH = 30, $iSUI_ButtonSpacing = 10 GUISetFont(9.5, Default, Default, 'Tahoma') Local $x = 10, $y = 10, $dy = 25 ; delete all controls DeleteAllControls() ; resize GUI WinSetTrans($hGUI, '', 192) WinSetTitle($hGUI, '', $sAppName & ' Setup') _WinPoseAroundCenter($hGUI, $iSUI_W + $iWDiff, $iSUI_H + $iHDiff, $iResizeSpeed) WinSetTrans($hGUI, '', 255) ; generate controls $gdGUIStart = GUICtrlCreateDummy() GUICtrlCreateLabel('Puzzle', $x, $y, 85, Default, $SS_CENTERIMAGE) GUICtrlSetFont(-1, Default, 600) $x += 5 $y += $dy GUICtrlCreateLabel('Puzzle Type:', $x, $y, 80, Default, $SS_CENTERIMAGE) $x += 90 $gPuzzleType_Addition = GUICtrlCreateLabel($symP, $x, $y, 41, 22, BitOR($SS_CENTER, $SS_CENTERIMAGE)) GUICtrlSetFont(-1, 12, Default, Default, 'Wingdings 2') $x += 42 $gPuzzleType_Multiplication = GUICtrlCreateLabel($symX, $x, $y, 41, 22, BitOR($SS_CENTER, $SS_CENTERIMAGE)) GUICtrlSetFont(-1, 12, Default, Default, 'Wingdings 2') Pref_RefreshPuzzleTypeButtons($gPuzzleType_Addition, $gPuzzleType_Multiplication) $x -= 42 $x -= 90 $y += $dy GUICtrlCreateLabel('Columns:', $x, $y, 80, Default, $SS_CENTERIMAGE) $x += 90 $gGridWidth_Down = GUICtrlCreateLabel('-', $x, $y, 23, 22, BitOR($SS_CENTER, $SS_CENTERIMAGE)) GUICtrlSetFont(-1, 13, 800) GUICtrlSetBkColor(-1, $COLOR_LIGHTGRAY) GUICtrlSetCursor(-1, $MCID_HAND) $x += 25 $gGridWidth_Data = GUICtrlCreateLabel($iColCount, $x, $y, 33, 22, BitOR($SS_CENTER, $SS_CENTERIMAGE)) GUICtrlSetBkColor(-1, $COLOR_LIGHTBLUE) $x += 35 $gGridWidth_Up = GUICtrlCreateLabel('+', $x, $y, 23, 22, BitOR($SS_CENTER, $SS_CENTERIMAGE)) GUICtrlSetFont(-1, 13, 800) GUICtrlSetBkColor(-1, $COLOR_LIGHTGRAY) GUICtrlSetCursor(-1, $MCID_HAND) $x -= 35 $x -= 25 $x -= 90 $y += $dy GUICtrlCreateLabel('Rows:', $x, $y, Default, Default, $SS_CENTERIMAGE) $x += 90 $gGridHeight_Down = GUICtrlCreateLabel('-', $x, $y, 23, 22, BitOR($SS_CENTER, $SS_CENTERIMAGE)) GUICtrlSetFont(-1, 13, 800) GUICtrlSetBkColor(-1, $COLOR_LIGHTGRAY) GUICtrlSetCursor(-1, $MCID_HAND) $x += 25 $gGridHeight_Data = GUICtrlCreateLabel($iRowCount, $x, $y, 33, 22, BitOR($SS_CENTER, $SS_CENTERIMAGE)) GUICtrlSetBkColor(-1, $COLOR_LIGHTBLUE) $x += 35 $gGridHeight_Up = GUICtrlCreateLabel('+', $x, $y, 23, 22, BitOR($SS_CENTER, $SS_CENTERIMAGE)) GUICtrlSetFont(-1, 13, 800) GUICtrlSetBkColor(-1, $COLOR_LIGHTGRAY) GUICtrlSetCursor(-1, $MCID_HAND) $x -= 35 $x -= 25 $x -= 90 $y += $dy $x -= 5 $y += $dy GUICtrlCreateLabel('Appearance', $x, $y, 85, Default, $SS_CENTERIMAGE) GUICtrlSetFont(-1, Default, 600) $y += $dy $x += 5 GUICtrlCreateLabel('Square Size:', $x, $y, Default, Default, $SS_CENTERIMAGE) $x += 90 $gSqrSize_Down = GUICtrlCreateLabel('-', $x, $y, 23, 22, BitOR($SS_CENTER, $SS_CENTERIMAGE)) GUICtrlSetFont(-1, 13, 800) GUICtrlSetBkColor(-1, $COLOR_LIGHTGRAY) GUICtrlSetCursor(-1, $MCID_HAND) $x += 25 $gSqrSize_Data = GUICtrlCreateLabel($iSqrSize, $x, $y, 33, 22, BitOR($SS_CENTER, $SS_CENTERIMAGE)) GUICtrlSetBkColor(-1, $COLOR_LIGHTBLUE) $x += 35 $gSqrSize_Up = GUICtrlCreateLabel('+', $x, $y, 23, 22, BitOR($SS_CENTER, $SS_CENTERIMAGE)) GUICtrlSetFont(-1, 13, 800) GUICtrlSetBkColor(-1, $COLOR_LIGHTGRAY) GUICtrlSetCursor(-1, $MCID_HAND) $x -= 35 $x -= 25 $x -= 90 $y += $dy GUICtrlCreateLabel('Resize speed:', $x, $y, Default, Default, $SS_CENTERIMAGE) $x += 90 $gResizeSpeed_data = GUICtrlCreateSlider($x, $y, 80) GUICtrlSetLimit(-1, 100) GUICtrlSetData(-1, $iResizeSpeed) $x -= 90 #cs FUTURE DEVELOPMENT ----- BUILD PHASE: allow zero sum/prod use 8 & 9 in multiplication puzzle (HARDER) increase/decrease target squares count (determine probability of a square to participate) allow negative numbers (HARDER) ----- GAME PHASE: show target squares count show current/target squares count allow marking cols & rows as balanced automatically mark cols & rows as balanced automatically mark invalid selection (div creating mod, sum/prod exceeding result) show diff calc allow hints allow real-time switch puzzle type #ce Local $gOK = GUICtrlCreateButton('Done', $iSUI_ButtonSpacing, $iSUI_H - $iSUI_ButtonH - $iSUI_ButtonSpacing, $iSUI_W - $iSUI_ButtonSpacing * 2, $iSUI_ButtonH) $gdGUIEnd = GUICtrlCreateDummy() ; user interaction While True $msg = GUIGetMsg() Switch $msg Case $gPuzzleType_Addition If Not $bPuzzleTypeIsAddition Then $bPuzzleTypeIsAddition = True Pref_RefreshPuzzleTypeButtons($gPuzzleType_Addition, $gPuzzleType_Multiplication) EndIf Case $gPuzzleType_Multiplication If $bPuzzleTypeIsAddition Then $bPuzzleTypeIsAddition = False Pref_RefreshPuzzleTypeButtons($gPuzzleType_Addition, $gPuzzleType_Multiplication) EndIf Case $gGridWidth_Up $iColCount = _Min($iColCount + 1, 9) GUICtrlSetData($gGridWidth_Data, $iColCount) Case $gGridWidth_Down $iColCount = _Max($iColCount - 1, 3) GUICtrlSetData($gGridWidth_Data, $iColCount) Case $gGridHeight_Up $iRowCount = _Min($iRowCount + 1, 9) GUICtrlSetData($gGridHeight_Data, $iRowCount) Case $gGridHeight_Down $iRowCount = _Max($iRowCount - 1, 3) GUICtrlSetData($gGridHeight_Data, $iRowCount) Case $gSqrSize_Up $iSqrSize = _Min($iSqrSize + 5, 60) GUICtrlSetData($gSqrSize_Data, $iSqrSize) Case $gSqrSize_Down $iSqrSize = _Max($iSqrSize - 5, 20) GUICtrlSetData($gSqrSize_Data, $iSqrSize) Case $gOK, $GUI_EVENT_CLOSE $iResizeSpeed = GUICtrlRead($gResizeSpeed_data) ExitLoop EndSwitch WEnd ; delete all controls DeleteAllControls() ; - resize GUI WinSetTrans($hGUI, '', 192) WinSetTitle($hGUI, '', $sAppName) $iGUI_Width = $iSqrSize * ($iColCount + 3) + 2 $iGUI_Height = $iSqrSize * ($iRowCount + 2) + 2 _WinPoseAroundCenter($hGUI, $iGUI_Width + $iWDiff, $iGUI_Height + $iHDiff, $iResizeSpeed) WinSetTrans($hGUI, '', 255) Return True EndFunc ;==>Pref Func Pref_RefreshPuzzleTypeButtons($gPuzzleType_Addition, $gPuzzleType_Multiplication) GUICtrlSetBkColor($gPuzzleType_Addition, $bPuzzleTypeIsAddition ? $COLOR_LIGHTBLUE : $COLOR_LIGHTGRAY) GUICtrlSetCursor($gPuzzleType_Addition, $bPuzzleTypeIsAddition ? $MCID_ARROW : $MCID_HAND) GUICtrlSetBkColor($gPuzzleType_Multiplication, $bPuzzleTypeIsAddition ? $COLOR_LIGHTGRAY : $COLOR_LIGHTBLUE) GUICtrlSetCursor($gPuzzleType_Multiplication, $bPuzzleTypeIsAddition ? $MCID_HAND : $MCID_ARROW) EndFunc ;==>Pref_RefreshPuzzleTypeButtons Func Help() Local Const $iSUI_W = 410, $iSUI_H = 350, $iSUI_ButtonH = 30, $iSUI_ButtonSpacing = 10 GUISetFont(9.5, Default, Default, 'Tahoma') Local $x = 15, $y = 10, $dy = 25 ; delete all controls DeleteAllControls() ; resize GUI WinSetTrans($hGUI, '', 192) WinSetTitle($hGUI, '', $sAppName & ' Instructions') _WinPoseAroundCenter($hGUI, $iSUI_W + $iWDiff, $iSUI_H + $iHDiff, $iResizeSpeed) WinSetTrans($hGUI, '', 255) ; generate controls $gdGUIStart = GUICtrlCreateDummy() GUICtrlCreateLabel('WARNING! This game may be highly addictive :-)' & @CRLF & @CRLF & _ 'Click the gray squares in the middle to select them and paint them green. The selected squares in every row must match the result of the row, displayed on the right-hand side; the selected squares in every column must match the result of the column, displayed at the bottom.' & @CRLF & @CRLF & _ 'If you eliminate a square, you may right-click it to paint it dark gray.' & @CRLF & @CRLF & _ 'The type of the puzzle - Addition or Multiplication - is indicated in the white square at the bottom-right corner.' & @CRLF & @CRLF & _ 'Click the "Tools" icon on the upper-left corner to change puzzle type, grid size and other preferences.' & @CRLF & @CRLF & _ 'Enjoy :-)', $x, $y, $iSUI_W - $x * 2, $iSUI_H - 60) $y += $dy $gOK = GUICtrlCreateButton('OK, I think I got it...', $iSUI_ButtonSpacing, $iSUI_H - $iSUI_ButtonH - $iSUI_ButtonSpacing, $iSUI_W - $iSUI_ButtonSpacing * 2, $iSUI_ButtonH) $gdGUIEnd = GUICtrlCreateDummy() ; user interaction While True $msg = GUIGetMsg() Switch $msg Case $gOK, $GUI_EVENT_CLOSE ExitLoop EndSwitch WEnd ; delete all controls DeleteAllControls() ; resize GUI WinSetTrans($hGUI, '', 192) WinSetTitle($hGUI, '', $sAppName) _WinPoseAroundCenter($hGUI, $iGUI_Width + $iWDiff, $iGUI_Height + $iHDiff, $iResizeSpeed) WinSetTrans($hGUI, '', 255) Return False EndFunc ;==>Help Func GuiCtrlDeleteAndZero(ByRef $gID) If GUICtrlDelete($gID) Then $gID = 0 Return 1 Else Return 0 EndIf EndFunc ;==>GuiCtrlDeleteAndZero Func DeleteAllControls() Local $gIDStart = $gdGUIStart Local $gIDEnd = $gdGUIEnd For $gID = $gIDStart To $gIDEnd GuiCtrlDeleteAndZero($gID) Next EndFunc ;==>DeleteAllControls Func _WinPoseAroundCenter($hGUI, $iNewWidth, $iNewHeight, $iSpeed = 0) Local $aPos = WinGetPos($hGUI) Local $x = $aPos[0] + $aPos[2] / 2 - $iNewWidth / 2 If $x < 0 Then $x = 0 If $x + $iNewWidth > @DesktopWidth Then $x = @DesktopWidth - $iNewWidth Local $y = $aPos[1] + $aPos[3] / 2 - $iNewHeight / 2 If $y < 0 Then $y = 0 If $y + $iNewHeight > @DesktopHeight Then $y = @DesktopHeight - $iNewHeight _WinPose($hGUI, '', $x, $y, $iNewWidth, $iNewHeight, $iSpeed) EndFunc ;==>_WinPoseAroundCenter enjoy! P.S. what's with the 12KB size limit on the screenshot? 🤔3 points
-
WinPose UDF - simultaneous fluent move and resize
argumentum and one other reacted to orbs for a topic
Brief: native WinMove() has a "speed" parameter for a more fluent movement. unfortunately, that applies to the change in position, but not the change in size. the position changes in the specified "speed", but size changes abruptly. _WinPose() is similar to WinMove(), except that move and resize are simultaneous, both conform to the speed parameter. UDF: (save as "WinPose.au3") #include-once #include <WinAPISysWin.au3> ; #FUNCTION# ==================================================================================================================== ; Name ..........: _WinPose ; Description ...: same as native WinMove(), except that move and resize are simultaneous, both conform to the speed parameter. ; Syntax ........: _WinPose($hWnd, $sText, $x, $y, $w, $h[, $speed = 0]) ; Parameters ....: $hWnd - the title/hWnd/class of the window to pose. ; $sText - the text of the window to pose. ; $x - X coordinate to move to. ; $y - Y coordinate to move to. ; $w - [optional] new width of the window. ; $h - [optional] new height of the window. ; $speed - [optional] the speed to pose the window (smaller value = faster speed, 0 = instantaneous). ; Return values .: Success - a handle to the window. ; Failure - 0 if the window is not found (also sets @error to non-zero). ; Author ........: orbs ; Modified ......: ; Remarks .......: parameters and return values are practically identical to those of the native WinMove() function. ; Related .......: ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _WinPose($hWnd, $sText, $x, $y, $w = Default, $h = Default, $speed = 0) ; find the window to move If Not IsHWnd($hWnd) Then $hWnd = WinGetHandle($hWnd, $sText) If @error Then Return SetError(1, 0, False) EndIf Local $aPos = WinGetPos($hWnd) If @error Then Return SetError(2, 0, False) ; initialize variables Local Enum $pos_x, $pos_y, $pos_w, $pos_h Local Enum $aiCurrent, $aiTarget, $aiDelta, $aiRatio Local $aPosTarget[4][4] = [[$aPos[$pos_x], $x, 0, 0], [$aPos[$pos_y], $y, 0, 0], [$aPos[$pos_w], $w, 0, 0], [$aPos[$pos_h], $h, 0, 0]] ; accomodate for Default keyword For $iElement = 0 To 3 If $aPosTarget[$iElement][$aiTarget] = Default Then $aPosTarget[$iElement][$aiTarget] = $aPos[$iElement] Next ; calculate delta For $iElement = 0 To 3 $aPosTarget[$iElement][$aiDelta] = $aPosTarget[$iElement][$aiTarget] - $aPosTarget[$iElement][$aiCurrent] Next ; find the maximum delta Local $iMaxElement = 0, $iMaxDelta = 0 For $iElement = 0 To 3 If Abs($aPosTarget[$iElement][$aiDelta]) > $iMaxDelta Then $iMaxElement = $iElement $iMaxDelta = $aPosTarget[$iElement][$aiDelta] EndIf Next ; accomodate for negative delta If ($aPosTarget[$iMaxElement][$aiTarget] - $aPos[$iMaxElement]) < 0 Then $iMaxDelta = -$iMaxDelta ; calculate ratio for all elements For $iElement = 0 To 3 $aPosTarget[$iElement][$aiRatio] = $aPosTarget[$iElement][$aiDelta] / $iMaxDelta Next ; move & resize the window gradually For $iStep = 0 To $iMaxDelta For $iElement = 0 To 3 $aPosTarget[$iElement][$aiCurrent] += $aPosTarget[$iElement][$aiRatio] Next For $i = 1 To $speed _WinAPI_MoveWindow($hWnd, _ $aPosTarget[$pos_x][$aiCurrent], _ $aPosTarget[$pos_y][$aiCurrent], _ $aPosTarget[$pos_w][$aiCurrent], _ $aPosTarget[$pos_h][$aiCurrent], False) Next Next ; validate final outcome is as expected Return WinMove($hWnd, '', $x, $y, $w, $h) EndFunc ;==>_WinPose Example: #AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 #include <AutoItConstants.au3> #include <WindowsConstants.au3> #include <GUIConstantsEx.au3> #include 'WinPose.au3' Global Const $iMinW = 250, $iMinH = 100 Global $x, $y, $w, $h, $speed Global $hGUI = GUICreate('_WinPose() Example', $iMinW, $iMinH) Global $gButton = GUICtrlCreateButton('Click Me!', 25, 25, 200, 50) GUICtrlSetResizing(-1, $GUI_DOCKBORDERS) GUISetState(@SW_SHOW) Global $msg While True $msg = GUIGetMsg() Switch $msg Case $GUI_EVENT_CLOSE ExitLoop Case $gButton $w = Random($iMinW, @DesktopWidth / 3, 1) $h = Random($iMinH, @DesktopHeight / 3, 1) $x = Random(0, @DesktopWidth - $w, 1) $y = Random(0, @DesktopHeight - $h, 1) $speed = Random(10, 100, 1) _WinPose($hGUI, '', $x, $y, $w, $h, $speed) EndSwitch WEnd click the button to pose the window in a new random position and size, in a random speed. enjoy 🙂2 points -
#include <GUIConstants.au3> #include <GuiTreeView.au3> #include <GuiImageList.au3> GUICreate("Icon Test", 300, 300) Local $idTreeView = GUICtrlCreateTreeView(10, 10, 280, 280) GUISetState() Local $hImageList = _GUIImageList_Create(16, 16, 5, 3) Local $idxFolder = _GUIImageList_AddIcon($hImageList, "shell32.dll", 4) Local $idxApp = _GUIImageList_AddIcon($hImageList, "shell32.dll", 2) _GUICtrlTreeView_SetNormalImageList(GUICtrlGetHandle($idTreeView), $hImageList) Local $h1 = _GUICtrlTreeView_Add($idTreeView, 0, "Category", $idxFolder) _GUICtrlTreeView_AddChild($idTreeView, $h1, "App", $idxApp) While 1 If GUIGetMsg() = $GUI_EVENT_CLOSE Then ExitLoop WEnd Win10.1 point
-
JSON UDF in pure AutoIt
WildByDesign reacted to SOLVE-SMART for a topic
I guess the FileWrite action that you described (README.md file) is necessary for the understanding of the cascade ".json --> AutoIt structure --> .json", yes ✅ . I also believe the JSON object manipulation was meant to be described ⚠ . The array/map handling of the JSON object, was maybe unclear. Once it's manipulated, the write to file action is quite simple (as you already mentioned). I won't be able to submit a PR (pull request) as a suggestion anytime soon, so here are just my brief thoughts on the matter. You already use "example.json" in the examples file "JSON-Test.au3". So I would extend the file by an example (with the example.json") for the topic above. Besides that @AspirinJunkie, thanks for the crazy improvements (impressive). Good work as usually 😁 . Best regards Sven1 point