Omnimaga

Calculator Community => TI Calculators => Axe => Topic started by: Munchor on December 04, 2010, 07:23:56 pm

Title: Mouse
Post by: Munchor on December 04, 2010, 07:23:56 pm
In a thingie I'm making, I need a mouse feature.

I'm using Qwerty55's code (thanks!):

Code: [Select]
:.This is the sprite
:[80C0A09088D0A818]→Pic1
:While 1
:.Store the screen to the back buffer
:StorePic
:.Display Sprite
:Pt-On(X,Y,Pic1
:DispGraph
:.Erase Sprite
:Pt-Change(X,Y,Pic1
:.Replace the part of the image erased by the Sprite
:RecallPic
:.I used conditionals because the boundary conditions are important
:.Replace with X+getKey(3)-getKey(2)→X if they aren't important.
:If getKey(3) and (X<92)
:X+1→X
:End
:If getKey(55) and (X<72)
:Pt-On(X,Y,Pic1+8
:End
:If getKey(2) and (X>2)
:X-1→X
:End
:If getKey(1) and (Y<60)
:Y+1→Y
:End
:If getKey(4) and (Y>2)
:Y-1→Y
:End
:End

Code: [Select]
.MOUSE
[101010FE10101000]->Pic1
While 1
.Store the screen to the back buffer
StorePic
.Display Sprite
Pt-On(X,Y,Pic1
DispGraph
.Erase Sprite
Pt-Change(X,Y,Pic1
.Replace the part of the image erased by the Sprite
RecallPic
.I used conditionals because the boundary conditions are important
.Replace with X+getKey(3)-getKey(2)->X if they aren't important.
Repeat getKey->Z
End
If Z=15
Goto 9
Else
If getKey(3) and (X<92)
X+1->X
End
If getKey(55) and (X<72)
Pt-On(X,Y,Pic1+8
End
If getKey(2) and (X>2)
X-1->X
End
If getKey(1) and (Y<60)
Y+1->Y
End
If getKey(4) and (Y>2)
Y-1->Y
End
End
End
Lbl 9

The one which is below was edited by me and is really really really slow. The above one is perfect and works really fine!

Any idea why? I know why (Else and GetKey) but how to fix? THX!
Title: Re: Mouse
Post by: AngelFish on December 04, 2010, 07:43:41 pm
It's the getkey->Z. Storing to a number and comparing the number takes forever. Try replacing every conditional with getkey(keycode).
Title: Re: Mouse
Post by: Runer112 on December 04, 2010, 07:43:53 pm
You said it yourself, the problem is the Repeat getKey→Z. Having that in the code will not let program execution advance until that getKey returns a value. When holding down the arrow keys, this will only return a value a few times per second, resulting in the mouse moving code only being reached a few times per second.

I see that you added it so you could press clear to jump out of it, so why not just use direct key input (e.g. getKey(15)) for the clear button as well as the arrow keys?
Title: Re: Mouse
Post by: DJ Omnimaga on December 04, 2010, 07:46:54 pm
Repeat Getkey is only for turn-based stuff where you want key detection to be done immediately. I think it's pretty much useless in Axe because it runs so fast. In BASIC it can be useful in games like Reuben Quest where otherwise key detection would be horrible, but not in Axe.

I would also use direct input (Getkey with a value in parenthesis afterward), like Runer112 said. Much faster and responsive. Regular getkey is more for menu selections.
Title: Re: Mouse
Post by: Happybobjr on December 04, 2010, 08:11:31 pm
I see some potential relatively major optimizations. which code will you be using?
Title: Re: Mouse
Post by: Munchor on December 05, 2010, 10:31:29 am
Code: [Select]
.MOUSE
[101010FE10101000]->Pic1
While 1
.Store the screen to the back buffer
StorePic
.Display Sprite
Pt-On(X,Y,Pic1
DispGraph
.Erase Sprite
Pt-Change(X,Y,Pic1
.Replace the part of the image erased by the Sprite
RecallPic
.I used conditionals because the boundary conditions are important
.Replace with X+getKey(3)-getKey(2)→X if they aren't important.
If getKey(3) and (X<92)
X+1→X
End
If getKey(55) and (X<72)
Pt-On(X,Y,Pic1+8
End
If getKey(2) and (X>2)
X-1→X
End
If getKey(1) and (Y<60)
Y+1→Y
End
If getKey(4) and (Y>2)
Y-1→Y
End
getKey(15)
End

I have tried many things, many getKey(15) if different places of the code, with no success :S
Title: Re: Mouse
Post by: Runer112 on December 05, 2010, 10:39:38 am
getKey(15) will equal 1 if the clear key is being held down and 0 if it isn't. That's all it does: return a value. If you want it to actually do something useful based on whether or not it (or any other key) is held down, you have to put it as the condition in an if statement and then put what you want it to do as the contents of the if statement, just like the other 5 if statements already in the code.
Title: Re: Mouse
Post by: Munchor on December 05, 2010, 10:40:35 am
getKey(15) will equal 1 if the clear key is being held down and 0 if it isn't. That's all it does. If you want it to actually do something useful, you have to put it as the condition in an if statement and then put what you want it to do as the contents of the if statement.

I also tried:

Code: [Select]
If getKey(15)
Goto 9
End
.The next line is the last one
Lbl 9
Title: Re: Mouse
Post by: Runer112 on December 05, 2010, 10:42:04 am
That should work, as long as you insert it into a suitable place in the code. Where did you try putting it?
Title: Re: Mouse
Post by: Happybobjr on December 05, 2010, 10:46:07 am
Code: [Select]
If getKey(3) and (X<92)
X+1→X
End
If getKey(2) and (X>2)
X-1→X
End
If getKey(1) and (Y<60)
Y+1→Y
End
If getKey(4) and (Y>2)
Y-1→Y
End

could be

Code: [Select]
X<92 * getKey(3) -  (X>2 * getKey(2)) + X -> X

Y<60 * getKey(1) -  (Y>2 * getKey(4)) + Y -> Y

I think this gives a speed increase and lowers the byte size.
It should also make the game speed slightly more stable, although this is so small that that wouldn't be noticed here.




Edit: are you tring to have clear end the game?

then you should have

:If Getkey(15)
:Fix 4
:Return
:End
Title: Re: Mouse
Post by: calc84maniac on December 05, 2010, 10:59:55 am
Code: [Select]
If getKey(3) and (X<92)
X+1→X
End
If getKey(2) and (X>2)
X-1→X
End
If getKey(1) and (Y<60)
Y+1→Y
End
If getKey(4) and (Y>2)
Y-1→Y
End

could be

Code: [Select]
X<92 * getKey(3) -  (X>2 * getKey(2)) + X -> X

Y<60 * getKey(1) -  (Y>2 * getKey(4)) + Y -> Y

I think this gives a speed increase and lowers the byte size.
It should also make the game speed slightly more stable, although this is so small that that wouldn't be noticed here.
I don't think so. Multiplication by a non-constant is definitely one of the slowest things you can do.

Edit:
You could replace the multiplications with and in this case, because you are operating on two values that are 0 or 1.
Title: Re: Mouse
Post by: Happybobjr on December 05, 2010, 11:07:15 am
hum, that means i will be editing my code. thanks.
Title: Re: Mouse
Post by: Munchor on December 05, 2010, 12:49:16 pm
Code: [Select]
.MOUSE
[101010FE10101000]->Pic1
While 1
.Store the screen to the back buffer
StorePic
.Display Sprite
Pt-On(X,Y,Pic1
DispGraph
.Erase Sprite
Pt-Change(X,Y,Pic1
.Replace the part of the image erased by the Sprite
RecallPic
.I used conditionals because the boundary conditions are important
.Replace with X+getKey(3)-getKey(2)→X if they aren't important.
If getKey(3) and (X<92)
X+1→X
End
If getKey(55) and (X<72)
Pt-On(X,Y,Pic1+8
End
If getKey(2) and (X>2)
X-1→X
End
If getKey(1) and (Y<60)
Y+1→Y
End
If getKey(4) and (Y>2)
Y-1→Y
End
If getKey(15)
Fix 4
Return
End
End

This works, why?

I have an idea of how to make mouse events now :)
Title: Re: Mouse
Post by: Happybobjr on December 05, 2010, 01:23:17 pm
return, while not part of a subroutine quits the program.

I always have fix 4 because i always use fix 5.
Title: Re: Mouse
Post by: Munchor on December 05, 2010, 01:24:09 pm
return, while not part of a subroutine quits the program.

I always have fix 4 because i always use fix 5.

I know what Fix 5 does (concetane images and text), but not Fix 4
Title: Re: Mouse
Post by: Happybobjr on December 05, 2010, 01:25:34 pm
fix 4 does the opposite of fix 5.

If you use fix 5, you must have fix 4 at the end of your program or else you calc will go bye bye (not die, it just goes funky.)
Title: Re: Mouse
Post by: Munchor on December 05, 2010, 01:27:17 pm
fix 4 does the opposite of fix 5.

If you use fix 5, you must have fix 4 at the end of your program or else you calc will go bye bye (not die, it just goes funky.)

Oh thanks much!!! It's now working, even as an Application.

Code: [Select]
To do: Mouse Events
Title: Re: Mouse
Post by: Happybobjr on December 05, 2010, 01:28:56 pm
need help with mouse events?

Is this anything to do with the secret project?
Title: Re: Mouse
Post by: Munchor on December 05, 2010, 01:33:01 pm
need help with mouse events?

Is this anything to do with the secret project?


Yes it has :P

So, I have X and Y which are the actual coordenates of the mouse.

However, a button has many coordenates, to check if when I press 2nd a button which size is 8*8 located at 10,10 is pressed:
Code: [Select]
// CODE FOR PICTURE 8*8
Pt-Change(10,10,Pic1
DispGrah
If getKey(54)
If X>=10 and X<=20 and Y>=10 AND Y<=20
ClrDraw
Text(0,0,"You pressed the button")

You think that would work?
Title: Re: Mouse
Post by: Happybobjr on December 05, 2010, 01:40:49 pm
i would have to slight for statements. (for statements that the variable is only used for these staements, so that the variable may be used in other for statements).

such that.


If getkey(54)
box(1)
for(a,0,7)
for(b,0,7)
If ((Button top left coordinants + A)=X) and ("" + B = Y)
do event
end
end
end
other button checks.
end
Title: Re: Mouse
Post by: Munchor on December 05, 2010, 01:50:25 pm
Code: [Select]
[7E8181818181817E]->Pic1
Pt-Change(10,10,Pic1
If getkey(54)
box(1)
for(a,0,7)
for(b,0,7)
If ((Button top left coordinants + A)=X) and ("" + B = Y)
ClrDraw
Text(0,0,"You pressed the button
Pause 9999
End
End
End
. Other button checks.
End

If ((Button top left coordinants + A)=X) and ("" + B = Y)

Have no idea what this means, what is A? And B? And ""?
Title: Re: Mouse
Post by: Happybobjr on December 05, 2010, 01:52:30 pm
"" means the y coordinate of the buttin

A and B are empty variables.
Title: Re: Mouse
Post by: Munchor on December 05, 2010, 01:53:58 pm
Code: [Select]
[7E8181818181817E]->Pic1
Pt-Change(10,10,Pic1
If getkey(54)
box(1)
for(a,0,7)
for(b,0,7)
If ((10 + A)=X) and ((10 + B) = Y)
ClrDraw
Text(0,0,"You pressed the button
Pause 9999
End
End
End
. Other button checks.
End

From my Axe experience I think there are curly braces somewhere here...

If ((10 + A)=X) and ((10 + B) = Y)

This is what you meant?
Title: Re: Mouse
Post by: Happybobjr on December 05, 2010, 01:55:02 pm
yes, if your button doesn't ever move.
Title: Re: Mouse
Post by: Munchor on December 05, 2010, 01:56:33 pm
Great! It's not a very big code, though. I pretend to have 7 buttons each moment and they will move, but I can handle that
Title: Re: Mouse
Post by: Happybobjr on December 05, 2010, 01:59:44 pm
the code could be shortened by allot using  > and < tests instead of for statements. I was just making a very genaric code.
Title: Re: Mouse
Post by: Munchor on December 05, 2010, 02:00:38 pm
Code: [Select]
// CODE FOR PICTURE 8*8
Pt-Change(10,10,Pic1
DispGrah
If getKey(54)
If X>=10 and X<=20 and Y>=10 AND Y<=20
ClrDraw
Text(0,0,"You pressed the button")

Stuff like that right?
Title: Re: Mouse
Post by: Happybobjr on December 05, 2010, 02:02:27 pm
20 should be 19.

and there should be parenthesis. around each test statement.
Title: Re: Mouse
Post by: AngelFish on December 05, 2010, 02:28:00 pm
need help with mouse events?

Is this anything to do with the secret project?


Yes it has :P

So, I have X and Y which are the actual coordenates of the mouse.

However, a button has many coordenates, to check if when I press 2nd a button which size is 8*8 located at 10,10 is pressed:


There's actually a slightly more efficient way of doing it. Let's say that you have a series of buttons arranged vertically, as is common in many GUIs. You could arrange the buttons with a simple algorithm like this:

Code: [Select]
:"WTRSNDMTLSPKOILFIRERS"→Str1
:For(A,0,6
:A*8+5→B
:Text(80,B,3*A+Str1)
:RectI(78,B-1,15,9
:RectI(79,B,13,7
:End

This will display three characters from the string for each button and draw a box around the text. When you go to click on a button, you just have to redo the algorithm with with the Y coordinate.

Basically,
Code: [Select]
:If getKey(54)
:Y-3/8→B
:End

An integer corresponding to the button will now be located in B. The top button on the page will return a 0, the second will return a 1, and so on. This is also easily extended to horizontally arranged buttons.


Title: Re: Mouse
Post by: Munchor on December 05, 2010, 02:30:37 pm
That's what you are doing in Powder Game, right?

It's a very good way, but truly complicated to get there. Nice brain ya got
Title: Re: Mouse
Post by: AngelFish on December 05, 2010, 02:32:41 pm
 
That's what you are doing in Powder Game, right?

It's a very good way, but truly complicated to get there. Nice brain ya got

Yes, it's how I'm doing the menu in Powder. Thanks.
Title: Re: Mouse
Post by: Munchor on December 05, 2010, 02:34:00 pm
Yes, it's how I doing the menu in Powder. Thanks.

If I evert get problems in my current code I'll use that, mine is not any of the above referenced, it's a bad one, 10 lines, but I like it
Title: Re: Mouse
Post by: Runer112 on December 05, 2010, 06:13:10 pm
There's actually a slightly more efficient way of doing it. Let's say that you have a series of buttons arranged vertically, as is common in many GUIs. You could arrange the buttons with a simple algorithm like this:

Code: [Select]
:"WTRSNDMTLSPKOILFIRERS"→Str1
:For(A,0,6
:A*8+5→B
:Text(80,B,3*A+Str1)
:RectI(78,B-1,15,9
:RectI(79,B,13,7
:End

This will display three characters from the string for each button and draw a box around the text. When you go to click on a button, you just have to redo the algorithm with with the Y coordinate.

I love optimizing code. :)

Code: [Select]
:"WTRSNDMTLSPKOILFIRERS"→Str1
:8→A
:While A-1→A
:Text(*8-3→B*256+80)
:Text A*3-3+Str1
:RectI(78,B-1,15,9)
:RectI(79,B,13,7)
:End
Title: Re: Mouse
Post by: AngelFish on December 05, 2010, 06:14:50 pm
Cool, mind if I use that?
Title: Re: Mouse
Post by: Runer112 on December 05, 2010, 06:17:46 pm
Of course, I optimized it so you could use it! (And because I get random impulses to optimize stuff)

EDIT: By the way, I just noticed that -4-Str1 should be -3-Str1. Make sure you got the changed version.

EDIT 2: In actually testing the code, it doesn't work as it should have... let me look into this.

EDIT 3: Found the error and corrected it. I got my argument for the pen movement command backwards so it was trying to draw things at a Y-value of 80. Make sure you get the version that has the line Text(*8-3→B*256+80).

EDIT 4: (Wow this is a lot of edits!) I'm guessing your code does this too, but do you also get the lack of borders between the middle buttons? This would fix it:

Code: [Select]
:"WTRSNDMTLSPKOILFIRERS"→Str1
:8→A+1→C
:While A-1→A
:Text(*8-3→B*256+80)
:Text A*3-3+Str1
:RectI(78,B-1,15,C)
:RectI(79,B,13,8→C-1)
:End
Title: Re: Mouse
Post by: Munchor on December 05, 2010, 06:22:57 pm
Runner, you are one hell of optimizer!
Title: Re: Mouse
Post by: AngelFish on December 05, 2010, 07:08:54 pm
EDIT 4: (Wow this is a lot of edits!) I'm guessing your code does this too, but do you also get the lack of borders between the middle buttons? This would fix it:
<snip>

I don't actually draw the borders in my code during loading. I added that part for Scout to use.
Title: Re: Mouse
Post by: Munchor on December 06, 2010, 08:44:30 am
/me is a helpful person