Skip to content

Commit

Permalink
Merge branch 'master' into upgrade-cimgui-go-2
Browse files Browse the repository at this point in the history
  • Loading branch information
gucio321 authored Nov 5, 2024
2 parents 106165f + 7ee0ffa commit 0d84011
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 44 deletions.
85 changes: 43 additions & 42 deletions CodeEditor.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
//nolint:gocritic,govet,unused,revive // this file is TODO. We don't want commentedOutCode linter issues here yet.
//nolint:gocritic,govet,revive,wsl // this file is TODO. We don't want commentedOutCode linter issues here yet.
package giu

import (
"fmt"
cte "github.com/AllenDang/cimgui-go/ImGuiColorTextEdit"
"github.com/AllenDang/cimgui-go/imgui"
)

// LanguageDefinition represents code editor's language definition.
type LanguageDefinition byte

// language definitions:.
const (
LanguageDefinitionSQL LanguageDefinition = iota
LanguageDefinitionCPP
LanguageDefinitionLua
LanguageDefinitionC
LanguageDefinitionNone LanguageDefinition = LanguageDefinition(cte.None)
LanguageDefinitionCPP LanguageDefinition = LanguageDefinition(cte.Cpp)
LanguageDefinitionC LanguageDefinition = LanguageDefinition(cte.C)
LanguageDefinitionCs LanguageDefinition = LanguageDefinition(cte.Cs)
LanguageDefinitionPython LanguageDefinition = LanguageDefinition(cte.Python)
LanguageDefinitionLua LanguageDefinition = LanguageDefinition(cte.Lua)
LanguageDefinitionJSON LanguageDefinition = LanguageDefinition(cte.Json)
LanguageDefinitionSQL LanguageDefinition = LanguageDefinition(cte.Sql)
LanguageDefinitionAngelScript LanguageDefinition = LanguageDefinition(cte.AngelScript)
LanguageDefinitionGlsl LanguageDefinition = LanguageDefinition(cte.Glsl)
LanguageDefinitionHlsl LanguageDefinition = LanguageDefinition(cte.Hlsl)
)

var _ Disposable = &codeEditorState{}

type codeEditorState struct {
// editor imgui.TextEditor
editor *cte.TextEditor
}

// Dispose implements Disposable interface.
func (s *codeEditorState) Dispose() {
// noop
s.editor.Destroy()
}

// static check if code editor implements Widget interface.
Expand All @@ -40,57 +48,41 @@ type CodeEditorWidget struct {

// CodeEditor creates new code editor widget.
func CodeEditor() *CodeEditorWidget {
panic("Code Editor is not implemented yet!")

return &CodeEditorWidget{
title: GenAutoID("##CodeEditor"),
}
}

// ID allows to manually set editor's ID.
// It isn't necessary to use it in a normal conditions.

func (ce *CodeEditorWidget) ID(id ID) *CodeEditorWidget {
ce.title = id
return ce
}

// ShowWhitespaces sets if whitespace is shown in code editor.
func (ce *CodeEditorWidget) ShowWhitespaces(s bool) *CodeEditorWidget {
// ce.getState().editor.SetShowWhitespaces(s)
ce.getState().editor.SetShowWhitespacesEnabled(s)
return ce
}

// TabSize sets editor's tab size.
func (ce *CodeEditorWidget) TabSize(size int) *CodeEditorWidget {
// ce.getState().editor.SetTabSize(size)
ce.getState().editor.SetTabSize(int32(size))
return ce
}

// LanguageDefinition sets code editor language definition.

func (ce *CodeEditorWidget) LanguageDefinition(definition LanguageDefinition) *CodeEditorWidget {
// s := ce.getState()
lookup := map[LanguageDefinition]func(){
// LanguageDefinitionSQL: s.editor.SetLanguageDefinitionSQL,
// LanguageDefinitionCPP: s.editor.SetLanguageDefinitionCPP,
// LanguageDefinitionLua: s.editor.SetLanguageDefinitionLua,
// LanguageDefinitionC: s.editor.SetLanguageDefinitionC,
}

setter, correctDefinition := lookup[definition]
if !correctDefinition {
panic(fmt.Sprintf("giu/CodeEditor.go: unknown language definition %d", definition))
}

setter()
s := ce.getState()
s.editor.SetLanguageDefinition(cte.LanguageDefinitionId(definition))

return ce
}

// Text sets editor's text.
func (ce *CodeEditorWidget) Text(str string) *CodeEditorWidget {
// ce.getState().editor.SetText(str)
ce.getState().editor.SetText(str)
return ce
}

Expand All @@ -102,6 +94,7 @@ func (ce *CodeEditorWidget) Text(str string) *CodeEditorWidget {

// HandleKeyboardInputs sets if editor should handle keyboard input.
func (ce *CodeEditorWidget) HandleKeyboardInputs(b bool) *CodeEditorWidget {
panic("not implemented")
// ce.getState().editor.SetHandleKeyboardInputs(b)
return ce
}
Expand All @@ -120,105 +113,113 @@ func (ce *CodeEditorWidget) Border(border bool) *CodeEditorWidget {

// HasSelection returns true if some text is selected.
func (ce *CodeEditorWidget) HasSelection() bool {
// return ce.getState().editor.HasSelection()
return false
return ce.getState().editor.AnyCursorHasSelection()
}

// GetSelectedText returns selected text.
func (ce *CodeEditorWidget) GetSelectedText() string {
panic("not implemented")
// return ce.getState().editor.GetSelectedText()
return ""
}

// GetText returns whole text from editor.
func (ce *CodeEditorWidget) GetText() string {
// return ce.getState().editor.GetText()
return ""
return ce.getState().editor.Text()
}

// GetCurrentLineText returns current line.
func (ce *CodeEditorWidget) GetCurrentLineText() string {
panic("not implemented")
// return ce.getState().editor.GetCurrentLineText()
return ""
}

// GetCursorPos returns cursor position.
// (in characters).
func (ce *CodeEditorWidget) GetCursorPos() (x, y int) {
// return ce.getState().editor.GetCursorPos()
return 0, 0
var px, py int32
ce.getState().editor.CursorPosition(&px, &py)
return int(px), int(py)
}

// GetSelectionStart returns star pos of selection.
func (ce *CodeEditorWidget) GetSelectionStart() (x, y int) {
panic("not implemented")
// return ce.getState().editor.GetSelectionStart()
return 0, 0
}

// InsertText inserts the `text`.
func (ce *CodeEditorWidget) InsertText(text string) {
panic("not implemented")
// ce.getState().editor.InsertText(text)
}

// GetWordUnderCursor returns the word under the cursor.
func (ce *CodeEditorWidget) GetWordUnderCursor() string {
panic("not implemented")
// return ce.getState().editor.GetWordUnderCursor()
return ""
}

// SelectWordUnderCursor selects the word under cursor.
func (ce *CodeEditorWidget) SelectWordUnderCursor() {
panic("not implemented")
// ce.getState().editor.SelectWordUnderCursor()
}

// IsTextChanged returns true if the editable text was changed in the frame.
func (ce *CodeEditorWidget) IsTextChanged() bool {
panic("not implemented")
// return ce.getState().editor.IsTextChanged()
return false
}

// GetScreenCursorPos returns cursor position on the screen.
// (in pixels).
func (ce *CodeEditorWidget) GetScreenCursorPos() (x, y int) {
panic("not implemented")
// return ce.getState().editor.GetScreenCursorPos()
return 0, 0
}

// Copy copies selection.
func (ce *CodeEditorWidget) Copy() {
// ce.getState().editor.Copy()
ce.getState().editor.Copy()
}

// Cut cuts selection.
func (ce *CodeEditorWidget) Cut() {
// ce.getState().editor.Cut()
ce.getState().editor.Cut()
}

// Paste does the same as Ctrl+V.
func (ce *CodeEditorWidget) Paste() {
// ce.getState().editor.Paste()
ce.getState().editor.Paste()
}

// Delete deletes the selection.
func (ce *CodeEditorWidget) Delete() {
panic("not implemented")
// ce.getState().editor.Delete()
}

// Build implements Widget interface.
func (ce *CodeEditorWidget) Build() {
// s := ce.getState()
s := ce.getState()

// register text in font atlas
// Context.FontAtlas.RegisterString(s.editor.GetText())
Context.FontAtlas.RegisterString(s.editor.Text())

// build editor
// s.editor.Render(ce.title, imgui.Vec2{X: ce.width, Y: ce.height}, ce.border)
s.editor.RenderV(string(ce.title), false, imgui.Vec2{X: ce.width, Y: ce.height}, ce.border)
}

func (ce *CodeEditorWidget) getState() (state *codeEditorState) {
if state = GetState[codeEditorState](Context, ce.title); state == nil {
state = &codeEditorState{
// editor: imgui.NewTextEditor(),
editor: cte.NewTextEditor(),
}

SetState(Context, ce.title, state)
Expand Down
5 changes: 3 additions & 2 deletions examples/codeeditor/codeeditor.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//nolint:wsl,gocritic,staticcheck // this is disabled now.
// Package main demonstrates how to use the Code Editor widget.
package main

import (
Expand Down Expand Up @@ -32,12 +32,12 @@ func loop() {
giu.Button("Set Text").OnClick(func() {
editor.Text("Set text")
}),
//nolint:gocritic,wsl // this should be here for documentation and as a reminder.
giu.Button("Set Error Marker").OnClick(func() {
panic("implement me!")
// errMarkers.Clear()
// errMarkers.Insert(1, "Error message")
// fmt.Println("ErrMarkers Size:", errMarkers.Size())

// editor.ErrorMarkers(errMarkers)
}),
),
Expand All @@ -48,6 +48,7 @@ func loop() {
func main() {
wnd := giu.NewMasterWindow("Code Editor", 800, 600, 0)

//nolint:gocritic // should be here for doc
// errMarkers = imgui.NewErrorMarkers()

editor = giu.CodeEditor().
Expand Down

0 comments on commit 0d84011

Please sign in to comment.