How to optimize this code?

Discuss any general programming issues here
Post Reply
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

How to optimize this code?

Post by Bugala »

I am in process of making my own buttons system, however, as I was testing it today, I noticed it is too slow. This is the current version of the actual handler code (wont work independent):

Code: Select all

Function s_Buttons:HandlerFunc(msg)   /* handlerfunc doesnt take into account donothing option yet*/
	msg.action = LowerStr(msg.action)
	Local mousexpos = MouseX()
	Local mouseypos = MouseY()
	
	ForEach(self.buttonslist, Function (ID, button)
			  If mousexpos > button.x And mousexpos < button.x + button.width And mouseypos > button.y And mouseypos < button.y + button.height And button.status <> msg.action
				If msg.action = "onmousedown" Or msg.action = "onmouseup" Or msg.action = "onrightmousedown" Or msg.action = "onrightmouseup"
				Or msg.action = "onwheeldown" Or msg.action = "onwheelup" Or msg.action = "onmidmousedown" Or msg.action = "onmidmouseup"
					button.status = msg.action
					If HaveItem(button.evttable, "changeto"..msg.action) Then button.evttable["changeto"..msg.action](button.userdata)
				Else
					button.status = "onmouseover"
					If HaveItem(button.evttable, "changetoonmouseover") Then button.evttable["changetoonmouseover"](button.userdata)
				EndIf
			  ElseIf button.status <> "onmouseout" And button.status <> "idle"
				If HaveItem(button.evttable, "changetoonmouseout") = True Then button.evttable["changetoonmouseout"](button.userdata)
				If HaveItem(button.evttable, "changetoonmouseout") = True Or HaveItem(button.evttable, "onmouseover") = True Or HaveItem(button, "GFX") And HaveItem(button.gfx, "onmouseover")
					button.status = "onmouseout"
				Else
					buttons.status = "idle"
				EndIf
			  ElseIf button.status = "onmouseout"
				button.status = "idle"
				If HaveItem(button.evttable, "changetoidle") = True Then button.evttable["changetoidle"](button.userdata)
			  EndIf
			  If HaveItem(button.evttable, button.status) = True Then button.evttable[buttons.status](button.userdata)
				  EndFunction
		)
EndFunction
If I have only one button, it works fine, but in my test I was having only 5 buttons yet, and it was simply too slow. For example, I had buttons that changed their appearance when mouse was over them, and then when moved out of button (OnMouseOut basically), then they would change their appearance again.

With only 5 buttons on screen, i was already able to move mouse quickly out of button, and if i stopped moving mouse at that point, button would stay as "OnMouseOver" state, since it didnt wake up next "OnMouseMove" event as execution of previous one took so long.

So any tips on optimizing my code? Like is RawGet faster than HaveItem or some other similar ideas? Or do I have to go back to using the official #SIMPLEBUTTONS?
Post Reply