Skip to content

Commit

Permalink
Merge pull request #176 from Kapim/devel
Browse files Browse the repository at this point in the history
Devel
  • Loading branch information
Kapim authored Oct 22, 2020
2 parents ece3d3b + e498c85 commit a4f9900
Show file tree
Hide file tree
Showing 32 changed files with 4,106 additions and 827 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -409,10 +409,10 @@ RectTransform:
m_Father: {fileID: 847291161079909801}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 1}
m_AnchorMax: {x: 0, y: 1}
m_AnchoredPosition: {x: 357.5, y: 0}
m_SizeDelta: {x: 715, y: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &1395264303805572330
MonoBehaviour:
Expand Down
2 changes: 1 addition & 1 deletion arcor2_AREditor/Assets/2D_EDITOR/Scripts/ActionMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public async void SaveParameters() {
Debug.Assert(ProjectManager.Instance.AllowEdit);
try {
await WebsocketManager.Instance.UpdateAction(CurrentAction.Data.Id, parameters, CurrentAction.GetFlows());
Base.Notifications.Instance.ShowNotification("Parameters saved", "");
Base.Notifications.Instance.ShowToastMessage("Parameters saved");
SaveParametersBtn.SetInteractivity(false, "Parameters unchanged");
parametersChanged = false;
if (string.IsNullOrEmpty(GameManager.Instance.ExecutingAction))
Expand Down
290 changes: 46 additions & 244 deletions arcor2_AREditor/Assets/2D_EDITOR/Scripts/ActionObjectMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,39 @@
using Base;
using System.Collections.Generic;
using static IO.Swagger.Model.UpdateObjectPoseUsingRobotRequestArgs;
using Newtonsoft.Json;
using System.Linq;

[RequireComponent(typeof(SimpleSideMenu))]
public class ActionObjectMenu : MonoBehaviour, IMenu {
public abstract class ActionObjectMenu : MonoBehaviour, IMenu {
public Base.ActionObject CurrentObject;
[SerializeField]
private TMPro.TMP_Text objectName;
public DropdownParameter RobotsList, EndEffectorList, PivotList;
public Button NextButton, PreviousButton, FocusObjectDoneButton, StartObjectFocusingButton, SavePositionButton;
public TMPro.TMP_Text CurrentPointLabel;
public GameObject RobotsListsBlock, UpdatePositionBlockMesh, UpdatePositionBlockVO;
protected TMPro.TMP_Text objectName;
public GameObject Parameters;
public Slider VisibilitySlider;
public InputDialog InputDialog;
public ButtonWithTooltip SaveParametersBtn;

public SwitchComponent ShowModelSwitch;

public ConfirmationDialog ConfirmationDialog;

private int currentFocusPoint = -1;

private GameObject model;
protected SimpleSideMenu menu;

private SimpleSideMenu menu;
protected bool parametersChanged = false;
public VerticalLayoutGroup DynamicContentLayout;
public GameObject CanvasRoot;

protected List<IParameter> objectParameters = new List<IParameter>();

private void Start() {
menu = GetComponent<SimpleSideMenu>();
Debug.Assert(objectName != null);
Debug.Assert(RobotsList != null);
Debug.Assert(EndEffectorList != null);
Debug.Assert(NextButton != null);
Debug.Assert(PreviousButton != null);
Debug.Assert(FocusObjectDoneButton != null);
Debug.Assert(StartObjectFocusingButton != null);
Debug.Assert(SavePositionButton != null);
Debug.Assert(CurrentPointLabel != null);
Debug.Assert(RobotsListsBlock != null);
Debug.Assert(UpdatePositionBlockMesh != null);
Debug.Assert(UpdatePositionBlockVO != null);
Debug.Assert(VisibilitySlider != null);
Debug.Assert(InputDialog != null);
Debug.Assert(ConfirmationDialog != null);

List<string> pivots = new List<string>();
foreach (string item in Enum.GetNames(typeof(PivotEnum))) {
pivots.Add(item);
}
PivotList.PutData(pivots, "Middle", OnPivotChanged);

}


Expand Down Expand Up @@ -94,253 +81,68 @@ public async void RenameObject(string newName) {
}


public async void UpdateMenu() {
public virtual void UpdateMenu() {
objectName.text = CurrentObject.Data.Name;
// Parameters:

VisibilitySlider.value = CurrentObject.GetVisibility() * 100;
if (!SceneManager.Instance.SceneStarted) {
UpdatePositionBlockVO.SetActive(false);
UpdatePositionBlockMesh.SetActive(false);
RobotsListsBlock.SetActive(false);
return;
}
if (currentFocusPoint >= 0)
return;
if (SceneManager.Instance.RobotInScene()) {
await RobotsList.gameObject.GetComponent<DropdownRobots>().Init(OnRobotChanged, true);
string robotId = null;
try {
robotId = SceneManager.Instance.RobotNameToId(RobotsList.GetValue().ToString());
} catch (ItemNotFoundException ex) {
Debug.LogError(ex);
robotId = null;
}
if (string.IsNullOrEmpty(robotId)) {
Notifications.Instance.ShowNotification("Robot not found", "Robot with name " + RobotsList.GetValue().ToString() + "does not exists");

} else {
OnRobotChanged(robotId);
}

if (CurrentObject.ActionObjectMetadata.ObjectModel?.Type == IO.Swagger.Model.ObjectModel.TypeEnum.Mesh) {
UpdatePositionBlockVO.SetActive(false);
UpdatePositionBlockMesh.SetActive(true);
RobotsListsBlock.SetActive(true);
} else if (CurrentObject.ActionObjectMetadata.ObjectModel != null) {
UpdatePositionBlockVO.SetActive(true);
UpdatePositionBlockMesh.SetActive(false);
RobotsListsBlock.SetActive(true);
ShowModelSwitch.Interactable = SceneManager.Instance.RobotsEEVisible;
if (ShowModelSwitch.Interactable && ShowModelSwitch.Switch.isOn) {
ShowModelOnEE();
}
} else {
UpdatePositionBlockVO.SetActive(false);
UpdatePositionBlockMesh.SetActive(false);
RobotsListsBlock.SetActive(false);
Parameters.GetComponent<VerticalLayoutGroup>().enabled = true;
foreach (Transform o in Parameters.transform) {
if (o.name != "Layout" && o.gameObject.tag != "Persistent") {
Destroy(o.gameObject);
}

} else {
UpdatePositionBlockVO.SetActive(false);
UpdatePositionBlockMesh.SetActive(false);
RobotsListsBlock.SetActive(false);
}


FocusObjectDoneButton.interactable = false;
NextButton.interactable = false;
PreviousButton.interactable = false;



}

private async void OnRobotChanged(string robot_id) {
EndEffectorList.Dropdown.dropdownItems.Clear();
await EndEffectorList.gameObject.GetComponent<DropdownEndEffectors>().Init(robot_id, OnEEChanged);
UpdateModelOnEE();
}

private void OnEEChanged(string eeId) {
UpdateModelOnEE();
}

private void OnPivotChanged(string pivot) {
UpdateModelOnEE();
}


public async void UpdateObjectPosition() {
if (RobotsList.Dropdown.dropdownItems.Count == 0 || EndEffectorList.Dropdown.dropdownItems.Count == 0) {
Base.NotificationsModernUI.Instance.ShowNotification("Failed to update object position", "No robot or end effector available");
return;
}
PivotEnum pivot = (PivotEnum) Enum.Parse(typeof(PivotEnum), (string) PivotList.GetValue());
IRobot robot = SceneManager.Instance.GetRobotByName((string) RobotsList.GetValue());
await WebsocketManager.Instance.UpdateActionObjectPoseUsingRobot(CurrentObject.Data.Id,
robot.GetId(), (string) EndEffectorList.GetValue(), pivot);
VisibilitySlider.value = CurrentObject.GetVisibility() * 100;

}


private void EnableFocusControls() {
SavePositionButton.GetComponent<Button>().interactable = true;
StartObjectFocusingButton.GetComponent<Button>().interactable = true;
NextButton.GetComponent<Button>().interactable = true;
PreviousButton.GetComponent<Button>().interactable = true;
FocusObjectDoneButton.GetComponent<Button>().interactable = true;
}

private void DisableFocusControls() {
SavePositionButton.GetComponent<Button>().interactable = false;
StartObjectFocusingButton.GetComponent<Button>().interactable = false;
NextButton.GetComponent<Button>().interactable = false;
PreviousButton.GetComponent<Button>().interactable = false;
FocusObjectDoneButton.GetComponent<Button>().interactable = false;
}

public async void StartObjectFocusing() {
if (RobotsList.Dropdown.dropdownItems.Count == 0 || EndEffectorList.Dropdown.dropdownItems.Count == 0) {
Base.NotificationsModernUI.Instance.ShowNotification("Failed to update object position", "No robot or end effector available");
return;
}
try {
await WebsocketManager.Instance.StartObjectFocusing(CurrentObject.Data.Id,
(string) RobotsList.GetValue(),
(string) EndEffectorList.GetValue());
currentFocusPoint = 0;
UpdateCurrentPointLabel();
GetComponent<SimpleSideMenu>().handleToggleStateOnPressed = false;
GetComponent<SimpleSideMenu>().overlayCloseOnPressed = false;
FocusObjectDoneButton.interactable = true;
if (CurrentObject.ActionObjectMetadata.ObjectModel.Mesh.FocusPoints.Count > 1) {
NextButton.interactable = true;
PreviousButton.interactable = true;
}
} catch (Base.RequestFailedException ex) {
Base.NotificationsModernUI.Instance.ShowNotification("Failed to start object focusing", ex.Message);
CurrentPointLabel.text = "";
currentFocusPoint = -1;
if (ex.Message == "Focusing already started.") { //TODO HACK! find better solution
FocusObjectDone();
}
}
}

public async void SavePosition() {
if (currentFocusPoint < 0)
return;
try {
await WebsocketManager.Instance.SavePosition(CurrentObject.Data.Id, currentFocusPoint);
} catch (Base.RequestFailedException ex) {
Base.NotificationsModernUI.Instance.ShowNotification("Failed to save current position", ex.Message);
}


}

public async void FocusObjectDone() {
try {
await WebsocketManager.Instance.FocusObjectDone(CurrentObject.Data.Id);
CurrentPointLabel.text = "";
GetComponent<SimpleSideMenu>().handleToggleStateOnPressed = true;
GetComponent<SimpleSideMenu>().overlayCloseOnPressed = true;
currentFocusPoint = -1;
FocusObjectDoneButton.interactable = false;
NextButton.interactable = false;
PreviousButton.interactable = false;
} catch (Base.RequestFailedException ex) {
Base.NotificationsModernUI.Instance.ShowNotification("Failed to focus object", ex.Message);
}
}
protected abstract void UpdateSaveBtn();


public void NextPoint() {
currentFocusPoint = Math.Min(currentFocusPoint + 1, CurrentObject.ActionObjectMetadata.ObjectModel.Mesh.FocusPoints.Count - 1);
PreviousButton.interactable = true;
if (currentFocusPoint == CurrentObject.ActionObjectMetadata.ObjectModel.Mesh.FocusPoints.Count - 1) {
NextButton.GetComponent<Button>().interactable = false;
} else {
NextButton.GetComponent<Button>().interactable = true;
}
UpdateCurrentPointLabel();
}
public void OnChangeParameterHandler(string parameterId, object newValue, bool isValueValid = true) {
if (!isValueValid) {
SaveParametersBtn.SetInteractivity(false, "Some parameter has invalid value");
} else if (CurrentObject.TryGetParameter(parameterId, out IO.Swagger.Model.Parameter parameter)) {
try {
if (JsonConvert.SerializeObject(newValue) != parameter.Value) {
parametersChanged = true;
SaveParametersBtn.SetInteractivity(true);
}
} catch (JsonReaderException) {
SaveParametersBtn.SetInteractivity(false, "Some parameter has invalid value");
}

public void PreviousPoint() {
currentFocusPoint = Math.Max(currentFocusPoint - 1, 0);
NextButton.interactable = true;
if (currentFocusPoint == 0) {
PreviousButton.GetComponent<Button>().interactable = false;
} else {
PreviousButton.GetComponent<Button>().interactable = true;
}
UpdateCurrentPointLabel();
}

private void UpdateCurrentPointLabel() {
CurrentPointLabel.text = "Point " + (currentFocusPoint + 1) + " out of " + CurrentObject.ActionObjectMetadata.ObjectModel.Mesh.FocusPoints.Count.ToString();
}


public void OnVisibilityChange(float value) {
if (CurrentObject != null)
CurrentObject.SetVisibility(value / 100f);
}

public void ShowModelOnEE() {
if (model != null)
HideModelOnEE();
model = CurrentObject.GetModelCopy();
if (model == null)
return;
UpdateModelOnEE();
}


private void UpdateModelOnEE() {
if (model == null)
return;
string robotName = (string) RobotsList.GetValue(), eeId = (string) EndEffectorList.GetValue();
if (string.IsNullOrEmpty(robotName) || string.IsNullOrEmpty(eeId)) {
throw new RequestFailedException("Robot or end effector not selected!");
}

try {
string robotId = SceneManager.Instance.RobotNameToId(robotName);
RobotEE ee = SceneManager.Instance.GetRobot(robotId).GetEE(eeId);
model.transform.parent = ee.gameObject.transform;


switch ((PivotEnum) Enum.Parse(typeof(PivotEnum), (string) PivotList.GetValue())) {
case PivotEnum.Top:
model.transform.localPosition = new Vector3(0, model.transform.localScale.y / 2, 0);
break;
case PivotEnum.Bottom:
model.transform.localPosition = new Vector3(0, -model.transform.localScale.y / 2, 0);
break;
case PivotEnum.Middle:
model.transform.localPosition = new Vector3(0, 0, 0);
break;
}
model.transform.localRotation = new Quaternion(0, 0, 0, 1);
} catch (ItemNotFoundException ex) {
Debug.LogError(ex);
Notifications.Instance.ShowNotification("End-effector position unknown", "Robot did not send position of selected end effector");
ShowModelSwitch.Switch.isOn = false;
}

public void ShowNextAO() {
ActionObject nextAO = SceneManager.Instance.GetNextActionObject(CurrentObject.Data.Id);
ShowActionObject(nextAO);
}

public void HideModelOnEE() {
if (model != null) {
Destroy(model);
}
model = null;
public void ShowPreviousAO() {
ActionObject previousAO = SceneManager.Instance.GetNextActionObject(CurrentObject.Data.Id);
ShowActionObject(previousAO);
}

public void OnMenuStateChanged() {
switch (menu.CurrentState) {
case SimpleSideMenu.State.Closed:
HideModelOnEE();
break;
}
private static void ShowActionObject(ActionObject actionObject) {
actionObject.ShowMenu();
SceneManager.Instance.SetSelectedObject(actionObject.gameObject);
actionObject.SendMessage("Select", true);
}

}
Loading

0 comments on commit a4f9900

Please sign in to comment.