sculpto
windows_input.cpp
Go to the documentation of this file.
1/*!****************************************************************//*!*
2 * \file windows_input.cpp
3 * \brief Input window_handle realisation module.
4 *
5 * \author Sabitov Kirill
6 * \date 18 June 2022
7 *********************************************************************/
8
9#include "sclpch.h"
10
11#include <imgui.h>
12
13#include "windows_input.h"
15
16void scl::windows_input_system::MouseInit()
17{
18 // Obtain position
19 POINT pt;
20 GetCursorPos(&pt);
21 ScreenToClient(*WindowHandle, &pt);
22
23 // Absolute values
24 Mouse.PosY = pt.x;
25 Mouse.PosX = pt.y;
26}
27
28void scl::windows_input_system::MouseRead()
29{
30 // Obtain position
31 POINT pt;
32 GetCursorPos(&pt);
33 ScreenToClient(*WindowHandle, &pt);
34
35 // Delta (relative to previus readed) values
36 Mouse.PosDeltaX = pt.x - Mouse.PosX;
37 Mouse.PosDeltaY = pt.y - Mouse.PosY;
38
39 // Absolute values
40 Mouse.PosX = pt.x;
41 Mouse.PosY = pt.y;
42
43 // Wheel (z) value
44 if (*Wheel != 0)
45 {
46 Mouse.PosDeltaZ = *Wheel * (!ImGui::GetIO().WantCaptureMouse);
47 Mouse.PosZ += *Wheel;
48 *Wheel = 0;
49 }
50 else Mouse.PosDeltaZ = 0;
51}
52
53void scl::windows_input_system::KeyboardInit()
54{
55 if (GetKeyboardState((PBYTE)Keyboard.Keys))
56 memcpy(Keyboard.KeysOld, Keyboard.Keys, 256);
57}
58
59void scl::windows_input_system::KeyboardRead()
60{
61 memcpy(Keyboard.KeysOld, Keyboard.Keys, 256);
62 bool _ = GetKeyboardState((PBYTE)Keyboard.Keys);
63
64 ImGuiIO io = ImGui::GetIO();
65 if (io.WantCaptureMouse)
66 Keyboard.Keys[(int)keycode::LBUTTON] =
67 Keyboard.Keys[(int)keycode::RBUTTON] =
68 Keyboard.Keys[(int)keycode::MBUTTON] = 0;
69
70 for (INT i = 0; i < 256; i++)
71 {
72 Keyboard.Keys[i] >>= 7;
73 Keyboard.KeysClick[i] = Keyboard.Keys[i] && !Keyboard.KeysOld[i];
74 }
75 if (io.WantCaptureKeyboard)
76 {
77 char lbut = Keyboard.Keys[(int)keycode::LBUTTON];
78 char rbut = Keyboard.Keys[(int)keycode::RBUTTON];
79 char mbut = Keyboard.Keys[(int)keycode::MBUTTON];
80
81 memset(Keyboard.Keys, 0, 256), memset(Keyboard.KeysClick, 0, 256);
82 Keyboard.Keys[(int)keycode::LBUTTON] = lbut;
83 Keyboard.Keys[(int)keycode::RBUTTON] = rbut;
84 Keyboard.Keys[(int)keycode::MBUTTON] = mbut;
85 }
86}
87
88void scl::windows_input_system::Init(window_handle *WindowHandle, int *MouseWheel)
89{
90 this->WindowHandle = WindowHandle;
91 this->Wheel = MouseWheel;
92}
93
95{
96 if (WindowHandle == nullptr || Wheel == nullptr) return;
97
98 MouseRead();
99 KeyboardRead();
100}
mouse_data Mouse
Definition: input.h:51
void Init(window_handle *WindowHandle, int *MouseWheel)
void SelfResponse() override
int window_handle
Definition: base.h:55
Sculpto library prehompiled header. Defines common definitions, includes commonly used modules.
Input window_handle definition module.