239 lines
8.3 KiB
C#
239 lines
8.3 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using WI;
|
|
using UnityEngine.UI;
|
|
using TMPro;
|
|
|
|
namespace CHN
|
|
{
|
|
|
|
public class Panel_ControlSetting : SettingPanel
|
|
{
|
|
KeyCode[] keyCodes;
|
|
|
|
OrbitalController orbitalController;
|
|
private OptionManager optionManager;
|
|
private Dictionary<string, UI_MappingButton> controlButton = new Dictionary<string, UI_MappingButton>();
|
|
private Dictionary<string, string> initControlOptions = new Dictionary<string, string>();
|
|
private Dictionary<string, string> mappingControlOptions = new Dictionary<string, string>();
|
|
|
|
private Dictionary<string, UI_SliderInputField> controlInputFields = new Dictionary<string, UI_SliderInputField>();
|
|
private Dictionary<string, string> initSpeedOptions = new Dictionary<string, string>();
|
|
private Dictionary<string, string> mappingSpeedOptions = new Dictionary<string, string>();
|
|
|
|
public UI_MappingButton currentClickButton;
|
|
public bool isMappingButtonClick;
|
|
|
|
public float minMoveSpeed;
|
|
public float maxMoveSpeed;
|
|
public float minRotateSpeed;
|
|
public float maxRotateSpeed;
|
|
public float minZoomSpeed;
|
|
public float maxZoomSpeed;
|
|
|
|
public void InitOptionSetting()
|
|
{
|
|
keyCodes = (KeyCode[])Enum.GetValues(typeof(KeyCode));
|
|
|
|
orbitalController = FindSingle<OrbitalController>();
|
|
optionManager = FindSingle<OptionManager>();
|
|
var controlOption = optionManager.GetOption(orbitalController);
|
|
|
|
var mappingButtons = GetComponentsInChildren<UI_MappingButton>();
|
|
foreach (var mappingButton in mappingButtons)
|
|
{
|
|
mappingButton.SetMappingButton();
|
|
|
|
controlButton.Add(mappingButton.button.name, mappingButton);
|
|
initControlOptions.Add(mappingButton.button.name, controlOption[mappingButton.button.name]);
|
|
mappingButton.Value.SetText(controlOption[mappingButton.button.name]);
|
|
mappingButton.onClickButton += OnClickButton;
|
|
}
|
|
|
|
var inputFields = GetComponentsInChildren<UI_SliderInputField>();
|
|
foreach(var inputField in inputFields)
|
|
{
|
|
inputField.SetSliderInputField();
|
|
|
|
controlInputFields.Add(inputField.name, inputField);
|
|
initSpeedOptions.Add(inputField.name, controlOption[inputField.name]);
|
|
SliderValueWithInRanage(inputField.name);
|
|
inputField.SetText(controlOption[inputField.name]);
|
|
}
|
|
}
|
|
|
|
private void OnClickButton(UI_MappingButton currentButton)
|
|
{
|
|
if (currentClickButton != null)
|
|
{
|
|
currentClickButton.ReductionLine();
|
|
currentClickButton.isClick = false;
|
|
}
|
|
|
|
isMappingButtonClick = true;
|
|
currentClickButton = currentButton;
|
|
currentClickButton.isClick = true;
|
|
currentClickButton.ExpandLine();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (!isMappingButtonClick)
|
|
return;
|
|
|
|
foreach (KeyCode keyCode in keyCodes)
|
|
{
|
|
if (IsMouseClick())
|
|
return;
|
|
|
|
if (Input.GetKeyDown(keyCode))
|
|
{
|
|
currentClickButton.Value.SetText(InputKeyCode(keyCode).ToString());
|
|
|
|
currentClickButton.Line.transform.localScale = new Vector3(0f,1f,1f);
|
|
currentClickButton.isClick = false;
|
|
isMappingButtonClick = false;
|
|
}
|
|
}
|
|
}
|
|
KeyCode InputKeyCode(KeyCode inputKeyCode)
|
|
{
|
|
foreach (var button in controlButton.Values)
|
|
{
|
|
var buttonText = button.Value;
|
|
|
|
if (buttonText.text == inputKeyCode.ToString())
|
|
{
|
|
return (KeyCode)Enum.Parse(typeof(KeyCode), currentClickButton.Value.text);
|
|
}
|
|
}
|
|
return inputKeyCode;
|
|
}
|
|
bool IsMouseClick()
|
|
{
|
|
bool isMouseClick = Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1) || Input.GetMouseButtonDown(2);
|
|
return isMouseClick;
|
|
}
|
|
|
|
public void SliderValueWithInRanage(string key)
|
|
{
|
|
float maxValue = 0;
|
|
float minValue = 0;
|
|
|
|
switch (key)
|
|
{
|
|
case "MoveSpeed":
|
|
maxValue = maxMoveSpeed;
|
|
minValue = minMoveSpeed;
|
|
break;
|
|
case "RotateSpeed":
|
|
maxValue = maxRotateSpeed;
|
|
minValue = minRotateSpeed;
|
|
break;
|
|
case "ZoomSpeed":
|
|
maxValue = maxZoomSpeed;
|
|
minValue = minRotateSpeed;
|
|
break;
|
|
}
|
|
controlInputFields[key].Slider.minValue = minValue;
|
|
controlInputFields[key].Slider.maxValue = maxValue;
|
|
|
|
}
|
|
public void AdjustTextValueWithinRange(string key)
|
|
{
|
|
float value = float.Parse(mappingSpeedOptions[key]);
|
|
|
|
float maxValue = 0;
|
|
float minValue = 0;
|
|
|
|
switch (key)
|
|
{
|
|
case "MoveSpeed":
|
|
maxValue = maxMoveSpeed;
|
|
minValue = minMoveSpeed;
|
|
break;
|
|
case "RotateSpeed":
|
|
maxValue = maxRotateSpeed;
|
|
minValue = minRotateSpeed;
|
|
break;
|
|
case "ZoomSpeed":
|
|
maxValue = maxZoomSpeed;
|
|
minValue = minRotateSpeed;
|
|
break;
|
|
}
|
|
|
|
float withinRangeValue = Mathf.Clamp(value, minValue, maxValue);
|
|
mappingSpeedOptions[key] = withinRangeValue.ToString();
|
|
controlInputFields[key].SetText(withinRangeValue.ToString());
|
|
}
|
|
|
|
internal override void Reset()
|
|
{
|
|
var optionManager = FindSingle<OptionManager>();
|
|
|
|
foreach (var key in initControlOptions.Keys)
|
|
{
|
|
controlButton[key].Value.text = initControlOptions[key];
|
|
}
|
|
optionManager.SetOptionValue(orbitalController, initControlOptions);
|
|
|
|
foreach (var key in initSpeedOptions.Keys)
|
|
{
|
|
controlInputFields[key].SetText(initSpeedOptions[key]);
|
|
}
|
|
optionManager.SetOptionValue(orbitalController, initSpeedOptions);
|
|
orbitalController.SetControllOptionValue();
|
|
}
|
|
|
|
internal override void Accept()
|
|
{
|
|
var optionManager = FindSingle<OptionManager>();
|
|
|
|
mappingControlOptions.Clear();
|
|
mappingSpeedOptions.Clear();
|
|
|
|
foreach (var controlButton in controlButton.Values)
|
|
{
|
|
mappingControlOptions.Add(controlButton.button.name, controlButton.Value.text);
|
|
}
|
|
optionManager.SetOptionValue(orbitalController, mappingControlOptions);
|
|
|
|
foreach (var controlInputField in controlInputFields.Values)
|
|
{
|
|
mappingSpeedOptions.Add(controlInputField.name, controlInputField.inputField.text);
|
|
AdjustTextValueWithinRange(controlInputField.name);
|
|
}
|
|
optionManager.SetOptionValue(orbitalController, mappingSpeedOptions);
|
|
orbitalController.SetControllOptionValue();
|
|
}
|
|
internal override void Open()
|
|
{
|
|
gameObject.SetActive(true);
|
|
}
|
|
internal override void Close()
|
|
{
|
|
isMappingButtonClick = false;
|
|
|
|
var controlOption = optionManager.GetOption(orbitalController);
|
|
foreach (var key in controlButton.Keys)
|
|
{
|
|
controlButton[key].Value.text = controlOption[key];
|
|
}
|
|
foreach (var key in controlInputFields.Keys)
|
|
{
|
|
controlInputFields[key].inputField.text = controlOption[key];
|
|
}
|
|
|
|
if (currentClickButton != null)
|
|
{
|
|
currentClickButton.Line.transform.localScale = new Vector3(0f, 1f, 1f);
|
|
currentClickButton.isClick = false;
|
|
}
|
|
|
|
gameObject.SetActive(false);
|
|
}
|
|
}
|
|
}
|