github.com/gop9/olt@v0.0.0-20200202132135-d956aad50b08/framework/command/command.go (about) 1 package command 2 3 import ( 4 "image" 5 "image/color" 6 7 "github.com/gop9/olt/framework/font" 8 "github.com/gop9/olt/framework/rect" 9 ) 10 11 // CommandBuffer is a list of drawing directives. 12 type Buffer struct { 13 Clip rect.Rect 14 Commands []Command 15 } 16 17 var nk_null_rect = rect.Rect{-8192.0, -8192.0, 16384.0, 16384.0} 18 19 func (buffer *Buffer) Reset() { 20 buffer.Clip = nk_null_rect 21 buffer.Commands = buffer.Commands[:0] 22 } 23 24 // Represents one drawing directive. 25 type Command struct { 26 rect.Rect 27 Kind CommandKind 28 Line Line 29 RectFilled RectFilled 30 TriangleFilled TriangleFilled 31 CircleFilled CircleFilled 32 Image Image 33 Text Text 34 } 35 36 type CommandKind uint8 37 38 const ( 39 ScissorCmd CommandKind = iota 40 LineCmd 41 RectFilledCmd 42 TriangleFilledCmd 43 CircleFilledCmd 44 ImageCmd 45 TextCmd 46 ) 47 48 type Line struct { 49 LineThickness uint16 50 Begin image.Point 51 End image.Point 52 Color color.RGBA 53 } 54 55 type RectFilled struct { 56 Rounding uint16 57 Color color.RGBA 58 } 59 60 type TriangleFilled struct { 61 A image.Point 62 B image.Point 63 C image.Point 64 Color color.RGBA 65 } 66 67 type CircleFilled struct { 68 Color color.RGBA 69 } 70 71 type Text struct { 72 Face font.Face 73 Foreground color.RGBA 74 String string 75 } 76 77 type Image struct { 78 Img *image.RGBA 79 } 80 81 func (b *Buffer) PushScissor(r rect.Rect) { 82 b.Clip = r 83 84 if len(b.Commands) > 0 && b.Commands[len(b.Commands)-1].Kind == ScissorCmd { 85 b.Commands[len(b.Commands)-1].Rect = r 86 return 87 } 88 89 var cmd Command 90 cmd.Kind = ScissorCmd 91 cmd.Rect = r 92 93 b.Commands = append(b.Commands, cmd) 94 } 95 96 func (b *Buffer) StrokeLine(p0, p1 image.Point, line_thickness int, c color.RGBA) { 97 var cmd Command 98 cmd.Kind = LineCmd 99 cmd.Line.LineThickness = uint16(line_thickness) 100 cmd.Line.Begin = p0 101 cmd.Line.End = p1 102 cmd.Line.Color = c 103 b.Commands = append(b.Commands, cmd) 104 } 105 106 func (b *Buffer) FillRect(rect rect.Rect, rounding uint16, c color.RGBA) { 107 if c.A == 0 && len(b.Commands) > 0 { 108 return 109 } 110 if !rect.Intersect(&b.Clip) { 111 return 112 } 113 114 var cmd Command 115 cmd.Kind = RectFilledCmd 116 cmd.RectFilled.Rounding = rounding 117 cmd.Rect = rect 118 cmd.RectFilled.Color = c 119 b.Commands = append(b.Commands, cmd) 120 } 121 122 func (b *Buffer) FillCircle(r rect.Rect, c color.RGBA) { 123 if c.A == 0 { 124 return 125 } 126 if !r.Intersect(&b.Clip) { 127 return 128 } 129 130 var cmd Command 131 cmd.Kind = CircleFilledCmd 132 cmd.Rect = r 133 cmd.CircleFilled.Color = c 134 b.Commands = append(b.Commands, cmd) 135 } 136 137 func (b *Buffer) FillTriangle(p0, p1, p2 image.Point, c color.RGBA) { 138 if c.A == 0 { 139 return 140 } 141 if !b.Clip.Contains(p0) || !b.Clip.Contains(p1) || !b.Clip.Contains(p2) { 142 return 143 } 144 145 var cmd Command 146 cmd.Kind = TriangleFilledCmd 147 cmd.TriangleFilled.A = p0 148 cmd.TriangleFilled.B = p1 149 cmd.TriangleFilled.C = p2 150 cmd.TriangleFilled.Color = c 151 b.Commands = append(b.Commands, cmd) 152 } 153 154 func (b *Buffer) DrawText(r rect.Rect, str string, face font.Face, fg color.RGBA) { 155 if len(str) == 0 || (fg.A == 0) { 156 return 157 } 158 if !r.Intersect(&b.Clip) { 159 return 160 } 161 162 var cmd Command 163 cmd.Kind = TextCmd 164 cmd.Rect = r 165 cmd.Text.Foreground = fg 166 cmd.Text.Face = face 167 cmd.Text.String = str 168 b.Commands = append(b.Commands, cmd) 169 } 170 171 func (b *Buffer) DrawImage(r rect.Rect, img *image.RGBA) { 172 if !r.Intersect(&b.Clip) { 173 return 174 } 175 176 var cmd Command 177 cmd.Kind = ImageCmd 178 cmd.Rect = r 179 cmd.Image.Img = img 180 b.Commands = append(b.Commands, cmd) 181 }