Coherent UI  2.5.3
A modern user interface library for games
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
Sample - Input in 2D

This tutorial demonstrates forwarding keyboard and mouse input to a Coherent::UI:View when the view is on the whole screen (e.g. HUD).

Note
Supplied code, unrelated to Coherent UI integration, is not representative for a well-designed, high-performance solution. It's written for simplicity and its purpose is to demonstrate integration in an existing game framework.

Building the sample

The sample solution is already configured and you only have to compile and run it.

It uses the sample application framework described in the previous sections (Base Application Framework, Sample Application Framework).

The output will be in the Coherent/Samples/UI/bin directory.

Prerequisites

  1. You should read the previous sections (Base Application Framework, Sample Application Framework) to get yourself familiar with the sample application framework used. Some details of the sample are hidden in the sample application framework for brevity.
  2. This sample build upon Sample - Custom File Handler and assumes you understand it.

Key points

  1. Overriding ApplicationBase::OnKeyEvent, ApplicationBase::OnMouseEvent and ApplicationBase::OnTouchEvent

Sample walkthrough

The changes from Sample - Custom File Handler are minimal.

First, the whole custom file handler code is thrown away, since we won't be using it.

The ViewEventListener and ContextEventListener classes haven't changed (except that the view created now doesn't use the coui:// scheme).

All the new code in this sample is just 3 methods - Application::OnKeyEvent, Application::OnMouseEvent (overridden from ApplicationBase) and Application::ShouldForwardEventToView.

Application::ShouldForwardEventToView just ensures the view is ready to receive events. Application::OnKeyEvent and Application::OnMouseEvent check if the event should be forwarded and just send them to the view.

That's all you have to do to get your input in Coherent UI!

It might seem very easy, but there is a lot of work done behind the scenes by the PlatformWindow class. When it receives a input event (keypress, mouse move, etc.) it has platform specific code that converts native events to Coherent Input events.

Mouse events are usually easy to convert as they are just positions and buttons. Key events, however, usually present trouble since platform specific key codes differ. Coherent UI uses Windows virtual key codes and that's what Coherent::UI::KeyEventData::KeyCode should represent. This means that you should convert the key codes produced by the input subsystem of your game to Windows virtual keys before sending them to Coherent UI.

There is sample code for converting X11 events in the Base Application Framework. You can check the CoherentInputEventsConvertorX11Linux.cpp file for the implementation.

Note
Currently touch events are only handled on Windows and Mac OS X.