Day 2

  1. SlateCore\Public\Widgets\SOverlay.h
/**
 * Implements an overlay widget.
 *
 * Overlay widgets allow for layering several widgets on top of each other.
 * Each slot of an overlay represents a layer that can contain one widget.
 * The slots will be rendered on top of each other in the order they are declared in code.
 *
 * Usage:
 *      SNew(SOverlay)
 *      + SOverlay::Slot(SNew(SMyWidget1))
 *      + SOverlay::Slot(SNew(SMyWidget2))
 *      + SOverlay::Slot(SNew(SMyWidget3))
 *
 *      Note that SWidget3 will be drawn on top of SWidget2 and SWidget1.
 */
class SLATECORE_API SOverlay
    : public SPanel
{
public: 

    /** A slot that support alignment of content and padding and z-order */
    class SLATECORE_API FOverlaySlot : public TSlotBase<FOverlaySlot>
    {
    public:
        FOverlaySlot()
            : TSlotBase<FOverlaySlot>()
            , ZOrder(0)
            , HAlignment(HAlign_Fill)
            , VAlignment(VAlign_Fill)
            , SlotPadding(0.0f)
        { }

        FOverlaySlot& HAlign( EHorizontalAlignment InHAlignment )
        {
            HAlignment = InHAlignment;
            return *this;
        }

        FOverlaySlot& VAlign( EVerticalAlignment InVAlignment )
        {
            VAlignment = InVAlignment;
            return *this;
        }

        FOverlaySlot& Padding(float Uniform)
        {
            SlotPadding = FMargin(Uniform);
            return *this;
        }

        FOverlaySlot& Padding(float Horizontal, float Vertical)
        {
            SlotPadding = FMargin(Horizontal, Vertical);
            return *this;
        }

        FOverlaySlot& Padding(float Left, float Top, float Right, float Bottom)
        {
            SlotPadding = FMargin(Left, Top, Right, Bottom);
            return *this;
        }

        FOverlaySlot& Padding( const TAttribute<FMargin> InPadding )
        {
            SlotPadding = InPadding;
            return *this;
        }

        /** Slots with larger ZOrder values will draw above slots with smaller ZOrder values.  Slots
        with the same ZOrder will simply draw in the order they were added.  Currently this only
        works for overlay slots that are added dynamically with AddWidget() and RemoveWidget() */
        int32 ZOrder;

        TEnumAsByte<EHorizontalAlignment> HAlignment;
        TEnumAsByte<EVerticalAlignment> VAlignment;

        TAttribute< FMargin > SlotPadding;
    };


    SLATE_BEGIN_ARGS( SOverlay )
    {
        _Visibility = EVisibility::SelfHitTestInvisible;
    }

        SLATE_SUPPORTS_SLOT( SOverlay::FOverlaySlot )

    SLATE_END_ARGS()

    SOverlay();

    /**
     * Construct this widget.
     *
     * @param   InArgs  The declaration data for this widget
     */
    void Construct( const FArguments& InArgs );

    /** Returns the number of child widgets */
    int32 GetNumWidgets() const;

    /**
     * Removes a widget from this overlay.
     *
     * @param   Widget  The widget content to remove
     */
    bool RemoveSlot( TSharedRef< SWidget > Widget );

    /** Adds a slot at the specified location (ignores Z-order) */
    FOverlaySlot& AddSlot(int32 ZOrder=INDEX_NONE);

    /** Removes a slot at the specified location */
    void RemoveSlot(int32 ZOrder=INDEX_NONE);

    /** Removes all children from the overlay */
    void ClearChildren();

    /** @return a new slot. Slots contain children for SOverlay */
    static FOverlaySlot& Slot()
    {
        return *(new FOverlaySlot());
    }

    // SWidget interface
    virtual void OnArrangeChildren( const FGeometry& AllottedGeometry, FArrangedChildren& ArrangedChildren ) const override;
    virtual FChildren* GetChildren() override;
    virtual int32 OnPaint( const FPaintArgs& Args, const FGeometry& AllottedGeometry, const FSlateRect& MyCullingRect, FSlateWindowElementList& OutDrawElements, int32 LayerId, const FWidgetStyle& InWidgetStyle, bool bParentEnabled ) const override;
    // End of SWidget interface

protected:
    // Begin SWidget overrides.
    virtual FVector2D ComputeDesiredSize(float) const override;
    // End SWidget overrides.

protected:
    /** The SOverlay's slots; each slot contains a child widget. */
    TPanelChildren<FOverlaySlot> Children;
};

SOverlay按照上下层布局子widgets。在这里我们看到FOverlaySlot,该Slot类型定义了子Widget的布局属性。
问题列表:

  1. FMargin是什么概念

  2. SlateCore\Public\Widgets\SNullWidget.h

/**
 * Implements a widget that can be used as a placeholder.
 *
 * Widgets that support slots, such as SOverlay and SHorizontalBox should initialize
 * their slots' child widgets to SNullWidget if no user defined widget was provided.
 */
class SLATECORE_API SNullWidget
{
public:

    /**
     * Returns a placeholder widget.
     *
     * @return The widget.
     */
    static TSharedRef<class SWidget> NullWidget;
};

空Widget, 起到占位作用,啥也不干。

  1. StateCore\Public\Widgets\SLeafWidget.h
/**
 * Implements a leaf widget.
 *
 * A LeafWidget is a Widget that has no slots for children.
 * LeafWidgets are usually intended as building blocks for aggregate widgets.
 */
class SLATECORE_API SLeafWidget
    : public SWidget
{
public:
    SLeafWidget()
    {
        bCanHaveChildren = false;
    }

    virtual void SetVisibility( TAttribute<EVisibility> InVisibility ) override final;

private:

    // Begin SWidget overrides

    /**
     * Overwritten from SWidget.
     *
     * LeafWidgets provide a visual representation of themselves. They do so by adding DrawElement(s)
     * to the OutDrawElements. DrawElements should have their positions set to absolute coordinates in
     * Window space; for this purpose the Slate system provides the AllottedGeometry parameter.
     * AllottedGeometry describes the space allocated for the visualization of this widget.
     *
     * Whenever possible, LeafWidgets should avoid dealing with layout properties. See TextBlock for an example.
     */
    virtual int32 OnPaint( const FPaintArgs& Args, const FGeometry& AllottedGeometry, const FSlateRect& MyCullingRect, FSlateWindowElementList& OutDrawElements, int32 LayerId, const FWidgetStyle& InWidgetStyle, bool bParentEnabled ) const override = 0;
    
    /**
     * Overwritten from SWidget.
     *
     * LeafWidgets should compute their DesiredSize based solely on their visual representation. There is no need to
     * take child widgets into account as LeafWidgets have none by definition. For example, the TextBlock widget simply
     * measures the area necessary to display its text with the given font and font size.
     */
    virtual FVector2D ComputeDesiredSize(float) const override = 0;
    
    /**
     * Overwritten from SWidget.
     *
     * Leaf widgets never have children.
     */
    virtual FChildren* GetChildren() override;

    virtual void OnArrangeChildren( const FGeometry& AllottedGeometry, FArrangedChildren& ArrangedChildren ) const override;

private:

    // Shared instance of FNoChildren for all widgets with no children.
    static FNoChildren NoChildrenInstance;
};

叶子Widget,没有子Widget.

  1. SlateCore\Public\Widgets\SBoxPanel.h
/**
 * A BoxPanel contains one child and describes how that child should be arranged on the screen.
 */
class SLATECORE_API SBoxPanel : public SPanel;
/** A Horizontal Box Panel. See SBoxPanel for more info. */
class SLATECORE_API SHorizontalBox : public SBoxPanel
/** A Vertical Box Panel. See SBoxPanel for more info. */
class SLATECORE_API SVerticalBox : public SBoxPanel;
/** A Vertical Box Panel. See SBoxPanel for more info. */
class SLATECORE_API SDragAndDropVerticalBox : public SVerticalBox
  1. SlateCore\Public\Widgets\SCompoundWidget.h
/**
 * A CompoundWidget is the base from which most non-primitive widgets should be built.
 * CompoundWidgets have a protected member named ChildSlot.
 */
class SLATECORE_API SCompoundWidget : public SWidget
{
public:

    /**
     * Returns the size scaling factor for this widget.
     *
     * @return Size scaling factor.
     */
    const FVector2D GetContentScale() const
    {
        return ContentScale.Get();
    }

    /**
     * Sets the content scale for this widget.
     *
     * @param InContentScale Content scaling factor.
     */
    void SetContentScale( const TAttribute< FVector2D >& InContentScale )
    {
        ContentScale = InContentScale;
    }

    /**
     * Gets the widget's color.
     */
    FLinearColor GetColorAndOpacity() const
    {
        return ColorAndOpacity.Get();
    }

    /**
     * Sets the widget's color.
     *
     * @param InColorAndOpacity The ColorAndOpacity to be applied to this widget and all its contents.
     */
    void SetColorAndOpacity( const TAttribute<FLinearColor>& InColorAndOpacity )
    {
        ColorAndOpacity = InColorAndOpacity;
    }

    /**
     * Sets the widget's foreground color.
     *
     * @param InColor The color to set.
     */
    void SetForegroundColor( const TAttribute<FSlateColor>& InForegroundColor )
    {
        ForegroundColor = InForegroundColor;
    }

public:

    // SWidgetOverrides
    virtual int32 OnPaint( const FPaintArgs& Args, const FGeometry& AllottedGeometry, const FSlateRect& MyCullingRect, FSlateWindowElementList& OutDrawElements, int32 LayerId, const FWidgetStyle& InWidgetStyle, bool bParentEnabled ) const override;
    virtual FChildren* GetChildren() override;
    virtual void OnArrangeChildren( const FGeometry& AllottedGeometry, FArrangedChildren& ArrangedChildren ) const override;
    virtual FSlateColor GetForegroundColor() const override;

public:
    virtual void SetVisibility( TAttribute<EVisibility> InVisibility ) override final;

protected:
    // Begin SWidget overrides.
    virtual FVector2D ComputeDesiredSize(float) const override;
    // End SWidget overrides.

protected:

    /** Disallow public construction */
    SCompoundWidget();

    /** The slot that contains this widget's descendants.*/
    FSimpleSlot ChildSlot;

    /** The layout scale to apply to this widget's contents; useful for animation. */
    TAttribute<FVector2D> ContentScale;

    /** The color and opacity to apply to this widget and all its descendants. */
    TAttribute<FLinearColor> ColorAndOpacity;

    /** Optional foreground color that will be inherited by all of this widget's contents */
    TAttribute<FSlateColor> ForegroundColor;
};
  1. SlateCore\Public\Widgets\SUserWidget.h
/**
 * Use SUserWidget as a base class to build aggregate widgets that are not meant
 * to serve as low-level building blocks. Examples include: a main menu, a user card,
 * an info dialog for a selected object, a splash screen.
 *
 * See SUserWidgetExample
 *
 * SMyWidget.h
 * -----------
 * class SMyWidget : public SUserWidget
 * { 
 *   public:
 *   SLATE_USER_ARGS( SMyWidget )
 *   {}
 *   SLATE_END_ARGS()
 *
 *   // MUST Provide this function for SNew to call!
 *   virtual void Construct( const FArguments& InArgs ) = 0;
 *
 *   virtual void DoSomething() = 0;
 * };
 *
 * SMyWidget.cpp
 * -------------
 * namespace Implementation 
 * {
 *   class SMyWidget : public ::SMyWidget
 *   {
 *     public:
 *     virtual void Construct( const FArguments& InArgs ) override
 *     {
 *        SUserWidget::Construct( SUserWidget::FArguments()
 *        [
 *           SNew(STextBlock)
 *           .Text( NSLOCTEXT("x", "x", "My Widget's Content") )
 *        ]
 *     }
 *     
 *     private:
 *     // Private implementation details can occur here
 *     // without ever leaking out into the .h file!
 *   }
 * }
 *
 * TSharedRef<SMyWidget> SMyWidget::New()
 * {
 *   return MakeShareable( new SMyWidget() );
 * }
 */
class SUserWidget : public SCompoundWidget
{
    public:
    struct FArguments : public TSlateBaseNamedArgs<SUserWidget>
    {
        typedef FArguments WidgetArgsType;
        FORCENOINLINE FArguments()
            : _Content()
            , _HAlign(HAlign_Fill)
            , _VAlign(VAlign_Fill)
        {}

        SLATE_DEFAULT_SLOT( FArguments, Content )
        SLATE_ARGUMENT( EHorizontalAlignment, HAlign )
        SLATE_ARGUMENT( EVerticalAlignment, VAlign )

    };
    
    protected:
    void Construct( const FArguments& InArgs )
    {
        this->ChildSlot
        .HAlign( InArgs._HAlign )
        .VAlign( InArgs._VAlign )
        [
            InArgs._Content.Widget
        ];
    }

    protected:
    /** User widgets can be allocated explicitly in the C++  */
    void* operator new ( const size_t InSize )
    {
        return FMemory::Malloc(InSize);
    }

};
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,482评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,377评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,762评论 0 342
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,273评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,289评论 5 373
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,046评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,351评论 3 400
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,988评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,476评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,948评论 2 324
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,064评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,712评论 4 323
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,261评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,264评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,486评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,511评论 2 354
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,802评论 2 345

推荐阅读更多精彩内容