All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.bloodshura.ignitium.ntv.keyboard.KeyboardMapping Maven / Gradle / Ivy

The newest version!
package com.github.bloodshura.ignitium.ntv.keyboard;

import com.github.bloodshura.ignitium.activity.logging.XLogger;
import com.github.bloodshura.ignitium.collection.map.XMap;
import com.github.bloodshura.ignitium.collection.map.impl.XLinkedMap;
import com.github.bloodshura.ignitium.collection.tuple.Pair;
import com.github.bloodshura.ignitium.collection.util.NumericCollections;
import com.github.bloodshura.ignitium.input.Key;
import com.github.bloodshura.ignitium.math.BaseConverter;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class KeyboardMapping {
	private boolean debug;
	private final XMap mapping;

	public KeyboardMapping() {
		this.mapping = new XLinkedMap<>();
	}

	public synchronized int idOf(@Nonnull Key key) {
		Integer id = mapping.get(key);

		if (id == null && shouldDebug()) {
			XLogger.warnln("Key not mapped: " + key.getDisplayName() + '.');

			return -1;
		}

		return id;
	}

	@Nonnull
	public synchronized int[] idsOf(@Nonnull Key key) {
		return NumericCollections.toIntArray(mapping.select(entry -> entry.getLeft() == key).map(Pair::getRight));
	}

	@Nullable
	public synchronized Key keyOf(int id) {
		Key key = mapping.getKey(id);

		if (key == null && shouldDebug()) {
			XLogger.warnln("Key code not mapped: 0x" + BaseConverter.encode(id, BaseConverter.HEXADECIMAL) + '.');
		}

		return key;
	}

	public synchronized void register(@Nonnull Key key, @Nonnull int... ids) {
		for (int id : ids) {
			mapping.add(key, id);
		}
	}

	public void setDebug(boolean debug) {
		this.debug = debug;
	}

	public boolean shouldDebug() {
		return debug;
	}

	public synchronized void unregister(@Nonnull Key key) {
		while (mapping.containsKey(key)) {
			mapping.removeKey(key);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy