How to create a dialog box in the plot window.

25 visualizaciones (últimos 30 días)
Ruben
Ruben el 16 de Jul. de 2024
Comentada: Ruben el 16 de Jul. de 2024
As a personal project, I have been writing a game engine in Matlab. It will be a sort of text-based RPG, where you input your command into a text box and then the game interprets that. Right now, this is the state of my getCommand function:
function GetCommand(n)
sprintf("GetCommand %d", n)
pause(0.1)
String = input('Input \n', 's');
String = split(String);
Command = String{1,1};
Subject = String{2,1};
switch command
case 'go'
go(subject)
end
The idea is to run Command through a switch function to find that command, and run the command using Subject as its argument.
It currently works well enough. However, the input function requests input directly from Matlab's command line. I would like to request input from the plot window, which is where I'm rendering the game. I found inputdlg(), but that creates an entirely new window, which is not what I want either.
I hope I made myself clear. Is there any way to create a dialog box within the plot window?

Respuesta aceptada

Divyajyoti Nayak
Divyajyoti Nayak el 16 de Jul. de 2024
Hi @Ruben, you can use MATLAB’s ‘UIControl’ objects to build the user interface of your game. You can learn more about ‘UIControl’ objects from this documentation page:
To make the dialog box you require, you can use the editable field ‘UIControl’ object. Here is a simple code demonstrating it:
clear
clc
f = figure;
Label = uicontrol('Style','text','Units', 'normalized','Position',[0.4, 0.5, 0.2, 0.1]);
EditableField = uicontrol('Style','edit','Units','normalized','Position',[0.4, 0.45, 0.2, 0.1]);
button = uicontrol('Style','pushbutton','String','Enter', 'Units','normalized','Position',[0.45,0.4,0.1,0.05]);
Label.String = "Command Window";
button.Callback = {@onClick, EditableField};
%This function is called when buton is clicked
function onClick(source, event, field)
% Extracting the string from the editable text field
command = field.String;
% Call function to use command here
% Example: function(command)
end
And here’s the output for it:
Hope this helps!
  1 comentario
Ruben
Ruben el 16 de Jul. de 2024
This is awesome! Is there any way to make it so the text is retrieved upon clicking the "Enter" key? Other than that its perfect

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by