summaryrefslogtreecommitdiff
path: root/server/lib/core/state/note_row.dart
blob: 5375da033fde01e5786df6483303abf2a651291f (plain)
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
part of 'game_state.dart';

// ignore_for_file: library_private_types_in_public_api

/// A free-form note that lives in the main list as its own row, styled like a
/// player/monster row but in a user-pickable colour.
///
/// A note can be unlinked (a free-floating row placed wherever the user drags
/// it) or linked to a figure (a character or monster row) by that figure's
/// [id]. When linked it is kept positioned directly beneath its target by
/// [GameState._reflowNoteRows]. For a monster target it may additionally be
/// pinned to a specific [standeeNr] to annotate one standee rather than the
/// whole group.
class NoteRow extends ListItemData {
  // Slate tone, clearly distinct from character/monster rows.
  static const int defaultColor = 0xFF4E5D6C;

  String _text = "";
  int _color = defaultColor;
  // "" means unlinked.
  String _linkedId = "";
  // 0 means "whole group / not standee-specific".
  int _standeeNr = 0;

  String get text => _text;
  int get color => _color;
  String get linkedId => _linkedId;
  int get standeeNr => _standeeNr;

  bool get isLinked => _linkedId.isNotEmpty;

  NoteRow(String anId) {
    id = anId;
  }

  NoteRow.create(
    String anId, {
    String text = "",
    int color = defaultColor,
    String linkedId = "",
    int standeeNr = 0,
  }) {
    id = anId;
    _text = text;
    _color = color;
    _linkedId = linkedId;
    _standeeNr = standeeNr;
  }

  NoteRow.fromJson(Map<String, dynamic> json) {
    id = json['id'] as String;
    _applyJson(json);
  }

  /// Updates fields in-place (preserving object identity across network
  /// syncs).
  void updateFromJson(Map<String, dynamic> json) {
    _applyJson(json);
  }

  void _applyJson(Map<String, dynamic> json) {
    _text = json['text'] as String? ?? "";
    _color = json['color'] as int? ?? defaultColor;
    _linkedId = json['linkedId'] as String? ?? "";
    _standeeNr = json['standeeNr'] as int? ?? 0;
  }

  void setText(_StateModifier _, String value) {
    _text = value;
  }

  void setColor(_StateModifier _, int value) {
    _color = value;
  }

  /// Links the note to [linkedId] (a figure id; "" to unlink) and, for monster
  /// targets, an optional [standeeNr] (0 for the whole group).
  void setLink(_StateModifier _, String linkedId, int standeeNr) {
    _linkedId = linkedId;
    _standeeNr = linkedId.isEmpty ? 0 : standeeNr;
  }

  @override
  Map<String, dynamic> toJson() => {
    'id': id,
    // Discriminator used by GameSaveState.load to rebuild the right type.
    'noteRow': true,
    'text': _text,
    'color': _color,
    'linkedId': _linkedId,
    'standeeNr': _standeeNr,
  };

  @override
  String toString() => json.encode(toJson());
}