/**
* Copyright (c) 2021 Vuplex Inc. All rights reserved.
*
* Licensed under the Vuplex Commercial Software Library License, you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* https://vuplex.com/commercial-library-license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using UnityEngine;
namespace Vuplex.WebView
{
///
/// A 3D, on-screen keyboard that you can hook up to a webview for typing.
/// You add a Keyboard to your scene by dragging Keyboard.prefab into it
/// via the editor or by programmatically calling `Keyboard.Instantiate()`.
/// For use in a `Canvas`, please see `CanvasKeyboard` instead.
///
///
/// The Keyboard's UI is a React.js app that runs inside a `WebViewPrefab` instance and
/// emits messages to the C# to indicate when characters have been pressed.
/// [The keyboard UI is open source and available on GitHub](https://github.com/vuplex/unity-keyboard).
///
///
/// // First, create a WebViewPrefab for our main web content.
/// var webViewPrefab = WebViewPrefab.Instantiate(0.6f, 0.3f);
/// webViewPrefab.transform.parent = transform;
/// webViewPrefab.transform.localPosition = new Vector3(0, 0f, 0.4f);
/// webViewPrefab.transform.LookAt(transform);
/// webViewPrefab.Initialized += (sender, e) => {
/// webViewPrefab.WebView.LoadUrl("https://www.google.com");
/// };
/// // Add a Keyboard under the main webview.
/// var keyboard = Keyboard.Instantiate();
/// keyboard.transform.parent = webViewPrefab.transform;
/// keyboard.transform.localPosition = new Vector3(0, -0.31f, 0);
/// keyboard.transform.localEulerAngles = new Vector3(0, 0, 0);
/// // Hook up the keyboard so that characters are routed to the main webview.
/// keyboard.InputReceived += (sender, e) => {
/// webViewPrefab.WebView.HandleKeyboardInput(e.Value);
/// };
///
///
/// The keyboard supports layouts for the following languages and automatically sets the layout
/// based on the operating system's default language:
/// - English
/// - Spanish
/// - French
/// - German
/// - Russian
/// - Danish
/// - Norwegian
/// - Swedish
///
public class Keyboard : BaseKeyboard
{
///
/// Sets the keyboard's initial resolution in pixels per Unity unit.
/// You can change the resolution to make the keyboard's content appear larger or smaller.
/// For more information on scaling web content, see
/// [this support article](https://support.vuplex.com/articles/how-to-scale-web-content).
///
[Label("Initial Resolution (px / Unity unit)")]
[Tooltip("You can change this to make web content appear larger or smaller.")]
public float InitialResolution = 1300;
///
/// The `WebViewPrefab` instance used for the keyboard UI.
///
public WebViewPrefab WebViewPrefab
{
get
{
return (WebViewPrefab)_webViewPrefab;
}
}
[Obsolete("Keyboard.Init() has been removed. The Keyboard script now initializes itself automatically, so Init() no longer needs to be called.", true)]
public void Init(float width, float height) { }
void _initKeyboard()
{
var size = transform.localScale;
transform.localScale = Vector3.one;
var webViewPrefab = gameObject.GetComponentInChildren();
if (!webViewPrefab) return;
_webViewPrefab = webViewPrefab;
webViewPrefab.InitialResolution = InitialResolution;
_webViewPrefab.transform.SetParent(transform, false);
_webViewPrefab.gameObject.layer = gameObject.layer;
// Shift the WebViewPrefab up by half its height so that it's in the same place
// as the palceholder.
_webViewPrefab.transform.localPosition = Vector3.zero;
_webViewPrefab.transform.localEulerAngles = Vector3.zero;
_init();
// Disable the placeholder that is used in the editor.
var placeholder = transform.Find("Placeholder");
if (placeholder != null)
{
placeholder.gameObject.SetActive(false);
}
}
void Start()
{
_initKeyboard();
}
const float DEFAULT_KEYBOARD_WIDTH = 0.5f;
const float DEFAULT_KEYBOARD_HEIGHT = 0.125f;
}
}