Skip to content

Commit

Permalink
C#-JS API fixes (#18)
Browse files Browse the repository at this point in the history
C#-JS API fixes
  • Loading branch information
viktor-ferenczi authored Oct 31, 2021
1 parent ed72884 commit 57466a8
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 75 deletions.
10 changes: 5 additions & 5 deletions EnhancedUI/Content/ControlPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ let rendering = false;
// noinspection JSUnusedGlobalSymbols
async function OnGameStateChange(version) {
// Is the model accessible?
if (TerminalViewModel === undefined)
if (typeof TerminalViewModel === typeof undefined)
return;

// Eliminate any any duplicate or redundant calls
Expand Down Expand Up @@ -123,7 +123,7 @@ function renderBlockProperty(parent, blockId, propertyState) {
value.append(input);

value.bind('change', async function (e) {
await TerminalViewModel.SetBlockProperty(blockId, propertyId, parseInt(input.value()));
await TerminalViewModel.SetBlockProperty(blockId, propertyId, parseInt(input.val()));
});

break;
Expand All @@ -143,7 +143,7 @@ function renderBlockProperty(parent, blockId, propertyState) {
value.append(input);

value.bind('change', async function (e) {
await TerminalViewModel.SetBlockProperty(blockId, propertyId, parseFloat(input.value()));
await TerminalViewModel.SetBlockProperty(blockId, propertyId, parseFloat(input.val()));
});

break;
Expand All @@ -162,7 +162,7 @@ function renderBlockProperty(parent, blockId, propertyState) {
value.append(input);

value.bind('change', async function (e) {
await TerminalViewModel.SetBlockProperty(blockId, propertyId, input.value());
await TerminalViewModel.SetBlockProperty(blockId, propertyId, input.val());
});

break;
Expand All @@ -183,7 +183,7 @@ function renderBlockProperty(parent, blockId, propertyState) {
value.append(input);

value.bind('change', async function (e) {
await TerminalViewModel.SetBlockProperty(blockId, propertyId, input.value());
await TerminalViewModel.SetBlockProperty(blockId, propertyId, input.val());
});

break;
Expand Down
2 changes: 1 addition & 1 deletion EnhancedUI/Gui/ChromiumGuiControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ private void OnGameStateChanged(long version)
if (!IsBrowserInitialized)
return;

chromium?.Browser.ExecuteScriptAsync($"OnGameStateChange({version})");
chromium?.Browser.ExecuteScriptAsync($"if (typeof OnGameStateChange != typeof undefined) OnGameStateChange({version})");
}

private void Navigate()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,19 @@ private static bool CreateControlPanelPageControlsPrefix(

// Focus the browser "control" by default when the tab is selected (see the original function)
___m_defaultFocusedControlKeyboard[MyTerminalPageEnum.ControlPanel] = control;
___m_defaultFocusedControlGamepad[MyTerminalPageEnum.ControlPanel] = control;

// FIXME: Looks like this is not needed, since it does not present in the original code either.
//___m_defaultFocusedControlGamepad[MyTerminalPageEnum.ControlPanel] = control;

return false;
}

[HarmonyPatch("AttachGroups")]
[HarmonyPrefix]
private static bool AttachGroupsPatch() => false;

[HarmonyPatch("DetachGroups")]
[HarmonyPrefix]
private static bool DetachGroupsPatch() => false;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Reflection;
using EnhancedUI.ViewModel;
using HarmonyLib;

namespace EnhancedUI.Gui.Terminal.ControlPanel
Expand Down
39 changes: 14 additions & 25 deletions EnhancedUI/ViewModel/BlockViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,19 @@ public class BlockViewModel : IDisposable
// ReSharper disable once UnusedMember.Global
// ReSharper disable once MemberCanBePrivate.Global
public string ClassName => block.GetType().Name;

// ReSharper disable once UnusedMember.Global
// ReSharper disable once MemberCanBePrivate.Global
public string TypeId => block.BlockDefinition.Id.TypeId.ToString();

// ReSharper disable once UnusedMember.Global
// ReSharper disable once MemberCanBePrivate.Global
public string SubtypeName => block.BlockDefinition.Id.SubtypeName;

// Geometry
// ReSharper disable once UnusedMember.Global
public int[] Position => block.Position.ToArray();

// ReSharper disable once UnusedMember.Global
public int[] Size => block.BlockDefinition.Size.ToArray();

Expand All @@ -85,17 +88,15 @@ public BlockViewModel(TerminalViewModel terminalViewModel, MyTerminalBlock termi

MyLog.Default.Info($"EnhancedUI: {this}");

UpdateFields(version);
UpdateFields(true);
CreatePropertyModels();

block.PropertiesChanged += OnPropertyChanged;
}
#pragma warning restore 8618

private bool UpdateFields(long version)
private void UpdateFields(bool changed = false)
{
var changed = false;

var isValid = !block.Closed && block.InScene && !block.IsPreview;
if (isValid != IsValid)
{
Expand Down Expand Up @@ -133,9 +134,7 @@ private bool UpdateFields(long version)
}

if (changed)
Version = version;

return changed;
Version = terminalModel.GetNextVersion();
}

private void CreatePropertyModels()
Expand All @@ -161,50 +160,39 @@ private void OnPropertyChanged(MyTerminalBlock obj)
}

// Updates model from game state, returns true if anything has changed
public bool Update(long version)
public void Update()
{
var changed = UpdateFields(version);
return UpdateProperties(version) || changed;
UpdateFields();
UpdateProperties();
}

private bool UpdateProperties(long version)
private void UpdateProperties()
{
var changed = false;
foreach (var property in Properties.Values)
changed = property.Update(block) || changed;

if (changed)
Version = version;

return changed;
Version = terminalModel.GetNextVersion();
}

// Applies model changes to game state, returns true if anything has changed
public bool Apply(long version)
public void Apply()
{
var changed = false;

var defaultName = block.DisplayNameText ?? block.DisplayName;
if (Name != defaultName && Name != block.CustomName.ToString())
{
block.CustomName.Clear();
block.CustomName.Append(Name);
changed = true;
}

if (CustomData != block.CustomData.ToString())
{
block.CustomData = CustomData;
changed = true;
}

foreach (var property in Properties.Values)
changed = property.Apply(block) || changed;

if (changed)
Version = version;

return changed;
property.Apply(block);
}

public void SetName(string name)
Expand All @@ -230,6 +218,7 @@ public void SetProperty(string propertyId, object? value)

private void NotifyChange()
{
Version = terminalModel.GetNextVersion();
terminalModel.NotifyUserModifiedBlock(Id);
}
}
Expand Down
7 changes: 5 additions & 2 deletions EnhancedUI/ViewModel/ITerminalViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ namespace EnhancedUI.ViewModel
public interface ITerminalViewModel
{
// TODO: Grid API
// TODO: Provide a way to query the ID of the interacted block.

// Returns the ID of the interacted block the player is directly connected to.
// Returns null if the player is not connected to a terminal port.
long? GetInteractedBlockId();

// Returns list of IDs of blocks the player have access to via the interacted block.
// Returns empty list if the player is not connected to a terminal port.
Expand All @@ -25,7 +28,7 @@ public interface ITerminalViewModel
// Modifies a block's CustomData, actual modification will happen on the next game update
void SetBlockCustomData(long blockId, string customData);

// Modified a property value, actual modification will happen on the next game update
// Modifies a block property's value, actual modification will happen on the next game update
void SetBlockProperty(long blockId, string propertyId, object? value);

// Returns the named groups of blocks the player have access to via the interacted block.
Expand Down
23 changes: 11 additions & 12 deletions EnhancedUI/ViewModel/PropertyViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public bool Update(MyTerminalBlock block)
}

// Applies the value to the in-game property
public bool Apply(MyTerminalBlock block)
public void Apply(MyTerminalBlock block)
{
return Write(block, property, Value);
Write(block, property, Value);
}

private static object? Read(MyTerminalBlock block, ITerminalProperty property)
Expand All @@ -68,41 +68,40 @@ public bool Apply(MyTerminalBlock block)
return null;
}

private static bool Write(MyTerminalBlock block, ITerminalProperty property, object? value)
private static void Write(MyTerminalBlock block, ITerminalProperty property, object? value)
{
if (value == Read(block, property))
return false;
var currentValue = Read(block, property);
if (value == currentValue)
return;

switch (property.TypeName)
{
case "Boolean":
property.AsBool().SetValue(block, value as bool? ?? property.AsBool().GetDefaultValue(block));
return true;
break;

case "Single":
property.AsFloat().SetValue(block, value as float? ?? property.AsFloat().GetDefaultValue(block));
return true;
break;

case "Int64":
property.As<long>().SetValue(block, value as long? ?? property.As<long>().GetDefaultValue(block));
return true;
break;

case "StringBuilder":
property.As<StringBuilder>().SetValue(block,
value == null
? property.As<StringBuilder>().GetDefaultValue(block)
: new StringBuilder(value as string ?? ""));
return true;
break;

case "Color":
property.As<Color>().SetValue(block,
value == null
? property.As<Color>().GetDefaultValue(block)
: ParseColor(value as string ?? ""));
return true;
break;
}

return false;
}

private static string FormatColor(Color c)
Expand Down
Loading

0 comments on commit 57466a8

Please sign in to comment.