gui

package
v0.0.0-...-e1c21c9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 10, 2019 License: Zlib Imports: 9 Imported by: 18

Documentation

Index

Constants

View Source
const (
	FlagCornerNone        FlagCorner = 0x0000
	FlagCornerTopLeft                = 0x0001
	FlagCornerTopRight               = 0x0002
	FlagCornerBottomRight            = 0x0004
	FlagCornerBottomLeft             = 0x0008
	FlagCornerAll                    = 0x000F
)
View Source
const (
	AlignCenter Align = iota
	AlignLeft         = 1 << iota
	AlignRight        = 1 << iota
	AlignTop          = 1 << iota
	AlignBottom       = 1 << iota
)
View Source
const (
	DefaultZOrder = int16(0xFFFF>>1 - 100)
)
View Source
const EventNone = EventType(0)

Variables

View Source
var DebugDraw = false

Functions

func CheckBox

func CheckBox(id ID, bb Rect, text string, style *CheckBoxStyle) bool

func ColorRect

func ColorRect(bb Rect, fill gfx.Color, rounding float32)

Widget: ColorRect

func Game2Gui

func Game2Gui(x, y float32) (x1, y1 float32)

func Gui2Game

func Gui2Game(x, y float32) (x1, y1 float32)

func HintAndScale

func HintAndScale() (hintx, hinty, skx, sky float32)

func Image

func Image(id ID, bb Rect, tex gfx.Tex2D, style *ImageStyle)

Widget: Image

func InputText

func InputText(id ID, bb Rect, hint string, style *InputStyle)

Widgets: InputEditor

func ListView

func ListView()

Widget: ListView TODO

func Move

func Move(x, y float32)

Move sets the ui coordinate's origin to (x, y)

func Offset

func Offset(dx, dy float32)

Offset move the ui coordinate's origin by (dx, dy)

func ProgressBar

func ProgressBar(id ID, bb Rect, fraction float32, style *ProgressBarStyle)

Widget: ProgressBar, Slider

func ScreenSize

func ScreenSize() (w, h float32)

ScreenSize return the physical width&height of the screen.

func SetFont

func SetFont(font font.Font)

func SetScreenSize

func SetScreenSize(w, h float32)

for internal usage, DO NOT call.

func SetVirtualResolution

func SetVirtualResolution(w, h float32)

SetVirtualResolution set the virtual resolution.

func SetZOrder

func SetZOrder(z int16) (old int16)

Set Z-Order for

func Text

func Text(id ID, bb Rect, text string, style *TextStyle)

Widgets: Text

func TextSizeColored

func TextSizeColored(id ID, bb Rect, text string, color gfx.Color, size float32)

func UseTheme

func UseTheme(style *Theme)

Theme:

func VirtualSize

func VirtualSize() (w, h float32)

Types

type Align

type Align uint32

type ButtonStyle

type ButtonStyle struct {
	TextStyle
	Padding
	Background struct {
		Normal  gfx.Color
		Pressed gfx.Color
	}
	Gravity  f32.Vec2
	Rounding float32
}

type CheckBoxStyle

type CheckBoxStyle struct {
}

type Context

type Context struct {
	DrawList
	*Theme
	Cursor
	// contains filtered or unexported fields
}

一个Context维护UI层逻辑: 1. 一个 DrawList,负责生成顶点 2. 共享的 Theme,指针引用,多个 Context 之间可以共享 3. State 维护,负责维护当前的 UI 状态,比如动画,按钮,列表... 窗口在 Context 之上维护,默认的Context可以随意的绘制 在窗口内绘制的UI会受到窗口的管理.

func DefaultContext

func DefaultContext() *Context

func NewContext

func NewContext(style *Theme) *Context

func (*Context) Button

func (ctx *Context) Button(id ID, bb *Rect, text string, style *ButtonStyle) (event EventType)

Widget: Button

func (*Context) CalcTextSize

func (ctx *Context) CalcTextSize(text string, wrapWidth float32, fnt font.Font, fontSize float32) f32.Vec2

func (*Context) CheckSlider

func (ctx *Context) CheckSlider(id ID, bound *Rect) (v float32, e EventType)

func (*Context) ClickEvent

func (ctx *Context) ClickEvent(id ID, rect *Rect) EventType

func (*Context) ColorBackground

func (ctx *Context) ColorBackground(event EventType, bb *Rect, normal, pressed gfx.Color, round float32)

偷师 flat-ui 中的设计,把空间的前景和背景分离,背景单独根据事件来变化.. 在 Android 中,Widget的前景和背景都可以根据控件状态发生变化 但是在大部分UI中,比如 Text/Image 只会改变背景的状态 偷懒的自定义UI,不做任何状态的改变... 所以说呢, 我们也采用偷懒的做法呗。。

func (*Context) DraggingEvent

func (ctx *Context) DraggingEvent(id ID, bound *Rect) EventType

func (*Context) DrawBorder

func (ctx *Context) DrawBorder(bb *Rect, color uint32, round, thick float32)

func (*Context) DrawCircle

func (ctx *Context) DrawCircle(x, y, radius float32, fill gfx.Color)

default segment = 12 TODO, circle scale factor

func (*Context) DrawCircleNoneFill

func (ctx *Context) DrawCircleNoneFill(x, y, radius float32, strokeColor gfx.Color, segment int, thickness float32)

segment default=12

func (*Context) DrawDebugBorder

func (ctx *Context) DrawDebugBorder(x, y, w, h float32, color uint32)

func (*Context) DrawGradient

func (ctx *Context) DrawGradient(bb Rect, c0, c1 gfx.Color, vertical bool)

func (*Context) DrawImage

func (ctx *Context) DrawImage(bound *Rect, tex gfx.Tex2D, style *ImageStyle)

func (*Context) DrawQuad

func (ctx *Context) DrawQuad(vertex [4]f32.Vec2, fill gfx.Color)

func (*Context) DrawRect

func (ctx *Context) DrawRect(bb *Rect, fill gfx.Color, round float32)

func (*Context) DrawText

func (ctx *Context) DrawText(bb *Rect, text string, style *TextStyle) (size f32.Vec2)

绘制元素, bb 存储相对于父容器的相对坐标..

func (*Context) EndScroll

func (ctx *Context) EndScroll()

func (*Context) Image

func (ctx *Context) Image(id ID, bb *Rect, tex gfx.Tex2D, style *ImageStyle)

Widget: Image

func (*Context) ImageBackground

func (ctx *Context) ImageBackground(eventType EventType)

func (*Context) ImageButton

func (ctx *Context) ImageButton(id ID, normal, pressed gfx.Tex2D, bb *Rect, style *ImageButtonStyle) (event EventType)

func (*Context) InputText

func (ctx *Context) InputText(id ID, bb *Rect, hint string, style *InputStyle)

Widgets: InputEditor

func (*Context) PushClipRect

func (ctx *Context) PushClipRect(minClip, maxClip f32.Vec2, intersectCurrent bool)

Clip:

func (*Context) Slider

func (ctx *Context) Slider(id ID, bb *Rect, value *float32, style *SliderStyle) (e EventType)

func (*Context) StartScroll

func (ctx *Context) StartScroll(size, offset f32.Vec2)

Scroll 效果的关键是使用裁切限制滚动区域,然后 通过计算拖拽,来得到争取的偏移

func (*Context) Text

func (ctx *Context) Text(id ID, bb *Rect, text string, style *TextStyle)

func (*Context) UseTheme

func (ctx *Context) UseTheme(style *Theme)

Theme:

type Cursor

type Cursor struct {
	X, Y float32
}

type DrawCmd

type DrawCmd struct {
	FirstIndex uint16
	ElemCount  uint16
	ClipRect   f32.Vec4
	TextureId  uint16
	// contains filtered or unexported fields
}

DrawList provide method to write primitives to buffer

type DrawIdx

type DrawIdx uint16

type DrawList

type DrawList struct {
	CmdBuffer []DrawCmd
	IdxBuffer []DrawIdx
	VtxBuffer []DrawVert

	// Data *DrawListSharedData
	OwnerName     string // 窗口名
	VtxCurrentIdx int    // VtxBuffer.Size

	// 指向当前正在使用的 cmdbuffer 的位置
	VtxWriter []DrawVert
	IdxWriter []DrawIdx

	ClipRectStack  []f32.Vec4
	TextureIdStack []uint16

	FullScreen      f32.Vec4
	TexUVWhitePixel f32.Vec2
	CircleVtx12     [12]f32.Vec2
	Font            font.Font
	FontSize        float32

	Flags  DrawListFlags
	ZOrder int16
	// contains filtered or unexported fields
}

func NewDrawList

func NewDrawList() *DrawList

func (*DrawList) AddBezierCurve

func (dl *DrawList) AddBezierCurve(pos0 f32.Vec2, cp0, cp1 f32.Vec2, pos1 f32.Vec2,
	color uint32, thickness float32, segments int)

func (*DrawList) AddCircle

func (dl *DrawList) AddCircle(centre f32.Vec2, radius float32, color uint32, segments int, thickness float32)

func (*DrawList) AddCircleFilled

func (dl *DrawList) AddCircleFilled(centre f32.Vec2, radius float32, color uint32, segments int)

func (*DrawList) AddCommand

func (dl *DrawList) AddCommand(elemCount int)

每次绘制都会产生一个 Command (可能会造成内存浪费! 1k cmd = 1000 * 6 * 4 = 24k) 为了减少内存可以一边添加一边尝试向前合并

func (*DrawList) AddConvexPolyFilled

func (dl *DrawList) AddConvexPolyFilled(points []f32.Vec2, color uint32)

Non Anti-aliased Fill

func (*DrawList) AddImage

func (dl *DrawList) AddImage(texId uint16, a, b f32.Vec2, uva, uvb f32.Vec2, color uint32)

func (*DrawList) AddImageNinePatch

func (dl *DrawList) AddImageNinePatch(texId uint16, min, max f32.Vec2, uva, uvb f32.Vec2, patch f32.Vec4, color uint32)

NinePatch Algorithm

12   13   14   15
     x1   x2     max
+----+----+----+
|    |    |    |
|    |    |p1  |
+----+----+----+ y2
|    |    |    |
|    |p0  |    |
+----+----+----+ y1
|    |    |    |
|    |    |    |
+----+----+----+

min

0    1    2    3

patch = {x1, x2, y1, y2} % TextureSize

func (*DrawList) AddImageQuad

func (dl *DrawList) AddImageQuad(texId uint16, a, b, c, d f32.Vec2, uva, uvb, uvc, uvd f32.Vec2, color uint32)

func (*DrawList) AddImageRound

func (dl *DrawList) AddImageRound(texId uint16, a, b f32.Vec2, uva, uvb f32.Vec2, color uint32, rounding float32, corners FlagCorner)

func (*DrawList) AddLine

func (dl *DrawList) AddLine(a, b f32.Vec2, color uint32, thickness float32)

func (*DrawList) AddPolyLine

func (dl *DrawList) AddPolyLine(points []f32.Vec2, color uint32, thickness float32, closed bool)

此处生成最终的顶点数据和索引数据 当前并不支持抗锯齿!!简单的用顶点生成线段

func (*DrawList) AddQuad

func (dl *DrawList) AddQuad(a, b, c, d f32.Vec2, color uint32, thickness float32)

func (*DrawList) AddQuadFilled

func (dl *DrawList) AddQuadFilled(a, b, c, d f32.Vec2, color uint32)

func (*DrawList) AddRect

func (dl *DrawList) AddRect(a, b f32.Vec2, color uint32, rounding float32, roundFlags FlagCorner, thickness float32)

所有非填充图形看来都是使用路径实现的

func (*DrawList) AddRectFilled

func (dl *DrawList) AddRectFilled(min, max f32.Vec2, color uint32, rounding float32, corner FlagCorner)

func (*DrawList) AddRectFilledMultiColor

func (dl *DrawList) AddRectFilledMultiColor()

func (*DrawList) AddText

func (dl *DrawList) AddText(pos f32.Vec2, text string, font font.Font, fontSize float32, color uint32, wrapWidth float32) (size f32.Vec2)

func (*DrawList) AddTriangle

func (dl *DrawList) AddTriangle(a, b, c f32.Vec2, color uint32, thickness float32)

func (*DrawList) AddTriangleFilled

func (dl *DrawList) AddTriangleFilled(a, b, c f32.Vec2, color uint32)

func (*DrawList) Clear

func (dl *DrawList) Clear()

TODO

func (*DrawList) Commands

func (dl *DrawList) Commands() []DrawCmd

func (*DrawList) CurrentClipRect

func (dl *DrawList) CurrentClipRect() (clip f32.Vec4)

func (*DrawList) CurrentTextureId

func (dl *DrawList) CurrentTextureId() (id uint16)

func (*DrawList) Empty

func (dl *DrawList) Empty() bool

func (*DrawList) GetClipRectMax

func (dl *DrawList) GetClipRectMax() f32.Vec2

func (*DrawList) GetClipRectMin

func (dl *DrawList) GetClipRectMin() f32.Vec2

func (*DrawList) Initialize

func (dl *DrawList) Initialize()

func (*DrawList) PathArcTo

func (dl *DrawList) PathArcTo(centre f32.Vec2, radius float32, min, max float32, segments int)

func (*DrawList) PathArcToFast

func (dl *DrawList) PathArcToFast(centre f32.Vec2, radius float32, min12, max12 int)

此处圆角的算法: 使用一个12边形近似圆形,采用中心放射算法,计算出 各个角度的sin/cos, 然后通过公式,得到圆圆形顶点 f(x) = centre.x + cos()*radius f(y) = centre.y + sin()*radius 以上, 可以提前算好 sin/cos 加速整个过程

func (*DrawList) PathBezierCurveTo

func (dl *DrawList) PathBezierCurveTo(p2, p3, p4 f32.Vec2, segments int)

func (*DrawList) PathClear

func (dl *DrawList) PathClear()

func (*DrawList) PathFillConvex

func (dl *DrawList) PathFillConvex(col uint32)

func (*DrawList) PathLineTo

func (dl *DrawList) PathLineTo(pos f32.Vec2)

func (*DrawList) PathLineToMergeDuplicate

func (dl *DrawList) PathLineToMergeDuplicate(pos f32.Vec2)

func (*DrawList) PathRect

func (dl *DrawList) PathRect(a, b f32.Vec2, rounding float32, corners FlagCorner)

func (*DrawList) PathStroke

func (dl *DrawList) PathStroke(color uint32, thickness float32, closed bool)

default: thickness=1.0

func (*DrawList) PopClipRect

func (dl *DrawList) PopClipRect()

func (*DrawList) PopTextureId

func (dl *DrawList) PopTextureId()

func (*DrawList) PrimQuadUV

func (dl *DrawList) PrimQuadUV(a, b, c, d f32.Vec2, uva, uvb, uvc, uvd f32.Vec2, color uint32)

func (*DrawList) PrimRect

func (dl *DrawList) PrimRect(min, max f32.Vec2, color uint32)

func (*DrawList) PrimRectUV

func (dl *DrawList) PrimRectUV(a, c f32.Vec2, uva, uvc f32.Vec2, color uint32)

func (*DrawList) PrimReserve

func (dl *DrawList) PrimReserve(idxCount, vtxCount int)

primitive operation, auto scale by 1024

func (*DrawList) PushClipRect

func (dl *DrawList) PushClipRect(min, max f32.Vec2, intersectCurrentClip bool)

Clip 相关的操作

func (*DrawList) PushClipRectFullScreen

func (dl *DrawList) PushClipRectFullScreen()

func (*DrawList) PushTextureId

func (dl *DrawList) PushTextureId(texId uint16)

func (*DrawList) Size

func (dl *DrawList) Size() (idx, vdx int)

func (*DrawList) UpdateClipRect

func (dl *DrawList) UpdateClipRect()

will result in new draw-call

func (*DrawList) UpdateTextureId

func (dl *DrawList) UpdateTextureId()

type DrawListFlags

type DrawListFlags uint32
const (
	FlagAntiAliasedLine DrawListFlags = iota
	FlagAntiAliasedFill
)

type DrawVert

type DrawVert struct {
	// contains filtered or unexported fields
}

type EventType

type EventType uint8
const (
	EventWentDown EventType = 1 << iota
	EventWentUp
	EventDown
	EventStartDrag
	EventEndDrag
	EventDragging
)

func Button

func Button(id ID, bb Rect, text string, style *ButtonStyle) (event EventType)

Widget: Button

func ImageButton

func ImageButton(id ID, bb Rect, normal, pressed gfx.Tex2D, style *ImageButtonStyle) EventType

func Slider

func Slider(id ID, bb Rect, value *float32, style *SliderStyle) (v EventType)

func (EventType) Down

func (et EventType) Down() bool

func (EventType) Dragging

func (et EventType) Dragging() bool

func (EventType) EndDrag

func (et EventType) EndDrag() bool

func (EventType) JustPressed

func (et EventType) JustPressed() bool

func (EventType) JustReleased

func (et EventType) JustReleased() bool

func (EventType) StartDrag

func (et EventType) StartDrag() bool

type FlagCorner

type FlagCorner uint32

Rounding corner: A: 0x0000 0001 top-left B: 0x0000 0002 top-right C: 0x0000 0004 down-right D: 0x0000 0008 down-left

type FontRender

type FontRender struct {
	*DrawList
	// contains filtered or unexported fields
}

工具结构,负责把字符串转化为顶点.. 拥有所有需要的条件属性

func (*FontRender) RenderText

func (fr *FontRender) RenderText(pos f32.Vec2, text string) (size f32.Vec2)

当前的实现中,不考虑裁切优化,全部绘制所有字符

func (*FontRender) RenderWrapped

func (fr *FontRender) RenderWrapped(pos f32.Vec2, text string, wrapWidth float32) (size f32.Vec2)

type ID

type ID int

每个控件都需要手动传入一个 Id

func GenID

func GenID(name string) ID

返回一个 Id

type IdMap

type IdMap map[string]ID

type ImageButtonStyle

type ImageButtonStyle struct {
	ImageStyle
	Padding
}

type ImageStyle

type ImageStyle struct {
	Tint gfx.Color
}

type InputStyle

type InputStyle struct {
	Visibility
	Color, HintColor uint32
	Size             float32
}

type Padding

type Padding struct {
	Left, Right, Top, Bottom float32
}

type ProgressBarStyle

type ProgressBarStyle struct {
}

type Rect

type Rect struct {
	X, Y float32
	W, H float32
}

UI绘制边界

func (*Rect) InRange

func (r *Rect) InRange(p f32.Vec2) bool

func (*Rect) Offset

func (r *Rect) Offset(dx, dy float32) *Rect

func (*Rect) Scale

func (r *Rect) Scale(skx, sky float32) *Rect

type SliderStyle

type SliderStyle struct {
	Bar, Knob gfx.Color
}

type TextStyle

type TextStyle struct {
	Font      font.Font
	Color     gfx.Color
	Size      float32
	Lines     int
	LineSpace float32
}

func (*TextStyle) SetColor

func (text *TextStyle) SetColor(color gfx.Color) *TextStyle

func (*TextStyle) SetFont

func (text *TextStyle) SetFont(f font.Font) *TextStyle

func (*TextStyle) SetSize

func (text *TextStyle) SetSize(size float32) *TextStyle

type Theme

type Theme struct {
	Text        TextStyle
	Button      ButtonStyle
	Image       ImageStyle
	ImageButton ImageButtonStyle
	Slider      SliderStyle

	// global config..
	Normal  gfx.Color
	Pressed gfx.Color
	// item 之间的空隙
	Spacing float32

	// default font
	font.Font
}
var ThemeDark *Theme
var ThemeLight *Theme

type UIRenderFeature

type UIRenderFeature struct {
	*gfx.MeshRender
	*DrawList
	*gfx.Camera

	Buffer struct {
		// contains filtered or unexported fields
	}
	// contains filtered or unexported fields
}

func (*UIRenderFeature) Draw

func (f *UIRenderFeature) Draw(nodes gfx.RenderNodes)

TODO rotation..

func (*UIRenderFeature) Extract

func (f *UIRenderFeature) Extract(v *gfx.View)

func (*UIRenderFeature) Flush

func (f *UIRenderFeature) Flush()

func (*UIRenderFeature) Register

func (f *UIRenderFeature) Register(rs *gfx.RenderSystem)

func (*UIRenderFeature) SetDrawList

func (f *UIRenderFeature) SetDrawList(dl *DrawList)

type Visibility

type Visibility uint8
const (
	Visible Visibility = iota
	InVisible
	Gone
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL