Clyde學習筆記三(Config)

在clyde中,幾乎所有的外部資源都是以config的形式存在。每個config都有一個name來唯一標識這個config。ManagedConfig是所有config的基類,它是一個抽象類,定義了一些最基礎的方法,ParameterizedConfig是它直接的子類。

 

1. ParameterizedConfig

 

ParameterizedConfig引入了parameter這一概念,利用parameter可以實現對config的個性化定製,這樣可以複用絕大多數的config,並且減少config的數量。比如你有一個model,這個model在不同的場景需要應用不同的貼圖,那麼與其製作2個不同的config,不如利用parameter這一特性,在resource editor中選擇創建一個direct parameter,給它一個名字叫“Texture”,然後把鼠標移到material mapping這一欄,按下ctrl-shift-C,複製material mapping屬性的路徑到剪貼板,然後再把此路徑粘貼到剛纔創建的parameter的path中。完成後,以後在代碼中,可以這樣來引用:

 

new Model(ctx, "flag.dat", "Texture", "texture.png");


 

“Texture”是parameter的name,“texture.png”是parameter的值。在其內部是以一個argumentMap來實現的。

 

 

/** The arguments applied to the configuration, if any. */
    @DeepOmit
    protected transient ArgumentMap _args;

 

以此也引申出base和derived概念。

 

 

    /** The instance from which the configuration is derived, if any (used to prevent the base
     * from being garbage-collected). */
    @DeepOmit
    protected transient ParameterizedConfig _base;

   /** Maps arguments to derived instances. */
    @DeepOmit
    protected transient SoftCache<ArgumentMap, ParameterizedConfig> _derived;

 

2. ConfigReference

 

ConfigReference以name和argumentMap屬性保存了對一個config的引用,因爲我們已經知道每個config都有一個name,但是這個name指向的config包括base和derived部分,所以以name和argumentMap可以唯一確定一個config的值。

 

 

    /** The name of the referenced configuration. */
    @Intern
    protected String _name;

    /** The arguments of the reference, mapped by name. */
    protected ArgumentMap _arguments = new ArgumentMap();
 

如果這個configReference是可以被編輯的,即標註有@Editable,在config editor中可以被編輯,並且在編輯時可以看到保存在configReference中的parameter的值。而且這個configreference是可以被嵌套的,比如說modelConfig中有materialMapping,materialMapping中有一個configReference<MaterialConfig>,在MaterialConfig中有一個configReference<TextureConfig>,那麼在創建一個materialMapping的時候可以編輯一個MaterialConfig,在編輯MaterialConfig時可以再繼續編輯TextureConfig。

 

3. ConfigGroup

 

在clyde中,幾乎所有的配置都保存爲config,都是從同一個基類中派生出來,但是從應用的角度來講,一般可以分成2類,一類是所謂的managed config,像角色或者場景中的對象,它們在ConfigManager啓動的時候被加載進來,保存在文件系統的rsrc/config路徑下,並且在classpath中的rsrc/config路徑下有一個manager.properties文件,一個典型配置文件看起來如下:

 

 

types = global, model, user_interface, scene

global.classes = com.threerings.tudey.config.ActorConfig, \
  com.threerings.tudey.config.AreaConfig, \
  com.threerings.tudey.config.BehaviorConfig, \
  com.threerings.opengl.gui.config.CursorConfig, \
  com.threerings.tudey.config.EffectConfig, \
  com.threerings.opengl.gui.config.FontConfig, \
  com.threerings.tudey.config.GroundConfig, \
  com.threerings.opengl.material.config.MaterialConfig, \

model.classes = com.threerings.opengl.gui.config.CursorConfig, \
  com.threerings.opengl.gui.config.FontConfig, \
  com.threerings.opengl.material.config.MaterialConfig, \

user_interface.classes = com.threerings.opengl.gui.config.CursorConfig, \
  com.threerings.opengl.gui.config.FontConfig, \
  com.threerings.opengl.gui.config.StyleConfig

scene.classes = com.threerings.tudey.config.ActorConfig, \
  com.threerings.tudey.config.AreaConfig, \
  com.threerings.tudey.config.EffectConfig, \
  com.threerings.tudey.config.GroundConfig, \

resource.classes = com.threerings.opengl.model.config.ModelConfig, \
  com.threerings.opengl.model.config.AnimationConfig, \
  com.threerings.opengl.gui.config.UserInterfaceConfig
 

在這裏,每個config class對應的不再是一個單個的config,而是一個config group,每一個config group在文件系統rsrc/config路徑下會有一個對應的.dat文件,在configManager啓動時,會默認的讀取type爲global下每個config class對應的.dat文件,載入相應的config group。這些config可以在config editor中被編輯。


在上圖可以看到,global標籤頁對應的是global type,group對應的是每一個config class,在每個group下有多個config,這些config還可以以目錄的形式組織起來。

 

除了managed config之外,還有單個形式的config,比如model或者animation,這些config只有在需要用到的時候纔會被載入,model和animation是在resource editor中編輯的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章