-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcommandlist.go
110 lines (100 loc) · 2.89 KB
/
commandlist.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package microui
/*============================================================================
** commandlist
**============================================================================*/
// adds a new command with type cmd_type to command_list
func (ctx *Context) PushCommand(cmd_type int) *Command {
cmd := Command{Type: cmd_type}
//expect(uintptr(len(ctx.CommandList))*size+size < MU_COMMANDLIST_SIZE)
cmd.Base.Type = cmd_type
cmd.Idx = len(ctx.CommandList)
ctx.CommandList = append(ctx.CommandList, &cmd)
return &cmd
}
// sets cmd to the next command in command_list, returns true if success
func (ctx *Context) NextCommand(cmd **Command) bool {
if len(ctx.CommandList) == 0 {
return false
}
if *cmd == nil {
*cmd = ctx.CommandList[0]
} else {
*cmd = ctx.CommandList[(*cmd).Idx+1]
}
for (*cmd).Idx < len(ctx.CommandList) {
if (*cmd).Type != MU_COMMAND_JUMP {
return true
}
idx := (*cmd).Jump.DstIdx
if idx > len(ctx.CommandList)-1 {
break
}
*cmd = ctx.CommandList[idx]
}
return false
}
// pushes a new jump command to command_list
func (ctx *Context) PushJump(dstIdx int) int {
cmd := ctx.PushCommand(MU_COMMAND_JUMP)
cmd.Jump.DstIdx = dstIdx
return len(ctx.CommandList) - 1
}
// pushes a new clip command
func (ctx *Context) SetClip(rect Rect) {
cmd := ctx.PushCommand(MU_COMMAND_CLIP)
cmd.Clip.Rect = rect
}
// pushes a new rect command
func (ctx *Context) DrawRect(rect Rect, color Color) {
rect2 := intersect_rects(rect, ctx.GetClipRect())
if rect2.W > 0 && rect2.H > 0 {
cmd := ctx.PushCommand(MU_COMMAND_RECT)
cmd.Rect.Rect = rect2
cmd.Rect.Color = color
}
}
func (ctx *Context) DrawBox(rect Rect, color Color) {
ctx.DrawRect(NewRect(rect.X+1, rect.Y, rect.W-2, 1), color)
ctx.DrawRect(NewRect(rect.X+1, rect.Y+rect.H-1, rect.W-2, 1), color)
ctx.DrawRect(NewRect(rect.X, rect.Y, 1, rect.H), color)
ctx.DrawRect(NewRect(rect.X+rect.W-1, rect.Y, 1, rect.H), color)
}
func (ctx *Context) DrawText(font Font, str string, pos Vec2, color Color) {
rect := NewRect(pos.X, pos.Y, ctx.TextWidth(font, str), ctx.TextHeight(font))
clipped := ctx.CheckClip(rect)
if clipped == MU_CLIP_ALL {
return
}
if clipped == MU_CLIP_PART {
ctx.SetClip(ctx.GetClipRect())
}
// add command
cmd := ctx.PushCommand(MU_COMMAND_TEXT)
cmd.Text.Str = str
cmd.Text.Pos = pos
cmd.Text.Color = color
cmd.Text.Font = font
// reset clipping if it was set
if clipped != 0 {
ctx.SetClip(UnclippedRect)
}
}
func (ctx *Context) DrawIcon(id int, rect Rect, color Color) {
// do clip command if the rect isn't fully contained within the cliprect
clipped := ctx.CheckClip(rect)
if clipped == MU_CLIP_ALL {
return
}
if clipped == MU_CLIP_PART {
ctx.SetClip(ctx.GetClipRect())
}
// do icon command
cmd := ctx.PushCommand(MU_COMMAND_ICON)
cmd.Icon.Id = id
cmd.Icon.Rect = rect
cmd.Icon.Color = color
// reset clipping if it was set
if clipped != 0 {
ctx.SetClip(UnclippedRect)
}
}