Mercurial > repos > blastem
comparison nuklear_ui/blastem_nuklear.c @ 1478:da1dce39e846 nuklear_ui
Refactored save slot related logic to reduce duplication and allow reuse in new UI. Get state loading/saving mostly working in new UI
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Fri, 24 Nov 2017 12:04:02 -0800 |
parents | 1cdd7f492af8 |
children | 77a401044935 |
comparison
equal
deleted
inserted
replaced
1477:1cdd7f492af8 | 1478:da1dce39e846 |
---|---|
6 #include "font.h" | 6 #include "font.h" |
7 #include "../render.h" | 7 #include "../render.h" |
8 #include "../render_sdl.h" | 8 #include "../render_sdl.h" |
9 #include "../util.h" | 9 #include "../util.h" |
10 #include "../paths.h" | 10 #include "../paths.h" |
11 #include "../saves.h" | |
11 #include "../blastem.h" | 12 #include "../blastem.h" |
12 | 13 |
13 static struct nk_context *context; | 14 static struct nk_context *context; |
14 | 15 |
15 typedef void (*view_fun)(struct nk_context *); | 16 typedef void (*view_fun)(struct nk_context *); |
76 typedef struct { | 77 typedef struct { |
77 const char *title; | 78 const char *title; |
78 view_fun next_view; | 79 view_fun next_view; |
79 } menu_item; | 80 } menu_item; |
80 | 81 |
82 static save_slot_info *slots; | |
83 static uint32_t num_slots, selected_slot; | |
84 | |
85 void view_choose_state(struct nk_context *context, uint8_t is_load) | |
86 { | |
87 uint32_t width = render_width(); | |
88 uint32_t height = render_height(); | |
89 if (nk_begin(context, "Slot Picker", nk_rect(0, 0, width, height), 0)) { | |
90 nk_layout_row_static(context, height - 100, width - 60, 1); | |
91 if (nk_group_begin(context, "Select Save Slot", NK_WINDOW_BORDER | NK_WINDOW_TITLE)) { | |
92 nk_layout_row_static(context, 28, width-100, 1); | |
93 if (!slots) { | |
94 slots = get_slot_info(current_system, &num_slots); | |
95 } | |
96 for (uint32_t i = 0; i < num_slots; i++) | |
97 { | |
98 int selected = i == selected_slot; | |
99 nk_selectable_label(context, slots[i].desc, NK_TEXT_ALIGN_LEFT, &selected); | |
100 if (selected && (slots[i].modification_time || !is_load)) { | |
101 selected_slot = i; | |
102 } | |
103 } | |
104 nk_group_end(context); | |
105 } | |
106 nk_layout_row_static(context, 52, width > 600 ? 300 : width / 2, 2); | |
107 if (nk_button_label(context, "Back")) { | |
108 current_view = previous_view; | |
109 } | |
110 if (is_load) { | |
111 if (nk_button_label(context, "Load")) { | |
112 current_system->load_state(current_system, selected_slot); | |
113 current_view = view_play; | |
114 } | |
115 } else { | |
116 if (nk_button_label(context, "Save")) { | |
117 current_system->save_state = selected_slot + 1; | |
118 current_view = view_play; | |
119 } | |
120 } | |
121 nk_end(context); | |
122 } | |
123 } | |
124 | |
125 void view_save_state(struct nk_context *context) | |
126 { | |
127 view_choose_state(context, 0); | |
128 } | |
129 | |
130 void view_load_state(struct nk_context *context) | |
131 { | |
132 view_choose_state(context, 1); | |
133 } | |
134 | |
81 static void menu(struct nk_context *context, uint32_t num_entries, const menu_item *items) | 135 static void menu(struct nk_context *context, uint32_t num_entries, const menu_item *items) |
82 { | 136 { |
83 const uint32_t button_height = 52; | 137 const uint32_t button_height = 52; |
84 const uint32_t ideal_button_width = 300; | 138 const uint32_t ideal_button_width = 300; |
85 const uint32_t button_space = 6; | 139 const uint32_t button_space = 6; |
98 previous_view = current_view; | 152 previous_view = current_view; |
99 current_view = items[i].next_view; | 153 current_view = items[i].next_view; |
100 if (!current_view) { | 154 if (!current_view) { |
101 exit(0); | 155 exit(0); |
102 } | 156 } |
157 if (current_view == view_save_state || current_view == view_load_state) { | |
158 free_slot_info(slots); | |
159 slots = NULL; | |
160 } | |
103 } | 161 } |
104 } | 162 } |
105 nk_layout_space_end(context); | 163 nk_layout_space_end(context); |
106 } | |
107 | |
108 void view_choose_state(struct nk_context *context, uint8_t is_load) | |
109 { | |
110 | |
111 } | |
112 | |
113 void view_save_state(struct nk_context *context) | |
114 { | |
115 view_choose_state(context, 0); | |
116 } | |
117 | |
118 void view_load_state(struct nk_context *context) | |
119 { | |
120 view_choose_state(context, 1); | |
121 } | 164 } |
122 | 165 |
123 void view_pause(struct nk_context *context) | 166 void view_pause(struct nk_context *context) |
124 { | 167 { |
125 static menu_item items[] = { | 168 static menu_item items[] = { |