2025-10-24 11:55:43 +09:00
|
|
|
|
using System;
|
2025-10-23 18:43:38 +09:00
|
|
|
|
using System.Threading.Tasks;
|
2025-10-14 16:38:35 +09:00
|
|
|
|
using UnityEngine;
|
2025-10-24 11:55:43 +09:00
|
|
|
|
using UnityEngine.XR.ARSubsystems;
|
2025-10-14 16:38:35 +09:00
|
|
|
|
|
|
|
|
|
|
public class ProgramPresenter
|
|
|
|
|
|
{
|
|
|
|
|
|
private ProgramModel model;
|
2025-10-23 18:43:38 +09:00
|
|
|
|
private IProgramView view;
|
2025-10-24 14:36:33 +09:00
|
|
|
|
private TCPView tcpView;
|
2025-10-15 10:39:44 +09:00
|
|
|
|
private RobotController controlledRobot;
|
2025-10-23 18:43:38 +09:00
|
|
|
|
private string _programId;
|
2025-10-24 11:55:43 +09:00
|
|
|
|
private bool lastKnownMotorState = false;
|
2025-10-14 16:38:35 +09:00
|
|
|
|
|
2025-10-24 14:36:33 +09:00
|
|
|
|
public ProgramPresenter(ProgramModel model, IProgramView view, TCPView tcpView)
|
2025-10-14 16:38:35 +09:00
|
|
|
|
{
|
|
|
|
|
|
this.model = model;
|
|
|
|
|
|
this.view = view;
|
2025-10-24 14:36:33 +09:00
|
|
|
|
this.tcpView = tcpView;
|
2025-10-14 16:38:35 +09:00
|
|
|
|
|
2025-10-23 18:43:38 +09:00
|
|
|
|
this.view.OnCreateProgramClicked += async (id) => await HandleCreateProgram(id);
|
2025-10-14 16:38:35 +09:00
|
|
|
|
this.view.OnLoadProgramListRequested += HandleLoadProgramList;
|
|
|
|
|
|
this.view.OnProgramSelectedToLoad += HandleProgramSelected;
|
2025-10-23 18:43:38 +09:00
|
|
|
|
this.view.OnOpenProgramClicked += async () => await HandleOpenProgram();
|
2025-10-14 16:38:35 +09:00
|
|
|
|
this.view.OnSaveClicked += HandleSaveProgram;
|
|
|
|
|
|
this.view.OnAddPointClicked += HandleAddPoint;
|
2025-10-24 14:36:33 +09:00
|
|
|
|
this.tcpView.OnTCPupdateRequested += HandleTCPViewUpdate;
|
2025-10-14 16:38:35 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-15 10:39:44 +09:00
|
|
|
|
public void RegisterControlledRobot(RobotController robot)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.controlledRobot = robot;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-24 11:55:43 +09:00
|
|
|
|
public async Task UpdateMotorStateAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
bool currentState = await model.GetRobotMotorStateAsync();
|
|
|
|
|
|
|
|
|
|
|
|
if (currentState != lastKnownMotorState)
|
|
|
|
|
|
{
|
|
|
|
|
|
controlledRobot.SetMotorState(currentState);
|
|
|
|
|
|
lastKnownMotorState = currentState;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogWarning($"<22><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʈ <20><><EFBFBD><EFBFBD>: {e.Message}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-15 10:39:44 +09:00
|
|
|
|
public void OnApplicationStart()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (controlledRobot != null)
|
|
|
|
|
|
{
|
2025-10-24 11:55:43 +09:00
|
|
|
|
Debug.Log("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20>κ<EFBFBD><CEBA><EFBFBD> <20><><EFBFBD>ϵǾ<CFB5><C7BE><EFBFBD><EFBFBD>ϴ<EFBFBD>.");
|
2025-10-15 10:39:44 +09:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogError("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20>κ<EFBFBD><CEBA><EFBFBD> <20><><EFBFBD>ϵ<EFBFBD><CFB5><EFBFBD> <20>ʾҽ<CABE><D2BD>ϴ<EFBFBD>");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-24 14:36:33 +09:00
|
|
|
|
|
2025-10-23 18:43:38 +09:00
|
|
|
|
private async Task HandleCreateProgram(string programId)
|
2025-10-14 16:38:35 +09:00
|
|
|
|
{
|
2025-10-23 18:43:38 +09:00
|
|
|
|
if (await model.CreateNewProgram(programId))
|
2025-10-14 16:38:35 +09:00
|
|
|
|
{
|
2025-10-24 11:55:43 +09:00
|
|
|
|
view.DisplayProgram(programId);
|
2025-10-15 10:39:44 +09:00
|
|
|
|
view.HideProgramSelectPanel();
|
|
|
|
|
|
OnApplicationStart();
|
2025-10-14 16:38:35 +09:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
view.ShowMessage($"Error: Program ID '{programId}.job' already exists or is invalid.");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void HandleLoadProgramList()
|
|
|
|
|
|
{
|
|
|
|
|
|
var programIds = model.GetAllProgramIds();
|
|
|
|
|
|
view.ShowProgramList(programIds);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void HandleProgramSelected(string programId)
|
|
|
|
|
|
{
|
2025-10-23 18:43:38 +09:00
|
|
|
|
_programId = programId;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async Task HandleOpenProgram()
|
|
|
|
|
|
{
|
|
|
|
|
|
if(_programId != null && await model.LoadProgram(_programId))
|
2025-10-14 16:38:35 +09:00
|
|
|
|
{
|
2025-10-24 11:55:43 +09:00
|
|
|
|
view.DisplayProgram(_programId);
|
2025-10-15 10:39:44 +09:00
|
|
|
|
view.HideProgramSelectPanel();
|
|
|
|
|
|
view.HideProgramList();
|
|
|
|
|
|
|
|
|
|
|
|
OnApplicationStart();
|
2025-10-23 18:43:38 +09:00
|
|
|
|
|
|
|
|
|
|
_programId = null;
|
2025-10-14 16:38:35 +09:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void HandleSaveProgram()
|
|
|
|
|
|
{
|
2025-10-23 18:43:38 +09:00
|
|
|
|
//model.SaveCurrentProgram();
|
2025-10-14 16:38:35 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void HandleAddPoint()
|
|
|
|
|
|
{
|
2025-10-23 18:43:38 +09:00
|
|
|
|
//// <20><><EFBFBD><EFBFBD><EFBFBD>δ<EFBFBD> UI<55><49><EFBFBD><EFBFBD> <20><>ǥ<EFBFBD><C7A5> <20>κ<EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʈ <20><>ǥ<EFBFBD><C7A5> <20>ƿ;<C6BF> <20><>
|
|
|
|
|
|
//// <20><><EFBFBD>⼭<EFBFBD><E2BCAD> <20><><EFBFBD>÷<EFBFBD> <20><><EFBFBD><EFBFBD> <20><>ǥ<EFBFBD><C7A5> <20><><EFBFBD><EFBFBD>
|
|
|
|
|
|
//Vector3 newPoint = new Vector3(Random.Range(-1f, 1f), Random.Range(0f, 1f), Random.Range(-1f, 1f));
|
2025-10-14 16:38:35 +09:00
|
|
|
|
|
2025-10-23 18:43:38 +09:00
|
|
|
|
//model.AddPointToCurrentProgram(newPoint);
|
|
|
|
|
|
//view.DisplayProgram(model.CurrentProgram);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-24 14:36:33 +09:00
|
|
|
|
private void HandleTCPViewUpdate()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (model.IsNewDataAvailable())
|
|
|
|
|
|
{
|
|
|
|
|
|
RobotData data = model.GetLatestRobotData();
|
|
|
|
|
|
|
|
|
|
|
|
tcpView.SetCoordinates(data);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-10-23 18:43:38 +09:00
|
|
|
|
private void Destroy()
|
|
|
|
|
|
{
|
|
|
|
|
|
this.view.OnCreateProgramClicked -= async (id) => await HandleCreateProgram(id);
|
|
|
|
|
|
this.view.OnLoadProgramListRequested -= HandleLoadProgramList;
|
|
|
|
|
|
this.view.OnProgramSelectedToLoad -= HandleProgramSelected;
|
|
|
|
|
|
this.view.OnOpenProgramClicked -= async () => await HandleOpenProgram();
|
|
|
|
|
|
this.view.OnSaveClicked -= HandleSaveProgram;
|
|
|
|
|
|
this.view.OnAddPointClicked -= HandleAddPoint;
|
2025-10-24 14:36:33 +09:00
|
|
|
|
this.tcpView.OnTCPupdateRequested -= HandleTCPViewUpdate;
|
2025-10-14 16:38:35 +09:00
|
|
|
|
}
|
|
|
|
|
|
}
|