Taking what I've learned, and applying it to MicroMacro
Posted: Thu Jun 04, 2015 9:27 pm
So, now that I've got a job working on a pretty serious programming project, I'm getting more experience with proper programming techniques. I'm quite fond of some of the things I'm learning, and over time I've been copying what I like into MicroMacro. For example, the Timestamp library class was (loosely) based on PHP's Carbon timestamp class, mixed with the event chaining in Eloquent.
Today, I decided to apply what I've learned to helping with event handling, since that seems to be quite an annoyance for some. The methodology here is to create an event handler object, bind that to the dispatcher, and let everything else happen automatically. OK, that sounds more complicated than it needs to be, so lets just get to an example.
First, lets create our event handler object:
You'll see we inherit from the base class EventHandler, and copy our event datas via the constructor. The handle() function is what gets called when the event is triggered. Pretty straight forward. So, onto main.lua, where we set it all up:
You'll see that in macro.init(), we bind our event (keypressed) to our TestHandler object. Of course we could bind multiple events (but only one handler to each event), but just 'keypressed' is good enough for a test. In macro.event(), we pass the event data on to the dispatcher so that it can create the event handler for us and trigger the event. As such, whenever we press a key, TestHandler:handle() is called, and self.vk contains the virtual key we pressed, and self.toggle contains the key's toggle state.
So, why pass the arguments to the constructor instead of directly to handle()? That's a good question. I'm not entirely sure I have a great answer for you. However, it would allow for more abstraction, as we can create another child of our TestHandler with altered attributes that would then retain all the functionality of TestHandler without having to repeat code. It also means that we can pass around the object containing all the variables attached to it without any extra work. Finally, it separates the code better such that the constructor() takes care of all the setup (maybe there's something special we want to do with the arguments, before actually handling it?) and handle() just does the processing.
Anyways, while it normally isn't "hard" to handle the events, I think something along these lines could make things even easier and much more clean. I'm mostly just experimenting and seeing what and what doesn't. I just thought I'd share. Maybe it might inspire you guys on your own things, or maybe you've got some valuable input.
Today, I decided to apply what I've learned to helping with event handling, since that seems to be quite an annoyance for some. The methodology here is to create an event handler object, bind that to the dispatcher, and let everything else happen automatically. OK, that sounds more complicated than it needs to be, so lets just get to an example.
First, lets create our event handler object:
Code: Select all
TestHandler = class.new(EventHandler);
function TestHandler:constructor(vk, toggle)
self.vk = vk;
self.toggle = toggle;
end
function TestHandler:handle()
printf("Event fired. Key: %s, Toggle: %s\n", self.vk, self.toggle);
end
Code: Select all
require('event/dispatcher');
include("testhandler.lua");
function macro.init(script, ...)
EventDispatcher:bindHandler('keypressed', TestHandler);
end
function macro.main(dt)
return true;
end
function macro.event(e, ...)
EventDispatcher:handle(e, ...);
end
So, why pass the arguments to the constructor instead of directly to handle()? That's a good question. I'm not entirely sure I have a great answer for you. However, it would allow for more abstraction, as we can create another child of our TestHandler with altered attributes that would then retain all the functionality of TestHandler without having to repeat code. It also means that we can pass around the object containing all the variables attached to it without any extra work. Finally, it separates the code better such that the constructor() takes care of all the setup (maybe there's something special we want to do with the arguments, before actually handling it?) and handle() just does the processing.
Anyways, while it normally isn't "hard" to handle the events, I think something along these lines could make things even easier and much more clean. I'm mostly just experimenting and seeing what and what doesn't. I just thought I'd share. Maybe it might inspire you guys on your own things, or maybe you've got some valuable input.