From 0338afe3cfddec51d4e80e6c2dd3927c75c06576 Mon Sep 17 00:00:00 2001 From: Devin Date: Tue, 16 Nov 2021 19:47:54 +0100 Subject: [PATCH] Quick and dirty customisation of keyboard controller for lists --- .../Shop/Controller/ListKeyboardController.cs | 85 +++++++++++++++++++ .../Controller/ListKeyboardController.cs.meta | 12 +++ Assets/Scripts/Shop/View/ShopView.cs | 18 +--- Assets/Scripts/Shop/View/ShopViewGrid.cs | 16 ++++ Assets/Scripts/Shop/View/ShopViewList.cs | 16 ++++ 5 files changed, 132 insertions(+), 15 deletions(-) create mode 100644 Assets/Scripts/Shop/Controller/ListKeyboardController.cs create mode 100644 Assets/Scripts/Shop/Controller/ListKeyboardController.cs.meta diff --git a/Assets/Scripts/Shop/Controller/ListKeyboardController.cs b/Assets/Scripts/Shop/Controller/ListKeyboardController.cs new file mode 100644 index 0000000..6b2288a --- /dev/null +++ b/Assets/Scripts/Shop/Controller/ListKeyboardController.cs @@ -0,0 +1,85 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +/// +/// This class provides a keyboard controller for a ShopModel in a list view, it defines how to handle keyboard input in HandleInput() +/// +public class ListKeyboardController : ShopController +{ + private ViewConfig viewConfig;//To move the focus up and down, we need to know how many columns the grid view has, in the current setup, + private int columnCount; //this information can be found in a ViewConfig scriptable object, which serves as a configuration file for + //views. + + private int currentItemIndex = 0;//The current item index is changed whenever the focus is moved with keyboard keys + + //------------------------------------------------------------------------------------------------------------------------ + // Initialize() + //------------------------------------------------------------------------------------------------------------------------ + //Override Initialize to set up additional information needed by this concrete controller: number of columns in the view + public override ShopController Initialize(ShopModel pShopModel) + { + base.Initialize(pShopModel);//Call base.Initialize to set up the model + currentItemIndex = model.GetSelectedItemIndex();//Synchronize the current item index with the model + viewConfig = Resources.Load("ViewConfig");//Load the ViewConfig scriptable object from the Resources folder + Debug.Assert(viewConfig != null); + columnCount = viewConfig.gridViewColumnCount;//Try to set up the column count, fails silently + return this; + } + + //------------------------------------------------------------------------------------------------------------------------ + // HandleInput() + //------------------------------------------------------------------------------------------------------------------------ + //Currently hardcoded to AWSD to move focus and K to confirm the selected item. No criterion for the grade, so since lists are assumed to have one colum, we just hardcode that + public override void HandleInput() + { + //Move the focus to the left if possible + if (Input.GetKeyDown(KeyCode.A)) + { + currentItemIndex--; + if (currentItemIndex < 0) + { + currentItemIndex = 0; + } + } + + //Move the focus to the right if possible + if (Input.GetKeyDown(KeyCode.D)) + { + currentItemIndex++; + if (currentItemIndex >= this.Model.inventory.GetItemCount()) + { + currentItemIndex = this.Model.inventory.GetItemCount() - 1; + } + } + + //Move the focus up if possible + if (Input.GetKeyDown(KeyCode.W)) + { + currentItemIndex--; + if (currentItemIndex < 0) + { + currentItemIndex = 0; + } + } + + //Move the focus down if possible + if (Input.GetKeyDown(KeyCode.S)) + { + currentItemIndex++; + if (currentItemIndex >= this.Model.inventory.GetItemCount()) + { + currentItemIndex = this.Model.inventory.GetItemCount() - 1; + } + } + + //Select the item + SelectItemByIndex(currentItemIndex); + + //Confirm the selected item when K is pressed + if (Input.GetKeyDown(KeyCode.K)) + { + ConfirmSelectedItem(); + } + } +} diff --git a/Assets/Scripts/Shop/Controller/ListKeyboardController.cs.meta b/Assets/Scripts/Shop/Controller/ListKeyboardController.cs.meta new file mode 100644 index 0000000..d54ee90 --- /dev/null +++ b/Assets/Scripts/Shop/Controller/ListKeyboardController.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 84679d97889a19f4286eeae8f2f4e66a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: + - viewConfig: {fileID: 11400000, guid: 5d1182a6d5a724428b8167840f0dfa92, type: 2} + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Shop/View/ShopView.cs b/Assets/Scripts/Shop/View/ShopView.cs index 5c40ef0..c9d10b8 100644 --- a/Assets/Scripts/Shop/View/ShopView.cs +++ b/Assets/Scripts/Shop/View/ShopView.cs @@ -31,7 +31,7 @@ public abstract class ShopView : MonoBehaviour protected ShopModel model; // Model in MVC pattern protected ShopModel other; // Other model in MVC pattern (our own inventory) - private ShopController shopController; //Controller in MVC pattern + protected ShopController shopController; //Controller in MVC pattern private ItemType itemFilter = ItemType.All; // View can filter items, and this is the filter we want to use for that @@ -149,24 +149,12 @@ public abstract class ShopView : MonoBehaviour //------------------------------------------------------------------------------------------------------------------------ // SwitchToKeyboardControl() //------------------------------------------------------------------------------------------------------------------------ - private void SwitchToKeyboardControl() - { - Destroy(shopController);//Remove the current controller component - shopController = gameObject.AddComponent().Initialize(model);//Create and add a keyboard controller - instructionText.text = "The current control mode is: Keyboard Control, WASD to select item, press K to buy. Press left mouse button to switch to Mouse Control."; - buyButton.gameObject.SetActive(false);//Hide the buy button because we only use keyboard - } + protected abstract void SwitchToKeyboardControl(); //------------------------------------------------------------------------------------------------------------------------ // SwitchToMouseControl() //------------------------------------------------------------------------------------------------------------------------ - private void SwitchToMouseControl() - { - Destroy(shopController);//Remove the current controller component - shopController = gameObject.AddComponent().Initialize(model);//Create and add a mouse controller - instructionText.text = "The current control mode is: Mouse Control, press 'K' to switch to Keyboard Control."; - buyButton.gameObject.SetActive(true);//Show the buy button for the mouse controler - } + protected abstract void SwitchToMouseControl(); // These three are from the observable interface, and it was decided to leave this stuff to the implementation for now. Keeping here for reference // public virtual void OnSelected(Item item) diff --git a/Assets/Scripts/Shop/View/ShopViewGrid.cs b/Assets/Scripts/Shop/View/ShopViewGrid.cs index 39935b1..3f81178 100644 --- a/Assets/Scripts/Shop/View/ShopViewGrid.cs +++ b/Assets/Scripts/Shop/View/ShopViewGrid.cs @@ -68,6 +68,22 @@ public class ShopViewGrid : ShopView } } + protected override void SwitchToKeyboardControl() + { + Destroy(shopController);//Remove the current controller component + shopController = gameObject.AddComponent().Initialize(model);//Create and add a keyboard controller + instructionText.text = "The current control mode is: Keyboard Control, WASD to select item, press K to buy. Press left mouse button to switch to Mouse Control."; + buyButton.gameObject.SetActive(false);//Hide the buy button because we only use keyboard + } + + protected override void SwitchToMouseControl() + { + Destroy(shopController);//Remove the current controller component + shopController = gameObject.AddComponent().Initialize(model);//Create and add a mouse controller + instructionText.text = "The current control mode is: Mouse Control, press 'K' to switch to Keyboard Control."; + buyButton.gameObject.SetActive(true);//Show the buy button for the mouse controler + } + private void OnValidate() { _gridLayoutGroup = (GridLayoutGroup) layoutGroup; diff --git a/Assets/Scripts/Shop/View/ShopViewList.cs b/Assets/Scripts/Shop/View/ShopViewList.cs index 982cba1..6bc984a 100644 --- a/Assets/Scripts/Shop/View/ShopViewList.cs +++ b/Assets/Scripts/Shop/View/ShopViewList.cs @@ -77,6 +77,22 @@ public class ShopViewList : ShopView, IShopModelObserver } } + protected override void SwitchToKeyboardControl() + { + Destroy(shopController);//Remove the current controller component + shopController = gameObject.AddComponent().Initialize(model);//Create and add a keyboard controller + instructionText.text = "The current control mode is: Keyboard Control, WASD to select item, press K to buy. Press left mouse button to switch to Mouse Control."; + buyButton.gameObject.SetActive(false);//Hide the buy button because we only use keyboard + } + + protected override void SwitchToMouseControl() + { + Destroy(shopController);//Remove the current controller component + shopController = gameObject.AddComponent().Initialize(model);//Create and add a mouse controller + instructionText.text = "The current control mode is: Mouse Control, press 'K' to switch to Keyboard Control."; + buyButton.gameObject.SetActive(true);//Show the buy button for the mouse controler + } + private void OnValidate() { _listLayoutGroup = (VerticalLayoutGroup) layoutGroup;