~kiwec/ke

ke/src/Input.cc -rw-r--r-- 749 bytes
1cc8481d — Wolf Clément Fix compilation 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <termbox.h>
#include "Input.h"

InputHandler* Input::m_head = nullptr;

void Input::add_handler(InputHandler* handler) {
	handler->m_next = m_head;
	m_head = handler;
}

void Input::remove_handler(InputHandler* handler) {
	if(m_head == handler) {
		m_head = handler->m_next;
	} else {
		// Walk the list of handlers to remove handler
		InputHandler* h = m_head;
		for(; h->m_next != handler; h = h->m_next);
		h->m_next = h->m_next->m_next;
	}
}

void Input::handle(tb_event* ev, bool is_mouse) {
	// Call handlers until one successfully handles,
	// starting from the latest added
	for(auto h = m_head; h != nullptr; h = h->m_next) {
		if(is_mouse) {
			if(h->handle_mouse(ev)) return;
		} else {
			if(h->handle_key(ev)) return;
		}
	}
}