@@ -101,6 +101,18 @@ pub:
101101 native_rendering bool // Cocoa on macOS/iOS, GDI+ on Windows
102102}
103103
104+ pub enum PenLineType {
105+ solid
106+ dashed
107+ dotted
108+ }
109+
110+ pub struct PenConfig {
111+ color gx.Color
112+ line_type PenLineType = .solid
113+ thickness int = 1
114+ }
115+
104116[heap ]
105117pub struct Context {
106118mut :
@@ -559,34 +571,70 @@ pub fn (gg &Context) end() {
559571 */
560572}
561573
574+ // resize the context's Window
562575pub fn (mut ctx Context) resize (width int , height int ) {
563576 ctx.width = width
564577 ctx.height = height
565578}
566579
580+ // draw_line draws a line between the points provided
567581pub fn (ctx &Context) draw_line (x f32 , y f32 , x2 f32 , y2 f32 , c gx.Color) {
568582 if c.a != 255 {
569583 sgl.load_pipeline (ctx.timage_pip)
570584 }
571- $if ! android {
572- if ctx.scale > 1 {
573- // Make the line more clear on hi dpi screens: draw a rectangle
574- mut width := (x2 - x)
575- mut height := (y2 - y)
576- if width == 0 {
577- width = 1
578- } else if height == 0 {
579- height = 1
585+
586+ ctx.draw_line_with_config (x, y, x2 , y2 , color: c)
587+ }
588+
589+ // draw_line_with_config draws a line between the points provided with the PenConfig
590+ pub fn (ctx &Context) draw_line_with_config (x f32 , y f32 , x2 f32 , y2 f32 , config PenConfig) {
591+ if config.color.a != 255 {
592+ sgl.load_pipeline (ctx.timage_pip)
593+ }
594+
595+ if config.thickness < = 0 {
596+ return
597+ }
598+
599+ nx := x * ctx.scale
600+ ny := y * ctx.scale
601+ nx2 := x2 * ctx.scale
602+ ny2 := y2 * ctx.scale
603+
604+ dx := nx2 - nx
605+ dy := ny2 - ny
606+ length := math.sqrtf (math.powf (x2 - x, 2 ) + math.powf (y2 - y, 2 ))
607+ theta := f32 (math.atan2 (dy, dx))
608+
609+ sgl.push_matrix ()
610+
611+ sgl.translate (nx, ny, 0 )
612+ sgl.rotate (theta, 0 , 0 , 1 )
613+ sgl.translate (- nx, - ny, 0 )
614+
615+ if config.line_type == .solid {
616+ ctx.draw_rect (x, y, length, config.thickness, config.color)
617+ } else {
618+ size := if config.line_type == .dotted { config.thickness } else { config.thickness * 3 }
619+ space := if size == 1 { 2 } else { size }
620+
621+ mut available := length
622+ mut start_x := x
623+
624+ for i := 0 ; available > 0 ; i++ {
625+ if i % 2 == 0 {
626+ ctx.draw_rect (start_x, y, size, config.thickness, config.color)
627+ available - = size
628+ start_x + = size
629+ continue
580630 }
581- ctx.draw_rect (x, y, width, height, c)
582- return
631+
632+ available - = space
633+ start_x + = space
583634 }
584635 }
585- sgl.c4b (c.r, c.g, c.b, c.a)
586- sgl.begin_line_strip ()
587- sgl.v2f (x * ctx.scale, y * ctx.scale)
588- sgl.v2f (x2 * ctx.scale, y2 * ctx.scale)
589- sgl.end ()
636+
637+ sgl.pop_matrix ()
590638}
591639
592640pub fn (ctx &Context) draw_rounded_rect (x f32 , y f32 , w f32 , h f32 , radius f32 , color gx.Color) {
0 commit comments