<commit_before>﻿#pragma once
#include "Input.h"
#include <iostream>
#include <memory>
#include "Logger.h"
#include "LightHandle.h"
#include "imgui/imgui-SFML.h"


Input::Input() :
	keyboard_flags(sf::Keyboard::Key::KeyCount, false),
	mouse_flags(sf::Mouse::Button::ButtonCount, false)
{

}

Input::~Input()
{

}

void Input::consume_sf_events(sf::RenderWindow *window) {

	std::list<sf::Event> sf_event_queue;

	sf::Event e;
	while (window->pollEvent(e)) {
		sf_event_queue.push_back(e);
		ImGui::SFML::ProcessEvent(e);
	}

	transpose_sf_events(sf_event_queue);

	sf_event_queue.clear();

}

void Input::consume_vr_events() {

}

void Input::handle_held_keys() {

	// When keys and buttons are pressed, add them to the held list.
	// When they are depressed, remove them

	for (auto&& event: event_queue) {

		// Key
		if (event->type == vr::Event::KeyPressed) {
			vr::KeyPressed *e = static_cast<vr::KeyPressed*>(event.get());
			held_keys.push_back(e->code);
		}
		else if (event->type == vr::Event::KeyReleased) {
			vr::KeyReleased *e = static_cast<vr::KeyReleased*>(event.get());
			held_keys.erase(std::remove(held_keys.begin(), held_keys.end(), e->code), held_keys.end());
		}

		// Mouse Button
		else if (event->type == vr::Event::MouseButtonPressed) {
			vr::MouseButtonPressed *e = static_cast<vr::MouseButtonPressed*>(event.get());
			held_mouse_buttons.push_back(e->button);
		}
		else if (event->type == vr::Event::MouseButtonReleased) {
			vr::MouseButtonReleased *e = static_cast<vr::MouseButtonReleased*>(event.get());
			held_mouse_buttons.erase(std::remove(held_mouse_buttons.begin(), held_mouse_buttons.end(), e->button), held_mouse_buttons.end());
		}
	}

	// Generate Held events for each of the held buttons and keys

	for (auto key : held_keys) {

		// Not sure if this is a good idea, but I'm going to grab
		// the real-time values of the mod keys and add them to the event

		bool alt = false;
		if (sf::Keyboard::isKeyPressed(sf::Keyboard::LAlt) || sf::Keyboard::isKeyPressed(sf::Keyboard::RAlt)) {
			alt = true;
		}
		
		bool control = false;
		if (sf::Keyboard::isKeyPressed(sf::Keyboard::LControl) || sf::Keyboard::isKeyPressed(sf::Keyboard::RControl)) {
			control = true;
		}

		bool shift = false;
		if (sf::Keyboard::isKeyPressed(sf::Keyboard::LShift) || sf::Keyboard::isKeyPressed(sf::Keyboard::RShift)) {
			shift = true;
		}

		bool system = false;
		if (sf::Keyboard::isKeyPressed(sf::Keyboard::LSystem) || sf::Keyboard::isKeyPressed(sf::Keyboard::RSystem)) {
			system = true;
		}
		
		event_queue.emplace_back(std::make_unique<vr::KeyHeld>(vr::KeyHeld(key, alt, control, shift, system)));
	}


	for (auto mouse_button : held_mouse_buttons) {
		
		// Again, I'm going to poll the real-time status of this event
		// to fill in the X and Y values. I can do this either with screen
		// co-ords or with viewport co-ords. I don't have access to the window
		// from here so for now I'm going to do screen co-ords

		sf::Vector2i mouse_pos = sf::Mouse::getPosition();

		event_queue.emplace_back(std::make_unique<vr::MouseButtonHeld>(vr::MouseButtonHeld(mouse_button, mouse_pos.x, mouse_pos.y)));
	}

}

void Input::dispatch_events() {

	while (event_queue.size() != 0) {
		notify_subscribers(std::move(event_queue.front()));
		event_queue.pop_front();
	}

}


void Input::render_gui() {

	ImGui::Begin("Input Debugger");

	ImDrawList* draw_list = ImGui::GetWindowDrawList();
	static ImVec4 col = ImVec4(1.0f, 0.0f, 1.0f, 1.0f);
	const ImVec2 p = ImGui::GetCursorScreenPos();
	const ImU32 col32 = ImColor(col);

	std::vector<float> axis_values = {
		sf::Joystick::getAxisPosition(0, sf::Joystick::Axis::X) / 2,
		sf::Joystick::getAxisPosition(0, sf::Joystick::Axis::Y) / 2,
		sf::Joystick::getAxisPosition(0, sf::Joystick::Axis::U) / 2,
		sf::Joystick::getAxisPosition(0, sf::Joystick::Axis::R) / 2,
		sf::Joystick::getAxisPosition(0, sf::Joystick::Axis::Z) / 2,
		sf::Joystick::getAxisPosition(0, sf::Joystick::Axis::V) / 2
	};

	ImGui::Columns(3, "Axis's"); // 4-ways, with border
	ImGui::Separator();
	ImGui::Text("X Y"); ImGui::NextColumn();
	ImGui::Text("U R"); ImGui::NextColumn();
	ImGui::Text("Z V"); ImGui::NextColumn();
	ImGui::Separator();

	for (int i = 0; i < 3; i++) {


		float offset = ImGui::GetColumnWidth(i);

		draw_list->AddLine(ImVec2(p.x + 0 + offset * i, p.y + 50), ImVec2(p.x + 100 + offset * i, p.y + 50), col32, 1.0);
		draw_list->AddLine(ImVec2(p.x + 50 + offset * i, p.y + 0), ImVec2(p.x + 50 + offset * i, p.y + 100), col32, 1.0);
		draw_list->AddCircleFilled(ImVec2(p.x + axis_values[2 * i] + 50 + offset * i, p.y + axis_values[2 * i + 1] + 50), 6, col32, 32);

		ImGui::Dummy(ImVec2(100, 100));
		ImGui::NextColumn();
	}

	ImGui::Text("Pressed Keyboard Keys");
	
	ImGui::Separator();
	
	ImGui::Columns(6);

	for (auto i : held_keys) {
		ImGui::Text(key_strings.at(i).c_str());
		ImGui::NextColumn();
	}

	ImGui::Separator();

	ImGui::End();

}


void Input::update_gui() {
	rendering = true;
}<commit_msg>It's the year 2017. Why the hell does VS not autosave?<commit_after>﻿#pragma once
#include "Input.h"
#include <iostream>
#include <memory>
#include "Logger.h"
#include "LightHandle.h"
#include "imgui/imgui-SFML.h"


Input::Input() :
	keyboard_flags(sf::Keyboard::Key::KeyCount, false),
	mouse_flags(sf::Mouse::Button::ButtonCount, false)
{

}

Input::~Input()
{

}

void Input::consume_sf_events(sf::RenderWindow *window) {

	std::list<sf::Event> sf_event_queue;

	sf::Event e;
	while (window->pollEvent(e)) {
		sf_event_queue.push_back(e);
		ImGui::SFML::ProcessEvent(e);
	}

	transpose_sf_events(sf_event_queue);

	sf_event_queue.clear();

}

void Input::consume_vr_events() {

}

void Input::handle_held_keys() {

	// When keys and buttons are pressed, add them to the held list.
	// When they are depressed, remove them

	for (auto&& event: event_queue) {

		// Key
		if (event->type == vr::Event::KeyPressed) {
			vr::KeyPressed *e = static_cast<vr::KeyPressed*>(event.get());
			held_keys.push_back(e->code);
		}
		else if (event->type == vr::Event::KeyReleased) {
			vr::KeyReleased *e = static_cast<vr::KeyReleased*>(event.get());
			held_keys.erase(std::remove(held_keys.begin(), held_keys.end(), e->code), held_keys.end());
		}

		// Mouse Button
		else if (event->type == vr::Event::MouseButtonPressed) {
			vr::MouseButtonPressed *e = static_cast<vr::MouseButtonPressed*>(event.get());
			held_mouse_buttons.push_back(e->button);
		}
		else if (event->type == vr::Event::MouseButtonReleased) {
			vr::MouseButtonReleased *e = static_cast<vr::MouseButtonReleased*>(event.get());
			held_mouse_buttons.erase(std::remove(held_mouse_buttons.begin(), held_mouse_buttons.end(), e->button), held_mouse_buttons.end());
		}
	}

	// Generate Held events for each of the held buttons and keys

	for (auto key : held_keys) {

		// Not sure if this is a good idea, but I'm going to grab
		// the real-time values of the mod keys and add them to the event

		bool alt = false;
		if (sf::Keyboard::isKeyPressed(sf::Keyboard::LAlt) || sf::Keyboard::isKeyPressed(sf::Keyboard::RAlt)) {
			alt = true;
		}
		
		bool control = false;
		if (sf::Keyboard::isKeyPressed(sf::Keyboard::LControl) || sf::Keyboard::isKeyPressed(sf::Keyboard::RControl)) {
			control = true;
		}

		bool shift = false;
		if (sf::Keyboard::isKeyPressed(sf::Keyboard::LShift) || sf::Keyboard::isKeyPressed(sf::Keyboard::RShift)) {
			shift = true;
		}

		bool system = false;
		if (sf::Keyboard::isKeyPressed(sf::Keyboard::LSystem) || sf::Keyboard::isKeyPressed(sf::Keyboard::RSystem)) {
			system = true;
		}
		
		event_queue.emplace_back(std::make_unique<vr::KeyHeld>(vr::KeyHeld(key, alt, control, shift, system)));
	}


	for (auto mouse_button : held_mouse_buttons) {
		
		// Again, I'm going to poll the real-time status of this event
		// to fill in the X and Y values. I can do this either with screen
		// co-ords or with viewport co-ords. I don't have access to the window
		// from here so for now I'm going to do screen co-ords

		sf::Vector2i mouse_pos = sf::Mouse::getPosition();

		event_queue.emplace_back(std::make_unique<vr::MouseButtonHeld>(vr::MouseButtonHeld(mouse_button, mouse_pos.x, mouse_pos.y)));
	}

}

void Input::dispatch_events() {

	while (event_queue.size() != 0) {
		notify_subscribers(std::move(event_queue.front()));
		event_queue.pop_front();
	}

}


void Input::render_gui() {

	ImGui::Begin("Input Debugger");

	ImDrawList* draw_list = ImGui::GetWindowDrawList();
	static ImVec4 col = ImVec4(1.0f, 0.0f, 1.0f, 1.0f);
	const ImVec2 p = ImGui::GetCursorScreenPos();
	const ImU32 col32 = ImColor(col);

	std::vector<float> axis_values = {
		sf::Joystick::getAxisPosition(0, sf::Joystick::Axis::X) / 2,
		sf::Joystick::getAxisPosition(0, sf::Joystick::Axis::Y) / 2,
		sf::Joystick::getAxisPosition(0, sf::Joystick::Axis::U) / 2,
		sf::Joystick::getAxisPosition(0, sf::Joystick::Axis::R) / 2,
		sf::Joystick::getAxisPosition(0, sf::Joystick::Axis::Z) / 2,
		sf::Joystick::getAxisPosition(0, sf::Joystick::Axis::V) / 2
	};

	ImGui::Columns(3, "Axis's"); // 4-ways, with border
	ImGui::Separator();
	ImGui::Text("X Y"); ImGui::NextColumn();
	ImGui::Text("U R"); ImGui::NextColumn();
	ImGui::Text("Z V"); ImGui::NextColumn();
	ImGui::Separator();

	for (int i = 0; i < 3; i++) {


		float offset = ImGui::GetColumnWidth(i);

		draw_list->AddLine(ImVec2(p.x + 0 + offset * i, p.y + 50), ImVec2(p.x + 100 + offset * i, p.y + 50), col32, 1.0);
		draw_list->AddLine(ImVec2(p.x + 50 + offset * i, p.y + 0), ImVec2(p.x + 50 + offset * i, p.y + 100), col32, 1.0);
		draw_list->AddCircleFilled(ImVec2(p.x + axis_values[2 * i] + 50 + offset * i, p.y + axis_values[2 * i + 1] + 50), 6, col32, 32);

		ImGui::Dummy(ImVec2(100, 100));
		ImGui::NextColumn();
	}

	ImGui::Separator();

	ImGui::Text("Pressed Keyboard Keys");
	
	ImGui::Columns(6);

	for (auto i : held_keys) {
		ImGui::Text(key_strings.at(i).c_str());
		ImGui::NextColumn();
	}

	ImGui::Separator();

	ImGui::End();

}


void Input::update_gui() {
	rendering = true;
}
{'fraction_non_alphanumeric': 0.1802802802802803, 'fraction_numerical': 0.016816816816816817, 'mean_word_length': 4.9434860202260555, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1012527', 'n_tokens_mistral': 4049, 'n_tokens_neox': 3736, 'n_words': 756}