AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

strange results when attempting a Gui Drag event...

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
Veovis



Joined: 13 Feb 2006
Posts: 389
Location: Utah

PostPosted: Wed Mar 15, 2006 5:35 am    Post subject: strange results when attempting a Gui Drag event... Reply with quote

Dont be discourage by how long this message is. I explain my problem very clearly (Personally its kind of annoying to help someone when they give very little detail, but sorry in advance if this is to much info.)

I am currently working on a game called Enigma



Quote:
You start with 5 dice, which are rolled repeatedly. Each roll, the player arranges the dice in to 2 pairs a single. The pairs are added to the lower table and the single is added to the upper table. Only 3 different numbers can be selected for the upper (single) table. Every roll you must place 1 dice in the upper (single) table. (if no dice is any of those numbers you 'throw away' any dice you want) The game ends when you place the 8th of a number into the single table. For the doubles: if you have used a number (8 for example) 0 times, you get no points, if you have used if 1 to 4 times you get -20 points, and past that you get more points based on rarity of that num (7 is worth little, 2 and 12 are most) So you want to use as few numbers as possible and as often as possible.


Hope that makes sense...

Anyways, in beta programming I am at the following status:

I launch the program, click Roll Dice, and then WOULD drag and drop the dice to pair them, but cant get that to work at all...So as a workaround i placed buttons next to all the rows in the tables to add to the rows. (to test the scoring code)

Heres the problem:
When i pick up the picture it pickes up everything next to it for the drag, AND loses its Background transparency for the drag. But when i set the dice down it behaves normally, at least until i pick it up again. And on top of that it doesnt go where the mouse is, but instead it goes down below it. ARGH!!
I cant make heads or tails for why any of this would happen...

What i will eventually do is make some code that will test to see if you dropped a dice onto another one and pair them by setting them next to each other.

What i might end up doing is having a different pic (an outline of the dice) that is used for dragging purposes.

Anyways, my main problems are:
1-Why does the background transparency die when i drag? (Bug in AHK or my code?)
2-why does the drag pick up other stuff? (Bug in AHK or my code?)
3-Why does the drag not stay on the mouse? (bug in my code for sure)


Heres a Zip with the script and pics: sigma.zip

Heres the code:
(its very ugly, being in beta, way to many unclear varaibles...)
Code:
;===========================
;------Initialization-------
;===========================
#singleinstance force

;-= create menus
Menu, FileMenu, add, &New`tCtrl-N, MenuFileNew
Menu, FileMenu, add, &Quit`tAlt-F4, MenuFileQuit
Menu, HelpMenu, add, &Rules`tCtrl-H, MenuHelpRules
Menu, HelpMenu, add, &About, MenuHelpAbout
Menu, MenuBar, add, &File, :FileMenu
Menu, MenuBar, add, &Help, :HelpMenu
Gui, Menu, MenuBar


;-=draw single buttons
gui, margin, 10,5
gui, add, button, section x10 y10 gAddSingle vSingle1, ?
gui, add, button, gAddSingle vSingle2, ?
gui, add, button, gAddSingle Vsingle3, ?

;-=draw single bars
gui, margin, 3,13
gui, add, pic, ys+6 ,singleback.png
gui, add, pic, yp xp w0 h15 vsingle1bar,blue.bmp
gui, add, pic, w0 h15 vsingle2bar,blue.bmp
gui, add, pic, w0 h15 vsingle3bar,blue.bmp

;-=draw 5 dice
gui, margin, 25, 20
gui, add, pic, xm vDice1Pic BackgroundTrans gDiceClick section, dice1.png
gui, add, pic, ys vDice2Pic BackgroundTrans gDiceClick, dice2.png
gui, add, pic, ys vDice3Pic BackgroundTrans gDiceClick, dice3.png
gui, add, pic, ys vDice4Pic BackgroundTrans gDiceClick, dice4.png
gui, add, pic, ys vDice5Pic BackgroundTrans gDiceClick, dice5.png

;-=draw double buttons
gui, margin, 0,0
gui, add, button, x10 yp+100 section gAddDouble, 2
gui, add, button, gAddDouble, 3
gui, add, button, gAddDouble, 4
gui, add, button, gAddDouble, 5
gui, add, button, gAddDouble, 6
gui, add, button, gAddDouble, 7
gui, add, button, gAddDouble, 8
gui, add, button, gAddDouble, 9
gui, add, button, gAddDouble, 10
gui, add, button, gAddDouble, 11   
gui, add, button, gAddDouble, 12

;-=draw double bars
gui, margin, 10,13
gui, add, pic, ys+6 section BackgroundTrans,back.png
gui, add, pic, ys xs w450 h10 vbar2,red.bmp
gui, add, pic, w0 h10 vbar3,red.bmp
gui, add, pic, w0 h10 vbar4,red.bmp
gui, add, pic, w0 h10 vbar5,red.bmp
gui, add, pic, w0 h10 vbar6,red.bmp
gui, add, pic, w0 h10 vbar7,red.bmp
gui, add, pic, w0 h10 vbar8,red.bmp
gui, add, pic, w0 h10 vbar9,red.bmp
gui, add, pic, w0 h10 vbar10,red.bmp
gui, add, pic, w0 h10 vbar11,red.bmp
gui, add, pic, w0 h10 vbar12,red.bmp


;-=draw score, reset button, roll button,
gui, font, s20
gui, add, text, vScore xs+435 ys+105, -999
guicontrol, ,Score,0

gui, add, button, x365 y20 w180 h60 gRollDice vButtonRollDice, Roll Dice
gui, font, s12

;-=set initial states of double bars to -5
loop, 11
{
   loopindex := A_index+1
   double%loopindex%val = -5   
   guicontrol,move,bar%loopindex%, w0
}
guicontrolget, doublebar,pos,bar2
loop, 5
{
   guicontrolget, currentpos, pos, Dice%A_index%Pic
   Dice%A_index%StandardX = %currentposX%
   Dice%A_index%StandardY = %currentposY%

}
gui, show
Gosub, RollDice
return

;=============================
;-------Games Controls--------
;=============================
MenuFileNew:
   loop, 11
   {
      loopindex := A_index+1
      double%loopindex%val = -5
      RedrawDoubleBar(loopindex)
   }
   loop, 3
   {
      single%A_index%val = 0
      temp = single%A_index%
      RedrawSingleBar(temp)
   }
   
   guicontrol, ,score,% TotalScores()
return

RollDice:
   Gosub, ResetDice
   loop, 5
   {
      random, random, 1, 6
      dice%A_index%val := random
      guicontrol, , dice%A_index%,dice%random%.png
   }
   ;guicontrol, disable, ButtonRollDice
return

ResetDice:
loop, 5
{
   currentx := dice%A_index%standardx
   currenty := dice%A_index%standardy
   guicontrol, move, dice%A_index%pic, x%currentx% y%currenty%
}
return

DiceClick:
   stringmid, dicenum, A_guicontrol, 5,1
   diceval := % dice%dicenum%val
   loop
   {
      ;if mod(A_index, 5) = 0
      ;   guicontrol, ,%A_guicontrol%, dice%diceval%.png
      GetKeyState, mouseisdown, LButton
      if mouseisdown = u
         break
      mousegetpos, mousex, mousey
      guicontrol, move, %A_guicontrol%, x%mousex% y%mousey% BackgroundTrans
      sleep 100
   }
   guicontrol, ,%A_guicontrol%, dice%diceval%.png
   guicontrol, move, %A_guicontrol%, x%mousex% y%mousey% BackgroundTrans   
return


TotalScores()
{
   total = 0
   loop, 12
   {
      if A_index = 1
         continue
      x := double%A_index%val
      if (x = -5) or (x = 0)
         continue
      else if x > 0
         if (A_index = 2) or (A_index = 12)
            Total += x*10
         else
            total += x*(abs(7-A_index)+3)
      else
         total -= 20
   }
   return %total%
}
;=============================
;-----Control Single Bars-----
;=============================
AddSingle:
   %A_guicontrol%val += 1
   RedrawSingleBar(A_guicontrol)
return

SubtractSingle:
return

RedrawSingleBar(bar)
{
   newbarW := %bar%val * 40
   guicontrol, move, %bar%bar, w%newbarW%
   if %bar%val = 8
      guicontrol, ,%bar%bar, red.bmp
   else
      guicontrol,,%bar%bar, blue.bmp
}

;=============================
;-----Control Double Bars-----
;=============================
AddDouble:
   double%a_guicontrol%val += 1
   RedrawDoubleBar(A_guicontrol)
   guicontrol, ,score,% TotalScores()
return

SubtractDouble:
return

RedrawDoubleBar(bar)
{
   global doublebarx
   if double%bar%val < 0
   {
      newbarX := doublebarX
      newbarW := 100 -(double%bar%val * -20)
      guicontrol, ,bar%bar%,red.bmp
   }else if double%bar%val = 0
   {
      newbarX := doublebarX+80
      newbarW = 20
      guicontrol, ,bar%bar%,blue.bmp
   }else if (double%bar%val > 0) and (double%bar%val < 10)
   {
      newbarX := doublebarX +100
      newbarW := double%bar%val*(abs(7-bar)+3)*4
      if (bar = 2) or (bar = 12)
         newbarW := double%bar%val*10*4
   }else if double%bar%val >= 10
   {
      double%bar%val = 10
      newbarX := doublebarX +100
      newbarW := double%bar%val*(abs(7-bar)+3)*4
      if (bar = 2) or (bar = 12)
         newbarW := double%bar%val*10*4
   }
   guicontrolget, currentbar,pos,bar%bar%
   if currentbarW <> newbarW
      guicontrol, move, bar%bar%, w%newbarW% x%newbarX%
}


;=============================
;-------Other Functions-------
;=============================
MenuFileQuit:
GuiClose:
ExitApp

MenuHelpRules:
msgbox, <welcome to beta, this will be made later>
return

MenuHelpAbout:
msgbox, <welcome to beta, this will be made later>
/*
;=============================
;----------TO DO List---------
;=============================

dice:
   - drag and drop pairs
   - drag drop single dice
   - code to unpair dice
   - code to add and SUBTRACT to tables when you pair (or unpair) the dice

rules:
   - make game end when you hit the 8th single

add RULES and HELP sections
preferences?



High scores:
 80 - Geneuis
 50 - excellent
 30 - good
  0 - beginner
-25 - try again
-50 - noob!

_________________

"Power can be given overnight, but responsibility must be taught. Long years go into its making."
Back to top
View user's profile Send private message Send e-mail Visit poster's website
PhiLho



Joined: 27 Dec 2005
Posts: 6786
Location: France (near Paris)

PostPosted: Wed Mar 15, 2006 3:30 pm    Post subject: Reply with quote

First, I find your program interesting and nice looking. I have to read more attentively the rules to understand them...
I suppose that "to 2 pairs a single" means "to 2 pairs and a single".

About the offset of the dices when dragged:
MouseGetPos retreive the coordinates relative to the current window, the (0, 0) coordinate being the top-left of the window (title bar).
GuiControl Move coordinates are relative to the GUI window's client area, ie. below the borders, title bar and the menu bar...
I don't know if AutoHotkey has a builtin mean to convert coordinates or compute the offset of the client area relative to the window top-left.
One way to acheive this is using DllCall of the ScreenToClient API function: set mouse coordinates in screen mode, get coords, convert them.
I started to experiment here, I will edit this post when I get more results.
You also have to compute the click coordinate inside the control, something like:
GuiControlGet dicePos, %A_GuiControl%
offsetX := dicePosX - initX
offsetY := dicePosY - initY
mousex -= offsetX
mousey -= offsetY
guicontrol, move, %A_guicontrol%, x%mousex% y%mousey%

More to come...
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
Veovis



Joined: 13 Feb 2006
Posts: 389
Location: Utah

PostPosted: Wed Mar 15, 2006 10:54 pm    Post subject: Reply with quote

Thank you PhiLho

I figured out that the other problems were due to me using gui, move and not gui, movedraw

I see the logic in your mouse offset, ill try it when i get home, thanks!
_________________

"Power can be given overnight, but responsibility must be taught. Long years go into its making."
Back to top
View user's profile Send private message Send e-mail Visit poster's website
PhiLho



Joined: 27 Dec 2005
Posts: 6786
Location: France (near Paris)

PostPosted: Thu Mar 16, 2006 3:57 pm    Post subject: Reply with quote

Slightly more complex than I wanted, but manageable:
Code:
; @struct: hold the UInt to write
; _value: value to write
; _offset: offset of the UInt from the start of the struct, in UInt size units
SetUInt(ByRef @struct, _value, _offset=0)
{
   local addr
   addr := &@struct + _offset * 4
   DllCall("RtlFillMemory", "UInt", addr,     "UInt", 1, "UChar", (_value & 0x000000FF))
   DllCall("RtlFillMemory", "UInt", addr + 1, "UInt", 1, "UChar", (_value & 0x0000FF00) >> 8)
   DllCall("RtlFillMemory", "UInt", addr + 2, "UInt", 1, "UChar", (_value & 0x00FF0000) >> 16)
   DllCall("RtlFillMemory", "UInt", addr + 3, "UInt", 1, "UChar", (_value & 0xFF000000) >> 24)
}

; @struct: hold the UInt to extract
; _offset: offset of the UInt from the start of the struct, in UInt size units
GetUInt(ByRef @struct, _offset=0)
{
   local addr
   addr := &@struct + _offset * 4
   Return *addr + (*(addr + 1) << 8) +  (*(addr + 2) << 16) + (*(addr + 3) << 24)
}

DragControl(_controlName)
{
   local hWnd, point
   local initScrX, initScrY
   local initWinX, initWinY
   local initCliX, initCliY
   local offsetX, offsetY
   local mouseState, mouseX, mouseY
;~    local dicePos ; must NOT be declared, strange...

   ; Init.: compute the offsets
   ; Screen coordinates of the mouse
   CoordMode Mouse, Screen
   MouseGetPos initScrX, initScrY, hWnd
   ; Window relative coordinates of the mouse
   CoordMode Mouse, Relative   ; Restore default
   MouseGetPos initWinX, initWinY
   ; Compute client area relative coordinates of the mouse
   VarSetCapacity(point, 8)
   SetUInt(point, initScrX)
   SetUInt(point, initScrY, 1)
   DllCall("ScreenToClient", "UInt", hWnd, "UInt", &point)
   initCliX := GetUInt(point)
   initCliY := GetUInt(point, 1)
   ; Coordinates of the control, relative to the client area
   GuiControlGet dicePos, Pos, %_controlName%
   ; Compute offset between click inside control and top-left corner of control
   offsetX :=  initCliX - dicePosX
   offsetY :=  initCliY - dicePosY
   ; Add offset between window and client area
   offsetX += initWinX - initCliX
   offsetY += initWinY - initCliY

   Loop
   {
      GetKeyState mouseState, LButton
      If (mouseState = "u")
         Break   ; Mouse button is released
      ; Window relative coordinates of the mouse
      MouseGetPos mouseX, mouseY
      ; Corrected to client relative coordinates
      mouseX -= offsetX
      mouseY -= offsetY
      GuiControl MoveDraw, %_controlName%, x%mouseX% y%mouseY%
      Sleep 100
   }
   GuiControl Move, %_controlName%, x%mouseX% y%mouseY%
}

DiceClick:
   DragControl(A_GuiControl)
Return

_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")


Last edited by PhiLho on Wed Mar 29, 2006 2:48 pm; edited 4 times in total
Back to top
View user's profile Send private message Visit poster's website
Veovis



Joined: 13 Feb 2006
Posts: 389
Location: Utah

PostPosted: Fri Mar 17, 2006 2:08 am    Post subject: Reply with quote

@PhiLho

Thankyou! Thankyou! Thankyou! Thankyou! Thankyou! Thankyou!
Thankyou! Thankyou! Thankyou! Thankyou! Thankyou! Thankyou!
Thankyou! Thankyou! Thankyou! Thankyou! Thankyou! Thankyou!
Thankyou! Thankyou! Thankyou! Thankyou! Thankyou! Thankyou!
Thankyou! Thankyou! Thankyou! Thankyou! Thankyou! Thankyou!
Thankyou! Thankyou! Thankyou! Thankyou! Thankyou! Thankyou!
Thankyou! Thankyou! Thankyou! Thankyou! Thankyou! Thankyou!

That works like a charm! I just changed the one line from gui, move to gui, movedraw and everything works perfectly!

Now i need to figure out the code to pair the dice into doubles and a single...
_________________

"Power can be given overnight, but responsibility must be taught. Long years go into its making."
Back to top
View user's profile Send private message Send e-mail Visit poster's website
PhiLho



Joined: 27 Dec 2005
Posts: 6786
Location: France (near Paris)

PostPosted: Fri Mar 17, 2006 3:04 pm    Post subject: Reply with quote

Really? That's strange, there was a bug where I changed the offset in SetUInt/GetUInt from UInt size to byte, but not in the call...
I went back to UInt size unit, and put the drag in a function, so it is reusable.
The post above have been updated.
Another update: I mixed up + and << precedence levels... I wonder how it worked...
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")


Last edited by PhiLho on Wed Mar 29, 2006 9:55 pm; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
garry



Joined: 19 Apr 2005
Posts: 1612
Location: switzerland

PostPosted: Wed Mar 29, 2006 7:13 pm    Post subject: Reply with quote

build your own paper ENIGMA:
http://mckoss.com/Crypto/Enigma.htm
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group