Skip to content

Commit

Permalink
Add layer for managing angle presets
Browse files Browse the repository at this point in the history
  • Loading branch information
wilszdev committed May 26, 2022
1 parent 6d2400a commit 8cccfb4
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 28 deletions.
46 changes: 27 additions & 19 deletions UFV Controller/imgui.ini
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ DockId=0x00000007,0

[Window][Gamepad Controls]
Pos=0,148
Size=640,582
Size=412,582
Collapsed=0
DockId=0x0000000E,1
DockId=0x0000000F,1

[Window][Outgoing COM]
Pos=1014,26
Expand All @@ -79,9 +79,9 @@ DockId=0x0000000C,1

[Window][Input Layer]
Pos=0,148
Size=640,582
Size=412,582
Collapsed=0
DockId=0x0000000E,0
DockId=0x0000000F,0

[Window][Comms Layer]
Pos=642,390
Expand All @@ -95,20 +95,28 @@ Size=1600,168
Collapsed=0
DockId=0x0000000D,0

[Window][Presets]
Pos=414,148
Size=226,582
Collapsed=0
DockId=0x00000010,0

[Docking][Data]
DockSpace ID=0xF2944C7F Window=0x4647B76E Pos=53,82 Size=1600,874 Split=Y
DockNode ID=0x0000000A Parent=0xF2944C7F SizeRef=1920,704 Split=X
DockNode ID=0x00000003 Parent=0x0000000A SizeRef=1088,874 Split=X
DockNode ID=0x0000000B Parent=0x00000003 SizeRef=505,874 Split=X Selected=0xB10717DB
DockNode ID=0x00000001 Parent=0x0000000B SizeRef=960,842 Split=Y Selected=0xB10717DB
DockNode ID=0x00000008 Parent=0x00000001 SizeRef=960,120 CentralNode=1 Selected=0xB10717DB
DockNode ID=0x0000000E Parent=0x00000001 SizeRef=960,582 Selected=0x05192D51
DockNode ID=0x00000002 Parent=0x0000000B SizeRef=958,842 Split=Y Selected=0x028D2322
DockNode ID=0x00000004 Parent=0x00000002 SizeRef=958,362 Selected=0x028D2322
DockNode ID=0x00000009 Parent=0x00000002 SizeRef=958,340 Selected=0x2F1DF914
DockNode ID=0x0000000C Parent=0x00000003 SizeRef=586,874 Selected=0x05BE2C37
DockNode ID=0x00000005 Parent=0x0000000A SizeRef=510,874 Split=Y Selected=0x028D2322
DockNode ID=0x00000006 Parent=0x00000005 SizeRef=510,268 Selected=0x028D2322
DockNode ID=0x00000007 Parent=0x00000005 SizeRef=510,604 Selected=0xC7F6FC68
DockNode ID=0x0000000D Parent=0xF2944C7F SizeRef=1920,168 Selected=0x64F50EE5
DockSpace ID=0xF2944C7F Window=0x4647B76E Pos=138,187 Size=1600,874 Split=Y
DockNode ID=0x0000000A Parent=0xF2944C7F SizeRef=1920,704 Split=X
DockNode ID=0x00000003 Parent=0x0000000A SizeRef=1088,874 Split=X
DockNode ID=0x0000000B Parent=0x00000003 SizeRef=505,874 Split=X Selected=0xB10717DB
DockNode ID=0x00000001 Parent=0x0000000B SizeRef=960,842 Split=Y Selected=0xB10717DB
DockNode ID=0x00000008 Parent=0x00000001 SizeRef=960,120 CentralNode=1 Selected=0xB10717DB
DockNode ID=0x0000000E Parent=0x00000001 SizeRef=960,582 Split=X Selected=0xEA7DDBFF
DockNode ID=0x0000000F Parent=0x0000000E SizeRef=619,582 Selected=0xEA7DDBFF
DockNode ID=0x00000010 Parent=0x0000000E SizeRef=339,582 Selected=0x8A63053B
DockNode ID=0x00000002 Parent=0x0000000B SizeRef=958,842 Split=Y Selected=0x028D2322
DockNode ID=0x00000004 Parent=0x00000002 SizeRef=958,362 Selected=0x028D2322
DockNode ID=0x00000009 Parent=0x00000002 SizeRef=958,340 Selected=0x2F1DF914
DockNode ID=0x0000000C Parent=0x00000003 SizeRef=586,874 Selected=0x05BE2C37
DockNode ID=0x00000005 Parent=0x0000000A SizeRef=510,874 Split=Y Selected=0x028D2322
DockNode ID=0x00000006 Parent=0x00000005 SizeRef=510,268 Selected=0x028D2322
DockNode ID=0x00000007 Parent=0x00000005 SizeRef=510,604 Selected=0xC7F6FC68
DockNode ID=0x0000000D Parent=0xF2944C7F SizeRef=1920,168 Selected=0x64F50EE5

Binary file added UFV Controller/presets.dat
Binary file not shown.
3 changes: 2 additions & 1 deletion UFV Controller/src/Layers/Layers.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
#include "FluidLayer.h"
#include "GamepadControlsLayer.h"
#include "InputLayer.h"
#include "LoggingLayer.h"
#include "LoggingLayer.h"
#include "PresetsLayer.h"
65 changes: 65 additions & 0 deletions UFV Controller/src/Layers/PresetsLayer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include "PresetsLayer.h"
#include "../Win32Helpers.h"

static void SavePresets()
{
HANDLE file = CreateFileA("presets.dat", GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0);
if (file && file != INVALID_HANDLE_VALUE)
{
DWORD size = sizeof(*g_anglePresets) * NUM_PRESETS;
DWORD written = 0;
if (!WriteFile(file, g_anglePresets, size, &written, 0))
Win32Log("[SavePresets] WriteFile() failed with error %d: %s",
GetLastError(), Win32GetErrorCodeDescription(GetLastError()).c_str());
CloseHandle(file);
}
else
Win32Log("[LoadPresets] CreateFileA() failed with error %d: %s",
GetLastError(), Win32GetErrorCodeDescription(GetLastError()).c_str());
}

static void LoadPresets()
{
HANDLE file = CreateFileA("presets.dat", GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);
if (file && file != INVALID_HANDLE_VALUE)
{
DWORD size = sizeof(*g_anglePresets) * NUM_PRESETS;
DWORD read = 0;
if (!ReadFile(file, g_anglePresets, size, &read, 0))
Win32Log("[LoadPresets] ReadFile failed with error %d: %s",
GetLastError(), Win32GetErrorCodeDescription(GetLastError()).c_str());
CloseHandle(file);
}
else
Win32Log("[LoadPresets] CreateFileA() failed with error %d: %s",
GetLastError(), Win32GetErrorCodeDescription(GetLastError()).c_str());
}

void PresetsLayer::OnUIRender()
{
ImGui::Begin("Presets");

for (int i = 0; i < NUM_PRESETS; ++i)
{
char buf[16] = {};
sprintf(buf, "Preset %d", i + 1);

char buf2[18] = "##";
strcat_s(buf2, buf);

ImGui::Text(buf); ImGui::SameLine();
ImGui::InputInt2(buf2, g_anglePresets[i].angles);
}

if (ImGui::Button("Save these presets"))
{
SavePresets();
}

ImGui::End();
}

void PresetsLayer::OnAttach()
{
LoadPresets();
}
10 changes: 10 additions & 0 deletions UFV Controller/src/Layers/PresetsLayer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once
#include "Walnut/Application.h"
#include "../WalnutApp.h"

class PresetsLayer : public Walnut::Layer
{
public:
virtual void OnUIRender() override;
virtual void OnAttach() override;
};
9 changes: 6 additions & 3 deletions UFV Controller/src/WalnutApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
#include "Walnut/EntryPoint.h"
#include "Layers/Layers.h"

ufv_state g_ufvState = { 0, 0, 0, false, 200};
ufv_state g_ufvState = { 0, 0, 0, false, 200 };
angle_preset g_anglePresets[NUM_PRESETS] = {};


void ApplyPreset(int index)
{
if (index < 0 || index > NUM_PRESETS - 1) return;

g_ufvState.tiltAngle = PRESET_TILT_ANGLE_VALUES[index];
g_ufvState.panAngle = PRESET_PAN_ANGLE_VALUES[index];
g_ufvState.tiltAngle = g_anglePresets[index].tilt;
g_ufvState.panAngle = g_anglePresets[index].pan;
}

Walnut::Application* Walnut::CreateApplication(int argc, char** argv)
Expand All @@ -25,6 +27,7 @@ Walnut::Application* Walnut::CreateApplication(int argc, char** argv)
app->PushLayer<GamepadControlsLayer>();
app->PushLayer<CommsLayer>();
app->PushLayer<LoggingLayer>();
app->PushLayer<PresetsLayer>();
app->SetMenubarCallback(
[app]()
{
Expand Down
23 changes: 18 additions & 5 deletions UFV Controller/src/WalnutApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,23 @@ struct ufv_state

extern ufv_state g_ufvState;

#define ANGLE_DEBOUNCE_DELAY_MS 100

#define NUM_PRESETS 6
const int PRESET_TILT_ANGLE_VALUES[NUM_PRESETS] = { -5,45,-5,45,-5,45 };
const int PRESET_PAN_ANGLE_VALUES[NUM_PRESETS] = { -60,60,-60,60,-60,60 };
struct angle_preset
{
struct
{
int pan;
int tilt;
};
union
{
int angles[2];
};
};

extern angle_preset g_anglePresets[NUM_PRESETS];


#define ANGLE_DEBOUNCE_DELAY_MS 100

void ApplyPreset(int index);
void ApplyPreset(int index);

0 comments on commit 8cccfb4

Please sign in to comment.