How to create a dialog box in the plot window.

39 次查看(过去 30 天)
Ruben
Ruben 2024-7-16,3:07
评论: Ruben 2024-7-16,14:12
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?

回答(1 个)

Divyajyoti Nayak
Divyajyoti Nayak 2024-7-16,4:23
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 个评论
Ruben
Ruben 2024-7-16,14:12
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by