FancyMenu Wiki/自定义菜单背景:修订间差异

// Edit via Wikiplus
(创建页面,内容为“== 关于 == 你可以使用FancyMenu编写你自己的自定义背景拓展模组,而且你可以在布局中使用它们。 重要:Menu Background API只在FancyMenu v2.6.2+可用! == 准备开发环境 == 请参阅 准备工作区。 == 添加菜单背景 == 每一个菜单背景都系要两个类。 第一个是<code>MenuBackgroundType</code>。 这个是你的背景类型(比如已经存在的动画,或是全景…”)
 
(// Edit via Wikiplus)
 
(未显示同一用户的3个中间版本)
 
=== 创建字符串模式背景 ===
输入字符串模式下的<code>MenuBackgroundTypes</code>从一个输入字符串中快速创建<code>MenuBackground</code>实例。
 
这些实例并不会想普通模式会被储存下来,而是每次布局被加载时新的实例也会被创建。
==== MenuBackground Class ====
创建一个<code>MenuBackground</code>的新子类并取一个合适的名字
 
接下来骂我会给我的示例类起名为ExampleMenuBackgroundForInputString。
接下来我会给我的示例类起名为<code>ExampleMenuBackgroundForInputString</code>。
 
这里有一些需要特别注意的事项。
=====构造方法(Constructor)=====
和普通模式不一样,你不需要为你的背景整一个唯一的标识符,因为它们并不会被储存下来。在输入字符串模式下,你为<code>MenuBackground</code>实例使用什么标识符基本上并不重要。
 
当然,这里仍然可以在构造上设置变量。
<syntaxhighlight lang="java">
public ExampleMenuBackgroundForInputString(@Nonnull MenuBackgroundType type, String imagePath) {
//Identifiers aren't really used for backgrounds that don't get registered to a type (because the type uses the input string),
 
}
</syntaxhighlight>
复制代码
=====onOpenMenu()=====
 
onOpenMenu()
这个方法对于输入字符串模式的背景没用,因为在每一次加载菜单时总会创建新的实例,所以这没啥可重置的。
<syntaxhighlight lang="java">
@Override
public void onOpenMenu() {
//所以在使用输入字符串模式时你不需要重置。
}
</syntaxhighlight>
复制代码
=====render()=====
 
render()
调用此方法来渲染你的菜单背景实例。
 
应该指我解释一下在这里做什么,对吧?
<syntaxhighlight lang="java">
//Here you will render the background instance.
//在这里你可以渲染背景实例。
 
}
</syntaxhighlight>
复制代码
=====完整样例类=====
 
完整的<code>MenuBackground</code>示
<syntaxhighlight lang="java">
完整的MenuBackground示例。
package de.keksuccino.fancymenu.api.background.example.with_input_string;
 
}
}
</syntaxhighlight>
复制代码
====MenuBackgroundType Class====
创建一个<code>MenuBackgroundType</code>的新子类并给它取合适的名字。
 
接下来我会将我的示例背景类型类命名为<code>ExampleMenuBackgroundTypeWithInputString</code>。
 
MenuBackgroundType Class
创建一个MenuBackgroundType的新子类并给它取合适的名字。
接下来我会将我的示例背景类型类命名为ExampleMenuBackgroundTypeWithInputString。
在这个类中有一些特别的点你需要留意。
=====构造方法(Constructor)=====
这里的构造方法用于设置你背景类型唯一的标识符。
<syntaxhighlight lang="java">
public ExampleMenuBackgroundTypeWithInputString() {
//This identifier needs to be UNIQUE! It is not possible to register multiple types with the same identifier.
super("example_type_input_string");
}
</syntaxhighlight>
复制代码
=====loadBackgrounds()=====
 
在输入字符串模式下,此方法未被使用,因为在此模式下,<code>MenuBackground</code>实例会被即时创建。
loadBackgrounds()
<syntaxhighlight lang="java">
在输入字符串模式下,此方法未被使用,因为在此模式下,MenuBackground实例会被即时创建。
@Override
public void loadBackgrounds() {
//这里什么都没有是因为背景实例是通过createInstanceFromInputString()即时创建的。
}
</syntaxhighlight>
复制代码
=====getDisplayName()=====
 
getDisplayName()
调用此方法获取背景类型名。
 
名称显示在布局编辑器的背景选项中。
<syntaxhighlight lang="java">
//You don't really have much space for the display name, so try to choose a short one ond explain the type further in the description.
//显示名字的空间很挤,你的类型名字简短点吧,在描述里详细说明就好。
return "Example Type w/ Input";
}
</syntaxhighlight>
复制代码
=====getDescription()=====
 
getDescription()
获取背景类型的描述。
 
在布局编辑器的背景选项中显示。
<syntaxhighlight lang="java">
//Gets displayed when hovering over the type switcher in the background options menu in the layout editor.
//在布局编辑器的背景选项菜单中悬停在类型切换器上时显示。
return l;
}
</syntaxhighlight>
复制代码
=====needsInputString()=====
 
needsInputString()
这是最重要的方法,因为你得在这选择背景类型模式。
在这里,我们返回true,<code>true</code>,因为我们想为咱的背景类型使用输入字符串模式。
返回<code>false</code>会将背景类型设置为普通模式。
<syntaxhighlight lang="java">
@Override
public boolean needsInputString() {
return true;
}
</syntaxhighlight>
复制代码
=====createInstanceFromInputString()=====
当处于输入字符串模式时,每次需要一个新的<code>MenuBackground</code>实例时都会调用这个方法。
 
createInstanceFromInputString()
当处于输入字符串模式时,每次需要一个新的MenuBackground实例时都会调用这个方法。
它基本是菜单背景工厂。
 
这个方法有给定的输入字符串作为参数,所以你可以用它来创建新的实例。
<syntaxhighlight lang="java">
//In input string mode, this will get called whenever a new background instance is needed.
//在输入字符串模式下,每当需要一个新的背景实例时,它都会被调用。
return new ExampleMenuBackgroundForInputString(this, inputString);
}
</syntaxhighlight>
复制代码
=====onInputStringButtonPress()=====
 
onInputStringButtonPress()
这里,你可以指定当用户在布局编辑器的后台选项中点击输入字符串按钮时会发生什么。
 
在这基本没啥限制,除了让猴子写代码,想干啥干啥,只要你可以在最后正确地将新背景设置成编辑器实例。
<syntaxhighlight lang="java">
//This gets called when the input string button in the background options is pressed by the user.
//这个方法当用户按下背景选项上的输入字符串按钮后会被调用。
 
}
</syntaxhighlight>
复制代码
=====inputStringButtonLabel()=====
 
inputStringButtonLabel()
返回布局编辑器的背景选项中的输入字符串按钮的标签。
<syntaxhighlight lang="java">
//The button label of the input string button in the background options.
//背景选项中输入字符串按钮的按钮标签。
return "Choose File";
}
</syntaxhighlight>
复制代码
=====inputStringButtonTooltip()=====
 
inputStringButtonTooltip()
返回布局编辑器的背景选项中输入字符串按钮的工具提示。
<syntaxhighlight lang="java">
//A tooltip that gets displayed when hovering over the input string button in the background options menu of the layout editor.
//当悬停在布局编辑器的背景选项菜单的输入字符串按钮上时,会显示工具提示。
return l;
}
</syntaxhighlight>
复制代码
=====完整示例=====
 
完整示例
下面是完整的MenuBackgroundType样例.
<syntaxhighlight lang="java">
package de.keksuccino.fancymenu.api.background.example.with_input_string;
 
 
}
</syntaxhighlight>
复制代码
 
====注册菜单背景====
你就快完成了
 
现在你只需要在游戏加载时把你的MenuBackgroundType注册到MenuBackgroundTypeRegistry.
现在你只需要在游戏加载时把你的<code>MenuBackgroundType</code>注册到<code>MenuBackgroundTypeRegistry</code>.
<syntaxhighlight lang="java">
package de.keksuccino.fancymenu;
 
 
}
</syntaxhighlight>
复制代码
[[分类:FancyMenu]]
维护员、​界面管理员、​巡查员、​监督员、​小部件编辑者
3,462

个编辑