Skip to content

Commit

Permalink
Control Panel view state (#13)
Browse files Browse the repository at this point in the history
Control Panel view state

* State more in-line with React's model
* Listing all blocks from the terminal system
* Calls to modify the block properties
* Reasonable printing of block properties for initial testing
  • Loading branch information
viktor-ferenczi authored Oct 3, 2021
1 parent ba792c0 commit 3f20e06
Show file tree
Hide file tree
Showing 20 changed files with 428 additions and 185 deletions.
3 changes: 1 addition & 2 deletions EnhancedUI/Content/ControlPanel.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ <h1 class="show">CONTROL PANEL</h1>

<p>Browser size: <span id="window-size"></span> pixels</p>

<ul id="blocks">
</ul>
<div id="blocks"></div>

</div>

Expand Down
96 changes: 84 additions & 12 deletions EnhancedUI/Content/ControlPanel.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,88 @@
var state = '';
var blockStates = null;

async function clearContent() {
$('#blocks').empty();
async function stateUpdated() {
const blockViews = $('#blocks');
blockViews.empty();

blockStates = await state.GetBlockStates();
for (const entityId in blockStates) {
renderBlock(blockViews, blockStates[entityId]);
}
}

function renderBlock(parent, blockState) {
let blockView = $('<div />');
blockView.addClass('block');
blockView.attr('id', 'block-' + blockState.EntityId);
renderBlockInner(blockView, blockState);
parent.append(blockView);
}

function renderBlockInner(blockView, blockState) {
let id = $('<div />');
id.addClass('entityId');
id.text(blockState.EntityId);
blockView.append(id);

let type = $('<div />');
type.addClass('type');
type.text(blockState.ClassName + ' | ' + blockState.TypeId + ' | ' + blockState.SubtypeName);
blockView.append(type);

let name = $('<div />');
name.addClass('name');
name.text(blockState.Name);
blockView.append(name);

let properties = $('<div />')
properties.addClass('properties');
for (const propertyId in blockState.PropertyStates) {
renderBlockProperty(properties, blockState.PropertyStates[propertyId])
}
blockView.append(properties)

blockView.append($('<hr />'))
}

function renderBlockProperty(parent, propertyState) {
let propertyView= $('<div />');
propertyView.addClass('property');

let value = $('<div />');
value.addClass('value');
switch(propertyState.TypeName) {
case "Boolean":
cb = $('<input />')
cb.attr('type', 'checkbox');
cb.attr('id', propertyState.Id);
if (propertyState.BoolValue) {
cb.attr('checked', 'checked');
}
value.append(cb);
label = $('<label />');
label.attr('for', propertyState.Id);
label.text(propertyState.Id);
value.append(label);
break;
case "Int64":
value.text(propertyState.Id + ': ' + propertyState.LongValue.toString());
break;
case "Single":
value.text(propertyState.Id + ': ' + propertyState.FloatValue.toString());
break;
default:
value.text(propertyState.Id + ' ' + propertyState.TypeName + ': ' + propertyState.BoolValue.toString() + " | " + propertyState.LongValue.toString() + " | " + propertyState.FloatValue.toString());
}

propertyView.append(value);

parent.append(propertyView);
}

async function updateContent() {
const blocks = await model.GetBlocks();
const ul = $('#blocks');
ul.empty();
blocks.forEach(block => {
let li = $('<li>');
li.append(document.createTextNode(block.Name));
ul.append(li);
});
async function blockStateUpdated(entityId) {
// let blockState = await state.GetBlockState(entityId);
// let blockView = $('#block-' + entityId);
// if (blockView.length > 0) {
// renderBlockInner(blockView, blockState);
// }
}
2 changes: 0 additions & 2 deletions EnhancedUI/Content/Inventory.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ <h1 class="show">INVENTORY</h1>

<p>Browser size: <span id="window-size"></span> pixels</p>

<p>Proxy result: <span id="result"></span></p>

</div>

</body>
Expand Down
4 changes: 1 addition & 3 deletions EnhancedUI/Content/Inventory.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
async function loadItems() {
const result = await model.TestAdd(1, 2);
$('#result').text(result.toString());
async function stateUpdated() {
}
15 changes: 14 additions & 1 deletion EnhancedUI/Content/common.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ html {
}

body {
overflow: hidden;
/*overflow: hidden;*/
}

.fullscreen {
Expand All @@ -26,4 +26,17 @@ body {

.hide {
display: none;
}

label {
padding-left: 0.5ch;
}

.block {
margin-bottom: 2vh;
}

.block .property {
margin-top: 0.5vh;
margin-left: 2vw;
}
13 changes: 5 additions & 8 deletions EnhancedUI/Content/common.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
$(document).ready(function () {
$("#window-size").text(`${window.innerWidth}x${window.innerHeight}`);
initializeProxy().then(_ => null);
});
$(document).ready(async function () {
await CefSharp.BindObjectAsync("state");
state.NotifyBound();

async function initializeProxy() {
await CefSharp.BindObjectAsync("model");
model.MarkLoaded();
}
$("#window-size").text(`${window.innerWidth}x${window.innerHeight}`);
});
32 changes: 0 additions & 32 deletions EnhancedUI/Gui/BrowserViewModel.cs

This file was deleted.

6 changes: 3 additions & 3 deletions EnhancedUI/Gui/Chromium.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class Chromium : IDisposable

public readonly ChromiumWebBrowser Browser;

public Chromium(Vector2I size, object viewModel)
public Chromium(Vector2I size, object state)
{
videoData = new byte[size.X * size.Y * 4];

Expand All @@ -31,11 +31,11 @@ public Chromium(Vector2I size, object viewModel)
Browser.JavascriptObjectRepository.ResolveObject += (sender, e) =>
{
var repo = e.ObjectRepository;
if (e.ObjectName == "model")
if (e.ObjectName == "state")
{
// No CamelCase of Javascript Names
repo.NameConverter = null;
repo.Register("model", viewModel, isAsync: true, options: BindingOptions.DefaultBinder);
repo.Register("state", state, isAsync: true, options: BindingOptions.DefaultBinder);
}
};
}
Expand Down
14 changes: 7 additions & 7 deletions EnhancedUI/Gui/ChromiumGuiControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ public class ChromiumGuiControl : MyGuiControlBase
private bool capsLock;
private Vector2I lastValidMousePosition = -Vector2I.One;
private List<MyKeys> lastPressedKeys = new();
private readonly IBrowserViewModel viewModel;
private readonly IPanelState state;

public ChromiumGuiControl(WebContent content, string name, IBrowserViewModel viewModel)
public ChromiumGuiControl(WebContent content, string name, IPanelState state)
{
this.content = content;
this.name = name;
this.viewModel = viewModel;
this.state = state;
}

protected override void OnSizeChanged()
Expand All @@ -59,12 +59,12 @@ private void CreatePlayerIfNeeded()
}

var rect = GetVideoScreenRectangle();
chromium = new Chromium(new Vector2I(rect.Width, rect.Height), viewModel);
chromium = new Chromium(new Vector2I(rect.Width, rect.Height), state);

chromium.Ready += OnChromiumReady;
chromium.Browser.LoadingStateChanged += OnBrowserLoadingStateChanged;

viewModel.SetBrowser(chromium.Browser);
state.SetBrowser(chromium.Browser);

player = new BatchDataPlayer(new Vector2I(rect.Width, rect.Height), chromium.GetVideoData);
VideoPlayPatch.RegisterVideoPlayer(name, player);
Expand All @@ -79,7 +79,7 @@ public override void OnRemoving()
return;
}

viewModel.SetBrowser(null);
state.SetBrowser(null);

chromium.Ready -= OnChromiumReady;
chromium.Browser.LoadingStateChanged -= OnBrowserLoadingStateChanged;
Expand Down Expand Up @@ -145,7 +145,7 @@ public override void Draw(float transitionAlpha, float backgroundTransitionAlpha
// Reloads the HTML document
private void ReloadPage()
{
viewModel.Reload();
state.Reload();
}

private void OpenWebDeveloperTools()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

namespace EnhancedUI.Gui
{
public interface IBrowserViewModel
public interface IPanelState
{
// Assigns the current Chromium browser to the view model, required to invoke JavaScript
// Access to the browser instance is required to invoke JavaScript
void SetBrowser(ChromiumWebBrowser? browser);

// Return True if the page has been loaded
bool HasLoaded();
bool HasBound();

// Marks the page as loaded
void MarkLoaded();
void NotifyBound();

// Reloads the page
void Reload();
Expand Down
38 changes: 38 additions & 0 deletions EnhancedUI/Gui/PanelState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using CefSharp;
using CefSharp.OffScreen;

namespace EnhancedUI.Gui
{
public class PanelState : IPanelState
{
// Stores a reference to the browser, required to invoke JavaScript code (send events from C# to JS)
protected ChromiumWebBrowser? Browser;

// True value indicates that the state has been bound to a JS accessible global variable already
private bool bound;

public void SetBrowser(ChromiumWebBrowser? browser)
{
Browser = browser;
}

// Checks whether the state has been bound to a JS accessible global variable already
public bool HasBound()
{
return bound && Browser?.IsBrowserInitialized == true;
}

// Invoked by JS code after the state is bound to a global variable
public virtual void NotifyBound()
{
bound = true;
}

// Reloads the page, which requires re-binding the state to a JS global variable
public virtual void Reload()
{
bound = false;
Browser?.Reload();
}
}
}
24 changes: 0 additions & 24 deletions EnhancedUI/Gui/Terminal/ControlPanel/BlockInfo.cs

This file was deleted.

Loading

0 comments on commit 3f20e06

Please sign in to comment.