diff options
| author | historia <[not public]> | 2026-06-29 04:03:38 -0400 |
|---|---|---|
| committer | historia <[not public]> | 2026-06-29 04:03:38 -0400 |
| commit | ba34490aaed5f7c242c2b0453130fefc07d0d5d0 (patch) | |
| tree | db5818757d5080221494cead1c7e61a576d1cb65 /internal/tui/app.go | |
| parent | b1e252c996a6332d391f96624a7d6f149eb097c4 (diff) | |
| download | tui-ascii-mapper-ba34490aaed5f7c242c2b0453130fefc07d0d5d0.tar.gz | |
Diffstat (limited to 'internal/tui/app.go')
| -rw-r--r-- | internal/tui/app.go | 155 |
1 files changed, 155 insertions, 0 deletions
diff --git a/internal/tui/app.go b/internal/tui/app.go new file mode 100644 index 0000000..066fa28 --- /dev/null +++ b/internal/tui/app.go @@ -0,0 +1,155 @@ +package tui + +import ( + "os" + + "tui-ascii-mapper/internal/config" + "tui-ascii-mapper/internal/mapio" + "tui-ascii-mapper/internal/model" + + "github.com/charmbracelet/bubbles/textinput" + tea "github.com/charmbracelet/bubbletea" +) + +type FilePickerState struct { + Files []os.DirEntry + CurDir string + Selected int + PopupX int + PopupY int + ListTop int +} + +type ColorPickerState struct { + Active bool + Cursor model.Point + Selected []string + ForText bool + GridX int + GridY int +} + +type AppModel struct { + map_ *model.Map + rootMap *model.Map + cursor model.Point + offset model.Point + prevCursor model.Point + + tool model.Tool + selected int + brushWidth int + unicode bool + colorMode bool + fillShapes bool + + mode model.Mode + dialog model.DialogType + + ti textinput.Model + + lineStart model.Point + linePreview []model.Point + rectStart model.Point + rectPreview []model.Point + circleCenter model.Point + circlePreview []model.Point + + textEditing bool + textInput textinput.Model + textColor string + movingLabel int + dragLabelOrigin model.Point + dragMouseOrigin model.Point + textCursorStart model.Point + + mouseDown bool + mouseBtn int + mouseStart model.Point + lastPaint model.Point + drawHeld bool + eraseHeld bool + + undo *model.UndoStack + + width int + height int + quitting bool + cfg config.Config + dialogMsg string + + colorPicker *ColorPickerState + dirty bool + undoPosAtSave int + hotkeySelect [10]int + palettePage int + filePicker *FilePickerState +} + +func (m *AppModel) curMap() *model.Map { return m.map_ } + +func (m *AppModel) curPalette() []model.Terrain { + if m.map_ != nil { + return m.map_.Palette + } + return nil +} + +func NewAppModel(cfg config.Config) *AppModel { + palette := make([]model.Terrain, len(cfg.Symbols)) + copy(palette, cfg.Symbols) + root := model.NewMap("untitled", cfg.DefaultMapWidth, cfg.DefaultMapHeight, palette) + + undo := &model.UndoStack{} + undo.Push(root) + undoAtSave := undo.Pos() + + ti := textinput.New() + ti.Placeholder = "" + ti.Prompt = "" + ti.CharLimit = 64 + + textTI := textinput.New() + textTI.Placeholder = "" + textTI.Prompt = "" + textTI.CharLimit = 256 + + return &AppModel{ + map_: root, + rootMap: root, + cursor: model.Point{X: 0, Y: 0}, + prevCursor: model.Point{X: -1, Y: -1}, + tool: model.ToolBrush, + brushWidth: 1, + selected: 0, + unicode: true, + colorMode: true, + mode: model.ModeNormal, + undo: undo, + cfg: cfg, + ti: ti, + textInput: textTI, + movingLabel: -1, + lastPaint: model.Point{X: -1, Y: -1}, + undoPosAtSave: undoAtSave, + } +} + +func (m *AppModel) Init() tea.Cmd { + return tea.Batch( + textinput.Blink, + tea.EnableMouseCellMotion, + ) +} + +func (m *AppModel) loadMapCmd(path string) tea.Cmd { + return func() tea.Msg { + md, err := mapio.DeserializeMap(path) + return mapLoadedMsg{data: md, err: err} + } +} + +type mapLoadedMsg struct { + data *model.Map + err error +} |
