How to create a dialog box in the plot window.

25 Ansichten (letzte 30 Tage)
Ruben
Ruben am 16 Jul. 2024
Kommentiert: Ruben am 16 Jul. 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?

Akzeptierte Antwort

Divyajyoti Nayak
Divyajyoti Nayak am 16 Jul. 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 Kommentar
Ruben
Ruben am 16 Jul. 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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Migrate GUIDE Apps finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by