Добавил форму-меню

This commit is contained in:
DIvan2000 2024-12-14 11:21:47 +04:00
parent 2d28599940
commit 6256105b42
7 changed files with 277 additions and 32 deletions

View File

@ -10,12 +10,15 @@ namespace laba3.Core
/// <summary> /// <summary>
/// TUI menu; /// TUI menu;
/// </summary> /// </summary>
internal class Menu public class Menu
{ {
private List<Option> options; private List<MenuOption> options;
private int selected; private int selected;
private string title; private string title;
public List<MenuOption> GetOptions() { return options; }
public string GetTitle() { return title; }
/// <summary> /// <summary>
/// Constructor of menu; /// Constructor of menu;
/// </summary> /// </summary>
@ -23,7 +26,7 @@ namespace laba3.Core
public Menu(string title) public Menu(string title)
{ {
this.title = title; this.title = title;
this.options = new List<Option> { }; this.options = new List<MenuOption> { };
} }
/// <summary> /// <summary>
@ -33,7 +36,7 @@ namespace laba3.Core
/// <param name="action">Action, runs if option selected</param> /// <param name="action">Action, runs if option selected</param>
public void AddOption(string name, Action action) public void AddOption(string name, Action action)
{ {
this.options.Add(new Option(name, action)); this.options.Add(new MenuOption(name, action));
} }
/// <summary> /// <summary>
@ -77,7 +80,7 @@ namespace laba3.Core
Console.Clear(); Console.Clear();
Console.WriteLine(title); Console.WriteLine(title);
int optionIndex = 0; int optionIndex = 0;
foreach (Option option in options) foreach (MenuOption option in options)
{ {
string pointer = optionIndex == selected ? " ->" : " "; string pointer = optionIndex == selected ? " ->" : " ";
Console.WriteLine($"{pointer}{option.Name}"); Console.WriteLine($"{pointer}{option.Name}");
@ -85,12 +88,12 @@ namespace laba3.Core
} }
} }
private struct Option public struct MenuOption
{ {
public string Name { get; } public string Name { get; }
public Action Action { get; } public Action Action { get; }
public Option(string name, Action action) public MenuOption(string name, Action action)
{ {
Name = name; Name = name;
Action = action; Action = action;

View File

@ -1,35 +1,41 @@
using laba3.Core; using laba3.Core;
using laba3.Subprograms; using laba3.Subprograms;
using ProgLab1.GUI;
using System; using System;
class Program class Program
{ {
static MenuForm mainMenuForm;
static MenuForm snakeDiffMenuForm;
static MenuForm snakeSizeMenuForm;
[STAThread] // Требуется для Windows Forms
static void Main() static void Main()
{ {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
int sizex = 20;
int sizey = 20;
SnakeGame.Level difficulty = 0;
Menu sizeMenu = new Menu("Select world size");
sizeMenu.AddOption("Small size (10x10)", () => { sizex = 10; sizey = 10; });
sizeMenu.AddOption("Medium size (20x20)", () => { sizex = 20; sizey = 20; });
sizeMenu.AddOption("Big size (40x20)", () => { sizex = 40; sizey = 20; });
Menu difficultyMenu = new Menu("Select difficulty");
difficultyMenu.AddOption("Easy", () => { difficulty = SnakeGame.Level.Low; });
difficultyMenu.AddOption("Medium", () => { difficulty = SnakeGame.Level.Medium; });
difficultyMenu.AddOption("Hard", () => { difficulty = SnakeGame.Level.High; });
Menu mainMenu = new Menu("Select option"); Menu mainMenu = new Menu("Select option");
mainMenu.AddOption("Guess answer math game", () => GuessAnswerMath.RunGame()); mainMenu.AddOption("Guess answer math game", () => GuessAnswerMath.RunGame());
mainMenu.AddOption("About me", () => PrintAboutMe()); mainMenu.AddOption("About me", () => PrintAboutMe());
mainMenu.AddOption("Array sort", () => new ArraySortDemo().Run()); mainMenu.AddOption("Array sort", () => new ArraySortDemo().Run());
mainMenu.AddOption("Snake game", () => { mainMenu.AddOption("Snake game", () => {
int sizex = 0;
int sizey = 0;
SnakeGame.Level difficulty = 0;
Menu sizeMenu = new Menu("Select world size");
sizeMenu.AddOption("Small size (10x10)", () => { sizex = 10; sizey = 10; });
sizeMenu.AddOption("Medium size (20x20)", () => { sizex = 20; sizey = 20; });
sizeMenu.AddOption("Big size (40x20)", () => { sizex = 40; sizey = 20; });
sizeMenu.RunMenu();
Menu difficultyMenu = new Menu("Select difficulty");
difficultyMenu.AddOption("Easy", () => { difficulty = SnakeGame.Level.Low; });
difficultyMenu.AddOption("Medium", () => { difficulty = SnakeGame.Level.Medium; });
difficultyMenu.AddOption("Hard", () => { difficulty = SnakeGame.Level.High; });
difficultyMenu.RunMenu();
SnakeGame game = new SnakeGame(difficulty, sizex, sizey); SnakeGame game = new SnakeGame(difficulty, sizex, sizey);
game.start(); game.start();
}); });
mainMenu.AddOption("Exit", () => Exit()); mainMenu.AddOption("Exit", () => Exit());
@ -37,13 +43,13 @@ class Program
Utils.Arrays withOneParam = new Utils.Arrays(10); Utils.Arrays withOneParam = new Utils.Arrays(10);
Utils.Arrays withTwoParams = new Utils.Arrays(10, 100); Utils.Arrays withTwoParams = new Utils.Arrays(10, 100);
while (true) mainMenuForm = new MenuForm(mainMenu);
{ snakeDiffMenuForm = new MenuForm(difficultyMenu);
mainMenu.RunMenu(); snakeSizeMenuForm = new MenuForm(sizeMenu);
Console.WriteLine("Press Enter to continue");
while (Console.ReadKey().Key != ConsoleKey.Enter) { } Application.Run(mainMenuForm);
Console.Clear();
}
} }
private static void PrintAboutMe() private static void PrintAboutMe()

39
GUI/MenuForm.Designer.cs generated Normal file
View File

@ -0,0 +1,39 @@
namespace ProgLab1.GUI
{
partial class MenuForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Text = "MenuForm";
}
#endregion
}
}

70
GUI/MenuForm.cs Normal file
View File

@ -0,0 +1,70 @@
using laba3.Core;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static laba3.Core.Menu;
namespace ProgLab1.GUI
{
public partial class MenuForm : Form
{
public MenuForm(Menu consoleMenu)
{
InitializeComponent();
this.Width = 800;
this.Height = 600;
AddMenu(consoleMenu);
}
/// <summary>
/// Adds menu to GUI
/// </summary>
/// <param name="consoleMenu">TUI menu</param>
private void AddMenu(Menu consoleMenu)
{
FlowLayoutPanel buttonPanel = new FlowLayoutPanel
{
Dock = DockStyle.Fill, // Растягиваем на всю форму
FlowDirection = FlowDirection.TopDown, // Кнопки располагаются сверху вниз
WrapContents = false // Запрет переноса
};
Label menuLabel = new Label
{
Text = consoleMenu.GetTitle(), // Текст заголовка
Font = new System.Drawing.Font("Arial", 16, System.Drawing.FontStyle.Bold), // Стиль шрифта
TextAlign = System.Drawing.ContentAlignment.MiddleCenter, // Выравнивание текста по центру
Dock = DockStyle.Top, // Заголовок сверху
Height = 40 // Высота заголовка
};
buttonPanel.Controls.Add(menuLabel);
foreach (MenuOption option in consoleMenu.GetOptions())
{
Button button = new Button
{
Text = option.Name,
Width = buttonPanel.Width - 10,
Height = 40,
Margin = new Padding(5),
};
// Связываем обработчик с OnSelect из консольного меню
button.Click += (sender, e) => option.Action();
buttonPanel.Controls.Add(button);
}
this.Controls.Add(buttonPanel);
}
}
}

120
GUI/MenuForm.resx Normal file
View File

@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

BIN
GUI/background.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 506 KiB

View File

@ -2,9 +2,16 @@
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework> <TargetFramework>net8.0-windows10.0.17763.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<UseWindowsForms>True</UseWindowsForms>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<None Update="GUI\background.jpg">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project> </Project>