/****************************************************************************************** * Chili Direct3D Engine * * Copyright 2018 PlanetChili * * * * This file is part of Chili Direct3D Engine. * * * * Chili Direct3D Engine is free software: you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation, either version 3 of the License, or * * (at your option) any later version. * * * * The Chili Direct3D Engine is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with The Chili Direct3D Engine. If not, see . * ******************************************************************************************/ #include "Keyboard.h" bool Keyboard::KeyIsPressed( unsigned char keycode ) const noexcept { return keystates[keycode]; } Keyboard::Event Keyboard::ReadKey() noexcept { if( keybuffer.size() > 0u ) { Keyboard::Event e = keybuffer.front(); keybuffer.pop(); return e; } else { return Keyboard::Event(); } } bool Keyboard::KeyIsEmpty() const noexcept { return keybuffer.empty(); } char Keyboard::ReadChar() noexcept { if( charbuffer.size() > 0u ) { unsigned char charcode = charbuffer.front(); charbuffer.pop(); return charcode; } else { return 0; } } bool Keyboard::CharIsEmpty() const noexcept { return charbuffer.empty(); } void Keyboard::FlushKey() noexcept { keybuffer = std::queue(); } void Keyboard::FlushChar() noexcept { charbuffer = std::queue(); } void Keyboard::Flush() noexcept { FlushKey(); FlushChar(); } void Keyboard::EnableAutorepeat() noexcept { autorepeatEnabled = true; } void Keyboard::DisableAutorepeat() noexcept { autorepeatEnabled = false; } bool Keyboard::AutorepeatIsEnabled() const noexcept { return autorepeatEnabled; } void Keyboard::OnKeyPressed( unsigned char keycode ) noexcept { keystates[keycode] = true; keybuffer.push( Keyboard::Event( Keyboard::Event::Type::Press,keycode ) ); TrimBuffer( keybuffer ); } void Keyboard::OnKeyReleased( unsigned char keycode ) noexcept { keystates[keycode] = false; keybuffer.push( Keyboard::Event( Keyboard::Event::Type::Release,keycode ) ); TrimBuffer( keybuffer ); } void Keyboard::OnChar( char character ) noexcept { charbuffer.push( character ); TrimBuffer( charbuffer ); } void Keyboard::ClearState() noexcept { keystates.reset(); } template void Keyboard::TrimBuffer( std::queue& buffer ) noexcept { while( buffer.size() > bufferSize ) { buffer.pop(); } }