Skip to content

Commit

Permalink
GUI: Start rewriting permission combo boxes to identify ranks by perm…
Browse files Browse the repository at this point in the history
…ission level, part 2
  • Loading branch information
UnknownShadow200 committed Mar 31, 2022
1 parent fad5497 commit 9edc54d
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 51 deletions.
25 changes: 9 additions & 16 deletions GUI/GuiPerms.cs
Expand Up @@ -32,40 +32,33 @@ public class GuiRank

internal static class GuiPerms
{
internal static string[] RankNames;
internal static LevelPermission[] RankPerms;
static List<GuiRank> Ranks, RanksRemove;

internal static void UpdateRanks() {
List<string> names = new List<string>(Group.AllRanks.Count);
List<LevelPermission> perms = new List<LevelPermission>(Group.AllRanks.Count);
Ranks = new List<GuiRank>(Group.AllRanks.Count);

foreach (Group group in Group.AllRanks) {
names.Add(group.Name);
perms.Add(group.Permission);

foreach (Group group in Group.AllRanks)
{
Ranks.Add(new GuiRank(group.Name, group.Permission));
}
RankNames = names.ToArray();
RankPerms = perms.ToArray();

RanksRemove = new List<GuiRank>(Ranks);
RanksRemove.Add(new GuiRank("(remove rank)", LevelPermission.Null));
}

internal static LevelPermission GetPermission(ComboBox box, LevelPermission defPerm) {
internal static LevelPermission GetSelectedRank(ComboBox box, LevelPermission defPerm) {
GuiRank rank = (GuiRank)box.SelectedItem;
return rank == null ? defPerm : rank.Permission;
}

internal static void SetDefaultIndex(ComboBox box, LevelPermission perm) {
Group grp = Group.Find(perm);
if (grp == null) {
internal static void SetSelectedRank(ComboBox box, LevelPermission perm) {
List<GuiRank> ranks = (List<GuiRank>)box.DataSource;
GuiRank rank = ranks.Find(r => r.Permission == perm);

if (rank == null) {
box.SelectedIndex = 1;
} else {
int idx = Array.IndexOf<string>(RankNames, grp.Name);
box.SelectedIndex = idx >= 0 ? idx : 1;
box.SelectedItem = rank;
}
}

Expand Down
26 changes: 13 additions & 13 deletions GUI/PropertyWindow/ItemPermsHelper.cs
Expand Up @@ -29,7 +29,7 @@ public sealed class ItemPermsHelper {

public void Update(ItemPerms perms) {
SupressEvents = true;
GuiPerms.SetDefaultIndex(MinBox, perms.MinRank);
GuiPerms.SetSelectedRank(MinBox, perms.MinRank);
SetSpecificPerms(perms.Allowed, AllowBoxes);
SetSpecificPerms(perms.Disallowed, DisallowBoxes);
SupressEvents = false;
Expand All @@ -41,16 +41,16 @@ public sealed class ItemPermsHelper {
}

public void OnMinRankChanged(ComboBox box) {
int idx = box.SelectedIndex;
if (idx == -1 || SupressEvents) return;
GuiRank rank = (GuiRank)box.SelectedItem;
if (rank == null || SupressEvents) return;
ItemPerms curPerms = GetCurPerms();
curPerms.MinRank = GuiPerms.RankPerms[idx];

curPerms.MinRank = rank.Permission;
}

public void OnSpecificChanged(ComboBox box) {
int idx = box.SelectedIndex;
if (idx == -1 || SupressEvents) return;
GuiRank rank = (GuiRank)box.SelectedItem;
if (rank == null || SupressEvents) return;
ItemPerms curPerms = GetCurPerms();

List<LevelPermission> perms;
Expand All @@ -72,23 +72,23 @@ public sealed class ItemPermsHelper {
boxes = AllowBoxes;
}

if (idx == box.Items.Count - 1) {
if (rank.Permission == LevelPermission.Null) {
if (boxIdx >= perms.Count) return;
perms.RemoveAt(boxIdx);

SupressEvents = true;
SetSpecificPerms(perms, boxes);
SupressEvents = false;
} else {
SetSpecific(boxes, boxIdx, perms, idx);
SetSpecific(boxes, boxIdx, perms, rank);
}
}

static void SetSpecific(ComboBox[] boxes, int boxIdx, List<LevelPermission> perms, int idx) {
static void SetSpecific(ComboBox[] boxes, int boxIdx, List<LevelPermission> perms, GuiRank rank) {
if (boxIdx < perms.Count) {
perms[boxIdx] = GuiPerms.RankPerms[idx];
perms[boxIdx] = rank.Permission;
} else {
perms.Add(GuiPerms.RankPerms[idx]);
perms.Add(rank.Permission);
}

// Activate next box
Expand Down Expand Up @@ -119,7 +119,7 @@ public sealed class ItemPermsHelper {
if (permsCount > i) {
box.Visible = true;
box.Enabled = true;
GuiPerms.SetDefaultIndex(box, perms[i]);
GuiPerms.SetSelectedRank(box, perms[i]);
}
}

Expand Down
10 changes: 5 additions & 5 deletions GUI/PropertyWindow/PropertyWindow.Commands.cs
Expand Up @@ -143,7 +143,7 @@ public partial class PropertyWindow : Form {
CommandExtraPerms perms = LookupExtraPerms(extraPermsList[i].CmdName, extraPermsList[i].Num);
if (perms == null) perms = extraPermsList[i];

GuiPerms.SetDefaultIndex(commandExtraBoxes[i], perms.MinRank);
GuiPerms.SetSelectedRank(commandExtraBoxes[i], perms.MinRank);
commandExtraBoxes[i].Visible = true;
commandExtraLabels[i].Text = "+ " + perms.Desc;
commandExtraLabels[i].Visible = true;
Expand All @@ -161,8 +161,8 @@ public partial class PropertyWindow : Form {
void cmd_cmbExtra_SelectedIndexChanged(object sender, EventArgs e) {
ComboBox box = (ComboBox)sender;
if (commandItems.SupressEvents) return;
int idx = box.SelectedIndex;
if (idx == -1) return;
GuiRank rank = (GuiRank)box.SelectedItem;
if (rank == null) return;

int boxIdx = Array.IndexOf<ComboBox>(commandExtraBoxes, box);
CommandExtraPerms orig = extraPermsList[boxIdx];
Expand All @@ -171,8 +171,8 @@ public partial class PropertyWindow : Form {
if (copy == null) {
copy = orig.Copy();
commandExtraPermsChanged.Add(copy);
}
copy.MinRank = GuiPerms.RankPerms[idx];
}
copy.MinRank = rank.Permission;
}
}
}
4 changes: 2 additions & 2 deletions GUI/PropertyWindow/PropertyWindow.Economy.cs
Expand Up @@ -137,7 +137,7 @@ class ThemeCell : DataGridViewComboBoxCell {
eco_numItemPrice.Value = eco_curItem.Price;
Eco_UpdateItemEnables();

GuiPerms.SetDefaultIndex(eco_cmbItemRank, eco_curItem.PurchaseRank);
GuiPerms.SetSelectedRank(eco_cmbItemRank, eco_curItem.PurchaseRank);
}

void eco_cbItem_CheckedChanged(object sender, EventArgs e) {
Expand All @@ -153,7 +153,7 @@ class ThemeCell : DataGridViewComboBoxCell {
const LevelPermission perm = LevelPermission.Guest;
if (eco_curItem == null) return;

eco_curItem.PurchaseRank = GuiPerms.GetPermission(eco_cmbItemRank, perm);
eco_curItem.PurchaseRank = GuiPerms.GetSelectedRank(eco_cmbItemRank, perm);
}


Expand Down
31 changes: 22 additions & 9 deletions GUI/PropertyWindow/PropertyWindow.Ranks.cs
Expand Up @@ -24,19 +24,32 @@ public partial class PropertyWindow : Form {
bool rankSupressEvents = false;

void LoadRankProps() {
GuiPerms.SetDefaultIndex(rank_cmbDefault, Group.DefaultRank.Permission);
GuiPerms.SetDefaultIndex(rank_cmbOsMap, Server.Config.OSPerbuildDefault);
rank_cbTPHigher.Checked = Server.Config.HigherRankTP;
rank_cbSilentAdmins.Checked = Server.Config.AdminsJoinSilently;
rank_cbEmpty.Checked = Server.Config.ListEmptyRanks;
LoadDefaultRank();
GuiPerms.SetRanks(rank_cmbOsMap);
GuiPerms.SetSelectedRank(rank_cmbOsMap, Server.Config.OSPerbuildDefault);

rank_cbTPHigher.Checked = Server.Config.HigherRankTP;
rank_cbSilentAdmins.Checked = Server.Config.AdminsJoinSilently;
rank_cbEmpty.Checked = Server.Config.ListEmptyRanks;
}

void LoadDefaultRank() {
rank_cmbDefault.Items.Clear();
foreach (Group group in Group.GroupList)
{
rank_cmbDefault.Items.Add(group.Name);
}

rank_cmbDefault.SelectedItem = Server.Config.DefaultRankName;
if (rank_cmbDefault.SelectedItem == null) rank_cmbDefault.SelectedIndex = 1; // guest rank (usually) TODO rethink
}

void ApplyRankProps() {
Server.Config.DefaultRankName = rank_cmbDefault.SelectedItem.ToString();
Server.Config.OSPerbuildDefault = GuiPerms.GetPermission(rank_cmbOsMap, LevelPermission.Owner);
Server.Config.HigherRankTP = rank_cbTPHigher.Checked;
Server.Config.DefaultRankName = rank_cmbDefault.SelectedItem.ToString();
Server.Config.OSPerbuildDefault = GuiPerms.GetSelectedRank(rank_cmbOsMap, LevelPermission.Owner);
Server.Config.HigherRankTP = rank_cbTPHigher.Checked;
Server.Config.AdminsJoinSilently = rank_cbSilentAdmins.Checked;
Server.Config.ListEmptyRanks = rank_cbEmpty.Checked;
Server.Config.ListEmptyRanks = rank_cbEmpty.Checked;
}


Expand Down
4 changes: 2 additions & 2 deletions GUI/PropertyWindow/PropertyWindow.Relay.cs
Expand Up @@ -49,7 +49,7 @@ public partial class PropertyWindow : Form {
ToggleIrcSettings(Server.Config.UseIRC);

GuiPerms.SetRanks(irc_cmbRank);
GuiPerms.SetDefaultIndex(irc_cmbRank, Server.Config.IRCControllerRank);
GuiPerms.SetSelectedRank(irc_cmbRank, Server.Config.IRCControllerRank);
irc_cmbVerify.Items.AddRange(Enum.GetNames(typeof(IRCControllerVerify)));
irc_cmbVerify.SelectedIndex = (int)Server.Config.IRCVerify;
irc_txtPrefix.Text = Server.Config.IRCCommandPrefix;
Expand All @@ -69,7 +69,7 @@ public partial class PropertyWindow : Form {
Server.Config.IRCShowWorldChanges = irc_cbWorldChanges.Checked;
Server.Config.IRCShowAFK = irc_cbAFK.Checked;

Server.Config.IRCControllerRank = GuiPerms.GetPermission(irc_cmbRank, LevelPermission.Admin);
Server.Config.IRCControllerRank = GuiPerms.GetSelectedRank(irc_cmbRank, LevelPermission.Admin);
Server.Config.IRCVerify = (IRCControllerVerify)irc_cmbVerify.SelectedIndex;
Server.Config.IRCCommandPrefix = irc_txtPrefix.Text;
}
Expand Down
4 changes: 2 additions & 2 deletions GUI/PropertyWindow/PropertyWindow.Security.cs
Expand Up @@ -24,7 +24,7 @@ public partial class PropertyWindow : Form {
sec_cbVerifyAdmins.Checked = Server.Config.verifyadmins;
sec_cbWhitelist.Checked = Server.Config.WhitelistedOnly;
GuiPerms.SetRanks(sec_cmbVerifyRank);
GuiPerms.SetDefaultIndex(sec_cmbVerifyRank, Server.Config.VerifyAdminsRank);
GuiPerms.SetSelectedRank(sec_cmbVerifyRank, Server.Config.VerifyAdminsRank);
sec_cmbVerifyRank.Enabled = Server.Config.verifyadmins;

sec_cbChatAuto.Checked = Server.Config.ChatSpamCheck;
Expand Down Expand Up @@ -54,7 +54,7 @@ public partial class PropertyWindow : Form {
void ApplySecurityProps() {
Server.Config.LogNotes = sec_cbLogNotes.Checked;
Server.Config.verifyadmins = sec_cbVerifyAdmins.Checked;
Server.Config.VerifyAdminsRank = GuiPerms.GetPermission(sec_cmbVerifyRank, LevelPermission.Operator);
Server.Config.VerifyAdminsRank = GuiPerms.GetSelectedRank(sec_cmbVerifyRank, LevelPermission.Operator);
Server.Config.WhitelistedOnly = sec_cbWhitelist.Checked;

Server.Config.ChatSpamCheck = sec_cbChatAuto.Checked;
Expand Down
2 changes: 0 additions & 2 deletions GUI/PropertyWindow/PropertyWindow.cs
Expand Up @@ -43,8 +43,6 @@ public partial class PropertyWindow : Form
OnStateChangedEvent.Register(HandleStateChanged, Priority.Low);
GuiPerms.UpdateRanks();

rank_cmbDefault.Items.AddRange(GuiPerms.RankNames);
GuiPerms.SetRanks(rank_cmbOsMap);
GuiPerms.SetRanks(blk_cmbMin);
GuiPerms.SetRanks(cmd_cmbMin);

Expand Down

0 comments on commit 9edc54d

Please sign in to comment.