Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added basic monospaced text editor (for testing it can temporarily be…

… opened with F9)
  • Loading branch information...
commit 033abb28f0737bfa3a2b611764f5e90945c2b727 1 parent b3822ec
@henon authored
View
38 GameModeFortress/ManicDiggerGameWindow.cs
@@ -67,7 +67,9 @@ public partial class ManicDiggerGameWindow : IMyGameWindow, ILocalPlayerPosition
[Inject]
public ITerrainTextures d_TerrainTextures;
[Inject]
- public HudChat d_HudChat;
+ public HudChat d_HudChat;
+ [Inject]
+ public HudTextEditor d_HudTextEditor;
[Inject]
public HudInventory d_HudInventory;
[Inject]
@@ -264,8 +266,12 @@ void ManicDiggerGameWindow_KeyPress(object sender, OpenTK.KeyPressEventArgs e)
}
}
}
+ }
+ if (guistate == GuiState.EditText)
+ {
+ d_HudTextEditor.HandleKeyPress(sender, e);
}
- }
+ }
float overheadcameradistance = 10;
float tppcameradistance = 3;
void Mouse_WheelChanged(object sender, OpenTK.Input.MouseWheelEventArgs e)
@@ -708,8 +714,12 @@ void Keyboard_KeyDown(object sender, OpenTK.Input.KeyboardKeyEventArgs e)
if (ENABLE_LAG == 0) { Log("Frame rate: vsync."); }
if (ENABLE_LAG == 1) { Log("Frame rate: unlimited."); }
if (ENABLE_LAG == 2) { Log("Frame rate: lag simulation."); }
+ }
+ if (e.Key == OpenTK.Input.Key.F9)
+ {
+ guistate = GuiState.EditText;
}
- if (e.Key == GetKey(OpenTK.Input.Key.F12))
+ if (e.Key == GetKey(OpenTK.Input.Key.F12))
{
d_Screenshot.SaveScreenshot();
screenshotflash = 5;
@@ -788,7 +798,14 @@ void Keyboard_KeyDown(object sender, OpenTK.Input.KeyboardKeyEventArgs e)
{
GuiStateBackToGame();
}
- }
+ }
+ else if (guistate == GuiState.EditText) {
+ if (e.Key == GetKey(OpenTK.Input.Key.Escape))
+ {
+ GuiStateBackToGame();
+ }
+ d_HudTextEditor.HandleKeyDown(sender, e);
+ }
else throw new Exception();
}
public int[] drawDistances = { 32, 64, 128, 256, 512 };
@@ -1179,7 +1196,8 @@ void FrameTick(FrameEventArgs e)
}
else if (guistate == GuiState.CraftingRecipes)
{
- }
+ }
+ else if (guistate == GuiState.EditText) { }
else throw new Exception();
float movespeednow = MoveSpeedNow();
Acceleration acceleration = new Acceleration();
@@ -2360,6 +2378,7 @@ enum GuiState
Inventory,
MapLoading,
CraftingRecipes,
+ EditText
}
private void DrawMouseCursor()
{
@@ -2424,6 +2443,11 @@ private void Draw2d()
{
DrawCraftingRecipes();
}
+ break;
+ case GuiState.EditText:
+ {
+ d_HudTextEditor.Render();
+ }
break;
default:
throw new Exception();
@@ -2462,8 +2486,8 @@ private void Draw2d()
d_The3d.Draw2dText(((int)lagSeconds).ToString(), Width - 100, 50 + 50 + 10, 12, Color.White);
}
d_The3d.PerspectiveMode();
- }
- public int DISCONNECTED_ICON_AFTER_SECONDS = 10;
+ }
+ public int DISCONNECTED_ICON_AFTER_SECONDS = 10;
private void DrawScreenshotFlash()
{
d_The3d.Draw2dTexture(d_The3d.WhiteTexture(), 0, 0, Width, Height, null, Color.White);
View
4 GameModeFortress/Program.cs
@@ -263,8 +263,8 @@ private void MakeGame()
{
UseShadowsSimple();
}
- w.d_HudChat = new ManicDigger.Gui.HudChat() { d_Draw2d = the3d, d_ViewportSize = w };
-
+ w.d_HudChat = new ManicDigger.Gui.HudChat() { d_Draw2d = the3d, d_ViewportSize = w };
+ w.d_HudTextEditor = new HudTextEditor() { d_ViewportSize = w };
var dataItems = new GameDataItemsBlocks() { d_Data = gamedata };
var inventoryController = clientgame;
var inventoryUtil = new InventoryUtil();
View
363 ManicDigger/Hud/HudTextEditor.cs
@@ -0,0 +1,363 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Drawing;
+using System.Text;
+using ManicDigger.Renderers;
+using OpenTK;
+using OpenTK.Input;
+
+namespace ManicDigger.Hud
+{
+ public class HudTextEditor
+ {
+
+ public HudTextEditor()
+ {
+ d_The3d = new The3d
+ {
+ d_Config3d = new Config3d(),
+ d_TextRenderer = new MonospacedTextRenderer()
+ };
+ CHARACTER_WIDTH = d_The3d.d_TextRenderer.MeasureTextSize(new string('M', 100), FONTSIZE).Width / 100f; // <--- measuring 100 gives a better result than measuring a single char
+ CHARACTER_HEIGHT = d_The3d.d_TextRenderer.MeasureTextSize("gG", FONTSIZE).Height;
+ }
+
+ The3d d_The3d;
+
+ public const float FONTSIZE = 9f;
+ public const float LINESPACING = 4f;
+ public readonly Color CURSOR_COLOR = Color.Black;
+ public readonly Color TEXT_COLOR = Color.Black;
+ public const float MARGIN_RIGHT = 20;
+ public const float MARGIN_TOP = 20;
+ public float CHARACTER_WIDTH, CHARACTER_HEIGHT;
+ public float CURSOR_OFFSET_X = 1.0f;
+
+ [Inject]
+ public IViewportSize d_ViewportSize;
+
+ private TextEditor m_editor = new TextEditor();
+
+ public void SetText(string text)
+ {
+ m_editor.Text = text;
+ }
+
+ public string GetText()
+ {
+ return m_editor.Text;
+ }
+
+ public void Render()
+ {
+ if (d_The3d != null)
+ {
+ DrawEditorBackground();
+ DrawVisibleLines();
+ DrawCursor();
+ }
+ }
+
+ private void DrawCursor()
+ {
+ var cursor_x = MARGIN_RIGHT + m_editor.CursorCol * CHARACTER_WIDTH + CURSOR_OFFSET_X;
+ var cursor_y = MARGIN_TOP + m_editor.CursorRow * (FONTSIZE + LINESPACING) - LINESPACING / 2;
+ d_The3d.Draw2dTexture(d_The3d.WhiteTexture(), cursor_x, cursor_y, 1, FONTSIZE * 2, null, CURSOR_COLOR);
+ }
+
+ private void DrawEditorBackground()
+ {
+ d_The3d.Draw2dTexture(d_The3d.WhiteTexture(), 0, 0, d_ViewportSize.Width, d_ViewportSize.Height, null, new FastColor(200, 250, 250, 250).ToColor());
+ }
+
+ private void DrawVisibleLines()
+ {
+ float y = 0;
+ foreach (var line in m_editor.Lines)
+ {
+ d_The3d.Draw2dText(line.Text, MARGIN_RIGHT, MARGIN_TOP + y, FONTSIZE, TEXT_COLOR);
+ y += FONTSIZE + LINESPACING;
+ }
+ }
+
+ public void HandleKeyDown(object sender, KeyboardKeyEventArgs e)
+ {
+ switch (e.Key)
+ {
+ case Key.Up:
+ m_editor.MoveCursorUpDown(-1);
+ break;
+ case Key.Down:
+ m_editor.MoveCursorUpDown(1);
+ break;
+ case Key.Right:
+ m_editor.MoveCursorLeftRight(1);
+ break;
+ case Key.Left:
+ m_editor.MoveCursorLeftRight(-1);
+ break;
+ case Key.BackSpace:
+ m_editor.BackSpace();
+ break;
+ case Key.Delete:
+ m_editor.Delete();
+ break;
+ case Key.Home:
+ m_editor.SetCursor(0, m_editor.CursorRow);
+ break;
+ case Key.End:
+ m_editor.GoToEndOfLine();
+ break;
+ }
+ }
+
+ public void HandleKeyPress(object sender, KeyPressEventArgs e)
+ {
+ char c = e.KeyChar;
+ if (c == '\r' || c == '\n')
+ {
+ m_editor.InsertCarriageReturn();
+ return;
+ }
+ if (c == '\t')
+ {
+ m_editor.InsertTab();
+ return;
+ }
+ if (char.IsLetterOrDigit(c) || char.IsWhiteSpace(c) || char.IsPunctuation(c) || char.IsSeparator(c) || char.IsSymbol(c))
+ {
+ m_editor.Insert(e.KeyChar.ToString());
+ return;
+ }
+ }
+ }
+
+ //Text editor model without any GUI dependencies
+ public class TextEditor
+ {
+ public const string TAB = " ";
+
+ List<TextLine> m_lines = new List<TextLine>() { new TextLine("Hello World!"), new TextLine("bla bla bla") };
+ public IEnumerable<TextLine> Lines { get { return m_lines; } }
+
+ public int CursorRow = 0;
+ public int CursorCol = 0;
+
+ public string Text
+ {
+ set
+ {
+ m_lines.Clear();
+ if (value == null)
+ return;
+ foreach (var line in value.Split('\r', '\n'))
+ m_lines.Add(new TextLine(line));
+ }
+ get
+ {
+ var text = new StringBuilder();
+ foreach (var line in m_lines)
+ text.AppendLine(line.Text);
+ return text.ToString();
+ }
+ }
+
+ public void MoveCursorLeftRight(int delta_col)
+ {
+ CursorCol += delta_col;
+ if (CursorCol < 0) // jump to end of previous row
+ {
+ if (CursorRow > 0)
+ {
+ CursorRow -= 1;
+ CursorCol = GetLineLength(CursorRow);
+ }
+ else
+ CursorCol = 0;
+ }
+ else if (CursorCol > GetLineLength(CursorRow)) // jump to start of next row
+ {
+ CursorRow += 1;
+ CursorCol = GetLineLength(CursorRow);
+ }
+ }
+
+ public void MoveCursorUpDown(int delta_row)
+ {
+ CursorRow += delta_row;
+ if (CursorRow < 0)
+ CursorRow = 0;
+ if (GetLineLength(CursorRow) < CursorCol)
+ CursorCol = GetLineLength(CursorRow); // reset to end of line because cursor is beyond the end of line (no "virtual space")
+ }
+
+ private int GetLineLength(int row)
+ {
+ if (row < 0)
+ return 0;
+ if (m_lines.Count <= row)
+ return 0;
+ return m_lines[row].Length;
+ }
+
+ public void Insert(string s)
+ {
+ var line = GetLine(CursorRow);
+ line.Insert(CursorCol, s);
+ CursorCol += s.Length;
+ }
+
+ public void InsertCarriageReturn()
+ {
+ var line = GetLine(CursorRow);
+ string removed = "";
+ if (line.Length > 0 && CursorCol < line.Length)
+ {
+ removed = line.GetSegment(CursorCol, line.Length);
+ line.Remove(CursorCol, line.Length);
+ }
+ CursorRow += 1;
+ if (CursorRow > m_lines.Count)
+ GetLine(CursorRow); // make sure empty lines are generated up to the insertion row index
+ m_lines.Insert(CursorRow, new TextLine(removed));
+ CursorCol = 0;
+ }
+
+ private TextLine GetLine(int row)
+ {
+ if (row < 0)
+ row = 0;
+ if (m_lines.Count <= row)
+ {
+ for (int i = m_lines.Count; i <= row; i++)
+ m_lines.Add(new TextLine(""));
+ }
+ var line = m_lines[row] ?? (m_lines[row] = new TextLine(""));
+ return line;
+ }
+
+ public void InsertTab()
+ {
+ Insert(TAB);
+ }
+
+ public void BackSpace()
+ {
+ if (CursorCol == 0 && CursorRow == 0)
+ return;
+ var line = GetLine(CursorRow);
+ if (CursorCol > 0)
+ {
+ line.Remove(CursorCol - 1, CursorCol);
+ CursorCol -= 1;
+ }
+ else
+ {
+ m_lines.RemoveAt(CursorRow);
+ CursorRow -= 1;
+ var line_above = GetLine(CursorRow);
+ var length = line_above.Length;
+ line_above.Append(line.Text);
+ CursorCol = length;
+ }
+ }
+
+ public void Delete()
+ {
+ var line = GetLine(CursorRow);
+ if (CursorRow >= m_lines.Count && CursorCol <= line.Length)
+ return;
+ if (CursorCol < line.Length)
+ {
+ line.Remove(CursorCol, CursorCol+1);
+ }
+ else
+ {
+ var line_below = GetLine(CursorRow+1);
+ m_lines.RemoveAt(CursorRow + 1);
+ line.Append(line_below.Text);
+ }
+ }
+
+ public void SetCursor(int col, int row)
+ {
+ CursorCol=col;
+ CursorRow=row;
+ CheckCursorPosition();
+ // TODO: scroll
+ }
+
+ private void CheckCursorPosition()
+ {
+ if (CursorCol < 0)
+ CursorCol = 0;
+ if (CursorRow < 0)
+ CursorRow = 0;
+ CursorCol = Math.Min(CursorCol, GetLine(CursorRow).Length);
+ }
+
+ public void GoToEndOfLine()
+ {
+ CursorCol = GetLine(CursorRow).Length;
+ // TODO: scroll
+ }
+ }
+
+ public class TextLine
+ {
+ public TextLine(string text)
+ {
+ m_text = new StringBuilder(text);
+ }
+
+ StringBuilder m_text;
+ public string Text
+ {
+ get { return m_text.ToString(); }
+ set
+ {
+ m_text.Remove(0, m_text.Length);
+ m_text.Append(value);
+ }
+ }
+
+ public int Length
+ {
+ get { return m_text.Length; }
+ }
+
+ public void Insert(int col, string s)
+ {
+ m_text.Insert(col, s);
+ }
+
+ public string GetSegment(int col_start, int col_end)
+ {
+ Debug.Assert(col_end >= col_start);
+ col_end = Math.Min(m_text.Length, col_end);
+ if (col_start == col_end || col_start > m_text.Length)
+ return "";
+ var s = new StringBuilder(col_end - col_start);
+ for (int i = col_start; i < col_end; i++)
+ {
+ s.Append(m_text[i]);
+ }
+ return s.ToString();
+ }
+
+ public void Remove(int col_start, int col_end)
+ {
+ Debug.Assert(col_end >= col_start);
+ if (col_start == col_end)
+ return;
+ m_text.Remove(col_start, col_end - col_start);
+ }
+
+ public void Append(string text)
+ {
+ m_text.Append(text);
+ }
+ }
+}
View
13 ManicDigger/ManicDigger.csproj
@@ -78,6 +78,7 @@
<Compile Include="Hud\Hud.cs" />
<Compile Include="Hud\HudChat.cs" />
<Compile Include="Hud\HudInventory.cs" />
+ <Compile Include="Hud\HudTextEditor.cs" />
<Compile Include="Interpolation.cs" />
<Compile Include="Inventory.cs" />
<Compile Include="IterableSet.cs" />
@@ -140,11 +141,11 @@
<None Include="c.snk" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
- <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
- Other similar extension points exist, see Microsoft.Common.targets.
- <Target Name="BeforeBuild">
- </Target>
- <Target Name="AfterBuild">
- </Target>
+ <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
+ Other similar extension points exist, see Microsoft.Common.targets.
+ <Target Name="BeforeBuild">
+ </Target>
+ <Target Name="AfterBuild">
+ </Target>
-->
</Project>

0 comments on commit 033abb2

Please sign in to comment.
Something went wrong with that request. Please try again.