dndgamerolls 0.1.10

DnD Game Rolls - D&D dice roller with CLI and 3D visualization using Bevy
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//! UI-related types and components
//!
//! This module contains all UI components: text displays, command input,
//! command history, zoom slider, tab navigation, and character screen.

use bevy::prelude::*;

// ============================================================================
// Tab Navigation
// ============================================================================

/// Current active tab/view
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, States, Hash)]
pub enum AppTab {
    #[default]
    DiceRoller,
    CharacterSheet,
    DndInfo,
    Contributors,
}

/// Resource for UI state
#[derive(Resource, Default)]
pub struct UiState {
    pub active_tab: AppTab,
    pub character_list_open: bool,
    pub selected_character_index: Option<usize>,
    pub editing_field: Option<EditingField>,
    pub show_save_confirmation: bool,
}

/// Which field is currently being edited
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum EditingField {
    CharacterName,
    CharacterClass,
    CharacterRace,
    CharacterLevel,
    AttributeStrength,
    AttributeDexterity,
    AttributeConstitution,
    AttributeIntelligence,
    AttributeWisdom,
    AttributeCharisma,
    ArmorClass,
    Initiative,
    Speed,
    HitPointsCurrent,
    HitPointsMaximum,
    ProficiencyBonus,
    // Skills and saves are handled by name
    Skill(String),
    SavingThrow(String),
    // Label editing (renaming skills/saves)
    SkillLabel(String),       // Editing the name of a skill
    SavingThrowLabel(String), // Editing the name of a saving throw
}

// ============================================================================
// Tab Bar Components
// ============================================================================

/// Marker for the tab bar container
#[derive(Component)]
pub struct TabBar;

/// Marker for a tab button
#[derive(Component)]
pub struct TabButton {
    pub tab: AppTab,
}

// ============================================================================
// Character Screen Components
// ============================================================================

/// Marker for the character screen root
#[derive(Component)]
pub struct CharacterScreenRoot;

// ============================================================================
// DnD Info Screen Components
// ============================================================================

/// Marker for the DnD info screen root
#[derive(Component)]
pub struct DndInfoScreenRoot;

/// Marker for scrollable info content
#[derive(Component)]
pub struct InfoScrollContent;

/// Marker for character list panel
#[derive(Component)]
pub struct CharacterListPanel;

/// Marker for character list item
#[derive(Component)]
pub struct CharacterListItem {
    pub index: usize,
}

/// Marker for the main character stats panel
#[derive(Component)]
pub struct CharacterStatsPanel;

/// Which group can be edited
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum GroupType {
    BasicInfo,
    Attributes,
    Combat,
    SavingThrows,
    Skills,
}

/// Marker for stat group container (e.g., "Basic Info", "Attributes", etc.)
#[derive(Component)]
pub struct StatGroup {
    pub name: String,
    pub group_type: GroupType,
}

/// Marker for group edit button
#[derive(Component)]
pub struct GroupEditButton {
    pub group_type: GroupType,
}

/// Marker for group add button (plus icon, shown in edit mode)
#[derive(Component)]
pub struct GroupAddButton {
    pub group_type: GroupType,
}

/// Marker for delete entry button (shown in edit mode)
#[derive(Component)]
pub struct DeleteEntryButton {
    pub group_type: GroupType,
    pub entry_id: String, // The key/name of the entry to delete
}

/// Marker for editable label (attribute name, skill name, etc.)
#[derive(Component)]
pub struct EditableLabel {
    pub group_type: GroupType,
    pub label_id: String, // e.g., "strength", "acrobatics"
}

/// Resource for tracking which groups are in edit mode
#[derive(Resource, Default)]
pub struct GroupEditState {
    pub editing_groups: std::collections::HashSet<GroupType>,
}

/// Resource for tracking when adding a new entry to a group
#[derive(Resource, Default)]
pub struct AddingEntryState {
    /// Which group we're adding to (None if not adding)
    pub adding_to: Option<GroupType>,
    /// The name being typed for the new entry
    pub new_entry_name: String,
}

/// Marker for the new entry input field
#[derive(Component)]
pub struct NewEntryInput {
    pub group_type: GroupType,
}

/// Marker for the confirm button when adding new entry
#[derive(Component)]
pub struct NewEntryConfirmButton {
    pub group_type: GroupType,
}

/// Marker for the cancel button when adding new entry
#[derive(Component)]
pub struct NewEntryCancelButton {
    pub group_type: GroupType,
}

/// Marker for a clickable label that can be renamed in edit mode
#[derive(Component)]
pub struct EditableLabelButton {
    pub field: EditingField,
    pub current_name: String,
}

/// Marker for the text inside an editable label
#[derive(Component)]
pub struct EditableLabelText {
    pub field: EditingField,
}

/// Marker for editable stat field
#[derive(Component)]
pub struct StatField {
    pub field: EditingField,
    pub is_numeric: bool,
}

/// Marker for editable text field value display
#[derive(Component)]
pub struct StatFieldValue {
    pub field: EditingField,
}

/// Marker for the save button
#[derive(Component)]
pub struct SaveButton;

/// Marker for the new character button
#[derive(Component)]
pub struct NewCharacterButton;

/// Marker for skill row
#[derive(Component)]
pub struct SkillRow {
    pub skill_name: String,
}

/// Marker for proficiency checkbox (button-based)
#[derive(Component)]
pub struct ProficiencyCheckbox {
    pub target: ProficiencyTarget,
}

/// What the proficiency checkbox targets
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ProficiencyTarget {
    Skill(String),
    SavingThrow(String),
}

/// Marker for expertise checkbox
#[derive(Component)]
pub struct ExpertiseCheckbox {
    pub skill_name: String,
}

/// Marker for saving throw row
#[derive(Component)]
pub struct SavingThrowRow {
    pub ability: String,
}

/// Marker for character list item text (for showing asterisk on unsaved changes)
#[derive(Component)]
pub struct CharacterListItemText {
    pub index: usize,
    pub base_name: String,
}

/// Marker for the scrollable content area
#[derive(Component)]
pub struct ScrollableContent;

/// Resource for tracking text input state
#[derive(Resource, Default)]
pub struct TextInputState {
    pub active_field: Option<EditingField>,
    pub current_text: String,
    pub cursor_position: usize,
    /// Fields that have been modified since last save
    pub modified_fields: std::collections::HashSet<EditingField>,
}

// ============================================================================
// Dice Roller Components (existing)
// ============================================================================

/// Marker component for the results text display
#[derive(Component)]
pub struct ResultsText;

/// Component for the command input text display
#[derive(Component)]
pub struct CommandInputText;

/// Resource for storing the current command input
#[derive(Resource, Default)]
pub struct CommandInput {
    pub text: String,
    pub active: bool,
}

/// Component for command history list display
#[derive(Component)]
pub struct CommandHistoryList;

/// Component for individual command history items
#[derive(Component)]
pub struct CommandHistoryItem {
    pub index: usize,
}

/// Resource for storing command history
#[derive(Resource, Default)]
pub struct CommandHistory {
    pub commands: Vec<String>,
    pub selected_index: Option<usize>,
}

impl CommandHistory {
    pub fn add_command(&mut self, cmd: String) {
        // Only add if not already in the list
        if !cmd.trim().is_empty() && !self.commands.contains(&cmd) {
            self.commands.push(cmd);
        }
    }
}

/// Resource for camera zoom level
#[derive(Resource)]
pub struct ZoomState {
    pub level: f32, // 0.0 = closest, 1.0 = farthest
    pub min_distance: f32,
    pub max_distance: f32,
}

impl Default for ZoomState {
    fn default() -> Self {
        Self {
            level: 0.3,        // Start closer (30% of range)
            min_distance: 4.0, // Can zoom in much closer
            max_distance: 25.0,
        }
    }
}

impl ZoomState {
    pub fn get_distance(&self) -> f32 {
        self.min_distance + self.level * (self.max_distance - self.min_distance)
    }
}

/// Component for the zoom slider container
#[derive(Component)]
pub struct ZoomSliderContainer;

/// Component for the zoom slider handle
#[derive(Component)]
pub struct ZoomSliderHandle;

/// Component for the zoom slider track
#[derive(Component)]
pub struct ZoomSliderTrack;

/// Marker for the dice roller view root (to show/hide)
#[derive(Component)]
pub struct DiceRollerRoot;

// ============================================================================
// Quick Roll Panel Components
// ============================================================================

/// Marker for the quick roll panel container
#[derive(Component)]
pub struct QuickRollPanel;

/// Types of quick roll actions
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum QuickRollType {
    Skill(String),
    AbilityCheck(String),
    SavingThrow(String),
}

/// Component for quick roll buttons
#[derive(Component)]
pub struct QuickRollButton {
    pub roll_type: QuickRollType,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_command_history_add() {
        let mut history = CommandHistory::default();
        assert!(history.commands.is_empty());

        history.add_command("--dice 2d6".to_string());
        assert_eq!(history.commands.len(), 1);
        assert_eq!(history.commands[0], "--dice 2d6");

        // Adding same command should not duplicate
        history.add_command("--dice 2d6".to_string());
        assert_eq!(history.commands.len(), 1);

        // Adding different command should add
        history.add_command("--dice 1d20".to_string());
        assert_eq!(history.commands.len(), 2);

        // Empty command should not be added
        history.add_command("".to_string());
        history.add_command("   ".to_string());
        assert_eq!(history.commands.len(), 2);
    }

    #[test]
    fn test_command_input_default() {
        let input = CommandInput::default();
        assert!(input.text.is_empty());
        assert!(!input.active);
    }

    #[test]
    fn test_zoom_state_default() {
        let zoom = ZoomState::default();
        assert_eq!(zoom.level, 0.3);
        assert_eq!(zoom.min_distance, 4.0);
        assert_eq!(zoom.max_distance, 25.0);
    }

    #[test]
    fn test_zoom_state_get_distance() {
        let zoom = ZoomState::default();
        // At level 0.3: 4.0 + 0.3 * (25.0 - 4.0) = 4.0 + 0.3 * 21.0 = 4.0 + 6.3 = 10.3
        assert!((zoom.get_distance() - 10.3).abs() < 0.01);

        let zoom_min = ZoomState {
            level: 0.0,
            min_distance: 4.0,
            max_distance: 25.0,
        };
        assert_eq!(zoom_min.get_distance(), 4.0);

        let zoom_max = ZoomState {
            level: 1.0,
            min_distance: 4.0,
            max_distance: 25.0,
        };
        assert_eq!(zoom_max.get_distance(), 25.0);
    }
}