private void UpdateActionSlot(EntityUid item, ItemActionType itemActionType, ActionSlot actionSlot, ActionAssignment? assignedActionType) { if (!_entityManager.TryGetEntity(item, out var itemEntity)) return; if (_actionManager.TryGet(itemActionType, out var action)) { actionSlot.Assign(action, itemEntity, true); } else { Logger.ErrorS("action", "unrecognized actionType {0}", assignedActionType); actionSlot.Clear(); return; } if (!_actionsComponent.TryGetItemActionState(itemActionType, item, out var actionState)) { // action is no longer tied to an item, this should never happen as we // check this at the start of this method. But just to be safe // we will restore our assignment here to the correct state Logger.ErrorS("action", "coding error, expected actionType {0} to have" + " a state but it didn't", assignedActionType); _actionsComponent.Assignments.AssignSlot(SelectedHotbar, actionSlot.SlotIndex, ActionAssignment.For(itemActionType)); actionSlot.Assign(action); return; } if (!actionState.Enabled) { // just disabled an action we were trying to target with, stop targeting if (SelectingTargetFor?.Action != null && SelectingTargetFor.Action == action) { StopTargeting(); } actionSlot.DisableAction(); } else { // action is currently granted actionSlot.EnableAction(); // if we are targeting with an action now on cooldown, stop targeting if we should if (SelectingTargetFor?.Action != null && SelectingTargetFor.Action == action && SelectingTargetFor.Item == itemEntity && actionState.IsOnCooldown(_gameTiming) && action.DeselectOnCooldown) { StopTargeting(); } } actionSlot.Cooldown = actionState.Cooldown; // check if we need to toggle it if (action.BehaviorType == BehaviorType.Toggle) { actionSlot.ToggledOn = actionState.ToggledOn; } }
private void OnItemPressed(BaseButton.ButtonEventArgs args) { if (args.Button is not ActionMenuItem actionMenuItem) { return; } switch (actionMenuItem.Action) { case ActionPrototype actionPrototype: _actionsComponent.Assignments.AutoPopulate(ActionAssignment.For(actionPrototype.ActionType), _actionsUI.SelectedHotbar); break; case ItemActionPrototype itemActionPrototype: _actionsComponent.Assignments.AutoPopulate(ActionAssignment.For(itemActionPrototype.ActionType), _actionsUI.SelectedHotbar); break; default: Logger.ErrorS("action", "unexpected action prototype {0}", actionMenuItem.Action); break; } _actionsUI.UpdateUI(); }
private void OnItemButtonUp(BaseButton.ButtonEventArgs args) { // note the buttonup only fires on the control that was originally // pressed to initiate the drag, NOT the one we are currently hovering if (args.Event.Function != EngineKeyFunctions.UIClick) { return; } if (UserInterfaceManager.CurrentlyHovered is ActionSlot targetSlot) { if (!_dragDropHelper.IsDragging || _dragDropHelper.Dragged?.Action == null) { _dragDropHelper.EndDrag(); return; } // drag and drop switch (_dragDropHelper.Dragged.Action) { // assign the dragged action to the target slot case ActionPrototype actionPrototype: _actionsComponent.Assignments.AssignSlot(_actionsUI.SelectedHotbar, targetSlot.SlotIndex, ActionAssignment.For(actionPrototype.ActionType)); break; case ItemActionPrototype itemActionPrototype: // the action menu doesn't show us if the action has an associated item, // so when we perform the assignment, we should check if we currently have an unassigned state // for this item and assign it tied to that item if so, otherwise assign it "itemless" // this is not particularly efficient but we don't maintain an index from // item action type to its action states, and this method should be pretty infrequent so it's probably fine var assigned = false; foreach (var(item, itemStates) in _actionsComponent.ItemActionStates()) { foreach (var(actionType, _) in itemStates) { if (actionType != itemActionPrototype.ActionType) { continue; } var assignment = ActionAssignment.For(actionType, item); if (_actionsComponent.Assignments.HasAssignment(assignment)) { continue; } // no assignment for this state, assign tied to the item assigned = true; _actionsComponent.Assignments.AssignSlot(_actionsUI.SelectedHotbar, targetSlot.SlotIndex, assignment); break; } if (assigned) { break; } } if (!assigned) { _actionsComponent.Assignments.AssignSlot(_actionsUI.SelectedHotbar, targetSlot.SlotIndex, ActionAssignment.For(itemActionPrototype.ActionType)); } break; } _actionsUI.UpdateUI(); } _dragDropHelper.EndDrag(); }