Resize a Square Using the Mouse
Forum » The Community / Challenges » Resize a Square Using the Mouse
started by: hartnellhartnell
on: 1207594865|%e %b %Y, %H:%M %Z|agohover
number of posts: 13
rss icon RSS: new posts
summary:
Advanced Challenge
Resize a Square Using the Mouse
hartnellhartnell 1207594865|%e %b %Y, %H:%M %Z|agohover

The challenge is this :

  • Create a square that can be
    • Stretched along the x and y axises
    • Resized simultaneously along the x and y axises.

Yeah, I know, just did it on the GMWiki, challenges will be posted here first whenever possible from now on.

unfold Resize a Square Using the Mouse by hartnellhartnell, 1207594865|%e %b %Y, %H:%M %Z|agohover
Re: Resize a Square Using the Mouse
GoshaGosha 1219333000|%e %b %Y, %H:%M %Z|agohover

Uhm, like so?

FreeBASIC:

Option Explicit

const SCRWIDTH as integer = 640
const SCRHEIGHT as integer = 480

dim mousey, mousex as integer

screen 18
while inkey <> "q"
    cls
    locate 2, 2
    print "Press q to quit."
    getmouse mousex, mousey
    if (mousex >= 0) and (mousey >= 0) then
        line (SCRWIDTH/2, SCRHEIGHT/2)-(mousex,mousey), 11, BF
    end if
    sleep 10
wend

end
last edited on 1219333217|%e %b %Y, %H:%M %Z|agohover by Gosha + show more
unfold Re: Resize a Square Using the Mouse by GoshaGosha, 1219333000|%e %b %Y, %H:%M %Z|agohover
Re: Resize a Square Using the Mouse
hartnellhartnell 1219340279|%e %b %Y, %H:%M %Z|agohover

This passes the challenge. :)

— hartnell

unfold Re: Resize a Square Using the Mouse by hartnellhartnell, 1219340279|%e %b %Y, %H:%M %Z|agohover
Re: Resize a Square Using the Mouse
hartnellhartnell 1219340360|%e %b %Y, %H:%M %Z|agohover

Er… crap. Does anyone know where that dumbass hartnell put the challenge scoreboard?

— hartnell

unfold Re: Resize a Square Using the Mouse by hartnellhartnell, 1219340360|%e %b %Y, %H:%M %Z|agohover
Re: Resize a Square Using the Mouse
GoshaGosha 1219346112|%e %b %Y, %H:%M %Z|agohover

So, I made a cube that can be moved and resized.

Added up to 131 rows with comments! hehe.
I wonder, is anything unessecary here? That could be removed/replaced with something alot easier:

Updated 08/08/27

Option Explicit

' Defines the cube start position
' The offsets are required to maintain the squares size.
' offset -> offset + size = size of square
' By the way, they're all shared so they can be used in subs.
Dim Shared offsetx As Integer = 20
Dim Shared offsety As Integer = 10
Dim Shared sizex As Integer = 400
Dim Shared sizey As Integer = 400

' Prepare variables for the mouse and the mouse buttons
Dim Shared mousey As Integer
Dim Shared mousex As Integer
Dim Shared mousebtns As Integer

' This is used so not only the mouse-over-stat describes whether stuff should be
' moved/resized (The mouse moves faster than the screen draws, so it causes a 
' lot of ruckus if this doesn't exist and holds the state of whether the mouse
' button is pressed or not)
Dim Shared btnpressed As Byte = 0

' This stuff is required for holding the variables of the mouse when it started,
' while the square is moving.
Dim Shared mouseyoffset As Integer
Dim Shared mousexoffset As Integer
' This is required so it holds the thing it's supposed to do. (Move/Resize)
' Eitherwise if you move the mouse too fast from the resizing box to the moving 
' one, while holding the mouse, it starts moving it instead of resizing it.
Dim Shared nocheck As Byte

' This function checks whether the mouse is over those four coordinates and
' returns either 1 for OVER and 0 for NOT OVER.
Function mouseOver(x , y , tox, toy) As Byte
    If (mousex >= x) And (mousex <= tox) And (mousey >= y) And (mousey <= toy) Then
        Return 1
    Else
        Return 0
    End If
End Function

' This draws the green rectangle and the filled ones inside and on the corner, 
' depending on  the state.
' 0 = Mose not over the resize/move cube
' 1 = Mouse over the moving cube
' 2 = Mouse over the resizing cube
Sub drawCube (state)
    Line (offsetx, offsety)-(offsetx+sizex, offsety+sizey), 10, b
    Select Case state
    Case 1
        Line (offsetx+10, offsety+10)-(offsetx+(sizex-10), offsety+(sizey-10)), 23, bf
        Line (offsetx+(sizex-5), offsety+(sizey-5))-(offsetx+(sizex+5), offsety+(sizey+5)), 20, bf
    Case 2
        Line (offsetx+10, offsety+10)-(offsetx+(sizex-10), offsety+(sizey-10)), 20, bf
        Line (offsetx+(sizex-5), offsety+(sizey-5))-(offsetx+(sizex+5), offsety+(sizey+5)), 23, bf
    Case Else
        Line (offsetx+10, offsety+10)-(offsetx+(sizex-10), offsety+(sizey-10)), 20, bf
        Line (offsetx+(sizex-5), offsety+(sizey-5))-(offsetx+(sizex+5), offsety+(sizey+5)), 20, bf
    End Select
End Sub

' This sub moves the cube according to the mouse starting position relative to
' the cube
Sub moveCube()
    offsetx = mousex - mousexoffset
    offsety = mousey - mouseyoffset
End Sub

' This sub resize the cube after the mouse on the bottom corner of the cube
Sub resizeCube()
    sizex = mousex - offsetx
    sizey = mousey - offsety
End Sub

' Lol, draws info. (Was intended for debugging, but wasn't required)
Sub drawInfo()
    Locate 2, 2
    Print "Press q to quit."
End Sub

Screen 18 ' 640x480
While Inkey <> "q"
    Cls
    Getmouse mousex, mousey,,mousebtns
    ' Check whether the mouse is over the the moving cube
    If(mouseOver(offsetx+10, offsety+10, offsetx+(sizex-10), offsety+(sizey-10))) = 1 Then
        drawCube(1)
        ' If the nocheck variable has been set, we haven't started moving
        If nocheck = 0 Then
            If mousebtns And 1 Then
                btnpressed = 1
                ' Sets the starting offset when moving the cube (At least I think this is required)
                mousexoffset = mousex - offsetx
                mouseyoffset = mousey - offsety
            End If
            ' Set that we have checked that the mousebtn is pressed. Keep the offsets
            nocheck = 1
        End If
        ' Check whether the mouse is over the little corner cube
    Elseif(mouseOver(offsetx+(sizex-6), offsety+(sizey-6),offsetx+(sizex+6), offsety+(sizey+6))) Then
        drawCube(2)
        ' Same as up there If this weren't here and you run the program and started
        ' resizing and quickly moved the button to the big moving cube it'd start
        ' moving instead of resizing. Annoying.
        If nocheck = 0 Then
            If mousebtns And 1 Then
                btnpressed = 2
            End If
            nocheck = 1
        End If
    Else 
        drawCube(0)
    End If

    ' If the buttons still are pressed, keep on doing your job.
    If (mousebtns And 1) And (btnpressed = 2) Then
        resizeCube()
    Elseif (mousebtns And 1) And (btnpressed = 1) Then
        moveCube()
        ' Otherwise stop doing it and let it start from anew. Reset everything.
    Else
        btnpressed = 0
        nocheck = 0
    End If

    ' Draw info /debugging info
    drawInfo()

    ' Wait a little while ( This is supposed to reduce flicker )
    Sleep 1
Wend

End
last edited on 1219850846|%e %b %Y, %H:%M %Z|agohover by Gosha + show more
unfold Re: Resize a Square Using the Mouse by GoshaGosha, 1219346112|%e %b %Y, %H:%M %Z|agohover
Re: Resize a Square Using the Mouse
hartnellhartnell 1219644921|%e %b %Y, %H:%M %Z|agohover

I can move it around but not resize it. Somehow it seems I'm in error, what am I missing? :)

Stay Frosty
— hartnell

unfold Re: Resize a Square Using the Mouse by hartnellhartnell, 1219644921|%e %b %Y, %H:%M %Z|agohover
Re: Resize a Square Using the Mouse
GoshaGosha 1219850176|%e %b %Y, %H:%M %Z|agohover

It's not a bug, It's a feature!

anyway. I just saw the offsets doesn't get updated or something (I fixed that in the code that's above now.).
But, have you tried to start with resizing it? .. It's that little box in the corner.
If it doesn't work it's maybe because I'm using something new in my version of FreeBASIC.. you could try downloading the last one.

To compile older code just add "-lang deprecated" to the compiler options.

last edited on 1219850471|%e %b %Y, %H:%M %Z|agohover by Gosha + show more
unfold Re: Resize a Square Using the Mouse by GoshaGosha, 1219850176|%e %b %Y, %H:%M %Z|agohover
Re: Resize a Square Using the Mouse
hartnellhartnell 1219904790|%e %b %Y, %H:%M %Z|agohover

It works! :)

It's strangely cool to use FBIde again.

— hartnell

unfold Re: Resize a Square Using the Mouse by hartnellhartnell, 1219904790|%e %b %Y, %H:%M %Z|agohover
Re: Resize a Square Using the Mouse
FuzzyFuzzy 1250788569|%e %b %Y, %H:%M %Z|agohover

Does this count?? (PlayBasic)

Do
    Cls 0
        Box 0, 0, MouseX(), MouseY(), 1
    Sync
Loop
unfold Re: Resize a Square Using the Mouse by FuzzyFuzzy, 1250788569|%e %b %Y, %H:%M %Z|agohover
Re: Resize a Square Using the Mouse
FuzzyFuzzy 1250789528|%e %b %Y, %H:%M %Z|agohover

Here is a bit more advanced one (although still basic!).

x# = getScreenWidth() / 2
y# = getScreenHeight() / 2

Box x#, y#, rnd(getScreenWidth()), rnd(getScreenHeight()), 1
Sync

Do
    If enterKey() = True
        Cls 0
            Box x#, y#, MouseX(), MouseY(), 1
        Sync
    EndIf
Loop
unfold Re: Resize a Square Using the Mouse by FuzzyFuzzy, 1250789528|%e %b %Y, %H:%M %Z|agohover
Re: Resize a Square Using the Mouse
commander of gamescommander of games 1250798116|%e %b %Y, %H:%M %Z|agohover

Do you use a sprite?

last edited on 1250798143|%e %b %Y, %H:%M %Z|agohover by commander of games + show more
unfold Re: Resize a Square Using the Mouse by commander of gamescommander of games, 1250798116|%e %b %Y, %H:%M %Z|agohover
Re: Resize a Square Using the Mouse
FuzzyFuzzy 1250851890|%e %b %Y, %H:%M %Z|agohover

I didn't but mine hasn't passed yet. Don't think you need to as says resize box not sprite.

unfold Re: Resize a Square Using the Mouse by FuzzyFuzzy, 1250851890|%e %b %Y, %H:%M %Z|agohover
Re: Resize a Square Using the Mouse
kake_fiskkake_fisk 1252798038|%e %b %Y, %H:%M %Z|agohover

C++ with SDL version. :)

SDL_Event event;
SDL_Rect square;
square.x = 1;
square.y = 1;
square.w = event.button.x - square.x;c
square.h = event.button.y - square.y;
 
while (isRunning)
{
    while (SDL_PollEvent(&event))
    {
        if (event.type == SDL_MOUSEMOTION)
        {
            square.w = event.button.x - square.x;
            square.h = event.button.y - square.y;
        }
    }
 
SDL_FillRect(screen, &square, SDL_MapRGB(screen->format, 0x0, 0x0, 0x0));
}
unfold Re: Resize a Square Using the Mouse by kake_fiskkake_fisk, 1252798038|%e %b %Y, %H:%M %Z|agohover
new post