Skip to content

Commit

Permalink
Add pan, tilt and zoom buttons to full view
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Lo Nigro <git@d.sb>
  • Loading branch information
Daniel15 committed Nov 18, 2024
1 parent 34d3646 commit 44301d2
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 80 deletions.
5 changes: 5 additions & 0 deletions src/WebCamControl.Core/ICamera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,9 @@ public interface ICamera : IDisposable
/// Gets or sets the tilt. This is a number of degrees between -180 and 180.
/// </summary>
public AngleControl? Tilt { get; }

/// <summary>
/// Gets or sets the zoom.
/// </summary>
public ICameraControl<int>? Zoom { get; }
}
12 changes: 12 additions & 0 deletions src/WebCamControl.Core/Linux/LinuxCamera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ ILogger<LinuxCameraEvents> eventsLogger
/// Gets or sets the tilt. This is a number of degrees between -180 and 180.
/// </summary>
public AngleControl? Tilt { get; private set; }

/// <summary>
/// Gets or sets the zoom.
/// </summary>
public ICameraControl<int>? Zoom { get; private set; }

/// <summary>
/// Any controls that are not supported as one of the high-level fields above.
Expand Down Expand Up @@ -179,6 +184,13 @@ private void CreateControls()
integers.Remove(ControlID.TiltAbsolute, out var tilt);
Tilt = AngleControl.CreateIfNotNull(tilt);

integers.Remove(ControlID.ZoomAbsolute, out var zoom);
if (zoom != null)
{
zoom.UserFriendlyValueDelegate = value => $"{value}%";
}
Zoom = zoom;

booleans.Remove(ControlID.AutoWhiteBalance, out var autoWhiteBalance);
AutoWhiteBalance = autoWhiteBalance;

Expand Down
11 changes: 11 additions & 0 deletions src/WebCamControl.Gtk/FullWindow.blp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ Adw.ApplicationWindow full_window {
"boxed-list",
"settings-page",
]

Adw.ActionRow {
title: "Pan and Tilt";
selectable: false;

[suffix]
Box _panAndTiltButtons {
margin-bottom: 4;
margin-top: 4;
}
}

Adw.ActionRow _exampleRow {
title: _("Brightness");
Expand Down
4 changes: 4 additions & 0 deletions src/WebCamControl.Gtk/FullWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class FullWindow : Adw.Window
[Connect] private readonly ListBox _controls = default!;
[Connect] private readonly ActionRow _exampleRow = default!;
[Connect] private readonly ListBox _presetsList = default!;
[Connect] private readonly Box _panAndTiltButtons = default!;
#pragma warning restore CS0649 // Field is never assigned to, and will always have its default value

public FullWindow(
Expand Down Expand Up @@ -47,10 +48,13 @@ IPresets presets
/// </summary>
private void InitializeWidgets()
{
_panAndTiltButtons.Append(new PanAndTiltButtons(_camera));

_controls.Remove(_exampleRow);

var potentialControls = new Widget?[]
{
CameraControlSlider.TryCreate(_camera.Zoom),
CameraControlSlider.TryCreate(_camera.Brightness),
CameraControlSwitch.TryCreate(_camera.AutoWhiteBalance),
CameraControlSlider.TryCreate(_camera.Temperature),
Expand Down
53 changes: 1 addition & 52 deletions src/WebCamControl.Gtk/MiniWindow.blp
Original file line number Diff line number Diff line change
Expand Up @@ -22,58 +22,7 @@ Adw.ApplicationWindow mini_window {
margin-bottom: 12;
margin-end: 12;

Grid {
column-spacing: 4;
row-spacing: 4;

Button _up {
tooltip-text: _('Up');
icon-name: 'go-up';
width-request: 50;
height-request: 50;

layout {
row: '0';
column: '1';
}
}

Button _down {
tooltip-text: _('Down');
icon-name: 'go-down';
width-request: 50;
height-request: 50;

layout {
row: '1';
column: '1';
}
}

Button _left {
tooltip-text: _('Left');
icon-name: 'go-previous';
width-request: 50;
height-request: 50;

layout {
row: '1';
column: '0';
}
}

Button _right {
tooltip-text: _('Right');
icon-name: 'go-next';
width-request: 50;
height-request: 50;

layout {
row: '1';
column: '2';
}
}
}
Box _panAndTiltButtons {}

Separator {
orientation: horizontal;
Expand Down
29 changes: 3 additions & 26 deletions src/WebCamControl.Gtk/MiniWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,13 @@ namespace WebCamControl.Gtk;
/// </summary>
public class MiniWindow : Adw.Window
{
private const float _panTiltAdjustmentAmount = 2f;
private const int _minPresetButtonCount = 6;

private readonly ICamera _camera;
private readonly IPresets _presets;

#pragma warning disable CS0649 // Field is never assigned to, and will always have its default value
[Connect] private readonly PressAndHoldButton _up = default!;
[Connect] private readonly PressAndHoldButton _down = default!;
[Connect] private readonly PressAndHoldButton _left = default!;
[Connect] private readonly PressAndHoldButton _right = default!;
[Connect] private readonly Box _panAndTiltButtons = default!;
[Connect] private readonly Box _buttonsBox = default!;
#pragma warning restore CS0649 // Field is never assigned to, and will always have its default value

Expand All @@ -45,32 +41,13 @@ IPresets presets
builder.Connect(this);
Title = "WebCamControl: " + _camera.Name;
// TODO: Configure proper icon
InitializePanAndTilt();

_panAndTiltButtons.Append(new PanAndTiltButtons(_camera));
InitializePresets();

presets.OnChange += (_, _) => InitializePresets();
}

private void InitializePanAndTilt()
{
_left.DisableCameraControlIfUnsupported(_camera.Pan);
_right.DisableCameraControlIfUnsupported(_camera.Pan);
if (_camera.Pan != null)
{
_left.OnHeld += (_, _) => _camera.Pan.Value -= _panTiltAdjustmentAmount;
_right.OnHeld += (_, _) => _camera.Pan.Value += _panTiltAdjustmentAmount;
}

_down.DisableCameraControlIfUnsupported(_camera.Tilt);
_up.DisableCameraControlIfUnsupported(_camera.Tilt);
if (_camera.Tilt != null)
{
_down.OnHeld += (_, _) => _camera.Tilt.Value -= _panTiltAdjustmentAmount;
_up.OnHeld += (_, _) => _camera.Tilt.Value += _panTiltAdjustmentAmount;
}
}

private void InitializePresets()
{
_buttonsBox.RemoveChildren();
Expand Down
4 changes: 2 additions & 2 deletions src/WebCamControl.Gtk/WebCamControl.Gtk.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<Target Name="BlueprintBuild" BeforeTargets="PrepareForBuild">
<!-- Build Blueprints -->
<Message Text="Running blueprint-compiler" Importance="high" />
<Exec Command="blueprint-compiler batch-compile $(MSBuildProjectDirectory)/obj/blueprints/ $(MSBuildProjectDirectory) $(MSBuildProjectDirectory)/*.blp"/>
<Exec Command="blueprint-compiler batch-compile $(MSBuildProjectDirectory)/obj/blueprints/ $(MSBuildProjectDirectory) $(MSBuildProjectDirectory)/**/*.blp"/>
</Target>


Expand All @@ -42,7 +42,7 @@
<!-- Add the blueprints as a resource -->
<ItemGroup>
<None Remove="obj\blueprints\*.ui"/>
<EmbeddedResource Include="obj\blueprints\*.ui">
<EmbeddedResource Include="obj\blueprints\**\*.ui">
<LogicalName>%(Filename)%(Extension)</LogicalName>
</EmbeddedResource>
</ItemGroup>
Expand Down
54 changes: 54 additions & 0 deletions src/WebCamControl.Gtk/Widgets/PanAndTiltButtons.blp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using Gtk 4.0;

Grid pan_and_tilt_buttons {
column-spacing: 4;
row-spacing: 4;

Button _up {
tooltip-text: _('Up');
icon-name: 'go-up';
width-request: 50;
height-request: 50;

layout {
row: '0';
column: '1';
}
}

Button _down {
tooltip-text: _('Down');
icon-name: 'go-down';
width-request: 50;
height-request: 50;

layout {
row: '1';
column: '1';
}
}

Button _left {
tooltip-text: _('Left');
icon-name: 'go-previous';
width-request: 50;
height-request: 50;

layout {
row: '1';
column: '0';
}
}

Button _right {
tooltip-text: _('Right');
icon-name: 'go-next';
width-request: 50;
height-request: 50;

layout {
row: '1';
column: '2';
}
}
}
63 changes: 63 additions & 0 deletions src/WebCamControl.Gtk/Widgets/PanAndTiltButtons.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2024 Daniel Lo Nigro <d@d.sb>

using Gtk;
using WebCamControl.Core;
using WebCamControl.Gtk.Extensions;

namespace WebCamControl.Gtk.Widgets;

/// <summary>
/// Renders up, down, left, and right buttons to adjust the pan and tilt.
/// </summary>
public class PanAndTiltButtons : Box
{
private const float _panTiltAdjustmentAmount = 2f;

private readonly ICamera _camera;
private readonly Builder _builder;

#pragma warning disable CS0649 // Field is never assigned to, and will always have its default value
[Connect] private readonly PressAndHoldButton _up = default!;
[Connect] private readonly PressAndHoldButton _down = default!;
[Connect] private readonly PressAndHoldButton _left = default!;
[Connect] private readonly PressAndHoldButton _right = default!;
#pragma warning restore CS0649 // Field is never assigned to, and will always have its default value

public PanAndTiltButtons(ICamera camera)
{
_camera = camera;
_builder = new Builder("PanAndTiltButtons.ui");
var rootWidget = (Grid)_builder.GetObject("pan_and_tilt_buttons")!;
Append(rootWidget);
_builder.Connect(this);

AttachEvents();
}

private void AttachEvents()
{
_left.DisableCameraControlIfUnsupported(_camera.Pan);
_right.DisableCameraControlIfUnsupported(_camera.Pan);
if (_camera.Pan != null)
{
_left.OnHeld += (_, _) => _camera.Pan.Value -= _panTiltAdjustmentAmount;
_right.OnHeld += (_, _) => _camera.Pan.Value += _panTiltAdjustmentAmount;
}

_down.DisableCameraControlIfUnsupported(_camera.Tilt);
_up.DisableCameraControlIfUnsupported(_camera.Tilt);
if (_camera.Tilt != null)
{
_down.OnHeld += (_, _) => _camera.Tilt.Value -= _panTiltAdjustmentAmount;
_up.OnHeld += (_, _) => _camera.Tilt.Value += _panTiltAdjustmentAmount;
}
}

public override void Dispose()
{
GC.SuppressFinalize(this);
base.Dispose();
_builder.Dispose();
}
}

0 comments on commit 44301d2

Please sign in to comment.