Jump to content
Luigi

help to improve this algorithm to move a box(poligon)

Recommended Posts

Luigi

Hello forum!

This code draw 3 box (retangules) in gui. You can move any box to anyware (inside GUI).

The main idea is move a poligon in the GUI.

At this time I can do this with regular poligon (box, retangule), its work fine...
When 3 box together, work fine!
It is my best in the moment.

I want to do this with irregular poligon (triangule and others, any poligon type). I need help here!

Someone can help-me to improve this algoritm?

#include-once
#include <Array.au3>
#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>

;#include <JSMN.au3>
;#include <object_dump.au3>

Opt("GUIOnEventMode", 1)
Opt("GUIEventOptions", 1)
Opt("MustDeclareVars", 1)

Global CONST $SD = "Scripting.Dictionary"
Global $hGui
Global $hGraphic, $hPen, $hBitmap, $hBackbuffer, $aTimer[2], $bIn = False, $static = -1
Global $aMouse
Global $aPos[16] = [0, 0, -@MSEC, @MSEC, 0, 0, False, False, -1, -1, 0, 0, 0, 0]
;   $aPos[0]    x atual do mouse
;   $aPos[1]    y autal do mouse
;   $aPos[2]    x antigo do mouse
;   $aPos[3]    y antigo do mouse
;   $aPos[4]    x atual do mouse, quando o botão esquerdo do mouse é pressionado
;   $aPos[5]    y atual do mouse, quando o botão esquerdo do mouse é pressionado
;   $aPos[6]    True:   botão direito do mouse pressionado
;               False:  botão direito do mouse liberado
;   $aPos[7]    True:   botão esquerdo do mouse pressionado
;               False:  botão esquerdo do mouse liberado
;   $aPos[8]    $aPos[6] antigo
;   $aPos[9]    $aPos[7] antigo
;   $aPos[10]   x relativo do objeto
;   $aPos[11]   y relativo do objeto
;   $aPos[12]   x de origem ao pegar
;   $aPos[13]   y de origem ao pegar

Global $sLabel = ""
Global $aIndex
Global $move
Global $g_iOBJ = 0

Global $aSize[2] = [320, 240]
Global Const $nPI = 3.1415926535897932384626433832795
Global $aBoard[6] = [0, 0, $aSize[0] - 0, $aSize[1] - 0]
$aBoard[4] = $aBoard[2] - $aBoard[0]
$aBoard[5] = $aBoard[3] - $aBoard[1]

Global $aDot[$aBoard[5]][$aBoard[4]]

ConsoleWrite("Ubound[ " & UBound($aDot, 1) & "," & UBound($aDot, 2) & "]" & @LF)
Global $oDot = ObjCreate($SD)

ConsoleWrite("$aBoard[ " & _ArrayToString($aBoard, ",") & "]" & @LF)


$hGui = GUICreate("draw", $aSize[0], $aSize[1])
GUISetOnEvent($GUI_EVENT_CLOSE, "quit")

_GDIPlus_Startup()
$hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGui)
$hBitmap = _GDIPlus_BitmapCreateFromGraphics($aBoard[4], $aBoard[5], $hGraphic)
$hBackbuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap)

$hPen = _GDIPlus_PenCreate()
_GDIPlus_GraphicsClear($hBackbuffer)
_GDIPlus_GraphicsSetSmoothingMode($hBackbuffer, 4)

add_box(20, 30, 20, 40)
add_box(80, 30, 20, 40)
add_box(30, 60, 60, 20)

AdlibRegister("_update", 20)
AdlibRegister("BoardIn", 10)

$aTimer[0] = TimerInit()
$aTimer[1] = $aTimer[0]

GUISetState(@SW_SHOWNORMAL)

While True
    If ($static <> $bIn) Or ($aPos[0] <> $aPos[2] Or $aPos[1] <> $aPos[3]) Or $aPos[6] <> $aPos[8] Then
        $aPos[8] = $aPos[6]
        $aPos[2] = $aPos[0]
        $aPos[3] = $aPos[1]
        $aIndex = $aDot[$aPos[1]][$aPos[0]]
        $sLabel = IsArray($aIndex) ? _ArrayToString($aIndex, ",") : ""
        WinSetTitle($hGui, "", "[" & $aPos[0] & "x" & $aPos[1] & "] $oo[" & $sLabel & "] $bIN{ " & $bIn & " }")
        $static = $bIn
    EndIf
    If $bIn And $aMouse[2] And Not $aPos[6] And Not $move Then
        $g_iOBJ = IsArray($aIndex) ? $aIndex[UBound($aIndex, 1) - 1] : 0
        If $g_iOBJ Then
            $aPos[4] = $aPos[0]
            $aPos[5] = $aPos[1]
            $aPos[6] = True
            pega()
            ConsoleWrite(" Pega[ " & $aPos[0] & "," & $aPos[1] & " ]" & @LF)
        EndIf
    EndIf
    If $bIn And Not $aMouse[2] And $aPos[6] And $move Then
        $aPos[6] = False
        $aPos[14] = $aPos[0]
        $aPos[15] = $aPos[1]
        solta()
        $aPos[4] = $aPos[0]
        $aPos[5] = $aPos[1]
        ConsoleWrite("Solta[ " & $aPos[0] & "," & $aPos[1] & " ]" & @LF)
    EndIf
    If $move Then move()
WEnd

Func move()
    If Not $g_iOBJ Then Return
    $oDot.Item($g_iOBJ).Item("x") = $aPos[0] - $aPos[10]
    $oDot.Item($g_iOBJ).Item("y") = $aPos[1] - $aPos[11]
EndFunc   ;==>move

Func pega()
    If Not $g_iOBJ Then Return
    $aPos[10] = $aPos[0] - $oDot.Item($g_iOBJ).Item("x")
    $aPos[11] = $aPos[1] - $oDot.Item($g_iOBJ).Item("y")
    $aPos[12] = $oDot.Item($g_iOBJ).Item("x")
    $aPos[13] = $oDot.Item($g_iOBJ).Item("y")
    $move = True
    ConsoleWrite("+$iXXr[ " & $aPos[10] & " ] $iYYr[ " & $aPos[11] & " ] " & @LF)
;~  dump($oDot.Item(UBound($aIndex, 1)))
EndFunc   ;==>pega

Func solta()
    If Not $g_iOBJ Then Return
    $move = False
    Local $iSearch

    Local $aTemp[1]
    For $jj = $aPos[12] To $aPos[12] + $oDot.Item($g_iOBJ).Item("l") - 1
        For $ii = $aPos[13] To $aPos[13] + $oDot.Item($g_iOBJ).Item("a") - 1
            $aTemp = $aDot[$ii][$jj]
            If IsArray($aTemp) Then
;~              ConsoleWrite("$aTemp[ " & _ArrayToString($aTemp, ",") & " ]"& @LF)
                $iSearch = _ArraySearch($aTemp, $g_iOBJ)
                If $iSearch >= 0 Then
                    _ArrayDelete($aTemp, $iSearch)
                    If Ubound($aTemp, 1) Then
                        $aDot[$ii][$jj] = $aTemp
                    Else
                        $aDot[$ii][$jj] = 0
                    EndIf
                EndIf
            EndIf
        Next
    Next

    $oDot.Item($g_iOBJ).Item("x") = $aPos[14] - $aPos[10]
    $oDot.Item($g_iOBJ).Item("y") = $aPos[15] - $aPos[11]

    For $jj = $oDot.Item($g_iOBJ).Item("x") To $oDot.Item($g_iOBJ).Item("x") + $oDot.Item($g_iOBJ).Item("l") - 1
        For $ii = $oDot.Item($g_iOBJ).Item("y") To $oDot.Item($g_iOBJ).Item("y") + $oDot.Item($g_iOBJ).Item("a") - 1
            $aTemp = $aDot[$ii][$jj]
            If IsArray($aTemp) Then
                _ArrayAdd($aTemp, $g_iOBJ)
            Else
                Local $aTemp[1] = [$g_iOBJ]
            EndIf
            $aDot[$ii][$jj] = $aTemp
        Next
    Next
    $g_iOBJ = 0
EndFunc   ;==>solta

Func add_box($xx, $yy, $ll, $aa)
    Local $iIndex = $oDot.Count + 1
;~  ConsoleWrite("$iIndex[ " & $iIndex & " ]" & @LF)
    Local $oo = ObjCreate($SD)
    Local $aLayer[1]
    Local $aTemp[1]
    $oo.Add("type", "box")
    $oo.Add("x", $xx)
    $oo.Add("y", $yy)
    $oo.Add("l", $ll)
    $oo.Add("a", $aa)
    For $jj = $xx To $xx + $ll - 1
        For $ii = $yy To $yy + $aa - 1
            If IsArray($aDot[$ii][$jj]) Then
                $aTemp = $aDot[$ii][$jj]
                _ArrayAdd($aTemp, $iIndex)
            Else
                Local $aTemp[1] = [$iIndex]
            EndIf
            $aDot[$ii][$jj] = $aTemp
        Next
    Next
    $oDot.Add($iIndex, $oo)
EndFunc   ;==>add_box

Func _update()
    _GDIPlus_GraphicsClear($hBackbuffer, 0xF0FFFFFF)
    For $each In $oDot
        _box($hBackbuffer, $oDot.Item($each).Item("x"), $oDot.Item($each).Item("y"), $oDot.Item($each).Item("l"), $oDot.Item($each).Item("a"))
    Next
    _GDIPlus_GraphicsDrawImageRect($hGraphic, $hBitmap, $aBoard[0], $aBoard[1], $aBoard[4], $aBoard[5])
EndFunc   ;==>_update

Func _box($hToGraphic, $xx, $yy, $ll, $aa)
    Local $aBox[5][2]
    $aBox[0][0] = 4
    $aBox[1][0] = $xx
    $aBox[1][1] = $yy
    $aBox[2][0] = $xx + $ll - 1
    $aBox[2][1] = $yy
    $aBox[3][0] = $xx + $ll - 1
    $aBox[3][1] = $yy + $aa - 1
    $aBox[4][0] = $xx
    $aBox[4][1] = $yy + $aa - 1
    _GDIPlus_GraphicsDrawPolygon($hToGraphic, $aBox)
EndFunc   ;==>_box

Func on_exit()
    _GDIPlus_GraphicsDispose($hBackbuffer)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_PenDispose($hPen)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_Shutdown()
EndFunc   ;==>on_exit

Func quit()
    Exit
EndFunc   ;==>quit

#cs
    ;~ $hMsg = GUIGetMsg()
    ;~ Switch $hMsg
    ;~ Case $GUI_EVENT_CLOSE
    ;~ _exit()
    ;~ EndSwitch

    ;~ Sleep(50)

    ;~ If WinActive($hGui) Then
    ;~ _update()
    ;~ If Not $active Then $active = True
    ;~ ElseIf Not WinActive($hGui) And $active Then
    ;~ $active = False
    ;~ EndIf

    ;~  If TimerDiff($aTimer[0]) >= 20 Then

    ;~      $aTimer[0] = TimerInit()
    ;~      _update()
    ;~  EndIf
#CE

Func BoardIn()
    $aMouse = GUIGetCursorInfo($hGui)
    $aPos[0] = $aMouse[0] - $aBoard[0]
    $aPos[0] = $aPos[0] < 0 ? 0 : ($aPos[0] >= $aBoard[4] ? $aBoard[4] - 1 : $aPos[0])
    $aPos[1] = $aMouse[1] - $aBoard[1]
    $aPos[1] = $aPos[1] < 0 ? 0 : ($aPos[1] >= $aBoard[5] ? $aBoard[5] - 1 : $aPos[1])
    If IsArray($aMouse) And $aMouse[0] >= $aBoard[0] And $aMouse[1] >= $aBoard[1] And $aMouse[0] <= $aBoard[2] And $aMouse[1] <= $aBoard[3] Then
        $bIn = True
        Return
    EndIf
    $bIn = False
EndFunc   ;==>BoardIn

 

Edited by Luigi

Visit my repository

Share this post


Link to post
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

×