Reputation: 61
How can i make this code to be much smaller in size and more efficient :) ..?. I have made it so far, from a bigger old code, but i think it's still huge.
if (affix == Mod.Affix)
{
Graphics.DrawText(text, textSize, position, Color.White);
switch (levels)
{
case 1:
{
Size level = Graphics.DrawText(text, textSize, position, Color.Yellow);
if (level != new Size())
{
position.Y += level.Height;
}
} break;
case 2:
{
Size level = Graphics.DrawText(text, textSize, position, Color.Red);
if (level != new Size())
{
position.Y += level.Height;
}
} break;
case 3:
{
Size level = Graphics.DrawText(text, textSize, position, Color.Green);
if (level != new Size())
{
position.Y += level.Height;
}
} break;
default:
Size nextLevel = Graphics.DrawText(text, textSize, position, Color.Black);
if (nextLevel != new Size())
{
position.Y += nextLevel.Height;
}
break;
}
}
Thank you in advance!
Upvotes: 0
Views: 2593
Reputation: 217
You can have a predifined mapping "level_to_color" like (for example initialzed in static constructor):
Dictionary<int,Color> _levelToColor = new Dictionary<int,Color>();
_levelToColor.Add(1, Color.Yellow);
_levelToColor.Add(2, Color.Red);
_levelToColor.Add(3, Color.Green);
And then your code might look like:
Color color = _levelToColor.ContainsKey( level ) ? _levelToColor[level] : Color.Black;
Size level = Graphics.DrawText(text, textSize, position, color);
position.Y += level.Height;
Upvotes: 0
Reputation: 6676
if (affix == Mod.Affix)
{
Graphics.DrawText(text, textSize, position, Color.White);
Size level =
(levels == 1) ? Graphics.DrawText(text, textSize, position, Color.Yellow)
:((levels == 2) ? Graphics.DrawText(text, textSize, position, Color.Red)
: ((levels == 3) ? Graphics.DrawText(text, textSize, position, Color.Green)
: Graphics.DrawText(text, textSize, position, Color.Black)));
if (level != new Size())
{
position.Y += nextLevel.Height;
}
}
Upvotes: -2
Reputation: 16393
Use a dictionary to map levels to colors:
private static Dictionary<int, Color> levelColors = new Dictionary<int, Color>
{
{ 1, Color.Yellow },
{ 2, Color.Red },
{ 3, Color.Green }
};
Then you can change your method to just be this:
Color color;
if (!levelColors.TryGetValue(levels, out color)) // try and get the color for the level
{
color = Color.Black; // Default to black if no level color found
}
Size level = Graphics.DrawText(text, textSize, position, color);
if (level != new Size())
{
position.Y += level.Height;
}
That way, you don't need to modify the method as you add/alter level colors, you just update the dictionary.
Upvotes: 4
Reputation: 1594
You are doing the same code for each colour, try this:
switch (levels)
{
case 1:
AddHeight(Color.Yellow);
break;
case 2:
AddHeight(Color.Red);
break;
case 3:
AddHeight(Color.Green);
break;
default:
AddHeight(Color.Black);
break;
}
public void AddHeight(Color color){
Size level = Graphics.DrawText(text, textSize, position, color);
if (level != new Size()) // ???
{
position.Y += level.Height;
}
}
Upvotes: 3
Reputation: 1433
Try something like this:
Color color = new Color();
switch (levels)
{
case 1:
color = Color.Yellow;
break;
case 2:
color = Color.Red;
break;
case 3:
color = Color.Green;
break;
default:
color = Color.Black;
break;
}
Size level = Graphics.DrawText(text, textSize, position, color);
if (level != new Size()) // ???
{
position.Y += level.Height;
}
Upvotes: 4