New Theme API NumPad UI7 Color Selector Color 2 Hex UI7 New Draw API (DrawCmd) UI7 New Debug API (DrawCmd) Add ThemeEditor Base
138 lines
4.3 KiB
C++
138 lines
4.3 KiB
C++
/**
|
|
* This file is part of RenderD7
|
|
* Copyright (C) 2021-2024 NPI-D7, tobid7
|
|
*
|
|
* This program 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.
|
|
*
|
|
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include <map>
|
|
#include <renderd7/Hid.hpp>
|
|
|
|
namespace RenderD7 {
|
|
class HidApi {
|
|
public:
|
|
HidApi() {}
|
|
~HidApi() {}
|
|
|
|
void setKdown(uint32_t &in) { actions[Hid::Down] = ∈ }
|
|
void setKheld(uint32_t &in) { actions[Hid::Held] = ∈ }
|
|
void setKup(uint32_t &in) { actions[Hid::Up] = ∈ }
|
|
void setKrepeat(uint32_t &in) { actions[Hid::DownRepeat] = ∈ }
|
|
void setTouchCoords(R7Vec2 &touch_coords) { touch_pos = &touch_coords; }
|
|
void setJS1Movement(R7Vec2 &mvmt) { js1_mv = &mvmt; }
|
|
void setJS2Movement(R7Vec2 &mvmt) { js2_mv = &mvmt; }
|
|
void bindKey(const std::string &event, uint32_t key) {
|
|
key_bindings[event] = key; // Overrides if existing
|
|
}
|
|
void lock(bool lock) { locked = lock; }
|
|
|
|
void clear() {
|
|
// Clears Functionality for 1 Frame
|
|
last_touch_pos = R7Vec2();
|
|
touch_pos[0] = R7Vec2();
|
|
dtp = R7Vec2();
|
|
backups[Hid::Down] = 0;
|
|
backups[Hid::Held] = 0;
|
|
backups[Hid::Up] = 0;
|
|
backups[Hid::DownRepeat] = 0;
|
|
}
|
|
|
|
bool isEvent(const std::string &event, Hid::Actions action) {
|
|
if (locked) return false;
|
|
if (key_bindings.find(event) == key_bindings.end())
|
|
return false; // Unknown Event
|
|
if (backups.find(action) == backups.end())
|
|
return false; // What? NOT Alowed acrion
|
|
if (backups[action] & key_bindings[event])
|
|
return true; // Action contains key as flag
|
|
return false; // Nothing to do
|
|
}
|
|
|
|
R7Vec2 getTouchPos() { return touch_pos[0]; }
|
|
R7Vec2 getLastTouchPos() { return last_touch_pos; }
|
|
R7Vec2 getTouchDownPos() { return dtp; }
|
|
|
|
void update() {
|
|
last_touch_pos = touch_pos[0];
|
|
if (isEvent("touch", Hid::Down)) {
|
|
dtp = touch_pos[0];
|
|
}
|
|
if (isEvent("touch", Hid::Up)) {
|
|
dtp = R7Vec2();
|
|
}
|
|
for (const auto &it : actions) {
|
|
backups[it.first] = it.second[0];
|
|
}
|
|
if (locked) {
|
|
actions[Hid::Down][0] = 0;
|
|
actions[Hid::Held][0] = 0;
|
|
actions[Hid::Up][0] = 0;
|
|
actions[Hid::DownRepeat][0] = 0;
|
|
}
|
|
}
|
|
|
|
private:
|
|
std::map<Hid::Actions, uint32_t *> actions;
|
|
std::map<Hid::Actions, uint32_t> backups;
|
|
R7Vec2 *touch_pos = nullptr;
|
|
R7Vec2 *js1_mv = nullptr;
|
|
R7Vec2 *js2_mv = nullptr;
|
|
|
|
R7Vec2 last_touch_pos;
|
|
R7Vec2 dtp;
|
|
|
|
std::map<std::string, uint32_t> key_bindings;
|
|
bool locked = false;
|
|
};
|
|
static HidApi hid_handler;
|
|
namespace Hid {
|
|
// Register Functions
|
|
// Register Current state values
|
|
void RegKeyDown(uint32_t &key_down) { hid_handler.setKdown(key_down); }
|
|
|
|
void RegKeyHeld(uint32_t &key_held) { hid_handler.setKheld(key_held); }
|
|
|
|
void RegKeyUp(uint32_t &key_up) { hid_handler.setKup(key_up); }
|
|
|
|
void RegKeyRepeat(uint32_t &repeat) { hid_handler.setKrepeat(repeat); }
|
|
|
|
void RegTouchCoords(R7Vec2 &touch_pos) {
|
|
hid_handler.setTouchCoords(touch_pos);
|
|
}
|
|
|
|
void RegAnalog1Movement(R7Vec2 &movement) {
|
|
hid_handler.setJS1Movement(movement);
|
|
}
|
|
|
|
void RegAnalog2Movement(R7Vec2 &movement) {
|
|
hid_handler.setJS2Movement(movement);
|
|
}
|
|
|
|
// Register Keys
|
|
void RegKeyEvent(const std::string &event, uint32_t key) {
|
|
hid_handler.bindKey(event, key);
|
|
}
|
|
|
|
bool IsEvent(const std::string &event, Actions action) {
|
|
return hid_handler.isEvent(event, action);
|
|
}
|
|
R7Vec2 GetTouchPosition() { return hid_handler.getTouchPos(); }
|
|
R7Vec2 GetLastTouchPosition() { return hid_handler.getLastTouchPos(); }
|
|
R7Vec2 GetTouchDownPosition() { return hid_handler.getTouchDownPos(); }
|
|
void Update() { hid_handler.update(); }
|
|
void Lock() { hid_handler.lock(true); }
|
|
void Unlock() { hid_handler.lock(false); }
|
|
void Clear() { hid_handler.clear(); }
|
|
} // namespace Hid
|
|
} // namespace RenderD7
|