big ol refactor

This commit is contained in:
2025-04-16 12:13:29 +02:00
parent b866ac879e
commit 9d60d5e9cd
10 changed files with 747 additions and 382 deletions
+15 -19
View File
@@ -11,13 +11,9 @@ import (
rl "github.com/gen2brain/raylib-go/raylib"
)
// Local UI constants (these could be moved to a centralized constants package later)
const (
maxMessages = 50
chatMargin = 10 // Margin from screen edges
chatHeight = 200
messageHeight = 20
inputHeight = 30
runeLimit = 256
runeLimit = 256
)
type Chat struct {
@@ -32,7 +28,7 @@ type Chat struct {
func NewChat() *Chat {
return &Chat{
messages: make([]types.ChatMessage, 0, maxMessages),
messages: make([]types.ChatMessage, 0, types.MaxChatMessages),
inputBuffer: make([]rune, 0, runeLimit),
}
}
@@ -44,7 +40,7 @@ func (c *Chat) AddMessage(playerID int32, content string) {
Time: time.Now(),
}
if len(c.messages) >= maxMessages {
if len(c.messages) >= types.MaxChatMessages {
c.messages = c.messages[1:]
}
c.messages = append(c.messages, msg)
@@ -72,14 +68,14 @@ func (c *Chat) HandleServerMessages(messages []*pb.ChatMessage) {
// Only add if it's not already in our history
if len(c.messages) == 0 || c.messages[len(c.messages)-1].Time.UnixNano() < msg.Timestamp {
if len(c.messages) >= maxMessages {
if len(c.messages) >= types.MaxChatMessages {
c.messages = c.messages[1:]
}
c.messages = append(c.messages, localMsg)
log.Printf("Added chat message from %s: %s", msg.Username, msg.Content)
// Scroll to latest message if it's not already visible
visibleMessages := int((chatHeight - inputHeight) / messageHeight)
visibleMessages := int((types.ChatHeight - types.InputHeight) / types.MessageHeight)
if len(c.messages) > visibleMessages {
c.scrollOffset = len(c.messages) - visibleMessages
}
@@ -114,16 +110,16 @@ func (c *Chat) Draw(screenWidth, screenHeight int32) {
defer c.mutex.RUnlock()
// Calculate chat window width based on screen width
chatWindowWidth := screenWidth - (chatMargin * 2)
chatWindowWidth := screenWidth - (types.ChatMargin * 2)
// Draw chat window background
chatX := float32(chatMargin)
chatY := float32(screenHeight - chatHeight - chatMargin)
rl.DrawRectangle(int32(chatX), int32(chatY), chatWindowWidth, chatHeight, rl.ColorAlpha(rl.Black, 0.5))
chatX := float32(types.ChatMargin)
chatY := float32(screenHeight - types.ChatHeight - types.ChatMargin)
rl.DrawRectangle(int32(chatX), int32(chatY), chatWindowWidth, types.ChatHeight, rl.ColorAlpha(rl.Black, 0.5))
// Draw messages from oldest to newest
messageY := chatY + 5
visibleMessages := int((chatHeight - inputHeight) / messageHeight)
visibleMessages := int((types.ChatHeight - types.InputHeight) / types.MessageHeight)
// Auto-scroll to bottom if no manual scrolling has occurred
if c.scrollOffset == 0 {
@@ -145,12 +141,12 @@ func (c *Chat) Draw(screenWidth, screenHeight int32) {
}
text := fmt.Sprintf("%s: %s", msg.Username, msg.Content)
rl.DrawText(text, int32(chatX)+5, int32(messageY), 20, color)
messageY += messageHeight
messageY += types.MessageHeight
}
// Draw input field
inputY := chatY + float32(chatHeight-inputHeight)
rl.DrawRectangle(int32(chatX), int32(inputY), chatWindowWidth, inputHeight, rl.ColorAlpha(rl.White, 0.3))
inputY := chatY + float32(types.ChatHeight-types.InputHeight)
rl.DrawRectangle(int32(chatX), int32(inputY), chatWindowWidth, types.InputHeight, rl.ColorAlpha(rl.White, 0.3))
if c.isTyping {
inputText := string(c.inputBuffer)
rl.DrawText(inputText, int32(chatX)+5, int32(inputY)+5, 20, rl.White)
@@ -168,7 +164,7 @@ func (c *Chat) Update() (string, bool) {
if !c.isTyping {
wheelMove := rl.GetMouseWheelMove()
if wheelMove != 0 {
maxScroll := max(0, len(c.messages)-int((chatHeight-inputHeight)/messageHeight))
maxScroll := max(0, len(c.messages)-int((types.ChatHeight-types.InputHeight)/types.MessageHeight))
c.scrollOffset = clamp(c.scrollOffset-int(wheelMove), 0, maxScroll)
}