Jump to content

MathMagic Screensaver


CoePSX
 Share

Recommended Posts

This started as a simple math play-around. Then came the idea of making a scrensaver.

Now it's really cool, with multi-colors and gradients. Fancy stuff. :whistle:

Like a real screensaver, it has a configuration interface. The only thing too complicated to

do was the preview on windows video configuration window.

Anyway, the script itself is quite simple. It renders a nice looking drawing based on circle

coordenates jumping N degrees at a time, and increasing it's diameter. You can see N

on the upper left corner of the screen.

Thanks to everyone who helped me doing this.

Specially Lakes, Icekirby1, fisofo and Wus. And thanks to piccaso and Valik for helping with the redraw stuff.

EDIT: Now it has customizable gradients and the keyboard down stuff are handled 100% in AutoIt.

#NoTrayIcon
#include <GUIConstants.au3>
#include <Misc.au3>

If $CmdLine[0] > 0 Then
    $CommandLine = StringLeft($CmdLine[1], 2)
Else
    $CommandLine = "/c"
EndIf

Global $DetailsLevel = RegRead("HKEY_CURRENT_USER\Software\MathMagicSCR", "DetailsLevel")
If $DetailsLevel = "" Then
    $DetailsLevel = 3
EndIf
Global $Delay = RegRead("HKEY_CURRENT_USER\Software\MathMagicSCR", "Delay")
If $Delay = "" Then
    $Delay = 30
EndIf
Global $WaitTime = RegRead("HKEY_CURRENT_USER\Software\MathMagicSCR", "WaitTime")
If $WaitTime = "" Then
    $WaitTime = 2
EndIf
Global $Cursor = RegRead("HKEY_CURRENT_USER\Software\MathMagicSCR", "Cursor")
If $Cursor = "" Then
    $Cursor = 1
EndIf
Global $MultiColor = RegRead("HKEY_CURRENT_USER\Software\MathMagicSCR", "MultiColor")
If $MultiColor = "" Then
    $MultiColor = 1
EndIf
Global $Gradient = RegRead("HKEY_CURRENT_USER\Software\MathMagicSCR", "Gradient")
If $Gradient = "" Then
    $Gradient = 1
EndIf
Global $GradientColor1 = RegRead("HKEY_CURRENT_USER\Software\MathMagicSCR", "Color1")
If $GradientColor1 = "" Then
    $GradientColor1 = 0x0000FF
EndIf
Global $GradientColor2 = RegRead("HKEY_CURRENT_USER\Software\MathMagicSCR", "Color2")
If $GradientColor2 = "" Then
    $GradientColor2 = 0x00FF00
EndIf
Global $RandomGradient = RegRead("HKEY_CURRENT_USER\Software\MathMagicSCR", "Random")
If $RandomGradient = "" Then
    $RandomGradient = 1
EndIf
Global Const $WM_KEYDOWN = 0x0100
Global Const $Pi = 3.1415926535897932384626433832795
Global $IncreaseSize = (360/($DetailsLevel*90)) * (@DesktopWidth/1000)
Global $MaxTimes = ($DetailsLevel * 90)
Global $Pos = MouseGetPos()
Global $State = 1
Global $Color0
Opt("ColorMode", 1)

If $CommandLine = "/c" Then
    ;Create the configuration window
    $GUI = GuiCreate("MathMagic Screensaver Options", 250, 200, -1, -1, $WS_CAPTION)
    GuiCtrlCreateLabel("Details level:", 10, 13, 100, 20)
    $DetailsSlider = GuiCtrlCreateSlider(110, 5, 120, 30)
    $DetailsLabel = GUICtrlCreateLabel($DetailsLevel, 225, 13, 20, 15, $SS_CENTER)
    GuiCtrlCreateLabel("Drawing speed:", 10, 40, 100, 25)
    $SpeedSlider = GuiCtrlCreateSlider(110, 35, 120, 30)
    $SpeedLabel = GUICtrlCreateLabel((100 - $Delay), 225, 43, 20, 15, $SS_CENTER)
    GuiCtrlCreateLabel("Time between draws:", 10, 75, 100, 25)
    $WaitSlider = GuiCtrlCreateSlider(110, 70, 120, 30)
    $WaitLabel = GUICtrlCreateLabel($WaitTime, 225, 78, 20, 15, $SS_CENTER)
    $CursorCheck = GUICtrlCreateCheckbox("Show pencil", 15, 115, 75, 15)
    $ColorCheck = GUICtrlCreateCheckbox("Multi-color", 95, 115, 70, 15)
    $GradientCheck = GUICtrlCreateCheckbox("Gradient", 170, 115, 65, 15)
    $ColorButton1 = GUICtrlCreateButton("->", 10, 135, 20, 20)
    Dim $GradientExample[121]
    For $i = 0 To 120
        $GradientExample[$i] = GUICtrlCreateLabel("", 30+$i, 135, 1, 20)
        GUICtrlSetBkColor(-1, 0xFFFFFF)
    Next
    $ColorButton2 = GUICtrlCreateButton("<-", 150, 135, 20, 20)
    $RandomCheck = GUICtrlCreateCheckbox("Random", 175, 138, 60, 15)
    $OK = GuiCtrlCreateButton("OK", 10, 160, 90, 30)
    $Cancel = GuiCtrlCreateButton("Cancel", 160, 160, 80, 30)
    $About = GuiCtrlCreateButton("About", 100, 160, 60, 30)
    GUICtrlSetData($DetailsSlider, $DetailsLevel)
    GUICtrlSetData($SpeedSlider, (100 - $Delay))
    GUICtrlSetData($WaitSlider, $WaitTime)
    GUICtrlSetLimit($DetailsSlider, 10, 1)
    GUICtrlSetLimit($SpeedSlider, 100, 1)
    GUICtrlSetLimit($WaitSlider, 10, 0)
    GUICtrlSetState($CursorCheck, $Cursor)
    GUICtrlSetState($ColorCheck, $MultiColor)
    GUICtrlSetState($GradientCheck, $Gradient)
    GUICtrlSetState($RandomCheck, $RandomGradient)
    UpdateExample()
    GuiSetState()
    
    While 1
        If GUICtrlRead($DetailsLabel) <> GUICtrlRead($DetailsSlider) Then GUICtrlSetData($DetailsLabel, GUICtrlRead($DetailsSlider))
        If GUICtrlRead($SpeedLabel) <> GUICtrlRead($SpeedSlider) Then GUICtrlSetData($SpeedLabel, GUICtrlRead($SpeedSlider))
        If GUICtrlRead($WaitLabel) <> GUICtrlRead($WaitSlider) Then GUICtrlSetData($WaitLabel, GUICtrlRead($WaitSlider))
        
        ;If Multi-Colors is selected, disables the gradient controls
        If GUICtrlRead($ColorCheck) = 1 And $State = 1 Then
            GUICtrlSetState($ColorButton1, $GUI_DISABLE)
            GUICtrlSetState($ColorButton2, $GUI_DISABLE)
            GUICtrlSetState($RandomCheck, $GUI_DISABLE)
            $State = 0
        ElseIf GUICtrlRead($ColorCheck) = 4 And $State = 0 Then
            GUICtrlSetState($ColorButton1, $GUI_ENABLE)
            GUICtrlSetState($ColorButton2, $GUI_ENABLE)
            GUICtrlSetState($RandomCheck, $GUI_ENABLE)
            $State = 1
        EndIf
        
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                Exit
            Case $Cancel
                Exit
            Case $About
                MsgBox(64, "About", "MathMagic Screensaver" & @CRLF & @CRLF & "2006 - CoePSX")
            Case $OK
                RegWrite("HKEY_CURRENT_USER\Software\MathMagicSCR", "DetailsLevel", "REG_SZ", GUICtrlRead($DetailsSlider))
                RegWrite("HKEY_CURRENT_USER\Software\MathMagicSCR", "Delay", "REG_SZ", 100 - GUICtrlRead($SpeedSlider))
                RegWrite("HKEY_CURRENT_USER\Software\MathMagicSCR", "WaitTime", "REG_SZ", GUICtrlRead($WaitSlider))
                RegWrite("HKEY_CURRENT_USER\Software\MathMagicSCR", "Cursor", "REG_SZ", GUICtrlRead($CursorCheck))
                RegWrite("HKEY_CURRENT_USER\Software\MathMagicSCR", "MultiColor", "REG_SZ", GUICtrlRead($ColorCheck))
                RegWrite("HKEY_CURRENT_USER\Software\MathMagicSCR", "Gradient", "REG_SZ", GUICtrlRead($GradientCheck))
                RegWrite("HKEY_CURRENT_USER\Software\MathMagicSCR", "Color1", "REG_SZ", $GradientColor1)
                RegWrite("HKEY_CURRENT_USER\Software\MathMagicSCR", "Color2", "REG_SZ", $GradientColor2)
                RegWrite("HKEY_CURRENT_USER\Software\MathMagicSCR", "Random", "REG_SZ", GUICtrlRead($RandomCheck))
                Exit
            Case $ColorButton1
                $GradientColor1 = _ChooseColor(1, $GradientColor1)
                UpdateExample()
            Case $ColorButton2
                $GradientColor2 = _ChooseColor(1, $GradientColor2)
                UpdateExample()
        EndSwitch
    WEnd
ElseIf $CommandLine = "/s" Then
    ;Creates the screensaver fullscreen window
    $Window = GUICreate("", @DesktopWidth, @DesktopHeight, 0, 0, $WS_POPUP, BitOr($WS_EX_TOPMOST, $WS_EX_TOOLWINDOW))
    $Label = GUICtrlCreateLabel("", 20, 20, 60, 30)
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetFont($Label, 18, 400)
    $Graphic = GUICtrlCreateGraphic(0, 0, @DesktopWidth, @DesktopHeight)
    $Pointer = GUICtrlCreateLabel("", @DesktopWidth, @DesktopHeight, 6, 6)
    GUICtrlSetBkColor($Pointer, 0xFFFFFF)
    GUISetBkColor(0)
    GUISetCursor(16, 1)
    GUISetState()
    
    ;Create an empty dummy window to handle the Windows Messages
    $Dummy = GUICreate("", @DesktopWidth, @DesktopHeight, 0, 0, $WS_POPUP, $WS_EX_TOOLWINDOW)
    GUIRegisterMsg($WM_KEYDOWN, "Quit")
    GUISetBkColor(0)
    GUISetCursor(16, 1)
    GUISetState()
    
    ;Sets the options to exit if user moves the mouse or presses a key
    Opt("GUIOnEventMode", 1)
    GUISetOnEvent($GUI_EVENT_MOUSEMOVE, "Quit")
    
    ;Opens the DLLs for the DLLCalls
    $GDI32 = DllOpen("gdi32.dll")
    $USER32 = DllOpen("user32.dll")
    $GUIHDC = DllCall ($USER32,"int","GetDC","hwnd", $Window)
    
    While 1
        ;Chooses a random iNumber and handle colors
        $PicSize = 1
        Do
            $iNumber = Random(10, 350, 1)
        Until $iNumber <> 180
        If $iNumber < 180 Then
            $iColors = Round(360/$iNumber)
        Else
            $iColors = Round(Abs(360/(360-$iNumber)))
        EndIf
        For $i = 0 To $iColors
            Assign("Color" & $i, ColorGradient(RandomColor(), RandomColor(), $MaxTimes+1))
        Next
        If $MultiColor = 4 And $RandomGradient = 4 Then
            $Color0 = ColorGradient($GradientColor1, $GradientColor2, $MaxTimes+1)
        EndIf
        
        ;Redraw the window
        DllCall($USER32, "int", "InvalidateRect", "hwnd", $Window, "int", 0, "int", 1)
        
        GUICtrlSetColor($Label, $Color0[0])
        GUICtrlSetData($Label, $iNumber & "°")
        
        ;This is the drawing part
        DllCall ($GDI32, "int", "MoveToEx", "hwnd", $GUIHDC[0], "int", @DesktopWidth/2, "int", @DesktopHeight/2, "ptr", 0)
        For $R = 1 To $MaxTimes
            $PicSize += $IncreaseSize
            $X = Sin($iNumber * ($R*($Pi/180))) * $PicSize + @DesktopWidth/2
            $Y= Cos($iNumber * ($R*($Pi/180))) * $PicSize + @DesktopHeight/2
            If $MultiColor = 1 Then
                If $Gradient = 1 Then
                    $NowColor = Eval("Color" & Mod($R, $iColors))
                    $NowColor = $NowColor[$R]
                Else
                    $NowColor = Eval("Color" & Mod($R, $iColors))
                    $NowColor = $NowColor[0]
                EndIf
            Else
                If $Gradient = 1 Then
                    $NowColor = $Color0[$R]
                Else
                    $NowColor = $Color0[0]
                EndIf
            EndIf
            GUICtrlSetColor($Label, $Color0[$R])
            If $Cursor = 1 Then GUICtrlSetPos($Pointer, $X-3, $Y-3)
            $Pen = DllCall($GDI32, "hwnd", "CreatePen", "int", "0", "int", "0", "hwnd", $NowColor)
            DllCall($GDI32, "hwnd", "SelectObject", "hwnd", $GUIHDC[0], "hwnd", $Pen[0])
            DllCall ($GDI32, "int", "LineTo", "hwnd", $GUIHDC[0], "int", $X, "int", $Y)
            Sleep($Delay)
        Next
        DllCall ($USER32,"int","ReleaseDC","int",$GUIHDC[0],"hwnd",$Window)
        Sleep($WaitTime*1000)
    WEnd
EndIf

Func Quit()
    GUIDelete($Dummy)
    GUIDelete($Window)
    DllCall($USER32, "int", "InvalidateRect", "hwnd", 0, "int", 0, "int", 1)
    Exit
EndFunc
Func UpdateExample()
    Local $ExampleColor = ColorGradient($GradientColor1, $GradientColor2, 121)
    For $i = 0 To 120
        GUICtrlSetBkColor($GradientExample[$i], $ExampleColor[$i])
    Next
EndFunc
Func ColorGradient($hInitialColor, $hFinalColor, $iReturnSize)
    $hInitialColor = Hex($hInitialColor, 6)
    $hFinalColor = Hex($hFinalColor, 6)
    
    Local $iRed1 = Dec (StringLeft($hInitialColor, 2))
    Local $iGreen1 = Dec (StringMid($hInitialColor, 3, 2))
    Local $iBlue1 = Dec (StringMid($hInitialColor, 5, 2))
    
    Local $iRed2 = Dec (StringLeft($hFinalColor, 2))
    Local $iGreen2 = Dec (StringMid($hFinalColor, 3, 2))
    Local $iBlue2 = Dec (StringMid($hFinalColor, 5, 2))
    
    Local $iPlusRed = ($iRed2-$iRed1)/($iReturnSize-1)
    Local $iPlusBlue = ($iBlue2-$iBlue1)/($iReturnSize-1)
    Local $iPlusGreen = ($iGreen2-$iGreen1)/($iReturnSize-1)
    
    Dim $iColorArray[$iReturnSize]
    For $i = 0 To $iReturnSize-1
        $iNowRed = Floor($iRed1 + ($iPlusRed*$i))
        $iNowBlue = Floor($iBlue1 + ($iPlusBlue*$i))
        $iNowGreen = Floor($iGreen1 + ($iPlusGreen*$i))
        $iColorArray[$i] = Dec (Hex($iNowRed, 2) & Hex($iNowGreen, 2) & Hex($iNowBlue, 2))
    Next
    Return ($iColorArray)
EndFunc
Func RandomColor($hMinColor = 0x000000, $hMaxColor = 0xFFFFFF)
    $hMinColor = Hex($hMinColor, 6)
    $hMaxColor = Hex($hMaxColor, 6)
    
    Local $iRed1 = Dec (StringLeft($hMinColor, 2))
    Local $iGreen1 = Dec (StringMid($hMinColor, 3, 2))
    Local $iBlue1 = Dec (StringMid($hMinColor, 5, 2))
    
    Local $iRed2 = Dec (StringLeft($hMaxColor, 2))
    Local $iGreen2 = Dec (StringMid($hMaxColor, 3, 2))
    Local $iBlue2 = Dec (StringMid($hMaxColor, 5, 2))
    
    Local $iRndRed = Random($iRed1, $iRed2, 1)
    Local $iRndGreen = Random($iGreen1, $iGreen2, 1)
    Local $iRndBlue = Random($iBlue1, $iBlue2, 1)
    
    Return Dec (Hex($iRndRed, 2) & Hex($iRndGreen, 2) & Hex($iRndBlue, 2))
EndFunc

And the attached compiled .SCR and source code.

MathMagicSCR.zip

Edited by CoePSX

[quote name='Valik' post='301213' date='Jan 31 2007, 10:36 PM']You seem to have a habit of putting things in the wrong place. I feel sorry for any female you attempt to have sex with.[/quote][font="Lucida Sans Unicode"][/font]

Link to comment
Share on other sites

Glad you worked everything out. It works perfect!

My Programs:AInstall - Create a standalone installer for your programUnit Converter - Converts Length, Area, Volume, Weight, Temperature and Pressure to different unitsBinary Clock - Hours, minutes and seconds have 10 columns each to display timeAutoIt Editor - Code Editor with Syntax Highlighting.Laserix Editor & Player - Create, Edit and Play Laserix LevelsLyric Syncer - Create and use Synchronised Lyrics.Connect 4 - 2 Player Connect 4 Game (Local or Online!, Formatted Chat!!)MD5, SHA-1, SHA-256, Tiger and Whirlpool Hash Finder - Dictionary and Brute Force FindCool Text Client - Create Rendered ImageMy UDF's:GUI Enhance - Enhance your GUIs visually.IDEA File Encryption - Encrypt and decrypt files easily! File Rename - Rename files easilyRC4 Text Encryption - Encrypt text using the RC4 AlgorithmPrime Number - Check if a number is primeString Remove - remove lots of strings at onceProgress Bar - made easySound UDF - Play, Pause, Resume, Seek and Stop.
Link to comment
Share on other sites

Thanks! The dll is really small. It's quite useful.

EDIT:

Although i just realized that

Global Const $WM_KEYDOWN = 0x0100

GUIRegisterMsg($WM_KEYDOWN, "Quit")

Would do the job XD

Thanks Piccaso, the freeware site is a great idea actually.

Edited by CoePSX

[quote name='Valik' post='301213' date='Jan 31 2007, 10:36 PM']You seem to have a habit of putting things in the wrong place. I feel sorry for any female you attempt to have sex with.[/quote][font="Lucida Sans Unicode"][/font]

Link to comment
Share on other sites

as far as i know the WM_KEY* Notifications are not send in this case.

i think its because of the label's...

but i didnt try it :whistle:

edit:

now i tryed it (again) and it (still) doesent work :)

and there will probably be an issue with the dll if you use 'RightMouseClick -> Install'

i think the dll will not be copyed with the scr... (fileinstall should solve that)

Edited by piccaso
CoProc Multi Process Helper libraryTrashBin.nfshost.com store your AutoIt related files here!AutoIt User Map
Link to comment
Share on other sites

You gave the idea of color in teh first place.

@Piccaso, you were right, the Message thing only worked without anything in the GUI. But pressing a key will still make it exit now :whistle:

[quote name='Valik' post='301213' date='Jan 31 2007, 10:36 PM']You seem to have a habit of putting things in the wrong place. I feel sorry for any female you attempt to have sex with.[/quote][font="Lucida Sans Unicode"][/font]

Link to comment
Share on other sites

You don't need the extra DLL. Just use the function _IdleTicks() from this topic and inside the loop keep on comparing it to the previous call. If the number is less than the last call you know the user moved the mouse or pressed a key. Example:

$last = 0
While 1
    $new = _IdleTicks()
    If $new < $last Then Exit    ;user did something
    $last = $new
WEnd
Edited by erifash
Link to comment
Share on other sites

but keep in mind that GetLastInputInfo reqires win2k

Ah, ok. I just really enjoy autoit-only solutions though. It shows how much the language can really shine. :P
Link to comment
Share on other sites

This so cool - I cannot wait until I can understand the code :P

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Link to comment
Share on other sites

Thanks for referencing me! Although I too didn't really do anything... but still :P

Couple things:

1. The speed level adjustment should probably be adjusted, on my pc everything is really slow, then there's like one "click" that's medium, one for fast, and one instantaneous. It'd be nice to have a bigger spread for the faster speeds

2. What about being able to specify the beginning and end colors for the gradient? Does autoit support a color-picker? And on a related note, would it be difficult to make the gradient cycle through more colors?

I'm gonna see what i can figure out for those two, but i'm a bit of a newb at autoit still. Nice work!

Link to comment
Share on other sites

Thanks for referencing me! Although I too didn't really do anything... but still :nuke:

Couple things:

1. The speed level adjustment should probably be adjusted, on my pc everything is really slow, then there's like one "click" that's medium, one for fast, and one instantaneous. It'd be nice to have a bigger spread for the faster speeds

2. What about being able to specify the beginning and end colors for the gradient? Does autoit support a color-picker? And on a related note, would it be difficult to make the gradient cycle through more colors?

I'm gonna see what i can figure out for those two, but i'm a bit of a newb at autoit still. Nice work!

OK... I did a few modifications in the script. Now it's even bigger... Not so simple anymore.

OK, about what you said:

1. Tried to adjust it, should be better now.

2. Done. Yes. It is possible, but it would be too much work.

Added a gradient example to the options window.

The mousemove and keyboard down stuff is solved only in AutoIt. I created a clean dummy window to handle the Windows messages.

The screen redrawing is now done using WinSetTrans.

Check the first post for the code. :P

[quote name='Valik' post='301213' date='Jan 31 2007, 10:36 PM']You seem to have a habit of putting things in the wrong place. I feel sorry for any female you attempt to have sex with.[/quote][font="Lucida Sans Unicode"][/font]

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