Skip to content

Commit

Permalink
Merge pull request #628 from gucio321/migration
Browse files Browse the repository at this point in the history
cimgui-go Migration
  • Loading branch information
gucio321 authored Sep 22, 2023
2 parents 46b00d8 + 903adab commit fcf5e15
Show file tree
Hide file tree
Showing 58 changed files with 1,599 additions and 1,305 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
- name: Set up Go environment
uses: actions/setup-go@v4
with:
go-version: 1.19.x
go-version: 1.21.x
id: go

- name: Cache Go modules
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/golangci-lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ jobs:
- name: Lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.50.1
version: v1.54.2
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ linters:
enable:
- asciicheck
- bodyclose
- depguard
- dogsled
- dupl
- errcheck
Expand Down
4 changes: 2 additions & 2 deletions Alignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"image"

"github.com/AllenDang/imgui-go"
imgui "github.com/AllenDang/cimgui-go"
)

// These constants holds information about where GetWidgetWidth should proceed their
Expand Down Expand Up @@ -177,7 +177,7 @@ func (a *AlignmentSetter) Build() {
// if you find anything else, please report it on
// https://github.com/AllenDang/giu Any contribution is appreciated!
func GetWidgetWidth(w Widget) (result float32) {
imgui.PushID(GenAutoID("GetWidgetWidthMeasurement"))
imgui.PushIDStr(GenAutoID("GetWidgetWidthMeasurement"))
defer imgui.PopID()

// save cursor position before doing anything
Expand Down
84 changes: 42 additions & 42 deletions Canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"image"
"image/color"

"github.com/AllenDang/imgui-go"
imgui "github.com/AllenDang/cimgui-go"
)

// Canvas represents imgui.DrawList
Expand All @@ -20,103 +20,103 @@ import (
//
// c := &Canvas{imgui.GetXXXDrawList()}
type Canvas struct {
DrawList imgui.DrawList
DrawList *imgui.DrawList
}

// GetCanvas returns current draw list (for current window).
// it will fail if called out of window's layout.
func GetCanvas() *Canvas {
return &Canvas{
DrawList: imgui.GetWindowDrawList(),
DrawList: imgui.WindowDrawList(),
}
}

// AddLine draws a line (from p1 to p2).
func (c *Canvas) AddLine(p1, p2 image.Point, col color.Color, thickness float32) {
c.DrawList.AddLine(ToVec2(p1), ToVec2(p2), ToVec4Color(col), thickness)
c.DrawList.AddLineV(ToVec2(p1), ToVec2(p2), ColorToUint(col), thickness)
}

// DrawFlags represents imgui.DrawFlags.
type DrawFlags int
type DrawFlags imgui.DrawFlags

// draw flags enum:.
const (
DrawFlagsNone DrawFlags = 0
DrawFlagsNone DrawFlags = DrawFlags(imgui.DrawFlagsNone)
// PathStroke(), AddPolyline(): specify that shape should be closed (note: this is always == 1 for legacy reasons).
DrawFlagsClosed DrawFlags = 1 << 0
DrawFlagsClosed DrawFlags = DrawFlags(imgui.DrawFlagsClosed)
// AddRect(), AddRectFilled(), PathRect(): enable rounding top-left corner only (when rounding > 0.0f, we default to all corners).
// Was 0x01.
DrawFlagsRoundCornersTopLeft DrawFlags = 1 << 4
DrawFlagsRoundCornersTopLeft DrawFlags = DrawFlags(imgui.DrawFlagsRoundCornersTopLeft)
// AddRect(), AddRectFilled(), PathRect(): enable rounding top-right corner only (when rounding > 0.0f, we default to all corners).
// Was 0x02.
DrawFlagsRoundCornersTopRight DrawFlags = 1 << 5
DrawFlagsRoundCornersTopRight DrawFlags = DrawFlags(imgui.DrawFlagsRoundCornersTopRight)
// AddRect(), AddRectFilled(), PathRect(): enable rounding bottom-left corner only (when rounding > 0.0f, we default to all corners).
// Was 0x04.
DrawFlagsRoundCornersBottomLeft DrawFlags = 1 << 6
DrawFlagsRoundCornersBottomLeft DrawFlags = DrawFlags(imgui.DrawFlagsRoundCornersBottomLeft)
// AddRect(), AddRectFilled(), PathRect(): enable rounding bottom-right corner only (when rounding > 0.0f,
// we default to all corners). Wax 0x08.
DrawFlagsRoundCornersBottomRight DrawFlags = 1 << 7
DrawFlagsRoundCornersBottomRight DrawFlags = DrawFlags(imgui.DrawFlagsRoundCornersBottomRight)
// AddRect(), AddRectFilled(), PathRect(): disable rounding on all corners (when rounding > 0.0f). This is NOT zero, NOT an implicit flag!
DrawFlagsRoundCornersNone DrawFlags = 1 << 8
DrawFlagsRoundCornersTop DrawFlags = DrawFlagsRoundCornersTopLeft | DrawFlagsRoundCornersTopRight
DrawFlagsRoundCornersBottom DrawFlags = DrawFlagsRoundCornersBottomLeft | DrawFlagsRoundCornersBottomRight
DrawFlagsRoundCornersLeft DrawFlags = DrawFlagsRoundCornersBottomLeft | DrawFlagsRoundCornersTopLeft
DrawFlagsRoundCornersRight DrawFlags = DrawFlagsRoundCornersBottomRight | DrawFlagsRoundCornersTopRight
DrawFlagsRoundCornersAll DrawFlags = DrawFlagsRoundCornersTopLeft | DrawFlagsRoundCornersTopRight |
DrawFlagsRoundCornersBottomLeft | DrawFlagsRoundCornersBottomRight
DrawFlagsRoundCornersNone DrawFlags = DrawFlags(imgui.DrawFlagsRoundCornersNone)
DrawFlagsRoundCornersTop DrawFlags = DrawFlags(imgui.DrawFlagsRoundCornersTop)
DrawFlagsRoundCornersBottom DrawFlags = DrawFlags(imgui.DrawFlagsRoundCornersBottom)
DrawFlagsRoundCornersLeft DrawFlags = DrawFlags(imgui.DrawFlagsRoundCornersLeft)
DrawFlagsRoundCornersRight DrawFlags = DrawFlags(imgui.DrawFlagsRoundCornersRight)
DrawFlagsRoundCornersAll DrawFlags = DrawFlags(imgui.DrawFlagsRoundCornersAll)

// Default to ALL corners if none of the RoundCornersXX flags are specified.
DrawFlagsRoundCornersDefault DrawFlags = DrawFlagsRoundCornersAll
DrawFlagsRoundCornersMask DrawFlags = DrawFlagsRoundCornersAll | DrawFlagsRoundCornersNone
DrawFlagsRoundCornersDefault DrawFlags = DrawFlags(imgui.DrawFlagsRoundCornersDefault)
DrawFlagsRoundCornersMask DrawFlags = DrawFlags(imgui.DrawFlagsRoundCornersMask)
)

// AddRect draws a rectangle.
func (c *Canvas) AddRect(pMin, pMax image.Point, col color.Color, rounding float32, roundingCorners DrawFlags, thickness float32) {
c.DrawList.AddRect(ToVec2(pMin), ToVec2(pMax), ToVec4Color(col), rounding, int(roundingCorners), thickness)
c.DrawList.AddRectV(ToVec2(pMin), ToVec2(pMax), ColorToUint(col), rounding, imgui.DrawFlags(roundingCorners), thickness)
}

// AddRectFilled draws a rectangle filled with `col`.
func (c *Canvas) AddRectFilled(pMin, pMax image.Point, col color.Color, rounding float32, roundingCorners DrawFlags) {
c.DrawList.AddRectFilled(ToVec2(pMin), ToVec2(pMax), ToVec4Color(col), rounding, int(roundingCorners))
c.DrawList.AddRectFilledV(ToVec2(pMin), ToVec2(pMax), ColorToUint(col), rounding, imgui.DrawFlags(roundingCorners))
}

// AddText draws text.
func (c *Canvas) AddText(pos image.Point, col color.Color, text string) {
c.DrawList.AddText(ToVec2(pos), ToVec4Color(col), Context.FontAtlas.RegisterString(text))
c.DrawList.AddTextVec2(ToVec2(pos), ColorToUint(col), Context.FontAtlas.RegisterString(text))
}

// AddBezierCubic draws bezier cubic.
func (c *Canvas) AddBezierCubic(pos0, cp0, cp1, pos1 image.Point, col color.Color, thickness float32, numSegments int) {
c.DrawList.AddBezierCubic(ToVec2(pos0), ToVec2(cp0), ToVec2(cp1), ToVec2(pos1), ToVec4Color(col), thickness, numSegments)
func (c *Canvas) AddBezierCubic(pos0, cp0, cp1, pos1 image.Point, col color.Color, thickness float32, numSegments int32) {
c.DrawList.AddBezierCubicV(ToVec2(pos0), ToVec2(cp0), ToVec2(cp1), ToVec2(pos1), ColorToUint(col), thickness, numSegments)
}

// AddTriangle draws a triangle.
func (c *Canvas) AddTriangle(p1, p2, p3 image.Point, col color.Color, thickness float32) {
c.DrawList.AddTriangle(ToVec2(p1), ToVec2(p2), ToVec2(p3), ToVec4Color(col), thickness)
c.DrawList.AddTriangleV(ToVec2(p1), ToVec2(p2), ToVec2(p3), ColorToUint(col), thickness)
}

// AddTriangleFilled draws a filled triangle.
func (c *Canvas) AddTriangleFilled(p1, p2, p3 image.Point, col color.Color) {
c.DrawList.AddTriangleFilled(ToVec2(p1), ToVec2(p2), ToVec2(p3), ToVec4Color(col))
c.DrawList.AddTriangleFilled(ToVec2(p1), ToVec2(p2), ToVec2(p3), ColorToUint(col))
}

// AddCircle draws a circle.
func (c *Canvas) AddCircle(center image.Point, radius float32, col color.Color, segments int, thickness float32) {
c.DrawList.AddCircle(ToVec2(center), radius, ToVec4Color(col), segments, thickness)
func (c *Canvas) AddCircle(center image.Point, radius float32, col color.Color, segments int32, thickness float32) {
c.DrawList.AddCircleV(ToVec2(center), radius, ColorToUint(col), segments, thickness)
}

// AddCircleFilled draws a filled circle.
func (c *Canvas) AddCircleFilled(center image.Point, radius float32, col color.Color) {
c.DrawList.AddCircleFilled(ToVec2(center), radius, ToVec4Color(col))
c.DrawList.AddCircleFilled(ToVec2(center), radius, ColorToUint(col))
}

// AddQuad draws a quad.
func (c *Canvas) AddQuad(p1, p2, p3, p4 image.Point, col color.Color, thickness float32) {
c.DrawList.AddQuad(ToVec2(p1), ToVec2(p2), ToVec2(p3), ToVec2(p4), ToVec4Color(col), thickness)
c.DrawList.AddQuadV(ToVec2(p1), ToVec2(p2), ToVec2(p3), ToVec2(p4), ColorToUint(col), thickness)
}

// AddQuadFilled draws a filled quad.
func (c *Canvas) AddQuadFilled(p1, p2, p3, p4 image.Point, col color.Color) {
c.DrawList.AddQuadFilled(ToVec2(p1), ToVec2(p2), ToVec2(p3), ToVec2(p4), ToVec4Color(col))
c.DrawList.AddQuadFilled(ToVec2(p1), ToVec2(p2), ToVec2(p3), ToVec2(p4), ColorToUint(col))
}

// Stateful path API, add points then finish with PathFillConvex() or PathStroke().
Expand All @@ -134,29 +134,29 @@ func (c *Canvas) PathLineToMergeDuplicate(pos image.Point) {
}

func (c *Canvas) PathFillConvex(col color.Color) {
c.DrawList.PathFillConvex(ToVec4Color(col))
c.DrawList.PathFillConvex(ColorToUint(col))
}

func (c *Canvas) PathStroke(col color.Color, closed bool, thickness float32) {
c.DrawList.PathStroke(ToVec4Color(col), closed, thickness)
func (c *Canvas) PathStroke(col color.Color, flags DrawFlags, thickness float32) {
c.DrawList.PathStrokeV(ColorToUint(col), imgui.DrawFlags(flags), thickness)
}

func (c *Canvas) PathArcTo(center image.Point, radius, min, max float32, numSegments int) {
c.DrawList.PathArcTo(ToVec2(center), radius, min, max, numSegments)
func (c *Canvas) PathArcTo(center image.Point, radius, min, max float32, numSegments int32) {
c.DrawList.PathArcToV(ToVec2(center), radius, min, max, numSegments)
}

func (c *Canvas) PathArcToFast(center image.Point, radius float32, min12, max12 int) {
func (c *Canvas) PathArcToFast(center image.Point, radius float32, min12, max12 int32) {
c.DrawList.PathArcToFast(ToVec2(center), radius, min12, max12)
}

func (c *Canvas) PathBezierCubicCurveTo(p1, p2, p3 image.Point, numSegments int) {
c.DrawList.PathBezierCubicCurveTo(ToVec2(p1), ToVec2(p2), ToVec2(p3), numSegments)
func (c *Canvas) PathBezierCubicCurveTo(p1, p2, p3 image.Point, numSegments int32) {
c.DrawList.PathBezierCubicCurveToV(ToVec2(p1), ToVec2(p2), ToVec2(p3), numSegments)
}

func (c *Canvas) AddImage(texture *Texture, pMin, pMax image.Point) {
c.DrawList.AddImage(texture.id, ToVec2(pMin), ToVec2(pMax))
c.DrawList.AddImage(texture.ID(), ToVec2(pMin), ToVec2(pMax))
}

func (c *Canvas) AddImageV(texture *Texture, pMin, pMax, uvMin, uvMax image.Point, col color.Color) {
c.DrawList.AddImageV(texture.id, ToVec2(pMin), ToVec2(pMax), ToVec2(uvMin), ToVec2(uvMax), ToVec4Color(col))
c.DrawList.AddImageV(texture.tex.ID(), ToVec2(pMin), ToVec2(pMax), ToVec2(uvMin), ToVec2(uvMax), ColorToUint(col))
}
19 changes: 10 additions & 9 deletions ClickableWidgets.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"image"
"image/color"

"github.com/AllenDang/imgui-go"
imgui "github.com/AllenDang/cimgui-go"
"golang.org/x/image/colornames"
)

Expand Down Expand Up @@ -64,7 +64,7 @@ func (b *ButtonWidget) ID(id string) *ButtonWidget {
// Build implements Widget interface.
func (b *ButtonWidget) Build() {
if b.disabled {
imgui.BeginDisabled(true)
imgui.BeginDisabled()
defer imgui.EndDisabled()
}

Expand Down Expand Up @@ -105,7 +105,7 @@ func (b *ArrowButtonWidget) ID(id string) *ArrowButtonWidget {

// Build implements Widget interface.
func (b *ArrowButtonWidget) Build() {
if imgui.ArrowButton(b.id, uint8(b.dir)) && b.onClick != nil {
if imgui.ArrowButton(b.id, imgui.Dir(b.dir)) && b.onClick != nil {
b.onClick()
}
}
Expand Down Expand Up @@ -224,15 +224,16 @@ func ImageButton(texture *Texture) *ImageButtonWidget {

// Build implements Widget interface.
func (b *ImageButtonWidget) Build() {
if b.texture == nil || b.texture.id == 0 {
if b.texture == nil || b.texture.tex == nil {
return
}

if imgui.ImageButtonV(
b.texture.id,
fmt.Sprintf("%v", b.texture.tex.ID()),
b.texture.tex.ID(),
imgui.Vec2{X: b.width, Y: b.height},
ToVec2(b.uv0), ToVec2(b.uv1),
b.framePadding, ToVec4Color(b.bgColor),
ToVec4Color(b.bgColor),
ToVec4Color(b.tintColor),
) && b.onClick != nil {
b.onClick()
Expand Down Expand Up @@ -406,7 +407,7 @@ func (r *RadioButtonWidget) OnChange(onChange func()) *RadioButtonWidget {

// Build implements Widget interface.
func (r *RadioButtonWidget) Build() {
if imgui.RadioButton(Context.FontAtlas.RegisterString(r.text), r.active) && r.onChange != nil {
if imgui.RadioButtonBool(Context.FontAtlas.RegisterString(r.text), r.active) && r.onChange != nil {
r.onChange()
}
}
Expand Down Expand Up @@ -481,7 +482,7 @@ func (s *SelectableWidget) Build() {
s.flags |= SelectableFlagsAllowDoubleClick
}

if imgui.SelectableV(Context.FontAtlas.RegisterString(s.label), s.selected, int(s.flags), imgui.Vec2{X: s.width, Y: s.height}) && s.onClick != nil {
if imgui.SelectableBoolV(Context.FontAtlas.RegisterString(s.label), s.selected, imgui.SelectableFlags(s.flags), imgui.Vec2{X: s.width, Y: s.height}) && s.onClick != nil {
s.onClick()
}

Expand Down Expand Up @@ -539,7 +540,7 @@ func (t *TreeNodeWidget) Layout(widgets ...Widget) *TreeNodeWidget {

// Build implements Widget interface.
func (t *TreeNodeWidget) Build() {
open := imgui.TreeNodeV(t.label, int(t.flags))
open := imgui.TreeNodeExStrV(t.label, imgui.TreeNodeFlags(t.flags))

if t.eventHandler != nil {
t.eventHandler()
Expand Down
Loading

0 comments on commit fcf5e15

Please sign in to comment.