如何使用基本节点/类枚举?

发布于 2025-02-04 04:51:27 字数 1206 浏览 3 评论 0原文

也许这是一个非常愚蠢的问题,但我在Google上找不到答案。

Godot中有一个节点 - textedit。它有几个枚举。其中之一叫做Menuitems。其中之一是菜单_clear和灰色的“ = 3”。它的描述指出:“擦除整个文本文本。”

我的问题是:如何使用此信息清除整个文本文本?

我尝试简单地访问它,然后

$TextEdit.MENU_CLEAR -> nothing

尝试将其放置为很长的

$TextEdit.MenuItems.MENU_CLEAR -> nothing

尝试将其作为函数调用

$TextEdit.MENU_CLEAR() -> nothing

print($TextEdit.MENU_CLEAR) -> "3"

路,一旦我致电菜单_clear,就应该执行。但是我做错了什么。 PLS,帮助

编辑1: 这是我的一件代码(只需检查有效的方法,一次在一行时运行所有内容):

if $TextEdit.get_line_count() > 6:
    $TextEdit.text = ""
    $TextEdit.menu_option($TextEdit.MENU_CLEAR)

这是我想做的:

if $TextEdit.get_line_count() > 6:
    $TextEdit.select(1, 0, 1, 0)    #selects the oldest line
    $TextEdit.menu_option($TextEdit.MENU_CUT)    #deletes it

这是我现在得到的屏幕截图 请参阅右侧的滑块 这就是我想要的 只有6行 因此,屏幕上总是只有6行,并且应删除旧线。那是我遇到枚举问题的地方,这就是帖子的原因。 PS 仅阅读设置为false

Maybe it's a really stupid question, but I can't find the answer on Google.

There is a node in Godot - TextEdit. It has several enums. One of them is called MenuItems. One of these is MENU_CLEAR and "= 3" in grey. The description for it states: "Erases the whole TextEdit text."

My question is: how do I use this info to clear the whole TextEdit text?

I tried simply accessing it with

$TextEdit.MENU_CLEAR -> nothing

I tried going the long way

$TextEdit.MenuItems.MENU_CLEAR -> nothing

I tried putting it as a function call

$TextEdit.MENU_CLEAR() -> nothing

However, if I try to print it, I get it's number

print($TextEdit.MENU_CLEAR) -> "3"

The description of MENU_CLEAR, at least for me, implies there is a function somewhere there, that should be executed once I make a call to MENU_CLEAR. But I do something wrong. Pls, help

Edit1:
Here's my piece of code (just checking what works, running everything after IF one line at a time):

if $TextEdit.get_line_count() > 6:
    $TextEdit.text = ""
    $TextEdit.menu_option($TextEdit.MENU_CLEAR)

Here's what I want to do:

if $TextEdit.get_line_count() > 6:
    $TextEdit.select(1, 0, 1, 0)    #selects the oldest line
    $TextEdit.menu_option($TextEdit.MENU_CUT)    #deletes it

Here's the screenshot of what I get now
See the slider on the right side
And here's what I want it to be
Just 6 lines
So there should always be only 6 lines at once on the screen, and the old lines should be deleted. That's where I encountered problems with enums, and that was the reason for the post.
P.S. Read only is set to false

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半暖夏伤 2025-02-11 04:51:27

清除textedit

我将使用$ textedit.text =“”清除所有文本。


解决您的问题。 菜单_Clear是一个常数。它具有值3。就其本身而言,它无能为力3不会做。

但是,enum(是具有名称的常数列表),因为某些东西使用它们。要找出什么,我建议您在文档中搜索enum的名称(在这种情况下为menuitems)(尤其是同一类)。

在这种情况处> Menu_option 方法。从文档中:

void Menu_option(int选项)

通过指定索引触发右键单击菜单操作。有关可用索引的列表,请参见menuitems

因此,您会这样使用它:

$TextEdit.menu_option(TextEdit.MENU_CLEAR)

是的,这也有效:

$TextEdit.menu_option($TextEdit.MENU_CLEAR)

或者您可以这样做(这是同一件事,除了它不太清楚它的含义):

$TextEdit.menu_option(3)

从Textedit开始时删除线条,

让我们说我们想要开始时删除线路。我们将删除的行数为lines_to_remove(例如1)。

我们可以使用Menu_option来执行此操作。这样:

  1. 选择要删除的行。换句话说,选择“形式”第一行的开始(0),到将留下的第一行的开始(lines_to_remove):

      $ textedit.select(0,0,lines_to_remove,0)
     
  2. 清除切割:

      $ textedit.menu_option(textedit.menu_cut)
     

    这将删除所选文本。但是,它也将设置剪贴板。

另外,我们可以操纵文本。例如:

var lines:Array = $TextEdit.text.split("\n")
var new_text := ""
for index in lines.size() - lines_to_remove:
    if index > 0:
        new_text += "\n"

    new_text += str(lines[index + lines_to_remove])

$TextEdit.text = new_text

在这里,我得到文本$ textedit.text,然后按新行分开,这给了我array。然后,我将要保留的array串联(即跳过要删除的元素),然后将结果设置回$ textedit.text.text


无论采用哪种方法,您都会发现光标将被放置在文本开始时。

因此,我们将在操纵文本之前存储位置:

var cursor_line:int = $TextEdit.cursor_get_line()
var cursor_column:int = $TextEdit.cursor_get_column()

然后将其放回...好吧,实际上我们要减去我们删除的行数,然后将其放回原处:

cursor_line -= lines_to_remove
$TextEdit.cursor_set_line(cursor_line)
$TextEdit.cursor_set_column(cursor_column)

如果这导致了负面CURSOR_LINE,将其放置在第一行上。我想我可以检查并设置cursor_linecursor_column在这种情况下为零,但是尝试此操作时并不是问题。


限制数字 。

现在,如果您想确保最多有几行(例如六行),则现在要确保最多有一条 我们可以阅读我们拥有多少行,减去您允许的最大值,如果结果为正,那就是必须删除多少行:

var target_number_of_lines := 6
var lines_to_remove:int = $TextEdit.get_line_count() - target_number_of_lines
if lines_to_remove > 0:
    pass # rest of the code here

您为什么需要执行此操作,而不仅仅是删除第一行?因为用户可能会一次粘贴多行。说到哪个,这旨在在“ text_changed”信号上运行(尽管它也可以在_process)上

func _on_TextEdit_text_changed() -> void:
    var target_number_of_lines := 6
    var lines_to_remove:int = $TextEdit.get_line_count() - target_number_of_lines
    if lines_to_remove > 0:
        var cursor_line:int = $TextEdit.cursor_get_line()
        var cursor_column:int = $TextEdit.cursor_get_column()

        $TextEdit.select(0, 0, lines_to_remove, 0)
        $TextEdit.menu_option(TextEdit.MENU_CUT)

        cursor_line -= lines_to_remove
        $TextEdit.cursor_set_line(cursor_line)
        $TextEdit.cursor_set_column(cursor_column)

运行剪贴板。


操纵文本属性也有效(并且不编写剪贴板)。这是代码:

func _on_TextEdit_text_changed() -> void:
    var target_number_of_lines := 6
    var lines_to_remove:int = $TextEdit.get_line_count() - target_number_of_lines
    if lines_to_remove > 0:
        var cursor_line:int = $TextEdit.cursor_get_line()
        var cursor_column:int = $TextEdit.cursor_get_column()

        var lines:Array = $TextEdit.text.split("\n")
        var new_text := ""
        for index in lines.size() - lines_to_remove:
            if index > 0:
                new_text += "\n"

            new_text += str(lines[index + lines_to_remove])

        $TextEdit.text = new_text

        cursor_line -= lines_to_remove
        $TextEdit.cursor_set_line(cursor_line)
        $TextEdit.cursor_set_column(cursor_column)

Clearing TextEdit

I would use $TextEdit.text = "" to clear all the text.


To address your question. The MENU_CLEAR is a constant. It has the value 3. By itself it does nothing a 3 would not do.

However, enum (which are lists of constants with a name) are defined because something uses them. To find out what, I suggest you search for the name of the enum (which is MenuItems in this case) in the documentation (in particular on the same class).

In this case we find the menu_option method. From the documentation:

void menu_option ( int option )

Triggers a right-click menu action by the specified index. See MenuItems for a list of available indexes.

Thus, you would use it like this:

$TextEdit.menu_option(TextEdit.MENU_CLEAR)

Yes, this also works:

$TextEdit.menu_option($TextEdit.MENU_CLEAR)

Or you can do it like this (which is the same thing, except it less clear what it means):

$TextEdit.menu_option(3)

Removing lines from the start of TextEdit

Let us say we want to remove lines at the start. The number of lines we will remove will be lines_to_remove (e.g. 1).

We can do this using menu_option. Like this:

  1. Select the lines we want to remove. In other words, select form the start of the first line (0), to the start of the first line that will stay (lines_to_remove):

    $TextEdit.select(0, 0, lines_to_remove, 0)
    
  2. Clear cut:

    $TextEdit.menu_option(TextEdit.MENU_CUT)
    

    This will remove the selected text. However, it will also set the clipboard.

Alternatively, we can manipulate text. For example:

var lines:Array = $TextEdit.text.split("\n")
var new_text := ""
for index in lines.size() - lines_to_remove:
    if index > 0:
        new_text += "\n"

    new_text += str(lines[index + lines_to_remove])

$TextEdit.text = new_text

Here I get the text $TextEdit.text and split it by the new line, which gives me an Array. Then I concatenate the elements form the Array that I want to keep (i.e. skipping the ones I want to remove), And set the result back to $TextEdit.text.


Regardless of the approach you will find that the cursor will be placed at the start of the text.

So, we are going to store the position before manipulating the text:

var cursor_line:int = $TextEdit.cursor_get_line()
var cursor_column:int = $TextEdit.cursor_get_column()

And then set it back... Well, actually we want to subtract the number of lines we removed and then set it back:

cursor_line -= lines_to_remove
$TextEdit.cursor_set_line(cursor_line)
$TextEdit.cursor_set_column(cursor_column)

If this resulted in a negative cursor_line, it will be placed on the first line. I suppose I could check and set both cursor_line and cursor_column to zero in that case, but it has not been a problem while trying this out.


Limiting the number of lines of TextEdit

Now, if you want to make sure you have at most a give number of lines (e.g. six lines). We can read how many lines we have, subtract the maximum you allow, and if the result is positive, that is how many lines have to remove:

var target_number_of_lines := 6
var lines_to_remove:int = $TextEdit.get_line_count() - target_number_of_lines
if lines_to_remove > 0:
    pass # rest of the code here

Why would you need to do this instead of just removing the first line? Because the user might paste multiple lines at once. Speaking of which, this is intended to run on the "text_changed" signal (although it would also work on _process):

func _on_TextEdit_text_changed() -> void:
    var target_number_of_lines := 6
    var lines_to_remove:int = $TextEdit.get_line_count() - target_number_of_lines
    if lines_to_remove > 0:
        var cursor_line:int = $TextEdit.cursor_get_line()
        var cursor_column:int = $TextEdit.cursor_get_column()

        $TextEdit.select(0, 0, lines_to_remove, 0)
        $TextEdit.menu_option(TextEdit.MENU_CUT)

        cursor_line -= lines_to_remove
        $TextEdit.cursor_set_line(cursor_line)
        $TextEdit.cursor_set_column(cursor_column)

Since this version is cutting the text, it writes the clipboard.


Manipulating the text property also works (and does not write the clipboard). This is the code:

func _on_TextEdit_text_changed() -> void:
    var target_number_of_lines := 6
    var lines_to_remove:int = $TextEdit.get_line_count() - target_number_of_lines
    if lines_to_remove > 0:
        var cursor_line:int = $TextEdit.cursor_get_line()
        var cursor_column:int = $TextEdit.cursor_get_column()

        var lines:Array = $TextEdit.text.split("\n")
        var new_text := ""
        for index in lines.size() - lines_to_remove:
            if index > 0:
                new_text += "\n"

            new_text += str(lines[index + lines_to_remove])

        $TextEdit.text = new_text

        cursor_line -= lines_to_remove
        $TextEdit.cursor_set_line(cursor_line)
        $TextEdit.cursor_set_column(cursor_column)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文