private void UpdateToolbar() { var recreateButtons = false; var existingButtons = _gameControlContainer.Children.OfType <Button>().ToList(); var toolbarItems = _toolbarProviders.SelectMany(x => x.GetToolbarItems()); foreach (var te in toolbarItems) { if (!existingButtons.Any(x => (long)x.Userdata == te.Id)) { // Button doesn't exist, need to recreate. recreateButtons = true; break; } } foreach (var eb in existingButtons) { if (!toolbarItems.Any(x => x.Id == (long)eb.Userdata)) { // Button doesn't exist, need to recreate. recreateButtons = true; break; } } if (!recreateButtons) { // Just sync properties. foreach (var eb in existingButtons) { var toolbarItem = toolbarItems.First(x => x.Id == (long)eb.Userdata); eb.Icon = _assetManager.Get <TextureAsset>(toolbarItem.Icon); eb.Enabled = toolbarItem.Enabled; eb.Toggled = toolbarItem.Toggled; } } else { // TODO: Optimize this to only change controls that need to be changed. foreach (var c in _gameControlContainer.Children.ToArray()) { _gameControlContainer.RemoveChild(c); } var size = 0; foreach (var tp in _toolbarProviders) { if (size > 0) { size += 30; } foreach (var te in tp.GetToolbarItems()) { var button = new Button { Icon = _assetManager.Get <TextureAsset>(te.Icon), Enabled = te.Enabled, Toggled = te.Toggled, Userdata = te.Id }; button.Click += (sender, e) => { te.Handler?.Invoke(te); }; _gameControlContainer.AddChild(button, new Rectangle(size, 8, 28, 28)); size += 30; } } _horizontalContainer.SetChildSize(_gameControlContainer, (size - 2).ToString()); } }