Jason's Development Blog

Tag: event

Java Event Maps

by on Oct.18, 2010, under Code, Java, Programming

I openly admit to not being fond of coding in Java. It’s just not got the elegance C# has. However, I will use Java if only to remind myself why I don’t like using it.

I recently did just this. Largely so I could see whether the event queue system I made in C++ could easily port to Java and C#. Guess what? It does. This pleases me.

Here’s a code example of how to use the event queue:

package eventtesting;

import JpmEvents.CEventMap;
import JpmEvents.IEvent;
import JpmEvents.IEventHandler;
import JpmEvents.BEvent;

/**
 *
 * @author Jason
 */
public class Main
{
    private class TestEvent1 extends BEvent { public TestEvent1() { super("TestEvent1"); } }
    private class TestEvent2 implements IEvent
    {
        public Integer Hash() { return Name().hashCode(); }
        public String Name() { return "TestEvent2" }
        public TestEvent2() {  }
    }

    private boolean OnEvent(TestEvent1 e)
    {
        System.out.println("Event One Called!!!");
        return false;
    }
    private boolean OnEvent(TestEvent2 e)
    {
        System.out.println("Event Two Called!!!");
        return false;
    }

    protected void Run()
    {
        CEventMap emMap = new CEventMap();

        emMap.Bind(new IEventHandler<TestEvent1>(){public boolean HandleEvent(TestEvent1 event){return OnEvent(event);}}, "TestEvent1");
        emMap.Bind(new IEventHandler<TestEvent2>(){public boolean HandleEvent(TestEvent2 event){return OnEvent(event);}}, "TestEvent2");

        System.out.println("Triggering events...");
        emMap.Trigger(new TestEvent2());

        System.out.println("Queueing events...");
        emMap.Queue(new TestEvent1());
        emMap.Queue(new TestEvent2());
        emMap.Queue(new TestEvent1());

        System.out.println("Flushing events...");
        emMap.Flush();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        Main m = new Main();
        m.Run();
    }
}

And here is the link to the Project Kenai page I’m making the library and it’s source available on, under the MIT Licence

.

Quote of the Day
“I account for morality as an accidental capability produced,
in its boundless stupidity, by a biological process that is normally
opposed to the expression of such a capability”
George C. Williams (May 12, 1926 – September 8, 2010)

Leave a Comment :, , , , , , more...

Refactoring

by on Oct.16, 2010, under Life, Technology

“I don’t like how I did this at all! TO THE REDESIGN ENTIRE SYSTEM FROM GROUND UP MOBILE!!!”

So I basically had the realisation that my for Project Pauper design was modular so would be better fit into multiple Dynamically Linked Libraries than one Statically Linked Library. Unfortunately, my event system relied on RTTI, which in C++ under Windows can’t be relied upon across DLLs. At all. Ever.

So I decided to pull the event system and replace it with a new one. And one thing led to another and suddenly I had decided I wanted to refactor half the code. Joy.

Zip and date old Pauper, move it to backup directory and begin working on new pauper. Most of the start up is just getting what worked in the old to work in the new, copy and paste or rewrite as I go.

Whilst I was at it I decided Window and Graphics Device should not be one item, but separate. This design is one I dropped awhile ago, but fsuck old me, new me says it comes back. I have finally figured a good way to keep window logic in it’s own thread though, via a thread-safe event queue sitting between the window’s events and the events Pauper has to respond to. It just feels cleaner for some reason.

Leave a Comment :, , , , , , , more...

Adverts