B4A Library [Code Module] HotSpots

stevel05

Expert
Licensed User
[Code Module] Screen HotSpots

I was playing with my favorite new toy ArraySort, when I came up with this idea for a hotspot manager. It's really simple and only requires a Code Module, but I've seen some discussion around it on the forum so I thought I'd share it in case.

It doesn't require any libraries and is petty self explanatory.

You need to Add the existing module 'HotSpotLib' from the attached project which is a code module. Create a panel with a touch sub.

You then just need to create one or more HotSpots with:

B4X:
HotSpotLib.AddHotSpot(10,10,60,70,"Tag1")
Then Call the find routine from within the Panel_Touch sub:

B4X:
Sub Panel1_Touch (Action As Int, X As Float, Y As Float)

   If Action = Activity.ACTION_DOWN Then
      Dim HS As HotSpotType = HotSpotLib.FindHotSpot(X,Y)
      'If Hotspot is not found an uninitialized HotSpotType is returned.
      If HS.IsInitialized Then
         Log("X "&X&" Y "&Y&" "&HS.Tag)
      End If
   End If
End Sub
I haven't tested it with too many hotspots, that comes next so if you get any problems let me know and I'll try to sort it.

There are no dependencies on additional Librarys.

If there are any suggestions for improvements, I'm only too happy to learn. Some of the search code may be a bit clunky, I don't use that too often.

For my own use, I will compile it to a library. But it's easier to share the source code this way and you can compile it if you want to.

V1.1 Added HotCircle
V1.2 Added FindHotSpotFirst,FindHotSpotLast, FindHotSpotAll
V1.3 Added Method IsInitialized
V1.4 Added Methods GetRectangle and ClearHotSpots
Removed DIP conversion from module, it should be specified in calls as required.
V1.5 bug Fix see post 14 I'm afraid this is a breaking fix and the module now needs to be initialized. HotSpotLib.Initialize

V1.6 Added Polygons
- draw routines
- fixed bugs and ommisions
 

Attachments

Last edited:

stevel05

Expert
Licensed User
I've just been integrating it into the app I wrote it for and it seems to be handling 200+ hotspots without a problem and pretty quickly.

I'm not sure it'll be up to game speed, but maybe I'll find out one day.
 

grant1842

Active Member
Licensed User
Hot Spot

I have a need to use an image stretched out to a Panel. (Image= 100x and 100y).

<<<< Problem>>>>
How to scale from smaller phone(320 x 480) to a bigger phone (600 x 960) and have the hot spot be in the area it needs to be so when clicked on it will respond accordingly.
 

stevel05

Expert
Licensed User
If you set the hotspots using percentages, they should fall into the correct relative positions when the image is sized.

Otherwise you'll need to scale the hotspots comparing the original image size and aspect ratio to the displayed size and aspect ratio.
 

stevel05

Expert
Licensed User
There is a description in section 5 of the beginners guide that describes Scaling if you want to have a look.
 

laviniut

Active Member
Licensed User
But if i have a panel with a few buttons, how can i know which button rise the touch event ? and i need too a doubleclick method for the buttons.
 

stevel05

Expert
Licensed User
You'd use the hot spots instead of buttons, you could draw anything you like under an invisible panel as a guide and handle the touch / slide and double click within the touch subroutine. You could use a timer to implement the double click.
 

laviniut

Active Member
Licensed User
is a good idea, but i am a beginner in programming. are you kindly to give me an example with a few buttons and a click timer ?
 

stevel05

Expert
Licensed User
Have a look at this one. It should do what you want. I have commented it but if you need to know anything else just ask. The images may be a little on the small size, I haven't scaled them. You can make them any size you like.
 

Attachments

laviniut

Active Member
Licensed User
Is working fine, but when i start a new activity at doubleclick action and goes back to main activity, timers is not ok (it rises doubleclick action on singleclick). i think some code for timers must be in Activity_Resume.
 

stevel05

Expert
Licensed User
Thanks, laviniut, you found a bug in the underlying module. the attached is rectified, I needed to add an initialize method to the module as it was storing the hotspots twice. This only occurred if you used the back key and activity create was called again.

Silly mistake, but thanks for finding it.

Just swap the new module for the module in your project, and add SLHotSpotLib.Initialize to the Activity_Create Sub.

You can rename the module using Project Change Module Name if you wish.

I'll change the released version shortly, or anyone else can take it from this example if you need it.
 

Attachments

stevel05

Expert
Licensed User
Upgrade to V1.6 adds polygons, drawroutines bug and ommission fixes, see the class release for more details (excluding customview elements). New module is attached to post 1 of this thread.
 

laviniut

Active Member
Licensed User
stevel05, your code is very useful for me. I 'push' on hotspots and speak, doubleclick... ok.
but now i'm in a trouble. I want to use hotspots to speak and doubleclick together with a long list of contacts from phone.
I don't know how to make it to work. If i put the list with contacts in a listview and listview is in front, i can scroll the list, but hotspots is not working, or if i put panel with hotspots in front then hotspots is working but i cannot scroll the list. The code i am referring is in AlegeContact activity.
Can you help me? If you want, i attached the code i am working.
 

Attachments

stevel05

Expert
Licensed User
stevel05, your code is very useful for me. I 'push' on hotspots and speak, doubleclick... ok.
but now i'm in a trouble. I want to use hotspots to speak and doubleclick together with a long list of contacts from phone.
I don't know how to make it to work. If i put the list with contacts in a listview and listview is in front, i can scroll the list, but hotspots is not working, or if i put panel with hotspots in front then hotspots is working but i cannot scroll the list. The code i am referring is in AlegeContact activity.
Can you help me? If you want, i attached the code i am working.
I will certainly have a look, but I'm afraid it won't be until Monday. I am going to be about very little this weekend. I'll let you know how I get on on Monday.
 

stevel05

Expert
Licensed User
@laviniut

Try the attached file, I have changed the touch routine to use reflection which allows you to return true or false, returning false (which is default) should pass the touch event back through and allow scrolling, returning true will consume the event.

Hope it helps.
 

Attachments

laviniut

Active Member
Licensed User
stevel05, you are great. thank you. now i can scroll, but how i can get from the list the item i touched. because i want to speak the item i touched in the list at singleclick and at doubleclick i want to call that contact. now i get only 1 click when i click or doubleclick in the list.
 
Last edited:
Top