aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/history/Bookmark.cpp
blob: 1786821e6494e99f448c550f05794b1e433a0072 (plain) (blame)
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
#include "Bookmark.hpp"

#include <openvic-dataloader/v2script/AbstractSyntaxTree.hpp>

#include "openvic-simulation/dataloader/NodeTools.hpp"

#include "types/Date.hpp"
#include "types/IdentifierRegistry.hpp"
#include "utility/Logger.hpp"

using namespace OpenVic;
using namespace OpenVic::NodeTools;

Bookmark::Bookmark(
   size_t new_index,
   std::string_view new_name,
   std::string_view new_description,
   Date new_date,
   uint32_t new_initial_camera_x,
   uint32_t new_initial_camera_y
)
   : HasIdentifier { std::to_string(new_index) },
     name { new_name },
     description { new_description },
     date { new_date },
     initial_camera_x { new_initial_camera_x },
     initial_camera_y { new_initial_camera_y } {
}

std::string_view Bookmark::get_name() const {
   return name;
}

std::string_view Bookmark::get_description() const {
   return description;
}

Date const& Bookmark::get_date() const {
   return date;
}

uint32_t Bookmark::get_initial_camera_x() const {
   return initial_camera_x;
}

uint32_t Bookmark::get_initial_camera_y() const {
   return initial_camera_y;
}

BookmarkManager::BookmarkManager() : bookmarks { "bookmarks" } {}

bool BookmarkManager::add_bookmark(
   std::string_view name,
   std::string_view description,
   Date date,
   uint32_t initial_camera_x,
   uint32_t initial_camera_y
) {
   return bookmarks.add_item({ bookmarks.size(), name, description, date, initial_camera_x, initial_camera_y });
}

bool BookmarkManager::load_bookmark_file(ast::NodeCPtr root) {
   bool ret = expect_dictionary(
      [this](std::string_view key, ast::NodeCPtr value) -> bool {
         if (key != "bookmark") {
            Logger::error("Invalid bookmark declaration ", key);
            return false;
         }

         std::string_view name, description;
         Date date;
         uint32_t initial_camera_x, initial_camera_y;

         bool ret = expect_dictionary_keys(
            "name", ONE_EXACTLY, expect_string(assign_variable_callback(name)),
            "desc", ONE_EXACTLY, expect_string(assign_variable_callback(description)),
            "date", ONE_EXACTLY, expect_date(assign_variable_callback(date)),
            "cameraX", ONE_EXACTLY, expect_uint(assign_variable_callback(initial_camera_x)),
            "cameraY", ONE_EXACTLY, expect_uint(assign_variable_callback(initial_camera_y))
         )(value);

         ret &= add_bookmark(name, description, date, initial_camera_x, initial_camera_y);
         return ret;
      }
   )(root);
   lock_bookmarks();

   return ret;
}