mirror of
				https://github.com/Zelda64Recomp/Zelda64Recomp.git
				synced 2025-10-30 08:03:03 +00:00 
			
		
		
		
	Enable mod reordering.
This commit is contained in:
		
							parent
							
								
									b2efabf4d8
								
							
						
					
					
						commit
						a4df328a4e
					
				
					 2 changed files with 56 additions and 4 deletions
				
			
		| 
						 | 
					@ -11,7 +11,7 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace recompui {
 | 
					namespace recompui {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
ModEntry::ModEntry(Element *parent, const recomp::mods::ModDetails &details, uint32_t mod_index, ModMenu *mod_menu) : Element(parent, Events(EventType::Click, EventType::Hover, EventType::Focus)) {
 | 
					ModEntry::ModEntry(Element *parent, uint32_t mod_index, ModMenu *mod_menu) : Element(parent, Events(EventType::Click, EventType::Hover, EventType::Focus, EventType::Drag)) {
 | 
				
			||||||
    assert(mod_menu != nullptr);
 | 
					    assert(mod_menu != nullptr);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    this->mod_index = mod_index;
 | 
					    this->mod_index = mod_index;
 | 
				
			||||||
| 
						 | 
					@ -29,6 +29,7 @@ ModEntry::ModEntry(Element *parent, const recomp::mods::ModDetails &details, uin
 | 
				
			||||||
    set_border_color(Color{ 242, 242, 242, 204 });
 | 
					    set_border_color(Color{ 242, 242, 242, 204 });
 | 
				
			||||||
    set_background_color(Color{ 242, 242, 242, 12 });
 | 
					    set_background_color(Color{ 242, 242, 242, 12 });
 | 
				
			||||||
    set_cursor(Cursor::Pointer);
 | 
					    set_cursor(Cursor::Pointer);
 | 
				
			||||||
 | 
					    set_drag(Drag::Drag);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ContextId context = get_current_context();
 | 
					    ContextId context = get_current_context();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -47,7 +48,7 @@ ModEntry::ModEntry(Element *parent, const recomp::mods::ModDetails &details, uin
 | 
				
			||||||
        body_container->set_overflow(Overflow::Hidden);
 | 
					        body_container->set_overflow(Overflow::Hidden);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            name_label = context.create_element<Label>(body_container, details.mod_id, LabelStyle::Normal);
 | 
					            name_label = context.create_element<Label>(body_container, LabelStyle::Normal);
 | 
				
			||||||
            description_label = context.create_element<Label>(body_container, "Short description of mod here.", LabelStyle::Small);
 | 
					            description_label = context.create_element<Label>(body_container, "Short description of mod here.", LabelStyle::Small);
 | 
				
			||||||
        } // body_container
 | 
					        } // body_container
 | 
				
			||||||
    } // this
 | 
					    } // this
 | 
				
			||||||
| 
						 | 
					@ -57,6 +58,14 @@ ModEntry::~ModEntry() {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void ModEntry::set_mod_drag_callback(std::function<void(uint32_t, EventDrag)> callback) {
 | 
				
			||||||
 | 
					    drag_callback = callback;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void ModEntry::set_mod_details(const recomp::mods::ModDetails &details) {
 | 
				
			||||||
 | 
					    name_label->set_text(details.mod_id);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void ModEntry::process_event(const Event& e) {
 | 
					void ModEntry::process_event(const Event& e) {
 | 
				
			||||||
    switch (e.type) {
 | 
					    switch (e.type) {
 | 
				
			||||||
    case EventType::Click:
 | 
					    case EventType::Click:
 | 
				
			||||||
| 
						 | 
					@ -66,6 +75,9 @@ void ModEntry::process_event(const Event& e) {
 | 
				
			||||||
        break;
 | 
					        break;
 | 
				
			||||||
    case EventType::Focus:
 | 
					    case EventType::Focus:
 | 
				
			||||||
        break;
 | 
					        break;
 | 
				
			||||||
 | 
					    case EventType::Drag:
 | 
				
			||||||
 | 
					        drag_callback(mod_index, std::get<EventDrag>(e.variant));
 | 
				
			||||||
 | 
					        break;
 | 
				
			||||||
    default:
 | 
					    default:
 | 
				
			||||||
        break;
 | 
					        break;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					@ -93,6 +105,39 @@ void ModMenu::mod_toggled(bool enabled) {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void ModMenu::mod_dragged(uint32_t mod_index, EventDrag drag) {
 | 
				
			||||||
 | 
					    // Binary search for the drag area.
 | 
				
			||||||
 | 
					    size_t low = 0;
 | 
				
			||||||
 | 
					    size_t high = mod_entries.size();
 | 
				
			||||||
 | 
					    while (low < high) {
 | 
				
			||||||
 | 
					        size_t mid = low + (high - low) / 2;
 | 
				
			||||||
 | 
					        float drag_area_top = mod_entries[mid]->get_absolute_top();
 | 
				
			||||||
 | 
					        if (drag.y < drag_area_top) {
 | 
				
			||||||
 | 
					            high = mid;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        else {
 | 
				
			||||||
 | 
					            low = mid + 1;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    size_t new_index = 0;
 | 
				
			||||||
 | 
					    if (low > 0) {
 | 
				
			||||||
 | 
					        new_index = low - 1;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    switch (drag.phase) {
 | 
				
			||||||
 | 
					    case DragPhase::End:
 | 
				
			||||||
 | 
					        recomp::mods::set_mod_index(game_mod_id, mod_details[mod_index].mod_id, new_index);
 | 
				
			||||||
 | 
					        mod_details = recomp::mods::get_mod_details(game_mod_id);
 | 
				
			||||||
 | 
					        for (size_t i = 0; i < mod_entries.size(); i++) {
 | 
				
			||||||
 | 
					            mod_entries[i]->set_mod_details(mod_details[i]);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        break;
 | 
				
			||||||
 | 
					    default:
 | 
				
			||||||
 | 
					        break;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// TODO remove this once this is migrated to the new system.
 | 
					// TODO remove this once this is migrated to the new system.
 | 
				
			||||||
ContextId sub_menu_context;
 | 
					ContextId sub_menu_context;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -177,7 +222,10 @@ void ModMenu::create_mod_list() {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Create the child elements for the list scroll.
 | 
					    // Create the child elements for the list scroll.
 | 
				
			||||||
    for (size_t mod_index = 0; mod_index < mod_details.size(); mod_index++) {
 | 
					    for (size_t mod_index = 0; mod_index < mod_details.size(); mod_index++) {
 | 
				
			||||||
        mod_entries.emplace_back(context.create_element<ModEntry>(list_scroll_container, mod_details[mod_index], mod_index, this));
 | 
					        ModEntry *mod_entry = context.create_element<ModEntry>(list_scroll_container, mod_index, this);
 | 
				
			||||||
 | 
					        mod_entry->set_mod_drag_callback(std::bind(&ModMenu::mod_dragged, this, std::placeholders::_1, std::placeholders::_2));
 | 
				
			||||||
 | 
					        mod_entry->set_mod_details(mod_details[mod_index]);
 | 
				
			||||||
 | 
					        mod_entries.emplace_back(mod_entry);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    set_active_mod(0);
 | 
					    set_active_mod(0);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -12,8 +12,10 @@ class ModMenu;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class ModEntry : public Element {
 | 
					class ModEntry : public Element {
 | 
				
			||||||
public:
 | 
					public:
 | 
				
			||||||
    ModEntry(Element *parent, const recomp::mods::ModDetails &details, uint32_t mod_index, ModMenu *mod_menu);
 | 
					    ModEntry(Element *parent, uint32_t mod_index, ModMenu *mod_menu);
 | 
				
			||||||
    virtual ~ModEntry();
 | 
					    virtual ~ModEntry();
 | 
				
			||||||
 | 
					    void set_mod_drag_callback(std::function<void(uint32_t, EventDrag)> callback);
 | 
				
			||||||
 | 
					    void set_mod_details(const recomp::mods::ModDetails &details);
 | 
				
			||||||
protected:
 | 
					protected:
 | 
				
			||||||
    virtual void process_event(const Event &e);
 | 
					    virtual void process_event(const Event &e);
 | 
				
			||||||
private:
 | 
					private:
 | 
				
			||||||
| 
						 | 
					@ -23,6 +25,7 @@ private:
 | 
				
			||||||
    Container *body_container = nullptr;
 | 
					    Container *body_container = nullptr;
 | 
				
			||||||
    Label *name_label = nullptr;
 | 
					    Label *name_label = nullptr;
 | 
				
			||||||
    Label *description_label = nullptr;
 | 
					    Label *description_label = nullptr;
 | 
				
			||||||
 | 
					    std::function<void(uint32_t, EventDrag)> drag_callback = nullptr;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class ModMenu : public Element {
 | 
					class ModMenu : public Element {
 | 
				
			||||||
| 
						 | 
					@ -33,6 +36,7 @@ public:
 | 
				
			||||||
private:
 | 
					private:
 | 
				
			||||||
    void refresh_mods();
 | 
					    void refresh_mods();
 | 
				
			||||||
    void mod_toggled(bool enabled);
 | 
					    void mod_toggled(bool enabled);
 | 
				
			||||||
 | 
					    void mod_dragged(uint32_t mod_index, EventDrag drag);
 | 
				
			||||||
    void mod_configure_requested();
 | 
					    void mod_configure_requested();
 | 
				
			||||||
    void mod_enum_option_changed(const std::string &id, uint32_t value);
 | 
					    void mod_enum_option_changed(const std::string &id, uint32_t value);
 | 
				
			||||||
    void mod_string_option_changed(const std::string &id, const std::string &value);
 | 
					    void mod_string_option_changed(const std::string &id, const std::string &value);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue