A game framework written with osu! in mind.
at master 2.1 kB view raw
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. 2// See the LICENCE file in the repository root for full licence text. 3 4using System; 5using osu.Framework.Platform; 6 7namespace osu.Framework.Input 8{ 9 public class GameWindowTextInput : ITextInputSource 10 { 11 private readonly IWindow window; 12 13 private string pending = string.Empty; 14 15 public GameWindowTextInput(IWindow window) 16 { 17 this.window = window; 18 } 19 20 protected virtual void HandleKeyPress(object sender, osuTK.KeyPressEventArgs e) => pending += e.KeyChar; 21 22 protected virtual void HandleKeyTyped(char c) => pending += c; 23 24 public bool ImeActive => false; 25 26 public string GetPendingText() 27 { 28 try 29 { 30 return pending; 31 } 32 finally 33 { 34 pending = string.Empty; 35 } 36 } 37 38 public void Deactivate() 39 { 40 switch (window) 41 { 42 case SDL2DesktopWindow win: 43 win.KeyTyped -= HandleKeyTyped; 44 break; 45 46 case OsuTKWindow tkWin: 47 tkWin.KeyPress -= HandleKeyPress; 48 break; 49 } 50 } 51 52 public void Activate() 53 { 54 switch (window) 55 { 56 case SDL2DesktopWindow win: 57 win.KeyTyped += HandleKeyTyped; 58 break; 59 60 case OsuTKWindow tkWin: 61 tkWin.KeyPress += HandleKeyPress; 62 break; 63 } 64 } 65 66 public void EnsureActivated() 67 { 68 } 69 70 private void imeCompose() 71 { 72 //todo: implement 73 OnNewImeComposition?.Invoke(string.Empty); 74 } 75 76 private void imeResult() 77 { 78 //todo: implement 79 OnNewImeResult?.Invoke(string.Empty); 80 } 81 82 public event Action<string> OnNewImeComposition; 83 public event Action<string> OnNewImeResult; 84 } 85}